Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use serde_json for serialization #13

Open
wants to merge 4 commits into
base: 0.1.0-delendum
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
685 changes: 429 additions & 256 deletions Cargo.lock

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ zstd-sys = { git = "https://github.com/lita-xyz/zstd-rs.git", branch = "zstd-sys
[dependencies]
valida-rs = { git = "https://github.com/lita-xyz/valida-rs.git", branch = "main" }
revm = { version = "14.0.0", features = ["std"], default-features = false }
reth-primitives = { git = "https://github.com/lita-xyz/reth.git", branch = "1.0.6-delendum" }
reth-chainspec = { git = "https://github.com/lita-xyz/reth.git", branch = "1.0.6-delendum" }
reth-trie-common = { git = "https://github.com/lita-xyz/reth.git", branch = "1.0.6-delendum" }
reth-primitives = { git = "https://github.com/lita-xyz/reth.git", branch = "1.1.1-delendum" }
reth-chainspec = { git = "https://github.com/lita-xyz/reth.git", branch = "1.1.1-delendum" }
reth-trie-common = { git = "https://github.com/lita-xyz/reth.git", branch = "1.1.1-delendum" }
alloy-rlp = { version = "0.3", default-features = false }
alloy-rlp-derive = { version = "0.3", default-features = false }
alloy-rpc-types = { git = "https://github.com/alloy-rs/alloy" }
alloy-rpc-types = "0.5.4"
alloy-consensus = "0.5.4"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.132"
thiserror = "1.0"
alloy-primitives = { version = "0.8.3", default-features = false, features = [
"rlp",
Expand All @@ -31,4 +33,4 @@ rlp = "0.5.2"
anyhow = "1.0.75"
hashbrown = "0.14.3"
hex = "0.4.3"
getrandom = "0.2.15"
getrandom = "0.2.15"
15 changes: 12 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![no_main]

use reth_primitives::B256;
use alloy_primitives::B256;
use revm::InMemoryDB;
use reth_valida::primitives::db::InMemoryDBHelper;
use reth_valida::primitives::mpt::keccak;
Expand All @@ -10,13 +10,22 @@ use reth_valida::primitives::ValidaRethInput;
valida_rs::entrypoint!(main);

pub fn main() {
let mut input = match valida_rs::io::read_and_deserialize::<ValidaRethInput>() {
let vec = match valida_rs::io::read() {
Ok(vec) => vec,
Err(e) => {
valida_rs::io::println(&format!("Error reading input: {}", e));
return;
}
};

let mut input = match serde_json::de::from_slice::<ValidaRethInput>(&vec.as_slice()) {
Ok(val) => val,
Err(e) => {
valida_rs::io::println(&format!("Error reading/deserializing input: {}", e));
valida_rs::io::println(&format!("Error deserializing input: {}", e));
return;
}
};

// Initialize the database.
let db = match InMemoryDB::initialize(&mut input) {
Ok(val) => val,
Expand Down
4 changes: 3 additions & 1 deletion src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ pub mod processor;
use crate::primitives::mpt::MptNode;
use crate::primitives::mpt::StorageEntry;

use reth_primitives::{Address, Bytes, Header, TransactionSignedNoHash, Withdrawal, B256};
use alloy_primitives::{Address, Bytes, B256};
use alloy_rpc_types::Withdrawal;
use reth_primitives::{Header, TransactionSignedNoHash};
use revm::primitives::HashMap;
use serde::{Deserialize, Serialize};

Expand Down
118 changes: 42 additions & 76 deletions src/primitives/alloy2reth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,91 +18,70 @@
//
//! Common conversions between Alloy and Reth types.

use alloy_rpc_types::AccessListItem as AlloyAccessListItem;
use alloy_rpc_types::Header as AlloyHeader;
use alloy_consensus::{TxLegacy, TxEip2930, TxEip1559};
use alloy_consensus::Transaction as AlloyTransactionTrait;
use alloy_primitives::U256;
use alloy_primitives::Signature as RethSignature;
use alloy_rpc_types::Signature as AlloySignature;
use alloy_rpc_types::Transaction as AlloyTransaction;
use alloy_rpc_types::Withdrawal as AlloyWithdrawal;
use reth_primitives::AccessListItem as RethAccessListItem;
use alloy_rpc_types::AccessList;
use reth_primitives::Header as RethHeader;
use reth_primitives::Signature as RethSignature;
use reth_primitives::TransactionSignedNoHash as RethTransaction;
use reth_primitives::Withdrawal as RethWithdrawal;
use reth_primitives::U256;
use reth_primitives::{Transaction, TransactionSignedNoHash, TxEip1559, TxEip2930, TxLegacy};
use reth_primitives::{Transaction, TransactionSignedNoHash};

/// A trait to convert from Alloy types to Reth types.
pub trait IntoReth<T> {
fn into_reth(self) -> T;
}

impl IntoReth<RethWithdrawal> for AlloyWithdrawal {
fn into_reth(self) -> RethWithdrawal {
RethWithdrawal {
index: self.index,
validator_index: self.validator_index,
amount: self.amount,
address: self.address,
}
}
}

impl IntoReth<RethTransaction> for AlloyTransaction {
fn into_reth(self) -> RethTransaction {
let tx_type: u64 = self.transaction_type.unwrap_or(0).into();
let inner_tx = match tx_type {
0 => Transaction::Legacy(TxLegacy {
chain_id: self.chain_id,
nonce: self.nonce,
gas_price: self.gas_price.unwrap(),
gas_limit: self.gas.try_into().unwrap(),
to: match self.to {
None => reth_primitives::TxKind::Create,
Some(to) => reth_primitives::TxKind::Call(to),
},
value: self.value,
input: self.input,
0 => Transaction::Legacy(TxLegacy {
chain_id: self.chain_id(),
nonce: self.nonce(),
gas_price: self.gas_price().unwrap(),
gas_limit: self.gas_limit().try_into().unwrap(),
to: self.kind(),
value: self.value(),
input: self.input().clone(),
}),
1 => Transaction::Eip2930(TxEip2930 {
chain_id: self.chain_id.unwrap(),
nonce: self.nonce,
gas_price: self.gas_price.unwrap(),
gas_limit: self.gas.try_into().unwrap(),
to: match self.to {
None => reth_primitives::TxKind::Create,
Some(to) => reth_primitives::TxKind::Call(to),
},
value: self.value,
input: self.input,
access_list: reth_primitives::AccessList(
self.access_list
chain_id: self.chain_id().unwrap(),
nonce: self.nonce(),
gas_price: self.gas_price().unwrap(),
gas_limit: self.gas_limit().try_into().unwrap(),
to: self.kind(),
value: self.value(),
input: self.input().clone(),
access_list: AccessList(
self.access_list()
.unwrap()
.iter()
.map(|item| item.clone().into_reth())
.map(|item| item.clone())
.collect(),
),
}),
2 => Transaction::Eip1559(TxEip1559 {
chain_id: self.chain_id.unwrap(),
nonce: self.nonce,
max_fee_per_gas: self.max_fee_per_gas.unwrap(),
max_priority_fee_per_gas: self.max_priority_fee_per_gas.unwrap(),
gas_limit: self.gas.try_into().unwrap(),
to: match self.to {
None => reth_primitives::TxKind::Create,
Some(to) => reth_primitives::TxKind::Call(to),
},
value: self.value,
input: self.input,
access_list: reth_primitives::AccessList(
self.access_list
chain_id: self.chain_id().unwrap(),
nonce: self.nonce(),
max_fee_per_gas: self.max_fee_per_gas(),
max_priority_fee_per_gas: self.max_priority_fee_per_gas().unwrap(),
gas_limit: self.gas_limit().try_into().unwrap(),
to: self.kind(),
value: self.value(),
input: self.input().clone(),
access_list: AccessList(
self.access_list()
.unwrap()
.iter()
.map(|item| item.clone().into_reth())
.map(|item| item.clone())
.collect(),
),
}),
_ => panic!("invalid tx type: {}", tx_type),
_ => panic!("invalid tx type"),
};
TransactionSignedNoHash {
signature: self.signature.unwrap().into_reth(),
Expand All @@ -111,15 +90,6 @@ impl IntoReth<RethTransaction> for AlloyTransaction {
}
}

impl IntoReth<RethAccessListItem> for AlloyAccessListItem {
fn into_reth(self) -> RethAccessListItem {
RethAccessListItem {
address: self.address,
storage_keys: self.storage_keys,
}
}
}

impl IntoReth<RethSignature> for AlloySignature {
fn into_reth(self) -> RethSignature {
// TODO: should be chain_id * 2 + 35.
Expand All @@ -129,20 +99,16 @@ impl IntoReth<RethSignature> for AlloySignature {
self.v
};

RethSignature {
r: self.r,
s: self.s,
odd_y_parity: recovery_id == U256::from(1),
}
RethSignature::from_rs_and_parity(self.r, self.s, recovery_id == U256::from(1)).unwrap()
}
}

impl IntoReth<RethHeader> for AlloyHeader {
fn into_reth(self) -> RethHeader {
RethHeader {
parent_hash: self.parent_hash.0.into(),
ommers_hash: self.ommers_hash.0.into(),
beneficiary: self.beneficiary.0.into(),
ommers_hash: self.uncles_hash.0.into(),
beneficiary: self.miner.0.into(),
state_root: self.state_root.0.into(),
transactions_root: self.transactions_root.0.into(),
receipts_root: self.receipts_root.0.into(),
Expand All @@ -154,13 +120,13 @@ impl IntoReth<RethHeader> for AlloyHeader {
gas_used: self.gas_used.try_into().unwrap(),
timestamp: self.timestamp,
extra_data: self.extra_data.0.clone().into(),
mix_hash: self.mix_hash,
nonce: u64::from_be_bytes(self.nonce.0),
mix_hash: self.mix_hash.unwrap(),
nonce: self.nonce.unwrap(),
base_fee_per_gas: Some(self.base_fee_per_gas.unwrap().try_into().unwrap()),
blob_gas_used: self.blob_gas_used.map(|x| x.try_into().unwrap()),
excess_blob_gas: self.excess_blob_gas.map(|x| x.try_into().unwrap()),
parent_beacon_block_root: self.parent_beacon_block_root,
requests_root: self.requests_hash,
requests_hash: self.requests_hash,
}
}
}
3 changes: 1 addition & 2 deletions src/primitives/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ use crate::primitives::ValidaRethInput;

use anyhow::{anyhow, Result};
// use hashbrown::hash_map::Entry;
use reth_primitives::Bytes;
use reth_primitives::{Address, B256, U256};
use alloy_primitives::{Address, Bytes, B256, U256};
use revm::db::AccountState;
use revm::db::DbAccount;
use revm::db::InMemoryDB;
Expand Down
3 changes: 1 addition & 2 deletions src/primitives/mpt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ use rlp::{Decodable, DecoderError, Prototype, Rlp};
use serde::{Deserialize, Serialize};
use thiserror::Error as ThisError;

use alloy_primitives::{TxNumber, U256};
use alloy_primitives::{Address, TxNumber, U256};
use alloy_rlp_derive::{RlpDecodable, RlpEncodable, RlpMaxEncodedLen};
use alloy_rpc_types::EIP1186AccountProofResponse;
use anyhow::Context;
use anyhow::Result;
use reth_primitives::Address;

pub type StorageEntry = (MptNode, Vec<U256>);

Expand Down
11 changes: 6 additions & 5 deletions src/primitives/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
use crate::primitives::mpt::{keccak, RlpBytes, StateAccount};
use crate::primitives::ValidaRethInput;

use alloy_primitives::{Address, Bloom, TxKind, U256};
use alloy_consensus::Transaction as AlloyTransactionTrait;
use alloy_rlp::BufMut;
use alloy_rpc_types::AccessListItem;
use anyhow::anyhow;
use reth_primitives::bytes::BufMut;
use reth_trie_common::root::ordered_trie_root_with_encoder;
use reth_primitives::{AccessListItem, Address, Bloom, Transaction, TxKind, TransactionSigned};
use reth_primitives::{Transaction, TransactionSigned};
use reth_primitives::{Receipt, ReceiptWithBloom};
use reth_primitives::{Header, U256};
use reth_primitives::Header;
use reth_chainspec::BaseFeeParams;
use revm::db::AccountState;
use revm::db::InMemoryDB;
Expand Down Expand Up @@ -316,8 +319,6 @@ impl EvmProcessor<InMemoryDB> {
// Update state trie root in header.
let header = self.header.as_mut().expect("Header not initialized");
header.state_root = state_trie.hash();

println!("{:?}", header);
}
}

Expand Down