feat: reth binary and parsing of eth chain tests (#38)

* Scaffolding for reth test binary. wip for blockchain tests

* wip models for chain json tests

* reth binary and chain test

* fmt,clippy

* Update bin/reth/src/lib.rs

Co-authored-by: Bjerg <onbjerg@users.noreply.github.com>

* Update bin/reth/src/lib.rs

Co-authored-by: Bjerg <onbjerg@users.noreply.github.com>

* Update bin/reth/src/test_eth_chain/mod.rs

Co-authored-by: Bjerg <onbjerg@users.noreply.github.com>

* Update bin/reth/src/test_eth_chain/mod.rs

Co-authored-by: Bjerg <onbjerg@users.noreply.github.com>

* Update bin/reth/src/test_eth_chain/mod.rs

Co-authored-by: Bjerg <onbjerg@users.noreply.github.com>

* Update bin/reth/src/test_eth_chain/models.rs

Co-authored-by: Bjerg <onbjerg@users.noreply.github.com>

* Update bin/reth/src/test_eth_chain/models.rs

Co-authored-by: Bjerg <onbjerg@users.noreply.github.com>

* Update bin/reth/src/test_eth_chain/mod.rs

Co-authored-by: Bjerg <onbjerg@users.noreply.github.com>

* Update bin/reth/src/test_eth_chain/runner.rs

Co-authored-by: Bjerg <onbjerg@users.noreply.github.com>

* Move JsonU256 to primitives

* fmt

* Use eyre

* nits

Co-authored-by: Bjerg <onbjerg@users.noreply.github.com>
This commit is contained in:
rakita
2022-10-12 17:43:01 +02:00
committed by GitHub
parent 08d8a7f030
commit ae5935e6b2
16 changed files with 789 additions and 18 deletions

View File

@ -12,4 +12,4 @@ reth-primitives = { path = "../primitives" }
reth-interfaces = { path = "../interfaces" }
async-trait = "0.1.57"
thiserror = "1.0.37"
anyhow = "1.0"
eyre = "0.6.8"

View File

@ -10,4 +10,8 @@ description = "Commonly used types in reth."
[dependencies]
fastrlp = { version = "0.1.3" }
ethers-core = { git = "https://github.com/gakonst/ethers-rs", default-features = false }
bytes = "1.2"
bytes = "1.2"
serde = "1.0"
[dev-dependencies]
serde_json = "1.0"

View File

@ -1,4 +1,4 @@
use crate::{Header, HeaderLocked, Receipt, Transaction, TransactionSigned};
use crate::{Header, HeaderLocked, Receipt, Transaction, TransactionSigned, H256};
use std::ops::Deref;
/// Ethereum full block.
@ -30,6 +30,13 @@ pub struct BlockLocked {
pub receipts: Vec<Receipt>,
}
impl BlockLocked {
/// Header hash.
pub fn hash(&self) -> H256 {
self.header.hash()
}
}
impl Deref for BlockLocked {
type Target = Header;
fn deref(&self) -> &Self::Target {

View File

@ -0,0 +1,94 @@
use crate::U256;
use serde::{
de::{Error, Visitor},
Deserialize, Deserializer, Serialize, Serializer,
};
use std::{fmt, str::FromStr};
/// Wrapper around primitive U256 type to handle edge cases of json parser
#[derive(Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
pub struct JsonU256(pub U256);
impl Serialize for JsonU256 {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.0.to_string().serialize(serializer)
}
}
impl<'a> Deserialize<'a> for JsonU256 {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'a>,
{
deserializer.deserialize_any(JsonU256Visitor)
}
}
struct JsonU256Visitor;
impl<'a> Visitor<'a> for JsonU256Visitor {
type Value = JsonU256;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "a hex encoding or decimal number")
}
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where
E: Error,
{
Ok(JsonU256(U256::from(value)))
}
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: Error,
{
let value = match value.len() {
0 => U256::from(0),
2 if value.starts_with("0x") => U256::zero(),
_ if value.starts_with("0x") => U256::from_str(&value[2..]).map_err(|e| {
Error::custom(format!("Parsin JsonU256 as hex failed {}: {}", value, e))
})?,
_ => U256::from_dec_str(value).map_err(|e| {
Error::custom(format!("Parsin JsonU256 as decimal failed {}: {:?}", value, e))
})?,
};
Ok(JsonU256(value))
}
fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
where
E: Error,
{
self.visit_str(value.as_ref())
}
}
#[cfg(test)]
mod test {
use super::JsonU256;
use crate::U256;
use serde_json;
#[test]
fn jsonu256_deserialize() {
let deserialized: Vec<JsonU256> =
serde_json::from_str(r#"["","0", "0x","10",10,"0x10"]"#).unwrap();
assert_eq!(
deserialized,
vec![
JsonU256(U256::from(0)),
JsonU256(U256::from(0)),
JsonU256(U256::from(0)),
JsonU256(U256::from(10)),
JsonU256(U256::from(10)),
JsonU256(U256::from(16)),
]
);
}
}

View File

@ -11,6 +11,7 @@ mod account;
mod block;
mod chain;
mod header;
mod jsonu256;
mod log;
mod receipt;
mod transaction;
@ -19,6 +20,7 @@ pub use account::Account;
pub use block::{Block, BlockLocked};
pub use chain::Chain;
pub use header::{Header, HeaderLocked};
pub use jsonu256::JsonU256;
pub use log::Log;
pub use receipt::Receipt;
pub use transaction::{AccessList, AccessListItem, Transaction, TransactionSigned, TxType};