From ba3f3e1942d08e389f917df0721e422d4b252930 Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Tue, 13 Feb 2018 15:13:01 +0100 Subject: [PATCH] rpcserver: express fee rates using types --- rpcserver.go | 70 +++++++++++++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 37 deletions(-) diff --git a/rpcserver.go b/rpcserver.go index 673a5464e..62d48ed3c 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -344,28 +344,28 @@ func addrPairsToOutputs(addrPairs map[string]int64) ([]*wire.TxOut, error) { // more addresses specified in the passed payment map. The payment map maps an // address to a specified output value to be sent to that address. func (r *rpcServer) sendCoinsOnChain(paymentMap map[string]int64, - feePerByte btcutil.Amount) (*chainhash.Hash, error) { + feeRate lnwallet.SatPerVByte) (*chainhash.Hash, error) { outputs, err := addrPairsToOutputs(paymentMap) if err != nil { return nil, err } - return r.server.cc.wallet.SendOutputs(outputs, feePerByte) + return r.server.cc.wallet.SendOutputs(outputs, feeRate) } -// determineFeePerByte will determine the fee in sat/byte that should be paid +// determineFeePerVSize will determine the fee in sat/vbyte that should be paid // given an estimator, a confirmation target, and a manual value for sat/byte. // A value is chosen based on the two free parameters as one, or both of them // can be zero. -func determineFeePerByte(feeEstimator lnwallet.FeeEstimator, targetConf int32, - satPerByte int64) (btcutil.Amount, error) { +func determineFeePerVSize(feeEstimator lnwallet.FeeEstimator, targetConf int32, + feePerByte int64) (lnwallet.SatPerVByte, error) { switch { // If the target number of confirmations is set, then we'll use that to // consult our fee estimator for an adequate fee. case targetConf != 0: - satPerByte, err := feeEstimator.EstimateFeePerByte( + feePerVSize, err := feeEstimator.EstimateFeePerVSize( uint32(targetConf), ) if err != nil { @@ -373,22 +373,22 @@ func determineFeePerByte(feeEstimator lnwallet.FeeEstimator, targetConf int32, "estimator: %v", err) } - return btcutil.Amount(satPerByte), nil + return feePerVSize, nil // If a manual sat/byte fee rate is set, then we'll use that directly. - case satPerByte != 0: - return btcutil.Amount(satPerByte), nil + case feePerByte != 0: + return lnwallet.SatPerVByte(feePerByte), nil // Otherwise, we'll attempt a relaxed confirmation target for the // transaction default: - satPerByte, err := feeEstimator.EstimateFeePerByte(6) + feePerVSize, err := feeEstimator.EstimateFeePerVSize(6) if err != nil { return 0, fmt.Errorf("unable to query fee "+ "estimator: %v", err) } - return satPerByte, nil + return feePerVSize, nil } } @@ -399,18 +399,18 @@ func (r *rpcServer) SendCoins(ctx context.Context, // Based on the passed fee related parameters, we'll determine an // appropriate fee rate for this transaction. - feePerByte, err := determineFeePerByte( + feeRate, err := determineFeePerVSize( r.server.cc.feeEstimator, in.TargetConf, in.SatPerByte, ) if err != nil { return nil, err } - rpcsLog.Infof("[sendcoins] addr=%v, amt=%v, sat/byte=%v", - in.Addr, btcutil.Amount(in.Amount), int64(feePerByte)) + rpcsLog.Infof("[sendcoins] addr=%v, amt=%v, sat/vbyte=%v", + in.Addr, btcutil.Amount(in.Amount), int64(feeRate)) paymentMap := map[string]int64{in.Addr: in.Amount} - txid, err := r.sendCoinsOnChain(paymentMap, feePerByte) + txid, err := r.sendCoinsOnChain(paymentMap, feeRate) if err != nil { return nil, err } @@ -427,17 +427,17 @@ func (r *rpcServer) SendMany(ctx context.Context, // Based on the passed fee related parameters, we'll determine an // approriate fee rate for this transaction. - feePerByte, err := determineFeePerByte( + feeRate, err := determineFeePerVSize( r.server.cc.feeEstimator, in.TargetConf, in.SatPerByte, ) if err != nil { return nil, err } - rpcsLog.Infof("[sendmany] outputs=%v, sat/byte=%v", - spew.Sdump(in.AddrToAmount), int64(feePerByte)) + rpcsLog.Infof("[sendmany] outputs=%v, sat/vbyte=%v", + spew.Sdump(in.AddrToAmount), int64(feeRate)) - txid, err := r.sendCoinsOnChain(in.AddrToAmount, feePerByte) + txid, err := r.sendCoinsOnChain(in.AddrToAmount, feeRate) if err != nil { return nil, err } @@ -742,15 +742,15 @@ func (r *rpcServer) OpenChannel(in *lnrpc.OpenChannelRequest, // Based on the passed fee related parameters, we'll determine an // appropriate fee rate for the funding transaction. - feePerByte, err := determineFeePerByte( + feeRate, err := determineFeePerVSize( r.server.cc.feeEstimator, in.TargetConf, in.SatPerByte, ) if err != nil { return err } - rpcsLog.Debugf("[openchannel]: using fee of %v sat/byte for funding "+ - "tx", int64(feePerByte)) + rpcsLog.Debugf("[openchannel]: using fee of %v sat/vbyte for funding "+ + "tx", int64(feeRate)) // Instruct the server to trigger the necessary events to attempt to // open a new channel. A stream is returned in place, this stream will @@ -758,7 +758,7 @@ func (r *rpcServer) OpenChannel(in *lnrpc.OpenChannelRequest, updateChan, errChan := r.server.OpenChannel( nodePubKey, localFundingAmt, lnwire.NewMSatFromSatoshis(remoteInitialBalance), - minHtlc, feePerByte, in.Private, + minHtlc, feeRate, in.Private, ) var outpoint wire.OutPoint @@ -864,20 +864,20 @@ func (r *rpcServer) OpenChannelSync(ctx context.Context, // Based on the passed fee related parameters, we'll determine an // appropriate fee rate for the funding transaction. - feePerByte, err := determineFeePerByte( + feeRate, err := determineFeePerVSize( r.server.cc.feeEstimator, in.TargetConf, in.SatPerByte, ) if err != nil { return nil, err } - rpcsLog.Tracef("[openchannel] target sat/byte for funding tx: %v", - int64(feePerByte)) + rpcsLog.Tracef("[openchannel] target sat/vbyte for funding tx: %v", + int64(feeRate)) updateChan, errChan := r.server.OpenChannel( nodepubKey, localFundingAmt, lnwire.NewMSatFromSatoshis(remoteInitialBalance), - minHtlc, feePerByte, in.Private, + minHtlc, feeRate, in.Private, ) select { @@ -1045,24 +1045,20 @@ func (r *rpcServer) CloseChannel(in *lnrpc.CloseChannelRequest, // Based on the passed fee related parameters, we'll determine // an appropriate fee rate for the cooperative closure // transaction. - feePerByte, err := determineFeePerByte( + feeRate, err := determineFeePerVSize( r.server.cc.feeEstimator, in.TargetConf, in.SatPerByte, ) if err != nil { return err } - rpcsLog.Debugf("Target sat/byte for closing transaction: %v", - int64(feePerByte)) + rpcsLog.Debugf("Target sat/vbyte for closing transaction: %v", + int64(feeRate)) - // When crating commitment transaction, or closure - // transactions, we typically deal in fees per-kw, so we'll - // convert now before passing the close request to the switch. - feePerWeight := (feePerByte / blockchain.WitnessScaleFactor) - if feePerWeight == 0 { + if feeRate == 0 { // If the fee rate returned isn't usable, then we'll // fall back to an lax fee estimate. - feePerWeight, err = r.server.cc.feeEstimator.EstimateFeePerWeight(6) + feeRate, err = r.server.cc.feeEstimator.EstimateFeePerVSize(6) if err != nil { return err } @@ -1072,7 +1068,7 @@ func (r *rpcServer) CloseChannel(in *lnrpc.CloseChannelRequest, // cooperative channel closure. So we'll forward the request to // the htlc switch which will handle the negotiation and // broadcast details. - feePerKw := feePerWeight * 1000 + feePerKw := feeRate.FeePerKWeight() updateChan, errChan = r.server.htlcSwitch.CloseLink(chanPoint, htlcswitch.CloseRegular, feePerKw) }