-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow bytecode replacement (#336)
* feat: add option to replace bytecodes * apply earlier * updated comments * cleaned up error handling * clippy
- Loading branch information
Showing
7 changed files
with
358 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
251 changes: 251 additions & 0 deletions
251
example_override/0x8340BF5CE80Ba4ED8C3A18E03567C40d04B05358.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Example override directory | ||
|
||
This directory is an example on how to use the `override-bytecodes-dir` flag. | ||
|
||
You put your bytecodes in this directory, under a proper file name - that should match the address that you're overriding, and run | ||
the era-test-node with `--override-bytecodes-dir=example_override` flag. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use std::fs; | ||
|
||
use crate::fork::ForkSource; | ||
use crate::node::InMemoryNode; | ||
use eyre::Context; | ||
use hex::FromHex; | ||
use serde::Deserialize; | ||
use std::str::FromStr; | ||
use zksync_types::Address; | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct ContractJson { | ||
bytecode: Bytecode, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct Bytecode { | ||
object: String, | ||
} | ||
|
||
// Loads a list of bytecodes and addresses from the directory and then inserts them directly | ||
// into the Node's storage. | ||
pub fn override_bytecodes<T: Clone + ForkSource + std::fmt::Debug>( | ||
node: &InMemoryNode<T>, | ||
bytecodes_dir: String, | ||
) -> eyre::Result<()> { | ||
for entry in fs::read_dir(bytecodes_dir)? { | ||
let entry = entry?; | ||
let path = entry.path(); | ||
|
||
if path.is_file() { | ||
let filename = match path.file_name().and_then(|name| name.to_str()) { | ||
Some(name) => name, | ||
None => eyre::bail!("Invalid filename {}", path.display().to_string()), | ||
}; | ||
|
||
// Look only at .json files. | ||
if let Some(filename) = filename.strip_suffix(".json") { | ||
let address = Address::from_str(filename) | ||
.wrap_err(format!("Cannot parse {} as address", filename))?; | ||
|
||
let file_content = fs::read_to_string(&path)?; | ||
let contract: ContractJson = serde_json::from_str(&file_content) | ||
.wrap_err(format!("Failed to parse json file {:?}", path))?; | ||
|
||
let bytecode = Vec::from_hex(contract.bytecode.object) | ||
.wrap_err(format!("Failed to parse hex from {:?}", path))?; | ||
|
||
node.override_bytecode(&address, &bytecode) | ||
.expect("Failed to override bytecode"); | ||
tracing::info!("+++++ Replacing bytecode at address {:?} +++++", address); | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters