Skip to content

Commit

Permalink
fix integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
Wollac committed Nov 17, 2023
1 parent 442f22d commit 95111b7
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 33 deletions.
7 changes: 7 additions & 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 testing/ef-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ zeth-lib = { path = "../../lib" }
zeth-primitives = { path = "../../primitives" }

[dev-dependencies]
diff = "0.1"
env_logger = "0.10"
log = "0.4"
risc0-zkvm = { workspace = true, features = ["prove"] }
Expand Down
34 changes: 15 additions & 19 deletions testing/ef-tests/src/ethers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::BTreeSet;

use ethers_core::types::{
Block, Bloom, Bytes, EIP1186ProofResponse, StorageProof, Transaction, H256, U256,
};
Expand All @@ -23,6 +25,7 @@ use super::*;
pub struct TestProvider {
pub state: TestState,
pub header: Header,
pub post: TestState,
}

impl Provider for TestProvider {
Expand Down Expand Up @@ -63,13 +66,18 @@ impl Provider for TestProvider {
}

fn get_proof(&mut self, query: &ProofQuery) -> Result<EIP1186ProofResponse, anyhow::Error> {
assert_eq!(query.block_no, self.header.number);

let indices = query
.indices
.iter()
.map(|idx| LibU256::from_be_bytes(idx.0));
get_proof(from_ethers_h160(query.address), indices, &self.state)

if query.block_no == self.header.number {
get_proof(from_ethers_h160(query.address), indices, &self.state)
} else if query.block_no == self.header.number + 1 {
get_proof(from_ethers_h160(query.address), indices, &self.post)
} else {
panic!("invalid block number: {}", query.block_no)
}
}

fn get_transaction_count(&mut self, query: &AccountQuery) -> Result<U256, anyhow::Error> {
Expand Down Expand Up @@ -125,7 +133,8 @@ impl Provider for TestProvider {
}
}

fn build_tries(state: &TestState) -> (MptNode, HashMap<Address, MptNode>) {
/// Builds the state trie and storage tries from the test state.
pub fn build_tries(state: &TestState) -> (MptNode, HashMap<Address, MptNode>) {
let mut state_trie = MptNode::default();
let mut storage_tries = HashMap::new();
for (address, account) in &state.0 {
Expand Down Expand Up @@ -169,7 +178,8 @@ fn get_proof(
.map(|p| p.into())
.collect();
let mut storage_proof = vec![];
for index in indices {
let index_set = indices.into_iter().collect::<BTreeSet<_>>();
for index in index_set {
let proof = StorageProof {
key: index.to_be_bytes().into(),
proof: mpt_proof(&storage_trie, keccak(index.to_be_bytes::<32>()))?
Expand Down Expand Up @@ -197,17 +207,3 @@ fn get_proof(
storage_proof,
})
}

/// Get EIP-1186 proofs for a set of addresses and storage keys.
pub fn get_state_update_proofs(
provider: &ProviderDb,
storage_keys: HashMap<Address, Vec<LibU256>>,
) -> Result<HashMap<Address, EIP1186ProofResponse>, anyhow::Error> {
let state = provider.into();

let mut result = HashMap::new();
for (address, indices) in storage_keys {
result.insert(address, get_proof(address, indices, &state)?);
}
Ok(result)
}
13 changes: 7 additions & 6 deletions testing/ef-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use zeth_primitives::{
Address, Bloom, Bytes, RlpBytes, StorageKey, B256, B64, U256, U64,
};

use crate::ethers::{get_state_update_proofs, TestProvider};
use crate::ethers::TestProvider;

pub mod ethers;

Expand Down Expand Up @@ -103,7 +103,7 @@ impl From<DbAccount> for TestAccount {
}
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct TestState(pub HashMap<Address, TestAccount>);

Expand Down Expand Up @@ -310,17 +310,19 @@ pub const BIG_STACK_SIZE: usize = 8 * 1024 * 1024;

pub fn create_input(
chain_spec: &ChainSpec,
state: TestState,
parent_header: Header,
parent_state: TestState,
header: Header,
transactions: Vec<TestTransaction>,
withdrawals: Vec<Withdrawal>,
state: TestState,
) -> Input<EthereumTxEssence> {
// create the provider DB
let provider_db = ProviderDb::new(
Box::new(TestProvider {
state,
state: parent_state,
header: parent_header.clone(),
post: state,
}),
parent_header.number,
);
Expand Down Expand Up @@ -355,8 +357,7 @@ pub fn create_input(
let provider_db = builder.mut_db().unwrap();

let parent_proofs = provider_db.get_initial_proofs().unwrap();
let proofs =
get_state_update_proofs(provider_db, provider_db.get_latest_db().storage_keys()).unwrap();
let proofs = provider_db.get_latest_proofs().unwrap();
let ancestor_headers = provider_db.get_ancestor_headers().unwrap();

let preflight_data = Data {
Expand Down
35 changes: 28 additions & 7 deletions testing/ef-tests/tests/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ use std::path::PathBuf;

use rstest::rstest;
use zeth_lib::builder::{BlockBuilderStrategy, EthereumStrategy};
use zeth_primitives::block::Header;
use zeth_primitives::{block::Header, trie::StateAccount};
use zeth_testeth::{
create_input, ethers,
ethtests::{read_eth_test, EthTestCase},
*,
};

#[rstest]
Expand All @@ -42,8 +42,6 @@ fn evm(
chain_spec,
} in read_eth_test(path)
{
let state = json.pre;
let parent_header = genesis;
// only one block supported for now
assert_eq!(json.blocks.len(), 1);
let block = json.blocks.pop().unwrap();
Expand All @@ -58,18 +56,41 @@ fn evm(
let expected_header: Header = block_header.clone().into();
assert_eq!(&expected_header.hash(), &block_header.hash);

// using the empty/default state for the input prepares all accounts for deletion
// this leads to larger input, but can never fail
let post_state = json.post.clone().unwrap_or_default();

let input = create_input(
&chain_spec,
state,
parent_header.clone(),
genesis,
json.pre,
expected_header.clone(),
block.transactions,
block.withdrawals.unwrap_or_default(),
post_state,
);

let (header, _) = EthereumStrategy::build_from(&chain_spec, input).unwrap();
let (header, state) = EthereumStrategy::build_from(&chain_spec, input).unwrap();

if let Some(post) = json.post {
let (exp_state, _) = ethers::build_tries(&post);

println!("diffing state trie:");
for diff in diff::slice(
&state.debug_rlp::<StateAccount>(),
&exp_state.debug_rlp::<StateAccount>(),
) {
match diff {
diff::Result::Left(l) => println!("✗{}", l),
diff::Result::Right(r) => println!("✓{}", r),
diff::Result::Both(l, _) => println!(" {}", l),
}
}
assert_eq!(state.hash(), exp_state.hash());
}

// the headers should match
assert_eq!(header, expected_header);
assert_eq!(header.hash(), expected_header.hash());
}
}
3 changes: 2 additions & 1 deletion testing/ef-tests/tests/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ fn executor(

let input = create_input(
&chain_spec,
json.pre,
genesis,
json.pre,
expected_header.clone(),
block.transactions,
block.withdrawals.unwrap_or_default(),
json.post.unwrap(),
);

let env = ExecutorEnv::builder()
Expand Down

0 comments on commit 95111b7

Please sign in to comment.