Skip to content

Commit

Permalink
Merge pull request #11 from HerodotusDev/compiler
Browse files Browse the repository at this point in the history
blocksample compiler
  • Loading branch information
rkdud007 committed Feb 2, 2024
2 parents 58c0988 + aafe10d commit 2d4e2c1
Show file tree
Hide file tree
Showing 16 changed files with 1,788 additions and 61 deletions.
1,295 changes: 1,283 additions & 12 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ members = [
common = { version = "0.1.0", path = "crates/common" }
types = { version = "0.1.0", path = "crates/types" }
decoder = { version = "0.1.0", path = "crates/decoder" }
fetcher = { version = "0.1.0", path = "crates/fetcher" }
tokio = { version = "1", features = ["full"] }
alloy-dyn-abi = "0.6.2"
alloy-primitives = "0.6.2"
alloy-rlp = { version = "0.3.4", features = ["derive"] }
anyhow = "1.0.79"
reth-primitives = { git = "https://github.com/paradigmxyz/reth", package = "reth-primitives", rev = "01d3df3" }
2 changes: 2 additions & 0 deletions crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ edition = "2021"

[dependencies]
alloy-primitives = { workspace = true }
alloy-rlp = { workspace = true }
anyhow = { workspace = true }
reth-primitives = { workspace = true }
48 changes: 48 additions & 0 deletions crates/common/src/block/account.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::str::FromStr;

use alloy_primitives::{hex, FixedBytes, U256};
use alloy_rlp::{Decodable, Encodable as _, RlpDecodable, RlpEncodable};

#[derive(Debug)]
pub enum AccountField {
Nonce,
Expand All @@ -8,6 +11,41 @@ pub enum AccountField {
CodeHash,
}

#[derive(Debug, RlpDecodable, RlpEncodable, PartialEq)]
pub struct Account {
pub nonce: u64,
pub balance: U256,
pub storage_root: FixedBytes<32>,
pub code_hash: FixedBytes<32>,
}

impl Account {
pub fn new(
nonce: u64,
balance: U256,
storage_root: FixedBytes<32>,
code_hash: FixedBytes<32>,
) -> Self {
Account {
nonce,
balance,
storage_root,
code_hash,
}
}

pub fn rlp_encode(&self) -> String {
let mut buffer = Vec::<u8>::new();
self.encode(&mut buffer);
hex::encode(buffer)
}

pub fn rlp_decode(rlp: &str) -> Self {
let decoded = <Account>::decode(&mut hex::decode(rlp).unwrap().as_slice()).unwrap();
decoded
}
}

impl FromStr for AccountField {
type Err = ();

Expand Down Expand Up @@ -51,3 +89,13 @@ impl AccountField {
}
}
}

pub fn decode_account_field(account_rlp: &str, field: AccountField) -> String {
let decoded = <Account>::decode(&mut hex::decode(account_rlp).unwrap().as_slice()).unwrap();
match field {
AccountField::Nonce => decoded.nonce.to_string(),
AccountField::Balance => decoded.balance.to_string(),
AccountField::StorageRoot => decoded.storage_root.to_string(),
AccountField::CodeHash => decoded.code_hash.to_string(),
}
}
86 changes: 69 additions & 17 deletions crates/common/src/block/header.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use std::str::FromStr;

use alloy_primitives::hex;
use alloy_rlp::Decodable;
use reth_primitives::Header;

#[derive(Debug)]
pub enum HeaderField {
ParentHash,
Expand All @@ -18,6 +22,10 @@ pub enum HeaderField {
MixHash,
Nonce,
BaseFeePerGas,
WithdrawalsRoot,
BlobGasUsed,
ExcessBlobGas,
ParentBeaconBlockRoot,
}

impl HeaderField {
Expand All @@ -39,28 +47,36 @@ impl HeaderField {
13 => Some(HeaderField::MixHash),
14 => Some(HeaderField::Nonce),
15 => Some(HeaderField::BaseFeePerGas),
16 => Some(HeaderField::WithdrawalsRoot),
17 => Some(HeaderField::BlobGasUsed),
18 => Some(HeaderField::ExcessBlobGas),
19 => Some(HeaderField::ParentBeaconBlockRoot),
_ => None,
}
}

pub fn to_index(&self) -> Option<usize> {
pub fn to_index(&self) -> usize {
match self {
HeaderField::ParentHash => Some(0),
HeaderField::OmmerHash => Some(1),
HeaderField::Beneficiary => Some(2),
HeaderField::StateRoot => Some(3),
HeaderField::TransactionsRoot => Some(4),
HeaderField::ReceiptsRoot => Some(5),
HeaderField::LogsBloom => Some(6),
HeaderField::Difficulty => Some(7),
HeaderField::Number => Some(8),
HeaderField::GasLimit => Some(9),
HeaderField::GasUsed => Some(10),
HeaderField::Timestamp => Some(11),
HeaderField::ExtraData => Some(12),
HeaderField::MixHash => Some(13),
HeaderField::Nonce => Some(14),
HeaderField::BaseFeePerGas => Some(15),
HeaderField::ParentHash => 0,
HeaderField::OmmerHash => 1,
HeaderField::Beneficiary => 2,
HeaderField::StateRoot => 3,
HeaderField::TransactionsRoot => 4,
HeaderField::ReceiptsRoot => 5,
HeaderField::LogsBloom => 6,
HeaderField::Difficulty => 7,
HeaderField::Number => 8,
HeaderField::GasLimit => 9,
HeaderField::GasUsed => 10,
HeaderField::Timestamp => 11,
HeaderField::ExtraData => 12,
HeaderField::MixHash => 13,
HeaderField::Nonce => 14,
HeaderField::BaseFeePerGas => 15,
HeaderField::WithdrawalsRoot => 16,
HeaderField::BlobGasUsed => 17,
HeaderField::ExcessBlobGas => 18,
HeaderField::ParentBeaconBlockRoot => 19,
}
}

Expand All @@ -82,6 +98,10 @@ impl HeaderField {
HeaderField::MixHash => "MIX_HASH",
HeaderField::Nonce => "NONCE",
HeaderField::BaseFeePerGas => "BASE_FEE_PER_GAS",
HeaderField::WithdrawalsRoot => "WITHDRAWALS_ROOT",
HeaderField::BlobGasUsed => "BLOB_GAS_USED",
HeaderField::ExcessBlobGas => "EXCESS_BLOB_GAS",
HeaderField::ParentBeaconBlockRoot => "PARENT_BEACON_BLOCK_ROOT",
}
}
}
Expand All @@ -107,7 +127,39 @@ impl FromStr for HeaderField {
"MIX_HASH" => Ok(HeaderField::MixHash),
"NONCE" => Ok(HeaderField::Nonce),
"BASE_FEE_PER_GAS" => Ok(HeaderField::BaseFeePerGas),
"WITHDRAWALS_ROOT" => Ok(HeaderField::WithdrawalsRoot),
"BLOB_GAS_USED" => Ok(HeaderField::BlobGasUsed),
"EXCESS_BLOB_GAS" => Ok(HeaderField::ExcessBlobGas),
"PARENT_BEACON_BLOCK_ROOT" => Ok(HeaderField::ParentBeaconBlockRoot),
_ => Err(()),
}
}
}

pub fn decode_header_field(header_rlp: &str, field: HeaderField) -> String {
let decoded =
<Header as Decodable>::decode(&mut hex::decode(header_rlp).unwrap().as_slice()).unwrap();

match field {
HeaderField::ParentHash => decoded.parent_hash.to_string(),
HeaderField::OmmerHash => decoded.ommers_hash.to_string(),
HeaderField::Beneficiary => decoded.beneficiary.to_string(),
HeaderField::StateRoot => decoded.state_root.to_string(),
HeaderField::TransactionsRoot => decoded.transactions_root.to_string(),
HeaderField::ReceiptsRoot => decoded.receipts_root.to_string(),
HeaderField::LogsBloom => decoded.logs_bloom.to_string(),
HeaderField::Difficulty => decoded.difficulty.to_string(),
HeaderField::Number => decoded.number.to_string(),
HeaderField::GasLimit => decoded.gas_limit.to_string(),
HeaderField::GasUsed => decoded.gas_used.to_string(),
HeaderField::Timestamp => decoded.timestamp.to_string(),
HeaderField::ExtraData => decoded.extra_data.to_string(),
HeaderField::MixHash => decoded.mix_hash.to_string(),
HeaderField::Nonce => decoded.nonce.to_string(),
HeaderField::BaseFeePerGas => decoded.base_fee_per_gas.unwrap().to_string(),
HeaderField::WithdrawalsRoot => decoded.withdrawals_root.unwrap().to_string(),
HeaderField::BlobGasUsed => decoded.blob_gas_used.unwrap().to_string(),
HeaderField::ExcessBlobGas => decoded.excess_blob_gas.unwrap().to_string(),
HeaderField::ParentBeaconBlockRoot => decoded.parent_beacon_block_root.unwrap().to_string(),
}
}
60 changes: 60 additions & 0 deletions crates/common/tests/account.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use std::str::FromStr;

use alloy_primitives::{FixedBytes, U256};
use common::block::account::Account;

#[test]
fn test_get_account_rlp() {
// let account_addr = "0x7b2f05ce9ae365c3dbf30657e2dc6449989e83d6";
let account = Account::new(
1,
U256::from(0),
FixedBytes::from_str("0x1c35dfde2b62d99d3a74fda76446b60962c4656814bdd7815eb6e5b8be1e7185")
.unwrap(),
FixedBytes::from_str("0xcd4f25236fff0ccac15e82bf4581beb08e95e1b5ba89de6031c75893cd91245c")
.unwrap(),
);
let account_rlp = account.rlp_encode();
assert_eq!(account_rlp, "f8440180a01c35dfde2b62d99d3a74fda76446b60962c4656814bdd7815eb6e5b8be1e7185a0cd4f25236fff0ccac15e82bf4581beb08e95e1b5ba89de6031c75893cd91245c");
let account = Account::new(
2,
U256::from(0),
FixedBytes::from_str("0x1c35dfde2b62d99d3a74fda76446b60962c4656814bdd7815eb6e5b8be1e7185")
.unwrap(),
FixedBytes::from_str("0xcd4f25236fff0ccac15e82bf4581beb08e95e1b5ba89de6031c75893cd91245c")
.unwrap(),
);
let account_rlp = account.rlp_encode();
assert_eq!(account_rlp, "f8440280a01c35dfde2b62d99d3a74fda76446b60962c4656814bdd7815eb6e5b8be1e7185a0cd4f25236fff0ccac15e82bf4581beb08e95e1b5ba89de6031c75893cd91245c");
let account = Account::new(
2,
U256::from(0x1),
FixedBytes::from_str("0x1c35dfde2b62d99d3a74fda76446b60962c4656814bdd7815eb6e5b8be1e7185")
.unwrap(),
FixedBytes::from_str("0xcd4f25236fff0ccac15e82bf4581beb08e95e1b5ba89de6031c75893cd91245c")
.unwrap(),
);
let account_rlp = account.rlp_encode();
assert_eq!(account_rlp, "f8440201a01c35dfde2b62d99d3a74fda76446b60962c4656814bdd7815eb6e5b8be1e7185a0cd4f25236fff0ccac15e82bf4581beb08e95e1b5ba89de6031c75893cd91245c");
}

#[test]
fn test_decode_account_rlp() {
let account_rlp = "f8440180a01c35dfde2b62d99d3a74fda76446b60962c4656814bdd7815eb6e5b8be1e7185a0cd4f25236fff0ccac15e82bf4581beb08e95e1b5ba89de6031c75893cd91245c";
let account = Account::rlp_decode(account_rlp);
assert_eq!(
account,
Account::new(
1,
U256::from(0),
FixedBytes::from_str(
"0x1c35dfde2b62d99d3a74fda76446b60962c4656814bdd7815eb6e5b8be1e7185"
)
.unwrap(),
FixedBytes::from_str(
"0xcd4f25236fff0ccac15e82bf4581beb08e95e1b5ba89de6031c75893cd91245c"
)
.unwrap()
)
);
}
39 changes: 39 additions & 0 deletions crates/common/tests/header.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::str::FromStr;

use alloy_primitives::{hex, Address, Bloom, Bytes, FixedBytes, U256};
use alloy_rlp::Decodable;
use reth_primitives::Header;

#[test]
pub fn test_rlp() {
let rlp_hex ="f90266a045adb684cb5458019c496206c1383894c360fe969a1028ba44955eadfa585cc5a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d4934794b636a68f834b4d75af9edc5fb0138bb4758ed293a01db2388923f7c78680b4a46bae725637013d74ad787ec5c861d3ade3df882d81a093586eb5f2781ded334a2a03d178f41dc06f271d7f1ff429e4da6ef42d12a773a0361590775fea7857cc048b9324c03e96f287199803ce1440ff1e12c5c6008049b901000420000a200308000025201005a30400008962800402185dc600144280040082221400010101200458002b0d88008028004206808408400402108f0812246200240a204365100109051c082a020081204200001060440090044044448100082100028001060640c011401a802000090331000408243804009402201240802082820403801141050a4a00208283202050000f10058894008000411050512800220a200000042275800280894080000202460040030000408001ce00282400000002a8c24210000200014a30040015020b04800020608800000850440240c06100011002000000200988001800000880128a050400329081c144080a040800000480839eb0f68401c9c380836f9a8e8465aa87809f496c6c756d696e61746520446d6f63726174697a6520447374726962757465a0c653e1c1cee990147f4439776cc3ead6f175e081998c33c93da41653112e89ce8800000000000000000da039db3f9d1fe0756e5aef4e2f0241ad957e999e49c981809c018425d0080f6cd2830400008405320000a0713ce910d12e99ba96492ff2f6411d4e0a3e567ab419e92e60cf5fc4aa74db7a".to_string();
let rlp = hex::decode(rlp_hex).unwrap();
let decoded = <Header as Decodable>::decode(&mut rlp.as_slice()).unwrap();
let expected_header = Header {
parent_hash:FixedBytes::from_str( "0x45adb684cb5458019c496206c1383894c360fe969a1028ba44955eadfa585cc5").unwrap(),
ommers_hash: FixedBytes::from_str( "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347").unwrap(),
beneficiary: Address::from_str("0xb636a68f834b4d75af9edc5fb0138bb4758ed293").unwrap(),
state_root:FixedBytes::from_str( "0x1db2388923f7c78680b4a46bae725637013d74ad787ec5c861d3ade3df882d81"
).unwrap(),
transactions_root: FixedBytes::from_str("0x93586eb5f2781ded334a2a03d178f41dc06f271d7f1ff429e4da6ef42d12a773"
).unwrap(),
receipts_root: FixedBytes::from_str("0x361590775fea7857cc048b9324c03e96f287199803ce1440ff1e12c5c6008049"
).unwrap(),
logs_bloom:Bloom::from_str("0x0420000a200308000025201005a30400008962800402185dc600144280040082221400010101200458002b0d88008028004206808408400402108f0812246200240a204365100109051c082a020081204200001060440090044044448100082100028001060640c011401a802000090331000408243804009402201240802082820403801141050a4a00208283202050000f10058894008000411050512800220a200000042275800280894080000202460040030000408001ce00282400000002a8c24210000200014a30040015020b04800020608800000850440240c06100011002000000200988001800000880128a050400329081c144080a0408000004").unwrap(),
difficulty: U256::from(0x0),
number: 0x9eb0f6u64,
gas_limit: 0x1c9c380u64,
gas_used: 0x6f9a8eu64,
timestamp: 0x65aa8780u64,
extra_data: Bytes::from_str("0x496c6c756d696e61746520446d6f63726174697a6520447374726962757465").unwrap(),
mix_hash: FixedBytes::from_str("0xc653e1c1cee990147f4439776cc3ead6f175e081998c33c93da41653112e89ce").unwrap(),
nonce:0x0u64,
base_fee_per_gas: Some(13),
withdrawals_root: Some(FixedBytes::from_str("0x39db3f9d1fe0756e5aef4e2f0241ad957e999e49c981809c018425d0080f6cd2").unwrap()),
blob_gas_used: Some(0x40000u64),
excess_blob_gas: Some(0x5320000u64),
parent_beacon_block_root: Some(FixedBytes::from_str("0x713ce910d12e99ba96492ff2f6411d4e0a3e567ab419e92e60cf5fc4aa74db7a").unwrap()),
};

assert_eq!(decoded, expected_header);
}
3 changes: 3 additions & 0 deletions crates/common/tests/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod account;
pub mod header;
pub mod utils;
Loading

0 comments on commit 2d4e2c1

Please sign in to comment.