Skip to content

Commit ea32aac

Browse files
authored
Merge pull request #10050 from ellemouton/graphMig2-channels
[graph mig 2]: graph/db: migrate graph channels and policies from kvdb to SQL
2 parents fb68f36 + 640caef commit ea32aac

File tree

5 files changed

+762
-51
lines changed

5 files changed

+762
-51
lines changed

docs/release-notes/release-notes-0.20.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ circuit. The indices are only available for forwarding events saved after v0.20.
9797
* [11](https://github.com/lightningnetwork/lnd/pull/9972)
9898
* Add graph SQL migration logic:
9999
* [1](https://github.com/lightningnetwork/lnd/pull/10036)
100+
* [2](https://github.com/lightningnetwork/lnd/pull/10050)
100101

101102
## RPC Updates
102103
* Previously the `RoutingPolicy` would return the inbound fee record in its

graph/db/graph_test.go

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3734,12 +3734,12 @@ func TestDisabledChannelIDs(t *testing.T) {
37343734
}
37353735
}
37363736

3737-
// TestEdgePolicyMissingMaxHtcl tests that if we find a ChannelEdgePolicy in
3737+
// TestEdgePolicyMissingMaxHTLC tests that if we find a ChannelEdgePolicy in
37383738
// the DB that indicates that it should support the htlc_maximum_value_msat
37393739
// field, but it is not part of the opaque data, then we'll handle it as it is
37403740
// unknown. It also checks that we are correctly able to overwrite it when we
37413741
// receive the proper update.
3742-
func TestEdgePolicyMissingMaxHtcl(t *testing.T) {
3742+
func TestEdgePolicyMissingMaxHTLC(t *testing.T) {
37433743
t.Parallel()
37443744
ctx := context.Background()
37453745

@@ -3797,45 +3797,10 @@ func TestEdgePolicyMissingMaxHtcl(t *testing.T) {
37973797
require.ErrorIs(t, err, ErrEdgePolicyOptionalFieldNotFound)
37983798

37993799
// Put the stripped bytes in the DB.
3800-
err = kvdb.Update(boltStore.db, func(tx kvdb.RwTx) error {
3801-
edges := tx.ReadWriteBucket(edgeBucket)
3802-
if edges == nil {
3803-
return ErrEdgeNotFound
3804-
}
3805-
3806-
edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
3807-
if edgeIndex == nil {
3808-
return ErrEdgeNotFound
3809-
}
3810-
3811-
var edgeKey [33 + 8]byte
3812-
copy(edgeKey[:], from)
3813-
byteOrder.PutUint64(edgeKey[33:], edge1.ChannelID)
3814-
3815-
var scratch [8]byte
3816-
var indexKey [8 + 8]byte
3817-
copy(indexKey[:], scratch[:])
3818-
byteOrder.PutUint64(indexKey[8:], edge1.ChannelID)
3819-
3820-
updateIndex, err := edges.CreateBucketIfNotExists(
3821-
edgeUpdateIndexBucket,
3822-
)
3823-
if err != nil {
3824-
return err
3825-
}
3826-
3827-
if err := updateIndex.Put(indexKey[:], nil); err != nil {
3828-
return err
3829-
}
3830-
3831-
return edges.Put(edgeKey[:], stripped)
3832-
}, func() {})
3833-
require.NoError(t, err, "error writing db")
3800+
putSerializedPolicy(t, boltStore.db, from, chanID, stripped)
38343801

38353802
// And add the second, unmodified edge.
3836-
if err := graph.UpdateEdgePolicy(ctx, edge2); err != nil {
3837-
t.Fatalf("unable to update edge: %v", err)
3838-
}
3803+
require.NoError(t, graph.UpdateEdgePolicy(ctx, edge2))
38393804

38403805
// Attempt to fetch the edge and policies from the DB. Since the policy
38413806
// we added is invalid according to the new format, it should be as we
@@ -3870,6 +3835,38 @@ func TestEdgePolicyMissingMaxHtcl(t *testing.T) {
38703835
assertEdgeInfoEqual(t, dbEdgeInfo, edgeInfo)
38713836
}
38723837

3838+
// putSerializedPolicy is a helper function that writes a serialized
3839+
// ChannelEdgePolicy to the edge bucket in the database.
3840+
func putSerializedPolicy(t *testing.T, db kvdb.Backend, from []byte,
3841+
chanID uint64, b []byte) {
3842+
3843+
err := kvdb.Update(db, func(tx kvdb.RwTx) error {
3844+
edges := tx.ReadWriteBucket(edgeBucket)
3845+
require.NotNil(t, edges)
3846+
3847+
edgeIndex := edges.NestedReadWriteBucket(edgeIndexBucket)
3848+
require.NotNil(t, edgeIndex)
3849+
3850+
var edgeKey [33 + 8]byte
3851+
copy(edgeKey[:], from)
3852+
byteOrder.PutUint64(edgeKey[33:], chanID)
3853+
3854+
var scratch [8]byte
3855+
var indexKey [8 + 8]byte
3856+
copy(indexKey[:], scratch[:])
3857+
byteOrder.PutUint64(indexKey[8:], chanID)
3858+
3859+
updateIndex, err := edges.CreateBucketIfNotExists(
3860+
edgeUpdateIndexBucket,
3861+
)
3862+
require.NoError(t, err)
3863+
require.NoError(t, updateIndex.Put(indexKey[:], nil))
3864+
3865+
return edges.Put(edgeKey[:], b)
3866+
}, func() {})
3867+
require.NoError(t, err, "error writing db")
3868+
}
3869+
38733870
// assertNumZombies queries the provided ChannelGraph for NumZombies, and
38743871
// asserts that the returned number is equal to expZombies.
38753872
func assertNumZombies(t *testing.T, graph *ChannelGraph, expZombies uint64) {

graph/db/kv_store.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2844,7 +2844,17 @@ func (c *KVStore) UpdateEdgePolicy(ctx context.Context,
28442844
edgeNotFound = false
28452845
},
28462846
Do: func(tx kvdb.RwTx) error {
2847-
var err error
2847+
// Validate that the ExtraOpaqueData is in fact a valid
2848+
// TLV stream. This is done here instead of within
2849+
// updateEdgePolicy so that updateEdgePolicy can be used
2850+
// by unit tests to recreate the case where we already
2851+
// have nodes persisted with invalid TLV data.
2852+
err := edge.ExtraOpaqueData.ValidateTLV()
2853+
if err != nil {
2854+
return fmt.Errorf("%w: %w",
2855+
ErrParsingExtraTLVBytes, err)
2856+
}
2857+
28482858
from, to, isUpdate1, err = updateEdgePolicy(tx, edge)
28492859
if err != nil {
28502860
log.Errorf("UpdateEdgePolicy faild: %v", err)
@@ -4487,8 +4497,9 @@ func putChanEdgePolicy(edges kvdb.RwBucket, edge *models.ChannelEdgePolicy,
44874497
//
44884498
// TODO(halseth): get rid of these invalid policies in a
44894499
// migration.
4490-
// TODO(elle): complete the above TODO in migration from kvdb
4491-
// to SQL.
4500+
//
4501+
// NOTE: the above TODO was completed in the SQL migration and
4502+
// so such edge cases no longer need to be handled there.
44924503
oldEdgePolicy, err := deserializeChanEdgePolicy(
44934504
bytes.NewReader(edgeBytes),
44944505
)
@@ -4703,12 +4714,6 @@ func serializeChanEdgePolicy(w io.Writer, edge *models.ChannelEdgePolicy,
47034714
}
47044715
}
47054716

4706-
// Validate that the ExtraOpaqueData is in fact a valid TLV stream.
4707-
err = edge.ExtraOpaqueData.ValidateTLV()
4708-
if err != nil {
4709-
return fmt.Errorf("%w: %w", ErrParsingExtraTLVBytes, err)
4710-
}
4711-
47124717
if len(edge.ExtraOpaqueData) > MaxAllowedExtraOpaqueBytes {
47134718
return ErrTooManyExtraOpaqueBytes(len(edge.ExtraOpaqueData))
47144719
}

0 commit comments

Comments
 (0)