test: fix hive tests (#1530)

Co-authored-by: lambdaclass-user <github@lambdaclass.com>
Co-authored-by: Dan Cline <6798349+Rjected@users.noreply.github.com>
This commit is contained in:
Francisco Krause Arnim
2023-02-28 22:35:37 -03:00
committed by GitHub
parent 4285186dbd
commit 4839adeaef
3 changed files with 175 additions and 113 deletions

View File

@@ -298,7 +298,7 @@ impl From<EthersGenesis> for ChainSpec {
}
/// A helper type for compatibility with geth's config
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(untagged)]
pub enum AllGenesisFormats {
/// The geth genesis format
@@ -583,13 +583,12 @@ impl ForkCondition {
#[cfg(test)]
mod tests {
use revm_primitives::U256;
use crate::{
Chain, ChainSpec, ChainSpecBuilder, ForkCondition, ForkHash, ForkId, Genesis, Hardfork,
Head, GOERLI, MAINNET, SEPOLIA,
AllGenesisFormats, Chain, ChainSpec, ChainSpecBuilder, ForkCondition, ForkHash, ForkId,
Genesis, Hardfork, Head, GOERLI, H256, MAINNET, SEPOLIA,
};
use ethers_core::types as EtherType;
use revm_primitives::U256;
fn test_fork_ids(spec: &ChainSpec, cases: &[(Head, ForkId)]) {
for (block, expected_id) in cases {
let computed_id = spec.fork_id(block);
@@ -1028,4 +1027,87 @@ mod tests {
&ForkCondition::Timestamp(0)
);
}
#[test]
fn hive_geth_json() {
let hive_json = r#"
{
"nonce": "0x0000000000000042",
"difficulty": "0x2123456",
"mixHash": "0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef1234",
"coinbase": "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"timestamp": "0x123456",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0xfafbfcfd",
"gasLimit": "0x2fefd8",
"alloc": {
"dbdbdb2cbd23b783741e8d7fcf51e459b497e4a6": {
"balance": "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
},
"e6716f9544a56c530d868e4bfbacb172315bdead": {
"balance": "0x11",
"code": "0x12"
},
"b9c015918bdaba24b4ff057a92a3873d6eb201be": {
"balance": "0x21",
"storage": {
"0x0000000000000000000000000000000000000000000000000000000000000001": "0x22"
}
},
"1a26338f0d905e295fccb71fa9ea849ffa12aaf4": {
"balance": "0x31",
"nonce": "0x32"
},
"0000000000000000000000000000000000000001": {
"balance": "0x41"
},
"0000000000000000000000000000000000000002": {
"balance": "0x51"
},
"0000000000000000000000000000000000000003": {
"balance": "0x61"
},
"0000000000000000000000000000000000000004": {
"balance": "0x71"
}
},
"config": {
"ethash": {},
"chainId": 10,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0
}
}
"#;
let genesis = serde_json::from_str::<AllGenesisFormats>(hive_json).unwrap();
let chainspec: ChainSpec = genesis.into();
assert_eq!(chainspec.genesis_hash, None);
assert_eq!(Chain::Named(EtherType::Chain::Optimism), chainspec.chain);
let expected_state_root: H256 =
hex_literal::hex!("9a6049ac535e3dc7436c189eaa81c73f35abd7f282ab67c32944ff0301d63360")
.into();
assert_eq!(chainspec.genesis_header().state_root, expected_state_root);
let hard_forks = vec![
Hardfork::Byzantium,
Hardfork::Homestead,
Hardfork::Istanbul,
Hardfork::Petersburg,
Hardfork::Constantinople,
];
for ref fork in hard_forks {
assert_eq!(chainspec.hardforks.get(fork).unwrap(), &ForkCondition::Block(0));
}
let expected_hash: H256 =
hex_literal::hex!("5ae31c6522bd5856129f66be3d582b842e4e9faaa87f21cce547128339a9db3c")
.into();
let hash = chainspec.genesis_header().hash_slow();
assert_eq!(hash, expected_hash);
}
}

View File

@@ -6,9 +6,8 @@ use crate::{
utils::serde_helpers::deserialize_stringified_u64,
Address, Bytes, H256, KECCAK_EMPTY, U256,
};
use bytes::BytesMut;
use ethers_core::utils::GenesisAccount as EthersGenesisAccount;
use reth_rlp::{length_of_length, Encodable, Header as RlpHeader};
use reth_rlp::{encode_fixed_size, length_of_length, Encodable, Header as RlpHeader};
use serde::{Deserialize, Serialize};
use triehash::sec_trie_root;
@@ -62,7 +61,8 @@ impl GenesisAccount {
// rather than rlp-encoding the storage, we just return the length of a single hash
// hashes are a fixed size, so it is safe to use the empty root for this
len += EMPTY_ROOT.length();
len += self.code.as_ref().map_or(KECCAK_EMPTY, keccak256).length();
// we are encoding a hash, so let's just use the length of the empty hash for the code hash
len += KECCAK_EMPTY.length();
len
}
}
@@ -83,13 +83,14 @@ impl Encodable for GenesisAccount {
self.storage
.as_ref()
.map_or(EMPTY_ROOT, |storage| {
if storage.is_empty() {
return EMPTY_ROOT
}
let storage_values =
storage.iter().filter(|(_k, &v)| v != KECCAK_EMPTY).map(|(&k, v)| {
let mut value_rlp = BytesMut::new();
v.encode(&mut value_rlp);
(k, value_rlp.freeze())
let value = U256::from_be_bytes(**v);
(k, encode_fixed_size(&value))
});
sec_trie_root::<KeccakHasher, _, _, _>(storage_values)
})
.encode(out);