Skip to content

Commit cf79384

Browse files
committed
test: Coin Selection, duplicated preset inputs selection
This exercises the bug inside CoinsResult::Erase that ends up on (1) a wallet crash or (2) a created and broadcasted tx that contains a reduced recipient's amount. This is covered by making the wallet selects the preset inputs twice during the coin selection process. Making the wallet think that the selection process result covers the entire tx target when it does not. It's actually creating a tx that sends more coins than what inputs are covering for. Which, combined with the SFFO option, makes the wallet incorrectly reduce the recipient's amount by the difference between the original target and the wrongly counted inputs. Which means, a created and relayed tx sending less coins to the destination than what the user inputted.
1 parent 341ba7f commit cf79384

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

src/wallet/test/spend_tests.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,50 @@ BOOST_FIXTURE_TEST_CASE(FillInputToWeightTest, BasicTestingSetup)
112112
// Note: We don't test the next boundary because of memory allocation constraints.
113113
}
114114

115+
BOOST_FIXTURE_TEST_CASE(wallet_duplicated_preset_inputs_test, TestChain100Setup)
116+
{
117+
// Verify that the wallet's Coin Selection process does not include pre-selected inputs twice in a transaction.
118+
119+
// Add 4 spendable UTXO, 50 BTC each, to the wallet (total balance 200 BTC)
120+
for (int i = 0; i < 4; i++) CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
121+
auto wallet = CreateSyncedWallet(*m_node.chain, WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return m_node.chainman->ActiveChain()), m_args, coinbaseKey);
122+
123+
LOCK(wallet->cs_wallet);
124+
auto available_coins = AvailableCoins(*wallet);
125+
std::vector<COutput> coins = available_coins.All();
126+
// Preselect the first 3 UTXO (150 BTC total)
127+
std::set<COutPoint> preset_inputs = {coins[0].outpoint, coins[1].outpoint, coins[2].outpoint};
128+
129+
// Try to create a tx that spends more than what preset inputs + wallet selected inputs are covering for.
130+
// The wallet can cover up to 200 BTC, and the tx target is 299 BTC.
131+
std::vector<CRecipient> recipients = {{GetScriptForDestination(*Assert(wallet->GetNewDestination(OutputType::BECH32, "dummy"))),
132+
/*nAmount=*/299 * COIN, /*fSubtractFeeFromAmount=*/true}};
133+
CCoinControl coin_control;
134+
coin_control.m_allow_other_inputs = true;
135+
for (const auto& outpoint : preset_inputs) {
136+
coin_control.Select(outpoint);
137+
}
138+
139+
// Attempt to send 299 BTC from a wallet that only has 200 BTC. The wallet should exclude
140+
// the preset inputs from the pool of available coins, realize that there is not enough
141+
// money to fund the 299 BTC payment, and fail with "Insufficient funds".
142+
//
143+
// Even with SFFO, the wallet can only afford to send 200 BTC.
144+
// If the wallet does not properly exclude preset inputs from the pool of available coins
145+
// prior to coin selection, it may create a transaction that does not fund the full payment
146+
// amount or, through SFFO, incorrectly reduce the recipient's amount by the difference
147+
// between the original target and the wrongly counted inputs (in this case 99 BTC)
148+
// so that the recipient's amount is no longer equal to the user's selected target of 299 BTC.
149+
150+
// First case, use 'subtract_fee_from_outputs=true'
151+
util::Result<CreatedTransactionResult> res_tx = CreateTransaction(*wallet, recipients, /*change_pos*/-1, coin_control);
152+
BOOST_CHECK(!res_tx.has_value());
153+
154+
// Second case, don't use 'subtract_fee_from_outputs'.
155+
recipients[0].fSubtractFeeFromAmount = false;
156+
res_tx = CreateTransaction(*wallet, recipients, /*change_pos*/-1, coin_control);
157+
BOOST_CHECK(!res_tx.has_value());
158+
}
159+
115160
BOOST_AUTO_TEST_SUITE_END()
116161
} // namespace wallet

test/functional/rpc_fundrawtransaction.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ def run_test(self):
107107
self.generate(self.nodes[0], 121)
108108

109109
self.test_add_inputs_default_value()
110+
self.test_preset_inputs_selection()
110111
self.test_weight_calculation()
111112
self.test_change_position()
112113
self.test_simple()
@@ -1200,6 +1201,50 @@ def test_add_inputs_default_value(self):
12001201

12011202
self.nodes[2].unloadwallet("test_preset_inputs")
12021203

1204+
def test_preset_inputs_selection(self):
1205+
self.log.info('Test wallet preset inputs are not double-counted or reused in coin selection')
1206+
1207+
# Create and fund the wallet with 4 UTXO of 5 BTC each (20 BTC total)
1208+
self.nodes[2].createwallet("test_preset_inputs_selection")
1209+
wallet = self.nodes[2].get_wallet_rpc("test_preset_inputs_selection")
1210+
outputs = {}
1211+
for _ in range(4):
1212+
outputs[wallet.getnewaddress(address_type="bech32")] = 5
1213+
self.nodes[0].sendmany("", outputs)
1214+
self.generate(self.nodes[0], 1)
1215+
1216+
# Select the preset inputs
1217+
coins = wallet.listunspent()
1218+
preset_inputs = [coins[0], coins[1], coins[2]]
1219+
1220+
# Now let's create the tx creation options
1221+
options = {
1222+
"inputs": preset_inputs,
1223+
"add_inputs": True, # automatically add coins from the wallet to fulfill the target
1224+
"subtract_fee_from_outputs": [0], # deduct fee from first output
1225+
"add_to_wallet": False
1226+
}
1227+
1228+
# Attempt to send 29 BTC from a wallet that only has 20 BTC. The wallet should exclude
1229+
# the preset inputs from the pool of available coins, realize that there is not enough
1230+
# money to fund the 29 BTC payment, and fail with "Insufficient funds".
1231+
#
1232+
# Even with SFFO, the wallet can only afford to send 20 BTC.
1233+
# If the wallet does not properly exclude preset inputs from the pool of available coins
1234+
# prior to coin selection, it may create a transaction that does not fund the full payment
1235+
# amount or, through SFFO, incorrectly reduce the recipient's amount by the difference
1236+
# between the original target and the wrongly counted inputs (in this case 9 BTC)
1237+
# so that the recipient's amount is no longer equal to the user's selected target of 29 BTC.
1238+
1239+
# First case, use 'subtract_fee_from_outputs = true'
1240+
assert_raises_rpc_error(-4, "Insufficient funds", wallet.send, outputs=[{wallet.getnewaddress(address_type="bech32"): 29}], options=options)
1241+
1242+
# Second case, don't use 'subtract_fee_from_outputs'
1243+
del options["subtract_fee_from_outputs"]
1244+
assert_raises_rpc_error(-4, "Insufficient funds", wallet.send, outputs=[{wallet.getnewaddress(address_type="bech32"): 29}], options=options)
1245+
1246+
self.nodes[2].unloadwallet("test_preset_inputs_selection")
1247+
12031248
def test_weight_calculation(self):
12041249
self.log.info("Test weight calculation with external inputs")
12051250

0 commit comments

Comments
 (0)