Skip to content

Commit a537d7a

Browse files
committed
wallet: SelectExternal actually external inputs
If an external input's utxo was created by a transaction that the wallet knows about, then it would not be selected using SelectExternal. This results in either funding failure or incorrect weight calculation.
1 parent f2d00bf commit a537d7a

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/wallet/spend.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,12 +1127,16 @@ bool FundTransaction(CWallet& wallet, CMutableTransaction& tx, CAmount& nFeeRet,
11271127
wallet.chain().findCoins(coins);
11281128

11291129
for (const CTxIn& txin : tx.vin) {
1130-
// if it's not in the wallet and corresponding UTXO is found than select as external output
11311130
const auto& outPoint = txin.prevout;
1132-
if (wallet.mapWallet.find(outPoint.hash) == wallet.mapWallet.end() && !coins[outPoint].out.IsNull()) {
1133-
coinControl.SelectExternal(outPoint, coins[outPoint].out);
1134-
} else {
1131+
if (wallet.IsMine(outPoint)) {
1132+
// The input was found in the wallet, so select as internal
11351133
coinControl.Select(outPoint);
1134+
} else if (coins[outPoint].out.IsNull()) {
1135+
error = _("Unable to find UTXO for external input");
1136+
return false;
1137+
} else {
1138+
// The input was not in the wallet, but is in the UTXO set, so select as external
1139+
coinControl.SelectExternal(outPoint, coins[outPoint].out);
11361140
}
11371141
}
11381142

test/functional/rpc_fundrawtransaction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ def test_invalid_input(self):
408408
inputs = [ {'txid' : "1c7f966dab21119bac53213a2bc7532bff1fa844c124fd750a7d0b1332440bd1", 'vout' : 0} ] #invalid vin!
409409
outputs = { self.nodes[0].getnewaddress() : 1.0}
410410
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
411-
assert_raises_rpc_error(-4, "Insufficient funds", self.nodes[2].fundrawtransaction, rawtx)
411+
assert_raises_rpc_error(-4, "Unable to find UTXO for external input", self.nodes[2].fundrawtransaction, rawtx)
412412

413413
def test_fee_p2pkh(self):
414414
"""Compare fee of a standard pubkeyhash transaction."""

0 commit comments

Comments
 (0)