Skip to content

Commit bfe14e6

Browse files
authored
Generate hash of DVM and EVM DBs (#3084)
1 parent 8ad98ec commit bfe14e6

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

src/dfi/rpc_accounts.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -3674,6 +3674,69 @@ UniValue logdvmstate(const JSONRPCRequest &request) {
36743674
return {};
36753675
}
36763676

3677+
UniValue logdbhashes(const JSONRPCRequest &request) {
3678+
RPCHelpMan{
3679+
"logdbhashes",
3680+
"Hash the DVM and EVM databases.\n",
3681+
{},
3682+
RPCResult{"{\n"
3683+
" \"height\": xxxxx, (numeric) The current block height\n"
3684+
" \"dvmhash\": \"hash\" (string) The SHA256 hash of the database dump files\n"
3685+
"}\n"},
3686+
RPCExamples{HelpExampleCli("logdbhashes", "")},
3687+
}
3688+
.Check(request);
3689+
3690+
LOCK(cs_main);
3691+
3692+
// Flush any pending changes to the DB. Not always written to disk.
3693+
pcustomcsview->Flush();
3694+
pcustomcsDB->Flush();
3695+
3696+
// Get the CDBWrapper instance from CCustomCSView
3697+
auto db = pcustomcsview->GetStorage().GetStorageLevelDB()->GetDB();
3698+
3699+
// Create a CDBIterator
3700+
auto pcursor = db->NewIterator(leveldb::ReadOptions());
3701+
3702+
// Create a SHA256 hasher
3703+
CSHA256 hasher;
3704+
3705+
// Seek to the beginning of the database
3706+
pcursor->SeekToFirst();
3707+
3708+
// Iterate over all key-value pairs
3709+
while (pcursor->Valid()) {
3710+
// Get the key and value slices
3711+
auto keySlice = pcursor->GetKey();
3712+
auto valueSlice = pcursor->GetValue();
3713+
3714+
// Feed the key and value into the hasher
3715+
hasher.Write((const unsigned char *)keySlice.data(), keySlice.size());
3716+
hasher.Write((const unsigned char *)valueSlice.data(), valueSlice.size());
3717+
3718+
// Move to the next key-value pair
3719+
pcursor->Next();
3720+
}
3721+
3722+
// Finalize the hash
3723+
unsigned char hash[CSHA256::OUTPUT_SIZE];
3724+
hasher.Finalize(hash);
3725+
3726+
// Convert hash to hex string
3727+
const auto hashHex = HexStr(hash, hash + CSHA256::OUTPUT_SIZE);
3728+
3729+
// Get the current block height
3730+
const auto height = ::ChainActive().Height();
3731+
3732+
// Prepare result
3733+
UniValue result(UniValue::VOBJ);
3734+
result.pushKV("height", height);
3735+
result.pushKV("dvmhash", hashHex);
3736+
3737+
return result;
3738+
}
3739+
36773740
static const CRPCCommand commands[] = {
36783741
// category name actor (function) params
36793742
// ------------- ------------------------ ---------------------- ----------
@@ -3706,6 +3769,7 @@ static const CRPCCommand commands[] = {
37063769
{"accounts", "getlockedtokens", &getlockedtokens, {"address"} },
37073770
{"accounts", "releaselockedtokens", &releaselockedtokens, {"releasePart"} },
37083771
{"hidden", "logdvmstate", &logdvmstate, {"size"} },
3772+
{"hidden", "logdbhashes", &logdbhashes, {""} },
37093773
};
37103774

37113775
void RegisterAccountsRPCCommands(CRPCTable &tableRPC) {

0 commit comments

Comments
 (0)