Skip to content

Commit 194710d

Browse files
committed
Merge bitcoin#25481: wallet: unify max signature logic
d54c5c8 wallet: use CCoinControl to estimate signature size (S3RK) a94659c wallet: replace GetTxSpendSize with CalculateMaximumSignedInputSize (S3RK) Pull request description: Currently `DummySignTx` and `DummySignInput` use different ways to determine signature size. This PR unifies the way wallet estimates signature size for various inputs. Instead of passing boolean flags from calling code the `use_max_sig` is now calculated at the place of signature creation using information available in `CCoinControl` ACKs for top commit: achow101: ACK d54c5c8 theStack: Code-review ACK d54c5c8 Tree-SHA512: e790903ad4683067070aa7dbf7434a1bd142282a5bc425112e64d88d27559f1a2cd60c68d6022feaf6b845237035cb18ece10f6243d719ba28173b69bd99110a
2 parents b9f9ed4 + d54c5c8 commit 194710d

File tree

6 files changed

+27
-36
lines changed

6 files changed

+27
-36
lines changed

src/bench/coin_selection.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ static void CoinSelection(benchmark::Bench& bench)
5858
// Create coins
5959
std::vector<COutput> coins;
6060
for (const auto& wtx : wtxs) {
61-
coins.emplace_back(COutPoint(wtx->GetHash(), 0), wtx->tx->vout.at(0), /*depth=*/6 * 24, GetTxSpendSize(wallet, *wtx, 0), /*spendable=*/true, /*solvable=*/true, /*safe=*/true, wtx->GetTxTime(), /*from_me=*/true, /*fees=*/ 0);
61+
const auto txout = wtx->tx->vout.at(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);
6263
}
6364

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

src/wallet/spend.cpp

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

30-
int GetTxSpendSize(const CWallet& wallet, const CWalletTx& wtx, unsigned int out, bool use_max_sig)
31-
{
32-
return CalculateMaximumSignedInputSize(wtx.tx->vout[out], &wallet, use_max_sig);
33-
}
34-
35-
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)
3631
{
3732
CMutableTransaction txn;
38-
txn.vin.push_back(CTxIn(COutPoint()));
39-
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)) {
4035
return -1;
4136
}
4237
return GetVirtualTransactionInputSize(txn.vin[0]);
4338
}
4439

45-
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)
4641
{
4742
const std::unique_ptr<SigningProvider> provider = wallet->GetSolvingProvider(txout.scriptPubKey);
48-
return CalculateMaximumSignedInputSize(txout, provider.get(), use_max_sig);
43+
return CalculateMaximumSignedInputSize(txout, COutPoint(), provider.get(), coin_control);
4944
}
5045

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

201-
int input_bytes = GetTxSpendSize(wallet, wtx, i, (coinControl && coinControl->fAllowWatchOnly));
196+
int input_bytes = CalculateMaximumSignedInputSize(output, COutPoint(), provider.get(), coinControl);
202197
result.coins.emplace_back(outpoint, output, nDepth, input_bytes, spendable, solvable, safeTx, wtx.GetTxTime(), tx_from_me, feerate);
203198
result.total_amount += output.nValue;
204199

@@ -289,8 +284,9 @@ std::map<CTxDestination, std::vector<COutput>> ListCoins(const CWallet& wallet)
289284
) {
290285
CTxDestination address;
291286
if (ExtractDestination(FindNonChangeParentOutput(wallet, *wtx.tx, output.n).scriptPubKey, address)) {
287+
const auto out = wtx.tx->vout.at(output.n);
292288
result[address].emplace_back(
293-
COutPoint(wtx.GetHash(), output.n), wtx.tx->vout.at(output.n), depth, GetTxSpendSize(wallet, wtx, output.n), /*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));
294290
}
295291
}
296292
}
@@ -450,14 +446,14 @@ std::optional<SelectionResult> SelectCoins(const CWallet& wallet, const std::vec
450446
if (ptr_wtx->tx->vout.size() <= outpoint.n) {
451447
return std::nullopt;
452448
}
453-
input_bytes = GetTxSpendSize(wallet, *ptr_wtx, outpoint.n, false);
454449
txout = ptr_wtx->tx->vout.at(outpoint.n);
450+
input_bytes = CalculateMaximumSignedInputSize(txout, &wallet, &coin_control);
455451
} else {
456452
// The input is external. We did not find the tx in mapWallet.
457453
if (!coin_control.GetExternalOutput(outpoint, txout)) {
458454
return std::nullopt;
459455
}
460-
input_bytes = CalculateMaximumSignedInputSize(txout, &coin_control.m_external_provider, /*use_max_sig=*/true);
456+
input_bytes = CalculateMaximumSignedInputSize(txout, outpoint, &coin_control.m_external_provider, &coin_control);
461457
}
462458
// If available, override calculated size with coin control specified size
463459
if (coin_control.HasInputWeight(outpoint)) {

src/wallet/spend.h

+5-12
Original file line numberDiff line numberDiff line change
@@ -14,23 +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 GetTxSpendSize(const CWallet& wallet, const CWalletTx& wtx, unsigned int out, bool use_max_sig = false);
20-
21-
//Get the marginal bytes of spending the specified output
22-
int CalculateMaximumSignedInputSize(const CTxOut& txout, const CWallet* pwallet, bool use_max_sig = false);
23-
int CalculateMaximumSignedInputSize(const CTxOut& txout, const SigningProvider* pwallet, bool use_max_sig = false);
24-
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);
2520
struct TxSize {
2621
int64_t vsize{-1};
2722
int64_t weight{-1};
2823
};
2924

30-
/** Calculate the size of the transaction assuming all signatures are max size
31-
* Use DummySignatureCreator, which inserts 71 byte signatures everywhere.
32-
* NOTE: this requires that all inputs must be in mapWallet (eg the tx should
33-
* 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. */
3427
TxSize CalculateMaximumSignedTxSize(const CTransaction& tx, const CWallet* wallet, const std::vector<CTxOut>& txouts, const CCoinControl* coin_control = nullptr);
3528
TxSize CalculateMaximumSignedTxSize(const CTransaction& tx, const CWallet* wallet, const CCoinControl* coin_control = nullptr) EXCLUSIVE_LOCKS_REQUIRED(wallet->cs_wallet);
3629

src/wallet/test/coinselector_tests.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +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-
coins.emplace_back(COutPoint(wtx.GetHash(), nInput), wtx.tx->vout.at(nInput), nAge, GetTxSpendSize(wallet, wtx, nInput), /*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);
9091
}
9192

9293
/** 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
@@ -955,7 +955,7 @@ bool AddWalletSetting(interfaces::Chain& chain, const std::string& wallet_name);
955955
//! Remove wallet name from persistent configuration so it will not be loaded on startup.
956956
bool RemoveWalletSetting(interfaces::Chain& chain, const std::string& wallet_name);
957957

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

960960
bool FillInputToWeight(CTxIn& txin, int64_t target_weight);
961961
} // namespace wallet

0 commit comments

Comments
 (0)