2021-09-21 19:18:20 +02:00
|
|
|
package channeldb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"testing"
|
|
|
|
|
2023-11-08 10:18:45 +01:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb/models"
|
2021-09-21 19:18:20 +02:00
|
|
|
"github.com/lightningnetwork/lnd/kvdb"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
"github.com/lightningnetwork/lnd/routing/route"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
pubKey1Bytes, _ = hex.DecodeString(
|
|
|
|
"0248f5cba4c6da2e4c9e01e81d1404dfac0cbaf3ee934a4fc117d2ea9a64" +
|
|
|
|
"22c91d",
|
|
|
|
)
|
|
|
|
pubKey2Bytes, _ = hex.DecodeString(
|
|
|
|
"038155ba86a8d3b23c806c855097ca5c9fa0f87621f1e7a7d2835ad057f6" +
|
|
|
|
"f4484f",
|
|
|
|
)
|
|
|
|
|
|
|
|
pubKey1, _ = route.NewVertexFromBytes(pubKey1Bytes)
|
|
|
|
pubKey2, _ = route.NewVertexFromBytes(pubKey2Bytes)
|
|
|
|
)
|
|
|
|
|
|
|
|
type node struct {
|
|
|
|
pubKey route.Vertex
|
|
|
|
features *lnwire.FeatureVector
|
|
|
|
|
2023-11-08 10:18:45 +01:00
|
|
|
edgeInfos []*models.ChannelEdgeInfo
|
|
|
|
outPolicies []*models.ChannelEdgePolicy
|
|
|
|
inPolicies []*models.ChannelEdgePolicy
|
2021-09-21 19:18:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *node) PubKey() route.Vertex {
|
|
|
|
return n.pubKey
|
|
|
|
}
|
|
|
|
func (n *node) Features() *lnwire.FeatureVector {
|
|
|
|
return n.features
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *node) ForEachChannel(tx kvdb.RTx,
|
2023-11-08 10:18:45 +01:00
|
|
|
cb func(kvdb.RTx, *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
|
|
|
|
*models.ChannelEdgePolicy) error) error {
|
2021-09-21 19:18:20 +02:00
|
|
|
|
|
|
|
for idx := range n.edgeInfos {
|
|
|
|
err := cb(
|
|
|
|
tx, n.edgeInfos[idx], n.outPolicies[idx],
|
|
|
|
n.inPolicies[idx],
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestGraphCacheAddNode tests that a channel going from node A to node B can be
|
|
|
|
// cached correctly, independent of the direction we add the channel as.
|
|
|
|
func TestGraphCacheAddNode(t *testing.T) {
|
2021-11-03 04:11:55 +01:00
|
|
|
t.Parallel()
|
|
|
|
|
2021-09-21 19:18:20 +02:00
|
|
|
runTest := func(nodeA, nodeB route.Vertex) {
|
|
|
|
t.Helper()
|
|
|
|
|
2021-09-21 19:18:22 +02:00
|
|
|
channelFlagA, channelFlagB := 0, 1
|
|
|
|
if nodeA == pubKey2 {
|
|
|
|
channelFlagA, channelFlagB = 1, 0
|
|
|
|
}
|
|
|
|
|
2023-11-08 10:18:45 +01:00
|
|
|
outPolicy1 := &models.ChannelEdgePolicy{
|
2021-09-21 19:18:20 +02:00
|
|
|
ChannelID: 1000,
|
2021-09-21 19:18:22 +02:00
|
|
|
ChannelFlags: lnwire.ChanUpdateChanFlags(channelFlagA),
|
2023-10-23 14:39:15 +02:00
|
|
|
ToNode: nodeB,
|
2022-07-05 15:01:18 +02:00
|
|
|
// Define an inbound fee.
|
|
|
|
ExtraOpaqueData: []byte{
|
|
|
|
253, 217, 3, 8, 0, 0, 0, 10, 0, 0, 0, 20,
|
|
|
|
},
|
2021-09-21 19:18:20 +02:00
|
|
|
}
|
2023-11-08 10:18:45 +01:00
|
|
|
inPolicy1 := &models.ChannelEdgePolicy{
|
2021-09-21 19:18:20 +02:00
|
|
|
ChannelID: 1000,
|
2021-09-21 19:18:22 +02:00
|
|
|
ChannelFlags: lnwire.ChanUpdateChanFlags(channelFlagB),
|
2023-10-23 14:39:15 +02:00
|
|
|
ToNode: nodeA,
|
2021-09-21 19:18:20 +02:00
|
|
|
}
|
|
|
|
node := &node{
|
|
|
|
pubKey: nodeA,
|
|
|
|
features: lnwire.EmptyFeatureVector(),
|
2023-11-08 10:18:45 +01:00
|
|
|
edgeInfos: []*models.ChannelEdgeInfo{{
|
2021-09-21 19:18:20 +02:00
|
|
|
ChannelID: 1000,
|
|
|
|
// Those are direction independent!
|
|
|
|
NodeKey1Bytes: pubKey1,
|
|
|
|
NodeKey2Bytes: pubKey2,
|
|
|
|
Capacity: 500,
|
|
|
|
}},
|
2023-11-08 10:18:45 +01:00
|
|
|
outPolicies: []*models.ChannelEdgePolicy{outPolicy1},
|
|
|
|
inPolicies: []*models.ChannelEdgePolicy{inPolicy1},
|
2021-09-21 19:18:20 +02:00
|
|
|
}
|
2021-09-21 19:18:25 +02:00
|
|
|
cache := NewGraphCache(10)
|
2021-09-21 19:18:20 +02:00
|
|
|
require.NoError(t, cache.AddNode(nil, node))
|
|
|
|
|
2021-09-21 19:18:22 +02:00
|
|
|
var fromChannels, toChannels []*DirectedChannel
|
|
|
|
_ = cache.ForEachChannel(nodeA, func(c *DirectedChannel) error {
|
|
|
|
fromChannels = append(fromChannels, c)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
_ = cache.ForEachChannel(nodeB, func(c *DirectedChannel) error {
|
|
|
|
toChannels = append(toChannels, c)
|
|
|
|
return nil
|
|
|
|
})
|
2021-09-21 19:18:20 +02:00
|
|
|
|
|
|
|
require.Len(t, fromChannels, 1)
|
|
|
|
require.Len(t, toChannels, 1)
|
|
|
|
|
2021-09-21 19:18:22 +02:00
|
|
|
require.Equal(t, outPolicy1 != nil, fromChannels[0].OutPolicySet)
|
|
|
|
assertCachedPolicyEqual(t, inPolicy1, fromChannels[0].InPolicy)
|
2021-09-21 19:18:20 +02:00
|
|
|
|
2021-09-21 19:18:22 +02:00
|
|
|
require.Equal(t, inPolicy1 != nil, toChannels[0].OutPolicySet)
|
|
|
|
assertCachedPolicyEqual(t, outPolicy1, toChannels[0].InPolicy)
|
2021-10-20 01:04:23 +02:00
|
|
|
|
|
|
|
// Now that we've inserted two nodes into the graph, check that
|
|
|
|
// we'll recover the same set of channels during ForEachNode.
|
|
|
|
nodes := make(map[route.Vertex]struct{})
|
|
|
|
chans := make(map[uint64]struct{})
|
|
|
|
_ = cache.ForEachNode(func(node route.Vertex,
|
|
|
|
edges map[uint64]*DirectedChannel) error {
|
|
|
|
|
|
|
|
nodes[node] = struct{}{}
|
2022-07-05 15:01:18 +02:00
|
|
|
for chanID, directedChannel := range edges {
|
2021-10-20 01:04:23 +02:00
|
|
|
chans[chanID] = struct{}{}
|
2022-07-05 15:01:18 +02:00
|
|
|
|
|
|
|
if node == nodeA {
|
|
|
|
require.NotZero(
|
|
|
|
t, directedChannel.InboundFee,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
require.Zero(
|
|
|
|
t, directedChannel.InboundFee,
|
|
|
|
)
|
|
|
|
}
|
2021-10-20 01:04:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
require.Len(t, nodes, 2)
|
|
|
|
require.Len(t, chans, 1)
|
2021-09-21 19:18:20 +02:00
|
|
|
}
|
2021-09-21 19:18:22 +02:00
|
|
|
|
2021-09-21 19:18:20 +02:00
|
|
|
runTest(pubKey1, pubKey2)
|
|
|
|
runTest(pubKey2, pubKey1)
|
|
|
|
}
|
2021-09-21 19:18:22 +02:00
|
|
|
|
2023-11-08 10:18:45 +01:00
|
|
|
func assertCachedPolicyEqual(t *testing.T, original *models.ChannelEdgePolicy,
|
|
|
|
cached *models.CachedEdgePolicy) {
|
2021-09-21 19:18:22 +02:00
|
|
|
|
|
|
|
require.Equal(t, original.ChannelID, cached.ChannelID)
|
|
|
|
require.Equal(t, original.MessageFlags, cached.MessageFlags)
|
|
|
|
require.Equal(t, original.ChannelFlags, cached.ChannelFlags)
|
|
|
|
require.Equal(t, original.TimeLockDelta, cached.TimeLockDelta)
|
|
|
|
require.Equal(t, original.MinHTLC, cached.MinHTLC)
|
|
|
|
require.Equal(t, original.MaxHTLC, cached.MaxHTLC)
|
|
|
|
require.Equal(t, original.FeeBaseMSat, cached.FeeBaseMSat)
|
|
|
|
require.Equal(
|
|
|
|
t, original.FeeProportionalMillionths,
|
|
|
|
cached.FeeProportionalMillionths,
|
|
|
|
)
|
|
|
|
require.Equal(
|
2023-10-23 14:39:15 +02:00
|
|
|
t, route.Vertex(original.ToNode), cached.ToNodePubKey(),
|
2021-09-21 19:18:22 +02:00
|
|
|
)
|
|
|
|
}
|