Skip to content

Commit

Permalink
Add evmhash and oceanahsh to logdbhashes (#3093)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jouzo authored Oct 11, 2024
1 parent bd5782f commit bc7f0e3
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions lib/ain-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ rocksdb.workspace = true
anyhow.workspace = true
num_cpus.workspace = true
log.workspace = true
sha2.workspace = true
hex.workspace = true
19 changes: 19 additions & 0 deletions lib/ain-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use rocksdb::{
IteratorMode, Options, DB,
};
use serde::{de::DeserializeOwned, Serialize};
use sha2::{Digest, Sha256};

pub type Result<T> = result::Result<T, DBError>;

Expand Down Expand Up @@ -168,6 +169,24 @@ impl Rocks {

Ok(())
}

pub fn hash_db_state(&self, cf_names: &[&'static str]) -> Result<String> {
let mut hasher = Sha256::new();

for cf_name in cf_names {
let cf = self.cf_handle(cf_name)?;
let iter = self.0.iterator_cf(cf, rocksdb::IteratorMode::Start);

for item in iter {
let (key, value) = item?;
hasher.update(&key);
hasher.update(&value);
}
}

let result = hasher.finalize();
Ok(hex::encode(result))
}
}

//
Expand Down
4 changes: 4 additions & 0 deletions lib/ain-evm/src/storage/block_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ impl BlockStore {
column: PhantomData,
}
}

pub fn hash_db_state(&self) -> Result<String> {
Ok(self.0.hash_db_state(&COLUMN_NAMES)?)
}
}

impl DBVersionControl for BlockStore {
Expand Down
4 changes: 4 additions & 0 deletions lib/ain-evm/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ impl Storage {
pub fn dump_db(&self, arg: DumpArg, from: Option<&str>, limit: usize) -> Result<String> {
self.blockstore.dump(&arg, from, limit)
}

pub fn hash_db_state(&self) -> Result<String> {
self.blockstore.hash_db_state()
}
}

impl Rollback for Storage {
Expand Down
4 changes: 4 additions & 0 deletions lib/ain-ocean/src/storage/ocean_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ impl OceanStore {
Ok(self.0.dump_table_sizes(&COLUMN_NAMES)?)
}

pub fn hash_db_state(&self) -> Result<String> {
Ok(self.0.hash_db_state(&COLUMN_NAMES)?)
}

pub fn compact(&self) {
self.0.compact();
}
Expand Down
5 changes: 5 additions & 0 deletions lib/ain-rs-exports/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -882,3 +882,8 @@ fn evm_try_unsafe_rename_dst20(
.push_tx_in_block_template(template.get_inner_mut()?, system_tx, native_hash)
}
}

#[ffi_fallible]
fn evm_try_get_hash_db_state() -> Result<String> {
SERVICES.evm.storage.hash_db_state()
}
2 changes: 2 additions & 0 deletions lib/ain-rs-exports/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ pub mod ffi {
native_hash: [u8; 32],
token: DST20TokenInfo,
);
fn evm_try_get_hash_db_state(result: &mut CrossBoundaryResult) -> String;
fn ocean_try_get_hash_db_state(result: &mut CrossBoundaryResult) -> String;
}

// ========= Debug ==========
Expand Down
5 changes: 5 additions & 0 deletions lib/ain-rs-exports/src/ocean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ pub fn ocean_invalidate_block(block_str: String) -> Result<()> {
fn ocean_try_set_tx_result(tx_type: u8, tx_hash: [u8; 32], result_ptr: usize) -> Result<()> {
ain_ocean::tx_result::index(&ain_ocean::SERVICES, tx_type, tx_hash, result_ptr)
}

#[ffi_fallible]
fn ocean_try_get_hash_db_state() -> Result<String> {
ain_ocean::SERVICES.store.hash_db_state()
}
14 changes: 14 additions & 0 deletions src/dfi/rpc_accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <dfi/validation.h>
#include <dfi/vaulthistory.h>
#include <ffi/ffihelpers.h>
#include <ffi/ffiexports.h>
#include <boost/asio.hpp>

#include <fstream>
Expand Down Expand Up @@ -3726,6 +3727,7 @@ UniValue logdbhashes(const JSONRPCRequest &request) {
// Convert hash to hex string
const auto hashHex = HexStr(hash, hash + CSHA256::OUTPUT_SIZE);


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

Expand All @@ -3734,6 +3736,18 @@ UniValue logdbhashes(const JSONRPCRequest &request) {
result.pushKV("height", height);
result.pushKV("dvmhash", hashHex);

const auto evmHashHex = XResultValueLogged(evm_try_get_hash_db_state(result));
if (evmHashHex) {
result.pushKV("evmhash", std::string(*evmHashHex));
}

if (gArgs.GetBoolArg("-oceanarchive", DEFAULT_OCEAN_INDEXER_ENABLED)) {
const auto oceanHashHex = XResultValueLogged(ocean_try_get_hash_db_state(result));
if (oceanHashHex) {
result.pushKV("oceanhash", std::string(*oceanHashHex));
}
}

return result;
}

Expand Down

0 comments on commit bc7f0e3

Please sign in to comment.