Skip to content

Commit 769ff05

Browse files
author
Antoine Riard
committed
Refactor some importprunedfunds checks with guard clause
Credit to jkczyz
1 parent 5971d38 commit 769ff05

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

src/wallet/rpcdump.cpp

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -363,31 +363,26 @@ UniValue importprunedfunds(const JSONRPCRequest& request)
363363
//Search partial merkle tree in proof for our transaction and index in valid block
364364
std::vector<uint256> vMatch;
365365
std::vector<unsigned int> vIndex;
366-
unsigned int txnIndex = 0;
367-
Optional<int> height;
368-
if (merkleBlock.txn.ExtractMatches(vMatch, vIndex) == merkleBlock.header.hashMerkleRoot) {
369-
370-
auto locked_chain = pwallet->chain().lock();
371-
height = locked_chain->getBlockHeight(merkleBlock.header.GetHash());
372-
if (height == nullopt) {
373-
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found in chain");
374-
}
375-
376-
std::vector<uint256>::const_iterator it;
377-
if ((it = std::find(vMatch.begin(), vMatch.end(), hashTx))==vMatch.end()) {
378-
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction given doesn't exist in proof");
379-
}
366+
if (merkleBlock.txn.ExtractMatches(vMatch, vIndex) != merkleBlock.header.hashMerkleRoot) {
367+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Something wrong with merkleblock");
368+
}
380369

381-
txnIndex = vIndex[it - vMatch.begin()];
370+
auto locked_chain = pwallet->chain().lock();
371+
Optional<int> height = locked_chain->getBlockHeight(merkleBlock.header.GetHash());
372+
if (height == nullopt) {
373+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found in chain");
382374
}
383-
else {
384-
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Something wrong with merkleblock");
375+
376+
std::vector<uint256>::const_iterator it;
377+
if ((it = std::find(vMatch.begin(), vMatch.end(), hashTx)) == vMatch.end()) {
378+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction given doesn't exist in proof");
385379
}
386380

381+
unsigned int txnIndex = vIndex[it - vMatch.begin()];
382+
387383
CWalletTx::Confirmation confirm(CWalletTx::Status::CONFIRMED, *height, merkleBlock.header.GetHash(), txnIndex);
388384
wtx.m_confirm = confirm;
389385

390-
auto locked_chain = pwallet->chain().lock();
391386
LOCK(pwallet->cs_wallet);
392387

393388
if (pwallet->IsMine(*wtx.tx)) {

0 commit comments

Comments
 (0)