Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ellemouton committed Nov 8, 2024
1 parent e27eccd commit 7fc2e04
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 54 deletions.
5 changes: 1 addition & 4 deletions autopilot/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,7 @@ func (d *dbNode) ForEachChannel(cb func(ChannelEdge) error) error {
return nil
}

node, err := d.db.FetchLightningNode(
context.TODO(), graphdb.NewKVDBRTx(tx),
ep.ToNode,
)
node, err := d.db.FetchLightningNodeWithTx(tx, ep.ToNode)
if err != nil {
return err
}
Expand Down
11 changes: 3 additions & 8 deletions cmd/multinode/graph_mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,24 +193,19 @@ func (g *GraphSourceMux) ForEachNode(ctx context.Context,
// a new one will be created.
//
// NOTE: this is part of the GraphSource interface.
func (g *GraphSourceMux) FetchLightningNode(ctx context.Context, tx graphdb.RTx,
func (g *GraphSourceMux) FetchLightningNode(ctx context.Context,
nodePub route.Vertex) (*models.LightningNode, error) {

srcPub, err := g.selfNodePub()
if err != nil {
return nil, err
}

lTx, rTx, err := extractRTxSet(tx)
if err != nil {
return nil, err
}

if bytes.Equal(srcPub[:], nodePub[:]) {
return g.local.FetchLightningNode(ctx, lTx, nodePub)
return g.local.FetchLightningNode(ctx, nodePub)
}

return g.remote.FetchLightningNode(ctx, rTx, nodePub)
return g.remote.FetchLightningNode(ctx, nodePub)
}

// ForEachNodeChannel iterates through all channels of the given node,
Expand Down
11 changes: 3 additions & 8 deletions cmd/multinode2/graph_mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,24 +193,19 @@ func (g *GraphSourceMux) ForEachNode(ctx context.Context,
// a new one will be created.
//
// NOTE: this is part of the GraphSource interface.
func (g *GraphSourceMux) FetchLightningNode(ctx context.Context, tx graphdb.RTx,
func (g *GraphSourceMux) FetchLightningNode(ctx context.Context,
nodePub route.Vertex) (*models.LightningNode, error) {

srcPub, err := g.selfNodePub()
if err != nil {
return nil, err
}

lTx, rTx, err := extractRTxSet(tx)
if err != nil {
return nil, err
}

if bytes.Equal(srcPub[:], nodePub[:]) {
return g.local.FetchLightningNode(ctx, lTx, nodePub)
return g.local.FetchLightningNode(ctx, nodePub)
}

return g.remote.FetchLightningNode(ctx, rTx, nodePub)
return g.remote.FetchLightningNode(ctx, nodePub)
}

// ForEachNodeChannel iterates through all channels of the given node,
Expand Down
2 changes: 1 addition & 1 deletion cmd/multinode2/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func (r *remoteWrapper) FetchNodeFeatures(ctx context.Context, tx graphdb.RTx, n
return unmarshalFeatures(resp.Node.Features), nil
}

func (r *remoteWrapper) FetchLightningNode(ctx context.Context, tx graphdb.RTx,
func (r *remoteWrapper) FetchLightningNode(ctx context.Context,
nodePub route.Vertex) (*models.LightningNode, error) {

resp, err := r.lnConn.GetNodeInfo(ctx, &lnrpc.NodeInfoRequest{
Expand Down
11 changes: 3 additions & 8 deletions cmd/multinode3/graph_mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,24 +193,19 @@ func (g *GraphSourceMux) ForEachNode(ctx context.Context,
// a new one will be created.
//
// NOTE: this is part of the GraphSource interface.
func (g *GraphSourceMux) FetchLightningNode(ctx context.Context, tx graphdb.RTx,
func (g *GraphSourceMux) FetchLightningNode(ctx context.Context,
nodePub route.Vertex) (*models.LightningNode, error) {

srcPub, err := g.selfNodePub()
if err != nil {
return nil, err
}

lTx, rTx, err := extractRTxSet(tx)
if err != nil {
return nil, err
}

if bytes.Equal(srcPub[:], nodePub[:]) {
return g.local.FetchLightningNode(ctx, lTx, nodePub)
return g.local.FetchLightningNode(ctx, nodePub)
}

return g.remote.FetchLightningNode(ctx, rTx, nodePub)
return g.remote.FetchLightningNode(ctx, nodePub)
}

// ForEachNodeChannel iterates through all channels of the given node,
Expand Down
2 changes: 1 addition & 1 deletion cmd/multinode3/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func (r *remoteWrapper) FetchNodeFeatures(ctx context.Context, tx graphdb.RTx, n
return unmarshalFeatures(resp.Node.Features), nil
}

func (r *remoteWrapper) FetchLightningNode(ctx context.Context, tx graphdb.RTx,
func (r *remoteWrapper) FetchLightningNode(ctx context.Context,
nodePub route.Vertex) (*models.LightningNode, error) {

resp, err := r.lnConn.GetNodeInfo(ctx, &lnrpc.NodeInfoRequest{
Expand Down
2 changes: 1 addition & 1 deletion graph/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1626,7 +1626,7 @@ func (b *Builder) GetChannelByID(chanID lnwire.ShortChannelID) (
func (b *Builder) FetchLightningNode(
node route.Vertex) (*models.LightningNode, error) {

return b.cfg.Graph.FetchLightningNode(context.TODO(), nil, node)
return b.cfg.Graph.FetchLightningNode(context.TODO(), node)
}

// ForAllOutgoingChannels is used to iterate over all outgoing channels owned by
Expand Down
29 changes: 14 additions & 15 deletions graph/db/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ func (c *ChannelGraph) AddrsForNode(ctx context.Context, nodePub *btcec.PublicKe
return false, nil, err
}

node, err := c.FetchLightningNode(ctx, nil, pubKey)
node, err := c.FetchLightningNodeWithTx(nil, pubKey)
// We don't consider it an error if the graph is unaware of the node.
switch {
case err != nil && !errors.Is(err, ErrGraphNodeNotFound):
Expand Down Expand Up @@ -586,8 +586,13 @@ func (c *ChannelGraph) FetchNodeFeatures(ctx context.Context, tx RTx,
return c.graphCache.GetFeatures(node), nil
}

kvdbRTx, err := extractKVDBRTx(tx)
if err != nil {
return nil, err
}

// Fallback that uses the database.
targetNode, err := c.FetchLightningNode(ctx, tx, node)
targetNode, err := c.FetchLightningNodeWithTx(kvdbRTx, node)
switch err {
// If the node exists and has features, return them directly.
case nil:
Expand Down Expand Up @@ -2980,19 +2985,18 @@ func (c *ChannelGraph) isPublic(tx kvdb.RTx, nodePub route.Vertex,

// FetchLightningNode attempts to look up a target node by its identity public
// key. If the node isn't found in the database, then ErrGraphNodeNotFound is
// returned. An optional transaction may be provided. If none is provided, then
// a new one will be created.
func (c *ChannelGraph) FetchLightningNode(_ context.Context, tx RTx,
// returned.
func (c *ChannelGraph) FetchLightningNode(_ context.Context,
nodePub route.Vertex) (*models.LightningNode, error) {

return c.fetchLightningNode(tx, nodePub)
return c.FetchLightningNodeWithTx(nil, nodePub)
}

// fetchLightningNode attempts to look up a target node by its identity public
// FetchLightningNodeWithTx attempts to look up a target node by its identity public
// key. If the node isn't found in the database, then ErrGraphNodeNotFound is
// returned. An optional transaction may be provided. If none is provided, then
// a new one will be created.
func (c *ChannelGraph) fetchLightningNode(tx RTx,
func (c *ChannelGraph) FetchLightningNodeWithTx(tx kvdb.RTx,
nodePub route.Vertex) (*models.LightningNode, error) {

var node *models.LightningNode
Expand Down Expand Up @@ -3024,12 +3028,7 @@ func (c *ChannelGraph) fetchLightningNode(tx RTx,
return nil
}

kvdbRTx, err := extractKVDBRTx(tx)
if err != nil {
return nil, err
}

if kvdbRTx == nil {
if tx == nil {
err := kvdb.View(
c.db, fetch, func() {
node = nil
Expand All @@ -3042,7 +3041,7 @@ func (c *ChannelGraph) fetchLightningNode(tx RTx,
return node, nil
}

err = fetch(kvdbRTx)
err := fetch(tx)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions graph/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ type DB interface {
// FetchLightningNode attempts to look up a target node by its identity
// public key. If the node isn't found in the database, then
// ErrGraphNodeNotFound is returned.
FetchLightningNode(ctx context.Context, tx graphdb.RTx,
nodePub route.Vertex) (*models.LightningNode, error)
FetchLightningNode(ctx context.Context, nodePub route.Vertex) (
*models.LightningNode, error)

// ForEachNode iterates through all the stored vertices/nodes in the
// graph, executing the passed callback with each node encountered. If
Expand Down
5 changes: 2 additions & 3 deletions interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/btcsuite/btcd/btcec/v2"
"github.com/lightningnetwork/lnd/channeldb"
graphdb "github.com/lightningnetwork/lnd/graph/db"
"github.com/lightningnetwork/lnd/graph/db/models"
"github.com/lightningnetwork/lnd/graph/graphsession"
"github.com/lightningnetwork/lnd/graph/stats"
Expand Down Expand Up @@ -70,8 +69,8 @@ type GraphSource interface {
// public key. If the node isn't found in the database, then
// graphdb.ErrGraphNodeNotFound is returned. An optional transaction may
// be provided. If none is provided, then a new one will be created.
FetchLightningNode(ctx context.Context, tx graphdb.RTx,
nodePub route.Vertex) (*models.LightningNode, error)
FetchLightningNode(ctx context.Context, nodePub route.Vertex) (
*models.LightningNode, error)
}

// Providers is an interface that LND itself can satisfy.
Expand Down
4 changes: 2 additions & 2 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6778,7 +6778,7 @@ func (r *rpcServer) GetNodeInfo(ctx context.Context,
// With the public key decoded, attempt to fetch the node corresponding
// to this public key. If the node cannot be found, then an error will
// be returned.
node, err := graph.FetchLightningNode(ctx, nil, pubKey)
node, err := graph.FetchLightningNode(ctx, pubKey)
switch {
case errors.Is(err, graphdb.ErrGraphNodeNotFound):
return nil, status.Error(codes.NotFound, err.Error())
Expand Down Expand Up @@ -7775,7 +7775,7 @@ func (r *rpcServer) ForwardingHistory(ctx context.Context,
return "", err
}

peer, err := r.server.graphSource.FetchLightningNode(ctx, nil, vertex)
peer, err := r.server.graphSource.FetchLightningNode(ctx, vertex)
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4908,7 +4908,7 @@ func (s *server) fetchNodeAdvertisedAddrs(pub *btcec.PublicKey) ([]net.Addr, err
return nil, err
}

node, err := s.graphSource.FetchLightningNode(context.TODO(), nil, vertex)
node, err := s.graphSource.FetchLightningNode(context.TODO(), vertex)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 7fc2e04

Please sign in to comment.