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

Generate hash of DVM and EVM DBs #3084

Merged
merged 1 commit into from
Oct 9, 2024
Merged
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
64 changes: 64 additions & 0 deletions src/dfi/rpc_accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3674,6 +3674,69 @@ UniValue logdvmstate(const JSONRPCRequest &request) {
return {};
}

UniValue logdbhashes(const JSONRPCRequest &request) {
RPCHelpMan{
"logdbhashes",
"Hash the DVM and EVM databases.\n",
{},
RPCResult{"{\n"
" \"height\": xxxxx, (numeric) The current block height\n"
" \"dvmhash\": \"hash\" (string) The SHA256 hash of the database dump files\n"
"}\n"},
RPCExamples{HelpExampleCli("logdbhashes", "")},
}
.Check(request);

LOCK(cs_main);

// Flush any pending changes to the DB. Not always written to disk.
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();
}

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

// Get the current block height
const auto height = ::ChainActive().Height();

// Prepare result
UniValue result(UniValue::VOBJ);
result.pushKV("height", height);
result.pushKV("dvmhash", hashHex);

return result;
}

static const CRPCCommand commands[] = {
// category name actor (function) params
// ------------- ------------------------ ---------------------- ----------
Expand Down Expand Up @@ -3706,6 +3769,7 @@ static const CRPCCommand commands[] = {
{"accounts", "getlockedtokens", &getlockedtokens, {"address"} },
{"accounts", "releaselockedtokens", &releaselockedtokens, {"releasePart"} },
{"hidden", "logdvmstate", &logdvmstate, {"size"} },
{"hidden", "logdbhashes", &logdbhashes, {""} },
};

void RegisterAccountsRPCCommands(CRPCTable &tableRPC) {
Expand Down
Loading