Skip to content

Commit

Permalink
Merge pull request #10 from 0xPolygonZero/trie_stats
Browse files Browse the repository at this point in the history
Trie stats, query params, and better usage docs
  • Loading branch information
Nashtare committed Jun 13, 2024
2 parents 52ee951 + c52ec99 commit 98d47e7
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 22 deletions.
64 changes: 51 additions & 13 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[workspace]
members = [ "common", "trie_diff", "trie_query"]
members = [ "common", "trie_diff", "trie_query", "trie_stats" ]
resolver = "2"

[workspace.dependencies]
anyhow = { version = "1.0.75", features = ["backtrace"] }
clap = { version = "4.4.6", features = ["derive", "env"] }
mpt_trie = { version = "0.1.0", git = "https://github.com/0xPolygonZero/zk_evm.git", branch = "robins_not_so_awesome_branch_for_brendan" }
mpt_trie = "0.3.0"
pretty_env_logger = "0.5.0"
trace_decoder = { version = "0.1.0", git = "https://github.com/0xPolygonZero/zk_evm.git", branch = "robins_not_so_awesome_branch_for_brendan" }
trace_decoder = "0.4.0"
serde_json = "1.0.114"
1 change: 1 addition & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { workspace = true }
mpt_trie = { workspace = true }
hex = "0.4.3"

Expand Down
9 changes: 8 additions & 1 deletion common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@ use trace_decoder::{
trace_protocol::TrieCompact,
};

pub const TRIE_PATH_DOC_STR: &str = r#"
Each path must point to a file that is either:
- (`*.compact`) Compact encoding (hex string) of an MPT trie (spec: https://gist.github.com/mandrigin/ff7eccf30d0ef9c572bafcb0ab665cff).
- (`*.json`) A serialized `HashedPartialTrie`.
"#;

pub fn read_input_from_file(p: &Path) -> HashedPartialTrie {
match p
.extension()
.map(|s| s.to_str().unwrap_or_default())
.unwrap_or_default()
{
// For now, if we ever get compact, we're going to just use the state trie.
"compact" => {
let out = process_compact_prestate_debug(read_compact_from_file(p)).unwrap();
out.witness_out.tries.state
out.witness_out.state_trie
}
"json" => read_json_trie_from_file(p),
_ => panic!(
Expand Down
6 changes: 5 additions & 1 deletion trie_diff/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use std::path::PathBuf;

use clap::Parser;
use common::read_input_from_file;
use common::{read_input_from_file, TRIE_PATH_DOC_STR};
use mpt_trie::debug_tools::diff::create_diff_between_tries;
use mpt_trie::partial_trie::PartialTrie;

#[derive(Debug, Parser)]
#[command(after_help = TRIE_PATH_DOC_STR)]
/// Attempts to find the lowest possible point where two given tries differ.
///
/// Takes in two paths to tries that we want to include in the diff.
struct ProgArgs {
a_path: PathBuf,
b_path: PathBuf,
Expand Down
41 changes: 37 additions & 4 deletions trie_query/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,51 @@
use std::path::PathBuf;

use clap::Parser;
use common::read_input_from_file;
use common::{read_input_from_file, TRIE_PATH_DOC_STR};
use mpt_trie::{
debug_tools::query::{get_path_from_query, DebugQueryParamsBuilder},
nibbles::Nibbles,
};

const QUERY_CONFIG_STR: &str = "Query Config";

/// Displays a "trace" of a key query for a given key, where the trace contains the nodes encountered.
#[derive(Debug, Parser)]
#[command(after_help = TRIE_PATH_DOC_STR)]
struct ProgArgs {
/// Path to the trie to query against.
trie_path: PathBuf,

/// The key to query in hex format (with or without the "0x").
key: Nibbles,

/// Config for the query output.
#[command(flatten)]
config: QueryConfig,
}

#[derive(Parser, Debug)]
struct QueryConfig {
/// Include the key piece (if the node contains a piece of the key) for each node.
#[arg(short = 'k', long, default_value_t = true, help_heading = QUERY_CONFIG_STR)]
include_key_piece_per_node: bool,

/// Include the node types in the trace.
#[arg(short = 't', long, default_value_t = true, help_heading = QUERY_CONFIG_STR)]
include_node_type: bool,

/// Include additional info that is specific to a given node type.
#[arg(short = 's', long, default_value_t = false, help_heading = QUERY_CONFIG_STR)]
include_node_specific_values: bool,
}

impl From<QueryConfig> for DebugQueryParamsBuilder {
fn from(v: QueryConfig) -> Self {
DebugQueryParamsBuilder::default()
.print_key_pieces(v.include_key_piece_per_node)
.print_node_type(v.include_node_type)
.print_node_specific_values(v.include_node_specific_values)
}
}

fn main() -> anyhow::Result<()> {
Expand All @@ -19,9 +54,7 @@ fn main() -> anyhow::Result<()> {
let p_args = ProgArgs::parse();
let trie = read_input_from_file(&p_args.trie_path);

let query = DebugQueryParamsBuilder::default()
.print_node_specific_values(true)
.build(p_args.key);
let query = DebugQueryParamsBuilder::from(p_args.config).build(p_args.key);
let res = get_path_from_query(&trie, query);

println!("{}", res);
Expand Down
14 changes: 14 additions & 0 deletions trie_stats/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "trie_stats"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
common = { path = "../common" }

anyhow = { workspace = true }
clap = { workspace = true }
mpt_trie ={ workspace = true }
pretty_env_logger = { workspace = true }
47 changes: 47 additions & 0 deletions trie_stats/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use std::path::PathBuf;

use clap::Parser;
use common::{read_input_from_file, TRIE_PATH_DOC_STR};
use mpt_trie::debug_tools::stats::get_trie_stats_with_name;

/// Analyze one or two tries and output stats.
#[derive(Debug, Parser)]
#[command(after_help = TRIE_PATH_DOC_STR)]
struct ProgArgs {
/// The primary trie to compare against.
trie_path: PathBuf,

/// If a second trie is provided, a comparison will be made between the two tries along with printout out their individual stats.
other_trie_path: Option<PathBuf>,
}

fn main() {
pretty_env_logger::init();

let p_args = ProgArgs::parse();
let trie = read_input_from_file(&p_args.trie_path);
let trie_name = p_args
.trie_path
.file_name()
.map(|os_str| os_str.to_string_lossy())
.unwrap_or("Trie".into());

let stats = get_trie_stats_with_name(&trie, trie_name.to_string());
println!("{}", stats);

if let Some(other_trie_path) = p_args.other_trie_path {
// A second trie was passed in. Print its stats and also do a comparison.
let other_trie = read_input_from_file(&other_trie_path);
let other_trie_name = other_trie_path
.file_name()
.map(|os_str| os_str.to_string_lossy())
.unwrap_or("Trie".into());
let other_trie_stats = get_trie_stats_with_name(&other_trie, other_trie_name.into());

println!("{}", other_trie_stats);

let comparison = stats.compare(&other_trie_stats);

println!("{}", comparison);
}
}

0 comments on commit 98d47e7

Please sign in to comment.