refactor: move graph related DB code to graph/db from channeldb

This is a pure refactor commit. It moves over all the graph related CRUD
code from `channeldb` to `graph/db`.
This commit is contained in:
Elle Mouton committed 2024-11-28 13:36:13 +02:00
1 parent 9f54ec90aa
commit 74a4b1922b
57 files changed
+512 -299

No files matched your search

+7 -7
View File
@@ -4,7 +4,7 @@ import (
"time"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/lightningnetwork/lnd/channeldb"
graphdb "github.com/lightningnetwork/lnd/graph/db"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/netann"
"github.com/lightningnetwork/lnd/routing/route"
@@ -36,7 +36,7 @@ type ChannelGraphTimeSeries interface {
// ID's represents the ID's that we don't know of which were in the
// passed superSet.
FilterKnownChanIDs(chain chainhash.Hash,
superSet []channeldb.ChannelUpdateInfo,
superSet []graphdb.ChannelUpdateInfo,
isZombieChan func(time.Time, time.Time) bool) (
[]lnwire.ShortChannelID, error)
@@ -45,7 +45,7 @@ type ChannelGraphTimeSeries interface {
// grouped by their common block height. We'll use this to to a remote
// peer's QueryChannelRange message.
FilterChannelRange(chain chainhash.Hash, startHeight, endHeight uint32,
withTimestamps bool) ([]channeldb.BlockChannelRange, error)
withTimestamps bool) ([]graphdb.BlockChannelRange, error)
// FetchChanAnns returns a full set of channel announcements as well as
// their updates that match the set of specified short channel ID's.
@@ -70,12 +70,12 @@ type ChannelGraphTimeSeries interface {
// in-protocol channel range queries to quickly and efficiently synchronize our
// channel state with all peers.
type ChanSeries struct {
graph *channeldb.ChannelGraph
graph *graphdb.ChannelGraph
}
// NewChanSeries constructs a new ChanSeries backed by a channeldb.ChannelGraph.
// The returned ChanSeries implements the ChannelGraphTimeSeries interface.
func NewChanSeries(graph *channeldb.ChannelGraph) *ChanSeries {
func NewChanSeries(graph *graphdb.ChannelGraph) *ChanSeries {
return &ChanSeries{
graph: graph,
}
@@ -200,7 +200,7 @@ func (c *ChanSeries) UpdatesInHorizon(chain chainhash.Hash,
//
// NOTE: This is part of the ChannelGraphTimeSeries interface.
func (c *ChanSeries) FilterKnownChanIDs(_ chainhash.Hash,
superSet []channeldb.ChannelUpdateInfo,
superSet []graphdb.ChannelUpdateInfo,
isZombieChan func(time.Time, time.Time) bool) (
[]lnwire.ShortChannelID, error) {
@@ -226,7 +226,7 @@ func (c *ChanSeries) FilterKnownChanIDs(_ chainhash.Hash,
//
// NOTE: This is part of the ChannelGraphTimeSeries interface.
func (c *ChanSeries) FilterChannelRange(_ chainhash.Hash, startHeight,
endHeight uint32, withTimestamps bool) ([]channeldb.BlockChannelRange,
endHeight uint32, withTimestamps bool) ([]graphdb.BlockChannelRange,
error) {
return c.graph.FilterChannelRange(
+10 -9
View File
@@ -21,6 +21,7 @@ import (
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/fn"
"github.com/lightningnetwork/lnd/graph"
graphdb "github.com/lightningnetwork/lnd/graph/db"
"github.com/lightningnetwork/lnd/graph/db/models"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/kvdb"
@@ -1686,7 +1687,7 @@ func (d *AuthenticatedGossiper) retransmitStaleAnns(now time.Time) error {
return nil
})
if err != nil && err != channeldb.ErrGraphNoEdgesFound {
if err != nil && err != graphdb.ErrGraphNoEdgesFound {
return fmt.Errorf("unable to retrieve outgoing channels: %w",
err)
}
@@ -1963,7 +1964,7 @@ func (d *AuthenticatedGossiper) addNode(msg *lnwire.NodeAnnouncement,
timestamp := time.Unix(int64(msg.Timestamp), 0)
features := lnwire.NewFeatureVector(msg.Features, lnwire.Features)
node := &channeldb.LightningNode{
node := &graphdb.LightningNode{
HaveNodeAnnouncement: true,
LastUpdate: timestamp,
Addresses: msg.Addresses,
@@ -2121,7 +2122,7 @@ func (d *AuthenticatedGossiper) processZombieUpdate(
// come through again.
err = d.cfg.Graph.MarkEdgeLive(scid)
switch {
case errors.Is(err, channeldb.ErrZombieEdgeNotFound):
case errors.Is(err, graphdb.ErrZombieEdgeNotFound):
log.Errorf("edge with chan_id=%v was not found in the "+
"zombie index: %v", err)
@@ -2166,7 +2167,7 @@ func (d *AuthenticatedGossiper) isMsgStale(msg lnwire.Message) bool {
// If the channel cannot be found, it is most likely a leftover
// message for a channel that was closed, so we can consider it
// stale.
if errors.Is(err, channeldb.ErrEdgeNotFound) {
if errors.Is(err, graphdb.ErrEdgeNotFound) {
return true
}
if err != nil {
@@ -2186,7 +2187,7 @@ func (d *AuthenticatedGossiper) isMsgStale(msg lnwire.Message) bool {
// If the channel cannot be found, it is most likely a leftover
// message for a channel that was closed, so we can consider it
// stale.
if errors.Is(err, channeldb.ErrEdgeNotFound) {
if errors.Is(err, graphdb.ErrEdgeNotFound) {
return true
}
if err != nil {
@@ -2936,7 +2937,7 @@ func (d *AuthenticatedGossiper) handleChanUpdate(nMsg *networkMsg,
case err == nil:
break
case errors.Is(err, channeldb.ErrZombieEdge):
case errors.Is(err, graphdb.ErrZombieEdge):
err = d.processZombieUpdate(chanInfo, graphScid, upd)
if err != nil {
log.Debug(err)
@@ -2949,11 +2950,11 @@ func (d *AuthenticatedGossiper) handleChanUpdate(nMsg *networkMsg,
// needed to ensure the edge exists in the graph before
// applying the update.
fallthrough
case errors.Is(err, channeldb.ErrGraphNotFound):
case errors.Is(err, graphdb.ErrGraphNotFound):
fallthrough
case errors.Is(err, channeldb.ErrGraphNoEdgesFound):
case errors.Is(err, graphdb.ErrGraphNoEdgesFound):
fallthrough
case errors.Is(err, channeldb.ErrEdgeNotFound):
case errors.Is(err, graphdb.ErrEdgeNotFound):
// If the edge corresponding to this ChannelUpdate was not
// found in the graph, this might be a channel in the process
// of being opened, and we haven't processed our own
+11 -10
View File
@@ -26,6 +26,7 @@ import (
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/fn"
"github.com/lightningnetwork/lnd/graph"
graphdb "github.com/lightningnetwork/lnd/graph/db"
"github.com/lightningnetwork/lnd/graph/db/models"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/kvdb"
@@ -92,7 +93,7 @@ type mockGraphSource struct {
bestHeight uint32
mu sync.Mutex
nodes []channeldb.LightningNode
nodes []graphdb.LightningNode
infos map[uint64]models.ChannelEdgeInfo
edges map[uint64][]models.ChannelEdgePolicy
zombies map[uint64][][33]byte
@@ -112,7 +113,7 @@ func newMockRouter(height uint32) *mockGraphSource {
var _ graph.ChannelGraphSource = (*mockGraphSource)(nil)
func (r *mockGraphSource) AddNode(node *channeldb.LightningNode,
func (r *mockGraphSource) AddNode(node *graphdb.LightningNode,
_ ...batch.SchedulerOption) error {
r.mu.Lock()
@@ -202,7 +203,7 @@ func (r *mockGraphSource) AddProof(chanID lnwire.ShortChannelID,
return nil
}
func (r *mockGraphSource) ForEachNode(func(node *channeldb.LightningNode) error) error {
func (r *mockGraphSource) ForEachNode(func(node *graphdb.LightningNode) error) error {
return nil
}
@@ -213,7 +214,7 @@ func (r *mockGraphSource) ForAllOutgoingChannels(cb func(tx kvdb.RTx,
r.mu.Lock()
defer r.mu.Unlock()
chans := make(map[uint64]channeldb.ChannelEdge)
chans := make(map[uint64]graphdb.ChannelEdge)
for _, info := range r.infos {
info := info
@@ -251,13 +252,13 @@ func (r *mockGraphSource) GetChannelByID(chanID lnwire.ShortChannelID) (
if !ok {
pubKeys, isZombie := r.zombies[chanIDInt]
if !isZombie {
return nil, nil, nil, channeldb.ErrEdgeNotFound
return nil, nil, nil, graphdb.ErrEdgeNotFound
}
return &models.ChannelEdgeInfo{
NodeKey1Bytes: pubKeys[0],
NodeKey2Bytes: pubKeys[1],
}, nil, nil, channeldb.ErrZombieEdge
}, nil, nil, graphdb.ErrZombieEdge
}
edges := r.edges[chanID.ToUint64()]
@@ -279,7 +280,7 @@ func (r *mockGraphSource) GetChannelByID(chanID lnwire.ShortChannelID) (
}
func (r *mockGraphSource) FetchLightningNode(
nodePub route.Vertex) (*channeldb.LightningNode, error) {
nodePub route.Vertex) (*graphdb.LightningNode, error) {
for _, node := range r.nodes {
if bytes.Equal(nodePub[:], node.PubKeyBytes[:]) {
@@ -287,7 +288,7 @@ func (r *mockGraphSource) FetchLightningNode(
}
}
return nil, channeldb.ErrGraphNodeNotFound
return nil, graphdb.ErrGraphNodeNotFound
}
// IsStaleNode returns true if the graph source has a node announcement for the
@@ -2319,7 +2320,7 @@ func TestProcessZombieEdgeNowLive(t *testing.T) {
// At this point, the channel should still be considered a zombie.
_, _, _, err = ctx.router.GetChannelByID(chanID)
if err != channeldb.ErrZombieEdge {
if err != graphdb.ErrZombieEdge {
t.Fatalf("channel should still be a zombie")
}
@@ -2442,7 +2443,7 @@ func TestReceiveRemoteChannelUpdateFirst(t *testing.T) {
// to the map of premature ChannelUpdates. Check that nothing
// was added to the graph.
chanInfo, e1, e2, err := ctx.router.GetChannelByID(batch.chanUpdAnn1.ShortChannelID)
if err != channeldb.ErrEdgeNotFound {
if err != graphdb.ErrEdgeNotFound {
t.Fatalf("Expected ErrEdgeNotFound, got: %v", err)
}
if chanInfo != nil {
+5 -5
View File
@@ -11,8 +11,8 @@ import (
"time"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/graph"
graphdb "github.com/lightningnetwork/lnd/graph/db"
"github.com/lightningnetwork/lnd/lnpeer"
"github.com/lightningnetwork/lnd/lnwire"
"golang.org/x/time/rate"
@@ -373,7 +373,7 @@ type GossipSyncer struct {
// bufferedChanRangeReplies is used in the waitingQueryChanReply to
// buffer all the chunked response to our query.
bufferedChanRangeReplies []channeldb.ChannelUpdateInfo
bufferedChanRangeReplies []graphdb.ChannelUpdateInfo
// numChanRangeRepliesRcvd is used to track the number of replies
// received as part of a QueryChannelRange. This field is primarily used
@@ -837,7 +837,7 @@ func (g *GossipSyncer) processChanRangeReply(msg *lnwire.ReplyChannelRange) erro
g.prevReplyChannelRange = msg
for i, scid := range msg.ShortChanIDs {
info := channeldb.NewChannelUpdateInfo(
info := graphdb.NewChannelUpdateInfo(
scid, time.Time{}, time.Time{},
)
@@ -1115,7 +1115,7 @@ func (g *GossipSyncer) replyChanRangeQuery(query *lnwire.QueryChannelRange) erro
// this as there's a transport message size limit which we'll need to
// adhere to. We also need to make sure all of our replies cover the
// expected range of the query.
sendReplyForChunk := func(channelChunk []channeldb.ChannelUpdateInfo,
sendReplyForChunk := func(channelChunk []graphdb.ChannelUpdateInfo,
firstHeight, lastHeight uint32, finalChunk bool) error {
// The number of blocks contained in the current chunk (the
@@ -1164,7 +1164,7 @@ func (g *GossipSyncer) replyChanRangeQuery(query *lnwire.QueryChannelRange) erro
var (
firstHeight = query.FirstBlockHeight
lastHeight uint32
channelChunk []channeldb.ChannelUpdateInfo
channelChunk []graphdb.ChannelUpdateInfo
)
// chunkSize is the maximum number of SCIDs that we can safely put in a
+9 -9
View File
@@ -13,7 +13,7 @@ import (
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/channeldb"
graphdb "github.com/lightningnetwork/lnd/graph/db"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/stretchr/testify/require"
)
@@ -42,7 +42,7 @@ type mockChannelGraphTimeSeries struct {
horizonReq chan horizonQuery
horizonResp chan []lnwire.Message
filterReq chan []channeldb.ChannelUpdateInfo
filterReq chan []graphdb.ChannelUpdateInfo
filterResp chan []lnwire.ShortChannelID
filterRangeReqs chan filterRangeReq
@@ -64,7 +64,7 @@ func newMockChannelGraphTimeSeries(
horizonReq: make(chan horizonQuery, 1),
horizonResp: make(chan []lnwire.Message, 1),
filterReq: make(chan []channeldb.ChannelUpdateInfo, 1),
filterReq: make(chan []graphdb.ChannelUpdateInfo, 1),
filterResp: make(chan []lnwire.ShortChannelID, 1),
filterRangeReqs: make(chan filterRangeReq, 1),
@@ -92,7 +92,7 @@ func (m *mockChannelGraphTimeSeries) UpdatesInHorizon(chain chainhash.Hash,
}
func (m *mockChannelGraphTimeSeries) FilterKnownChanIDs(chain chainhash.Hash,
superSet []channeldb.ChannelUpdateInfo,
superSet []graphdb.ChannelUpdateInfo,
isZombieChan func(time.Time, time.Time) bool) (
[]lnwire.ShortChannelID, error) {
@@ -102,16 +102,16 @@ func (m *mockChannelGraphTimeSeries) FilterKnownChanIDs(chain chainhash.Hash,
}
func (m *mockChannelGraphTimeSeries) FilterChannelRange(chain chainhash.Hash,
startHeight, endHeight uint32, withTimestamps bool) (
[]channeldb.BlockChannelRange, error) {
[]graphdb.BlockChannelRange, error) {
m.filterRangeReqs <- filterRangeReq{startHeight, endHeight}
reply := <-m.filterRangeResp
channelsPerBlock := make(map[uint32][]channeldb.ChannelUpdateInfo)
channelsPerBlock := make(map[uint32][]graphdb.ChannelUpdateInfo)
for _, cid := range reply {
channelsPerBlock[cid.BlockHeight] = append(
channelsPerBlock[cid.BlockHeight],
channeldb.ChannelUpdateInfo{
graphdb.ChannelUpdateInfo{
ShortChannelID: cid,
},
)
@@ -127,11 +127,11 @@ func (m *mockChannelGraphTimeSeries) FilterChannelRange(chain chainhash.Hash,
})
channelRanges := make(
[]channeldb.BlockChannelRange, 0, len(channelsPerBlock),
[]graphdb.BlockChannelRange, 0, len(channelsPerBlock),
)
for _, block := range blocks {
channelRanges = append(
channelRanges, channeldb.BlockChannelRange{
channelRanges, graphdb.BlockChannelRange{
Height: block,
Channels: channelsPerBlock[block],
},