lnwallet: accept channeldb as a param in constructor
lnwallet is no longer responsible for creating/opening channeldb. The call is now expected to do so.
This commit is contained in:
parent
27fee0e7d3
commit
fe6d71e95e
@ -4,7 +4,6 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"math"
|
"math"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
@ -24,7 +23,6 @@ import (
|
|||||||
"github.com/btcsuite/btcwallet/chain"
|
"github.com/btcsuite/btcwallet/chain"
|
||||||
"github.com/btcsuite/btcwallet/waddrmgr"
|
"github.com/btcsuite/btcwallet/waddrmgr"
|
||||||
btcwallet "github.com/btcsuite/btcwallet/wallet"
|
btcwallet "github.com/btcsuite/btcwallet/wallet"
|
||||||
"github.com/btcsuite/btcwallet/walletdb"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -197,8 +195,7 @@ type LightningWallet struct {
|
|||||||
// A wrapper around a namespace within boltdb reserved for ln-based
|
// A wrapper around a namespace within boltdb reserved for ln-based
|
||||||
// wallet meta-data. See the 'channeldb' package for further
|
// wallet meta-data. See the 'channeldb' package for further
|
||||||
// information.
|
// information.
|
||||||
ChannelDB *channeldb.DB
|
channelDB *channeldb.DB
|
||||||
db walletdb.DB
|
|
||||||
|
|
||||||
// Used by in order to obtain notifications about funding transaction
|
// Used by in order to obtain notifications about funding transaction
|
||||||
// reaching a specified confirmation depth, and to catch
|
// reaching a specified confirmation depth, and to catch
|
||||||
@ -248,7 +245,7 @@ type LightningWallet struct {
|
|||||||
// If the wallet has never been created (according to the passed dataDir), first-time
|
// If the wallet has never been created (according to the passed dataDir), first-time
|
||||||
// setup is executed.
|
// setup is executed.
|
||||||
// TODO(roasbeef): fin...add config
|
// TODO(roasbeef): fin...add config
|
||||||
func NewLightningWallet(config *Config) (*LightningWallet, walletdb.DB, error) {
|
func NewLightningWallet(config *Config, cdb *channeldb.DB) (*LightningWallet, 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)
|
||||||
|
|
||||||
@ -259,15 +256,10 @@ func NewLightningWallet(config *Config) (*LightningWallet, walletdb.DB, error) {
|
|||||||
pubPass = config.PublicPass
|
pubPass = config.PublicPass
|
||||||
}
|
}
|
||||||
|
|
||||||
var walletDB walletdb.DB
|
|
||||||
loader := btcwallet.NewLoader(config.NetParams, netDir)
|
loader := btcwallet.NewLoader(config.NetParams, netDir)
|
||||||
loader.RunAfterLoad(func(w *btcwallet.Wallet, db walletdb.DB) {
|
|
||||||
walletDB = db
|
|
||||||
})
|
|
||||||
|
|
||||||
walletExists, err := loader.WalletExists()
|
walletExists, err := loader.WalletExists()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var createID bool
|
var createID bool
|
||||||
@ -277,7 +269,7 @@ func NewLightningWallet(config *Config) (*LightningWallet, walletdb.DB, error) {
|
|||||||
wallet, err = loader.CreateNewWallet(pubPass, config.PrivatePass,
|
wallet, err = loader.CreateNewWallet(pubPass, config.PrivatePass,
|
||||||
config.HdSeed)
|
config.HdSeed)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
createID = true
|
createID = true
|
||||||
@ -286,21 +278,12 @@ func NewLightningWallet(config *Config) (*LightningWallet, walletdb.DB, error) {
|
|||||||
// 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, err = loader.OpenExistingWallet(pubPass, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a special namespace for our unique payment channel related
|
|
||||||
// meta-data. Subsequently initializing the channeldb around the
|
|
||||||
// created namespace.
|
|
||||||
lnNamespace, err := walletDB.Namespace(lightningNamespaceKey)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
cdb := channeldb.New(wallet.Manager, lnNamespace)
|
|
||||||
|
|
||||||
if err := wallet.Manager.Unlock(config.PrivatePass); err != nil {
|
if err := wallet.Manager.Unlock(config.PrivatePass); err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we just created the wallet, then reserve, and store a key for
|
// If we just created the wallet, then reserve, and store a key for
|
||||||
@ -308,14 +291,14 @@ func NewLightningWallet(config *Config) (*LightningWallet, walletdb.DB, error) {
|
|||||||
if createID {
|
if createID {
|
||||||
adrs, err := wallet.Manager.NextInternalAddresses(waddrmgr.DefaultAccountNum, 1)
|
adrs, err := wallet.Manager.NextInternalAddresses(waddrmgr.DefaultAccountNum, 1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
idPubkeyHash := adrs[0].Address().ScriptAddress()
|
idPubkeyHash := adrs[0].Address().ScriptAddress()
|
||||||
if err := cdb.PutIdKey(idPubkeyHash); err != nil {
|
if err := cdb.PutIdKey(idPubkeyHash); err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
log.Printf("stored identity key pubkey hash in channeldb\n")
|
log.Infof("stored identity key pubkey hash in channeldb")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a special websockets rpc client for btcd which will be used
|
// Create a special websockets rpc client for btcd which will be used
|
||||||
@ -323,7 +306,7 @@ func NewLightningWallet(config *Config) (*LightningWallet, walletdb.DB, error) {
|
|||||||
rpcc, err := chain.NewRPCClient(config.NetParams, config.RpcHost,
|
rpcc, err := chain.NewRPCClient(config.NetParams, config.RpcHost,
|
||||||
config.RpcUser, config.RpcPass, config.CACert, false, 20)
|
config.RpcUser, config.RpcPass, config.CACert, false, 20)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Using the same authentication info, create a config for a second
|
// Using the same authentication info, create a config for a second
|
||||||
@ -341,16 +324,15 @@ func NewLightningWallet(config *Config) (*LightningWallet, walletdb.DB, error) {
|
|||||||
}
|
}
|
||||||
chainNotifier, err := btcdnotify.NewBtcdNotifier(rpcConfig)
|
chainNotifier, err := btcdnotify.NewBtcdNotifier(rpcConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(roasbeef): logging
|
// TODO(roasbeef): logging
|
||||||
return &LightningWallet{
|
return &LightningWallet{
|
||||||
db: walletDB,
|
|
||||||
chainNotifier: chainNotifier,
|
chainNotifier: chainNotifier,
|
||||||
rpc: rpcc,
|
rpc: rpcc,
|
||||||
Wallet: wallet,
|
Wallet: wallet,
|
||||||
ChannelDB: cdb,
|
channelDB: cdb,
|
||||||
msgChan: make(chan interface{}, msgBufferSize),
|
msgChan: make(chan interface{}, msgBufferSize),
|
||||||
// TODO(roasbeef): make this atomic.Uint32 instead? Which is
|
// TODO(roasbeef): make this atomic.Uint32 instead? Which is
|
||||||
// faster, locks or CAS? I'm guessing CAS because assembly:
|
// faster, locks or CAS? I'm guessing CAS because assembly:
|
||||||
@ -359,7 +341,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{}),
|
||||||
}, walletDB, nil
|
}, 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
|
||||||
@ -781,8 +763,7 @@ func (l *LightningWallet) handleContributionMsg(req *addContributionMsg) {
|
|||||||
_, addrs, _, _ := txscript.ExtractPkScriptAddrs(prevOut.PkScript, l.cfg.NetParams)
|
_, addrs, _, _ := txscript.ExtractPkScriptAddrs(prevOut.PkScript, l.cfg.NetParams)
|
||||||
apkh, ok := addrs[0].(*btcutil.AddressPubKeyHash)
|
apkh, ok := addrs[0].(*btcutil.AddressPubKeyHash)
|
||||||
if !ok {
|
if !ok {
|
||||||
req.err <- btcwallet.ErrUnsupportedTransactionType
|
req.err <- fmt.Errorf("only p2pkh wallet outputs are supported")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ai, err := l.Manager.Address(apkh)
|
ai, err := l.Manager.Address(apkh)
|
||||||
@ -998,7 +979,7 @@ func (l *LightningWallet) handleFundingCounterPartySigs(msg *addCounterPartySigs
|
|||||||
|
|
||||||
// Add the complete funding transaction to the DB, in it's open bucket
|
// Add the complete funding transaction to the DB, in it's open bucket
|
||||||
// which will be used for the lifetime of this channel.
|
// which will be used for the lifetime of this channel.
|
||||||
err = l.ChannelDB.PutOpenChannel(pendingReservation.partialState)
|
err = l.channelDB.PutOpenChannel(pendingReservation.partialState)
|
||||||
|
|
||||||
// Create a goroutine to watch the chain so we can open the channel once
|
// Create a goroutine to watch the chain so we can open the channel once
|
||||||
// the funding tx has enough confirmations.
|
// the funding tx has enough confirmations.
|
||||||
@ -1030,7 +1011,7 @@ out:
|
|||||||
|
|
||||||
// Finally, create and officially open the payment channel!
|
// Finally, create and officially open the payment channel!
|
||||||
// TODO(roasbeef): CreationTime once tx is 'open'
|
// TODO(roasbeef): CreationTime once tx is 'open'
|
||||||
channel, _ := newLightningChannel(l, l.chainNotifier, l.ChannelDB,
|
channel, _ := newLightningChannel(l, l.chainNotifier, l.channelDB,
|
||||||
res.partialState)
|
res.partialState)
|
||||||
res.chanOpen <- channel
|
res.chanOpen <- channel
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user