From 044e81bbd49fea428b1f8238595b57301827b404 Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Mon, 11 Jun 2018 14:47:15 -0700 Subject: [PATCH] channeldb: add DeleteLinkNode method --- channeldb/nodes.go | 18 ++++++++++++++++++ channeldb/nodes_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/channeldb/nodes.go b/channeldb/nodes.go index 3b1e7feed..a845744de 100644 --- a/channeldb/nodes.go +++ b/channeldb/nodes.go @@ -127,6 +127,24 @@ func putLinkNode(nodeMetaBucket *bolt.Bucket, l *LinkNode) error { return nodeMetaBucket.Put(nodePub, b.Bytes()) } +// DeleteLinkNode removes the link node with the given identity from the +// database. +func (d *DB) DeleteLinkNode(identity *btcec.PublicKey) error { + return d.Update(func(tx *bolt.Tx) error { + return d.deleteLinkNode(tx, identity) + }) +} + +func (d *DB) deleteLinkNode(tx *bolt.Tx, identity *btcec.PublicKey) error { + nodeMetaBucket := tx.Bucket(nodeInfoBucket) + if nodeMetaBucket == nil { + return ErrLinkNodesNotFound + } + + pubKey := identity.SerializeCompressed() + return nodeMetaBucket.Delete(pubKey) +} + // FetchLinkNode attempts to lookup the data for a LinkNode based on a target // identity public key. If a particular LinkNode for the passed identity public // key cannot be found, then ErrNodeNotFound if returned. diff --git a/channeldb/nodes_test.go b/channeldb/nodes_test.go index 7f968efe2..755177aa7 100644 --- a/channeldb/nodes_test.go +++ b/channeldb/nodes_test.go @@ -106,3 +106,35 @@ func TestLinkNodeEncodeDecode(t *testing.T) { addr2.String(), node1DB.Addresses[1].String()) } } + +func TestDeleteLinkNode(t *testing.T) { + t.Parallel() + + cdb, cleanUp, err := makeTestDB() + if err != nil { + t.Fatalf("unable to make test database: %v", err) + } + defer cleanUp() + + _, pubKey := btcec.PrivKeyFromBytes(btcec.S256(), key[:]) + addr := &net.TCPAddr{ + IP: net.ParseIP("127.0.0.1"), + Port: 1337, + } + linkNode := cdb.NewLinkNode(wire.TestNet3, pubKey, addr) + if err := linkNode.Sync(); err != nil { + t.Fatalf("unable to write link node to db: %v", err) + } + + if _, err := cdb.FetchLinkNode(pubKey); err != nil { + t.Fatalf("unable to find link node: %v", err) + } + + if err := cdb.DeleteLinkNode(pubKey); err != nil { + t.Fatalf("unable to delete link node from db: %v", err) + } + + if _, err := cdb.FetchLinkNode(pubKey); err == nil { + t.Fatal("should not have found link node in db, but did") + } +}