rpcserver: express fee rates using types

This commit is contained in:
Johan T. Halseth
2018-02-13 15:13:01 +01:00
parent b9d1eceda3
commit ba3f3e1942

View File

@@ -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 // 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. // address to a specified output value to be sent to that address.
func (r *rpcServer) sendCoinsOnChain(paymentMap map[string]int64, func (r *rpcServer) sendCoinsOnChain(paymentMap map[string]int64,
feePerByte btcutil.Amount) (*chainhash.Hash, error) { feeRate lnwallet.SatPerVByte) (*chainhash.Hash, error) {
outputs, err := addrPairsToOutputs(paymentMap) outputs, err := addrPairsToOutputs(paymentMap)
if err != nil { if err != nil {
return nil, err 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. // 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 // A value is chosen based on the two free parameters as one, or both of them
// can be zero. // can be zero.
func determineFeePerByte(feeEstimator lnwallet.FeeEstimator, targetConf int32, func determineFeePerVSize(feeEstimator lnwallet.FeeEstimator, targetConf int32,
satPerByte int64) (btcutil.Amount, error) { feePerByte int64) (lnwallet.SatPerVByte, error) {
switch { switch {
// If the target number of confirmations is set, then we'll use that to // If the target number of confirmations is set, then we'll use that to
// consult our fee estimator for an adequate fee. // consult our fee estimator for an adequate fee.
case targetConf != 0: case targetConf != 0:
satPerByte, err := feeEstimator.EstimateFeePerByte( feePerVSize, err := feeEstimator.EstimateFeePerVSize(
uint32(targetConf), uint32(targetConf),
) )
if err != nil { if err != nil {
@@ -373,22 +373,22 @@ func determineFeePerByte(feeEstimator lnwallet.FeeEstimator, targetConf int32,
"estimator: %v", err) "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. // If a manual sat/byte fee rate is set, then we'll use that directly.
case satPerByte != 0: case feePerByte != 0:
return btcutil.Amount(satPerByte), nil return lnwallet.SatPerVByte(feePerByte), nil
// Otherwise, we'll attempt a relaxed confirmation target for the // Otherwise, we'll attempt a relaxed confirmation target for the
// transaction // transaction
default: default:
satPerByte, err := feeEstimator.EstimateFeePerByte(6) feePerVSize, err := feeEstimator.EstimateFeePerVSize(6)
if err != nil { if err != nil {
return 0, fmt.Errorf("unable to query fee "+ return 0, fmt.Errorf("unable to query fee "+
"estimator: %v", err) "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 // Based on the passed fee related parameters, we'll determine an
// appropriate fee rate for this transaction. // appropriate fee rate for this transaction.
feePerByte, err := determineFeePerByte( feeRate, err := determineFeePerVSize(
r.server.cc.feeEstimator, in.TargetConf, in.SatPerByte, r.server.cc.feeEstimator, in.TargetConf, in.SatPerByte,
) )
if err != nil { if err != nil {
return nil, err return nil, err
} }
rpcsLog.Infof("[sendcoins] addr=%v, amt=%v, sat/byte=%v", rpcsLog.Infof("[sendcoins] addr=%v, amt=%v, sat/vbyte=%v",
in.Addr, btcutil.Amount(in.Amount), int64(feePerByte)) in.Addr, btcutil.Amount(in.Amount), int64(feeRate))
paymentMap := map[string]int64{in.Addr: in.Amount} paymentMap := map[string]int64{in.Addr: in.Amount}
txid, err := r.sendCoinsOnChain(paymentMap, feePerByte) txid, err := r.sendCoinsOnChain(paymentMap, feeRate)
if err != nil { if err != nil {
return nil, err 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 // Based on the passed fee related parameters, we'll determine an
// approriate fee rate for this transaction. // approriate fee rate for this transaction.
feePerByte, err := determineFeePerByte( feeRate, err := determineFeePerVSize(
r.server.cc.feeEstimator, in.TargetConf, in.SatPerByte, r.server.cc.feeEstimator, in.TargetConf, in.SatPerByte,
) )
if err != nil { if err != nil {
return nil, err return nil, err
} }
rpcsLog.Infof("[sendmany] outputs=%v, sat/byte=%v", rpcsLog.Infof("[sendmany] outputs=%v, sat/vbyte=%v",
spew.Sdump(in.AddrToAmount), int64(feePerByte)) spew.Sdump(in.AddrToAmount), int64(feeRate))
txid, err := r.sendCoinsOnChain(in.AddrToAmount, feePerByte) txid, err := r.sendCoinsOnChain(in.AddrToAmount, feeRate)
if err != nil { if err != nil {
return nil, err 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 // Based on the passed fee related parameters, we'll determine an
// appropriate fee rate for the funding transaction. // appropriate fee rate for the funding transaction.
feePerByte, err := determineFeePerByte( feeRate, err := determineFeePerVSize(
r.server.cc.feeEstimator, in.TargetConf, in.SatPerByte, r.server.cc.feeEstimator, in.TargetConf, in.SatPerByte,
) )
if err != nil { if err != nil {
return err return err
} }
rpcsLog.Debugf("[openchannel]: using fee of %v sat/byte for funding "+ rpcsLog.Debugf("[openchannel]: using fee of %v sat/vbyte for funding "+
"tx", int64(feePerByte)) "tx", int64(feeRate))
// Instruct the server to trigger the necessary events to attempt to // Instruct the server to trigger the necessary events to attempt to
// open a new channel. A stream is returned in place, this stream will // 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( updateChan, errChan := r.server.OpenChannel(
nodePubKey, localFundingAmt, nodePubKey, localFundingAmt,
lnwire.NewMSatFromSatoshis(remoteInitialBalance), lnwire.NewMSatFromSatoshis(remoteInitialBalance),
minHtlc, feePerByte, in.Private, minHtlc, feeRate, in.Private,
) )
var outpoint wire.OutPoint 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 // Based on the passed fee related parameters, we'll determine an
// appropriate fee rate for the funding transaction. // appropriate fee rate for the funding transaction.
feePerByte, err := determineFeePerByte( feeRate, err := determineFeePerVSize(
r.server.cc.feeEstimator, in.TargetConf, in.SatPerByte, r.server.cc.feeEstimator, in.TargetConf, in.SatPerByte,
) )
if err != nil { if err != nil {
return nil, err return nil, err
} }
rpcsLog.Tracef("[openchannel] target sat/byte for funding tx: %v", rpcsLog.Tracef("[openchannel] target sat/vbyte for funding tx: %v",
int64(feePerByte)) int64(feeRate))
updateChan, errChan := r.server.OpenChannel( updateChan, errChan := r.server.OpenChannel(
nodepubKey, localFundingAmt, nodepubKey, localFundingAmt,
lnwire.NewMSatFromSatoshis(remoteInitialBalance), lnwire.NewMSatFromSatoshis(remoteInitialBalance),
minHtlc, feePerByte, in.Private, minHtlc, feeRate, in.Private,
) )
select { select {
@@ -1045,24 +1045,20 @@ func (r *rpcServer) CloseChannel(in *lnrpc.CloseChannelRequest,
// Based on the passed fee related parameters, we'll determine // Based on the passed fee related parameters, we'll determine
// an appropriate fee rate for the cooperative closure // an appropriate fee rate for the cooperative closure
// transaction. // transaction.
feePerByte, err := determineFeePerByte( feeRate, err := determineFeePerVSize(
r.server.cc.feeEstimator, in.TargetConf, in.SatPerByte, r.server.cc.feeEstimator, in.TargetConf, in.SatPerByte,
) )
if err != nil { if err != nil {
return err return err
} }
rpcsLog.Debugf("Target sat/byte for closing transaction: %v", rpcsLog.Debugf("Target sat/vbyte for closing transaction: %v",
int64(feePerByte)) int64(feeRate))
// When crating commitment transaction, or closure if feeRate == 0 {
// 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 the fee rate returned isn't usable, then we'll // If the fee rate returned isn't usable, then we'll
// fall back to an lax fee estimate. // 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 { if err != nil {
return err return err
} }
@@ -1072,7 +1068,7 @@ func (r *rpcServer) CloseChannel(in *lnrpc.CloseChannelRequest,
// cooperative channel closure. So we'll forward the request to // cooperative channel closure. So we'll forward the request to
// the htlc switch which will handle the negotiation and // the htlc switch which will handle the negotiation and
// broadcast details. // broadcast details.
feePerKw := feePerWeight * 1000 feePerKw := feeRate.FeePerKWeight()
updateChan, errChan = r.server.htlcSwitch.CloseLink(chanPoint, updateChan, errChan = r.server.htlcSwitch.CloseLink(chanPoint,
htlcswitch.CloseRegular, feePerKw) htlcswitch.CloseRegular, feePerKw)
} }