Skip to content

Commit

Permalink
[Refactor] Avoid more mapWallet lookups (budget / sapling code)
Browse files Browse the repository at this point in the history
  • Loading branch information
random-zebra committed Dec 13, 2021
1 parent c706743 commit f46891a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
5 changes: 3 additions & 2 deletions src/rpc/budget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ UniValue preparebudget(const JSONRPCRequest& request)
throw JSONRPCError(RPC_WALLET_ERROR, res.ToString());

// Store proposal name as a comment
assert(pwallet->mapWallet.count(wtx->GetHash()));
pwallet->mapWallet.at(wtx->GetHash()).SetComment("Proposal: " + strProposalName);
auto it = pwallet->mapWallet.find(wtx->GetHash());
assert(it != pwallet->mapWallet.end());
it->second.SetComment("Proposal: " + strProposalName);

return wtx->GetHash().ToString();
}
Expand Down
21 changes: 11 additions & 10 deletions src/sapling/saplingscriptpubkeyman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ void SaplingScriptPubKeyMan::UpdateSaplingNullifierNoteMapForBlock(const CBlock
LOCK(wallet->cs_wallet);

for (const auto& tx : pblock->vtx) {
const uint256& hash = tx->GetHash();
bool txIsOurs = wallet->mapWallet.count(hash);
if (txIsOurs) {
UpdateSaplingNullifierNoteMapWithTx(wallet->mapWallet.at(hash));
auto it = wallet->mapWallet.find(tx->GetHash());
if (it != wallet->mapWallet.end()) {
UpdateSaplingNullifierNoteMapWithTx(it->second);
}
}
}
Expand Down Expand Up @@ -746,8 +745,9 @@ CAmount SaplingScriptPubKeyMan::GetDebit(const CTransaction& tx, const isminefil
// If we have the spend nullifier, it means that this input is ours.
// The transaction (and decrypted note data) has been added to the wallet.
const SaplingOutPoint& op = it->second;
assert(wallet->mapWallet.count(op.hash));
const auto& wtx = wallet->mapWallet.at(op.hash);
auto wit = wallet->mapWallet.find(op.hash);
assert(wit != wallet->mapWallet.end());
const auto& wtx = wit->second;
assert(wtx.mapSaplingNoteData.count(op));
const auto& nd = wtx.mapSaplingNoteData.at(op);
assert(nd.IsMyNote()); // todo: Add watch only check.
Expand Down Expand Up @@ -820,10 +820,11 @@ void SaplingScriptPubKeyMan::GetSaplingNoteWitnesses(const std::vector<SaplingOu
Optional<uint256> rt;
int i = 0;
for (SaplingOutPoint note : notes) {
if (wallet->mapWallet.count(note.hash) &&
wallet->mapWallet.at(note.hash).mapSaplingNoteData.count(note) &&
wallet->mapWallet.at(note.hash).mapSaplingNoteData[note].witnesses.size() > 0) {
witnesses[i] = wallet->mapWallet.at(note.hash).mapSaplingNoteData[note].witnesses.front();
auto it = wallet->mapWallet.find(note.hash);
if (it != wallet->mapWallet.end() &&
it->second.mapSaplingNoteData.count(note) &&
it->second.mapSaplingNoteData[note].witnesses.size() > 0) {
witnesses[i] = it->second.mapSaplingNoteData[note].witnesses.front();
if (!rt) {
rt = witnesses[i]->root();
} else {
Expand Down
17 changes: 10 additions & 7 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1094,12 +1094,13 @@ static UniValue ShieldSendManyTo(CWallet * const pwallet,

// add comments
const uint256& txHash = uint256S(txid);
assert(pwallet->mapWallet.count(txHash));
auto it = pwallet->mapWallet.find(txHash);
assert(it != pwallet->mapWallet.end());
if (!commentStr.empty()) {
pwallet->mapWallet.at(txHash).mapValue["comment"] = commentStr;
it->second.mapValue["comment"] = commentStr;
}
if (!toStr.empty()) {
pwallet->mapWallet.at(txHash).mapValue["to"] = toStr;
it->second.mapValue["to"] = toStr;
}

return txid;
Expand Down Expand Up @@ -1532,9 +1533,10 @@ UniValue viewshieldtransaction(const JSONRPCRequest& request)
hash.SetHex(request.params[0].get_str());

UniValue entry(UniValue::VOBJ);
if (!pwallet->mapWallet.count(hash))
auto it = pwallet->mapWallet.find(hash);
if (it == pwallet->mapWallet.end())
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid or non-wallet transaction id");
const CWalletTx& wtx = pwallet->mapWallet.at(hash);
const CWalletTx& wtx = it->second;

if (!wtx.tx->IsShieldedTx()) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid transaction, no shield data available");
Expand Down Expand Up @@ -2807,8 +2809,9 @@ UniValue listreceivedbyshieldaddress(const JSONRPCRequest& request)
int index = -1;
int64_t time = 0;

if (pwallet->mapWallet.count(entry.op.hash)) {
const CWalletTx& wtx = pwallet->mapWallet.at(entry.op.hash);
auto it = pwallet->mapWallet.find(entry.op.hash);
if (it != pwallet->mapWallet.end()) {
const CWalletTx& wtx = it->second;
if (!wtx.m_confirm.hashBlock.IsNull())
height = mapBlockIndex[wtx.m_confirm.hashBlock]->nHeight;
index = wtx.m_confirm.nIndex;
Expand Down

0 comments on commit f46891a

Please sign in to comment.