Skip to content

Commit

Permalink
Use bincode for merged block hashes file
Browse files Browse the repository at this point in the history
  • Loading branch information
i1i1 committed Dec 12, 2024
1 parent 4d34ccf commit 4ca1949
Show file tree
Hide file tree
Showing 29 changed files with 30 additions and 55 deletions.
35 changes: 20 additions & 15 deletions bins/fetch/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
collections::BTreeMap,
error::Error,
fs::{self, File},
io::{BufReader, Write},
io::BufReader,
num::NonZeroUsize,
};

Expand Down Expand Up @@ -72,8 +72,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
serde_json::to_writer(block_file, &block)
.map_err(|err| format!("Failed to write block to file: {err}"))?;

// Populate bytecodes and state from RPC storage.
let mut state = BTreeMap::<Address, EvmAccount>::new();
// TODO: Deduplicate logic with [for_each_block_from_disk] when there is more usage
let mut bytecodes: BTreeMap<B256, EvmCode> = match File::open("data/bytecodes.bincode.gz") {
Ok(compressed_file) => {
Expand All @@ -83,6 +81,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
Err(_) => BTreeMap::new(),
};
bytecodes.extend(storage.get_cache_bytecodes());

// Populate bytecodes and state from RPC storage.
let mut state = BTreeMap::<Address, EvmAccount>::new();
for (address, mut account) in storage.get_cache_accounts() {
if let Some(code) = account.code.take() {
let code_hash = account
Expand All @@ -95,28 +96,32 @@ async fn main() -> Result<(), Box<dyn Error>> {
}

// Write compressed bytecodes to disk.
let file_bytecodes = File::create("data/bytecodes.bincode.gz")
let writer_bytecodes = File::create("data/bytecodes.bincode.gz")
.map(|f| GzEncoder::new(f, Compression::default()))
.map_err(|err| format!("Failed to create compressed bytecodes file: {err}"))?;
let serialized_bytecodes = bincode::serialize(&bytecodes)
.map_err(|err| format!("Failed to serialize bytecodes to bincode: {err}"))?;
GzEncoder::new(file_bytecodes, Compression::default())
.write_all(&serialized_bytecodes)
bincode::serialize_into(writer_bytecodes, &bytecodes)
.map_err(|err| format!("Failed to write bytecodes to file: {err}"))?;

// Write pre-state to disk.
let file_state = File::create(format!("{block_dir}/pre_state.json"))
.map_err(|err| format!("Failed to create pre-state file: {err}"))?;
let json_state = serde_json::to_value(&state)
.map_err(|err| format!("Failed to serialize pre-state to JSON: {err}"))?;
serde_json::to_writer(file_state, &json_state)
serde_json::to_writer(file_state, &state)
.map_err(|err| format!("Failed to write pre-state to file: {err}"))?;

// Write block hashes to disk.
let block_hashes: BTreeMap<u64, B256> = storage.get_cache_block_hashes().into_iter().collect();
// TODO: Deduplicate logic with [for_each_block_from_disk] when there is more usage
let mut block_hashes = match File::open("data/block_hashes.bincode") {
Ok(compressed_file) => bincode::deserialize_from(compressed_file)
.map_err(|err| format!("Failed to deserialize bytecodes from file: {err}"))?,
Err(_) => BTreeMap::<u64, B256>::new(),
};

block_hashes.extend(storage.get_cache_block_hashes());

if !block_hashes.is_empty() {
let file = File::create(format!("{block_dir}/block_hashes.json"))
// Write compressed block hashes to disk
let file = File::create("data/block_hashes.bincode")
.map_err(|err| format!("Failed to create block hashes file: {err}"))?;
serde_json::to_writer(file, &block_hashes)
bincode::serialize_into(file, &block_hashes)
.map_err(|err| format!("Failed to write block hashes to file: {err}"))?;
}

Expand Down
24 changes: 10 additions & 14 deletions crates/pevm/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ use alloy_primitives::{Address, Bytes, PrimitiveSignature, TxKind, B256, U256};
use alloy_rpc_types_eth::{Block, BlockTransactions, Header};
use flate2::bufread::GzDecoder;
use hashbrown::HashMap;
use pevm::{
chain::PevmChain, BlockHashes, BuildSuffixHasher, Bytecodes, EvmAccount, InMemoryStorage,
};
use pevm::{chain::PevmChain, BlockHashes, BuildSuffixHasher, EvmAccount, InMemoryStorage};

/// runner module
pub mod runner;
Expand All @@ -32,19 +30,22 @@ pub fn for_each_block_from_disk(mut handler: impl FnMut(Block, InMemoryStorage<'
let data_dir = std::path::PathBuf::from("../../data");

// TODO: Deduplicate logic with [bin/fetch.rs] when there is more usage
let bytecodes: Bytecodes = bincode::deserialize_from(GzDecoder::new(BufReader::new(
let bytecodes = bincode::deserialize_from(GzDecoder::new(BufReader::new(
File::open(data_dir.join("bytecodes.bincode.gz")).unwrap(),
)))
.unwrap();

let block_hashes = bincode::deserialize_from::<_, BlockHashes>(BufReader::new(
File::open(data_dir.join("block_hashes.bincode")).unwrap(),
))
.unwrap();

for block_path in fs::read_dir(data_dir.join("blocks")).unwrap() {
let block_path = block_path.unwrap().path();
let block_number = block_path.file_name().unwrap().to_str().unwrap();

let block_dir = data_dir.join("blocks").join(block_number);
let block_dir = data_dir.join("blocks").join(block_path);

// Parse block
let block: Block = serde_json::from_reader(BufReader::new(
let block = serde_json::from_reader(BufReader::new(
File::open(block_dir.join("block.json")).unwrap(),
))
.unwrap();
Expand All @@ -55,14 +56,9 @@ pub fn for_each_block_from_disk(mut handler: impl FnMut(Block, InMemoryStorage<'
)
.unwrap();

// Parse block hashes
let block_hashes: BlockHashes = File::open(block_dir.join("block_hashes.json"))
.map(|file| serde_json::from_reader::<_, BlockHashes>(BufReader::new(file)).unwrap())
.unwrap_or_default();

handler(
block,
InMemoryStorage::new(accounts, Some(&bytecodes), block_hashes),
InMemoryStorage::new(accounts, Some(&bytecodes), block_hashes.clone()),
);
}
}
Expand Down
Binary file added data/block_hashes.bincode
Binary file not shown.
1 change: 0 additions & 1 deletion data/blocks/11114732/block_hashes.json

This file was deleted.

1 change: 0 additions & 1 deletion data/blocks/12047794/block_hashes.json

This file was deleted.

1 change: 0 additions & 1 deletion data/blocks/12159808/block_hashes.json

This file was deleted.

1 change: 0 additions & 1 deletion data/blocks/12459406/block_hashes.json

This file was deleted.

1 change: 0 additions & 1 deletion data/blocks/12964999/block_hashes.json

This file was deleted.

1 change: 0 additions & 1 deletion data/blocks/13217637/block_hashes.json

This file was deleted.

1 change: 0 additions & 1 deletion data/blocks/14029313/block_hashes.json

This file was deleted.

1 change: 0 additions & 1 deletion data/blocks/14334629/block_hashes.json

This file was deleted.

1 change: 0 additions & 1 deletion data/blocks/14383540/block_hashes.json

This file was deleted.

1 change: 0 additions & 1 deletion data/blocks/15199017/block_hashes.json

This file was deleted.

1 change: 0 additions & 1 deletion data/blocks/15537393/block_hashes.json

This file was deleted.

1 change: 0 additions & 1 deletion data/blocks/19444337/block_hashes.json

This file was deleted.

1 change: 0 additions & 1 deletion data/blocks/19606599/block_hashes.json

This file was deleted.

1 change: 0 additions & 1 deletion data/blocks/19807137/block_hashes.json

This file was deleted.

1 change: 0 additions & 1 deletion data/blocks/19860366/block_hashes.json

This file was deleted.

1 change: 0 additions & 1 deletion data/blocks/19923400/block_hashes.json

This file was deleted.

1 change: 0 additions & 1 deletion data/blocks/19932703/block_hashes.json

This file was deleted.

1 change: 0 additions & 1 deletion data/blocks/19933597/block_hashes.json

This file was deleted.

1 change: 0 additions & 1 deletion data/blocks/2462997/block_hashes.json

This file was deleted.

1 change: 0 additions & 1 deletion data/blocks/4864590/block_hashes.json

This file was deleted.

1 change: 0 additions & 1 deletion data/blocks/5283152/block_hashes.json

This file was deleted.

1 change: 0 additions & 1 deletion data/blocks/5526571/block_hashes.json

This file was deleted.

1 change: 0 additions & 1 deletion data/blocks/7279999/block_hashes.json

This file was deleted.

1 change: 0 additions & 1 deletion data/blocks/7280000/block_hashes.json

This file was deleted.

1 change: 0 additions & 1 deletion data/blocks/8889776/block_hashes.json

This file was deleted.

1 change: 0 additions & 1 deletion data/blocks/9069000/block_hashes.json

This file was deleted.

0 comments on commit 4ca1949

Please sign in to comment.