From fbb3bc184db2aaed2b98227abc9d8897c461a2bf Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 13 Aug 2020 16:43:00 -0700 Subject: [PATCH 1/2] wallet/txauthor: fix bug in dust calculation In this commit, we fix a 3 year old bug in dust calculation. Before this commit, the target fee of the transaction to be crafted would be used to determine dust. If the target fee is very high, then this would cause over all higher fees, as we'd base that dust computation off of that fee rate, rather than the min relay fee. To fix this, we always use the min relay fee at all times when computing dust. --- wallet/txauthor/author.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wallet/txauthor/author.go b/wallet/txauthor/author.go index 18f996c..34449b5 100644 --- a/wallet/txauthor/author.go +++ b/wallet/txauthor/author.go @@ -133,7 +133,7 @@ func NewUnsignedTransaction(outputs []*wire.TxOut, relayFeePerKb btcutil.Amount, changeIndex := -1 changeAmount := inputAmount - targetAmount - maxRequiredFee if changeAmount != 0 && !txrules.IsDustAmount(changeAmount, - txsizes.P2WPKHPkScriptSize, relayFeePerKb) { + txsizes.P2WPKHPkScriptSize, txrules.DefaultRelayFeePerKb) { changeScript, err := fetchChange() if err != nil { return nil, err From cde4b5dca4e43cdacd7def5c207551c1058a3792 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 13 Aug 2020 16:57:32 -0700 Subject: [PATCH 2/2] wallet/txauthor: rename relayFeePerKb to feeRatePerKb The new name more accurately reflects the usage of the value. This is the target fee rate that the constructed transaction should aim to meet. --- wallet/txauthor/author.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/wallet/txauthor/author.go b/wallet/txauthor/author.go index 34449b5..5290c77 100644 --- a/wallet/txauthor/author.go +++ b/wallet/txauthor/author.go @@ -83,12 +83,12 @@ type ChangeSource func() ([]byte, error) // InputSourceError is returned. // // BUGS: Fee estimation may be off when redeeming non-compressed P2PKH outputs. -func NewUnsignedTransaction(outputs []*wire.TxOut, relayFeePerKb btcutil.Amount, +func NewUnsignedTransaction(outputs []*wire.TxOut, feeRatePerKb btcutil.Amount, fetchInputs InputSource, fetchChange ChangeSource) (*AuthoredTx, error) { targetAmount := SumOutputValues(outputs) estimatedSize := txsizes.EstimateVirtualSize(0, 1, 0, outputs, true) - targetFee := txrules.FeeForSerializeSize(relayFeePerKb, estimatedSize) + targetFee := txrules.FeeForSerializeSize(feeRatePerKb, estimatedSize) for { inputAmount, inputs, inputValues, scripts, err := fetchInputs(targetAmount + targetFee) @@ -117,7 +117,7 @@ func NewUnsignedTransaction(outputs []*wire.TxOut, relayFeePerKb btcutil.Amount, maxSignedSize := txsizes.EstimateVirtualSize(p2pkh, p2wpkh, nested, outputs, true) - maxRequiredFee := txrules.FeeForSerializeSize(relayFeePerKb, maxSignedSize) + maxRequiredFee := txrules.FeeForSerializeSize(feeRatePerKb, maxSignedSize) remainingAmount := inputAmount - targetAmount if remainingAmount < maxRequiredFee { targetFee = maxRequiredFee