Skip to content

Commit

Permalink
Merge pull request lightningnetwork#8117 from ellemouton/removeDBFrom…
Browse files Browse the repository at this point in the history
…ChannelDBSchemas

channeldb+refactor: remove `kvdb.Backend` from channel DB schemas
  • Loading branch information
ellemouton authored Nov 9, 2023
2 parents 6de3fea + 0e82293 commit 6122452
Show file tree
Hide file tree
Showing 49 changed files with 1,443 additions and 1,376 deletions.
82 changes: 49 additions & 33 deletions autopilot/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
"github.com/btcsuite/btcd/btcutil"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/routing/route"
Expand Down Expand Up @@ -53,6 +54,8 @@ func ChannelGraphFromDatabase(db *channeldb.ChannelGraph) ChannelGraph {
// channeldb.LightningNode. The wrapper method implement the autopilot.Node
// interface.
type dbNode struct {
db *channeldb.ChannelGraph

tx kvdb.RTx

node *channeldb.LightningNode
Expand All @@ -67,15 +70,15 @@ var _ Node = (*dbNode)(nil)
// will be returned in serialized compressed format.
//
// NOTE: Part of the autopilot.Node interface.
func (d dbNode) PubKey() [33]byte {
func (d *dbNode) PubKey() [33]byte {
return d.node.PubKeyBytes
}

// Addrs returns a slice of publicly reachable public TCP addresses that the
// peer is known to be listening on.
//
// NOTE: Part of the autopilot.Node interface.
func (d dbNode) Addrs() []net.Addr {
func (d *dbNode) Addrs() []net.Addr {
return d.node.Addresses
}

Expand All @@ -85,32 +88,42 @@ func (d dbNode) Addrs() []net.Addr {
// describes the active channel.
//
// NOTE: Part of the autopilot.Node interface.
func (d dbNode) ForEachChannel(cb func(ChannelEdge) error) error {
return d.node.ForEachChannel(d.tx, func(tx kvdb.RTx,
ei *channeldb.ChannelEdgeInfo, ep, _ *channeldb.ChannelEdgePolicy) error {

// Skip channels for which no outgoing edge policy is available.
//
// TODO(joostjager): Ideally the case where channels have a nil
// policy should be supported, as autopilot is not looking at
// the policies. For now, it is not easily possible to get a
// reference to the other end LightningNode object without
// retrieving the policy.
if ep == nil {
return nil
}
func (d *dbNode) ForEachChannel(cb func(ChannelEdge) error) error {
return d.db.ForEachNodeChannel(d.tx, d.node.PubKeyBytes,
func(tx kvdb.RTx, ei *models.ChannelEdgeInfo, ep,
_ *models.ChannelEdgePolicy) error {

// Skip channels for which no outgoing edge policy is
// available.
//
// TODO(joostjager): Ideally the case where channels
// have a nil policy should be supported, as autopilot
// is not looking at the policies. For now, it is not
// easily possible to get a reference to the other end
// LightningNode object without retrieving the policy.
if ep == nil {
return nil
}

edge := ChannelEdge{
ChanID: lnwire.NewShortChanIDFromInt(ep.ChannelID),
Capacity: ei.Capacity,
Peer: dbNode{
tx: tx,
node: ep.Node,
},
}
node, err := d.db.FetchLightningNode(tx, ep.ToNode)
if err != nil {
return err
}

return cb(edge)
})
edge := ChannelEdge{
ChanID: lnwire.NewShortChanIDFromInt(
ep.ChannelID,
),
Capacity: ei.Capacity,
Peer: &dbNode{
tx: tx,
db: d.db,
node: node,
},
}

return cb(edge)
})
}

// ForEachNode is a higher-order function that should be called once for each
Expand All @@ -127,7 +140,8 @@ func (d *databaseChannelGraph) ForEachNode(cb func(Node) error) error {
return nil
}

node := dbNode{
node := &dbNode{
db: d.db,
tx: tx,
node: n,
}
Expand All @@ -150,7 +164,7 @@ func (d *databaseChannelGraph) addRandChannel(node1, node2 *btcec.PublicKey,
return nil, err
}

dbNode, err := d.db.FetchLightningNode(vertex)
dbNode, err := d.db.FetchLightningNode(nil, vertex)
switch {
case err == channeldb.ErrGraphNodeNotFound:
fallthrough
Expand Down Expand Up @@ -222,15 +236,15 @@ func (d *databaseChannelGraph) addRandChannel(node1, node2 *btcec.PublicKey,
}

chanID := randChanID()
edge := &channeldb.ChannelEdgeInfo{
edge := &models.ChannelEdgeInfo{
ChannelID: chanID.ToUint64(),
Capacity: capacity,
}
edge.AddNodeKeys(lnNode1, lnNode2, lnNode1, lnNode2)
if err := d.db.AddChannelEdge(edge); err != nil {
return nil, nil, err
}
edgePolicy := &channeldb.ChannelEdgePolicy{
edgePolicy := &models.ChannelEdgePolicy{
SigBytes: testSig.Serialize(),
ChannelID: chanID.ToUint64(),
LastUpdate: time.Now(),
Expand All @@ -246,7 +260,7 @@ func (d *databaseChannelGraph) addRandChannel(node1, node2 *btcec.PublicKey,
if err := d.db.UpdateEdgePolicy(edgePolicy); err != nil {
return nil, nil, err
}
edgePolicy = &channeldb.ChannelEdgePolicy{
edgePolicy = &models.ChannelEdgePolicy{
SigBytes: testSig.Serialize(),
ChannelID: chanID.ToUint64(),
LastUpdate: time.Now(),
Expand All @@ -265,14 +279,16 @@ func (d *databaseChannelGraph) addRandChannel(node1, node2 *btcec.PublicKey,
return &ChannelEdge{
ChanID: chanID,
Capacity: capacity,
Peer: dbNode{
Peer: &dbNode{
db: d.db,
node: vertex1,
},
},
&ChannelEdge{
ChanID: chanID,
Capacity: capacity,
Peer: dbNode{
Peer: &dbNode{
db: d.db,
node: vertex2,
},
},
Expand Down
4 changes: 3 additions & 1 deletion channeldb/channel_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package channeldb
import (
"reflect"
"testing"

"github.com/lightningnetwork/lnd/channeldb/models"
)

// TestChannelCache checks the behavior of the channelCache with respect to
Expand Down Expand Up @@ -98,7 +100,7 @@ func assertHasChanEntries(t *testing.T, c *channelCache, start, end uint64) {
// channelForInt generates a unique ChannelEdge given an integer.
func channelForInt(i uint64) ChannelEdge {
return ChannelEdge{
Info: &ChannelEdgeInfo{
Info: &models.ChannelEdgeInfo{
ChannelID: i,
},
}
Expand Down
2 changes: 1 addition & 1 deletion channeldb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1351,7 +1351,7 @@ func (d *DB) AddrsForNode(nodePub *btcec.PublicKey) ([]net.Addr,
if err != nil {
return nil, err
}
graphNode, err := d.graph.FetchLightningNode(pubKey)
graphNode, err := d.graph.FetchLightningNode(nil, pubKey)
if err != nil && err != ErrGraphNodeNotFound {
return nil, err
} else if err == ErrGraphNodeNotFound {
Expand Down
Loading

0 comments on commit 6122452

Please sign in to comment.