Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replaced IndexSet type of index_to_key_map by VecDeque #58

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/processor/tree/tree_wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
use std::{fs, path::Path, str::FromStr};
use std::{collections::VecDeque, fs, path::Path, str::FromStr};

use blake2::{Blake2s256, Digest};
use ethers::types::{Address, H256, U256};
use eyre::Result;
use indexmap::IndexSet;
use state_reconstruct_fetcher::{constants::storage::INITAL_STATE_PATH, types::CommitBlock};
use zksync_merkle_tree::{Database, MerkleTree, RocksDBWrapper};

use super::RootHash;

pub struct TreeWrapper {
tree: MerkleTree<RocksDBWrapper>,
pub index_to_key_map: IndexSet<U256>,
pub index_to_key_map: VecDeque<U256>,
}

impl TreeWrapper {
/// Attempts to create a new [`TreeWrapper`].
pub fn new(db_path: &Path, mut index_to_key_map: IndexSet<U256>) -> Result<Self> {
pub fn new(db_path: &Path, mut index_to_key_map: VecDeque<U256>) -> Result<Self> {
let db = RocksDBWrapper::new(db_path);
let mut tree = MerkleTree::new(db);

Expand All @@ -42,14 +41,14 @@ impl TreeWrapper {
let value = H256::from(value);

key_value_pairs.push((key, value));
self.index_to_key_map.insert(key);
self.index_to_key_map.push_back(key);
}

// REPEATED CALLDATA.
for (index, value) in &block.repeated_storage_changes {
let index = usize::try_from(*index).expect("truncation failed");
// Index is 1-based so we subtract 1.
let key = *self.index_to_key_map.get_index(index - 1).unwrap();
let key = self.index_to_key_map[index - 1];
let value = H256::from(value);

key_value_pairs.push((key, value));
Expand All @@ -74,7 +73,7 @@ impl TreeWrapper {
/// Attempts to reconstruct the genesis state from a CSV file.
fn reconstruct_genesis_state<D: Database>(
tree: &mut MerkleTree<D>,
index_to_key: &mut IndexSet<U256>,
index_to_key: &mut VecDeque<U256>,
path: &str,
) -> Result<()> {
fn cleanup_encoding(input: &'_ str) -> &'_ str {
Expand Down Expand Up @@ -167,7 +166,7 @@ fn reconstruct_genesis_state<D: Database>(
let key = U256::from_little_endian(&derived_key);
let value = H256::from(tmp);
key_value_pairs.push((key, value));
index_to_key.insert(key);
index_to_key.push_back(key);
}

let output = tree.extend(key_value_pairs);
Expand Down
5 changes: 2 additions & 3 deletions state-reconstruct-fetcher/src/snapshot.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{fs, io, path::Path};
use std::{collections::VecDeque, fs, io, path::Path};

use ethers::types::{U256, U64};
use indexmap::IndexSet;
use serde::{Deserialize, Serialize};

/// Struct containing the fields used for restoring the tree state.
Expand All @@ -12,7 +11,7 @@ pub struct StateSnapshot {
/// The latest l2 block number that was processed.
pub latest_l2_block_number: u64,
/// The mappings of index to key values.
pub index_to_key_map: IndexSet<U256>,
pub index_to_key_map: VecDeque<U256>,
}

impl StateSnapshot {
Expand Down