Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Wallet] Avoid second mapWallet lookup #2679

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
23 changes: 14 additions & 9 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 Expand Up @@ -3295,9 +3298,11 @@ UniValue gettransaction(const JSONRPCRequest& request)
filter = filter | ISMINE_WATCH_ONLY;

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;

CAmount nCredit = wtx.GetCredit(filter);
CAmount nDebit = wtx.GetDebit(filter);
Expand Down
5 changes: 3 additions & 2 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -755,8 +755,9 @@ void CWallet::AddToSpends(const COutPoint& outpoint, const uint256& wtxid)

void CWallet::AddToSpends(const uint256& wtxid)
{
assert(mapWallet.count(wtxid));
CWalletTx& thisTx = mapWallet.at(wtxid);
auto it = mapWallet.find(wtxid);
assert(it != mapWallet.end());
CWalletTx& thisTx = it->second;
if (thisTx.IsCoinBase()) // Coinbases don't spend anything!
return;

Expand Down