Skip to content

Commit

Permalink
ref: include batch number in root hash query (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeapoz authored Oct 24, 2023
1 parent a2356ec commit 2296a66
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
6 changes: 2 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::{
};

use clap::Parser;
use cli::{Cli, Command, Query, ReconstructSource};
use cli::{Cli, Command, ReconstructSource};
use constants::storage;
use eyre::Result;
use snapshot::StateSnapshot;
Expand Down Expand Up @@ -131,9 +131,7 @@ async fn main() -> Result<()> {
};

let tree = QueryTree::new(&db_path);
let result = match query {
Query::RootHash => tree.latest_root_hash(),
};
let result = tree.query(&query);

if json {
println!("{}", serde_json::to_string(&result)?);
Expand Down
30 changes: 26 additions & 4 deletions src/processor/tree/query_tree.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
use std::path::Path;
use std::{fmt, path::Path};

use serde::Serialize;
use zksync_merkle_tree::{MerkleTree, RocksDBWrapper};

use super::RootHash;
use crate::cli::Query;

#[derive(Serialize)]
pub struct RootHashQuery {
pub batch: u64,
pub root_hash: String,
}

impl fmt::Display for RootHashQuery {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Batch: {}\nRoot Hash: {}", self.batch, self.root_hash)
}
}

pub struct QueryTree<'a>(MerkleTree<'a, RocksDBWrapper>);

Expand All @@ -16,7 +29,16 @@ impl QueryTree<'static> {
Self(tree)
}

pub fn latest_root_hash(&self) -> RootHash {
self.0.latest_root_hash()
pub fn query(&self, query: &Query) -> RootHashQuery {
match query {
Query::RootHash => self.query_root_hash(),
}
}

fn query_root_hash(&self) -> RootHashQuery {
RootHashQuery {
batch: self.0.latest_version().unwrap_or_default(),
root_hash: hex::encode(self.0.latest_root_hash()),
}
}
}

0 comments on commit 2296a66

Please sign in to comment.