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

In-memory graph cache for faster pathfinding #5642

Merged
merged 15 commits into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion autopilot/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (d *databaseChannelGraph) addRandChannel(node1, node2 *btcec.PublicKey,
return nil, err
}

dbNode, err := d.db.FetchLightningNode(nil, vertex)
dbNode, err := d.db.FetchLightningNode(vertex)
switch {
case err == channeldb.ErrGraphNodeNotFound:
fallthrough
Expand Down
2 changes: 1 addition & 1 deletion chainreg/chainregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type Config struct {

// ChanStateDB is a pointer to the database that stores the channel
// state.
ChanStateDB *channeldb.DB
ChanStateDB *channeldb.ChannelStateDB

// BlockCacheSize is the size (in bytes) of blocks kept in memory.
BlockCacheSize uint64
Expand Down
22 changes: 14 additions & 8 deletions chanbackup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ type LiveChannelSource interface {
// 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)
Expand All @@ -31,15 +35,15 @@ type LiveChannelSource interface {
// 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(chanSource LiveChannelSource,
func assembleChanBackup(addrSource AddressSource,
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 are associated with the peer for this channel.
nodeAddrs, err := chanSource.AddrsForNode(openChan.IdentityPub)
// that are associated with the peer for this channel.
nodeAddrs, err := addrSource.AddrsForNode(openChan.IdentityPub)
if err != nil {
return nil, err
}
Expand All @@ -52,8 +56,8 @@ func assembleChanBackup(chanSource LiveChannelSource,
// FetchBackupForChan attempts to create a plaintext static channel backup for
// 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) (*Single, error) {
func FetchBackupForChan(chanPoint wire.OutPoint, chanSource LiveChannelSource,
addrSource AddressSource) (*Single, error) {

// First, we'll query the channel source to see if the channel is known
// and open within the database.
Expand All @@ -66,7 +70,7 @@ func FetchBackupForChan(chanPoint wire.OutPoint,

// Once we have the target channel, we can assemble the backup using
// the source to obtain any extra information that we may need.
staticChanBackup, err := assembleChanBackup(chanSource, targetChan)
staticChanBackup, err := assembleChanBackup(addrSource, targetChan)
if err != nil {
return nil, fmt.Errorf("unable to create chan backup: %v", err)
}
Expand All @@ -76,7 +80,9 @@ func FetchBackupForChan(chanPoint wire.OutPoint,

// FetchStaticChanBackups will return a plaintext static channel back up for
// all known active/open channels within the passed channel source.
func FetchStaticChanBackups(chanSource LiveChannelSource) ([]Single, error) {
func FetchStaticChanBackups(chanSource LiveChannelSource,
addrSource AddressSource) ([]Single, error) {

// First, we'll query the backup source for information concerning all
// currently open and available channels.
openChans, err := chanSource.FetchAllChannels()
Expand All @@ -89,7 +95,7 @@ func FetchStaticChanBackups(chanSource LiveChannelSource) ([]Single, error) {
// channel.
staticChanBackups := make([]Single, 0, len(openChans))
for _, openChan := range openChans {
chanBackup, err := assembleChanBackup(chanSource, openChan)
chanBackup, err := assembleChanBackup(addrSource, openChan)
if err != nil {
return nil, err
}
Expand Down
10 changes: 6 additions & 4 deletions chanbackup/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ func TestFetchBackupForChan(t *testing.T) {
},
}
for i, testCase := range testCases {
_, err := FetchBackupForChan(testCase.chanPoint, chanSource)
_, err := FetchBackupForChan(
testCase.chanPoint, chanSource, chanSource,
)
switch {
// If this is a valid test case, and we failed, then we'll
// return an error.
Expand Down Expand Up @@ -167,7 +169,7 @@ func TestFetchStaticChanBackups(t *testing.T) {
// With the channel source populated, we'll now attempt to create a set
// of backups for all the channels. This should succeed, as all items
// are populated within the channel source.
backups, err := FetchStaticChanBackups(chanSource)
backups, err := FetchStaticChanBackups(chanSource, chanSource)
if err != nil {
t.Fatalf("unable to create chan back ups: %v", err)
}
Expand All @@ -184,7 +186,7 @@ func TestFetchStaticChanBackups(t *testing.T) {
copy(n[:], randomChan2.IdentityPub.SerializeCompressed())
delete(chanSource.addrs, n)

_, err = FetchStaticChanBackups(chanSource)
_, err = FetchStaticChanBackups(chanSource, chanSource)
if err == nil {
t.Fatalf("query with incomplete information should fail")
}
Expand All @@ -193,7 +195,7 @@ func TestFetchStaticChanBackups(t *testing.T) {
// source at all, then we'll fail as well.
chanSource = newMockChannelSource()
chanSource.failQuery = true
_, err = FetchStaticChanBackups(chanSource)
_, err = FetchStaticChanBackups(chanSource, chanSource)
if err == nil {
t.Fatalf("query should fail")
}
Expand Down