mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-22 15:04:44 +01:00
Refactor some importprunedfunds checks with guard clause
Credit to jkczyz
This commit is contained in:
parent
5971d3848e
commit
769ff05e48
1 changed files with 14 additions and 19 deletions
|
@ -363,31 +363,26 @@ UniValue importprunedfunds(const JSONRPCRequest& request)
|
||||||
//Search partial merkle tree in proof for our transaction and index in valid block
|
//Search partial merkle tree in proof for our transaction and index in valid block
|
||||||
std::vector<uint256> vMatch;
|
std::vector<uint256> vMatch;
|
||||||
std::vector<unsigned int> vIndex;
|
std::vector<unsigned int> vIndex;
|
||||||
unsigned int txnIndex = 0;
|
if (merkleBlock.txn.ExtractMatches(vMatch, vIndex) != merkleBlock.header.hashMerkleRoot) {
|
||||||
Optional<int> height;
|
|
||||||
if (merkleBlock.txn.ExtractMatches(vMatch, vIndex) == merkleBlock.header.hashMerkleRoot) {
|
|
||||||
|
|
||||||
auto locked_chain = pwallet->chain().lock();
|
|
||||||
height = locked_chain->getBlockHeight(merkleBlock.header.GetHash());
|
|
||||||
if (height == nullopt) {
|
|
||||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found in chain");
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<uint256>::const_iterator it;
|
|
||||||
if ((it = std::find(vMatch.begin(), vMatch.end(), hashTx))==vMatch.end()) {
|
|
||||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction given doesn't exist in proof");
|
|
||||||
}
|
|
||||||
|
|
||||||
txnIndex = vIndex[it - vMatch.begin()];
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Something wrong with merkleblock");
|
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Something wrong with merkleblock");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto locked_chain = pwallet->chain().lock();
|
||||||
|
Optional<int> height = locked_chain->getBlockHeight(merkleBlock.header.GetHash());
|
||||||
|
if (height == nullopt) {
|
||||||
|
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found in chain");
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<uint256>::const_iterator it;
|
||||||
|
if ((it = std::find(vMatch.begin(), vMatch.end(), hashTx)) == vMatch.end()) {
|
||||||
|
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction given doesn't exist in proof");
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int txnIndex = vIndex[it - vMatch.begin()];
|
||||||
|
|
||||||
CWalletTx::Confirmation confirm(CWalletTx::Status::CONFIRMED, *height, merkleBlock.header.GetHash(), txnIndex);
|
CWalletTx::Confirmation confirm(CWalletTx::Status::CONFIRMED, *height, merkleBlock.header.GetHash(), txnIndex);
|
||||||
wtx.m_confirm = confirm;
|
wtx.m_confirm = confirm;
|
||||||
|
|
||||||
auto locked_chain = pwallet->chain().lock();
|
|
||||||
LOCK(pwallet->cs_wallet);
|
LOCK(pwallet->cs_wallet);
|
||||||
|
|
||||||
if (pwallet->IsMine(*wtx.tx)) {
|
if (pwallet->IsMine(*wtx.tx)) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue