2016-12-08 09:47:01 +03:00
|
|
|
package channeldb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-03-16 04:56:25 +03:00
|
|
|
"crypto/sha256"
|
2016-12-08 09:47:01 +03:00
|
|
|
"fmt"
|
|
|
|
"image/color"
|
2017-10-02 17:38:45 +03:00
|
|
|
"math"
|
2017-03-06 04:22:18 +03:00
|
|
|
"math/big"
|
2016-12-08 09:47:01 +03:00
|
|
|
prand "math/rand"
|
|
|
|
"net"
|
|
|
|
"reflect"
|
2016-12-20 03:58:27 +03:00
|
|
|
"runtime"
|
2016-12-08 09:47:01 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
2018-07-22 05:49:54 +03:00
|
|
|
"github.com/coreos/bbolt"
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2016-12-08 09:47:01 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-02-17 12:29:23 +03:00
|
|
|
testAddr = &net.TCPAddr{IP: (net.IP)([]byte{0xA, 0x0, 0x0, 0x1}),
|
|
|
|
Port: 9000}
|
2017-02-23 03:24:22 +03:00
|
|
|
anotherAddr, _ = net.ResolveTCPAddr("tcp",
|
|
|
|
"[2001:db8:85a3:0:0:8a2e:370:7334]:80")
|
|
|
|
testAddrs = []net.Addr{testAddr, anotherAddr}
|
2017-03-06 04:22:18 +03:00
|
|
|
|
2019-09-10 13:27:39 +03:00
|
|
|
testSig = &btcec.Signature{
|
2017-03-06 04:22:18 +03:00
|
|
|
R: new(big.Int),
|
|
|
|
S: new(big.Int),
|
|
|
|
}
|
|
|
|
_, _ = testSig.R.SetString("63724406601629180062774974542967536251589935445068131219452686511677818569431", 10)
|
|
|
|
_, _ = testSig.S.SetString("18801056069249825825291287104931333862866033135609736119018462340006816851118", 10)
|
2017-03-20 12:24:55 +03:00
|
|
|
|
2017-10-11 21:37:54 +03:00
|
|
|
testFeatures = lnwire.NewFeatureVector(nil, lnwire.GlobalFeatures)
|
2016-12-08 09:47:01 +03:00
|
|
|
)
|
|
|
|
|
2019-05-09 11:48:58 +03:00
|
|
|
func createLightningNode(db *DB, priv *btcec.PrivateKey) (*LightningNode, error) {
|
2016-12-08 09:47:01 +03:00
|
|
|
updateTime := prand.Int63()
|
|
|
|
|
|
|
|
pub := priv.PubKey().SerializeCompressed()
|
2018-01-31 07:19:40 +03:00
|
|
|
n := &LightningNode{
|
2017-07-14 22:25:02 +03:00
|
|
|
HaveNodeAnnouncement: true,
|
2018-01-31 07:19:40 +03:00
|
|
|
AuthSigBytes: testSig.Serialize(),
|
2017-07-14 22:25:02 +03:00
|
|
|
LastUpdate: time.Unix(updateTime, 0),
|
|
|
|
Color: color.RGBA{1, 2, 3, 0},
|
|
|
|
Alias: "kek" + string(pub[:]),
|
|
|
|
Features: testFeatures,
|
|
|
|
Addresses: testAddrs,
|
|
|
|
db: db,
|
2018-01-31 07:19:40 +03:00
|
|
|
}
|
|
|
|
copy(n.PubKeyBytes[:], priv.PubKey().SerializeCompressed())
|
|
|
|
|
|
|
|
return n, nil
|
2016-12-08 09:47:01 +03:00
|
|
|
}
|
|
|
|
|
2019-05-09 11:48:58 +03:00
|
|
|
func createTestVertex(db *DB) (*LightningNode, error) {
|
|
|
|
priv, err := btcec.NewPrivateKey(btcec.S256())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return createLightningNode(db, priv)
|
|
|
|
}
|
|
|
|
|
2016-12-08 09:47:01 +03:00
|
|
|
func TestNodeInsertionAndDeletion(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-12-08 09:47:01 +03:00
|
|
|
db, cleanUp, err := makeTestDB()
|
2016-12-22 23:04:41 +03:00
|
|
|
defer cleanUp()
|
2016-12-08 09:47:01 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
graph := db.ChannelGraph()
|
|
|
|
|
|
|
|
// We'd like to test basic insertion/deletion for vertexes from the
|
|
|
|
// graph, so we'll create a test vertex to start with.
|
|
|
|
_, testPub := btcec.PrivKeyFromBytes(btcec.S256(), key[:])
|
|
|
|
node := &LightningNode{
|
2017-07-14 22:25:02 +03:00
|
|
|
HaveNodeAnnouncement: true,
|
2018-01-31 07:19:40 +03:00
|
|
|
AuthSigBytes: testSig.Serialize(),
|
2017-07-14 22:25:02 +03:00
|
|
|
LastUpdate: time.Unix(1232342, 0),
|
|
|
|
Color: color.RGBA{1, 2, 3, 0},
|
|
|
|
Alias: "kek",
|
|
|
|
Features: testFeatures,
|
|
|
|
Addresses: testAddrs,
|
2018-09-01 06:12:45 +03:00
|
|
|
ExtraOpaqueData: []byte("extra new data"),
|
2017-07-14 22:25:02 +03:00
|
|
|
db: db,
|
2016-12-08 09:47:01 +03:00
|
|
|
}
|
2018-01-31 07:19:40 +03:00
|
|
|
copy(node.PubKeyBytes[:], testPub.SerializeCompressed())
|
2016-12-08 09:47:01 +03:00
|
|
|
|
|
|
|
// First, insert the node into the graph DB. This should succeed
|
|
|
|
// without any errors.
|
|
|
|
if err := graph.AddLightningNode(node); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, fetch the node from the database to ensure everything was
|
|
|
|
// serialized properly.
|
|
|
|
dbNode, err := graph.FetchLightningNode(testPub)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to locate node: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-01-31 07:19:40 +03:00
|
|
|
if _, exists, err := graph.HasLightningNode(dbNode.PubKeyBytes); err != nil {
|
2016-12-22 23:49:30 +03:00
|
|
|
t.Fatalf("unable to query for node: %v", err)
|
|
|
|
} else if !exists {
|
|
|
|
t.Fatalf("node should be found but wasn't")
|
|
|
|
}
|
|
|
|
|
2016-12-08 09:47:01 +03:00
|
|
|
// The two nodes should match exactly!
|
2017-03-20 12:24:55 +03:00
|
|
|
if err := compareNodes(node, dbNode); err != nil {
|
|
|
|
t.Fatalf("nodes don't match: %v", err)
|
2016-12-08 09:47:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next, delete the node from the graph, this should purge all data
|
|
|
|
// related to the node.
|
|
|
|
if err := graph.DeleteLightningNode(testPub); err != nil {
|
|
|
|
t.Fatalf("unable to delete node; %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, attempt to fetch the node again. This should fail as the
|
2018-02-07 06:11:11 +03:00
|
|
|
// node should have been deleted from the database.
|
2016-12-08 09:47:01 +03:00
|
|
|
_, err = graph.FetchLightningNode(testPub)
|
|
|
|
if err != ErrGraphNodeNotFound {
|
|
|
|
t.Fatalf("fetch after delete should fail!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-14 22:25:02 +03:00
|
|
|
// TestPartialNode checks that we can add and retrieve a LightningNode where
|
|
|
|
// where only the pubkey is known to the database.
|
|
|
|
func TestPartialNode(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db, cleanUp, err := makeTestDB()
|
|
|
|
defer cleanUp()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
graph := db.ChannelGraph()
|
|
|
|
|
|
|
|
// We want to be able to insert nodes into the graph that only has the
|
|
|
|
// PubKey set.
|
|
|
|
_, testPub := btcec.PrivKeyFromBytes(btcec.S256(), key[:])
|
|
|
|
node := &LightningNode{
|
|
|
|
HaveNodeAnnouncement: false,
|
|
|
|
}
|
2018-01-31 07:19:40 +03:00
|
|
|
copy(node.PubKeyBytes[:], testPub.SerializeCompressed())
|
2017-07-14 22:25:02 +03:00
|
|
|
|
|
|
|
if err := graph.AddLightningNode(node); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, fetch the node from the database to ensure everything was
|
|
|
|
// serialized properly.
|
|
|
|
dbNode, err := graph.FetchLightningNode(testPub)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to locate node: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-01-31 07:19:40 +03:00
|
|
|
if _, exists, err := graph.HasLightningNode(dbNode.PubKeyBytes); err != nil {
|
2017-07-14 22:25:02 +03:00
|
|
|
t.Fatalf("unable to query for node: %v", err)
|
|
|
|
} else if !exists {
|
|
|
|
t.Fatalf("node should be found but wasn't")
|
|
|
|
}
|
|
|
|
|
|
|
|
// The two nodes should match exactly! (with default values for
|
|
|
|
// LastUpdate and db set to satisfy compareNodes())
|
|
|
|
node = &LightningNode{
|
|
|
|
HaveNodeAnnouncement: false,
|
|
|
|
LastUpdate: time.Unix(0, 0),
|
|
|
|
db: db,
|
|
|
|
}
|
2018-01-31 07:19:40 +03:00
|
|
|
copy(node.PubKeyBytes[:], testPub.SerializeCompressed())
|
2017-07-14 22:25:02 +03:00
|
|
|
|
|
|
|
if err := compareNodes(node, dbNode); err != nil {
|
|
|
|
t.Fatalf("nodes don't match: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, delete the node from the graph, this should purge all data
|
|
|
|
// related to the node.
|
|
|
|
if err := graph.DeleteLightningNode(testPub); err != nil {
|
2018-01-31 07:19:40 +03:00
|
|
|
t.Fatalf("unable to delete node: %v", err)
|
2017-07-14 22:25:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, attempt to fetch the node again. This should fail as the
|
2018-02-07 06:11:11 +03:00
|
|
|
// node should have been deleted from the database.
|
2017-07-14 22:25:02 +03:00
|
|
|
_, err = graph.FetchLightningNode(testPub)
|
|
|
|
if err != ErrGraphNodeNotFound {
|
|
|
|
t.Fatalf("fetch after delete should fail!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-08 09:47:01 +03:00
|
|
|
func TestAliasLookup(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-12-08 09:47:01 +03:00
|
|
|
db, cleanUp, err := makeTestDB()
|
2016-12-22 23:04:41 +03:00
|
|
|
defer cleanUp()
|
2016-12-08 09:47:01 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
graph := db.ChannelGraph()
|
|
|
|
|
|
|
|
// We'd like to test the alias index within the database, so first
|
|
|
|
// create a new test node.
|
|
|
|
testNode, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the node to the graph's database, this should also insert an
|
|
|
|
// entry into the alias index for this node.
|
|
|
|
if err := graph.AddLightningNode(testNode); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, attempt to lookup the alias. The alias should exactly match
|
|
|
|
// the one which the test node was assigned.
|
2018-01-31 07:19:40 +03:00
|
|
|
nodePub, err := testNode.PubKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate pubkey: %v", err)
|
|
|
|
}
|
|
|
|
dbAlias, err := graph.LookupAlias(nodePub)
|
2016-12-08 09:47:01 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to find alias: %v", err)
|
|
|
|
}
|
|
|
|
if dbAlias != testNode.Alias {
|
|
|
|
t.Fatalf("aliases don't match, expected %v got %v",
|
|
|
|
testNode.Alias, dbAlias)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that looking up a non-existent alias results in an error.
|
|
|
|
node, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
2018-01-31 07:19:40 +03:00
|
|
|
nodePub, err = node.PubKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate pubkey: %v", err)
|
|
|
|
}
|
|
|
|
_, err = graph.LookupAlias(nodePub)
|
2016-12-08 09:47:01 +03:00
|
|
|
if err != ErrNodeAliasNotFound {
|
|
|
|
t.Fatalf("alias lookup should fail for non-existent pubkey")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSourceNode(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-12-08 09:47:01 +03:00
|
|
|
db, cleanUp, err := makeTestDB()
|
2016-12-22 23:04:41 +03:00
|
|
|
defer cleanUp()
|
2016-12-08 09:47:01 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
graph := db.ChannelGraph()
|
|
|
|
|
|
|
|
// We'd like to test the setting/getting of the source node, so we
|
|
|
|
// first create a fake node to use within the test.
|
|
|
|
testNode, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to fetch the source node, this should return an error as the
|
|
|
|
// source node hasn't yet been set.
|
|
|
|
if _, err := graph.SourceNode(); err != ErrSourceNodeNotSet {
|
|
|
|
t.Fatalf("source node shouldn't be set in new graph")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the source the source node, this should insert the node into the
|
|
|
|
// database in a special way indicating it's the source node.
|
|
|
|
if err := graph.SetSourceNode(testNode); err != nil {
|
|
|
|
t.Fatalf("unable to set source node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the source node from the database, it should exactly match
|
|
|
|
// the one we set above.
|
|
|
|
sourceNode, err := graph.SourceNode()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch source node: %v", err)
|
|
|
|
}
|
2017-03-20 12:24:55 +03:00
|
|
|
if err := compareNodes(testNode, sourceNode); err != nil {
|
|
|
|
t.Fatalf("nodes don't match: %v", err)
|
2016-12-08 09:47:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEdgeInsertionDeletion(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-12-08 09:47:01 +03:00
|
|
|
db, cleanUp, err := makeTestDB()
|
2016-12-22 23:04:41 +03:00
|
|
|
defer cleanUp()
|
2016-12-08 09:47:01 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
graph := db.ChannelGraph()
|
|
|
|
|
|
|
|
// We'd like to test the insertion/deletion of edges, so we create two
|
|
|
|
// vertexes to connect.
|
|
|
|
node1, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
node2, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-04-18 05:03:27 +03:00
|
|
|
// In addition to the fake vertexes we create some fake channel
|
2016-12-08 09:47:01 +03:00
|
|
|
// identifiers.
|
|
|
|
chanID := uint64(prand.Int63())
|
|
|
|
outpoint := wire.OutPoint{
|
|
|
|
Hash: rev,
|
|
|
|
Index: 9,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the new edge to the database, this should proceed without any
|
|
|
|
// errors.
|
2018-01-31 07:19:40 +03:00
|
|
|
node1Pub, err := node1.PubKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate node key: %v", err)
|
|
|
|
}
|
|
|
|
node2Pub, err := node2.PubKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate node key: %v", err)
|
|
|
|
}
|
2017-03-06 04:22:18 +03:00
|
|
|
edgeInfo := ChannelEdgeInfo{
|
2018-01-31 07:19:40 +03:00
|
|
|
ChannelID: chanID,
|
|
|
|
ChainHash: key,
|
2017-03-06 04:22:18 +03:00
|
|
|
AuthProof: &ChannelAuthProof{
|
2018-01-31 07:19:40 +03:00
|
|
|
NodeSig1Bytes: testSig.Serialize(),
|
|
|
|
NodeSig2Bytes: testSig.Serialize(),
|
|
|
|
BitcoinSig1Bytes: testSig.Serialize(),
|
|
|
|
BitcoinSig2Bytes: testSig.Serialize(),
|
2017-03-06 04:22:18 +03:00
|
|
|
},
|
|
|
|
ChannelPoint: outpoint,
|
|
|
|
Capacity: 9000,
|
|
|
|
}
|
2018-01-31 07:19:40 +03:00
|
|
|
copy(edgeInfo.NodeKey1Bytes[:], node1Pub.SerializeCompressed())
|
|
|
|
copy(edgeInfo.NodeKey2Bytes[:], node2Pub.SerializeCompressed())
|
|
|
|
copy(edgeInfo.BitcoinKey1Bytes[:], node1Pub.SerializeCompressed())
|
|
|
|
copy(edgeInfo.BitcoinKey2Bytes[:], node2Pub.SerializeCompressed())
|
2017-03-06 04:22:18 +03:00
|
|
|
|
|
|
|
if err := graph.AddChannelEdge(&edgeInfo); err != nil {
|
2016-12-08 09:47:01 +03:00
|
|
|
t.Fatalf("unable to create channel edge: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-06-18 13:35:22 +03:00
|
|
|
// Ensure that both policies are returned as unknown (nil).
|
|
|
|
_, e1, e2, err := graph.FetchChannelEdgesByID(chanID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch channel edge")
|
|
|
|
}
|
|
|
|
if e1 != nil || e2 != nil {
|
|
|
|
t.Fatalf("channel edges not unknown")
|
|
|
|
}
|
|
|
|
|
2016-12-08 09:47:01 +03:00
|
|
|
// Next, attempt to delete the edge from the database, again this
|
|
|
|
// should proceed without any issues.
|
2019-04-17 23:26:34 +03:00
|
|
|
if err := graph.DeleteChannelEdges(chanID); err != nil {
|
2016-12-08 09:47:01 +03:00
|
|
|
t.Fatalf("unable to delete edge: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-03-06 04:22:18 +03:00
|
|
|
// Ensure that any query attempts to lookup the delete channel edge are
|
|
|
|
// properly deleted.
|
|
|
|
if _, _, _, err := graph.FetchChannelEdgesByOutpoint(&outpoint); err == nil {
|
|
|
|
t.Fatalf("channel edge not deleted")
|
|
|
|
}
|
|
|
|
if _, _, _, err := graph.FetchChannelEdgesByID(chanID); err == nil {
|
|
|
|
t.Fatalf("channel edge not deleted")
|
|
|
|
}
|
2019-03-27 23:06:34 +03:00
|
|
|
isZombie, _, _ := graph.IsZombieEdge(chanID)
|
|
|
|
if !isZombie {
|
|
|
|
t.Fatal("channel edge not marked as zombie")
|
|
|
|
}
|
2017-03-06 04:22:18 +03:00
|
|
|
|
2016-12-08 09:47:01 +03:00
|
|
|
// Finally, attempt to delete a (now) non-existent edge within the
|
|
|
|
// database, this should result in an error.
|
2019-04-17 23:26:34 +03:00
|
|
|
err = graph.DeleteChannelEdges(chanID)
|
2016-12-08 09:47:01 +03:00
|
|
|
if err != ErrEdgeNotFound {
|
|
|
|
t.Fatalf("deleting a non-existent edge should fail!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-17 05:04:29 +03:00
|
|
|
func createEdge(height, txIndex uint32, txPosition uint16, outPointIndex uint32,
|
|
|
|
node1, node2 *LightningNode) (ChannelEdgeInfo, lnwire.ShortChannelID) {
|
|
|
|
|
|
|
|
shortChanID := lnwire.ShortChannelID{
|
|
|
|
BlockHeight: height,
|
|
|
|
TxIndex: txIndex,
|
|
|
|
TxPosition: txPosition,
|
|
|
|
}
|
|
|
|
outpoint := wire.OutPoint{
|
|
|
|
Hash: rev,
|
|
|
|
Index: outPointIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
node1Pub, _ := node1.PubKey()
|
|
|
|
node2Pub, _ := node2.PubKey()
|
|
|
|
edgeInfo := ChannelEdgeInfo{
|
|
|
|
ChannelID: shortChanID.ToUint64(),
|
|
|
|
ChainHash: key,
|
|
|
|
AuthProof: &ChannelAuthProof{
|
|
|
|
NodeSig1Bytes: testSig.Serialize(),
|
|
|
|
NodeSig2Bytes: testSig.Serialize(),
|
|
|
|
BitcoinSig1Bytes: testSig.Serialize(),
|
|
|
|
BitcoinSig2Bytes: testSig.Serialize(),
|
|
|
|
},
|
|
|
|
ChannelPoint: outpoint,
|
|
|
|
Capacity: 9000,
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(edgeInfo.NodeKey1Bytes[:], node1Pub.SerializeCompressed())
|
|
|
|
copy(edgeInfo.NodeKey2Bytes[:], node2Pub.SerializeCompressed())
|
|
|
|
copy(edgeInfo.BitcoinKey1Bytes[:], node1Pub.SerializeCompressed())
|
|
|
|
copy(edgeInfo.BitcoinKey2Bytes[:], node2Pub.SerializeCompressed())
|
|
|
|
|
|
|
|
return edgeInfo, shortChanID
|
|
|
|
}
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// TestDisconnectBlockAtHeight checks that the pruned state of the channel
|
2017-10-02 17:38:45 +03:00
|
|
|
// database is what we expect after calling DisconnectBlockAtHeight.
|
2018-02-07 06:11:11 +03:00
|
|
|
func TestDisconnectBlockAtHeight(t *testing.T) {
|
2017-10-02 17:38:45 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db, cleanUp, err := makeTestDB()
|
|
|
|
defer cleanUp()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
graph := db.ChannelGraph()
|
2018-06-28 06:05:45 +03:00
|
|
|
sourceNode, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create source node: %v", err)
|
|
|
|
}
|
|
|
|
if err := graph.SetSourceNode(sourceNode); err != nil {
|
|
|
|
t.Fatalf("unable to set source node: %v", err)
|
|
|
|
}
|
2017-10-02 17:38:45 +03:00
|
|
|
|
|
|
|
// We'd like to test the insertion/deletion of edges, so we create two
|
|
|
|
// vertexes to connect.
|
|
|
|
node1, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
node2, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// In addition to the fake vertexes we create some fake channel
|
|
|
|
// identifiers.
|
|
|
|
var spendOutputs []*wire.OutPoint
|
|
|
|
var blockHash chainhash.Hash
|
|
|
|
copy(blockHash[:], bytes.Repeat([]byte{1}, 32))
|
|
|
|
|
|
|
|
// Prune the graph a few times to make sure we have entries in the
|
|
|
|
// prune log.
|
|
|
|
_, err = graph.PruneGraph(spendOutputs, &blockHash, 155)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to prune graph: %v", err)
|
|
|
|
}
|
|
|
|
var blockHash2 chainhash.Hash
|
|
|
|
copy(blockHash2[:], bytes.Repeat([]byte{2}, 32))
|
|
|
|
|
|
|
|
_, err = graph.PruneGraph(spendOutputs, &blockHash2, 156)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to prune graph: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll create 3 almost identical edges, so first create a helper
|
|
|
|
// method containing all logic for doing so.
|
|
|
|
|
|
|
|
// Create an edge which has its block height at 156.
|
|
|
|
height := uint32(156)
|
2018-04-17 05:04:29 +03:00
|
|
|
edgeInfo, _ := createEdge(height, 0, 0, 0, node1, node2)
|
2017-10-02 17:38:45 +03:00
|
|
|
|
|
|
|
// Create an edge with block height 157. We give it
|
|
|
|
// maximum values for tx index and position, to make
|
|
|
|
// sure our database range scan get edges from the
|
|
|
|
// entire range.
|
2018-04-17 05:04:29 +03:00
|
|
|
edgeInfo2, _ := createEdge(
|
|
|
|
height+1, math.MaxUint32&0x00ffffff, math.MaxUint16, 1,
|
|
|
|
node1, node2,
|
|
|
|
)
|
2017-10-02 17:38:45 +03:00
|
|
|
|
|
|
|
// Create a third edge, this with a block height of 155.
|
2018-04-17 05:04:29 +03:00
|
|
|
edgeInfo3, _ := createEdge(height-1, 0, 0, 2, node1, node2)
|
2017-10-02 17:38:45 +03:00
|
|
|
|
|
|
|
// Now add all these new edges to the database.
|
|
|
|
if err := graph.AddChannelEdge(&edgeInfo); err != nil {
|
|
|
|
t.Fatalf("unable to create channel edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := graph.AddChannelEdge(&edgeInfo2); err != nil {
|
|
|
|
t.Fatalf("unable to create channel edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := graph.AddChannelEdge(&edgeInfo3); err != nil {
|
|
|
|
t.Fatalf("unable to create channel edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call DisconnectBlockAtHeight, which should prune every channel
|
2018-04-18 05:02:04 +03:00
|
|
|
// that has a funding height of 'height' or greater.
|
2017-10-02 17:38:45 +03:00
|
|
|
removed, err := graph.DisconnectBlockAtHeight(uint32(height))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to prune %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The two edges should have been removed.
|
|
|
|
if len(removed) != 2 {
|
|
|
|
t.Fatalf("expected two edges to be removed from graph, "+
|
|
|
|
"only %d were", len(removed))
|
|
|
|
}
|
|
|
|
if removed[0].ChannelID != edgeInfo.ChannelID {
|
|
|
|
t.Fatalf("expected edge to be removed from graph")
|
|
|
|
}
|
|
|
|
if removed[1].ChannelID != edgeInfo2.ChannelID {
|
|
|
|
t.Fatalf("expected edge to be removed from graph")
|
|
|
|
}
|
|
|
|
|
|
|
|
// The two first edges should be removed from the db.
|
2019-03-27 23:06:57 +03:00
|
|
|
_, _, has, isZombie, err := graph.HasChannelEdge(edgeInfo.ChannelID)
|
2017-10-02 17:38:45 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for edge: %v", err)
|
|
|
|
}
|
|
|
|
if has {
|
|
|
|
t.Fatalf("edge1 was not pruned from the graph")
|
|
|
|
}
|
2019-03-27 23:06:57 +03:00
|
|
|
if isZombie {
|
|
|
|
t.Fatal("reorged edge1 should not be marked as zombie")
|
|
|
|
}
|
|
|
|
_, _, has, isZombie, err = graph.HasChannelEdge(edgeInfo2.ChannelID)
|
2017-10-02 17:38:45 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for edge: %v", err)
|
|
|
|
}
|
|
|
|
if has {
|
|
|
|
t.Fatalf("edge2 was not pruned from the graph")
|
|
|
|
}
|
2019-03-27 23:06:57 +03:00
|
|
|
if isZombie {
|
|
|
|
t.Fatal("reorged edge2 should not be marked as zombie")
|
|
|
|
}
|
2017-10-02 17:38:45 +03:00
|
|
|
|
|
|
|
// Edge 3 should not be removed.
|
2019-03-27 23:06:57 +03:00
|
|
|
_, _, has, isZombie, err = graph.HasChannelEdge(edgeInfo3.ChannelID)
|
2017-10-02 17:38:45 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for edge: %v", err)
|
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
t.Fatalf("edge3 was pruned from the graph")
|
|
|
|
}
|
2019-03-27 23:06:57 +03:00
|
|
|
if isZombie {
|
|
|
|
t.Fatal("edge3 was marked as zombie")
|
|
|
|
}
|
2017-10-02 17:38:45 +03:00
|
|
|
|
|
|
|
// PruneTip should be set to the blockHash we specified for the block
|
|
|
|
// at height 155.
|
|
|
|
hash, h, err := graph.PruneTip()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get prune tip: %v", err)
|
|
|
|
}
|
|
|
|
if !blockHash.IsEqual(hash) {
|
|
|
|
t.Fatalf("expected best block to be %x, was %x", blockHash, hash)
|
|
|
|
}
|
|
|
|
if h != height-1 {
|
|
|
|
t.Fatalf("expected best block height to be %d, was %d", height-1, h)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-06 04:22:18 +03:00
|
|
|
func assertEdgeInfoEqual(t *testing.T, e1 *ChannelEdgeInfo,
|
|
|
|
e2 *ChannelEdgeInfo) {
|
|
|
|
|
|
|
|
if e1.ChannelID != e2.ChannelID {
|
|
|
|
t.Fatalf("chan id's don't match: %v vs %v", e1.ChannelID,
|
|
|
|
e2.ChannelID)
|
|
|
|
}
|
|
|
|
|
2017-08-22 09:04:35 +03:00
|
|
|
if e1.ChainHash != e2.ChainHash {
|
|
|
|
t.Fatalf("chain hashes don't match: %v vs %v", e1.ChainHash,
|
|
|
|
e2.ChainHash)
|
|
|
|
}
|
|
|
|
|
2018-01-31 07:19:40 +03:00
|
|
|
if !bytes.Equal(e1.NodeKey1Bytes[:], e2.NodeKey1Bytes[:]) {
|
2017-03-06 04:22:18 +03:00
|
|
|
t.Fatalf("nodekey1 doesn't match")
|
|
|
|
}
|
2018-01-31 07:19:40 +03:00
|
|
|
if !bytes.Equal(e1.NodeKey2Bytes[:], e2.NodeKey2Bytes[:]) {
|
2017-03-06 04:22:18 +03:00
|
|
|
t.Fatalf("nodekey2 doesn't match")
|
|
|
|
}
|
2018-01-31 07:19:40 +03:00
|
|
|
if !bytes.Equal(e1.BitcoinKey1Bytes[:], e2.BitcoinKey1Bytes[:]) {
|
2017-03-06 04:22:18 +03:00
|
|
|
t.Fatalf("bitcoinkey1 doesn't match")
|
|
|
|
}
|
2018-01-31 07:19:40 +03:00
|
|
|
if !bytes.Equal(e1.BitcoinKey2Bytes[:], e2.BitcoinKey2Bytes[:]) {
|
2017-03-06 04:22:18 +03:00
|
|
|
t.Fatalf("bitcoinkey2 doesn't match")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(e1.Features, e2.Features) {
|
|
|
|
t.Fatalf("features doesn't match: %x vs %x", e1.Features,
|
|
|
|
e2.Features)
|
|
|
|
}
|
|
|
|
|
2018-01-31 07:19:40 +03:00
|
|
|
if !bytes.Equal(e1.AuthProof.NodeSig1Bytes, e2.AuthProof.NodeSig1Bytes) {
|
2017-03-06 04:22:18 +03:00
|
|
|
t.Fatalf("nodesig1 doesn't match: %v vs %v",
|
2018-01-31 07:19:40 +03:00
|
|
|
spew.Sdump(e1.AuthProof.NodeSig1Bytes),
|
|
|
|
spew.Sdump(e2.AuthProof.NodeSig1Bytes))
|
2017-03-06 04:22:18 +03:00
|
|
|
}
|
2018-01-31 07:19:40 +03:00
|
|
|
if !bytes.Equal(e1.AuthProof.NodeSig2Bytes, e2.AuthProof.NodeSig2Bytes) {
|
2017-03-06 04:22:18 +03:00
|
|
|
t.Fatalf("nodesig2 doesn't match")
|
|
|
|
}
|
2018-01-31 07:19:40 +03:00
|
|
|
if !bytes.Equal(e1.AuthProof.BitcoinSig1Bytes, e2.AuthProof.BitcoinSig1Bytes) {
|
2017-03-06 04:22:18 +03:00
|
|
|
t.Fatalf("bitcoinsig1 doesn't match")
|
|
|
|
}
|
2018-01-31 07:19:40 +03:00
|
|
|
if !bytes.Equal(e1.AuthProof.BitcoinSig2Bytes, e2.AuthProof.BitcoinSig2Bytes) {
|
2017-03-06 04:22:18 +03:00
|
|
|
t.Fatalf("bitcoinsig2 doesn't match")
|
|
|
|
}
|
|
|
|
|
|
|
|
if e1.ChannelPoint != e2.ChannelPoint {
|
|
|
|
t.Fatalf("channel point match: %v vs %v", e1.ChannelPoint,
|
|
|
|
e2.ChannelPoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
if e1.Capacity != e2.Capacity {
|
|
|
|
t.Fatalf("capacity doesn't match: %v vs %v", e1.Capacity,
|
|
|
|
e2.Capacity)
|
|
|
|
}
|
2018-09-01 06:12:45 +03:00
|
|
|
|
|
|
|
if !bytes.Equal(e1.ExtraOpaqueData, e2.ExtraOpaqueData) {
|
|
|
|
t.Fatalf("extra data doesn't match: %v vs %v",
|
|
|
|
e2.ExtraOpaqueData, e2.ExtraOpaqueData)
|
|
|
|
}
|
2017-03-06 04:22:18 +03:00
|
|
|
}
|
|
|
|
|
2019-01-12 20:59:44 +03:00
|
|
|
func createChannelEdge(db *DB, node1, node2 *LightningNode) (*ChannelEdgeInfo,
|
|
|
|
*ChannelEdgePolicy, *ChannelEdgePolicy) {
|
2016-12-08 09:47:01 +03:00
|
|
|
|
|
|
|
var (
|
|
|
|
firstNode *LightningNode
|
|
|
|
secondNode *LightningNode
|
|
|
|
)
|
2018-01-31 07:19:40 +03:00
|
|
|
if bytes.Compare(node1.PubKeyBytes[:], node2.PubKeyBytes[:]) == -1 {
|
2016-12-08 09:47:01 +03:00
|
|
|
firstNode = node1
|
|
|
|
secondNode = node2
|
|
|
|
} else {
|
|
|
|
firstNode = node2
|
|
|
|
secondNode = node1
|
|
|
|
}
|
|
|
|
|
2018-04-18 05:03:27 +03:00
|
|
|
// In addition to the fake vertexes we create some fake channel
|
2016-12-08 09:47:01 +03:00
|
|
|
// identifiers.
|
|
|
|
chanID := uint64(prand.Int63())
|
|
|
|
outpoint := wire.OutPoint{
|
|
|
|
Hash: rev,
|
|
|
|
Index: 9,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the new edge to the database, this should proceed without any
|
|
|
|
// errors.
|
2017-03-06 04:22:18 +03:00
|
|
|
edgeInfo := &ChannelEdgeInfo{
|
2018-01-31 07:19:40 +03:00
|
|
|
ChannelID: chanID,
|
|
|
|
ChainHash: key,
|
2017-03-06 04:22:18 +03:00
|
|
|
AuthProof: &ChannelAuthProof{
|
2018-01-31 07:19:40 +03:00
|
|
|
NodeSig1Bytes: testSig.Serialize(),
|
|
|
|
NodeSig2Bytes: testSig.Serialize(),
|
|
|
|
BitcoinSig1Bytes: testSig.Serialize(),
|
|
|
|
BitcoinSig2Bytes: testSig.Serialize(),
|
2017-03-06 04:22:18 +03:00
|
|
|
},
|
2018-09-01 06:12:45 +03:00
|
|
|
ChannelPoint: outpoint,
|
|
|
|
Capacity: 1000,
|
|
|
|
ExtraOpaqueData: []byte("new unknown feature"),
|
2017-03-06 04:22:18 +03:00
|
|
|
}
|
2018-01-31 07:19:40 +03:00
|
|
|
copy(edgeInfo.NodeKey1Bytes[:], firstNode.PubKeyBytes[:])
|
|
|
|
copy(edgeInfo.NodeKey2Bytes[:], secondNode.PubKeyBytes[:])
|
|
|
|
copy(edgeInfo.BitcoinKey1Bytes[:], firstNode.PubKeyBytes[:])
|
|
|
|
copy(edgeInfo.BitcoinKey2Bytes[:], secondNode.PubKeyBytes[:])
|
2016-12-08 09:47:01 +03:00
|
|
|
|
2017-03-06 04:22:18 +03:00
|
|
|
edge1 := &ChannelEdgePolicy{
|
2018-01-31 07:19:40 +03:00
|
|
|
SigBytes: testSig.Serialize(),
|
2016-12-08 09:47:01 +03:00
|
|
|
ChannelID: chanID,
|
|
|
|
LastUpdate: time.Unix(433453, 0),
|
2019-01-12 20:59:44 +03:00
|
|
|
MessageFlags: 1,
|
2019-01-12 20:59:43 +03:00
|
|
|
ChannelFlags: 0,
|
2017-03-06 04:22:18 +03:00
|
|
|
TimeLockDelta: 99,
|
2016-12-08 09:47:01 +03:00
|
|
|
MinHTLC: 2342135,
|
2019-01-12 20:59:44 +03:00
|
|
|
MaxHTLC: 13928598,
|
2016-12-08 09:47:01 +03:00
|
|
|
FeeBaseMSat: 4352345,
|
|
|
|
FeeProportionalMillionths: 3452352,
|
2018-10-07 05:34:32 +03:00
|
|
|
Node: secondNode,
|
|
|
|
ExtraOpaqueData: []byte("new unknown feature2"),
|
|
|
|
db: db,
|
2016-12-08 09:47:01 +03:00
|
|
|
}
|
2017-03-06 04:22:18 +03:00
|
|
|
edge2 := &ChannelEdgePolicy{
|
2018-01-31 07:19:40 +03:00
|
|
|
SigBytes: testSig.Serialize(),
|
2016-12-08 09:47:01 +03:00
|
|
|
ChannelID: chanID,
|
|
|
|
LastUpdate: time.Unix(124234, 0),
|
2019-01-12 20:59:44 +03:00
|
|
|
MessageFlags: 1,
|
2019-01-12 20:59:43 +03:00
|
|
|
ChannelFlags: 1,
|
2017-03-06 04:22:18 +03:00
|
|
|
TimeLockDelta: 99,
|
2016-12-08 09:47:01 +03:00
|
|
|
MinHTLC: 2342135,
|
2019-01-12 20:59:44 +03:00
|
|
|
MaxHTLC: 13928598,
|
2016-12-08 09:47:01 +03:00
|
|
|
FeeBaseMSat: 4352345,
|
|
|
|
FeeProportionalMillionths: 90392423,
|
2018-10-07 05:34:32 +03:00
|
|
|
Node: firstNode,
|
|
|
|
ExtraOpaqueData: []byte("new unknown feature1"),
|
|
|
|
db: db,
|
2016-12-08 09:47:01 +03:00
|
|
|
}
|
|
|
|
|
2019-01-12 20:59:44 +03:00
|
|
|
return edgeInfo, edge1, edge2
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEdgeInfoUpdates(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db, cleanUp, err := makeTestDB()
|
|
|
|
defer cleanUp()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
graph := db.ChannelGraph()
|
|
|
|
|
|
|
|
// We'd like to test the update of edges inserted into the database, so
|
|
|
|
// we create two vertexes to connect.
|
|
|
|
node1, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
if err := graph.AddLightningNode(node1); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
node2, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
if err := graph.AddLightningNode(node2); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an edge and add it to the db.
|
|
|
|
edgeInfo, edge1, edge2 := createChannelEdge(db, node1, node2)
|
2019-01-28 11:06:48 +03:00
|
|
|
|
|
|
|
// Make sure inserting the policy at this point, before the edge info
|
|
|
|
// is added, will fail.
|
|
|
|
if err := graph.UpdateEdgePolicy(edge1); err != ErrEdgeNotFound {
|
|
|
|
t.Fatalf("expected ErrEdgeNotFound, got: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the edge info.
|
2019-01-12 20:59:44 +03:00
|
|
|
if err := graph.AddChannelEdge(edgeInfo); err != nil {
|
|
|
|
t.Fatalf("unable to create channel edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
chanID := edgeInfo.ChannelID
|
|
|
|
outpoint := edgeInfo.ChannelPoint
|
|
|
|
|
|
|
|
// Next, insert both edge policies into the database, they should both
|
|
|
|
// be inserted without any issues.
|
2017-03-06 04:22:18 +03:00
|
|
|
if err := graph.UpdateEdgePolicy(edge1); err != nil {
|
2016-12-08 09:47:01 +03:00
|
|
|
t.Fatalf("unable to update edge: %v", err)
|
|
|
|
}
|
2017-03-06 04:22:18 +03:00
|
|
|
if err := graph.UpdateEdgePolicy(edge2); err != nil {
|
2016-12-08 09:47:01 +03:00
|
|
|
t.Fatalf("unable to update edge: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-12-22 23:49:30 +03:00
|
|
|
// Check for existence of the edge within the database, it should be
|
|
|
|
// found.
|
2019-03-27 23:06:57 +03:00
|
|
|
_, _, found, isZombie, err := graph.HasChannelEdge(chanID)
|
2016-12-22 23:49:30 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for edge: %v", err)
|
2019-03-27 23:06:57 +03:00
|
|
|
}
|
|
|
|
if !found {
|
2016-12-22 23:49:30 +03:00
|
|
|
t.Fatalf("graph should have of inserted edge")
|
|
|
|
}
|
2019-03-27 23:06:57 +03:00
|
|
|
if isZombie {
|
|
|
|
t.Fatal("live edge should not be marked as zombie")
|
|
|
|
}
|
2016-12-22 23:49:30 +03:00
|
|
|
|
2017-03-06 04:22:18 +03:00
|
|
|
// We should also be able to retrieve the channelID only knowing the
|
2016-12-27 06:52:03 +03:00
|
|
|
// channel point of the channel.
|
|
|
|
dbChanID, err := graph.ChannelID(&outpoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to retrieve channel ID: %v", err)
|
|
|
|
}
|
|
|
|
if dbChanID != chanID {
|
|
|
|
t.Fatalf("chan ID's mismatch, expected %v got %v", dbChanID,
|
|
|
|
chanID)
|
|
|
|
}
|
|
|
|
|
2016-12-08 09:47:01 +03:00
|
|
|
// With the edges inserted, perform some queries to ensure that they've
|
|
|
|
// been inserted properly.
|
2017-03-06 04:22:18 +03:00
|
|
|
dbEdgeInfo, dbEdge1, dbEdge2, err := graph.FetchChannelEdgesByID(chanID)
|
2016-12-08 09:47:01 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch channel by ID: %v", err)
|
|
|
|
}
|
2017-03-20 12:24:55 +03:00
|
|
|
if err := compareEdgePolicies(dbEdge1, edge1); err != nil {
|
|
|
|
t.Fatalf("edge doesn't match: %v", err)
|
2016-12-08 09:47:01 +03:00
|
|
|
}
|
2017-03-20 12:24:55 +03:00
|
|
|
if err := compareEdgePolicies(dbEdge2, edge2); err != nil {
|
|
|
|
t.Fatalf("edge doesn't match: %v", err)
|
2016-12-08 09:47:01 +03:00
|
|
|
}
|
2017-03-06 04:22:18 +03:00
|
|
|
assertEdgeInfoEqual(t, dbEdgeInfo, edgeInfo)
|
2016-12-08 09:47:01 +03:00
|
|
|
|
|
|
|
// Next, attempt to query the channel edges according to the outpoint
|
|
|
|
// of the channel.
|
2017-03-06 04:22:18 +03:00
|
|
|
dbEdgeInfo, dbEdge1, dbEdge2, err = graph.FetchChannelEdgesByOutpoint(&outpoint)
|
2016-12-08 09:47:01 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch channel by ID: %v", err)
|
|
|
|
}
|
2017-03-20 12:24:55 +03:00
|
|
|
if err := compareEdgePolicies(dbEdge1, edge1); err != nil {
|
|
|
|
t.Fatalf("edge doesn't match: %v", err)
|
2016-12-08 09:47:01 +03:00
|
|
|
}
|
2017-03-20 12:24:55 +03:00
|
|
|
if err := compareEdgePolicies(dbEdge2, edge2); err != nil {
|
|
|
|
t.Fatalf("edge doesn't match: %v", err)
|
2016-12-08 09:47:01 +03:00
|
|
|
}
|
2017-03-06 04:22:18 +03:00
|
|
|
assertEdgeInfoEqual(t, dbEdgeInfo, edgeInfo)
|
2016-12-08 09:47:01 +03:00
|
|
|
}
|
|
|
|
|
2017-03-06 04:22:18 +03:00
|
|
|
func randEdgePolicy(chanID uint64, op wire.OutPoint, db *DB) *ChannelEdgePolicy {
|
2016-12-08 09:47:01 +03:00
|
|
|
update := prand.Int63()
|
|
|
|
|
2018-04-17 05:04:29 +03:00
|
|
|
return newEdgePolicy(chanID, op, db, update)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newEdgePolicy(chanID uint64, op wire.OutPoint, db *DB,
|
|
|
|
updateTime int64) *ChannelEdgePolicy {
|
|
|
|
|
2017-03-06 04:22:18 +03:00
|
|
|
return &ChannelEdgePolicy{
|
2016-12-08 09:47:01 +03:00
|
|
|
ChannelID: chanID,
|
2018-04-17 05:04:29 +03:00
|
|
|
LastUpdate: time.Unix(updateTime, 0),
|
2019-01-12 20:59:44 +03:00
|
|
|
MessageFlags: 1,
|
2019-01-12 20:59:43 +03:00
|
|
|
ChannelFlags: 0,
|
2017-03-06 04:22:18 +03:00
|
|
|
TimeLockDelta: uint16(prand.Int63()),
|
2017-08-22 09:04:35 +03:00
|
|
|
MinHTLC: lnwire.MilliSatoshi(prand.Int63()),
|
2019-01-12 20:59:44 +03:00
|
|
|
MaxHTLC: lnwire.MilliSatoshi(prand.Int63()),
|
2017-08-22 09:04:35 +03:00
|
|
|
FeeBaseMSat: lnwire.MilliSatoshi(prand.Int63()),
|
|
|
|
FeeProportionalMillionths: lnwire.MilliSatoshi(prand.Int63()),
|
2018-10-07 05:34:32 +03:00
|
|
|
db: db,
|
2016-12-08 09:47:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGraphTraversal(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-12-08 09:47:01 +03:00
|
|
|
db, cleanUp, err := makeTestDB()
|
2016-12-22 23:04:41 +03:00
|
|
|
defer cleanUp()
|
2016-12-08 09:47:01 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
graph := db.ChannelGraph()
|
|
|
|
|
|
|
|
// We'd like to test some of the graph traversal capabilities within
|
|
|
|
// the DB, so we'll create a series of fake nodes to insert into the
|
|
|
|
// graph.
|
|
|
|
const numNodes = 20
|
|
|
|
nodes := make([]*LightningNode, numNodes)
|
|
|
|
nodeIndex := map[string]struct{}{}
|
|
|
|
for i := 0; i < numNodes; i++ {
|
|
|
|
node, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes[i] = node
|
|
|
|
nodeIndex[node.Alias] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add each of the nodes into the graph, they should be inserted
|
|
|
|
// without error.
|
|
|
|
for _, node := range nodes {
|
|
|
|
if err := graph.AddLightningNode(node); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over each node as returned by the graph, if all nodes are
|
|
|
|
// reached, then the map created above should be empty.
|
2018-11-30 07:04:21 +03:00
|
|
|
err = graph.ForEachNode(nil, func(_ *bbolt.Tx, node *LightningNode) error {
|
2016-12-08 09:47:01 +03:00
|
|
|
delete(nodeIndex, node.Alias)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("for each failure: %v", err)
|
|
|
|
}
|
|
|
|
if len(nodeIndex) != 0 {
|
|
|
|
t.Fatalf("all nodes not reached within ForEach")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine which node is "smaller", we'll need this in order to
|
|
|
|
// properly create the edges for the graph.
|
|
|
|
var firstNode, secondNode *LightningNode
|
2018-01-31 07:19:40 +03:00
|
|
|
if bytes.Compare(nodes[0].PubKeyBytes[:], nodes[1].PubKeyBytes[:]) == -1 {
|
2016-12-08 09:47:01 +03:00
|
|
|
firstNode = nodes[0]
|
|
|
|
secondNode = nodes[1]
|
|
|
|
} else {
|
|
|
|
firstNode = nodes[0]
|
|
|
|
secondNode = nodes[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create 5 channels between the first two nodes we generated above.
|
|
|
|
const numChannels = 5
|
|
|
|
chanIndex := map[uint64]struct{}{}
|
|
|
|
for i := 0; i < numChannels; i++ {
|
2017-03-16 04:56:25 +03:00
|
|
|
txHash := sha256.Sum256([]byte{byte(i)})
|
2016-12-08 09:47:01 +03:00
|
|
|
chanID := uint64(i + 1)
|
|
|
|
op := wire.OutPoint{
|
|
|
|
Hash: txHash,
|
|
|
|
Index: 0,
|
|
|
|
}
|
|
|
|
|
2017-03-06 04:22:18 +03:00
|
|
|
edgeInfo := ChannelEdgeInfo{
|
2018-01-31 07:19:40 +03:00
|
|
|
ChannelID: chanID,
|
|
|
|
ChainHash: key,
|
2017-03-06 04:22:18 +03:00
|
|
|
AuthProof: &ChannelAuthProof{
|
2018-01-31 07:19:40 +03:00
|
|
|
NodeSig1Bytes: testSig.Serialize(),
|
|
|
|
NodeSig2Bytes: testSig.Serialize(),
|
|
|
|
BitcoinSig1Bytes: testSig.Serialize(),
|
|
|
|
BitcoinSig2Bytes: testSig.Serialize(),
|
2017-03-06 04:22:18 +03:00
|
|
|
},
|
|
|
|
ChannelPoint: op,
|
|
|
|
Capacity: 1000,
|
|
|
|
}
|
2018-01-31 07:19:40 +03:00
|
|
|
copy(edgeInfo.NodeKey1Bytes[:], nodes[0].PubKeyBytes[:])
|
|
|
|
copy(edgeInfo.NodeKey2Bytes[:], nodes[1].PubKeyBytes[:])
|
|
|
|
copy(edgeInfo.BitcoinKey1Bytes[:], nodes[0].PubKeyBytes[:])
|
|
|
|
copy(edgeInfo.BitcoinKey2Bytes[:], nodes[1].PubKeyBytes[:])
|
2017-03-06 04:22:18 +03:00
|
|
|
err := graph.AddChannelEdge(&edgeInfo)
|
2016-12-08 09:47:01 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create and add an edge with random data that points from
|
|
|
|
// node1 -> node2.
|
2017-03-06 04:22:18 +03:00
|
|
|
edge := randEdgePolicy(chanID, op, db)
|
2019-01-12 20:59:43 +03:00
|
|
|
edge.ChannelFlags = 0
|
2016-12-08 09:47:01 +03:00
|
|
|
edge.Node = secondNode
|
2018-01-31 07:19:40 +03:00
|
|
|
edge.SigBytes = testSig.Serialize()
|
2017-03-06 04:22:18 +03:00
|
|
|
if err := graph.UpdateEdgePolicy(edge); err != nil {
|
2016-12-08 09:47:01 +03:00
|
|
|
t.Fatalf("unable to update edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create another random edge that points from node2 -> node1
|
|
|
|
// this time.
|
2017-03-06 04:22:18 +03:00
|
|
|
edge = randEdgePolicy(chanID, op, db)
|
2019-01-12 20:59:43 +03:00
|
|
|
edge.ChannelFlags = 1
|
2016-12-08 09:47:01 +03:00
|
|
|
edge.Node = firstNode
|
2018-01-31 07:19:40 +03:00
|
|
|
edge.SigBytes = testSig.Serialize()
|
2017-03-06 04:22:18 +03:00
|
|
|
if err := graph.UpdateEdgePolicy(edge); err != nil {
|
2016-12-08 09:47:01 +03:00
|
|
|
t.Fatalf("unable to update edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
chanIndex[chanID] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate through all the known channels within the graph DB, once
|
2018-04-18 05:03:27 +03:00
|
|
|
// again if the map is empty that indicates that all edges have
|
2016-12-08 09:47:01 +03:00
|
|
|
// properly been reached.
|
2017-03-06 04:22:18 +03:00
|
|
|
err = graph.ForEachChannel(func(ei *ChannelEdgeInfo, _ *ChannelEdgePolicy,
|
|
|
|
_ *ChannelEdgePolicy) error {
|
|
|
|
|
|
|
|
delete(chanIndex, ei.ChannelID)
|
2016-12-08 09:47:01 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("for each failure: %v", err)
|
|
|
|
}
|
|
|
|
if len(chanIndex) != 0 {
|
|
|
|
t.Fatalf("all edges not reached within ForEach")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, we want to test the ability to iterate over all the
|
|
|
|
// outgoing channels for a particular node.
|
|
|
|
numNodeChans := 0
|
2018-11-30 07:04:21 +03:00
|
|
|
err = firstNode.ForEachChannel(nil, func(_ *bbolt.Tx, _ *ChannelEdgeInfo,
|
2017-08-22 09:04:35 +03:00
|
|
|
outEdge, inEdge *ChannelEdgePolicy) error {
|
2017-04-14 23:14:02 +03:00
|
|
|
|
2018-06-18 13:35:22 +03:00
|
|
|
// All channels between first and second node should have fully
|
|
|
|
// (both sides) specified policies.
|
|
|
|
if inEdge == nil || outEdge == nil {
|
|
|
|
return fmt.Errorf("channel policy not present")
|
|
|
|
}
|
|
|
|
|
2018-04-18 05:03:27 +03:00
|
|
|
// Each should indicate that it's outgoing (pointed
|
2016-12-08 09:47:01 +03:00
|
|
|
// towards the second node).
|
2018-01-31 07:19:40 +03:00
|
|
|
if !bytes.Equal(outEdge.Node.PubKeyBytes[:], secondNode.PubKeyBytes[:]) {
|
2017-08-22 09:04:35 +03:00
|
|
|
return fmt.Errorf("wrong outgoing edge")
|
|
|
|
}
|
|
|
|
|
|
|
|
// The incoming edge should also indicate that it's pointing to
|
|
|
|
// the origin node.
|
2018-01-31 07:19:40 +03:00
|
|
|
if !bytes.Equal(inEdge.Node.PubKeyBytes[:], firstNode.PubKeyBytes[:]) {
|
2016-12-08 09:47:01 +03:00
|
|
|
return fmt.Errorf("wrong outgoing edge")
|
|
|
|
}
|
2017-08-22 09:04:35 +03:00
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
numNodeChans++
|
2016-12-08 09:47:01 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("for each failure: %v", err)
|
|
|
|
}
|
|
|
|
if numNodeChans != numChannels {
|
2017-08-22 09:04:35 +03:00
|
|
|
t.Fatalf("all edges for node not reached within ForEach: "+
|
|
|
|
"expected %v, got %v", numChannels, numNodeChans)
|
2016-12-08 09:47:01 +03:00
|
|
|
}
|
|
|
|
}
|
2016-12-20 03:58:27 +03:00
|
|
|
|
2017-01-06 00:56:27 +03:00
|
|
|
func assertPruneTip(t *testing.T, graph *ChannelGraph, blockHash *chainhash.Hash,
|
2016-12-20 03:58:27 +03:00
|
|
|
blockHeight uint32) {
|
|
|
|
|
|
|
|
pruneHash, pruneHeight, err := graph.PruneTip()
|
|
|
|
if err != nil {
|
|
|
|
_, _, line, _ := runtime.Caller(1)
|
|
|
|
t.Fatalf("line %v: unable to fetch prune tip: %v", line, err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(blockHash[:], pruneHash[:]) {
|
|
|
|
_, _, line, _ := runtime.Caller(1)
|
|
|
|
t.Fatalf("line: %v, prune tips don't match, expected %x got %x",
|
|
|
|
line, blockHash, pruneHash)
|
|
|
|
}
|
|
|
|
if pruneHeight != blockHeight {
|
|
|
|
_, _, line, _ := runtime.Caller(1)
|
|
|
|
t.Fatalf("line %v: prune heights don't match, expected %v "+
|
|
|
|
"got %v", line, blockHeight, pruneHeight)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
func assertNumChans(t *testing.T, graph *ChannelGraph, n int) {
|
2016-12-20 03:58:27 +03:00
|
|
|
numChans := 0
|
2017-03-06 04:22:18 +03:00
|
|
|
if err := graph.ForEachChannel(func(*ChannelEdgeInfo, *ChannelEdgePolicy,
|
|
|
|
*ChannelEdgePolicy) error {
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
numChans++
|
2016-12-20 03:58:27 +03:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
_, _, line, _ := runtime.Caller(1)
|
2018-06-28 06:05:45 +03:00
|
|
|
t.Fatalf("line %v: unable to scan channels: %v", line, err)
|
2016-12-20 03:58:27 +03:00
|
|
|
}
|
|
|
|
if numChans != n {
|
|
|
|
_, _, line, _ := runtime.Caller(1)
|
2017-05-11 02:19:42 +03:00
|
|
|
t.Fatalf("line %v: expected %v chans instead have %v", line,
|
|
|
|
n, numChans)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-28 06:05:45 +03:00
|
|
|
func assertNumNodes(t *testing.T, graph *ChannelGraph, n int) {
|
|
|
|
numNodes := 0
|
2018-11-30 07:04:21 +03:00
|
|
|
err := graph.ForEachNode(nil, func(_ *bbolt.Tx, _ *LightningNode) error {
|
2018-06-28 06:05:45 +03:00
|
|
|
numNodes++
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
_, _, line, _ := runtime.Caller(1)
|
|
|
|
t.Fatalf("line %v: unable to scan nodes: %v", line, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if numNodes != n {
|
|
|
|
_, _, line, _ := runtime.Caller(1)
|
|
|
|
t.Fatalf("line %v: expected %v nodes, got %v", line, n, numNodes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 05:10:07 +03:00
|
|
|
func assertChanViewEqual(t *testing.T, a []EdgePoint, b []EdgePoint) {
|
2017-05-11 02:19:42 +03:00
|
|
|
if len(a) != len(b) {
|
|
|
|
_, _, line, _ := runtime.Caller(1)
|
2018-02-07 06:11:11 +03:00
|
|
|
t.Fatalf("line %v: chan views don't match", line)
|
2017-05-11 02:19:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
chanViewSet := make(map[wire.OutPoint]struct{})
|
|
|
|
for _, op := range a {
|
2018-07-18 05:10:07 +03:00
|
|
|
chanViewSet[op.OutPoint] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, op := range b {
|
|
|
|
if _, ok := chanViewSet[op.OutPoint]; !ok {
|
|
|
|
_, _, line, _ := runtime.Caller(1)
|
|
|
|
t.Fatalf("line %v: chanPoint(%v) not found in first "+
|
|
|
|
"view", line, op)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertChanViewEqualChanPoints(t *testing.T, a []EdgePoint, b []*wire.OutPoint) {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
_, _, line, _ := runtime.Caller(1)
|
|
|
|
t.Fatalf("line %v: chan views don't match", line)
|
|
|
|
}
|
|
|
|
|
|
|
|
chanViewSet := make(map[wire.OutPoint]struct{})
|
|
|
|
for _, op := range a {
|
|
|
|
chanViewSet[op.OutPoint] = struct{}{}
|
2017-05-11 02:19:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, op := range b {
|
|
|
|
if _, ok := chanViewSet[*op]; !ok {
|
|
|
|
_, _, line, _ := runtime.Caller(1)
|
2018-07-18 05:10:07 +03:00
|
|
|
t.Fatalf("line %v: chanPoint(%v) not found in first "+
|
|
|
|
"view", line, op)
|
2017-05-11 02:19:42 +03:00
|
|
|
}
|
2016-12-20 03:58:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGraphPruning(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-12-20 03:58:27 +03:00
|
|
|
db, cleanUp, err := makeTestDB()
|
2016-12-22 23:04:41 +03:00
|
|
|
defer cleanUp()
|
2016-12-20 03:58:27 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
graph := db.ChannelGraph()
|
2018-06-28 06:05:45 +03:00
|
|
|
sourceNode, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create source node: %v", err)
|
|
|
|
}
|
|
|
|
if err := graph.SetSourceNode(sourceNode); err != nil {
|
|
|
|
t.Fatalf("unable to set source node: %v", err)
|
|
|
|
}
|
2016-12-20 03:58:27 +03:00
|
|
|
|
|
|
|
// As initial set up for the test, we'll create a graph with 5 vertexes
|
|
|
|
// and enough edges to create a fully connected graph. The graph will
|
|
|
|
// be rather simple, representing a straight line.
|
|
|
|
const numNodes = 5
|
|
|
|
graphNodes := make([]*LightningNode, numNodes)
|
|
|
|
for i := 0; i < numNodes; i++ {
|
|
|
|
node, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := graph.AddLightningNode(node); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
graphNodes[i] = node
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the vertexes created, we'll next create a series of channels
|
|
|
|
// between them.
|
|
|
|
channelPoints := make([]*wire.OutPoint, 0, numNodes-1)
|
2018-07-18 05:10:07 +03:00
|
|
|
edgePoints := make([]EdgePoint, 0, numNodes-1)
|
2016-12-20 03:58:27 +03:00
|
|
|
for i := 0; i < numNodes-1; i++ {
|
2017-03-16 04:56:25 +03:00
|
|
|
txHash := sha256.Sum256([]byte{byte(i)})
|
2016-12-20 03:58:27 +03:00
|
|
|
chanID := uint64(i + 1)
|
|
|
|
op := wire.OutPoint{
|
|
|
|
Hash: txHash,
|
|
|
|
Index: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
channelPoints = append(channelPoints, &op)
|
|
|
|
|
2017-03-06 04:22:18 +03:00
|
|
|
edgeInfo := ChannelEdgeInfo{
|
2018-01-31 07:19:40 +03:00
|
|
|
ChannelID: chanID,
|
|
|
|
ChainHash: key,
|
2017-03-06 04:22:18 +03:00
|
|
|
AuthProof: &ChannelAuthProof{
|
2018-01-31 07:19:40 +03:00
|
|
|
NodeSig1Bytes: testSig.Serialize(),
|
|
|
|
NodeSig2Bytes: testSig.Serialize(),
|
|
|
|
BitcoinSig1Bytes: testSig.Serialize(),
|
|
|
|
BitcoinSig2Bytes: testSig.Serialize(),
|
2017-03-06 04:22:18 +03:00
|
|
|
},
|
|
|
|
ChannelPoint: op,
|
|
|
|
Capacity: 1000,
|
|
|
|
}
|
2018-01-31 07:19:40 +03:00
|
|
|
copy(edgeInfo.NodeKey1Bytes[:], graphNodes[i].PubKeyBytes[:])
|
|
|
|
copy(edgeInfo.NodeKey2Bytes[:], graphNodes[i+1].PubKeyBytes[:])
|
|
|
|
copy(edgeInfo.BitcoinKey1Bytes[:], graphNodes[i].PubKeyBytes[:])
|
|
|
|
copy(edgeInfo.BitcoinKey2Bytes[:], graphNodes[i+1].PubKeyBytes[:])
|
2017-03-06 04:22:18 +03:00
|
|
|
if err := graph.AddChannelEdge(&edgeInfo); err != nil {
|
2016-12-20 03:58:27 +03:00
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-07-18 05:10:07 +03:00
|
|
|
pkScript, err := genMultiSigP2WSH(
|
|
|
|
edgeInfo.BitcoinKey1Bytes[:], edgeInfo.BitcoinKey2Bytes[:],
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to gen multi-sig p2wsh: %v", err)
|
|
|
|
}
|
|
|
|
edgePoints = append(edgePoints, EdgePoint{
|
|
|
|
FundingPkScript: pkScript,
|
|
|
|
OutPoint: op,
|
|
|
|
})
|
|
|
|
|
2016-12-20 03:58:27 +03:00
|
|
|
// Create and add an edge with random data that points from
|
|
|
|
// node_i -> node_i+1
|
2017-03-06 04:22:18 +03:00
|
|
|
edge := randEdgePolicy(chanID, op, db)
|
2019-01-12 20:59:43 +03:00
|
|
|
edge.ChannelFlags = 0
|
2016-12-20 03:58:27 +03:00
|
|
|
edge.Node = graphNodes[i]
|
2018-01-31 07:19:40 +03:00
|
|
|
edge.SigBytes = testSig.Serialize()
|
2017-03-06 04:22:18 +03:00
|
|
|
if err := graph.UpdateEdgePolicy(edge); err != nil {
|
2016-12-20 03:58:27 +03:00
|
|
|
t.Fatalf("unable to update edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create another random edge that points from node_i+1 ->
|
|
|
|
// node_i this time.
|
2017-03-06 04:22:18 +03:00
|
|
|
edge = randEdgePolicy(chanID, op, db)
|
2019-01-12 20:59:43 +03:00
|
|
|
edge.ChannelFlags = 1
|
2016-12-20 03:58:27 +03:00
|
|
|
edge.Node = graphNodes[i]
|
2018-01-31 07:19:40 +03:00
|
|
|
edge.SigBytes = testSig.Serialize()
|
2017-03-06 04:22:18 +03:00
|
|
|
if err := graph.UpdateEdgePolicy(edge); err != nil {
|
2016-12-20 03:58:27 +03:00
|
|
|
t.Fatalf("unable to update edge: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-11 02:19:42 +03:00
|
|
|
// With all the channel points added, we'll consult the graph to ensure
|
|
|
|
// it has the same channel view as the one we just constructed.
|
|
|
|
channelView, err := graph.ChannelView()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get graph channel view: %v", err)
|
|
|
|
}
|
2018-07-18 05:10:07 +03:00
|
|
|
assertChanViewEqual(t, channelView, edgePoints)
|
2017-05-11 02:19:42 +03:00
|
|
|
|
2016-12-20 03:58:27 +03:00
|
|
|
// Now with our test graph created, we can test the pruning
|
|
|
|
// capabilities of the channel graph.
|
|
|
|
|
|
|
|
// First we create a mock block that ends up closing the first two
|
|
|
|
// channels.
|
2017-01-06 00:56:27 +03:00
|
|
|
var blockHash chainhash.Hash
|
2016-12-20 03:58:27 +03:00
|
|
|
copy(blockHash[:], bytes.Repeat([]byte{1}, 32))
|
|
|
|
blockHeight := uint32(1)
|
|
|
|
block := channelPoints[:2]
|
2017-03-06 04:22:18 +03:00
|
|
|
prunedChans, err := graph.PruneGraph(block, &blockHash, blockHeight)
|
2016-12-20 03:58:27 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to prune graph: %v", err)
|
|
|
|
}
|
2017-03-06 04:22:18 +03:00
|
|
|
if len(prunedChans) != 2 {
|
2018-06-06 03:30:50 +03:00
|
|
|
t.Fatalf("incorrect number of channels pruned: "+
|
|
|
|
"expected %v, got %v", 2, prunedChans)
|
2016-12-20 03:58:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now ensure that the prune tip has been updated.
|
|
|
|
assertPruneTip(t, graph, &blockHash, blockHeight)
|
|
|
|
|
|
|
|
// Count up the number of channels known within the graph, only 2
|
|
|
|
// should be remaining.
|
2018-02-07 06:11:11 +03:00
|
|
|
assertNumChans(t, graph, 2)
|
2016-12-20 03:58:27 +03:00
|
|
|
|
2017-05-11 02:19:42 +03:00
|
|
|
// Those channels should also be missing from the channel view.
|
|
|
|
channelView, err = graph.ChannelView()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get graph channel view: %v", err)
|
|
|
|
}
|
2018-07-18 05:10:07 +03:00
|
|
|
assertChanViewEqualChanPoints(t, channelView, channelPoints[2:])
|
2017-05-11 02:19:42 +03:00
|
|
|
|
2016-12-20 03:58:27 +03:00
|
|
|
// Next we'll create a block that doesn't close any channels within the
|
|
|
|
// graph to test the negative error case.
|
2017-03-16 04:56:25 +03:00
|
|
|
fakeHash := sha256.Sum256([]byte("test prune"))
|
2016-12-20 03:58:27 +03:00
|
|
|
nonChannel := &wire.OutPoint{
|
|
|
|
Hash: fakeHash,
|
|
|
|
Index: 9,
|
|
|
|
}
|
2017-03-16 04:56:25 +03:00
|
|
|
blockHash = sha256.Sum256(blockHash[:])
|
2016-12-20 03:58:27 +03:00
|
|
|
blockHeight = 2
|
2018-06-06 03:30:50 +03:00
|
|
|
prunedChans, err = graph.PruneGraph(
|
|
|
|
[]*wire.OutPoint{nonChannel}, &blockHash, blockHeight,
|
|
|
|
)
|
2016-12-20 03:58:27 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to prune graph: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// No channels should have been detected as pruned.
|
2017-03-06 04:22:18 +03:00
|
|
|
if len(prunedChans) != 0 {
|
2016-12-20 03:58:27 +03:00
|
|
|
t.Fatalf("channels were pruned but shouldn't have been")
|
|
|
|
}
|
|
|
|
|
2018-06-28 06:05:45 +03:00
|
|
|
// Once again, the prune tip should have been updated. We should still
|
|
|
|
// see both channels and their participants, along with the source node.
|
2016-12-20 03:58:27 +03:00
|
|
|
assertPruneTip(t, graph, &blockHash, blockHeight)
|
2018-02-07 06:11:11 +03:00
|
|
|
assertNumChans(t, graph, 2)
|
2018-06-28 06:05:45 +03:00
|
|
|
assertNumNodes(t, graph, 4)
|
2016-12-20 03:58:27 +03:00
|
|
|
|
|
|
|
// Finally, create a block that prunes the remainder of the channels
|
|
|
|
// from the graph.
|
2017-03-16 04:56:25 +03:00
|
|
|
blockHash = sha256.Sum256(blockHash[:])
|
2016-12-20 03:58:27 +03:00
|
|
|
blockHeight = 3
|
2018-06-06 03:30:50 +03:00
|
|
|
prunedChans, err = graph.PruneGraph(
|
|
|
|
channelPoints[2:], &blockHash, blockHeight,
|
|
|
|
)
|
2016-12-20 03:58:27 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to prune graph: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-06-06 03:30:50 +03:00
|
|
|
// The remainder of the channels should have been pruned from the
|
|
|
|
// graph.
|
2017-03-06 04:22:18 +03:00
|
|
|
if len(prunedChans) != 2 {
|
2018-06-06 03:30:50 +03:00
|
|
|
t.Fatalf("incorrect number of channels pruned: "+
|
|
|
|
"expected %v, got %v", 2, len(prunedChans))
|
2016-12-20 03:58:27 +03:00
|
|
|
}
|
|
|
|
|
2018-06-28 06:05:45 +03:00
|
|
|
// The prune tip should be updated, no channels should be found, and
|
|
|
|
// only the source node should remain within the current graph.
|
2016-12-20 03:58:27 +03:00
|
|
|
assertPruneTip(t, graph, &blockHash, blockHeight)
|
2018-02-07 06:11:11 +03:00
|
|
|
assertNumChans(t, graph, 0)
|
2018-06-28 06:05:45 +03:00
|
|
|
assertNumNodes(t, graph, 1)
|
2017-05-11 02:19:42 +03:00
|
|
|
|
|
|
|
// Finally, the channel view at this point in the graph should now be
|
2018-06-06 03:30:50 +03:00
|
|
|
// completely empty. Those channels should also be missing from the
|
|
|
|
// channel view.
|
2017-05-11 02:19:42 +03:00
|
|
|
channelView, err = graph.ChannelView()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get graph channel view: %v", err)
|
|
|
|
}
|
|
|
|
if len(channelView) != 0 {
|
|
|
|
t.Fatalf("channel view should be empty, instead have: %v",
|
|
|
|
channelView)
|
|
|
|
}
|
2016-12-20 03:58:27 +03:00
|
|
|
}
|
2017-03-20 12:24:55 +03:00
|
|
|
|
2018-04-17 05:04:29 +03:00
|
|
|
// TestHighestChanID tests that we're able to properly retrieve the highest
|
|
|
|
// known channel ID in the database.
|
|
|
|
func TestHighestChanID(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db, cleanUp, err := makeTestDB()
|
|
|
|
defer cleanUp()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
graph := db.ChannelGraph()
|
|
|
|
|
|
|
|
// If we don't yet have any channels in the database, then we should
|
|
|
|
// get a channel ID of zero if we ask for the highest channel ID.
|
|
|
|
bestID, err := graph.HighestChanID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get highest ID: %v", err)
|
|
|
|
}
|
|
|
|
if bestID != 0 {
|
|
|
|
t.Fatalf("best ID w/ no chan should be zero, is instead: %v",
|
|
|
|
bestID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll insert two channels into the database, with each channel
|
|
|
|
// connecting the same two nodes.
|
|
|
|
node1, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
node2, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The first channel with be at height 10, while the other will be at
|
|
|
|
// height 100.
|
|
|
|
edge1, _ := createEdge(10, 0, 0, 0, node1, node2)
|
|
|
|
edge2, chanID2 := createEdge(100, 0, 0, 0, node1, node2)
|
|
|
|
|
|
|
|
if err := graph.AddChannelEdge(&edge1); err != nil {
|
|
|
|
t.Fatalf("unable to create channel edge: %v", err)
|
|
|
|
}
|
|
|
|
if err := graph.AddChannelEdge(&edge2); err != nil {
|
|
|
|
t.Fatalf("unable to create channel edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that the edges has been inserted, we'll query for the highest
|
|
|
|
// known channel ID in the database.
|
|
|
|
bestID, err = graph.HighestChanID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get highest ID: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if bestID != chanID2.ToUint64() {
|
|
|
|
t.Fatalf("expected %v got %v for best chan ID: ",
|
|
|
|
chanID2.ToUint64(), bestID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we add another edge, then the current best chan ID should be
|
|
|
|
// updated as well.
|
|
|
|
edge3, chanID3 := createEdge(1000, 0, 0, 0, node1, node2)
|
|
|
|
if err := graph.AddChannelEdge(&edge3); err != nil {
|
|
|
|
t.Fatalf("unable to create channel edge: %v", err)
|
|
|
|
}
|
|
|
|
bestID, err = graph.HighestChanID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get highest ID: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if bestID != chanID3.ToUint64() {
|
|
|
|
t.Fatalf("expected %v got %v for best chan ID: ",
|
|
|
|
chanID3.ToUint64(), bestID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestChanUpdatesInHorizon tests the we're able to properly retrieve all known
|
|
|
|
// channel updates within a specific time horizon. It also tests that upon
|
|
|
|
// insertion of a new edge, the edge update index is updated properly.
|
|
|
|
func TestChanUpdatesInHorizon(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db, cleanUp, err := makeTestDB()
|
|
|
|
defer cleanUp()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
graph := db.ChannelGraph()
|
|
|
|
|
|
|
|
// If we issue an arbitrary query before any channel updates are
|
|
|
|
// inserted in the database, we should get zero results.
|
|
|
|
chanUpdates, err := graph.ChanUpdatesInHorizon(
|
|
|
|
time.Unix(999, 0), time.Unix(9999, 0),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to updates for updates: %v", err)
|
|
|
|
}
|
|
|
|
if len(chanUpdates) != 0 {
|
|
|
|
t.Fatalf("expected 0 chan updates, instead got %v",
|
|
|
|
len(chanUpdates))
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll start by creating two nodes which will seed our test graph.
|
|
|
|
node1, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
if err := graph.AddLightningNode(node1); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
node2, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
if err := graph.AddLightningNode(node2); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll now create 10 channels between the two nodes, with update
|
|
|
|
// times 10 seconds after each other.
|
|
|
|
const numChans = 10
|
|
|
|
startTime := time.Unix(1234, 0)
|
|
|
|
endTime := startTime
|
|
|
|
edges := make([]ChannelEdge, 0, numChans)
|
|
|
|
for i := 0; i < numChans; i++ {
|
|
|
|
txHash := sha256.Sum256([]byte{byte(i)})
|
|
|
|
op := wire.OutPoint{
|
|
|
|
Hash: txHash,
|
|
|
|
Index: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
channel, chanID := createEdge(
|
|
|
|
uint32(i*10), 0, 0, 0, node1, node2,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := graph.AddChannelEdge(&channel); err != nil {
|
|
|
|
t.Fatalf("unable to create channel edge: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-09-05 04:36:25 +03:00
|
|
|
edge1UpdateTime := endTime
|
|
|
|
edge2UpdateTime := edge1UpdateTime.Add(time.Second)
|
|
|
|
endTime = endTime.Add(time.Second * 10)
|
2018-04-17 05:04:29 +03:00
|
|
|
|
|
|
|
edge1 := newEdgePolicy(
|
2018-09-05 04:36:25 +03:00
|
|
|
chanID.ToUint64(), op, db, edge1UpdateTime.Unix(),
|
2018-04-17 05:04:29 +03:00
|
|
|
)
|
2019-01-12 20:59:43 +03:00
|
|
|
edge1.ChannelFlags = 0
|
2018-04-17 05:04:29 +03:00
|
|
|
edge1.Node = node2
|
|
|
|
edge1.SigBytes = testSig.Serialize()
|
|
|
|
if err := graph.UpdateEdgePolicy(edge1); err != nil {
|
|
|
|
t.Fatalf("unable to update edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
edge2 := newEdgePolicy(
|
2018-09-05 04:36:25 +03:00
|
|
|
chanID.ToUint64(), op, db, edge2UpdateTime.Unix(),
|
2018-04-17 05:04:29 +03:00
|
|
|
)
|
2019-01-12 20:59:43 +03:00
|
|
|
edge2.ChannelFlags = 1
|
2018-04-17 05:04:29 +03:00
|
|
|
edge2.Node = node1
|
|
|
|
edge2.SigBytes = testSig.Serialize()
|
|
|
|
if err := graph.UpdateEdgePolicy(edge2); err != nil {
|
|
|
|
t.Fatalf("unable to update edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
edges = append(edges, ChannelEdge{
|
|
|
|
Info: &channel,
|
|
|
|
Policy1: edge1,
|
|
|
|
Policy2: edge2,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// With our channels loaded, we'll now start our series of queries.
|
|
|
|
queryCases := []struct {
|
|
|
|
start time.Time
|
|
|
|
end time.Time
|
|
|
|
|
|
|
|
resp []ChannelEdge
|
|
|
|
}{
|
|
|
|
// If we query for a time range that's strictly below our set
|
|
|
|
// of updates, then we'll get an empty result back.
|
|
|
|
{
|
|
|
|
start: time.Unix(100, 0),
|
|
|
|
end: time.Unix(200, 0),
|
|
|
|
},
|
|
|
|
|
|
|
|
// If we query for a time range that's well beyond our set of
|
|
|
|
// updates, we should get an empty set of results back.
|
|
|
|
{
|
|
|
|
start: time.Unix(99999, 0),
|
|
|
|
end: time.Unix(999999, 0),
|
|
|
|
},
|
|
|
|
|
|
|
|
// If we query for the start time, and 10 seconds directly
|
|
|
|
// after it, we should only get a single update, that first
|
|
|
|
// one.
|
|
|
|
{
|
|
|
|
start: time.Unix(1234, 0),
|
|
|
|
end: startTime.Add(time.Second * 10),
|
|
|
|
|
|
|
|
resp: []ChannelEdge{edges[0]},
|
|
|
|
},
|
|
|
|
|
|
|
|
// If we add 10 seconds past the first update, and then
|
|
|
|
// subtract 10 from the last update, then we should only get
|
|
|
|
// the 8 edges in the middle.
|
|
|
|
{
|
|
|
|
start: startTime.Add(time.Second * 10),
|
|
|
|
end: endTime.Add(-time.Second * 10),
|
|
|
|
|
|
|
|
resp: edges[1:9],
|
|
|
|
},
|
|
|
|
|
|
|
|
// If we use the start and end time as is, we should get the
|
|
|
|
// entire range.
|
|
|
|
{
|
|
|
|
start: startTime,
|
|
|
|
end: endTime,
|
|
|
|
|
|
|
|
resp: edges,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, queryCase := range queryCases {
|
|
|
|
resp, err := graph.ChanUpdatesInHorizon(
|
|
|
|
queryCase.start, queryCase.end,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for updates: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp) != len(queryCase.resp) {
|
|
|
|
t.Fatalf("expected %v chans, got %v chans",
|
|
|
|
len(queryCase.resp), len(resp))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(resp); i++ {
|
|
|
|
chanExp := queryCase.resp[i]
|
|
|
|
chanRet := resp[i]
|
|
|
|
|
|
|
|
assertEdgeInfoEqual(t, chanExp.Info, chanRet.Info)
|
|
|
|
|
|
|
|
err := compareEdgePolicies(chanExp.Policy1, chanRet.Policy1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
compareEdgePolicies(chanExp.Policy2, chanRet.Policy2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestNodeUpdatesInHorizon tests that we're able to properly scan and retrieve
|
|
|
|
// the most recent node updates within a particular time horizon.
|
|
|
|
func TestNodeUpdatesInHorizon(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db, cleanUp, err := makeTestDB()
|
|
|
|
defer cleanUp()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
graph := db.ChannelGraph()
|
|
|
|
|
|
|
|
startTime := time.Unix(1234, 0)
|
|
|
|
endTime := startTime
|
|
|
|
|
|
|
|
// If we issue an arbitrary query before we insert any nodes into the
|
|
|
|
// database, then we shouldn't get any results back.
|
|
|
|
nodeUpdates, err := graph.NodeUpdatesInHorizon(
|
|
|
|
time.Unix(999, 0), time.Unix(9999, 0),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for node updates: %v", err)
|
|
|
|
}
|
|
|
|
if len(nodeUpdates) != 0 {
|
|
|
|
t.Fatalf("expected 0 node updates, instead got %v",
|
|
|
|
len(nodeUpdates))
|
|
|
|
}
|
|
|
|
|
2018-09-06 11:48:46 +03:00
|
|
|
// We'll create 10 node announcements, each with an update timestamp 10
|
2018-04-17 05:04:29 +03:00
|
|
|
// seconds after the other.
|
|
|
|
const numNodes = 10
|
|
|
|
nodeAnns := make([]LightningNode, 0, numNodes)
|
|
|
|
for i := 0; i < numNodes; i++ {
|
|
|
|
nodeAnn, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test vertex: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The node ann will use the current end time as its last
|
|
|
|
// update them, then we'll add 10 seconds in order to create
|
|
|
|
// the proper update time for the next node announcement.
|
|
|
|
updateTime := endTime
|
|
|
|
endTime = updateTime.Add(time.Second * 10)
|
|
|
|
|
|
|
|
nodeAnn.LastUpdate = updateTime
|
|
|
|
|
|
|
|
nodeAnns = append(nodeAnns, *nodeAnn)
|
|
|
|
|
|
|
|
if err := graph.AddLightningNode(nodeAnn); err != nil {
|
|
|
|
t.Fatalf("unable to add lightning node: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
queryCases := []struct {
|
|
|
|
start time.Time
|
|
|
|
end time.Time
|
|
|
|
|
|
|
|
resp []LightningNode
|
|
|
|
}{
|
|
|
|
// If we query for a time range that's strictly below our set
|
|
|
|
// of updates, then we'll get an empty result back.
|
|
|
|
{
|
|
|
|
start: time.Unix(100, 0),
|
|
|
|
end: time.Unix(200, 0),
|
|
|
|
},
|
|
|
|
|
|
|
|
// If we query for a time range that's well beyond our set of
|
|
|
|
// updates, we should get an empty set of results back.
|
|
|
|
{
|
|
|
|
start: time.Unix(99999, 0),
|
|
|
|
end: time.Unix(999999, 0),
|
|
|
|
},
|
|
|
|
|
|
|
|
// If we skip he first time epoch with out start time, then we
|
|
|
|
// should get back every now but the first.
|
|
|
|
{
|
|
|
|
start: startTime.Add(time.Second * 10),
|
|
|
|
end: endTime,
|
|
|
|
|
|
|
|
resp: nodeAnns[1:],
|
|
|
|
},
|
|
|
|
|
|
|
|
// If we query for the range as is, we should get all 10
|
|
|
|
// announcements back.
|
|
|
|
{
|
|
|
|
start: startTime,
|
|
|
|
end: endTime,
|
|
|
|
|
|
|
|
resp: nodeAnns,
|
|
|
|
},
|
|
|
|
|
|
|
|
// If we reduce the ending time by 10 seconds, then we should
|
|
|
|
// get all but the last node we inserted.
|
|
|
|
{
|
|
|
|
start: startTime,
|
|
|
|
end: endTime.Add(-time.Second * 10),
|
|
|
|
|
|
|
|
resp: nodeAnns[:9],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, queryCase := range queryCases {
|
|
|
|
resp, err := graph.NodeUpdatesInHorizon(queryCase.start, queryCase.end)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for nodes: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp) != len(queryCase.resp) {
|
|
|
|
t.Fatalf("expected %v nodes, got %v nodes",
|
|
|
|
len(queryCase.resp), len(resp))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(resp); i++ {
|
|
|
|
err := compareNodes(&queryCase.resp[i], &resp[i])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFilterKnownChanIDs tests that we're able to properly perform the set
|
|
|
|
// differences of an incoming set of channel ID's, and those that we already
|
|
|
|
// know of on disk.
|
|
|
|
func TestFilterKnownChanIDs(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db, cleanUp, err := makeTestDB()
|
|
|
|
defer cleanUp()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
graph := db.ChannelGraph()
|
|
|
|
|
|
|
|
// If we try to filter out a set of channel ID's before we even know of
|
|
|
|
// any channels, then we should get the entire set back.
|
|
|
|
preChanIDs := []uint64{1, 2, 3, 4}
|
|
|
|
filteredIDs, err := graph.FilterKnownChanIDs(preChanIDs)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to filter chan IDs: %v", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(preChanIDs, filteredIDs) {
|
|
|
|
t.Fatalf("chan IDs shouldn't have been filtered!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll start by creating two nodes which will seed our test graph.
|
|
|
|
node1, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
if err := graph.AddLightningNode(node1); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
node2, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
if err := graph.AddLightningNode(node2); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll add 5 channel ID's to the graph, each of them having a
|
|
|
|
// block height 10 blocks after the previous.
|
|
|
|
const numChans = 5
|
|
|
|
chanIDs := make([]uint64, 0, numChans)
|
|
|
|
for i := 0; i < numChans; i++ {
|
|
|
|
channel, chanID := createEdge(
|
|
|
|
uint32(i*10), 0, 0, 0, node1, node2,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := graph.AddChannelEdge(&channel); err != nil {
|
|
|
|
t.Fatalf("unable to create channel edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
chanIDs = append(chanIDs, chanID.ToUint64())
|
|
|
|
}
|
|
|
|
|
2019-04-05 04:34:19 +03:00
|
|
|
const numZombies = 5
|
|
|
|
zombieIDs := make([]uint64, 0, numZombies)
|
|
|
|
for i := 0; i < numZombies; i++ {
|
|
|
|
channel, chanID := createEdge(
|
|
|
|
uint32(i*10+1), 0, 0, 0, node1, node2,
|
|
|
|
)
|
|
|
|
if err := graph.AddChannelEdge(&channel); err != nil {
|
|
|
|
t.Fatalf("unable to create channel edge: %v", err)
|
|
|
|
}
|
2019-04-17 23:26:34 +03:00
|
|
|
err := graph.DeleteChannelEdges(channel.ChannelID)
|
2019-04-10 03:47:12 +03:00
|
|
|
if err != nil {
|
2019-04-05 04:34:19 +03:00
|
|
|
t.Fatalf("unable to mark edge zombie: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
zombieIDs = append(zombieIDs, chanID.ToUint64())
|
|
|
|
}
|
|
|
|
|
2018-04-17 05:04:29 +03:00
|
|
|
queryCases := []struct {
|
|
|
|
queryIDs []uint64
|
|
|
|
|
|
|
|
resp []uint64
|
|
|
|
}{
|
|
|
|
// If we attempt to filter out all chanIDs we know of, the
|
|
|
|
// response should be the empty set.
|
|
|
|
{
|
|
|
|
queryIDs: chanIDs,
|
|
|
|
},
|
2019-04-05 04:34:19 +03:00
|
|
|
// If we attempt to filter out all zombies that we know of, the
|
|
|
|
// response should be the empty set.
|
|
|
|
{
|
|
|
|
queryIDs: zombieIDs,
|
|
|
|
},
|
2018-04-17 05:04:29 +03:00
|
|
|
|
|
|
|
// If we query for a set of ID's that we didn't insert, we
|
|
|
|
// should get the same set back.
|
|
|
|
{
|
|
|
|
queryIDs: []uint64{99, 100},
|
|
|
|
resp: []uint64{99, 100},
|
|
|
|
},
|
|
|
|
|
|
|
|
// If we query for a super-set of our the chan ID's inserted,
|
|
|
|
// we should only get those new chanIDs back.
|
|
|
|
{
|
|
|
|
queryIDs: append(chanIDs, []uint64{99, 101}...),
|
|
|
|
resp: []uint64{99, 101},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, queryCase := range queryCases {
|
|
|
|
resp, err := graph.FilterKnownChanIDs(queryCase.queryIDs)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to filter chan IDs: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(resp, queryCase.resp) {
|
|
|
|
t.Fatalf("expected %v, got %v", spew.Sdump(queryCase.resp),
|
|
|
|
spew.Sdump(resp))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFilterChannelRange tests that we're able to properly retrieve the full
|
|
|
|
// set of short channel ID's for a given block range.
|
|
|
|
func TestFilterChannelRange(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db, cleanUp, err := makeTestDB()
|
|
|
|
defer cleanUp()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
graph := db.ChannelGraph()
|
|
|
|
|
|
|
|
// We'll first populate our graph with two nodes. All channels created
|
|
|
|
// below will be made between these two nodes.
|
|
|
|
node1, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
if err := graph.AddLightningNode(node1); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
node2, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
if err := graph.AddLightningNode(node2); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we try to filter a channel range before we have any channels
|
|
|
|
// inserted, we should get an empty slice of results.
|
|
|
|
resp, err := graph.FilterChannelRange(10, 100)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to filter channels: %v", err)
|
|
|
|
}
|
|
|
|
if len(resp) != 0 {
|
|
|
|
t.Fatalf("expected zero chans, instead got %v", len(resp))
|
|
|
|
}
|
|
|
|
|
|
|
|
// To start, we'll create a set of channels, each mined in a block 10
|
|
|
|
// blocks after the prior one.
|
|
|
|
startHeight := uint32(100)
|
|
|
|
endHeight := startHeight
|
|
|
|
const numChans = 10
|
|
|
|
chanIDs := make([]uint64, 0, numChans)
|
|
|
|
for i := 0; i < numChans; i++ {
|
|
|
|
chanHeight := endHeight
|
|
|
|
channel, chanID := createEdge(
|
|
|
|
uint32(chanHeight), uint32(i+1), 0, 0, node1, node2,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := graph.AddChannelEdge(&channel); err != nil {
|
|
|
|
t.Fatalf("unable to create channel edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
chanIDs = append(chanIDs, chanID.ToUint64())
|
|
|
|
|
|
|
|
endHeight += 10
|
|
|
|
}
|
|
|
|
|
|
|
|
// With our channels inserted, we'll construct a series of queries that
|
|
|
|
// we'll execute below in order to exercise the features of the
|
|
|
|
// FilterKnownChanIDs method.
|
|
|
|
queryCases := []struct {
|
|
|
|
startHeight uint32
|
|
|
|
endHeight uint32
|
|
|
|
|
|
|
|
resp []uint64
|
|
|
|
}{
|
|
|
|
// If we query for the entire range, then we should get the same
|
|
|
|
// set of short channel IDs back.
|
|
|
|
{
|
|
|
|
startHeight: startHeight,
|
|
|
|
endHeight: endHeight,
|
|
|
|
|
|
|
|
resp: chanIDs,
|
|
|
|
},
|
|
|
|
|
|
|
|
// If we query for a range of channels right before our range, we
|
|
|
|
// shouldn't get any results back.
|
|
|
|
{
|
|
|
|
startHeight: 0,
|
|
|
|
endHeight: 10,
|
|
|
|
},
|
|
|
|
|
|
|
|
// If we only query for the last height (range wise), we should
|
|
|
|
// only get that last channel.
|
|
|
|
{
|
|
|
|
startHeight: endHeight - 10,
|
|
|
|
endHeight: endHeight - 10,
|
|
|
|
|
|
|
|
resp: chanIDs[9:],
|
|
|
|
},
|
|
|
|
|
|
|
|
// If we query for just the first height, we should only get a
|
|
|
|
// single channel back (the first one).
|
|
|
|
{
|
|
|
|
startHeight: startHeight,
|
|
|
|
endHeight: startHeight,
|
|
|
|
|
|
|
|
resp: chanIDs[:1],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, queryCase := range queryCases {
|
|
|
|
resp, err := graph.FilterChannelRange(
|
|
|
|
queryCase.startHeight, queryCase.endHeight,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to issue range query: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(resp, queryCase.resp) {
|
|
|
|
t.Fatalf("case #%v: expected %v, got %v", i,
|
|
|
|
queryCase.resp, resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFetchChanInfos tests that we're able to properly retrieve the full set
|
|
|
|
// of ChannelEdge structs for a given set of short channel ID's.
|
|
|
|
func TestFetchChanInfos(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db, cleanUp, err := makeTestDB()
|
|
|
|
defer cleanUp()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
graph := db.ChannelGraph()
|
|
|
|
|
|
|
|
// We'll first populate our graph with two nodes. All channels created
|
|
|
|
// below will be made between these two nodes.
|
|
|
|
node1, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
if err := graph.AddLightningNode(node1); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
node2, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
if err := graph.AddLightningNode(node2); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll make 5 test channels, ensuring we keep track of which channel
|
|
|
|
// ID corresponds to a particular ChannelEdge.
|
|
|
|
const numChans = 5
|
|
|
|
startTime := time.Unix(1234, 0)
|
|
|
|
endTime := startTime
|
|
|
|
edges := make([]ChannelEdge, 0, numChans)
|
|
|
|
edgeQuery := make([]uint64, 0, numChans)
|
|
|
|
for i := 0; i < numChans; i++ {
|
|
|
|
txHash := sha256.Sum256([]byte{byte(i)})
|
|
|
|
op := wire.OutPoint{
|
|
|
|
Hash: txHash,
|
|
|
|
Index: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
channel, chanID := createEdge(
|
|
|
|
uint32(i*10), 0, 0, 0, node1, node2,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := graph.AddChannelEdge(&channel); err != nil {
|
|
|
|
t.Fatalf("unable to create channel edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
updateTime := endTime
|
|
|
|
endTime = updateTime.Add(time.Second * 10)
|
|
|
|
|
|
|
|
edge1 := newEdgePolicy(
|
|
|
|
chanID.ToUint64(), op, db, updateTime.Unix(),
|
|
|
|
)
|
2019-01-12 20:59:43 +03:00
|
|
|
edge1.ChannelFlags = 0
|
2018-04-17 05:04:29 +03:00
|
|
|
edge1.Node = node2
|
|
|
|
edge1.SigBytes = testSig.Serialize()
|
|
|
|
if err := graph.UpdateEdgePolicy(edge1); err != nil {
|
|
|
|
t.Fatalf("unable to update edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
edge2 := newEdgePolicy(
|
|
|
|
chanID.ToUint64(), op, db, updateTime.Unix(),
|
|
|
|
)
|
2019-01-12 20:59:43 +03:00
|
|
|
edge2.ChannelFlags = 1
|
2018-04-17 05:04:29 +03:00
|
|
|
edge2.Node = node1
|
|
|
|
edge2.SigBytes = testSig.Serialize()
|
|
|
|
if err := graph.UpdateEdgePolicy(edge2); err != nil {
|
|
|
|
t.Fatalf("unable to update edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
edges = append(edges, ChannelEdge{
|
|
|
|
Info: &channel,
|
|
|
|
Policy1: edge1,
|
|
|
|
Policy2: edge2,
|
|
|
|
})
|
|
|
|
|
|
|
|
edgeQuery = append(edgeQuery, chanID.ToUint64())
|
|
|
|
}
|
|
|
|
|
2019-04-10 03:11:58 +03:00
|
|
|
// Add an additional edge that does not exist. The query should skip
|
|
|
|
// this channel and return only infos for the edges that exist.
|
|
|
|
edgeQuery = append(edgeQuery, 500)
|
|
|
|
|
|
|
|
// Add an another edge to the query that has been marked as a zombie
|
|
|
|
// edge. The query should also skip this channel.
|
|
|
|
zombieChan, zombieChanID := createEdge(
|
|
|
|
666, 0, 0, 0, node1, node2,
|
|
|
|
)
|
|
|
|
if err := graph.AddChannelEdge(&zombieChan); err != nil {
|
|
|
|
t.Fatalf("unable to create channel edge: %v", err)
|
|
|
|
}
|
2019-04-17 23:26:34 +03:00
|
|
|
err = graph.DeleteChannelEdges(zombieChan.ChannelID)
|
2019-04-10 03:11:58 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to delete and mark edge zombie: %v", err)
|
|
|
|
}
|
|
|
|
edgeQuery = append(edgeQuery, zombieChanID.ToUint64())
|
|
|
|
|
2018-04-17 05:04:29 +03:00
|
|
|
// We'll now attempt to query for the range of channel ID's we just
|
|
|
|
// inserted into the database. We should get the exact same set of
|
|
|
|
// edges back.
|
|
|
|
resp, err := graph.FetchChanInfos(edgeQuery)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch chan edges: %v", err)
|
|
|
|
}
|
|
|
|
if len(resp) != len(edges) {
|
|
|
|
t.Fatalf("expected %v edges, instead got %v", len(edges),
|
|
|
|
len(resp))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(resp); i++ {
|
|
|
|
err := compareEdgePolicies(resp[i].Policy1, edges[i].Policy1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("edge doesn't match: %v", err)
|
|
|
|
}
|
|
|
|
err = compareEdgePolicies(resp[i].Policy2, edges[i].Policy2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("edge doesn't match: %v", err)
|
|
|
|
}
|
|
|
|
assertEdgeInfoEqual(t, resp[i].Info, edges[i].Info)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-18 13:35:22 +03:00
|
|
|
// TestIncompleteChannelPolicies tests that a channel that only has a policy
|
|
|
|
// specified on one end is properly returned in ForEachChannel calls from
|
|
|
|
// both sides.
|
|
|
|
func TestIncompleteChannelPolicies(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db, cleanUp, err := makeTestDB()
|
|
|
|
defer cleanUp()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
graph := db.ChannelGraph()
|
|
|
|
|
|
|
|
// Create two nodes.
|
|
|
|
node1, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
if err := graph.AddLightningNode(node1); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
node2, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
if err := graph.AddLightningNode(node2); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create channel between nodes.
|
|
|
|
txHash := sha256.Sum256([]byte{0})
|
|
|
|
op := wire.OutPoint{
|
|
|
|
Hash: txHash,
|
|
|
|
Index: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
channel, chanID := createEdge(
|
|
|
|
uint32(0), 0, 0, 0, node1, node2,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := graph.AddChannelEdge(&channel); err != nil {
|
|
|
|
t.Fatalf("unable to create channel edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that channel is reported with unknown policies.
|
|
|
|
|
|
|
|
checkPolicies := func(node *LightningNode, expectedIn, expectedOut bool) {
|
|
|
|
calls := 0
|
2018-11-30 07:04:21 +03:00
|
|
|
node.ForEachChannel(nil, func(_ *bbolt.Tx, _ *ChannelEdgeInfo,
|
2018-06-18 13:35:22 +03:00
|
|
|
outEdge, inEdge *ChannelEdgePolicy) error {
|
|
|
|
|
|
|
|
if !expectedOut && outEdge != nil {
|
|
|
|
t.Fatalf("Expected no outgoing policy")
|
|
|
|
}
|
|
|
|
|
|
|
|
if expectedOut && outEdge == nil {
|
|
|
|
t.Fatalf("Expected an outgoing policy")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !expectedIn && inEdge != nil {
|
|
|
|
t.Fatalf("Expected no incoming policy")
|
|
|
|
}
|
|
|
|
|
|
|
|
if expectedIn && inEdge == nil {
|
|
|
|
t.Fatalf("Expected an incoming policy")
|
|
|
|
}
|
|
|
|
|
|
|
|
calls++
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if calls != 1 {
|
|
|
|
t.Fatalf("Expected only one callback call")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkPolicies(node2, false, false)
|
|
|
|
|
|
|
|
// Only create an edge policy for node1 and leave the policy for node2
|
|
|
|
// unknown.
|
|
|
|
updateTime := time.Unix(1234, 0)
|
|
|
|
|
|
|
|
edgePolicy := newEdgePolicy(
|
|
|
|
chanID.ToUint64(), op, db, updateTime.Unix(),
|
|
|
|
)
|
2019-01-12 20:59:43 +03:00
|
|
|
edgePolicy.ChannelFlags = 0
|
2018-06-18 13:35:22 +03:00
|
|
|
edgePolicy.Node = node2
|
|
|
|
edgePolicy.SigBytes = testSig.Serialize()
|
|
|
|
if err := graph.UpdateEdgePolicy(edgePolicy); err != nil {
|
|
|
|
t.Fatalf("unable to update edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
checkPolicies(node1, false, true)
|
|
|
|
checkPolicies(node2, true, false)
|
|
|
|
|
|
|
|
// Create second policy and assert that both policies are reported
|
|
|
|
// as present.
|
|
|
|
edgePolicy = newEdgePolicy(
|
|
|
|
chanID.ToUint64(), op, db, updateTime.Unix(),
|
|
|
|
)
|
2019-01-12 20:59:43 +03:00
|
|
|
edgePolicy.ChannelFlags = 1
|
2018-06-18 13:35:22 +03:00
|
|
|
edgePolicy.Node = node1
|
|
|
|
edgePolicy.SigBytes = testSig.Serialize()
|
|
|
|
if err := graph.UpdateEdgePolicy(edgePolicy); err != nil {
|
|
|
|
t.Fatalf("unable to update edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
checkPolicies(node1, true, true)
|
|
|
|
checkPolicies(node2, true, true)
|
|
|
|
}
|
|
|
|
|
2018-06-06 03:30:50 +03:00
|
|
|
// TestChannelEdgePruningUpdateIndexDeletion tests that once edges are deleted
|
|
|
|
// from the graph, then their entries within the update index are also cleaned
|
|
|
|
// up.
|
|
|
|
func TestChannelEdgePruningUpdateIndexDeletion(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db, cleanUp, err := makeTestDB()
|
|
|
|
defer cleanUp()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
graph := db.ChannelGraph()
|
2018-06-28 06:05:45 +03:00
|
|
|
sourceNode, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create source node: %v", err)
|
|
|
|
}
|
|
|
|
if err := graph.SetSourceNode(sourceNode); err != nil {
|
|
|
|
t.Fatalf("unable to set source node: %v", err)
|
|
|
|
}
|
2018-06-06 03:30:50 +03:00
|
|
|
|
|
|
|
// We'll first populate our graph with two nodes. All channels created
|
|
|
|
// below will be made between these two nodes.
|
|
|
|
node1, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
if err := graph.AddLightningNode(node1); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
node2, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
if err := graph.AddLightningNode(node2); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the two nodes created, we'll now create a random channel, as
|
|
|
|
// well as two edges in the database with distinct update times.
|
|
|
|
edgeInfo, chanID := createEdge(100, 0, 0, 0, node1, node2)
|
|
|
|
if err := graph.AddChannelEdge(&edgeInfo); err != nil {
|
|
|
|
t.Fatalf("unable to add edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
edge1 := randEdgePolicy(chanID.ToUint64(), edgeInfo.ChannelPoint, db)
|
2019-01-12 20:59:43 +03:00
|
|
|
edge1.ChannelFlags = 0
|
2018-06-06 03:30:50 +03:00
|
|
|
edge1.Node = node1
|
|
|
|
edge1.SigBytes = testSig.Serialize()
|
|
|
|
if err := graph.UpdateEdgePolicy(edge1); err != nil {
|
|
|
|
t.Fatalf("unable to update edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
edge2 := randEdgePolicy(chanID.ToUint64(), edgeInfo.ChannelPoint, db)
|
2019-01-12 20:59:43 +03:00
|
|
|
edge2.ChannelFlags = 1
|
2018-06-06 03:30:50 +03:00
|
|
|
edge2.Node = node2
|
|
|
|
edge2.SigBytes = testSig.Serialize()
|
|
|
|
if err := graph.UpdateEdgePolicy(edge2); err != nil {
|
|
|
|
t.Fatalf("unable to update edge: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-09-01 00:59:35 +03:00
|
|
|
// checkIndexTimestamps is a helper function that checks the edge update
|
|
|
|
// index only includes the given timestamps.
|
|
|
|
checkIndexTimestamps := func(timestamps ...uint64) {
|
|
|
|
timestampSet := make(map[uint64]struct{})
|
|
|
|
for _, t := range timestamps {
|
|
|
|
timestampSet[t] = struct{}{}
|
2018-06-06 03:30:50 +03:00
|
|
|
}
|
|
|
|
|
2018-11-30 07:04:21 +03:00
|
|
|
err := db.View(func(tx *bbolt.Tx) error {
|
2018-09-01 00:59:35 +03:00
|
|
|
edges := tx.Bucket(edgeBucket)
|
|
|
|
if edges == nil {
|
|
|
|
return ErrGraphNoEdgesFound
|
|
|
|
}
|
|
|
|
edgeUpdateIndex := edges.Bucket(edgeUpdateIndexBucket)
|
|
|
|
if edgeUpdateIndex == nil {
|
|
|
|
return ErrGraphNoEdgesFound
|
|
|
|
}
|
2018-06-06 03:30:50 +03:00
|
|
|
|
2018-09-01 00:59:35 +03:00
|
|
|
numEntries := edgeUpdateIndex.Stats().KeyN
|
|
|
|
expectedEntries := len(timestampSet)
|
|
|
|
if numEntries != expectedEntries {
|
|
|
|
return fmt.Errorf("expected %v entries in the "+
|
|
|
|
"update index, got %v", expectedEntries,
|
|
|
|
numEntries)
|
|
|
|
}
|
2018-06-06 03:30:50 +03:00
|
|
|
|
2018-09-01 00:59:35 +03:00
|
|
|
return edgeUpdateIndex.ForEach(func(k, _ []byte) error {
|
|
|
|
t := byteOrder.Uint64(k[:8])
|
|
|
|
if _, ok := timestampSet[t]; !ok {
|
|
|
|
return fmt.Errorf("found unexpected "+
|
|
|
|
"timestamp "+"%d", t)
|
|
|
|
}
|
2018-06-06 03:30:50 +03:00
|
|
|
|
2018-09-01 00:59:35 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2018-06-06 03:30:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-01 00:59:35 +03:00
|
|
|
// With both edges policies added, we'll make sure to check they exist
|
|
|
|
// within the edge update index.
|
|
|
|
checkIndexTimestamps(
|
|
|
|
uint64(edge1.LastUpdate.Unix()),
|
2018-09-01 00:59:36 +03:00
|
|
|
uint64(edge2.LastUpdate.Unix()),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Now, we'll update the edge policies to ensure the old timestamps are
|
|
|
|
// removed from the update index.
|
2019-01-12 20:59:43 +03:00
|
|
|
edge1.ChannelFlags = 2
|
2018-09-01 00:59:36 +03:00
|
|
|
edge1.LastUpdate = time.Now()
|
|
|
|
if err := graph.UpdateEdgePolicy(edge1); err != nil {
|
|
|
|
t.Fatalf("unable to update edge: %v", err)
|
|
|
|
}
|
2019-01-12 20:59:43 +03:00
|
|
|
edge2.ChannelFlags = 3
|
2018-09-01 00:59:36 +03:00
|
|
|
edge2.LastUpdate = edge1.LastUpdate.Add(time.Hour)
|
|
|
|
if err := graph.UpdateEdgePolicy(edge2); err != nil {
|
|
|
|
t.Fatalf("unable to update edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the policies updated, we should now be able to find their
|
|
|
|
// updated entries within the update index.
|
|
|
|
checkIndexTimestamps(
|
|
|
|
uint64(edge1.LastUpdate.Unix()),
|
2018-09-01 00:59:35 +03:00
|
|
|
uint64(edge2.LastUpdate.Unix()),
|
|
|
|
)
|
|
|
|
|
2018-06-06 03:30:50 +03:00
|
|
|
// Now we'll prune the graph, removing the edges, and also the update
|
|
|
|
// index entries from the database all together.
|
|
|
|
var blockHash chainhash.Hash
|
|
|
|
copy(blockHash[:], bytes.Repeat([]byte{2}, 32))
|
|
|
|
_, err = graph.PruneGraph(
|
|
|
|
[]*wire.OutPoint{&edgeInfo.ChannelPoint}, &blockHash, 101,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to prune graph: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-09-01 00:59:35 +03:00
|
|
|
// Finally, we'll check the database state one last time to conclude
|
|
|
|
// that we should no longer be able to locate _any_ entries within the
|
|
|
|
// edge update index.
|
|
|
|
checkIndexTimestamps()
|
2018-06-06 03:30:50 +03:00
|
|
|
}
|
|
|
|
|
2018-07-22 05:49:54 +03:00
|
|
|
// TestPruneGraphNodes tests that unconnected vertexes are pruned via the
|
|
|
|
// PruneSyncState method.
|
|
|
|
func TestPruneGraphNodes(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db, cleanUp, err := makeTestDB()
|
|
|
|
defer cleanUp()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll start off by inserting our source node, to ensure that it's
|
|
|
|
// the only node left after we prune the graph.
|
|
|
|
graph := db.ChannelGraph()
|
|
|
|
sourceNode, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create source node: %v", err)
|
|
|
|
}
|
|
|
|
if err := graph.SetSourceNode(sourceNode); err != nil {
|
|
|
|
t.Fatalf("unable to set source node: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-07-23 04:57:10 +03:00
|
|
|
// With the source node inserted, we'll now add three nodes to the
|
|
|
|
// channel graph, at the end of the scenario, only two of these nodes
|
|
|
|
// should still be in the graph.
|
2018-07-22 05:49:54 +03:00
|
|
|
node1, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
if err := graph.AddLightningNode(node1); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
node2, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
if err := graph.AddLightningNode(node2); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
2018-07-23 04:57:10 +03:00
|
|
|
node3, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
if err := graph.AddLightningNode(node3); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll now add a new edge to the graph, but only actually advertise
|
|
|
|
// the edge of *one* of the nodes.
|
|
|
|
edgeInfo, chanID := createEdge(100, 0, 0, 0, node1, node2)
|
|
|
|
if err := graph.AddChannelEdge(&edgeInfo); err != nil {
|
|
|
|
t.Fatalf("unable to add edge: %v", err)
|
|
|
|
}
|
2018-07-22 05:49:54 +03:00
|
|
|
|
2018-07-23 04:57:10 +03:00
|
|
|
// We'll now insert an advertised edge, but it'll only be the edge that
|
|
|
|
// points from the first to the second node.
|
|
|
|
edge1 := randEdgePolicy(chanID.ToUint64(), edgeInfo.ChannelPoint, db)
|
2019-01-12 20:59:43 +03:00
|
|
|
edge1.ChannelFlags = 0
|
2018-07-23 04:57:10 +03:00
|
|
|
edge1.Node = node1
|
|
|
|
edge1.SigBytes = testSig.Serialize()
|
|
|
|
if err := graph.UpdateEdgePolicy(edge1); err != nil {
|
|
|
|
t.Fatalf("unable to update edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll now initiate a around of graph pruning.
|
2018-07-22 05:49:54 +03:00
|
|
|
if err := graph.PruneGraphNodes(); err != nil {
|
|
|
|
t.Fatalf("unable to prune graph nodes: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-07-23 04:57:10 +03:00
|
|
|
// At this point, there should be 3 nodes left in the graph still: the
|
|
|
|
// source node (which can't be pruned), and node 1+2. Nodes 1 and two
|
|
|
|
// should still be left in the graph as there's half of an advertised
|
|
|
|
// edge between them.
|
|
|
|
assertNumNodes(t, graph, 3)
|
|
|
|
|
|
|
|
// Finally, we'll ensure that node3, the only fully unconnected node as
|
|
|
|
// properly deleted from the graph and not another node in its place.
|
|
|
|
node3Pub, err := node3.PubKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch the pubkey of node3: %v", err)
|
|
|
|
}
|
|
|
|
if _, err := graph.FetchLightningNode(node3Pub); err == nil {
|
|
|
|
t.Fatalf("node 3 should have been deleted!")
|
|
|
|
}
|
2018-07-22 05:49:54 +03:00
|
|
|
}
|
|
|
|
|
2018-07-23 07:05:40 +03:00
|
|
|
// TestAddChannelEdgeShellNodes tests that when we attempt to add a ChannelEdge
|
|
|
|
// to the graph, one or both of the nodes the edge involves aren't found in the
|
|
|
|
// database, then shell edges are created for each node if needed.
|
|
|
|
func TestAddChannelEdgeShellNodes(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db, cleanUp, err := makeTestDB()
|
|
|
|
defer cleanUp()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
graph := db.ChannelGraph()
|
|
|
|
|
|
|
|
// To start, we'll create two nodes, and only add one of them to the
|
|
|
|
// channel graph.
|
|
|
|
node1, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
if err := graph.AddLightningNode(node1); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
node2, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll now create an edge between the two nodes, as a result, node2
|
|
|
|
// should be inserted into the database as a shell node.
|
|
|
|
edgeInfo, _ := createEdge(100, 0, 0, 0, node1, node2)
|
|
|
|
if err := graph.AddChannelEdge(&edgeInfo); err != nil {
|
|
|
|
t.Fatalf("unable to add edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
node1Pub, err := node1.PubKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to parse node 1 pub: %v", err)
|
|
|
|
}
|
|
|
|
node2Pub, err := node2.PubKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to parse node 2 pub: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that node1 was inserted as a full node, while node2 only has
|
|
|
|
// a shell node present.
|
|
|
|
node1, err = graph.FetchLightningNode(node1Pub)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch node1: %v", err)
|
|
|
|
}
|
|
|
|
if !node1.HaveNodeAnnouncement {
|
|
|
|
t.Fatalf("have shell announcement for node1, shouldn't")
|
|
|
|
}
|
|
|
|
|
|
|
|
node2, err = graph.FetchLightningNode(node2Pub)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch node2: %v", err)
|
|
|
|
}
|
|
|
|
if node2.HaveNodeAnnouncement {
|
|
|
|
t.Fatalf("should have shell announcement for node2, but is full")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-17 04:20:51 +03:00
|
|
|
// TestNodePruningUpdateIndexDeletion tests that once a node has been removed
|
|
|
|
// from the channel graph, we also remove the entry from the update index as
|
|
|
|
// well.
|
|
|
|
func TestNodePruningUpdateIndexDeletion(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db, cleanUp, err := makeTestDB()
|
|
|
|
defer cleanUp()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
graph := db.ChannelGraph()
|
|
|
|
|
|
|
|
// We'll first populate our graph with a single node that will be
|
|
|
|
// removed shortly.
|
|
|
|
node1, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
if err := graph.AddLightningNode(node1); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll confirm that we can retrieve the node using
|
|
|
|
// NodeUpdatesInHorizon, using a time that's slightly beyond the last
|
|
|
|
// update time of our test node.
|
|
|
|
startTime := time.Unix(9, 0)
|
|
|
|
endTime := node1.LastUpdate.Add(time.Minute)
|
|
|
|
nodesInHorizon, err := graph.NodeUpdatesInHorizon(startTime, endTime)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch nodes in horizon: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should only have a single node, and that node should exactly
|
|
|
|
// match the node we just inserted.
|
|
|
|
if len(nodesInHorizon) != 1 {
|
|
|
|
t.Fatalf("should have 1 nodes instead have: %v",
|
|
|
|
len(nodesInHorizon))
|
|
|
|
}
|
|
|
|
if err := compareNodes(node1, &nodesInHorizon[0]); err != nil {
|
|
|
|
t.Fatalf("nodes don't match: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll now delete the node from the graph, this should result in it
|
|
|
|
// being removed from the update index as well.
|
|
|
|
nodePub, _ := node1.PubKey()
|
|
|
|
if err := graph.DeleteLightningNode(nodePub); err != nil {
|
|
|
|
t.Fatalf("unable to delete node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that the node has been deleted, we'll again query the nodes in
|
|
|
|
// the horizon. This time we should have no nodes at all.
|
|
|
|
nodesInHorizon, err = graph.NodeUpdatesInHorizon(startTime, endTime)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch nodes in horizon: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(nodesInHorizon) != 0 {
|
|
|
|
t.Fatalf("should have zero nodes instead have: %v",
|
|
|
|
len(nodesInHorizon))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 01:35:54 +03:00
|
|
|
// TestNodeIsPublic ensures that we properly detect nodes that are seen as
|
|
|
|
// public within the network graph.
|
|
|
|
func TestNodeIsPublic(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// We'll start off the test by creating a small network of 3
|
|
|
|
// participants with the following graph:
|
|
|
|
//
|
|
|
|
// Alice <-> Bob <-> Carol
|
|
|
|
//
|
|
|
|
// We'll need to create a separate database and channel graph for each
|
|
|
|
// participant to replicate real-world scenarios (private edges being in
|
|
|
|
// some graphs but not others, etc.).
|
|
|
|
aliceDB, cleanUp, err := makeTestDB()
|
|
|
|
defer cleanUp()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
aliceNode, err := createTestVertex(aliceDB)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
aliceGraph := aliceDB.ChannelGraph()
|
|
|
|
if err := aliceGraph.SetSourceNode(aliceNode); err != nil {
|
|
|
|
t.Fatalf("unable to set source node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bobDB, cleanUp, err := makeTestDB()
|
|
|
|
defer cleanUp()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
bobNode, err := createTestVertex(bobDB)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
bobGraph := bobDB.ChannelGraph()
|
|
|
|
if err := bobGraph.SetSourceNode(bobNode); err != nil {
|
|
|
|
t.Fatalf("unable to set source node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
carolDB, cleanUp, err := makeTestDB()
|
|
|
|
defer cleanUp()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
carolNode, err := createTestVertex(carolDB)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
carolGraph := carolDB.ChannelGraph()
|
|
|
|
if err := carolGraph.SetSourceNode(carolNode); err != nil {
|
|
|
|
t.Fatalf("unable to set source node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
aliceBobEdge, _ := createEdge(10, 0, 0, 0, aliceNode, bobNode)
|
|
|
|
bobCarolEdge, _ := createEdge(10, 1, 0, 1, bobNode, carolNode)
|
|
|
|
|
|
|
|
// After creating all of our nodes and edges, we'll add them to each
|
|
|
|
// participant's graph.
|
|
|
|
nodes := []*LightningNode{aliceNode, bobNode, carolNode}
|
|
|
|
edges := []*ChannelEdgeInfo{&aliceBobEdge, &bobCarolEdge}
|
|
|
|
dbs := []*DB{aliceDB, bobDB, carolDB}
|
|
|
|
graphs := []*ChannelGraph{aliceGraph, bobGraph, carolGraph}
|
|
|
|
for i, graph := range graphs {
|
|
|
|
for _, node := range nodes {
|
|
|
|
node.db = dbs[i]
|
|
|
|
if err := graph.AddLightningNode(node); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, edge := range edges {
|
|
|
|
edge.db = dbs[i]
|
|
|
|
if err := graph.AddChannelEdge(edge); err != nil {
|
|
|
|
t.Fatalf("unable to add edge: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkNodes is a helper closure that will be used to assert that the
|
|
|
|
// given nodes are seen as public/private within the given graphs.
|
|
|
|
checkNodes := func(nodes []*LightningNode, graphs []*ChannelGraph,
|
|
|
|
public bool) {
|
|
|
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
for _, node := range nodes {
|
|
|
|
for _, graph := range graphs {
|
|
|
|
isPublic, err := graph.IsPublicNode(node.PubKeyBytes)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to determine if pivot "+
|
|
|
|
"is public: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case isPublic && !public:
|
|
|
|
t.Fatalf("expected %x to be private",
|
|
|
|
node.PubKeyBytes)
|
|
|
|
case !isPublic && public:
|
|
|
|
t.Fatalf("expected %x to be public",
|
|
|
|
node.PubKeyBytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Due to the way the edges were set up above, we'll make sure each node
|
|
|
|
// can correctly determine that every other node is public.
|
|
|
|
checkNodes(nodes, graphs, true)
|
|
|
|
|
|
|
|
// Now, we'll remove the edge between Alice and Bob from everyone's
|
|
|
|
// graph. This will make Alice be seen as a private node as it no longer
|
|
|
|
// has any advertised edges.
|
|
|
|
for _, graph := range graphs {
|
2019-04-17 23:26:34 +03:00
|
|
|
err := graph.DeleteChannelEdges(aliceBobEdge.ChannelID)
|
2018-10-18 01:35:54 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to remove edge: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
checkNodes(
|
|
|
|
[]*LightningNode{aliceNode},
|
|
|
|
[]*ChannelGraph{bobGraph, carolGraph},
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
|
|
|
|
// We'll also make the edge between Bob and Carol private. Within Bob's
|
|
|
|
// and Carol's graph, the edge will exist, but it will not have a proof
|
|
|
|
// that allows it to be advertised. Within Alice's graph, we'll
|
|
|
|
// completely remove the edge as it is not possible for her to know of
|
|
|
|
// it without it being advertised.
|
|
|
|
for i, graph := range graphs {
|
2019-04-17 23:26:34 +03:00
|
|
|
err := graph.DeleteChannelEdges(bobCarolEdge.ChannelID)
|
2018-10-18 01:35:54 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to remove edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if graph == aliceGraph {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
bobCarolEdge.AuthProof = nil
|
|
|
|
bobCarolEdge.db = dbs[i]
|
|
|
|
if err := graph.AddChannelEdge(&bobCarolEdge); err != nil {
|
|
|
|
t.Fatalf("unable to add edge: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the modifications above, Bob should now be seen as a private
|
|
|
|
// node from both Alice's and Carol's perspective.
|
|
|
|
checkNodes(
|
|
|
|
[]*LightningNode{bobNode},
|
|
|
|
[]*ChannelGraph{aliceGraph, carolGraph},
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-06-12 13:40:02 +03:00
|
|
|
// TestDisabledChannelIDs ensures that the disabled channels within the
|
|
|
|
// disabledEdgePolicyBucket are managed properly and the list returned from
|
|
|
|
// DisabledChannelIDs is correct.
|
|
|
|
func TestDisabledChannelIDs(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db, cleanUp, err := makeTestDB()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
graph := db.ChannelGraph()
|
|
|
|
|
|
|
|
// Create first node and add it to the graph.
|
|
|
|
node1, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
if err := graph.AddLightningNode(node1); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create second node and add it to the graph.
|
|
|
|
node2, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
if err := graph.AddLightningNode(node2); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adding a new channel edge to the graph.
|
|
|
|
edgeInfo, edge1, edge2 := createChannelEdge(db, node1, node2)
|
|
|
|
if err := graph.AddLightningNode(node2); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := graph.AddChannelEdge(edgeInfo); err != nil {
|
|
|
|
t.Fatalf("unable to create channel edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure no disabled channels exist in the bucket on start.
|
|
|
|
disabledChanIds, err := graph.DisabledChannelIDs()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get disabled channel ids: %v", err)
|
|
|
|
}
|
|
|
|
if len(disabledChanIds) > 0 {
|
|
|
|
t.Fatalf("expected empty disabled channels, got %v disabled channels",
|
|
|
|
len(disabledChanIds))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add one disabled policy and ensure the channel is still not in the
|
|
|
|
// disabled list.
|
|
|
|
edge1.ChannelFlags |= lnwire.ChanUpdateDisabled
|
|
|
|
if err := graph.UpdateEdgePolicy(edge1); err != nil {
|
|
|
|
t.Fatalf("unable to update edge: %v", err)
|
|
|
|
}
|
|
|
|
disabledChanIds, err = graph.DisabledChannelIDs()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get disabled channel ids: %v", err)
|
|
|
|
}
|
|
|
|
if len(disabledChanIds) > 0 {
|
|
|
|
t.Fatalf("expected empty disabled channels, got %v disabled channels",
|
|
|
|
len(disabledChanIds))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add second disabled policy and ensure the channel is now in the
|
|
|
|
// disabled list.
|
|
|
|
edge2.ChannelFlags |= lnwire.ChanUpdateDisabled
|
|
|
|
if err := graph.UpdateEdgePolicy(edge2); err != nil {
|
|
|
|
t.Fatalf("unable to update edge: %v", err)
|
|
|
|
}
|
|
|
|
disabledChanIds, err = graph.DisabledChannelIDs()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get disabled channel ids: %v", err)
|
|
|
|
}
|
|
|
|
if len(disabledChanIds) != 1 || disabledChanIds[0] != edgeInfo.ChannelID {
|
|
|
|
t.Fatalf("expected disabled channel with id %v, "+
|
|
|
|
"got %v", edgeInfo.ChannelID, disabledChanIds)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the channel edge and ensure it is removed from the disabled list.
|
|
|
|
if err = graph.DeleteChannelEdges(edgeInfo.ChannelID); err != nil {
|
|
|
|
t.Fatalf("unable to delete channel edge: %v", err)
|
|
|
|
}
|
|
|
|
disabledChanIds, err = graph.DisabledChannelIDs()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get disabled channel ids: %v", err)
|
|
|
|
}
|
|
|
|
if len(disabledChanIds) > 0 {
|
|
|
|
t.Fatalf("expected empty disabled channels, got %v disabled channels",
|
|
|
|
len(disabledChanIds))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 20:59:44 +03:00
|
|
|
// TestEdgePolicyMissingMaxHtcl tests that if we find a ChannelEdgePolicy in
|
|
|
|
// the DB that indicates that it should support the htlc_maximum_value_msat
|
|
|
|
// field, but it is not part of the opaque data, then we'll handle it as it is
|
|
|
|
// unknown. It also checks that we are correctly able to overwrite it when we
|
|
|
|
// receive the proper update.
|
|
|
|
func TestEdgePolicyMissingMaxHtcl(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db, cleanUp, err := makeTestDB()
|
|
|
|
defer cleanUp()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
graph := db.ChannelGraph()
|
|
|
|
|
|
|
|
// We'd like to test the update of edges inserted into the database, so
|
|
|
|
// we create two vertexes to connect.
|
|
|
|
node1, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
if err := graph.AddLightningNode(node1); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
node2, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
edgeInfo, edge1, edge2 := createChannelEdge(db, node1, node2)
|
|
|
|
if err := graph.AddLightningNode(node2); err != nil {
|
|
|
|
t.Fatalf("unable to add node: %v", err)
|
|
|
|
}
|
|
|
|
if err := graph.AddChannelEdge(edgeInfo); err != nil {
|
|
|
|
t.Fatalf("unable to create channel edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
chanID := edgeInfo.ChannelID
|
|
|
|
from := edge2.Node.PubKeyBytes[:]
|
|
|
|
to := edge1.Node.PubKeyBytes[:]
|
|
|
|
|
|
|
|
// We'll remove the no max_htlc field from the first edge policy, and
|
|
|
|
// all other opaque data, and serialize it.
|
|
|
|
edge1.MessageFlags = 0
|
|
|
|
edge1.ExtraOpaqueData = nil
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
err = serializeChanEdgePolicy(&b, edge1, to)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to serialize policy")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the max_htlc field. The extra bytes added to the serialization
|
|
|
|
// will be the opaque data containing the serialized field.
|
|
|
|
edge1.MessageFlags = lnwire.ChanUpdateOptionMaxHtlc
|
|
|
|
edge1.MaxHTLC = 13928598
|
|
|
|
var b2 bytes.Buffer
|
|
|
|
err = serializeChanEdgePolicy(&b2, edge1, to)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to serialize policy")
|
|
|
|
}
|
|
|
|
|
|
|
|
withMaxHtlc := b2.Bytes()
|
|
|
|
|
|
|
|
// Remove the opaque data from the serialization.
|
|
|
|
stripped := withMaxHtlc[:len(b.Bytes())]
|
|
|
|
|
|
|
|
// Attempting to deserialize these bytes should return an error.
|
|
|
|
r := bytes.NewReader(stripped)
|
|
|
|
err = db.View(func(tx *bbolt.Tx) error {
|
|
|
|
nodes := tx.Bucket(nodeBucket)
|
|
|
|
if nodes == nil {
|
|
|
|
return ErrGraphNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = deserializeChanEdgePolicy(r, nodes)
|
|
|
|
if err != ErrEdgePolicyOptionalFieldNotFound {
|
|
|
|
t.Fatalf("expected "+
|
|
|
|
"ErrEdgePolicyOptionalFieldNotFound, got %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error reading db: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put the stripped bytes in the DB.
|
|
|
|
err = db.Update(func(tx *bbolt.Tx) error {
|
|
|
|
edges := tx.Bucket(edgeBucket)
|
|
|
|
if edges == nil {
|
|
|
|
return ErrEdgeNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
edgeIndex := edges.Bucket(edgeIndexBucket)
|
|
|
|
if edgeIndex == nil {
|
|
|
|
return ErrEdgeNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
var edgeKey [33 + 8]byte
|
|
|
|
copy(edgeKey[:], from)
|
|
|
|
byteOrder.PutUint64(edgeKey[33:], edge1.ChannelID)
|
|
|
|
|
|
|
|
var scratch [8]byte
|
|
|
|
var indexKey [8 + 8]byte
|
|
|
|
copy(indexKey[:], scratch[:])
|
|
|
|
byteOrder.PutUint64(indexKey[8:], edge1.ChannelID)
|
|
|
|
|
|
|
|
updateIndex, err := edges.CreateBucketIfNotExists(edgeUpdateIndexBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := updateIndex.Put(indexKey[:], nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return edges.Put(edgeKey[:], stripped)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error writing db: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// And add the second, unmodified edge.
|
|
|
|
if err := graph.UpdateEdgePolicy(edge2); err != nil {
|
|
|
|
t.Fatalf("unable to update edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to fetch the edge and policies from the DB. Since the policy
|
|
|
|
// we added is invalid according to the new format, it should be as we
|
|
|
|
// are not aware of the policy (indicated by the policy returned being
|
|
|
|
// nil)
|
|
|
|
dbEdgeInfo, dbEdge1, dbEdge2, err := graph.FetchChannelEdgesByID(chanID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch channel by ID: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The first edge should have a nil-policy returned
|
|
|
|
if dbEdge1 != nil {
|
|
|
|
t.Fatalf("expected db edge to be nil")
|
|
|
|
}
|
|
|
|
if err := compareEdgePolicies(dbEdge2, edge2); err != nil {
|
|
|
|
t.Fatalf("edge doesn't match: %v", err)
|
|
|
|
}
|
|
|
|
assertEdgeInfoEqual(t, dbEdgeInfo, edgeInfo)
|
|
|
|
|
|
|
|
// Now add the original, unmodified edge policy, and make sure the edge
|
|
|
|
// policies then become fully populated.
|
|
|
|
if err := graph.UpdateEdgePolicy(edge1); err != nil {
|
|
|
|
t.Fatalf("unable to update edge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dbEdgeInfo, dbEdge1, dbEdge2, err = graph.FetchChannelEdgesByID(chanID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch channel by ID: %v", err)
|
|
|
|
}
|
|
|
|
if err := compareEdgePolicies(dbEdge1, edge1); err != nil {
|
|
|
|
t.Fatalf("edge doesn't match: %v", err)
|
|
|
|
}
|
|
|
|
if err := compareEdgePolicies(dbEdge2, edge2); err != nil {
|
|
|
|
t.Fatalf("edge doesn't match: %v", err)
|
|
|
|
}
|
|
|
|
assertEdgeInfoEqual(t, dbEdgeInfo, edgeInfo)
|
|
|
|
}
|
|
|
|
|
2019-07-16 02:59:14 +03:00
|
|
|
// assertNumZombies queries the provided ChannelGraph for NumZombies, and
|
|
|
|
// asserts that the returned number is equal to expZombies.
|
|
|
|
func assertNumZombies(t *testing.T, graph *ChannelGraph, expZombies uint64) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
numZombies, err := graph.NumZombies()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query number of zombies: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if numZombies != expZombies {
|
|
|
|
t.Fatalf("expected %d zombies, found %d",
|
|
|
|
expZombies, numZombies)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-27 23:06:12 +03:00
|
|
|
// TestGraphZombieIndex ensures that we can mark edges correctly as zombie/live.
|
|
|
|
func TestGraphZombieIndex(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// We'll start by creating our test graph along with a test edge.
|
|
|
|
db, cleanUp, err := makeTestDB()
|
|
|
|
defer cleanUp()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test database: %v", err)
|
|
|
|
}
|
|
|
|
graph := db.ChannelGraph()
|
|
|
|
|
|
|
|
node1, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test vertex: %v", err)
|
|
|
|
}
|
|
|
|
node2, err := createTestVertex(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create test vertex: %v", err)
|
|
|
|
}
|
2019-04-10 03:47:12 +03:00
|
|
|
|
|
|
|
// Swap the nodes if the second's pubkey is smaller than the first.
|
|
|
|
// Without this, the comparisons at the end will fail probabilistically.
|
|
|
|
if bytes.Compare(node2.PubKeyBytes[:], node1.PubKeyBytes[:]) < 0 {
|
|
|
|
node1, node2 = node2, node1
|
|
|
|
}
|
|
|
|
|
2019-03-27 23:06:12 +03:00
|
|
|
edge, _, _ := createChannelEdge(db, node1, node2)
|
2019-04-10 03:47:12 +03:00
|
|
|
if err := graph.AddChannelEdge(edge); err != nil {
|
|
|
|
t.Fatalf("unable to create channel edge: %v", err)
|
|
|
|
}
|
2019-03-27 23:06:12 +03:00
|
|
|
|
2019-04-10 03:47:12 +03:00
|
|
|
// Since the edge is known the graph and it isn't a zombie, IsZombieEdge
|
|
|
|
// should not report the channel as a zombie.
|
2019-03-27 23:06:12 +03:00
|
|
|
isZombie, _, _ := graph.IsZombieEdge(edge.ChannelID)
|
|
|
|
if isZombie {
|
|
|
|
t.Fatal("expected edge to not be marked as zombie")
|
|
|
|
}
|
2019-07-16 02:59:14 +03:00
|
|
|
assertNumZombies(t, graph, 0)
|
2019-03-27 23:06:12 +03:00
|
|
|
|
2019-04-10 03:47:12 +03:00
|
|
|
// If we delete the edge and mark it as a zombie, then we should expect
|
|
|
|
// to see it within the index.
|
2019-04-17 23:26:34 +03:00
|
|
|
err = graph.DeleteChannelEdges(edge.ChannelID)
|
2019-03-27 23:06:12 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to mark edge as zombie: %v", err)
|
|
|
|
}
|
|
|
|
isZombie, pubKey1, pubKey2 := graph.IsZombieEdge(edge.ChannelID)
|
|
|
|
if !isZombie {
|
|
|
|
t.Fatal("expected edge to be marked as zombie")
|
|
|
|
}
|
|
|
|
if pubKey1 != node1.PubKeyBytes {
|
|
|
|
t.Fatalf("expected pubKey1 %x, got %x", node1.PubKeyBytes,
|
|
|
|
pubKey1)
|
|
|
|
}
|
|
|
|
if pubKey2 != node2.PubKeyBytes {
|
|
|
|
t.Fatalf("expected pubKey2 %x, got %x", node2.PubKeyBytes,
|
|
|
|
pubKey2)
|
|
|
|
}
|
2019-07-16 02:59:14 +03:00
|
|
|
assertNumZombies(t, graph, 1)
|
2019-03-27 23:06:12 +03:00
|
|
|
|
|
|
|
// Similarly, if we mark the same edge as live, we should no longer see
|
|
|
|
// it within the index.
|
|
|
|
if err := graph.MarkEdgeLive(edge.ChannelID); err != nil {
|
|
|
|
t.Fatalf("unable to mark edge as live: %v", err)
|
|
|
|
}
|
|
|
|
isZombie, _, _ = graph.IsZombieEdge(edge.ChannelID)
|
|
|
|
if isZombie {
|
|
|
|
t.Fatal("expected edge to not be marked as zombie")
|
|
|
|
}
|
2019-07-16 02:59:14 +03:00
|
|
|
assertNumZombies(t, graph, 0)
|
2019-03-27 23:06:12 +03:00
|
|
|
}
|
|
|
|
|
2017-03-20 12:24:55 +03:00
|
|
|
// compareNodes is used to compare two LightningNodes while excluding the
|
|
|
|
// Features struct, which cannot be compared as the semantics for reserializing
|
|
|
|
// the featuresMap have not been defined.
|
|
|
|
func compareNodes(a, b *LightningNode) error {
|
2018-04-17 05:04:29 +03:00
|
|
|
if a.LastUpdate != b.LastUpdate {
|
|
|
|
return fmt.Errorf("node LastUpdate doesn't match: expected %v, \n"+
|
|
|
|
"got %v", a.LastUpdate, b.LastUpdate)
|
2017-03-20 12:24:55 +03:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(a.Addresses, b.Addresses) {
|
|
|
|
return fmt.Errorf("Addresses doesn't match: expected %#v, \n "+
|
|
|
|
"got %#v", a.Addresses, b.Addresses)
|
|
|
|
}
|
2018-01-31 07:19:40 +03:00
|
|
|
if !reflect.DeepEqual(a.PubKeyBytes, b.PubKeyBytes) {
|
2017-03-20 12:24:55 +03:00
|
|
|
return fmt.Errorf("PubKey doesn't match: expected %#v, \n "+
|
2018-01-31 07:19:40 +03:00
|
|
|
"got %#v", a.PubKeyBytes, b.PubKeyBytes)
|
2017-03-20 12:24:55 +03:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(a.Color, b.Color) {
|
|
|
|
return fmt.Errorf("Color doesn't match: expected %#v, \n "+
|
|
|
|
"got %#v", a.Color, b.Color)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(a.Alias, b.Alias) {
|
|
|
|
return fmt.Errorf("Alias doesn't match: expected %#v, \n "+
|
|
|
|
"got %#v", a.Alias, b.Alias)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(a.db, b.db) {
|
|
|
|
return fmt.Errorf("db doesn't match: expected %#v, \n "+
|
|
|
|
"got %#v", a.db, b.db)
|
|
|
|
}
|
2017-07-14 22:25:02 +03:00
|
|
|
if !reflect.DeepEqual(a.HaveNodeAnnouncement, b.HaveNodeAnnouncement) {
|
|
|
|
return fmt.Errorf("HaveNodeAnnouncement doesn't match: expected %#v, \n "+
|
|
|
|
"got %#v", a.HaveNodeAnnouncement, b.HaveNodeAnnouncement)
|
|
|
|
}
|
2018-09-01 06:12:45 +03:00
|
|
|
if !bytes.Equal(a.ExtraOpaqueData, b.ExtraOpaqueData) {
|
|
|
|
return fmt.Errorf("extra data doesn't match: %v vs %v",
|
|
|
|
a.ExtraOpaqueData, b.ExtraOpaqueData)
|
|
|
|
}
|
2017-03-20 12:24:55 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// compareEdgePolicies is used to compare two ChannelEdgePolices using
|
|
|
|
// compareNodes, so as to exclude comparisons of the Nodes' Features struct.
|
|
|
|
func compareEdgePolicies(a, b *ChannelEdgePolicy) error {
|
|
|
|
if a.ChannelID != b.ChannelID {
|
|
|
|
return fmt.Errorf("ChannelID doesn't match: expected %v, "+
|
|
|
|
"got %v", a.ChannelID, b.ChannelID)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(a.LastUpdate, b.LastUpdate) {
|
2018-04-17 05:04:29 +03:00
|
|
|
return fmt.Errorf("edge LastUpdate doesn't match: expected %#v, \n "+
|
2017-03-20 12:24:55 +03:00
|
|
|
"got %#v", a.LastUpdate, b.LastUpdate)
|
|
|
|
}
|
2019-01-12 20:59:43 +03:00
|
|
|
if a.MessageFlags != b.MessageFlags {
|
|
|
|
return fmt.Errorf("MessageFlags doesn't match: expected %v, "+
|
|
|
|
"got %v", a.MessageFlags, b.MessageFlags)
|
|
|
|
}
|
|
|
|
if a.ChannelFlags != b.ChannelFlags {
|
|
|
|
return fmt.Errorf("ChannelFlags doesn't match: expected %v, "+
|
|
|
|
"got %v", a.ChannelFlags, b.ChannelFlags)
|
2017-03-20 12:24:55 +03:00
|
|
|
}
|
|
|
|
if a.TimeLockDelta != b.TimeLockDelta {
|
|
|
|
return fmt.Errorf("TimeLockDelta doesn't match: expected %v, "+
|
|
|
|
"got %v", a.TimeLockDelta, b.TimeLockDelta)
|
|
|
|
}
|
|
|
|
if a.MinHTLC != b.MinHTLC {
|
|
|
|
return fmt.Errorf("MinHTLC doesn't match: expected %v, "+
|
|
|
|
"got %v", a.MinHTLC, b.MinHTLC)
|
|
|
|
}
|
2019-01-12 20:59:44 +03:00
|
|
|
if a.MaxHTLC != b.MaxHTLC {
|
|
|
|
return fmt.Errorf("MaxHTLC doesn't match: expected %v, "+
|
|
|
|
"got %v", a.MaxHTLC, b.MaxHTLC)
|
|
|
|
}
|
2017-03-20 12:24:55 +03:00
|
|
|
if a.FeeBaseMSat != b.FeeBaseMSat {
|
|
|
|
return fmt.Errorf("FeeBaseMSat doesn't match: expected %v, "+
|
|
|
|
"got %v", a.FeeBaseMSat, b.FeeBaseMSat)
|
|
|
|
}
|
|
|
|
if a.FeeProportionalMillionths != b.FeeProportionalMillionths {
|
|
|
|
return fmt.Errorf("FeeProportionalMillionths doesn't match: "+
|
|
|
|
"expected %v, got %v", a.FeeProportionalMillionths,
|
|
|
|
b.FeeProportionalMillionths)
|
|
|
|
}
|
2018-09-01 06:12:45 +03:00
|
|
|
if !bytes.Equal(a.ExtraOpaqueData, b.ExtraOpaqueData) {
|
|
|
|
return fmt.Errorf("extra data doesn't match: %v vs %v",
|
|
|
|
a.ExtraOpaqueData, b.ExtraOpaqueData)
|
|
|
|
}
|
2017-03-20 12:24:55 +03:00
|
|
|
if err := compareNodes(a.Node, b.Node); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(a.db, b.db) {
|
|
|
|
return fmt.Errorf("db doesn't match: expected %#v, \n "+
|
|
|
|
"got %#v", a.db, b.db)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-05-09 11:48:58 +03:00
|
|
|
|
|
|
|
// TestLightningNodeSigVerifcation checks that we can use the LightningNode's
|
|
|
|
// pubkey to verify signatures.
|
|
|
|
func TestLightningNodeSigVerification(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Create some dummy data to sign.
|
|
|
|
var data [32]byte
|
|
|
|
if _, err := prand.Read(data[:]); err != nil {
|
|
|
|
t.Fatalf("unable to read prand: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create private key and sign the data with it.
|
|
|
|
priv, err := btcec.NewPrivateKey(btcec.S256())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to crete priv key: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sign, err := priv.Sign(data[:])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to sign: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity check that the signature checks out.
|
|
|
|
if !sign.Verify(data[:], priv.PubKey()) {
|
|
|
|
t.Fatalf("signature doesn't check out")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a LightningNode from the same private key.
|
|
|
|
db, cleanUp, err := makeTestDB()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
node, err := createLightningNode(db, priv)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// And finally check that we can verify the same signature from the
|
|
|
|
// pubkey returned from the lightning node.
|
|
|
|
nodePub, err := node.PubKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get pubkey: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !sign.Verify(data[:], nodePub) {
|
|
|
|
t.Fatalf("unable to verify sig")
|
|
|
|
}
|
|
|
|
}
|
2019-08-29 18:20:37 +03:00
|
|
|
|
|
|
|
// TestComputeFee tests fee calculation based on both in- and outgoing amt.
|
|
|
|
func TestComputeFee(t *testing.T) {
|
|
|
|
var (
|
|
|
|
policy = ChannelEdgePolicy{
|
|
|
|
FeeBaseMSat: 10000,
|
|
|
|
FeeProportionalMillionths: 30000,
|
|
|
|
}
|
|
|
|
outgoingAmt = lnwire.MilliSatoshi(1000000)
|
|
|
|
expectedFee = lnwire.MilliSatoshi(40000)
|
|
|
|
)
|
|
|
|
|
|
|
|
fee := policy.ComputeFee(outgoingAmt)
|
|
|
|
if fee != expectedFee {
|
|
|
|
t.Fatalf("expected fee %v, got %v", expectedFee, fee)
|
|
|
|
}
|
|
|
|
|
|
|
|
fwdFee := policy.ComputeFeeFromIncoming(outgoingAmt + fee)
|
|
|
|
if fwdFee != expectedFee {
|
|
|
|
t.Fatalf("expected fee %v, but got %v", fee, fwdFee)
|
|
|
|
}
|
|
|
|
}
|