Skip to content

Commit 61c1d25

Browse files
committed
graph/db: factor out helper code
Here we factor out some of the crud code in TestEdgePolicyMissingMaxHtcl so that we can re-use it later on.
1 parent 0162ee9 commit 61c1d25

File tree

1 file changed

+36
-39
lines changed

1 file changed

+36
-39
lines changed

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) {

0 commit comments

Comments
 (0)