2019-10-24 13:04:26 +03:00
|
|
|
package migration_01_to_11
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
2019-12-13 05:22:19 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb/kvdb"
|
2019-10-24 13:04:26 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
dbName = "channel.db"
|
|
|
|
dbFilePermission = 0600
|
|
|
|
)
|
|
|
|
|
|
|
|
// migration is a function which takes a prior outdated version of the database
|
|
|
|
// instances and mutates the key/bucket structure to arrive at a more
|
|
|
|
// up-to-date version of the database.
|
2019-12-13 05:22:19 +03:00
|
|
|
type migration func(tx kvdb.RwTx) error
|
2019-10-24 13:04:26 +03:00
|
|
|
|
|
|
|
var (
|
|
|
|
// Big endian is the preferred byte order, due to cursor scans over
|
|
|
|
// integer keys iterating in order.
|
|
|
|
byteOrder = binary.BigEndian
|
|
|
|
)
|
|
|
|
|
|
|
|
// DB is the primary datastore for the lnd daemon. The database stores
|
|
|
|
// information related to nodes, routing data, open/closed channels, fee
|
|
|
|
// schedules, and reputation data.
|
|
|
|
type DB struct {
|
2019-12-13 05:22:19 +03:00
|
|
|
kvdb.Backend
|
2019-10-24 13:04:26 +03:00
|
|
|
dbPath string
|
|
|
|
graph *ChannelGraph
|
|
|
|
now func() time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open opens an existing channeldb. Any necessary schemas migrations due to
|
|
|
|
// updates will take place as necessary.
|
|
|
|
func Open(dbPath string, modifiers ...OptionModifier) (*DB, error) {
|
|
|
|
path := filepath.Join(dbPath, dbName)
|
|
|
|
|
|
|
|
if !fileExists(path) {
|
|
|
|
if err := createChannelDB(dbPath); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := DefaultOptions()
|
|
|
|
for _, modifier := range modifiers {
|
|
|
|
modifier(&opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Specify bbolt freelist options to reduce heap pressure in case the
|
|
|
|
// freelist grows to be very large.
|
kvdb: add timeout options for bbolt (#4787)
* mod: bump btcwallet version to accept db timeout
* btcwallet: add DBTimeOut in config
* kvdb: add database timeout option for bbolt
This commit adds a DBTimeout option in bbolt config. The relevant
functions walletdb.Open/Create are updated to use this config. In
addition, the bolt compacter also applies the new timeout option.
* channeldb: add DBTimeout in db options
This commit adds the DBTimeout option for channeldb. A new unit
test file is created to test the default options. In addition,
the params used in kvdb.Create inside channeldb_test is updated
with a DefaultDBTimeout value.
* contractcourt+routing: use DBTimeout in kvdb
This commit touches multiple test files in contractcourt and routing.
The call of function kvdb.Create and kvdb.Open are now updated with
the new param DBTimeout, using the default value kvdb.DefaultDBTimeout.
* lncfg: add DBTimeout option in db config
The DBTimeout option is added to db config. A new unit test is
added to check the default DB config is created as expected.
* migration: add DBTimeout param in kvdb.Create/kvdb.Open
* keychain: update tests to use DBTimeout param
* htlcswitch+chainreg: add DBTimeout option
* macaroons: support DBTimeout config in creation
This commit adds the DBTimeout during the creation of macaroons.db.
The usage of kvdb.Create and kvdb.Open in its tests are updated with
a timeout value using kvdb.DefaultDBTimeout.
* walletunlocker: add dbTimeout option in UnlockerService
This commit adds a new param, dbTimeout, during the creation of
UnlockerService. This param is then passed to wallet.NewLoader
inside various service calls, specifying a timeout value to be
used when opening the bbolt. In addition, the macaroonService
is also called with this dbTimeout param.
* watchtower/wtdb: add dbTimeout param during creation
This commit adds the dbTimeout param for the creation of both
watchtower.db and wtclient.db.
* multi: add db timeout param for walletdb.Create
This commit adds the db timeout param for the function call
walletdb.Create. It touches only the test files found in chainntnfs,
lnwallet, and routing.
* lnd: pass DBTimeout config to relevant services
This commit enables lnd to pass the DBTimeout config to the following
services/config/functions,
- chainControlConfig
- walletunlocker
- wallet.NewLoader
- macaroons
- watchtower
In addition, the usage of wallet.Create is updated too.
* sample-config: add dbtimeout option
2020-12-08 02:31:49 +03:00
|
|
|
bdb, err := kvdb.Open(
|
|
|
|
kvdb.BoltBackendName, path,
|
|
|
|
opts.NoFreelistSync, opts.DBTimeout,
|
|
|
|
)
|
2019-10-24 13:04:26 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
chanDB := &DB{
|
2019-12-13 05:22:19 +03:00
|
|
|
Backend: bdb,
|
|
|
|
dbPath: dbPath,
|
|
|
|
now: time.Now,
|
2019-10-24 13:04:26 +03:00
|
|
|
}
|
|
|
|
chanDB.graph = newChannelGraph(
|
|
|
|
chanDB, opts.RejectCacheSize, opts.ChannelCacheSize,
|
|
|
|
)
|
|
|
|
|
|
|
|
return chanDB, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// createChannelDB creates and initializes a fresh version of channeldb. In
|
|
|
|
// the case that the target path has not yet been created or doesn't yet exist,
|
|
|
|
// then the path is created. Additionally, all required top-level buckets used
|
|
|
|
// within the database are created.
|
|
|
|
func createChannelDB(dbPath string) error {
|
|
|
|
if !fileExists(dbPath) {
|
|
|
|
if err := os.MkdirAll(dbPath, 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
path := filepath.Join(dbPath, dbName)
|
kvdb: add timeout options for bbolt (#4787)
* mod: bump btcwallet version to accept db timeout
* btcwallet: add DBTimeOut in config
* kvdb: add database timeout option for bbolt
This commit adds a DBTimeout option in bbolt config. The relevant
functions walletdb.Open/Create are updated to use this config. In
addition, the bolt compacter also applies the new timeout option.
* channeldb: add DBTimeout in db options
This commit adds the DBTimeout option for channeldb. A new unit
test file is created to test the default options. In addition,
the params used in kvdb.Create inside channeldb_test is updated
with a DefaultDBTimeout value.
* contractcourt+routing: use DBTimeout in kvdb
This commit touches multiple test files in contractcourt and routing.
The call of function kvdb.Create and kvdb.Open are now updated with
the new param DBTimeout, using the default value kvdb.DefaultDBTimeout.
* lncfg: add DBTimeout option in db config
The DBTimeout option is added to db config. A new unit test is
added to check the default DB config is created as expected.
* migration: add DBTimeout param in kvdb.Create/kvdb.Open
* keychain: update tests to use DBTimeout param
* htlcswitch+chainreg: add DBTimeout option
* macaroons: support DBTimeout config in creation
This commit adds the DBTimeout during the creation of macaroons.db.
The usage of kvdb.Create and kvdb.Open in its tests are updated with
a timeout value using kvdb.DefaultDBTimeout.
* walletunlocker: add dbTimeout option in UnlockerService
This commit adds a new param, dbTimeout, during the creation of
UnlockerService. This param is then passed to wallet.NewLoader
inside various service calls, specifying a timeout value to be
used when opening the bbolt. In addition, the macaroonService
is also called with this dbTimeout param.
* watchtower/wtdb: add dbTimeout param during creation
This commit adds the dbTimeout param for the creation of both
watchtower.db and wtclient.db.
* multi: add db timeout param for walletdb.Create
This commit adds the db timeout param for the function call
walletdb.Create. It touches only the test files found in chainntnfs,
lnwallet, and routing.
* lnd: pass DBTimeout config to relevant services
This commit enables lnd to pass the DBTimeout config to the following
services/config/functions,
- chainControlConfig
- walletunlocker
- wallet.NewLoader
- macaroons
- watchtower
In addition, the usage of wallet.Create is updated too.
* sample-config: add dbtimeout option
2020-12-08 02:31:49 +03:00
|
|
|
bdb, err := kvdb.Create(
|
|
|
|
kvdb.BoltBackendName, path, false, kvdb.DefaultDBTimeout,
|
|
|
|
)
|
2019-10-24 13:04:26 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-13 05:22:19 +03:00
|
|
|
err = kvdb.Update(bdb, func(tx kvdb.RwTx) error {
|
|
|
|
if _, err := tx.CreateTopLevelBucket(openChannelBucket); err != nil {
|
2019-10-24 13:04:26 +03:00
|
|
|
return err
|
|
|
|
}
|
2019-12-13 05:22:19 +03:00
|
|
|
if _, err := tx.CreateTopLevelBucket(closedChannelBucket); err != nil {
|
2019-10-24 13:04:26 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-13 05:22:19 +03:00
|
|
|
if _, err := tx.CreateTopLevelBucket(invoiceBucket); err != nil {
|
2019-10-24 13:04:26 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-13 05:22:19 +03:00
|
|
|
if _, err := tx.CreateTopLevelBucket(paymentBucket); err != nil {
|
2019-10-24 13:04:26 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-13 05:22:19 +03:00
|
|
|
nodes, err := tx.CreateTopLevelBucket(nodeBucket)
|
2019-10-24 13:04:26 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = nodes.CreateBucket(aliasIndexBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = nodes.CreateBucket(nodeUpdateIndexBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-13 05:22:19 +03:00
|
|
|
edges, err := tx.CreateTopLevelBucket(edgeBucket)
|
2019-10-24 13:04:26 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := edges.CreateBucket(edgeIndexBucket); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := edges.CreateBucket(edgeUpdateIndexBucket); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := edges.CreateBucket(channelPointBucket); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := edges.CreateBucket(zombieBucket); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-13 05:22:19 +03:00
|
|
|
graphMeta, err := tx.CreateTopLevelBucket(graphMetaBucket)
|
2019-10-24 13:04:26 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = graphMeta.CreateBucket(pruneLogBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-13 05:22:19 +03:00
|
|
|
if _, err := tx.CreateTopLevelBucket(metaBucket); err != nil {
|
2019-10-24 13:04:26 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
meta := &Meta{
|
2019-10-24 13:25:28 +03:00
|
|
|
DbVersionNumber: 0,
|
2019-10-24 13:04:26 +03:00
|
|
|
}
|
|
|
|
return putMeta(meta, tx)
|
2020-10-26 16:06:32 +03:00
|
|
|
}, func() {})
|
2019-10-24 13:04:26 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create new channeldb")
|
|
|
|
}
|
|
|
|
|
|
|
|
return bdb.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// fileExists returns true if the file exists, and false otherwise.
|
|
|
|
func fileExists(path string) bool {
|
|
|
|
if _, err := os.Stat(path); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// FetchClosedChannels attempts to fetch all closed channels from the database.
|
|
|
|
// The pendingOnly bool toggles if channels that aren't yet fully closed should
|
|
|
|
// be returned in the response or not. When a channel was cooperatively closed,
|
|
|
|
// it becomes fully closed after a single confirmation. When a channel was
|
|
|
|
// forcibly closed, it will become fully closed after _all_ the pending funds
|
|
|
|
// (if any) have been swept.
|
|
|
|
func (d *DB) FetchClosedChannels(pendingOnly bool) ([]*ChannelCloseSummary, error) {
|
|
|
|
var chanSummaries []*ChannelCloseSummary
|
|
|
|
|
2020-05-07 01:45:50 +03:00
|
|
|
if err := kvdb.View(d, func(tx kvdb.RTx) error {
|
2019-12-13 05:22:19 +03:00
|
|
|
closeBucket := tx.ReadBucket(closedChannelBucket)
|
2019-10-24 13:04:26 +03:00
|
|
|
if closeBucket == nil {
|
|
|
|
return ErrNoClosedChannels
|
|
|
|
}
|
|
|
|
|
|
|
|
return closeBucket.ForEach(func(chanID []byte, summaryBytes []byte) error {
|
|
|
|
summaryReader := bytes.NewReader(summaryBytes)
|
|
|
|
chanSummary, err := deserializeCloseChannelSummary(summaryReader)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the query specified to only include pending
|
|
|
|
// channels, then we'll skip any channels which aren't
|
|
|
|
// currently pending.
|
|
|
|
if !chanSummary.IsPending && pendingOnly {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
chanSummaries = append(chanSummaries, chanSummary)
|
|
|
|
return nil
|
|
|
|
})
|
2020-10-20 17:18:40 +03:00
|
|
|
}, func() {
|
|
|
|
chanSummaries = nil
|
2019-10-24 13:04:26 +03:00
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return chanSummaries, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChannelGraph returns a new instance of the directed channel graph.
|
|
|
|
func (d *DB) ChannelGraph() *ChannelGraph {
|
|
|
|
return d.graph
|
|
|
|
}
|