Skip to content

Commit

Permalink
dev: use from_reader and to_writer_pretty from serde_json, remove…
Browse files Browse the repository at this point in the history
… reading from memory to string
  • Loading branch information
Harsh Bajpai authored and Harsh Bajpai committed Sep 13, 2023
1 parent 7654705 commit dd2b5c0
Show file tree
Hide file tree
Showing 4 changed files with 352 additions and 27 deletions.
28 changes: 15 additions & 13 deletions crates/hive-utils/src/hive/genesis.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::HashMap;
use std::fs;
use std::fs::File;
use std::io::Error as IoError;
use std::path::Path;

Expand Down Expand Up @@ -51,7 +51,8 @@ pub struct HiveGenesisConfig {

impl HiveGenesisConfig {
pub fn from_file(path: &str) -> Result<Self> {
Ok(serde_json::from_str(&fs::read_to_string(path)?)?)
let hive_genesis_file = File::open(path).unwrap();
Ok(serde_json::from_reader(hive_genesis_file).unwrap())
}
}

Expand All @@ -73,7 +74,7 @@ lazy_static! {
pub async fn serialize_hive_to_madara_genesis_config(
hive_genesis: HiveGenesisConfig,
mut madara_loader: GenesisLoader,
combined_genesis: &Path,
combined_genesis_path: &Path,
compiled_path: &Path,
) -> Result<(), IoError> {
// Compute the class hash of Kakarot contracts
Expand Down Expand Up @@ -224,10 +225,10 @@ pub async fn serialize_hive_to_madara_genesis_config(
madara_loader.storage.push(is_initialized);
});

// Serialize the loader to a string
let madara_genesis_str = serde_json::to_string_pretty(&madara_loader)?;
// Write the string to a file
fs::write(combined_genesis, madara_genesis_str)?;
let combined_genesis_file =
File::options().create_new(true).write(true).append(true).open(combined_genesis_path).unwrap();
// Serialize the loader to a string and then write to a file
serde_json::to_writer_pretty(combined_genesis_file, &madara_loader)?;

Ok(())
}
Expand All @@ -252,6 +253,7 @@ pub struct AccountInfo {

#[cfg(test)]
mod tests {
use std::fs::File;
use std::str::FromStr;

use reth_primitives::U256;
Expand Down Expand Up @@ -303,21 +305,21 @@ mod tests {
let hive_genesis = HiveGenesisConfig::from_file("./src/test_data/hive_genesis.json").unwrap();
let madara_loader =
serde_json::from_str::<GenesisLoader>(std::include_str!("../test_data/madara_genesis.json")).unwrap();
let combined_genesis = Path::new("./src/test_data/combined_genesis.json");
let combined_genesis_path = Path::new("./src/test_data/combined_genesis.json");
let compiled_path = Path::new("./cairo-contracts/build");

// When
serialize_hive_to_madara_genesis_config(hive_genesis, madara_loader, combined_genesis, compiled_path)
serialize_hive_to_madara_genesis_config(hive_genesis, madara_loader, combined_genesis_path, compiled_path)
.await
.unwrap();

let combined_genesis_file = File::open(combined_genesis_path).unwrap();

// Then
let combined_genesis = fs::read_to_string("./src/test_data/combined_genesis.json").unwrap();
let loader: GenesisLoader =
serde_json::from_str(&combined_genesis).expect("Failed to read combined_genesis.json");
let loader: GenesisLoader = serde_json::from_reader(combined_genesis_file).unwrap();
assert_eq!(9 + 3 + 7, loader.contracts.len()); // 9 original + 3 Kakarot contracts + 7 hive

// After
fs::remove_file("./src/test_data/combined_genesis.json").unwrap();
std::fs::remove_file(combined_genesis_path).unwrap();
}
}
7 changes: 4 additions & 3 deletions crates/test-utils/src/bin/dump-katana.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::fs::File;

use ethers::abi::Token;
use git2::{Repository, SubmoduleIgnore};
Expand Down Expand Up @@ -35,11 +36,11 @@ async fn main() {
.dump_state()
.expect("Failed to call dump_state on Katana state");

let state = serde_json::to_string(&dump_state).expect("Failed to serialize state");

// Dump the state
std::fs::create_dir_all(".katana/").expect("Failed to create Kakata dump dir");
std::fs::write(".katana/dump.json", state).expect("Failed to write dump to .katana/dump.json");
let katana_dump_file =
File::options().create_new(true).read(true).write(true).append(true).open(".katana/dump.json").unwrap();
serde_json::to_writer(katana_dump_file, &dump_state);

Check failure on line 43 in crates/test-utils/src/bin/dump-katana.rs

View workflow job for this annotation

GitHub Actions / Linters / clippy

unused `std::result::Result` that must be used

let deployer_account = DeployerAccount {
address: test_context.client().deployer_account().address(),
Expand Down
21 changes: 10 additions & 11 deletions crates/test-utils/src/deploy_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::HashMap;
use std::fs::{self};
use std::fs::{self, File};
use std::path::{Path, PathBuf};
use std::sync::Arc;

Expand Down Expand Up @@ -103,12 +103,10 @@ pub fn get_contract(filename: &str) -> CompactContractBytecode {
let compiled_solidity_path = std::path::Path::new(&foundry_default_out).join(dot_sol).join(dot_json);
let compiled_solidity_path_from_root = root_project_path!(&compiled_solidity_path);

// Read the content of the file
let contents = fs::read_to_string(compiled_solidity_path_from_root).unwrap_or_else(|_| {
let compiled_solidity_file = File::open(compiled_solidity_path_from_root).unwrap_or_else(|_| {
panic!("Could not read file: {}. please run `make setup` to ensure solidity files are compiled", filename)
});

serde_json::from_str(&contents).unwrap()
serde_json::from_reader(compiled_solidity_file).unwrap()
}

/// Encodes a contract's bytecode and constructor arguments into deployable bytecode.
Expand Down Expand Up @@ -773,11 +771,12 @@ impl KakarotTestEnvironmentContext {
let sequencer = construct_kakarot_test_sequencer().await;

// Get root path
let mut dir = root_project_path!(".katana/dump.json");
let mut katana_dump_path = root_project_path!(".katana/dump.json");

// Load the dumped state into the sequencer
let state = std::fs::read_to_string(dir.clone()).expect("Failed to read Katana dump");
let state: SerializableState = serde_json::from_str(&state).expect("Failed to deserialize Katana dump");
let katana_dump_file = File::open(&katana_dump_path).unwrap();
let state: SerializableState =
serde_json::from_reader(katana_dump_file).expect("Failed to deserialize Katana dump");

let mut sequencer_state = sequencer.sequencer.backend.state.write().await;
sequencer_state.load_state(state).expect("Failed to load dumped state into sequencer");
Expand All @@ -787,9 +786,9 @@ impl KakarotTestEnvironmentContext {
sequencer.sequencer.backend.mine_empty_block().await;

// Load the dumped contracts
dir.pop();
dir.push("contracts.json");
let contracts = std::fs::read(dir).expect("Failed to read contracts");
katana_dump_path.pop();
katana_dump_path.push("contracts.json");
let contracts = std::fs::read(katana_dump_path).expect("Failed to read contracts");
let contracts: HashMap<&str, serde_json::Value> =
serde_json::from_slice(&contracts).expect("Failed to deserialize contracts");

Expand Down
Loading

0 comments on commit dd2b5c0

Please sign in to comment.