lnwallet: switch over to using btcwallet's new Loader type

Allows us to remove a lot of duplicated code pertaining to wallet
setup. We also gain hooks into the wallet to trigger callbacks once the
wallet is created/opened.
This commit is contained in:
Olaoluwa Osuntokun 2016-02-03 12:00:28 -08:00
parent 92c14c99c3
commit b3cdc6167f

@ -7,8 +7,6 @@ import (
"fmt" "fmt"
"log" "log"
"math" "math"
"os"
"path/filepath"
"sync" "sync"
"sync/atomic" "sync/atomic"
@ -250,7 +248,6 @@ type LightningWallet struct {
func NewLightningWallet(config *Config) (*LightningWallet, walletdb.DB, error) { func NewLightningWallet(config *Config) (*LightningWallet, walletdb.DB, error) {
// Ensure the wallet exists or create it when the create flag is set. // Ensure the wallet exists or create it when the create flag is set.
netDir := networkDir(config.DataDir, config.NetParams) netDir := networkDir(config.DataDir, config.NetParams)
dbPath := filepath.Join(netDir, walletDbName)
var pubPass []byte var pubPass []byte
if config.PublicPass == nil { if config.PublicPass == nil {
@ -259,36 +256,41 @@ func NewLightningWallet(config *Config) (*LightningWallet, walletdb.DB, error) {
pubPass = config.PublicPass pubPass = config.PublicPass
} }
// Wallet has never been created, perform initial set up. var walletDB walletdb.DB
var createID bool loader := btcwallet.NewLoader(config.NetParams, netDir)
if !fileExists(dbPath) { loader.RunAfterLoad(func(w *btcwallet.Wallet, db walletdb.DB) {
// Ensure the data directory for the network exists. walletDB = db
if err := checkCreateDir(netDir); err != nil { })
fmt.Fprintln(os.Stderr, err)
return nil, nil, err
}
// Attempt to create a new wallet walletExists, err := loader.WalletExists()
if err := createWallet(config.PrivatePass, pubPass, if err != nil {
config.HdSeed, dbPath, config.NetParams); err != nil { return nil, nil, err
fmt.Fprintln(os.Stderr, err) }
var createID bool
var wallet *btcwallet.Wallet
if !walletExists {
// Wallet has never been created, perform initial set up.
wallet, err = loader.CreateNewWallet(pubPass, config.PrivatePass,
config.HdSeed)
if err != nil {
return nil, nil, err return nil, nil, err
} }
createID = true createID = true
} } else {
// Wallet has been created and been initialized at this point, open it
// Wallet has been created and been initialized at this point, open it // along with all the required DB namepsaces, and the DB itself.
// along with all the required DB namepsaces, and the DB itself. wallet, err = loader.OpenExistingWallet(pubPass, false)
wallet, db, err := openWallet(pubPass, netDir, config.NetParams) if err != nil {
if err != nil { return nil, nil, err
return nil, nil, err }
} }
// Create a special namespace for our unique payment channel related // Create a special namespace for our unique payment channel related
// meta-data. Subsequently initializing the channeldb around the // meta-data. Subsequently initializing the channeldb around the
// created namespace. // created namespace.
lnNamespace, err := db.Namespace(lightningNamespaceKey) lnNamespace, err := walletDB.Namespace(lightningNamespaceKey)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@ -320,7 +322,7 @@ func NewLightningWallet(config *Config) (*LightningWallet, walletdb.DB, error) {
// TODO(roasbeef): logging // TODO(roasbeef): logging
return &LightningWallet{ return &LightningWallet{
db: db, db: walletDB,
chainNotifier: chainNotifier, chainNotifier: chainNotifier,
Wallet: wallet, Wallet: wallet,
ChannelDB: cdb, ChannelDB: cdb,
@ -332,7 +334,7 @@ func NewLightningWallet(config *Config) (*LightningWallet, walletdb.DB, error) {
cfg: config, cfg: config,
fundingLimbo: make(map[uint64]*ChannelReservation), fundingLimbo: make(map[uint64]*ChannelReservation),
quit: make(chan struct{}), quit: make(chan struct{}),
}, db, nil }, walletDB, nil
} }
// Startup establishes a connection to the RPC source, and spins up all // Startup establishes a connection to the RPC source, and spins up all