From 7c0a03c7c87e0bab1705177c117b78e72cc101cf Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Thu, 13 Jun 2019 17:30:22 -0700 Subject: [PATCH] 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. --- server.go | 46 ++++++++++++++++++++++++++++------------------ utxonursery.go | 14 -------------- 2 files changed, 28 insertions(+), 32 deletions(-) diff --git a/server.go b/server.go index ee6998ae..73a0ef6e 100644 --- a/server.go +++ b/server.go @@ -20,6 +20,7 @@ import ( "github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/connmgr" + "github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcutil" "github.com/coreos/bbolt" @@ -729,10 +730,8 @@ func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB, } s.sweeper = sweep.New(&sweep.UtxoSweeperConfig{ - FeeEstimator: cc.feeEstimator, - GenSweepScript: func() ([]byte, error) { - return newSweepPkScript(cc.wallet) - }, + FeeEstimator: cc.feeEstimator, + GenSweepScript: newSweepPkScriptGen(cc.wallet), Signer: cc.wallet.Cfg.Signer, PublishTransaction: cc.wallet.PublishTransaction, NewBatchTimer: func() <-chan time.Time { @@ -775,10 +774,8 @@ func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB, ChainHash: *activeNetParams.GenesisHash, IncomingBroadcastDelta: DefaultIncomingBroadcastDelta, OutgoingBroadcastDelta: DefaultOutgoingBroadcastDelta, - NewSweepAddr: func() ([]byte, error) { - return newSweepPkScript(cc.wallet) - }, - PublishTx: cc.wallet.PublishTransaction, + NewSweepAddr: newSweepPkScriptGen(cc.wallet), + PublishTx: cc.wallet.PublishTransaction, DeliverResolutionMsg: func(msgs ...contractcourt.ResolutionMsg) error { for _, msg := range msgs { err := s.htlcSwitch.ProcessContractResolution(msg) @@ -851,12 +848,10 @@ func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB, }, chanDB) s.breachArbiter = newBreachArbiter(&BreachConfig{ - CloseLink: closeLink, - DB: chanDB, - Estimator: s.cc.feeEstimator, - GenSweepScript: func() ([]byte, error) { - return newSweepPkScript(cc.wallet) - }, + CloseLink: closeLink, + DB: chanDB, + Estimator: s.cc.feeEstimator, + GenSweepScript: newSweepPkScriptGen(cc.wallet), Notifier: cc.chainNotifier, PublishTransaction: cc.wallet.PublishTransaction, ContractBreaches: contractBreaches, @@ -1075,10 +1070,8 @@ func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB, } s.towerClient, err = wtclient.New(&wtclient.Config{ - Signer: cc.wallet.Cfg.Signer, - NewAddress: func() ([]byte, error) { - return newSweepPkScript(cc.wallet) - }, + Signer: cc.wallet.Cfg.Signer, + NewAddress: newSweepPkScriptGen(cc.wallet), SecretKeyRing: s.cc.keyRing, Dial: cfg.net.Dial, AuthDial: wtclient.AuthDial, @@ -3233,3 +3226,20 @@ func (s *server) applyChannelUpdate(update *lnwire.ChannelUpdate) error { 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) + } +} diff --git a/utxonursery.go b/utxonursery.go index bfdc075a..0e4501b6 100644 --- a/utxonursery.go +++ b/utxonursery.go @@ -8,7 +8,6 @@ import ( "sync" "sync/atomic" - "github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcutil" "github.com/davecgh/go-spew/spew" @@ -1231,19 +1230,6 @@ func (u *utxoNursery) closeAndRemoveIfMature(chanPoint *wire.OutPoint) error { 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 // htlc outputs through incubation. The first stage requires broadcasting a // presigned timeout txn that spends from the CLTV locked output on the