From 3350160a6c497d156275b2b534c0b4577047a24a Mon Sep 17 00:00:00 2001 From: Duke Leto Date: Wed, 18 Jul 2018 16:02:53 +0000 Subject: [PATCH 1/2] Fix bug where finding index entries of other types, such as timestampindex, stopped our iterator --- src/txdb.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/txdb.cpp b/src/txdb.cpp index cb0358403..5608cbb07 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -411,6 +411,7 @@ extern UniValue CBlockTreeDB::Snapshot() pcursor->SeekToLast(); int64_t startingHeight = chainActive.Height(); + //fprintf(stderr, "starting_height=%li\n", startingHeight); while (pcursor->Valid()) { @@ -423,7 +424,8 @@ extern UniValue CBlockTreeDB::Snapshot() ssKey >> chType; ssKey >> indexKey; - if ( chType == DB_ADDRESSUNSPENTINDEX ) + //fprintf(stderr, "chType=%d\n", chType); + if (chType == DB_ADDRESSUNSPENTINDEX) { try { leveldb::Slice slValue = pcursor->value(); @@ -435,7 +437,7 @@ extern UniValue CBlockTreeDB::Snapshot() std::map ::iterator pos = addressAmounts.find(address); if (pos == addressAmounts.end()) { // insert new address + utxo amount - //fprintf(stderr, "inserting new address %s with amount %li\n", address.c_str(), nValue); + fprintf(stderr, "inserting new address %s with amount %li\n", address.c_str(), nValue); addressAmounts[address] = nValue; totalAddresses++; } else { @@ -446,9 +448,10 @@ extern UniValue CBlockTreeDB::Snapshot() //fprintf(stderr,"{\"%s\", %.8f},\n",address.c_str(),(double)nValue/COIN); total += nValue; } catch (const std::exception& e) { - return error("failed to get address index value"); + // this should not happen normally, but maybe if an index is corrupt + fprintf(stderr, "failed to get address index value\n"); } - } else { break; } + } } catch (const std::exception& e) { fprintf(stderr, "%s: LevelDB exception! - %s\n", __func__, e.what()); break; @@ -477,9 +480,11 @@ extern UniValue CBlockTreeDB::Snapshot() addressesSorted.push_back(obj); } - result.push_back(make_pair("addresses", addressesSorted)); - result.push_back(make_pair("total", total / COIN )); - result.push_back(make_pair("average",(double) (total/COIN) / totalAddresses )); + if (totalAddresses > 0) { + result.push_back(make_pair("addresses", addressesSorted)); + result.push_back(make_pair("total", total / COIN )); + result.push_back(make_pair("average",(double) (total/COIN) / totalAddresses )); + } // Total number of addresses in this snaphot result.push_back(make_pair("total_addresses", totalAddresses)); // The snapshot began at this block height From a4d41984f67bcc7472469b564de92b8ab1505eca Mon Sep 17 00:00:00 2001 From: Duke Leto Date: Wed, 18 Jul 2018 19:44:16 +0000 Subject: [PATCH 2/2] Modify how we iterate and deal with the end of iteration, since iter->Valid seems to always be true --- src/txdb.cpp | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/txdb.cpp b/src/txdb.cpp index 5608cbb07..af6cd0fbf 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -402,61 +402,59 @@ bool getAddressFromIndex(const int &type, const uint160 &hash, std::string &addr extern UniValue CBlockTreeDB::Snapshot() { char chType; int64_t total = 0; int64_t totalAddresses = 0; std::string address; - boost::scoped_ptr pcursor(NewIterator()); + boost::scoped_ptr iter(NewIterator()); std::map addressAmounts; UniValue result(UniValue::VOBJ); result.push_back(Pair("start_time", time(NULL))); - //pcursor->SeekToFirst(); - pcursor->SeekToLast(); - int64_t startingHeight = chainActive.Height(); - //fprintf(stderr, "starting_height=%li\n", startingHeight); - - while (pcursor->Valid()) + fprintf(stderr, "Starting snapshot at height %li\n", startingHeight); + for (iter->SeekToLast(); iter->Valid(); iter->Prev()) { boost::this_thread::interruption_point(); try { - leveldb::Slice slKey = pcursor->key(); + leveldb::Slice slKey = iter->key(); CDataStream ssKey(slKey.data(), slKey.data()+slKey.size(), SER_DISK, CLIENT_VERSION); CAddressIndexIteratorKey indexKey; - ssKey >> chType; - ssKey >> indexKey; + + ssKey >> chType; + ssKey >> indexKey; //fprintf(stderr, "chType=%d\n", chType); if (chType == DB_ADDRESSUNSPENTINDEX) { try { - leveldb::Slice slValue = pcursor->value(); + leveldb::Slice slValue = iter->value(); CDataStream ssValue(slValue.data(), slValue.data()+slValue.size(), SER_DISK, CLIENT_VERSION); CAmount nValue; ssValue >> nValue; getAddressFromIndex(indexKey.type, indexKey.hashBytes, address); + std::map ::iterator pos = addressAmounts.find(address); if (pos == addressAmounts.end()) { // insert new address + utxo amount - fprintf(stderr, "inserting new address %s with amount %li\n", address.c_str(), nValue); + //fprintf(stderr, "inserting new address %s with amount %li\n", address.c_str(), nValue); addressAmounts[address] = nValue; totalAddresses++; } else { // update unspent tally for this address + //fprintf(stderr, "updating address %s with new utxo amount %li\n", address.c_str(), nValue); addressAmounts[address] += nValue; } - //fprintf(stderr,"{\"%s\", %.8f},\n",address.c_str(),(double)nValue/COIN); total += nValue; + } catch (const std::exception& e) { - // this should not happen normally, but maybe if an index is corrupt - fprintf(stderr, "failed to get address index value\n"); + fprintf(stderr, "DONE %s: LevelDB addressindex exception! - %s\n", __func__, e.what()); + break; } - } + } } catch (const std::exception& e) { - fprintf(stderr, "%s: LevelDB exception! - %s\n", __func__, e.what()); + fprintf(stderr, "DONE reading index entries\n"); break; } - pcursor->Prev(); } UniValue addresses(UniValue::VARR);