Skip to content

Commit 2f9d65d

Browse files
kwvgknst
andcommitted
refactor: cleanup index code, style updates, deduplication
Co-authored-by: Konstantin Akimov <[email protected]>
1 parent eaca912 commit 2f9d65d

File tree

5 files changed

+41
-72
lines changed

5 files changed

+41
-72
lines changed

src/rpc/index_util.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ extern RecursiveMutex cs_main;
2727
//! throws JSONRPCError if address index is unavailable
2828
bool GetAddressIndex(CBlockTreeDB& block_tree_db, const uint160& addressHash, const AddressType type,
2929
std::vector<CAddressIndexEntry>& addressIndex,
30-
const int32_t start = 0, const int32_t end = 0)
30+
const int32_t start, const int32_t end)
3131
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
3232
//! throws JSONRPCError if address index is unavailable
3333
bool GetAddressUnspentIndex(CBlockTreeDB& block_tree_db, const uint160& addressHash, const AddressType type,

src/rpc/misc.cpp

+10-23
Original file line numberDiff line numberDiff line change
@@ -818,18 +818,10 @@ static RPCHelpMan getaddressdeltas()
818818
{
819819
LOCK(::cs_main);
820820
for (const auto& address : addresses) {
821-
if (start > 0 && end > 0) {
822-
if (!GetAddressIndex(*chainman.m_blockman.m_block_tree_db, address.first, address.second,
823-
addressIndex, start, end))
824-
{
825-
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address");
826-
}
827-
} else {
828-
if (!GetAddressIndex(*chainman.m_blockman.m_block_tree_db, address.first, address.second,
829-
addressIndex))
830-
{
831-
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address");
832-
}
821+
if (start <= 0 || end <= 0) { start = 0; end = 0; }
822+
if (!GetAddressIndex(*chainman.m_blockman.m_block_tree_db, address.first, address.second,
823+
addressIndex, start, end)) {
824+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address");
833825
}
834826
}
835827
}
@@ -896,7 +888,8 @@ static RPCHelpMan getaddressbalance()
896888
{
897889
LOCK(::cs_main);
898890
for (const auto& address : addresses) {
899-
if (!GetAddressIndex(*chainman.m_blockman.m_block_tree_db, address.first, address.second, addressIndex)) {
891+
if (!GetAddressIndex(*chainman.m_blockman.m_block_tree_db, address.first, address.second, addressIndex,
892+
/*start=*/0, /*end=*/0)) {
900893
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address");
901894
}
902895
}
@@ -977,16 +970,10 @@ static RPCHelpMan getaddresstxids()
977970
{
978971
LOCK(::cs_main);
979972
for (const auto& address : addresses) {
980-
if (start > 0 && end > 0) {
981-
if (!GetAddressIndex(*chainman.m_blockman.m_block_tree_db, address.first, address.second,
982-
addressIndex, start, end)) {
983-
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address");
984-
}
985-
} else {
986-
if (!GetAddressIndex(*chainman.m_blockman.m_block_tree_db, address.first, address.second,
987-
addressIndex)) {
988-
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address");
989-
}
973+
if (start <= 0 || end <= 0) { start = 0; end = 0; }
974+
if (!GetAddressIndex(*chainman.m_blockman.m_block_tree_db, address.first, address.second,
975+
addressIndex, start, end)) {
976+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address");
990977
}
991978
}
992979
}

src/txdb.cpp

+14-12
Original file line numberDiff line numberDiff line change
@@ -291,23 +291,23 @@ bool CBlockTreeDB::ReadSpentIndex(const CSpentIndexKey key, CSpentIndexValue& va
291291

292292
bool CBlockTreeDB::UpdateSpentIndex(const std::vector<CSpentIndexEntry>& vect) {
293293
CDBBatch batch(*this);
294-
for (std::vector<std::pair<CSpentIndexKey,CSpentIndexValue>>::const_iterator it=vect.begin(); it!=vect.end(); it++) {
295-
if (it->second.IsNull()) {
296-
batch.Erase(std::make_pair(DB_SPENTINDEX, it->first));
294+
for (const auto& [key, value] : vect) {
295+
if (value.IsNull()) {
296+
batch.Erase(std::make_pair(DB_SPENTINDEX, key));
297297
} else {
298-
batch.Write(std::make_pair(DB_SPENTINDEX, it->first), it->second);
298+
batch.Write(std::make_pair(DB_SPENTINDEX, key), value);
299299
}
300300
}
301301
return WriteBatch(batch);
302302
}
303303

304304
bool CBlockTreeDB::UpdateAddressUnspentIndex(const std::vector<CAddressUnspentIndexEntry>& vect) {
305305
CDBBatch batch(*this);
306-
for (std::vector<CAddressUnspentIndexEntry>::const_iterator it=vect.begin(); it!=vect.end(); it++) {
307-
if (it->second.IsNull()) {
308-
batch.Erase(std::make_pair(DB_ADDRESSUNSPENTINDEX, it->first));
306+
for (const auto& [key, value] : vect) {
307+
if (value.IsNull()) {
308+
batch.Erase(std::make_pair(DB_ADDRESSUNSPENTINDEX, key));
309309
} else {
310-
batch.Write(std::make_pair(DB_ADDRESSUNSPENTINDEX, it->first), it->second);
310+
batch.Write(std::make_pair(DB_ADDRESSUNSPENTINDEX, key), value);
311311
}
312312
}
313313
return WriteBatch(batch);
@@ -340,15 +340,17 @@ bool CBlockTreeDB::ReadAddressUnspentIndex(const uint160& addressHash, const Add
340340

341341
bool CBlockTreeDB::WriteAddressIndex(const std::vector<CAddressIndexEntry>& vect) {
342342
CDBBatch batch(*this);
343-
for (std::vector<CAddressIndexEntry>::const_iterator it=vect.begin(); it!=vect.end(); it++)
344-
batch.Write(std::make_pair(DB_ADDRESSINDEX, it->first), it->second);
343+
for (const auto& [key, value] : vect) {
344+
batch.Write(std::make_pair(DB_ADDRESSINDEX, key), value);
345+
}
345346
return WriteBatch(batch);
346347
}
347348

348349
bool CBlockTreeDB::EraseAddressIndex(const std::vector<CAddressIndexEntry>& vect) {
349350
CDBBatch batch(*this);
350-
for (std::vector<CAddressIndexEntry>::const_iterator it=vect.begin(); it!=vect.end(); it++)
351-
batch.Erase(std::make_pair(DB_ADDRESSINDEX, it->first));
351+
for (const auto& [key, _] : vect) {
352+
batch.Erase(std::make_pair(DB_ADDRESSINDEX, key));
353+
}
352354
return WriteBatch(batch);
353355
}
354356

src/txmempool.cpp

+10-22
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ bool CTxMemPool::getAddressIndex(const std::vector<CMempoolAddressDeltaKey>& add
609609
{
610610
LOCK(cs);
611611
for (const auto& address : addresses) {
612-
addressDeltaMap::const_iterator ait = mapAddress.lower_bound(address);
612+
auto ait = mapAddress.lower_bound(address);
613613
while (ait != mapAddress.end() && (*ait).first.m_address_bytes == address.m_address_bytes
614614
&& (*ait).first.m_address_type == address.m_address_type) {
615615
results.push_back(*ait);
@@ -619,20 +619,15 @@ bool CTxMemPool::getAddressIndex(const std::vector<CMempoolAddressDeltaKey>& add
619619
return true;
620620
}
621621

622-
bool CTxMemPool::removeAddressIndex(const uint256 txhash)
622+
void CTxMemPool::removeAddressIndex(const uint256 txhash)
623623
{
624624
LOCK(cs);
625-
addressDeltaMapInserted::iterator it = mapAddressInserted.find(txhash);
626-
627-
if (it != mapAddressInserted.end()) {
628-
std::vector<CMempoolAddressDeltaKey> keys = (*it).second;
629-
for (std::vector<CMempoolAddressDeltaKey>::iterator mit = keys.begin(); mit != keys.end(); mit++) {
630-
mapAddress.erase(*mit);
625+
if (auto it = mapAddressInserted.find(txhash); it != mapAddressInserted.end()) {
626+
for (const auto& key : (*it).second) {
627+
mapAddress.erase(key);
631628
}
632629
mapAddressInserted.erase(it);
633630
}
634-
635-
return true;
636631
}
637632

638633
void CTxMemPool::addSpentIndex(const CTxMemPoolEntry& entry, const CCoinsViewCache& view)
@@ -668,29 +663,22 @@ void CTxMemPool::addSpentIndex(const CTxMemPoolEntry& entry, const CCoinsViewCac
668663
bool CTxMemPool::getSpentIndex(const CSpentIndexKey& key, CSpentIndexValue& value) const
669664
{
670665
LOCK(cs);
671-
mapSpentIndex::const_iterator it = mapSpent.find(key);
672-
673-
if (it != mapSpent.end()) {
666+
if (auto it = mapSpent.find(key); it != mapSpent.end()) {
674667
value = it->second;
675668
return true;
676669
}
677670
return false;
678671
}
679672

680-
bool CTxMemPool::removeSpentIndex(const uint256 txhash)
673+
void CTxMemPool::removeSpentIndex(const uint256 txhash)
681674
{
682675
LOCK(cs);
683-
mapSpentIndexInserted::iterator it = mapSpentInserted.find(txhash);
684-
685-
if (it != mapSpentInserted.end()) {
686-
std::vector<CSpentIndexKey> keys = (*it).second;
687-
for (std::vector<CSpentIndexKey>::iterator mit = keys.begin(); mit != keys.end(); mit++) {
688-
mapSpent.erase(*mit);
676+
if (auto it = mapSpentInserted.find(txhash); it != mapSpentInserted.end()) {
677+
for (const auto& key : (*it).second) {
678+
mapSpent.erase(key);
689679
}
690680
mapSpentInserted.erase(it);
691681
}
692-
693-
return true;
694682
}
695683

696684
void CTxMemPool::addUncheckedProTx(indexed_transaction_set::iterator& newit, const CTransaction& tx)

src/txmempool.h

+6-14
Original file line numberDiff line numberDiff line change
@@ -519,18 +519,10 @@ class CTxMemPool
519519
private:
520520
typedef std::map<txiter, setEntries, CompareIteratorByHash> cacheMap;
521521

522-
523-
typedef std::map<CMempoolAddressDeltaKey, CMempoolAddressDelta, CMempoolAddressDeltaKeyCompare> addressDeltaMap;
524-
addressDeltaMap mapAddress;
525-
526-
typedef std::map<uint256, std::vector<CMempoolAddressDeltaKey> > addressDeltaMapInserted;
527-
addressDeltaMapInserted mapAddressInserted;
528-
529-
typedef std::map<CSpentIndexKey, CSpentIndexValue, CSpentIndexKeyCompare> mapSpentIndex;
530-
mapSpentIndex mapSpent;
531-
532-
typedef std::map<uint256, std::vector<CSpentIndexKey> > mapSpentIndexInserted;
533-
mapSpentIndexInserted mapSpentInserted;
522+
std::map<CMempoolAddressDeltaKey, CMempoolAddressDelta, CMempoolAddressDeltaKeyCompare> mapAddress;
523+
std::map<uint256, std::vector<CMempoolAddressDeltaKey>> mapAddressInserted;
524+
std::map<CSpentIndexKey, CSpentIndexValue, CSpentIndexKeyCompare> mapSpent;
525+
std::map<uint256, std::vector<CSpentIndexKey>> mapSpentInserted;
534526

535527
std::multimap<uint256, uint256> mapProTxRefs; // proTxHash -> transaction (all TXs that refer to an existing proTx)
536528
std::map<CService, uint256> mapProTxAddresses;
@@ -618,11 +610,11 @@ class CTxMemPool
618610
void addAddressIndex(const CTxMemPoolEntry& entry, const CCoinsViewCache& view);
619611
bool getAddressIndex(const std::vector<CMempoolAddressDeltaKey>& addresses,
620612
std::vector<CMempoolAddressDeltaEntry>& results) const;
621-
bool removeAddressIndex(const uint256 txhash);
613+
void removeAddressIndex(const uint256 txhash);
622614

623615
void addSpentIndex(const CTxMemPoolEntry& entry, const CCoinsViewCache& view);
624616
bool getSpentIndex(const CSpentIndexKey& key, CSpentIndexValue& value) const;
625-
bool removeSpentIndex(const uint256 txhash);
617+
void removeSpentIndex(const uint256 txhash);
626618

627619
void removeRecursive(const CTransaction& tx, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs);
628620
/** After reorg, filter the entries that would no longer be valid in the next block, and update

0 commit comments

Comments
 (0)