From da853923b6c905457ffeb9990b22e443b214e732 Mon Sep 17 00:00:00 2001 From: Bushstar Date: Mon, 4 Nov 2024 11:24:53 +0000 Subject: [PATCH 1/2] Log DVM hash without undos --- src/dfi/rpc_accounts.cpp | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/dfi/rpc_accounts.cpp b/src/dfi/rpc_accounts.cpp index 54363401ad..6f87df3c0c 100644 --- a/src/dfi/rpc_accounts.cpp +++ b/src/dfi/rpc_accounts.cpp @@ -3700,8 +3700,9 @@ UniValue logdbhashes(const JSONRPCRequest &request) { // Create a CDBIterator auto pcursor = db->NewIterator(leveldb::ReadOptions()); - // Create a SHA256 hasher + // Create SHA256 hashers CSHA256 hasher; + CSHA256 hasherNoUndo; // Seek to the beginning of the database pcursor->SeekToFirst(); @@ -3712,9 +3713,16 @@ UniValue logdbhashes(const JSONRPCRequest &request) { auto keySlice = pcursor->GetKey(); auto valueSlice = pcursor->GetValue(); - // Feed the key and value into the hasher - hasher.Write((const unsigned char *)keySlice.data(), keySlice.size()); - hasher.Write((const unsigned char *)valueSlice.data(), valueSlice.size()); + const auto key = std::string(keySlice.data(), keySlice.size()); + if (!key.empty() && key[0] == 'u') { + hasher.Write((const unsigned char *)keySlice.data(), keySlice.size()); + hasher.Write((const unsigned char *)valueSlice.data(), valueSlice.size()); + } else { + hasher.Write((const unsigned char *)keySlice.data(), keySlice.size()); + hasher.Write((const unsigned char *)valueSlice.data(), valueSlice.size()); + hasherNoUndo.Write((const unsigned char *)keySlice.data(), keySlice.size()); + hasherNoUndo.Write((const unsigned char *)valueSlice.data(), valueSlice.size()); + } // Move to the next key-value pair pcursor->Next(); @@ -3723,12 +3731,15 @@ UniValue logdbhashes(const JSONRPCRequest &request) { // Delete iterator delete pcursor; - // Finalize the hash + // Finalize the hashes unsigned char hash[CSHA256::OUTPUT_SIZE]; + unsigned char hashNoUndo[CSHA256::OUTPUT_SIZE]; hasher.Finalize(hash); + hasher.Finalize(hashNoUndo); - // Convert hash to hex string + // Convert hashes to hex string const auto hashHex = HexStr(hash, hash + CSHA256::OUTPUT_SIZE); + const auto hashHexNoUndo = HexStr(hashNoUndo, hashNoUndo + CSHA256::OUTPUT_SIZE); // Get the current block height const auto height = ::ChainActive().Height(); @@ -3737,6 +3748,7 @@ UniValue logdbhashes(const JSONRPCRequest &request) { UniValue result(UniValue::VOBJ); result.pushKV("height", height); result.pushKV("dvmhash", hashHex); + result.pushKV("dvmhash_no_undo", hashHexNoUndo); const auto evmHashHex = XResultValueLogged(evm_try_get_hash_db_state(result)); if (evmHashHex) { From 639bb43f240a9c3ff7eb7e876ddb33f47fd0608f Mon Sep 17 00:00:00 2001 From: Bushstar Date: Mon, 4 Nov 2024 13:11:18 +0000 Subject: [PATCH 2/2] Log DVM hashes pre and post consolidation --- src/dfi/masternodes.cpp | 48 ++++++++++++++++++++++++++++++++++++++++ src/dfi/masternodes.h | 2 ++ src/dfi/rpc_accounts.cpp | 47 +-------------------------------------- src/init.cpp | 10 +++++++++ 4 files changed, 61 insertions(+), 46 deletions(-) diff --git a/src/dfi/masternodes.cpp b/src/dfi/masternodes.cpp index cf697c7a1e..6aef72faf9 100644 --- a/src/dfi/masternodes.cpp +++ b/src/dfi/masternodes.cpp @@ -1435,3 +1435,51 @@ void CalcMissingRewardTempFix(CCustomCSView &mnview, const uint32_t targetHeight } } } + +std::pair GetDVMDBHashes(CCustomCSView &view) { + auto db = view.GetStorage().GetStorageLevelDB()->GetDB(); + + // Create a CDBIterator + auto pcursor = db->NewIterator(leveldb::ReadOptions()); + + // Create SHA256 hashers + CSHA256 hasher; + CSHA256 hasherNoUndo; + + // Seek to the beginning of the database + pcursor->SeekToFirst(); + + // Iterate over all key-value pairs + while (pcursor->Valid()) { + // Get the key and value slices + auto keySlice = pcursor->GetKey(); + auto valueSlice = pcursor->GetValue(); + + const auto key = std::string(keySlice.data(), keySlice.size()); + if (!key.empty() && key[0] != 'u') { + hasherNoUndo.Write((const unsigned char *)keySlice.data(), keySlice.size()); + hasherNoUndo.Write((const unsigned char *)valueSlice.data(), valueSlice.size()); + } + + hasher.Write((const unsigned char *)keySlice.data(), keySlice.size()); + hasher.Write((const unsigned char *)valueSlice.data(), valueSlice.size()); + + // Move to the next key-value pair + pcursor->Next(); + } + + // Delete iterator + delete pcursor; + + // Finalize the hashes + unsigned char hash[CSHA256::OUTPUT_SIZE]; + unsigned char hashNoUndo[CSHA256::OUTPUT_SIZE]; + hasher.Finalize(hash); + hasher.Finalize(hashNoUndo); + + // Convert hashes to hex string + const auto hashHex = HexStr(hash, hash + CSHA256::OUTPUT_SIZE); + const auto hashHexNoUndo = HexStr(hashNoUndo, hashNoUndo + CSHA256::OUTPUT_SIZE); + + return {hashHex, hashHexNoUndo}; +} \ No newline at end of file diff --git a/src/dfi/masternodes.h b/src/dfi/masternodes.h index 2abe4f804c..1f2b0db9d2 100644 --- a/src/dfi/masternodes.h +++ b/src/dfi/masternodes.h @@ -51,6 +51,8 @@ CAmount GetProposalCreationFee(int height, const CCustomCSView &view, const CCre // Missing call fixed in: https://github.com/DeFiCh/ain/pull/1766 void CalcMissingRewardTempFix(CCustomCSView &mnview, const uint32_t targetHeight, const CWallet &wallet); +std::pair GetDVMDBHashes(CCustomCSView &view); + enum class UpdateMasternodeType : uint8_t { None = 0x00, OwnerAddress = 0x01, diff --git a/src/dfi/rpc_accounts.cpp b/src/dfi/rpc_accounts.cpp index 6f87df3c0c..b29e73f3e8 100644 --- a/src/dfi/rpc_accounts.cpp +++ b/src/dfi/rpc_accounts.cpp @@ -3694,52 +3694,7 @@ UniValue logdbhashes(const JSONRPCRequest &request) { pcustomcsview->Flush(); pcustomcsDB->Flush(); - // Get the CDBWrapper instance from CCustomCSView - auto db = pcustomcsview->GetStorage().GetStorageLevelDB()->GetDB(); - - // Create a CDBIterator - auto pcursor = db->NewIterator(leveldb::ReadOptions()); - - // Create SHA256 hashers - CSHA256 hasher; - CSHA256 hasherNoUndo; - - // Seek to the beginning of the database - pcursor->SeekToFirst(); - - // Iterate over all key-value pairs - while (pcursor->Valid()) { - // Get the key and value slices - auto keySlice = pcursor->GetKey(); - auto valueSlice = pcursor->GetValue(); - - const auto key = std::string(keySlice.data(), keySlice.size()); - if (!key.empty() && key[0] == 'u') { - hasher.Write((const unsigned char *)keySlice.data(), keySlice.size()); - hasher.Write((const unsigned char *)valueSlice.data(), valueSlice.size()); - } else { - hasher.Write((const unsigned char *)keySlice.data(), keySlice.size()); - hasher.Write((const unsigned char *)valueSlice.data(), valueSlice.size()); - hasherNoUndo.Write((const unsigned char *)keySlice.data(), keySlice.size()); - hasherNoUndo.Write((const unsigned char *)valueSlice.data(), valueSlice.size()); - } - - // Move to the next key-value pair - pcursor->Next(); - } - - // Delete iterator - delete pcursor; - - // Finalize the hashes - unsigned char hash[CSHA256::OUTPUT_SIZE]; - unsigned char hashNoUndo[CSHA256::OUTPUT_SIZE]; - hasher.Finalize(hash); - hasher.Finalize(hashNoUndo); - - // Convert hashes to hex string - const auto hashHex = HexStr(hash, hash + CSHA256::OUTPUT_SIZE); - const auto hashHexNoUndo = HexStr(hashNoUndo, hashNoUndo + CSHA256::OUTPUT_SIZE); + auto [hashHex, hashHexNoUndo] = GetDVMDBHashes(*pcustomcsview); // Get the current block height const auto height = ::ChainActive().Height(); diff --git a/src/init.cpp b/src/init.cpp index 1def0f90e8..885a5e1f1c 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -2268,6 +2268,11 @@ bool AppInitMain(InitInterfaces& interfaces) } } + { + auto [hashHex, hashHexNoUndo] = GetDVMDBHashes(*pcustomcsview); + LogPrintf("Pre-consolidate rewards for DVM hash: %s hash-no-undo: %s\n", hashHex, hashHexNoUndo); + } + if (fullRewardConsolidation) { LogPrintf("Consolidate rewards for all addresses..\n"); @@ -2301,6 +2306,11 @@ bool AppInitMain(InitInterfaces& interfaces) ConsolidateRewards(*pcustomcsview, ::ChainActive().Height(), ownersToConsolidate, true, skipStatic); } pcustomcsview->Flush(); + + { + auto [hashHex, hashHexNoUndo] = GetDVMDBHashes(*pcustomcsview); + LogPrintf("Post-consolidate rewards for DVM hash: %s hash-no-undo: %s\n", hashHex, hashHexNoUndo); + } } // ********************************************************* Step 12: import blocks