Skip to content

Commit 3069a67

Browse files
committed
graph/db: let GraghCache.AddChannel take CachedEdgeInfo
Define a new CachedEdgeInfo type and let the graph cache's AddChannel use this. This will let us later on (for the SQL impl of the graph db) only load from the DB what we actually need for the graph cache.
1 parent 3ea86bf commit 3069a67

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

graph/db/graph.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func (c *ChannelGraph) populateCache() error {
178178
policy1, policy2 *models.ChannelEdgePolicy) error {
179179

180180
c.graphCache.AddChannel(
181-
info,
181+
models.NewCachedEdge(info),
182182
models.NewCachedPolicy(policy1),
183183
models.NewCachedPolicy(policy2),
184184
)
@@ -316,7 +316,7 @@ func (c *ChannelGraph) AddChannelEdge(edge *models.ChannelEdgeInfo,
316316
}
317317

318318
if c.graphCache != nil {
319-
c.graphCache.AddChannel(edge, nil, nil)
319+
c.graphCache.AddChannel(models.NewCachedEdge(edge), nil, nil)
320320
}
321321

322322
select {
@@ -352,7 +352,7 @@ func (c *ChannelGraph) MarkEdgeLive(chanID uint64) error {
352352
info := infos[0]
353353

354354
c.graphCache.AddChannel(
355-
info.Info,
355+
models.NewCachedEdge(info.Info),
356356
models.NewCachedPolicy(info.Policy1),
357357
models.NewCachedPolicy(info.Policy2),
358358
)

graph/db/graph_cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func (c *GraphCache) AddNodeFeatures(node route.Vertex,
114114
// and policy 2 does not matter, the directionality is extracted from the info
115115
// and policy flags automatically. The policy will be set as the outgoing policy
116116
// on one node and the incoming policy on the peer's side.
117-
func (c *GraphCache) AddChannel(info *models.ChannelEdgeInfo,
117+
func (c *GraphCache) AddChannel(info *models.CachedEdgeInfo,
118118
policy1, policy2 *models.CachedEdgePolicy) {
119119

120120
if info == nil {

graph/db/graph_cache_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func TestGraphCacheAddNode(t *testing.T) {
6161
}
6262
cache := NewGraphCache(10)
6363
cache.AddNodeFeatures(nodeA, lnwire.EmptyFeatureVector())
64-
cache.AddChannel(&models.ChannelEdgeInfo{
64+
cache.AddChannel(&models.CachedEdgeInfo{
6565
ChannelID: 1000,
6666
// Those are direction independent!
6767
NodeKey1Bytes: pubKey1,

graph/db/models/cached_edge_info.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package models
2+
3+
import "github.com/btcsuite/btcd/btcutil"
4+
5+
// CachedEdgeInfo is a struct that only caches the information of a
6+
// ChannelEdgeInfo that we actually use for pathfinding and therefore need to
7+
// store in the cache.
8+
type CachedEdgeInfo struct {
9+
// ChannelID is the unique channel ID for the channel. The first 3
10+
// bytes are the block height, the next 3 the index within the block,
11+
// and the last 2 bytes are the output index for the channel.
12+
ChannelID uint64
13+
14+
// NodeKey1Bytes is the raw public key of the first node.
15+
NodeKey1Bytes [33]byte
16+
17+
// NodeKey2Bytes is the raw public key of the second node.
18+
NodeKey2Bytes [33]byte
19+
20+
// Capacity is the total capacity of the channel, this is determined by
21+
// the value output in the outpoint that created this channel.
22+
Capacity btcutil.Amount
23+
}
24+
25+
// NewCachedEdge creates a new CachedEdgeInfo from the provided ChannelEdgeInfo.
26+
func NewCachedEdge(edgeInfo *ChannelEdgeInfo) *CachedEdgeInfo {
27+
return &CachedEdgeInfo{
28+
ChannelID: edgeInfo.ChannelID,
29+
NodeKey1Bytes: edgeInfo.NodeKey1Bytes,
30+
NodeKey2Bytes: edgeInfo.NodeKey2Bytes,
31+
Capacity: edgeInfo.Capacity,
32+
}
33+
}

0 commit comments

Comments
 (0)