-
Notifications
You must be signed in to change notification settings - Fork 360
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reimplementation of jsontests in Apache-2 license (#209)
* Basic state test decoding * Basic run structure * Init in-memory backend * Implement Backend and FullBackend for InMemoryBackend * Init cloned backend * Finish implementation of state root check * Fix cargo fmt
- Loading branch information
Showing
18 changed files
with
876 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,4 +60,5 @@ with-serde = [ | |
[workspace] | ||
members = [ | ||
"interpreter", | ||
"jsontests", | ||
] |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "jsontests" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
evm = { path = ".." } | ||
serde = { version = "1", features = ["derive"] } | ||
serde_json = "1" | ||
primitive-types = { version = "0.12", features = ["rlp", "serde"] } | ||
clap = { version = "4", features = ["derive"] } | ||
thiserror = "1" | ||
hex = { version = "0.4", features = ["serde"] } | ||
ethereum = "0.15.0" | ||
rlp = "0.5" | ||
sha3 = "0.10" |
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,21 @@ | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum TestError { | ||
#[error("state root is different")] | ||
StateMismatch, | ||
} | ||
|
||
#[derive(Error, Debug)] | ||
pub enum Error { | ||
#[error("io error")] | ||
IO(#[from] std::io::Error), | ||
#[error("json error")] | ||
JSON(#[from] serde_json::Error), | ||
#[error("evm error")] | ||
EVM(#[from] evm::ExitError), | ||
#[error("unsupported fork")] | ||
UnsupportedFork, | ||
#[error("test error")] | ||
Test(#[from] TestError), | ||
} |
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,94 @@ | ||
use evm::backend::in_memory::InMemoryBackend; | ||
use evm::utils::h256_to_u256; | ||
use primitive_types::{H256, U256}; | ||
use sha3::{Digest, Keccak256}; | ||
|
||
/// Basic account type. | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub struct TrieAccount { | ||
/// Nonce of the account. | ||
pub nonce: U256, | ||
/// Balance of the account. | ||
pub balance: U256, | ||
/// Storage root of the account. | ||
pub storage_root: H256, | ||
/// Code hash of the account. | ||
pub code_hash: H256, | ||
/// Code version of the account. | ||
pub code_version: U256, | ||
} | ||
|
||
impl rlp::Encodable for TrieAccount { | ||
fn rlp_append(&self, stream: &mut rlp::RlpStream) { | ||
let use_short_version = self.code_version == U256::zero(); | ||
|
||
match use_short_version { | ||
true => { | ||
stream.begin_list(4); | ||
} | ||
false => { | ||
stream.begin_list(5); | ||
} | ||
} | ||
|
||
stream.append(&self.nonce); | ||
stream.append(&self.balance); | ||
stream.append(&self.storage_root); | ||
stream.append(&self.code_hash); | ||
|
||
if !use_short_version { | ||
stream.append(&self.code_version); | ||
} | ||
} | ||
} | ||
|
||
impl rlp::Decodable for TrieAccount { | ||
fn decode(rlp: &rlp::Rlp) -> Result<Self, rlp::DecoderError> { | ||
let use_short_version = match rlp.item_count()? { | ||
4 => true, | ||
5 => false, | ||
_ => return Err(rlp::DecoderError::RlpIncorrectListLen), | ||
}; | ||
|
||
Ok(TrieAccount { | ||
nonce: rlp.val_at(0)?, | ||
balance: rlp.val_at(1)?, | ||
storage_root: rlp.val_at(2)?, | ||
code_hash: rlp.val_at(3)?, | ||
code_version: if use_short_version { | ||
U256::zero() | ||
} else { | ||
rlp.val_at(4)? | ||
}, | ||
}) | ||
} | ||
} | ||
|
||
pub fn state_root(backend: &InMemoryBackend) -> H256 { | ||
let tree = backend | ||
.current_layer() | ||
.state | ||
.iter() | ||
.map(|(address, account)| { | ||
let storage_root = ethereum::util::sec_trie_root( | ||
account | ||
.storage | ||
.iter() | ||
.map(|(k, v)| (k, rlp::encode(&h256_to_u256(*v)))), | ||
); | ||
|
||
let code_hash = H256::from_slice(&Keccak256::digest(&account.code)); | ||
let account = TrieAccount { | ||
nonce: account.nonce, | ||
balance: account.balance, | ||
storage_root, | ||
code_hash, | ||
code_version: U256::zero(), | ||
}; | ||
|
||
(address, rlp::encode(&account)) | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
ethereum::util::sec_trie_root(tree) | ||
} |
Oops, something went wrong.