Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1/3] Graph RIP: refactor+graph: move all graph related DB code to the graph package #9236

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
62ab59f
channeldb: export net.Addr encode/decode methods
ellemouton Oct 22, 2024
5ed3057
channeldb+graph/db: move net.Addr encode/decode to graph db package
ellemouton Oct 22, 2024
b28b4b6
channeldb/graphdb: move outpoint ser/deser funcs to graphdb
ellemouton Oct 22, 2024
2a79c7c
contractcourt: use graphdb outpoint helpers
ellemouton Oct 22, 2024
6534f37
multi+refactor: move models package to graph/db
ellemouton Oct 22, 2024
36eec1d
refactor: move graph related DB code to graph/db from channeldb
ellemouton Oct 22, 2024
58342da
channeldb+graphdb: init graph DB outside of channel db
ellemouton Oct 22, 2024
d02ab58
channeldb: define a single AddrSource interface
ellemouton Oct 22, 2024
cc4b677
channeldb: implement a multi-source AddrSource
ellemouton Oct 22, 2024
3f7a282
channeldb: let AddrsForNode indicate if the node was found or not
ellemouton Nov 3, 2024
829d50e
multi: let chan and graph db implement AddrSource
ellemouton Oct 22, 2024
abfad5b
channeldb: remove graph db from channeldb
ellemouton Oct 22, 2024
adeaf15
refactor: move graphsession pkg to graph package
ellemouton Oct 22, 2024
9431edb
multi: move LightningNode struct to models package
ellemouton Oct 29, 2024
1598f14
multi: remove kvdb.Tx from ChannelGraphSource.ForAllOutgoingChannels
ellemouton Oct 29, 2024
7edf99f
itest: assert no failed updates in test
ellemouton Oct 29, 2024
fb6c6ba
multi: fix linter errors
ellemouton Oct 24, 2024
aa92e4d
docs: add release notes entry for 9236
ellemouton Nov 1, 2024
e017ccf
TEMP: show diff files separation works
ellemouton Oct 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions autopilot/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package autopilot
import (
"bytes"
"encoding/hex"
"errors"
"net"
"sort"
"sync/atomic"
Expand All @@ -11,8 +12,8 @@ import (
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
"github.com/btcsuite/btcd/btcutil"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
graphdb "github.com/lightningnetwork/lnd/graph/db"
"github.com/lightningnetwork/lnd/graph/db/models"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/routing/route"
Expand All @@ -35,7 +36,7 @@ var (
//
// TODO(roasbeef): move inmpl to main package?
type databaseChannelGraph struct {
db *channeldb.ChannelGraph
db *graphdb.ChannelGraph
}

// A compile time assertion to ensure databaseChannelGraph meets the
Expand All @@ -44,7 +45,7 @@ var _ ChannelGraph = (*databaseChannelGraph)(nil)

// ChannelGraphFromDatabase returns an instance of the autopilot.ChannelGraph
// backed by a live, open channeldb instance.
func ChannelGraphFromDatabase(db *channeldb.ChannelGraph) ChannelGraph {
func ChannelGraphFromDatabase(db *graphdb.ChannelGraph) ChannelGraph {
return &databaseChannelGraph{
db: db,
}
Expand All @@ -54,11 +55,11 @@ func ChannelGraphFromDatabase(db *channeldb.ChannelGraph) ChannelGraph {
// channeldb.LightningNode. The wrapper method implement the autopilot.Node
// interface.
type dbNode struct {
db *channeldb.ChannelGraph
db *graphdb.ChannelGraph

tx kvdb.RTx

node *channeldb.LightningNode
node *models.LightningNode
}

// A compile time assertion to ensure dbNode meets the autopilot.Node
Expand Down Expand Up @@ -134,7 +135,9 @@ func (d *dbNode) ForEachChannel(cb func(ChannelEdge) error) error {
//
// NOTE: Part of the autopilot.ChannelGraph interface.
func (d *databaseChannelGraph) ForEachNode(cb func(Node) error) error {
return d.db.ForEachNode(func(tx kvdb.RTx, n *channeldb.LightningNode) error {
return d.db.ForEachNode(func(tx kvdb.RTx,
n *models.LightningNode) error {

// We'll skip over any node that doesn't have any advertised
// addresses. As we won't be able to reach them to actually
// open any channels.
Expand All @@ -157,7 +160,7 @@ func (d *databaseChannelGraph) ForEachNode(cb func(Node) error) error {
func (d *databaseChannelGraph) addRandChannel(node1, node2 *btcec.PublicKey,
capacity btcutil.Amount) (*ChannelEdge, *ChannelEdge, error) {

fetchNode := func(pub *btcec.PublicKey) (*channeldb.LightningNode, error) {
fetchNode := func(pub *btcec.PublicKey) (*models.LightningNode, error) {
if pub != nil {
vertex, err := route.NewVertexFromBytes(
pub.SerializeCompressed(),
Expand All @@ -168,10 +171,10 @@ func (d *databaseChannelGraph) addRandChannel(node1, node2 *btcec.PublicKey,

dbNode, err := d.db.FetchLightningNode(vertex)
switch {
case err == channeldb.ErrGraphNodeNotFound:
case errors.Is(err, graphdb.ErrGraphNodeNotFound):
fallthrough
case err == channeldb.ErrGraphNotFound:
graphNode := &channeldb.LightningNode{
case errors.Is(err, graphdb.ErrGraphNotFound):
graphNode := &models.LightningNode{
HaveNodeAnnouncement: true,
Addresses: []net.Addr{
&net.TCPAddr{
Expand All @@ -198,7 +201,7 @@ func (d *databaseChannelGraph) addRandChannel(node1, node2 *btcec.PublicKey,
if err != nil {
return nil, err
}
dbNode := &channeldb.LightningNode{
dbNode := &models.LightningNode{
HaveNodeAnnouncement: true,
Addresses: []net.Addr{
&net.TCPAddr{
Expand Down Expand Up @@ -302,7 +305,7 @@ func (d *databaseChannelGraph) addRandNode() (*btcec.PublicKey, error) {
if err != nil {
return nil, err
}
dbNode := &channeldb.LightningNode{
dbNode := &models.LightningNode{
HaveNodeAnnouncement: true,
Addresses: []net.Addr{
&net.TCPAddr{
Expand Down Expand Up @@ -478,7 +481,7 @@ func (m *memChannelGraph) addRandNode() (*btcec.PublicKey, error) {
// databaseChannelGraphCached wraps a channeldb.ChannelGraph instance with the
// necessary API to properly implement the autopilot.ChannelGraph interface.
type databaseChannelGraphCached struct {
db *channeldb.ChannelGraph
db *graphdb.ChannelGraph
}

// A compile time assertion to ensure databaseChannelGraphCached meets the
Expand All @@ -487,7 +490,7 @@ var _ ChannelGraph = (*databaseChannelGraphCached)(nil)

// ChannelGraphFromCachedDatabase returns an instance of the
// autopilot.ChannelGraph backed by a live, open channeldb instance.
func ChannelGraphFromCachedDatabase(db *channeldb.ChannelGraph) ChannelGraph {
func ChannelGraphFromCachedDatabase(db *graphdb.ChannelGraph) ChannelGraph {
return &databaseChannelGraphCached{
db: db,
}
Expand All @@ -498,7 +501,7 @@ func ChannelGraphFromCachedDatabase(db *channeldb.ChannelGraph) ChannelGraph {
// interface.
type dbNodeCached struct {
node route.Vertex
channels map[uint64]*channeldb.DirectedChannel
channels map[uint64]*graphdb.DirectedChannel
}

// A compile time assertion to ensure dbNodeCached meets the autopilot.Node
Expand Down Expand Up @@ -552,7 +555,7 @@ func (nc dbNodeCached) ForEachChannel(cb func(ChannelEdge) error) error {
// NOTE: Part of the autopilot.ChannelGraph interface.
func (dc *databaseChannelGraphCached) ForEachNode(cb func(Node) error) error {
return dc.db.ForEachNodeCached(func(n route.Vertex,
channels map[uint64]*channeldb.DirectedChannel) error {
channels map[uint64]*graphdb.DirectedChannel) error {

if len(channels) > 0 {
node := dbNodeCached{
Expand Down
23 changes: 14 additions & 9 deletions autopilot/prefattach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (

"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
"github.com/lightningnetwork/lnd/channeldb"
graphdb "github.com/lightningnetwork/lnd/graph/db"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/stretchr/testify/require"
)

Expand All @@ -24,17 +25,21 @@ type testGraph interface {
}

func newDiskChanGraph(t *testing.T) (testGraph, error) {
// Next, create channeldb for the first time.
cdb, err := channeldb.Open(t.TempDir())
if err != nil {
return nil, err
}
t.Cleanup(func() {
require.NoError(t, cdb.Close())
backend, err := kvdb.GetBoltBackend(&kvdb.BoltBackendConfig{
DBPath: t.TempDir(),
DBFileName: "graph.db",
NoFreelistSync: true,
AutoCompact: false,
AutoCompactMinAge: kvdb.DefaultBoltAutoCompactMinAge,
DBTimeout: kvdb.DefaultDBTimeout,
})
require.NoError(t, err)

graphDB, err := graphdb.NewChannelGraph(backend)
require.NoError(t, err)

return &databaseChannelGraph{
db: cdb.ChannelGraph(),
db: graphDB,
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion chainreg/chainregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"github.com/lightningnetwork/lnd/chainntnfs/btcdnotify"
"github.com/lightningnetwork/lnd/chainntnfs/neutrinonotify"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/fn"
"github.com/lightningnetwork/lnd/graph/db/models"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/kvdb"
Expand Down
4 changes: 2 additions & 2 deletions chainreg/no_chain_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/btcsuite/btcwallet/chain"
"github.com/btcsuite/btcwallet/waddrmgr"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/channeldb"
graphdb "github.com/lightningnetwork/lnd/graph/db"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
"github.com/lightningnetwork/lnd/routing/chainview"
)
Expand Down Expand Up @@ -94,7 +94,7 @@ func (n *NoChainBackend) DisconnectedBlocks() <-chan *chainview.FilteredBlock {
return make(chan *chainview.FilteredBlock)
}

func (n *NoChainBackend) UpdateFilter([]channeldb.EdgePoint, uint32) error {
func (n *NoChainBackend) UpdateFilter([]graphdb.EdgePoint, uint32) error {
return nil
}

Expand Down
27 changes: 9 additions & 18 deletions chanbackup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package chanbackup

import (
"fmt"
"net"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/fn"
"github.com/lightningnetwork/lnd/kvdb"
)

// LiveChannelSource is an interface that allows us to query for the set of
Expand All @@ -20,34 +17,28 @@ type LiveChannelSource interface {

// FetchChannel attempts to locate a live channel identified by the
// passed chanPoint. Optionally an existing db tx can be supplied.
FetchChannel(tx kvdb.RTx, chanPoint wire.OutPoint) (
*channeldb.OpenChannel, error)
}

// AddressSource is an interface that allows us to query for the set of
// addresses a node can be connected to.
type AddressSource interface {
// AddrsForNode returns all known addresses for the target node public
// key.
AddrsForNode(nodePub *btcec.PublicKey) ([]net.Addr, error)
FetchChannel(chanPoint wire.OutPoint) (*channeldb.OpenChannel, error)
}

// assembleChanBackup attempts to assemble a static channel backup for the
// passed open channel. The backup includes all information required to restore
// the channel, as well as addressing information so we can find the peer and
// reconnect to them to initiate the protocol.
func assembleChanBackup(addrSource AddressSource,
func assembleChanBackup(addrSource channeldb.AddrSource,
openChan *channeldb.OpenChannel) (*Single, error) {

log.Debugf("Crafting backup for ChannelPoint(%v)",
openChan.FundingOutpoint)

// First, we'll query the channel source to obtain all the addresses
// that are associated with the peer for this channel.
nodeAddrs, err := addrSource.AddrsForNode(openChan.IdentityPub)
known, nodeAddrs, err := addrSource.AddrsForNode(openChan.IdentityPub)
if err != nil {
return nil, err
}
if !known {
return nil, fmt.Errorf("node unknown by address source")
}

single := NewSingle(openChan, nodeAddrs)

Expand Down Expand Up @@ -100,11 +91,11 @@ func buildCloseTxInputs(
// the target channel identified by its channel point. If we're unable to find
// the target channel, then an error will be returned.
func FetchBackupForChan(chanPoint wire.OutPoint, chanSource LiveChannelSource,
addrSource AddressSource) (*Single, error) {
addrSource channeldb.AddrSource) (*Single, error) {

// First, we'll query the channel source to see if the channel is known
// and open within the database.
targetChan, err := chanSource.FetchChannel(nil, chanPoint)
targetChan, err := chanSource.FetchChannel(chanPoint)
if err != nil {
// If we can't find the channel, then we return with an error,
// as we have nothing to backup.
Expand All @@ -124,7 +115,7 @@ func FetchBackupForChan(chanPoint wire.OutPoint, chanSource LiveChannelSource,
// FetchStaticChanBackups will return a plaintext static channel back up for
// all known active/open channels within the passed channel source.
func FetchStaticChanBackups(chanSource LiveChannelSource,
addrSource AddressSource) ([]Single, error) {
addrSource channeldb.AddrSource) ([]Single, error) {

// First, we'll query the backup source for information concerning all
// currently open and available channels.
Expand Down
14 changes: 6 additions & 8 deletions chanbackup/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -40,7 +39,7 @@ func (m *mockChannelSource) FetchAllChannels() ([]*channeldb.OpenChannel, error)
return chans, nil
}

func (m *mockChannelSource) FetchChannel(_ kvdb.RTx, chanPoint wire.OutPoint) (
func (m *mockChannelSource) FetchChannel(chanPoint wire.OutPoint) (
*channeldb.OpenChannel, error) {

if m.failQuery {
Expand All @@ -62,20 +61,19 @@ func (m *mockChannelSource) addAddrsForNode(nodePub *btcec.PublicKey, addrs []ne
m.addrs[nodeKey] = addrs
}

func (m *mockChannelSource) AddrsForNode(nodePub *btcec.PublicKey) ([]net.Addr, error) {
func (m *mockChannelSource) AddrsForNode(nodePub *btcec.PublicKey) (bool,
[]net.Addr, error) {

if m.failQuery {
return nil, fmt.Errorf("fail")
return false, nil, fmt.Errorf("fail")
}

var nodeKey [33]byte
copy(nodeKey[:], nodePub.SerializeCompressed())

addrs, ok := m.addrs[nodeKey]
if !ok {
return nil, fmt.Errorf("can't find addr")
}

return addrs, nil
return ok, addrs, nil
}

// TestFetchBackupForChan tests that we're able to construct a single channel
Expand Down
15 changes: 2 additions & 13 deletions channel_notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,13 @@ package lnd

import (
"fmt"
"net"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/chanbackup"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channelnotifier"
)

// addrSource is an interface that allow us to get the addresses for a target
// node. We'll need this in order to be able to properly proxy the
// notifications to create SCBs.
type addrSource interface {
// AddrsForNode returns all known addresses for the target node public
// key.
AddrsForNode(nodePub *btcec.PublicKey) ([]net.Addr, error)
}

// channelNotifier is an implementation of the chanbackup.ChannelNotifier
// interface using the existing channelnotifier.ChannelNotifier struct. This
// implementation allows us to satisfy all the dependencies of the
Expand All @@ -32,7 +21,7 @@ type channelNotifier struct {
// addrs is an implementation of the addrSource interface that allows
// us to get the latest set of addresses for a given node. We'll need
// this to be able to create an SCB for new channels.
addrs addrSource
addrs channeldb.AddrSource
}

// SubscribeChans requests a new channel subscription relative to the initial
Expand All @@ -56,7 +45,7 @@ func (c *channelNotifier) SubscribeChans(startingChans map[wire.OutPoint]struct{
// chanUpdates channel to inform subscribers about new pending or
// confirmed channels.
sendChanOpenUpdate := func(newOrPendingChan *channeldb.OpenChannel) {
nodeAddrs, err := c.addrs.AddrsForNode(
_, nodeAddrs, err := c.addrs.AddrsForNode(
newOrPendingChan.IdentityPub,
)
if err != nil {
Expand Down
Loading
Loading