Skip to content

Commit d54c5c8

Browse files
committed
wallet: use CCoinControl to estimate signature size
1 parent a94659c commit d54c5c8

File tree

6 files changed

+25
-29
lines changed

6 files changed

+25
-29
lines changed

src/bench/coin_selection.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static void CoinSelection(benchmark::Bench& bench)
5959
std::vector<COutput> coins;
6060
for (const auto& wtx : wtxs) {
6161
const auto txout = wtx->tx->vout.at(0);
62-
coins.emplace_back(COutPoint(wtx->GetHash(), 0), txout, /*depth=*/6 * 24, CalculateMaximumSignedInputSize(txout, &wallet, false), /*spendable=*/true, /*solvable=*/true, /*safe=*/true, wtx->GetTxTime(), /*from_me=*/true, /*fees=*/ 0);
62+
coins.emplace_back(COutPoint(wtx->GetHash(), 0), txout, /*depth=*/6 * 24, CalculateMaximumSignedInputSize(txout, &wallet, /*coin_control=*/nullptr), /*spendable=*/true, /*solvable=*/true, /*safe=*/true, wtx->GetTxTime(), /*from_me=*/true, /*fees=*/ 0);
6363
}
6464

6565
const CoinEligibilityFilter filter_standard(1, 6, 0);

src/wallet/spend.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,20 @@ using interfaces::FoundBlock;
2727
namespace wallet {
2828
static constexpr size_t OUTPUT_GROUP_MAX_ENTRIES{100};
2929

30-
int CalculateMaximumSignedInputSize(const CTxOut& txout, const SigningProvider* provider, bool use_max_sig)
30+
int CalculateMaximumSignedInputSize(const CTxOut& txout, const COutPoint outpoint, const SigningProvider* provider, const CCoinControl* coin_control)
3131
{
3232
CMutableTransaction txn;
33-
txn.vin.push_back(CTxIn(COutPoint()));
34-
if (!provider || !DummySignInput(*provider, txn.vin[0], txout, use_max_sig)) {
33+
txn.vin.push_back(CTxIn(outpoint));
34+
if (!provider || !DummySignInput(*provider, txn.vin[0], txout, coin_control)) {
3535
return -1;
3636
}
3737
return GetVirtualTransactionInputSize(txn.vin[0]);
3838
}
3939

40-
int CalculateMaximumSignedInputSize(const CTxOut& txout, const CWallet* wallet, bool use_max_sig)
40+
int CalculateMaximumSignedInputSize(const CTxOut& txout, const CWallet* wallet, const CCoinControl* coin_control)
4141
{
4242
const std::unique_ptr<SigningProvider> provider = wallet->GetSolvingProvider(txout.scriptPubKey);
43-
return CalculateMaximumSignedInputSize(txout, provider.get(), use_max_sig);
43+
return CalculateMaximumSignedInputSize(txout, COutPoint(), provider.get(), coin_control);
4444
}
4545

4646
// txouts needs to be in the order of tx.vin
@@ -193,7 +193,7 @@ CoinsResult AvailableCoins(const CWallet& wallet,
193193
// Filter by spendable outputs only
194194
if (!spendable && only_spendable) continue;
195195

196-
int input_bytes = CalculateMaximumSignedInputSize(output, provider.get(), coinControl && coinControl->fAllowWatchOnly);
196+
int input_bytes = CalculateMaximumSignedInputSize(output, COutPoint(), provider.get(), coinControl);
197197
result.coins.emplace_back(outpoint, output, nDepth, input_bytes, spendable, solvable, safeTx, wtx.GetTxTime(), tx_from_me, feerate);
198198
result.total_amount += output.nValue;
199199

@@ -286,7 +286,7 @@ std::map<CTxDestination, std::vector<COutput>> ListCoins(const CWallet& wallet)
286286
if (ExtractDestination(FindNonChangeParentOutput(wallet, *wtx.tx, output.n).scriptPubKey, address)) {
287287
const auto out = wtx.tx->vout.at(output.n);
288288
result[address].emplace_back(
289-
COutPoint(wtx.GetHash(), output.n), out, depth, CalculateMaximumSignedInputSize(out, &wallet, false), /*spendable=*/ true, /*solvable=*/ true, /*safe=*/ false, wtx.GetTxTime(), CachedTxIsFromMe(wallet, wtx, ISMINE_ALL));
289+
COutPoint(wtx.GetHash(), output.n), out, depth, CalculateMaximumSignedInputSize(out, &wallet, /*coin_control=*/nullptr), /*spendable=*/ true, /*solvable=*/ true, /*safe=*/ false, wtx.GetTxTime(), CachedTxIsFromMe(wallet, wtx, ISMINE_ALL));
290290
}
291291
}
292292
}
@@ -443,14 +443,14 @@ std::optional<SelectionResult> SelectCoins(const CWallet& wallet, const std::vec
443443
if (ptr_wtx->tx->vout.size() <= outpoint.n) {
444444
return std::nullopt;
445445
}
446-
input_bytes = CalculateMaximumSignedInputSize(ptr_wtx->tx->vout[outpoint.n], &wallet, false);
447446
txout = ptr_wtx->tx->vout.at(outpoint.n);
447+
input_bytes = CalculateMaximumSignedInputSize(txout, &wallet, &coin_control);
448448
} else {
449449
// The input is external. We did not find the tx in mapWallet.
450450
if (!coin_control.GetExternalOutput(outpoint, txout)) {
451451
return std::nullopt;
452452
}
453-
input_bytes = CalculateMaximumSignedInputSize(txout, &coin_control.m_external_provider, /*use_max_sig=*/true);
453+
input_bytes = CalculateMaximumSignedInputSize(txout, outpoint, &coin_control.m_external_provider, &coin_control);
454454
}
455455
// If available, override calculated size with coin control specified size
456456
if (coin_control.HasInputWeight(outpoint)) {

src/wallet/spend.h

+5-9
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,16 @@
1414

1515
namespace wallet {
1616
/** Get the marginal bytes if spending the specified output from this transaction.
17-
* use_max_sig indicates whether to use the maximum sized, 72 byte signature when calculating the
18-
* size of the input spend. This should only be set when watch-only outputs are allowed */
19-
int CalculateMaximumSignedInputSize(const CTxOut& txout, const CWallet* pwallet, bool use_max_sig = false);
20-
int CalculateMaximumSignedInputSize(const CTxOut& txout, const SigningProvider* pwallet, bool use_max_sig = false);
21-
17+
* Use CoinControl to determine whether to expect signature grinding when calculating the size of the input spend. */
18+
int CalculateMaximumSignedInputSize(const CTxOut& txout, const CWallet* pwallet, const CCoinControl* coin_control = nullptr);
19+
int CalculateMaximumSignedInputSize(const CTxOut& txout, const COutPoint outpoint, const SigningProvider* pwallet, const CCoinControl* coin_control = nullptr);
2220
struct TxSize {
2321
int64_t vsize{-1};
2422
int64_t weight{-1};
2523
};
2624

27-
/** Calculate the size of the transaction assuming all signatures are max size
28-
* Use DummySignatureCreator, which inserts 71 byte signatures everywhere.
29-
* NOTE: this requires that all inputs must be in mapWallet (eg the tx should
30-
* be AllInputsMine). */
25+
/** Calculate the size of the transaction using CoinControl to determine
26+
* whether to expect signature grinding when calculating the size of the input spend. */
3127
TxSize CalculateMaximumSignedTxSize(const CTransaction& tx, const CWallet* wallet, const std::vector<CTxOut>& txouts, const CCoinControl* coin_control = nullptr);
3228
TxSize CalculateMaximumSignedTxSize(const CTransaction& tx, const CWallet* wallet, const CCoinControl* coin_control = nullptr) EXCLUSIVE_LOCKS_REQUIRED(wallet->cs_wallet);
3329

src/wallet/test/coinselector_tests.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ static void add_coin(std::vector<COutput>& coins, CWallet& wallet, const CAmount
8686
auto ret = wallet.mapWallet.emplace(std::piecewise_construct, std::forward_as_tuple(txid), std::forward_as_tuple(MakeTransactionRef(std::move(tx)), TxStateInactive{}));
8787
assert(ret.second);
8888
CWalletTx& wtx = (*ret.first).second;
89-
const auto txout = wtx.tx->vout.at(nInput);
90-
coins.emplace_back(COutPoint(wtx.GetHash(), nInput), txout, nAge, CalculateMaximumSignedInputSize(txout, &wallet, false), /*spendable=*/ true, /*solvable=*/ true, /*safe=*/ true, wtx.GetTxTime(), fIsFromMe, feerate);
89+
const auto& txout = wtx.tx->vout.at(nInput);
90+
coins.emplace_back(COutPoint(wtx.GetHash(), nInput), txout, nAge, CalculateMaximumSignedInputSize(txout, &wallet, /*coin_control=*/nullptr), /*spendable=*/ true, /*solvable=*/ true, /*safe=*/ true, wtx.GetTxTime(), fIsFromMe, feerate);
9191
}
9292

9393
/** Check if SelectionResult a is equivalent to SelectionResult b.

src/wallet/wallet.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -1504,13 +1504,16 @@ bool CWallet::AddWalletFlags(uint64_t flags)
15041504
}
15051505

15061506
// Helper for producing a max-sized low-S low-R signature (eg 71 bytes)
1507-
// or a max-sized low-S signature (e.g. 72 bytes) if use_max_sig is true
1508-
bool DummySignInput(const SigningProvider& provider, CTxIn &tx_in, const CTxOut &txout, bool use_max_sig)
1507+
// or a max-sized low-S signature (e.g. 72 bytes) depending on coin_control
1508+
bool DummySignInput(const SigningProvider& provider, CTxIn &tx_in, const CTxOut &txout, const CCoinControl* coin_control)
15091509
{
15101510
// Fill in dummy signatures for fee calculation.
15111511
const CScript& scriptPubKey = txout.scriptPubKey;
15121512
SignatureData sigdata;
15131513

1514+
// Use max sig if watch only inputs were used or if this particular input is an external input
1515+
// to ensure a sufficient fee is attained for the requested feerate.
1516+
const bool use_max_sig = coin_control && (coin_control->fAllowWatchOnly || coin_control->IsExternalSelected(tx_in.prevout));
15141517
if (!ProduceSignature(provider, use_max_sig ? DUMMY_MAXIMUM_SIGNATURE_CREATOR : DUMMY_SIGNATURE_CREATOR, scriptPubKey, sigdata)) {
15151518
return false;
15161519
}
@@ -1577,12 +1580,9 @@ bool CWallet::DummySignTx(CMutableTransaction &txNew, const std::vector<CTxOut>
15771580
nIn++;
15781581
continue;
15791582
}
1580-
// Use max sig if watch only inputs were used or if this particular input is an external input
1581-
// to ensure a sufficient fee is attained for the requested feerate.
1582-
const bool use_max_sig = coin_control && (coin_control->fAllowWatchOnly || coin_control->IsExternalSelected(txin.prevout));
15831583
const std::unique_ptr<SigningProvider> provider = GetSolvingProvider(txout.scriptPubKey);
1584-
if (!provider || !DummySignInput(*provider, txin, txout, use_max_sig)) {
1585-
if (!coin_control || !DummySignInput(coin_control->m_external_provider, txin, txout, use_max_sig)) {
1584+
if (!provider || !DummySignInput(*provider, txin, txout, coin_control)) {
1585+
if (!coin_control || !DummySignInput(coin_control->m_external_provider, txin, txout, coin_control)) {
15861586
return false;
15871587
}
15881588
}

src/wallet/wallet.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ bool AddWalletSetting(interfaces::Chain& chain, const std::string& wallet_name);
932932
//! Remove wallet name from persistent configuration so it will not be loaded on startup.
933933
bool RemoveWalletSetting(interfaces::Chain& chain, const std::string& wallet_name);
934934

935-
bool DummySignInput(const SigningProvider& provider, CTxIn &tx_in, const CTxOut &txout, bool use_max_sig);
935+
bool DummySignInput(const SigningProvider& provider, CTxIn &tx_in, const CTxOut &txout, const CCoinControl* coin_control = nullptr);
936936

937937
bool FillInputToWeight(CTxIn& txin, int64_t target_weight);
938938
} // namespace wallet

0 commit comments

Comments
 (0)