Skip to content

Commit

Permalink
wip: use spk cache when signing
Browse files Browse the repository at this point in the history
if the wallet has a large amount of SPKMans (e.g. after migrating a legacy wallet), we can
avoid iterating through all of them by using the spk cache. The downside is we iterate through the tx inputs
again when trying to sign with a particular SPK. If all our inputs are managed by the same SPK, then
this is always faster due to the early exit. If not, its still faster when number of inputs < SPKMans in the wallet.
It is slower in the case where number of inputs > SPKMans and all of the inputs are from different SPKs, which
seems unlikely. This worst case could be improved by threading the input all the way through when signing, but
that needs more thought.
  • Loading branch information
josibake committed Feb 2, 2024
1 parent 6a4888c commit 5c3a2da
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2121,11 +2121,14 @@ bool CWallet::SignTransaction(CMutableTransaction& tx) const
bool CWallet::SignTransaction(CMutableTransaction& tx, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, bilingual_str>& input_errors) const
{
// Try to sign with all ScriptPubKeyMans
for (ScriptPubKeyMan* spk_man : GetAllScriptPubKeyMans()) {
// spk_man->SignTransaction will return true if the transaction is complete,
// so we can exit early and return true if that happens
if (spk_man->SignTransaction(tx, coins, sighash, input_errors)) {
return true;
for (const CTxIn& tx_in : tx.vin) {
// Use the scriptPubKey cache to grab the correct set of SPKMan(s)
for (const ScriptPubKeyMan* spk_man : GetScriptPubKeyMans(coins.at(tx_in.prevout).out.scriptPubKey)) {
// spk_man->SignTransaction will return true if the transaction is complete,
// so we can exit early and return true if that happens
if (spk_man->SignTransaction(tx, coins, sighash, input_errors)) {
return true;
}
}
}

Expand Down

0 comments on commit 5c3a2da

Please sign in to comment.