server+utxonursery: generate pkscript closures

This commit moves the newSweepPkScript function
previously in the nursery to be a helper function
within the server. Additionally, the function now
returns a closure that satisfies the configuration
interfaces of several other subsystems.

As a result, the configuration sites contain much
less boilerplate, as it's now encapsulated in
the newSweepPkScriptGen helper.
This commit is contained in:
Conner Fromknecht 2019-06-13 17:30:22 -07:00
parent efcdefee39
commit 7c0a03c7c8
No known key found for this signature in database
GPG Key ID: E7D737B67FA592C7
2 changed files with 28 additions and 32 deletions

@ -20,6 +20,7 @@ import (
"github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/connmgr" "github.com/btcsuite/btcd/connmgr"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
"github.com/coreos/bbolt" "github.com/coreos/bbolt"
@ -730,9 +731,7 @@ func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB,
s.sweeper = sweep.New(&sweep.UtxoSweeperConfig{ s.sweeper = sweep.New(&sweep.UtxoSweeperConfig{
FeeEstimator: cc.feeEstimator, FeeEstimator: cc.feeEstimator,
GenSweepScript: func() ([]byte, error) { GenSweepScript: newSweepPkScriptGen(cc.wallet),
return newSweepPkScript(cc.wallet)
},
Signer: cc.wallet.Cfg.Signer, Signer: cc.wallet.Cfg.Signer,
PublishTransaction: cc.wallet.PublishTransaction, PublishTransaction: cc.wallet.PublishTransaction,
NewBatchTimer: func() <-chan time.Time { NewBatchTimer: func() <-chan time.Time {
@ -775,9 +774,7 @@ func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB,
ChainHash: *activeNetParams.GenesisHash, ChainHash: *activeNetParams.GenesisHash,
IncomingBroadcastDelta: DefaultIncomingBroadcastDelta, IncomingBroadcastDelta: DefaultIncomingBroadcastDelta,
OutgoingBroadcastDelta: DefaultOutgoingBroadcastDelta, OutgoingBroadcastDelta: DefaultOutgoingBroadcastDelta,
NewSweepAddr: func() ([]byte, error) { NewSweepAddr: newSweepPkScriptGen(cc.wallet),
return newSweepPkScript(cc.wallet)
},
PublishTx: cc.wallet.PublishTransaction, PublishTx: cc.wallet.PublishTransaction,
DeliverResolutionMsg: func(msgs ...contractcourt.ResolutionMsg) error { DeliverResolutionMsg: func(msgs ...contractcourt.ResolutionMsg) error {
for _, msg := range msgs { for _, msg := range msgs {
@ -854,9 +851,7 @@ func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB,
CloseLink: closeLink, CloseLink: closeLink,
DB: chanDB, DB: chanDB,
Estimator: s.cc.feeEstimator, Estimator: s.cc.feeEstimator,
GenSweepScript: func() ([]byte, error) { GenSweepScript: newSweepPkScriptGen(cc.wallet),
return newSweepPkScript(cc.wallet)
},
Notifier: cc.chainNotifier, Notifier: cc.chainNotifier,
PublishTransaction: cc.wallet.PublishTransaction, PublishTransaction: cc.wallet.PublishTransaction,
ContractBreaches: contractBreaches, ContractBreaches: contractBreaches,
@ -1076,9 +1071,7 @@ func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB,
s.towerClient, err = wtclient.New(&wtclient.Config{ s.towerClient, err = wtclient.New(&wtclient.Config{
Signer: cc.wallet.Cfg.Signer, Signer: cc.wallet.Cfg.Signer,
NewAddress: func() ([]byte, error) { NewAddress: newSweepPkScriptGen(cc.wallet),
return newSweepPkScript(cc.wallet)
},
SecretKeyRing: s.cc.keyRing, SecretKeyRing: s.cc.keyRing,
Dial: cfg.net.Dial, Dial: cfg.net.Dial,
AuthDial: wtclient.AuthDial, AuthDial: wtclient.AuthDial,
@ -3233,3 +3226,20 @@ func (s *server) applyChannelUpdate(update *lnwire.ChannelUpdate) error {
return ErrServerShuttingDown return ErrServerShuttingDown
} }
} }
// newSweepPkScriptGen creates closure that generates a new public key script
// which should be used to sweep any funds into the on-chain wallet.
// Specifically, the script generated is a version 0, pay-to-witness-pubkey-hash
// (p2wkh) output.
func newSweepPkScriptGen(
wallet lnwallet.WalletController) func() ([]byte, error) {
return func() ([]byte, error) {
sweepAddr, err := wallet.NewAddress(lnwallet.WitnessPubKey, false)
if err != nil {
return nil, err
}
return txscript.PayToAddrScript(sweepAddr)
}
}

@ -8,7 +8,6 @@ import (
"sync" "sync"
"sync/atomic" "sync/atomic"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
@ -1231,19 +1230,6 @@ func (u *utxoNursery) closeAndRemoveIfMature(chanPoint *wire.OutPoint) error {
return nil return nil
} }
// newSweepPkScript creates a new public key script which should be used to
// sweep any time-locked, or contested channel funds into the wallet.
// Specifically, the script generated is a version 0, pay-to-witness-pubkey-hash
// (p2wkh) output.
func newSweepPkScript(wallet lnwallet.WalletController) ([]byte, error) {
sweepAddr, err := wallet.NewAddress(lnwallet.WitnessPubKey, false)
if err != nil {
return nil, err
}
return txscript.PayToAddrScript(sweepAddr)
}
// babyOutput represents a two-stage CSV locked output, and is used to track // babyOutput represents a two-stage CSV locked output, and is used to track
// htlc outputs through incubation. The first stage requires broadcasting a // htlc outputs through incubation. The first stage requires broadcasting a
// presigned timeout txn that spends from the CLTV locked output on the // presigned timeout txn that spends from the CLTV locked output on the