Skip to content

Commit

Permalink
feat: dump_declared_classes can dump for a specific block range
Browse files Browse the repository at this point in the history
  • Loading branch information
Yael-Starkware committed Oct 17, 2023
1 parent 5e611ab commit 2ff9ce9
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/dump_declared_classes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ license-file.workspace = true

[dependencies]
papyrus_storage = { path = "../papyrus_storage", version = "0.0.5" }
clap.workspace = true
52 changes: 47 additions & 5 deletions crates/dump_declared_classes/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,56 @@
use papyrus_storage::utils::dump_declared_classes_table_to_file;
use clap::{Arg, Command};
use papyrus_storage::utils::{
dump_declared_classes_table_by_block_range,
dump_declared_classes_table_to_file,
};
use papyrus_storage::StorageResult;

/// This executable dumps the declared_classes table from the storage to a file.
/// The file path can be passed as an argument, otherwise it will be dumped to
/// "dump_declared_classes.json".
/// A starting block and an ending block can also be passed as optional arguments, otherwise the
/// entire table will be dumped.
fn main() {
let args = std::env::args().collect::<Vec<_>>();
let default_file_path = "dump_declared_classes.json".to_string();
let file_path = args.get(1).unwrap_or(&default_file_path);
let matches = Command::new("Dump declared classes")
.arg(
Arg::new("file_path")
.short('f')
.long("file_path")
.default_value("dump_declared_classes.json")
.help("The file path to dump the declared classes table to."),
)
.arg(
Arg::new("start_block")
.short('s')
.long("start_block")
.help("The block number to start dumping from."),
)
.arg(
Arg::new("end_block")
.short('e')
.long("end_block")
.help("The block number to end dumping at."),
)
.get_matches();

match dump_declared_classes_table_to_file(file_path) {
let file_path = matches.get_one::<String>("file_path").unwrap().as_str();
let res: StorageResult<()> =
if matches.contains_id("start_block") && matches.contains_id("end_block") {
let start_block = matches
.get_one::<String>("start_block")
.unwrap()
.parse::<u64>()
.expect("Failed parsing start_block");
let end_block = matches
.get_one::<String>("end_block")
.unwrap()
.parse::<u64>()
.expect("Failed parsing end_block");
dump_declared_classes_table_by_block_range(start_block, end_block, file_path)
} else {
dump_declared_classes_table_to_file(file_path)
};
match res {
Ok(_) => println!("Dumped declared_classes table to file: {}", file_path),
Err(e) => println!("Failed dumping declared_classes table with error: {}", e),
}
Expand Down
35 changes: 35 additions & 0 deletions crates/papyrus_storage/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ mod utils_test;
use std::fs::File;
use std::io::{BufWriter, Write};

use starknet_api::block::BlockNumber;

use crate::db::serialization::StorageSerde;
use crate::db::{DbIter, TableIdentifier, RO};
use crate::state::StateStorageReader;
use crate::{open_storage, StorageConfig, StorageResult, StorageTxn};

/// Dumps a table from the storage to a file in JSON format.
Expand Down Expand Up @@ -46,3 +49,35 @@ pub fn dump_declared_classes_table_to_file(file_path: &str) -> StorageResult<()>
dump_table_to_file(&txn, &txn.tables.declared_classes, file_path)?;
Ok(())
}

/// Dumps the declared_classes at a given block range from the storage to a file.
pub fn dump_declared_classes_table_by_block_range(
start_block: u64,
end_block: u64,
file_path: &str,
) -> StorageResult<()> {
let storage_config = StorageConfig::default();
let (storage_reader, _) = open_storage(storage_config)?;
let txn = storage_reader.begin_ro_txn()?;
let table_handle = txn.txn.open_table(&txn.tables.declared_classes)?;
let file = File::create(file_path)?;
let mut writer = BufWriter::new(file);
writer.write_all(b"[")?;
let mut first = true;
for block_number in start_block..=end_block {
println!("block_number: {}", block_number);
if let Some(thin_state_diff) = txn.get_state_diff(BlockNumber(block_number))? {
for (class_hash, _) in thin_state_diff.declared_classes.iter() {
if let Some(contract_class) = table_handle.get(&txn.txn, class_hash)? {
if !first {
writer.write_all(b",")?;
}
serde_json::to_writer(&mut writer, &(class_hash, &contract_class))?;
first = false;
}
}
};
}
writer.write_all(b"]")?;
Ok(())
}

0 comments on commit 2ff9ce9

Please sign in to comment.