From a3cd24840410db032acc7be3a29a0741255075a5 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 23 Nov 2017 13:38:08 -0600 Subject: [PATCH] funding+utxonursery+breacharbiter: use new FeeEstimator API --- fundingmanager.go | 28 +++++++++++++++------------- mock.go | 4 +++- pilot.go | 7 ++++++- utxonursery.go | 13 ++++++++----- 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/fundingmanager.go b/fundingmanager.go index 0e44e24f..30646ed4 100644 --- a/fundingmanager.go +++ b/fundingmanager.go @@ -35,7 +35,7 @@ const ( // maxFundingAmount is a soft-limit of the maximum channel size // accepted within the Lightning Protocol Currently. This limit is // currently defined in BOLT-0002, and serves as an initial - // precaturioary limit while implementations are battle tested in the + // precautionary limit while implementations are battle tested in the // real world. // // TODO(roasbeef): add command line param to modify @@ -2017,23 +2017,25 @@ func (f *fundingManager) handleInitFundingMsg(msg *initFundingMsg) { ourDustLimit) // First, we'll query the fee estimator for a fee that should get the - // commitment transaction into the next block (conf target of 1). We - // target the next block here to ensure that we'll be able to execute a - // timely unilateral channel closure if needed. - // - // TODO(roasbeef): shouldn't be targeting next block - feePerWeight := btcutil.Amount(f.cfg.FeeEstimator.EstimateFeePerWeight(1)) + // commitment transaction confirmed by the next few blocks (conf target + // of 3). We target the near blocks here to ensure that we'll be able + // to execute a timely unilateral channel closure if needed. + feePerWeight, err := f.cfg.FeeEstimator.EstimateFeePerWeight(3) + if err != nil { + msg.err <- err + return + } // The protocol currently operates on the basis of fee-per-kw, so we'll // multiply the computed sat/weight by 1000 to arrive at fee-per-kw. - feePerKw := feePerWeight * 1000 + commitFeePerKw := feePerWeight * 1000 // Initialize a funding reservation with the local wallet. If the // wallet doesn't have enough funds to commit to this channel, then the // request will fail, and be aborted. reservation, err := f.cfg.Wallet.InitChannelReservation(capacity, - localAmt, msg.pushAmt, feePerKw, peerKey, - msg.peerAddress.Address, &msg.chainHash) + localAmt, msg.pushAmt, commitFeePerKw, msg.fundingFeePerWeight, + peerKey, msg.peerAddress.Address, &msg.chainHash) if err != nil { msg.err <- err return @@ -2043,8 +2045,8 @@ func (f *fundingManager) handleInitFundingMsg(msg *initFundingMsg) { // reservation throughout its lifetime. chanID := f.nextPendingChanID() - fndgLog.Infof("Target sat/kw for pendingID(%x): %v", chanID, - int64(feePerKw)) + fndgLog.Infof("Target commit tx sat/kw for pendingID(%x): %v", chanID, + int64(commitFeePerKw)) // If a pending channel map for this peer isn't already created, then // we create one, ultimately allowing us to track this pending @@ -2089,7 +2091,7 @@ func (f *fundingManager) handleInitFundingMsg(msg *initFundingMsg) { MaxValueInFlight: maxValue, ChannelReserve: chanReserve, HtlcMinimum: ourContribution.MinHTLC, - FeePerKiloWeight: uint32(feePerKw), + FeePerKiloWeight: uint32(commitFeePerKw), CsvDelay: uint16(remoteCsvDelay), MaxAcceptedHTLCs: maxHtlcs, FundingKey: ourContribution.MultiSigKey, diff --git a/mock.go b/mock.go index daee7029..4b437fe7 100644 --- a/mock.go +++ b/mock.go @@ -141,7 +141,9 @@ func (m *mockWalletController) NewRawKey() (*btcec.PublicKey, error) { func (m *mockWalletController) FetchRootKey() (*btcec.PrivateKey, error) { return m.rootKey, nil } -func (*mockWalletController) SendOutputs(outputs []*wire.TxOut) (*chainhash.Hash, error) { +func (*mockWalletController) SendOutputs(outputs []*wire.TxOut, + _ btcutil.Amount) (*chainhash.Hash, error) { + return nil, nil } diff --git a/pilot.go b/pilot.go index 1a678789..044da91b 100644 --- a/pilot.go +++ b/pilot.go @@ -85,7 +85,12 @@ func (c *chanController) OpenChannel(target *btcec.PublicKey, // With the connection established, we'll now establish our connection // to the target peer, waiting for the first update before we exit. - updateStream, errChan := c.server.OpenChannel(-1, target, amt, 0) + feePerWeight, err := c.server.cc.feeEstimator.EstimateFeePerWeight(3) + if err != nil { + return err + } + updateStream, errChan := c.server.OpenChannel(-1, target, amt, 0, + feePerWeight) select { case err := <-errChan: diff --git a/utxonursery.go b/utxonursery.go index 0051756f..e9321781 100644 --- a/utxonursery.go +++ b/utxonursery.go @@ -877,15 +877,18 @@ func (u *utxoNursery) sweepCsvSpendableOutputsTxn(txWeight uint64, } // Using the txn weight estimate, compute the required txn fee. - feePerWeight := u.cfg.Estimator.EstimateFeePerWeight(1) - txFee := btcutil.Amount(txWeight * feePerWeight) + feePerWeight, err := u.cfg.Estimator.EstimateFeePerWeight(6) + if err != nil { + return nil, err + } + txFee := btcutil.Amount(txWeight) * feePerWeight // Sweep as much possible, after subtracting txn fees. sweepAmt := int64(totalSum - txFee) - // Create the sweep transaction that we will be building. We use version - // 2 as it is required for CSV. The txn will sweep the amount after fees - // to the pkscript generated above. + // Create the sweep transaction that we will be building. We use + // version 2 as it is required for CSV. The txn will sweep the amount + // after fees to the pkscript generated above. sweepTx := wire.NewMsgTx(2) sweepTx.AddTxOut(&wire.TxOut{ PkScript: pkScript,