Skip to content

Commit 76f946d

Browse files
committed
graph/db: version IsZombie and update tests
Update testEdgeInsertionDeletion to be run for both protocol versions.
1 parent ea2be9c commit 76f946d

File tree

5 files changed

+49
-66
lines changed

5 files changed

+49
-66
lines changed

graph/db/graph.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ func (c *ChannelGraph) ChannelView() ([]EdgePoint, error) {
735735
func (c *ChannelGraph) IsZombieEdge(chanID uint64) (bool, [33]byte, [33]byte,
736736
error) {
737737

738-
return c.db.IsZombieEdge(chanID)
738+
return c.db.IsZombieEdge(lnwire.GossipVersion1, chanID)
739739
}
740740

741741
// NumZombies returns the current number of zombie channels in the graph.
@@ -806,6 +806,13 @@ func (c *VersionedGraph) FetchChannelEdgesByOutpoint(op *wire.OutPoint) (
806806
return c.db.FetchChannelEdgesByOutpoint(c.v, op)
807807
}
808808

809+
// IsZombieEdge returns whether the edge is considered zombie for this version.
810+
func (c *VersionedGraph) IsZombieEdge(chanID uint64) (bool, [33]byte,
811+
[33]byte, error) {
812+
813+
return c.db.IsZombieEdge(c.v, chanID)
814+
}
815+
809816
// AddrsForNode returns all known addresses for the target node public key.
810817
func (c *VersionedGraph) AddrsForNode(ctx context.Context,
811818
nodePub *btcec.PublicKey) (bool, []net.Addr, error) {

graph/db/graph_test.go

Lines changed: 25 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ var versionedTests = []versionedTest{
138138
name: "alias lookup",
139139
test: testAliasLookup,
140140
},
141+
{
142+
name: "edge insertion deletion",
143+
test: testEdgeInsertionDeletion,
144+
},
141145
{
142146
name: "partial node",
143147
test: testPartialNode,
@@ -488,79 +492,41 @@ func testSourceNode(t *testing.T, v lnwire.GossipVersion) {
488492
compareNodes(t, testNode, sourceNode)
489493
}
490494

491-
// TestEdgeInsertionDeletion tests the basic CRUD operations for channel edges.
492-
func TestEdgeInsertionDeletion(t *testing.T) {
495+
// testEdgeInsertionDeletion tests the basic CRUD operations for channel edges.
496+
func testEdgeInsertionDeletion(t *testing.T, v lnwire.GossipVersion) {
493497
t.Parallel()
494498
ctx := t.Context()
495499

496-
graph := MakeTestGraph(t)
500+
graph := NewVersionedGraph(MakeTestGraph(t), v)
497501

498502
// We'd like to test the insertion/deletion of edges, so we create two
499503
// vertexes to connect.
500-
node1 := createTestVertex(t, lnwire.GossipVersion1)
501-
node2 := createTestVertex(t, lnwire.GossipVersion1)
502-
503-
// In addition to the fake vertexes we create some fake channel
504-
// identifiers.
505-
chanID := uint64(prand.Int63())
506-
outpoint := wire.OutPoint{
507-
Hash: rev,
508-
Index: 9,
509-
}
510-
511-
// Add the new edge to the database, this should proceed without any
512-
// errors.
513-
node1Pub, err := node1.PubKey()
514-
require.NoError(t, err, "unable to generate node key")
515-
node2Pub, err := node2.PubKey()
516-
require.NoError(t, err, "unable to generate node key")
517-
518-
node1Vertex, err := route.NewVertexFromBytes(
519-
node1Pub.SerializeCompressed(),
520-
)
521-
require.NoError(t, err)
522-
node2Vertex, err := route.NewVertexFromBytes(
523-
node2Pub.SerializeCompressed(),
524-
)
525-
require.NoError(t, err)
526-
527-
btcKey1, err := route.NewVertexFromBytes(
528-
node1Pub.SerializeCompressed(),
529-
)
530-
require.NoError(t, err)
531-
btcKey2, err := route.NewVertexFromBytes(
532-
node2Pub.SerializeCompressed(),
533-
)
534-
require.NoError(t, err)
504+
node1 := createTestVertex(t, v)
505+
node2 := createTestVertex(t, v)
535506

536-
proof := models.NewV1ChannelAuthProof(
537-
testSig.Serialize(),
538-
testSig.Serialize(),
539-
testSig.Serialize(),
540-
testSig.Serialize(),
507+
// Create a fake channel and add it to the graph.
508+
const (
509+
blockHeight = 1234
510+
txIndex = 1
511+
txPosition = 0
512+
outPointIndex = 9
541513
)
542514

543-
edgeInfo, err := models.NewV1Channel(
544-
chanID,
545-
*chaincfg.MainNetParams.GenesisHash,
546-
node1Vertex,
547-
node2Vertex,
548-
&models.ChannelV1Fields{
549-
BitcoinKey1Bytes: btcKey1,
550-
BitcoinKey2Bytes: btcKey2,
551-
},
552-
models.WithChanProof(proof),
553-
models.WithChannelPoint(outpoint),
554-
models.WithCapacity(9000),
515+
edgeInfo, shortChanID := createEdge(
516+
v, blockHeight, txIndex, txPosition, outPointIndex, node1, node2,
555517
)
556-
require.NoError(t, err)
518+
chanID := shortChanID.ToUint64()
519+
outpoint := wire.OutPoint{
520+
Hash: rev,
521+
Index: outPointIndex,
522+
}
557523

558524
require.NoError(t, graph.AddChannelEdge(ctx, edgeInfo))
559-
assertEdgeWithNoPoliciesInCache(t, graph, edgeInfo)
525+
assertEdgeWithNoPoliciesInCache(t, graph.ChannelGraph, edgeInfo)
560526

561527
// Show that trying to insert the same channel again will return the
562528
// expected error.
563-
err = graph.AddChannelEdge(ctx, edgeInfo)
529+
err := graph.AddChannelEdge(ctx, edgeInfo)
564530
require.ErrorIs(t, err, ErrEdgeAlreadyExist)
565531

566532
// Ensure that both policies are returned as unknown (nil).
@@ -572,7 +538,7 @@ func TestEdgeInsertionDeletion(t *testing.T) {
572538
// Next, attempt to delete the edge from the database, again this
573539
// should proceed without any issues.
574540
require.NoError(t, graph.DeleteChannelEdges(false, true, chanID))
575-
assertNoEdge(t, graph, chanID)
541+
assertNoEdge(t, graph.ChannelGraph, chanID)
576542

577543
// Ensure that any query attempts to lookup the delete channel edge are
578544
// properly deleted.

graph/db/interfaces.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,8 @@ type Store interface { //nolint:interfacebloat
314314
// IsZombieEdge returns whether the edge is considered zombie. If it is
315315
// a zombie, then the two node public keys corresponding to this edge
316316
// are also returned.
317-
IsZombieEdge(chanID uint64) (bool, [33]byte, [33]byte, error)
317+
IsZombieEdge(v lnwire.GossipVersion, chanID uint64) (bool, [33]byte,
318+
[33]byte, error)
318319

319320
// NumZombies returns the current number of zombie channels in the
320321
// graph.

graph/db/kv_store.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4258,14 +4258,18 @@ func (c *KVStore) markEdgeLiveUnsafe(tx kvdb.RwTx, chanID uint64) error {
42584258
// IsZombieEdge returns whether the edge is considered zombie. If it is a
42594259
// zombie, then the two node public keys corresponding to this edge are also
42604260
// returned.
4261-
func (c *KVStore) IsZombieEdge(chanID uint64) (bool, [33]byte, [33]byte,
4262-
error) {
4261+
func (c *KVStore) IsZombieEdge(v lnwire.GossipVersion,
4262+
chanID uint64) (bool, [33]byte, [33]byte, error) {
42634263

42644264
var (
42654265
isZombie bool
42664266
pubKey1, pubKey2 [33]byte
42674267
)
42684268

4269+
if v != lnwire.GossipVersion1 {
4270+
return false, [33]byte{}, [33]byte{}, ErrVersionNotSupportedForKVDB
4271+
}
4272+
42694273
err := kvdb.View(c.db, func(tx kvdb.RTx) error {
42704274
edges := tx.ReadBucket(edgeBucket)
42714275
if edges == nil {

graph/db/sql_store.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,8 +1814,8 @@ func (s *SQLStore) MarkEdgeLive(chanID uint64) error {
18141814
// returned.
18151815
//
18161816
// NOTE: part of the Store interface.
1817-
func (s *SQLStore) IsZombieEdge(chanID uint64) (bool, [33]byte, [33]byte,
1818-
error) {
1817+
func (s *SQLStore) IsZombieEdge(v lnwire.GossipVersion,
1818+
chanID uint64) (bool, [33]byte, [33]byte, error) {
18191819

18201820
var (
18211821
ctx = context.TODO()
@@ -1824,11 +1824,16 @@ func (s *SQLStore) IsZombieEdge(chanID uint64) (bool, [33]byte, [33]byte,
18241824
chanIDB = channelIDToBytes(chanID)
18251825
)
18261826

1827+
if !isKnownGossipVersion(v) {
1828+
return false, [33]byte{}, [33]byte{},
1829+
fmt.Errorf("unsupported gossip version: %d", v)
1830+
}
1831+
18271832
err := s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error {
18281833
zombie, err := db.GetZombieChannel(
18291834
ctx, sqlc.GetZombieChannelParams{
18301835
Scid: chanIDB,
1831-
Version: int16(lnwire.GossipVersion1),
1836+
Version: int16(v),
18321837
},
18331838
)
18341839
if errors.Is(err, sql.ErrNoRows) {

0 commit comments

Comments
 (0)