Skip to content

Commit 1212808

Browse files
committed
Merge #16257: [wallet] abort when attempting to fund a transaction above -maxtxfee
806b005 [wallet] abort when attempting to fund a transaction above maxtxfee (Sjors Provoost) Pull request description: `FundTransaction` calls `GetMinimumFee` which, when the fee rate is absurdly high, quietly reduces the fee to `-maxtxfee`. Becaue an absurdly high fee rate is usually the result of a fat finger, aborting seems safer behavior. Before: ``` bitcoin-cli walletcreatefundedpsbt '[]' '[{"tb1q...": 0.01}]' 0 '{"feeRate": 10}' true { "psbt": "cHNidP8...gAA=", "fee": 0.10000000, "changepos": 1 } ``` After: ``` bitcoin-cli walletcreatefundedpsbt '[]' '[{"tb1q...": 0.01}]' 0 '{"feeRate": 10}' true error code: -25 error message: Fee exceeds maximum configured by -maxtxfee ``` QT still checks the max fee rate as expected: <img width="566" alt="Schermafbeelding 2019-06-20 om 19 52 00" src="https://user-images.githubusercontent.com/10217/59888424-a2aa7100-9395-11e9-8ae6-8a3c1f7de585.png"> ACKs for top commit: laanwj: Code review ACK 806b005 Tree-SHA512: bee95811711cdab100b614d2347921407af3b400aea613ca156953ed3f60b924ad29a1d335bd0e240c0b7c0fbb360226bab03294d226a5560cdf2a3f21e6d406
2 parents b3edacb + 806b005 commit 1212808

File tree

10 files changed

+27
-13
lines changed

10 files changed

+27
-13
lines changed

doc/release-notes-0.18.1-16257.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Wallet changes
2+
--------------
3+
When creating a transaction with a fee above `-maxtxfee` (default 0.1 BTC),
4+
the RPC commands `walletcreatefundedpsbt` and `fundrawtransaction` will now fail
5+
instead of rounding down the fee. Beware that the `feeRate` argument is specified
6+
in BTC per kilobyte, not satoshi per byte.

src/policy/fees.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ enum class FeeReason {
4343
PAYTXFEE,
4444
FALLBACK,
4545
REQUIRED,
46-
MAXTXFEE,
4746
};
4847

4948
/* Used to determine type of fee estimation requested */

src/qt/walletmodel.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,7 @@ WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransact
221221
return TransactionCreationFailed;
222222
}
223223

224-
// reject absurdly high fee. (This can never happen because the
225-
// wallet caps the fee at m_default_max_tx_fee. This merely serves as a
226-
// belt-and-suspenders check)
224+
// Reject absurdly high fee
227225
if (nFeeRequired > m_wallet->getDefaultMaxTxFee())
228226
return AbsurdFee;
229227
}

src/util/error.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ std::string TransactionErrorString(const TransactionError err)
2727
return "PSBTs not compatible (different transactions)";
2828
case TransactionError::SIGHASH_MISMATCH:
2929
return "Specified sighash value does not match existing value";
30+
case TransactionError::MAX_FEE_EXCEEDED:
31+
return "Fee exceeds maximum configured by -maxtxfee";
3032
// no default case, so the compiler can warn about missing cases
3133
}
3234
assert(false);

src/util/error.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ enum class TransactionError {
2727
INVALID_PSBT,
2828
PSBT_MISMATCH,
2929
SIGHASH_MISMATCH,
30+
MAX_FEE_EXCEEDED,
3031
};
3132

3233
std::string TransactionErrorString(const TransactionError error);

src/util/fees.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ std::string StringForFeeReason(FeeReason reason) {
1818
{FeeReason::PAYTXFEE, "PayTxFee set"},
1919
{FeeReason::FALLBACK, "Fallback fee"},
2020
{FeeReason::REQUIRED, "Minimum Required Fee"},
21-
{FeeReason::MAXTXFEE, "MaxTxFee limit"}
2221
};
2322
auto reason_string = fee_reason_strings.find(reason);
2423

src/wallet/fees.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,7 @@ CAmount GetRequiredFee(const CWallet& wallet, unsigned int nTxBytes)
1818

1919
CAmount GetMinimumFee(const CWallet& wallet, unsigned int nTxBytes, const CCoinControl& coin_control, FeeCalculation* feeCalc)
2020
{
21-
CAmount fee_needed = GetMinimumFeeRate(wallet, coin_control, feeCalc).GetFee(nTxBytes);
22-
// Always obey the maximum
23-
const CAmount max_tx_fee = wallet.m_default_max_tx_fee;
24-
if (fee_needed > max_tx_fee) {
25-
fee_needed = max_tx_fee;
26-
if (feeCalc) feeCalc->reason = FeeReason::MAXTXFEE;
27-
}
28-
return fee_needed;
21+
return GetMinimumFeeRate(wallet, coin_control, feeCalc).GetFee(nTxBytes);
2922
}
3023

3124
CFeeRate GetRequiredFeeRate(const CWallet& wallet)

src/wallet/wallet.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2694,6 +2694,11 @@ bool CWallet::FundTransaction(CMutableTransaction& tx, CAmount& nFeeRet, int& nC
26942694
}
26952695
}
26962696

2697+
if (nFeeRet > this->m_default_max_tx_fee) {
2698+
strFailReason = TransactionErrorString(TransactionError::MAX_FEE_EXCEEDED);
2699+
return false;
2700+
}
2701+
26972702
return true;
26982703
}
26992704

test/functional/rpc_fundrawtransaction.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ def run_test(self):
662662
result = self.nodes[3].fundrawtransaction(rawtx) # uses min_relay_tx_fee (set by settxfee)
663663
result2 = self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 2*min_relay_tx_fee})
664664
result3 = self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 10*min_relay_tx_fee})
665+
assert_raises_rpc_error(-4, "Fee exceeds maximum configured by -maxtxfee", self.nodes[3].fundrawtransaction, rawtx, {"feeRate": 1})
665666
result_fee_rate = result['fee'] * 1000 / count_bytes(result['hex'])
666667
assert_fee_amount(result2['fee'], count_bytes(result2['hex']), 2 * result_fee_rate)
667668
assert_fee_amount(result3['fee'], count_bytes(result3['hex']), 10 * result_fee_rate)

test/functional/rpc_psbt.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from test_framework.test_framework import BitcoinTestFramework
1010
from test_framework.util import (
1111
assert_equal,
12+
assert_greater_than,
1213
assert_raises_rpc_error,
1314
connect_nodes_bi,
1415
disconnect_nodes,
@@ -129,6 +130,15 @@ def run_test(self):
129130
assert_equal(walletprocesspsbt_out['complete'], True)
130131
self.nodes[1].sendrawtransaction(self.nodes[1].finalizepsbt(walletprocesspsbt_out['psbt'])['hex'])
131132

133+
# feeRate of 0.1 BTC / KB produces a total fee slightly below -maxtxfee (~0.05280000):
134+
res = self.nodes[1].walletcreatefundedpsbt([{"txid":txid,"vout":p2wpkh_pos},{"txid":txid,"vout":p2sh_p2wpkh_pos},{"txid":txid,"vout":p2pkh_pos}], {self.nodes[1].getnewaddress():29.99}, 0, {"feeRate": 0.1})
135+
assert_greater_than(res["fee"], 0.05)
136+
assert_greater_than(0.06, res["fee"])
137+
138+
# feeRate of 10 BTC / KB produces a total fee well above -maxtxfee
139+
# previously this was silenty capped at -maxtxfee
140+
assert_raises_rpc_error(-4, "Fee exceeds maximum configured by -maxtxfee", self.nodes[1].walletcreatefundedpsbt, [{"txid":txid,"vout":p2wpkh_pos},{"txid":txid,"vout":p2sh_p2wpkh_pos},{"txid":txid,"vout":p2pkh_pos}], {self.nodes[1].getnewaddress():29.99}, 0, {"feeRate": 10})
141+
132142
# partially sign multisig things with node 1
133143
psbtx = self.nodes[1].walletcreatefundedpsbt([{"txid":txid,"vout":p2wsh_pos},{"txid":txid,"vout":p2sh_pos},{"txid":txid,"vout":p2sh_p2wsh_pos}], {self.nodes[1].getnewaddress():29.99})['psbt']
134144
walletprocesspsbt_out = self.nodes[1].walletprocesspsbt(psbtx)

0 commit comments

Comments
 (0)