Skip to content

Commit

Permalink
Add account hashes to logdbhashes (#3121)
Browse files Browse the repository at this point in the history
* Add account hashes to logdbhashes

* Make naming consistent

---------

Co-authored-by: Prasanna Loganathar <[email protected]>
  • Loading branch information
Bushstar and prasannavl authored Nov 13, 2024
1 parent e58bdbc commit 4e3933b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
20 changes: 15 additions & 5 deletions src/dfi/masternodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1436,7 +1436,7 @@ void CalcMissingRewardTempFix(CCustomCSView &mnview, const uint32_t targetHeight
}
}

std::pair<std::string, std::string> GetDVMDBHashes(CCustomCSView &view) {
std::tuple<std::string, std::string, std::string> GetDVMDBHashes(CCustomCSView &view) {
auto db = view.GetStorage().GetStorageLevelDB()->GetDB();

// Create a CDBIterator
Expand All @@ -1445,6 +1445,7 @@ std::pair<std::string, std::string> GetDVMDBHashes(CCustomCSView &view) {
// Create SHA256 hashers
CSHA256 hasher;
CSHA256 hasherNoUndo;
CSHA256 hasherAccount;

// Seek to the beginning of the database
pcursor->SeekToFirst();
Expand All @@ -1456,9 +1457,15 @@ std::pair<std::string, std::string> GetDVMDBHashes(CCustomCSView &view) {
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());
if (!key.empty()) {
if (key[0] != 'u') {
hasherNoUndo.Write((const unsigned char *)keySlice.data(), keySlice.size());
hasherNoUndo.Write((const unsigned char *)valueSlice.data(), valueSlice.size());
}
if (key[0] == 'a') {
hasherAccount.Write((const unsigned char *)keySlice.data(), keySlice.size());
hasherAccount.Write((const unsigned char *)valueSlice.data(), valueSlice.size());
}
}

hasher.Write((const unsigned char *)keySlice.data(), keySlice.size());
Expand All @@ -1474,12 +1481,15 @@ std::pair<std::string, std::string> GetDVMDBHashes(CCustomCSView &view) {
// Finalize the hashes
unsigned char hash[CSHA256::OUTPUT_SIZE];
unsigned char hashNoUndo[CSHA256::OUTPUT_SIZE];
unsigned char hashAccount[CSHA256::OUTPUT_SIZE];
hasher.Finalize(hash);
hasherNoUndo.Finalize(hashNoUndo);
hasherAccount.Finalize(hashAccount);

// Convert hashes to hex string
const auto hashHex = HexStr(hash, hash + CSHA256::OUTPUT_SIZE);
const auto hashHexNoUndo = HexStr(hashNoUndo, hashNoUndo + CSHA256::OUTPUT_SIZE);
const auto hashHexAccount = HexStr(hashAccount, hashAccount + CSHA256::OUTPUT_SIZE);

return {hashHex, hashHexNoUndo};
return {hashHex, hashHexNoUndo, hashHexAccount};
}
2 changes: 1 addition & 1 deletion src/dfi/masternodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ 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);
std::tuple<std::string, std::string, std::string> GetDVMDBHashes(CCustomCSView &view);

enum class UpdateMasternodeType : uint8_t {
None = 0x00,
Expand Down
3 changes: 2 additions & 1 deletion src/dfi/rpc_accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3697,7 +3697,7 @@ UniValue logdbhashes(const JSONRPCRequest &request) {
pcustomcsview->Flush();
pcustomcsDB->Flush();

auto [hashHex, hashHexNoUndo] = GetDVMDBHashes(*pcustomcsview);
auto [hashHex, hashHexNoUndo, hashHexAccount] = GetDVMDBHashes(*pcustomcsview);

// Get the current block height
const auto height = ::ChainActive().Height();
Expand All @@ -3713,6 +3713,7 @@ UniValue logdbhashes(const JSONRPCRequest &request) {
// - consolidaterewards at different points if pre-static addresses are involved.
result.pushKV("dvmhash", hashHex);
result.pushKV("dvmwithoutundohash", hashHexNoUndo);
result.pushKV("dvmaccounthash", hashHexAccount);

auto res = XResultValueLogged(evm_try_get_latest_block_hash(result));
if (res) {
Expand Down
8 changes: 4 additions & 4 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2251,8 +2251,8 @@ bool AppInitMain(InitInterfaces& interfaces)
}

{
auto [hashHex, hashHexNoUndo] = GetDVMDBHashes(*pcustomcsview);
LogPrintf("Pre-consolidate rewards for DVM hash: %s hash-no-undo: %s\n", hashHex, hashHexNoUndo);
auto [hashHex, hashHexNoUndo, hashHexAccount] = GetDVMDBHashes(*pcustomcsview);
LogPrintf("Pre-consolidate rewards for DVM hash: %s hash-no-undo: %s hash-account: %s\n", hashHex, hashHexNoUndo, hashHexAccount);
}

if (fullRewardConsolidation) {
Expand Down Expand Up @@ -2291,8 +2291,8 @@ bool AppInitMain(InitInterfaces& interfaces)
pcustomcsDB->Flush();

{
auto [hashHex, hashHexNoUndo] = GetDVMDBHashes(*pcustomcsview);
LogPrintf("Post-consolidate rewards for DVM hash: %s hash-no-undo: %s\n", hashHex, hashHexNoUndo);
auto [hashHex, hashHexNoUndo, hashHexAccount] = GetDVMDBHashes(*pcustomcsview);
LogPrintf("Post-consolidate rewards for DVM hash: %s hash-no-undo: %s hash-account: %s\n", hashHex, hashHexNoUndo, hashHexAccount);
}
}

Expand Down
16 changes: 8 additions & 8 deletions test/functional/feature_consolidate_rewards.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ def setup(self):
def pre_fork24_consolidate(self):

# Compare hash before consolidation
hash_0 = self.nodes[0].logdbhashes()["dvmhash_no_undo"]
hash_1 = self.nodes[1].logdbhashes()["dvmhash_no_undo"]
hash_0 = self.nodes[0].logdbhashes()["dvmwithoutundohash"]
hash_1 = self.nodes[1].logdbhashes()["dvmwithoutundohash"]
assert_equal(hash_0, hash_1)

# Generate rewards
Expand Down Expand Up @@ -186,8 +186,8 @@ def pre_fork24_consolidate(self):
self.idGOOGL = list(self.nodes[0].gettoken(self.symbolGOOGL).keys())[0]

# Compare hash before consolidation
hash_0 = self.nodes[0].logdbhashes()["dvmhash_no_undo"]
hash_1 = self.nodes[1].logdbhashes()["dvmhash_no_undo"]
hash_0 = self.nodes[0].logdbhashes()["dvmwithoutundohash"]
hash_1 = self.nodes[1].logdbhashes()["dvmwithoutundohash"]
assert_equal(hash_0, hash_1)

def post_fork24_consolidate(self):
Expand All @@ -200,8 +200,8 @@ def post_fork24_consolidate(self):
self.sync_blocks()

# Compare hash before consolidation
hash_0 = self.nodes[0].logdbhashes()["dvmhash_no_undo"]
hash_1 = self.nodes[1].logdbhashes()["dvmhash_no_undo"]
hash_0 = self.nodes[0].logdbhashes()["dvmwithoutundohash"]
hash_1 = self.nodes[1].logdbhashes()["dvmwithoutundohash"]
assert_equal(hash_0, hash_1)

# Stop node
Expand All @@ -224,8 +224,8 @@ def post_fork24_consolidate(self):
self.sync_blocks()

# Compare hash before consolidation
hash_0 = self.nodes[0].logdbhashes()["dvmhash_no_undo"]
hash_1 = self.nodes[1].logdbhashes()["dvmhash_no_undo"]
hash_0 = self.nodes[0].logdbhashes()["dvmwithoutundohash"]
hash_1 = self.nodes[1].logdbhashes()["dvmwithoutundohash"]
assert_equal(hash_0, hash_1)


Expand Down

0 comments on commit 4e3933b

Please sign in to comment.