channeldb: reallocate node in ForEachNodeCacheable

Allows cacheableNode to be used outside of the callback. This is a
preparation for optimization of the graph cache population.
This commit is contained in:
Joost Jager committed 2022-01-14 14:57:27 +01:00
1 parent cb4703638f
commit 2e2229a2e7
1 file changed
+25 -19
+25 -19
View File
@@ -690,7 +690,6 @@ func (c *ChannelGraph) ForEachNodeCacheable(cb func(kvdb.RTx,
return ErrGraphNotFound return ErrGraphNotFound
} }
cacheableNode := newGraphCacheNode(route.Vertex{}, nil)
return nodes.ForEach(func(pubKey, nodeBytes []byte) error { return nodes.ForEach(func(pubKey, nodeBytes []byte) error {
// If this is the source key, then we skip this // If this is the source key, then we skip this
// iteration as the value for this key is a pubKey // iteration as the value for this key is a pubKey
@@ -700,8 +699,8 @@ func (c *ChannelGraph) ForEachNodeCacheable(cb func(kvdb.RTx,
} }
nodeReader := bytes.NewReader(nodeBytes) nodeReader := bytes.NewReader(nodeBytes)
err := deserializeLightningNodeCacheable( cacheableNode, err := deserializeLightningNodeCacheable(
nodeReader, cacheableNode, nodeReader,
) )
if err != nil { if err != nil {
return err return err
@@ -2802,8 +2801,6 @@ func (c *ChannelGraph) FetchLightningNode(nodePub route.Vertex) (
type graphCacheNode struct { type graphCacheNode struct {
pubKeyBytes route.Vertex pubKeyBytes route.Vertex
features *lnwire.FeatureVector features *lnwire.FeatureVector
nodeScratch [8]byte
} }
// newGraphCacheNode returns a new cache optimized node. // newGraphCacheNode returns a new cache optimized node.
@@ -4152,51 +4149,60 @@ func fetchLightningNode(nodeBucket kvdb.RBucket,
return deserializeLightningNode(nodeReader) return deserializeLightningNode(nodeReader)
} }
func deserializeLightningNodeCacheable(r io.Reader, node *graphCacheNode) error { func deserializeLightningNodeCacheable(r io.Reader) (*graphCacheNode, error) {
// Always populate a feature vector, even if we don't have a node // Always populate a feature vector, even if we don't have a node
// announcement and short circuit below. // announcement and short circuit below.
node.features = lnwire.EmptyFeatureVector() node := newGraphCacheNode(
route.Vertex{},
lnwire.EmptyFeatureVector(),
)
var nodeScratch [8]byte
// Skip ahead: // Skip ahead:
// - LastUpdate (8 bytes) // - LastUpdate (8 bytes)
if _, err := r.Read(node.nodeScratch[:]); err != nil { if _, err := r.Read(nodeScratch[:]); err != nil {
return err return nil, err
} }
if _, err := io.ReadFull(r, node.pubKeyBytes[:]); err != nil { if _, err := io.ReadFull(r, node.pubKeyBytes[:]); err != nil {
return err return nil, err
} }
// Read the node announcement flag. // Read the node announcement flag.
if _, err := r.Read(node.nodeScratch[:2]); err != nil { if _, err := r.Read(nodeScratch[:2]); err != nil {
return err return nil, err
} }
hasNodeAnn := byteOrder.Uint16(node.nodeScratch[:2]) hasNodeAnn := byteOrder.Uint16(nodeScratch[:2])
// The rest of the data is optional, and will only be there if we got a // The rest of the data is optional, and will only be there if we got a
// node announcement for this node. // node announcement for this node.
if hasNodeAnn == 0 { if hasNodeAnn == 0 {
return nil return node, nil
} }
// We did get a node announcement for this node, so we'll have the rest // We did get a node announcement for this node, so we'll have the rest
// of the data available. // of the data available.
var rgb uint8 var rgb uint8
if err := binary.Read(r, byteOrder, &rgb); err != nil { if err := binary.Read(r, byteOrder, &rgb); err != nil {
return err return nil, err
} }
if err := binary.Read(r, byteOrder, &rgb); err != nil { if err := binary.Read(r, byteOrder, &rgb); err != nil {
return err return nil, err
} }
if err := binary.Read(r, byteOrder, &rgb); err != nil { if err := binary.Read(r, byteOrder, &rgb); err != nil {
return err return nil, err
} }
if _, err := wire.ReadVarString(r, 0); err != nil { if _, err := wire.ReadVarString(r, 0); err != nil {
return err return nil, err
} }
return node.features.Decode(r) if err := node.features.Decode(r); err != nil {
return nil, err
}
return node, nil
} }
func deserializeLightningNode(r io.Reader) (LightningNode, error) { func deserializeLightningNode(r io.Reader) (LightningNode, error) {