From 109f57c83ed4092a2be2301011d3b3136a0ce3af Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kittywhiskers@users.noreply.github.com> Date: Mon, 25 Sep 2023 22:53:15 +0530 Subject: [PATCH] refactor: use range-based loops instead of iterators Co-authored-by: Konstantin Akimov --- src/rpc/blockchain.cpp | 10 ++-- src/rpc/misc.cpp | 109 ++++++++++++++++++++++------------------- src/txmempool.cpp | 6 +-- 3 files changed, 66 insertions(+), 59 deletions(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index f0a0bbede1..d0b66b3d1e 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -838,8 +838,8 @@ static UniValue getblockhashes(const JSONRPCRequest& request) } UniValue result(UniValue::VARR); - for (std::vector::const_iterator it=blockHashes.begin(); it!=blockHashes.end(); it++) { - result.push_back(it->GetHex()); + for (const auto& hash : blockHashes) { + result.push_back(hash.GetHex()); } return result; @@ -1828,9 +1828,9 @@ static UniValue getchaintips(const JSONRPCRequest& request) } } - for (std::set::iterator it = setOrphans.begin(); it != setOrphans.end(); ++it) { - if (setPrevs.erase(*it) == 0) { - setTips.insert(*it); + for (const auto& orphan : setOrphans) { + if (setPrevs.erase(orphan) == 0) { + setTips.insert(orphan); } } diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index b56d7a5291..2bf14af50f 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -635,13 +635,10 @@ static bool getAddressesFromParams(const UniValue& params, std::vector values = addressValues.getValues(); - - for (std::vector::iterator it = values.begin(); it != values.end(); ++it) { - + for (const auto& address : addressValues.getValues()) { uint160 hashBytes; int type{AddressType::UNKNOWN}; - if (!getIndexKey(it->get_str(), hashBytes, type)) { + if (!getIndexKey(address.get_str(), hashBytes, type)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address"); } addresses.push_back(std::make_pair(hashBytes, type)); @@ -711,23 +708,24 @@ static UniValue getaddressmempool(const JSONRPCRequest& request) UniValue result(UniValue::VARR); - for (std::vector >::iterator it = indexes.begin(); - it != indexes.end(); it++) { + for (const auto& index : indexes) { + const auto& mempoolAddressDeltaKey = index.first; + const auto& mempoolAddressDelta = index.second; std::string address; - if (!getAddressFromIndex(it->first.m_address_type, it->first.m_address_bytes, address)) { + if (!getAddressFromIndex(mempoolAddressDeltaKey.m_address_type, mempoolAddressDeltaKey.m_address_bytes, address)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unknown address type"); } UniValue delta(UniValue::VOBJ); delta.pushKV("address", address); - delta.pushKV("txid", it->first.m_tx_hash.GetHex()); - delta.pushKV("index", (int)it->first.m_tx_index); - delta.pushKV("satoshis", it->second.m_amount); - delta.pushKV("timestamp", count_seconds(it->second.m_time)); - if (it->second.m_amount < 0) { - delta.pushKV("prevtxid", it->second.m_prev_hash.GetHex()); - delta.pushKV("prevout", (int)it->second.m_prev_out); + delta.pushKV("txid", mempoolAddressDeltaKey.m_tx_hash.GetHex()); + delta.pushKV("index", (int)mempoolAddressDeltaKey.m_tx_index); + delta.pushKV("satoshis", mempoolAddressDelta.m_amount); + delta.pushKV("timestamp", count_seconds(mempoolAddressDelta.m_time)); + if (mempoolAddressDelta.m_amount < 0) { + delta.pushKV("prevtxid", mempoolAddressDelta.m_prev_hash.GetHex()); + delta.pushKV("prevout", (int)mempoolAddressDelta.m_prev_out); } result.push_back(delta); } @@ -773,8 +771,8 @@ static UniValue getaddressutxos(const JSONRPCRequest& request) std::vector > unspentOutputs; - for (std::vector >::iterator it = addresses.begin(); it != addresses.end(); it++) { - if (!GetAddressUnspent((*it).first, (*it).second, unspentOutputs)) { + for (const auto& address : addresses) { + if (!GetAddressUnspent(address.first, address.second, unspentOutputs)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); } } @@ -783,19 +781,22 @@ static UniValue getaddressutxos(const JSONRPCRequest& request) UniValue result(UniValue::VARR); - for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { + for (const auto& unspentOutput : unspentOutputs) { + const auto& unspentKey = unspentOutput.first; + const auto& unspentValue = unspentOutput.second; + UniValue output(UniValue::VOBJ); std::string address; - if (!getAddressFromIndex(it->first.m_address_type, it->first.m_address_bytes, address)) { + if (!getAddressFromIndex(unspentKey.m_address_type, unspentKey.m_address_bytes, address)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unknown address type"); } output.pushKV("address", address); - output.pushKV("txid", it->first.m_tx_hash.GetHex()); - output.pushKV("outputIndex", (int)it->first.m_tx_index); - output.pushKV("script", HexStr(it->second.m_tx_script)); - output.pushKV("satoshis", it->second.m_amount); - output.pushKV("height", it->second.m_block_height); + output.pushKV("txid", unspentKey.m_tx_hash.GetHex()); + output.pushKV("outputIndex", (int)unspentKey.m_tx_index); + output.pushKV("script", HexStr(unspentValue.m_tx_script)); + output.pushKV("satoshis", unspentValue.m_amount); + output.pushKV("height", unspentValue.m_block_height); result.push_back(output); } @@ -855,13 +856,13 @@ static UniValue getaddressdeltas(const JSONRPCRequest& request) std::vector > addressIndex; - for (std::vector >::iterator it = addresses.begin(); it != addresses.end(); it++) { + for (const auto& address : addresses) { if (start > 0 && end > 0) { - if (!GetAddressIndex((*it).first, (*it).second, addressIndex, start, end)) { + if (!GetAddressIndex(address.first, address.second, addressIndex, start, end)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); } } else { - if (!GetAddressIndex((*it).first, (*it).second, addressIndex)) { + if (!GetAddressIndex(address.first, address.second, addressIndex)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); } } @@ -869,18 +870,20 @@ static UniValue getaddressdeltas(const JSONRPCRequest& request) UniValue result(UniValue::VARR); - for (std::vector >::const_iterator it=addressIndex.begin(); it!=addressIndex.end(); it++) { + for (const auto& index : addressIndex) { + const auto& indexKey = index.first; + const auto& indexDelta = index.second; std::string address; - if (!getAddressFromIndex(it->first.m_address_type, it->first.m_address_bytes, address)) { + if (!getAddressFromIndex(indexKey.m_address_type, indexKey.m_address_bytes, address)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unknown address type"); } UniValue delta(UniValue::VOBJ); - delta.pushKV("satoshis", it->second); - delta.pushKV("txid", it->first.m_tx_hash.GetHex()); - delta.pushKV("index", (int)it->first.m_tx_index); - delta.pushKV("blockindex", (int)it->first.m_block_tx_pos); - delta.pushKV("height", it->first.m_block_height); + delta.pushKV("satoshis", indexDelta); + delta.pushKV("txid", indexKey.m_tx_hash.GetHex()); + delta.pushKV("index", (int)indexKey.m_tx_index); + delta.pushKV("blockindex", (int)indexKey.m_block_tx_pos); + delta.pushKV("height", indexKey.m_block_height); delta.pushKV("address", address); result.push_back(delta); } @@ -921,8 +924,8 @@ static UniValue getaddressbalance(const JSONRPCRequest& request) std::vector > addressIndex; - for (std::vector >::iterator it = addresses.begin(); it != addresses.end(); it++) { - if (!GetAddressIndex((*it).first, (*it).second, addressIndex)) { + for (const auto& address : addresses) { + if (!GetAddressIndex(address.first, address.second, addressIndex)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); } } @@ -935,16 +938,18 @@ static UniValue getaddressbalance(const JSONRPCRequest& request) CAmount balance_immature = 0; CAmount received = 0; - for (std::vector >::const_iterator it=addressIndex.begin(); it!=addressIndex.end(); it++) { - if (it->second > 0) { - received += it->second; + for (const auto& index : addressIndex) { + const auto& indexKey = index.first; + const auto& indexDelta = index.second; + if (indexDelta > 0) { + received += indexDelta; } - if (it->first.m_block_tx_pos == 0 && nHeight - it->first.m_block_height < COINBASE_MATURITY) { - balance_immature += it->second; + if (indexKey.m_block_tx_pos == 0 && nHeight - indexKey.m_block_height < COINBASE_MATURITY) { + balance_immature += indexDelta; } else { - balance_spendable += it->second; + balance_spendable += indexDelta; } - balance += it->second; + balance += indexDelta; } UniValue result(UniValue::VOBJ); @@ -997,13 +1002,13 @@ static UniValue getaddresstxids(const JSONRPCRequest& request) std::vector > addressIndex; - for (std::vector >::iterator it = addresses.begin(); it != addresses.end(); it++) { + for (const auto& address : addresses) { if (start > 0 && end > 0) { - if (!GetAddressIndex((*it).first, (*it).second, addressIndex, start, end)) { + if (!GetAddressIndex(address.first, address.second, addressIndex, start, end)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); } } else { - if (!GetAddressIndex((*it).first, (*it).second, addressIndex)) { + if (!GetAddressIndex(address.first, address.second, addressIndex)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); } } @@ -1012,9 +1017,11 @@ static UniValue getaddresstxids(const JSONRPCRequest& request) std::set > txids; UniValue result(UniValue::VARR); - for (std::vector >::const_iterator it=addressIndex.begin(); it!=addressIndex.end(); it++) { - int height = it->first.m_block_height; - std::string txid = it->first.m_tx_hash.GetHex(); + for (const auto& index : addressIndex) { + const auto& indexKey = index.first; + + int height = indexKey.m_block_height; + std::string txid = indexKey.m_tx_hash.GetHex(); if (addresses.size() > 1) { txids.insert(std::make_pair(height, txid)); @@ -1026,8 +1033,8 @@ static UniValue getaddresstxids(const JSONRPCRequest& request) } if (addresses.size() > 1) { - for (std::set >::const_iterator it=txids.begin(); it!=txids.end(); it++) { - result.push_back(it->second); + for (const auto& tx : txids) { + result.push_back(tx.second); } } diff --git a/src/txmempool.cpp b/src/txmempool.cpp index c175a6a5c8..6ad711829b 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -510,9 +510,9 @@ bool CTxMemPool::getAddressIndex(std::vector > &addresse std::vector > &results) { LOCK(cs); - for (std::vector >::iterator it = addresses.begin(); it != addresses.end(); it++) { - addressDeltaMap::iterator ait = mapAddress.lower_bound(CMempoolAddressDeltaKey((*it).second, (*it).first)); - while (ait != mapAddress.end() && (*ait).first.m_address_bytes == (*it).first && (*ait).first.m_address_type == (*it).second) { + for (const auto& address : addresses) { + addressDeltaMap::iterator ait = mapAddress.lower_bound(CMempoolAddressDeltaKey(address.second, address.first)); + while (ait != mapAddress.end() && (*ait).first.m_address_bytes == address.first && (*ait).first.m_address_type == address.second) { results.push_back(*ait); ait++; }