Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log DVM hash without undos #3109

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/dfi/masternodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1435,3 +1435,51 @@ void CalcMissingRewardTempFix(CCustomCSView &mnview, const uint32_t targetHeight
}
}
}

std::pair<std::string, std::string> 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};
}
2 changes: 2 additions & 0 deletions src/dfi/masternodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::string> GetDVMDBHashes(CCustomCSView &view);

enum class UpdateMasternodeType : uint8_t {
None = 0x00,
OwnerAddress = 0x01,
Expand Down
37 changes: 2 additions & 35 deletions src/dfi/rpc_accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3694,41 +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 a SHA256 hasher
CSHA256 hasher;

// 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();

// 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());

// Move to the next key-value pair
pcursor->Next();
}

// Delete iterator
delete pcursor;

// Finalize the hash
unsigned char hash[CSHA256::OUTPUT_SIZE];
hasher.Finalize(hash);

// Convert hash to hex string
const auto hashHex = HexStr(hash, hash + CSHA256::OUTPUT_SIZE);
auto [hashHex, hashHexNoUndo] = GetDVMDBHashes(*pcustomcsview);

// Get the current block height
const auto height = ::ChainActive().Height();
Expand All @@ -3737,6 +3703,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) {
Expand Down
10 changes: 10 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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
Expand Down
Loading