Skip to content

Commit c9dc9b4

Browse files
committed
refactor
1 parent 5ce8918 commit c9dc9b4

File tree

7 files changed

+709
-555
lines changed

7 files changed

+709
-555
lines changed

crates/primitives/Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ workspace = true
1515
[dependencies]
1616
alloy-consensus = { workspace = true, features = ["k256"] }
1717
alloy-eips.workspace = true
18+
alloy-rpc-types-debug.workspace = true
1819
alloy-rpc-types-eth.workspace = true
1920
alloy-serde.workspace = true
2021

21-
alloy-eip2930 = { workspace = true, features = ["rkyv"] }
22-
2322
alloy-rlp.workspace = true
2423

2524
alloy-primitives.workspace = true

crates/primitives/src/lib.rs

+67-111
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
//! Stateless Block Verifier primitives library.
22
3+
extern crate core;
4+
5+
use core::fmt;
6+
37
/// Predeployed contracts
48
#[cfg(feature = "scroll")]
59
pub mod predeployed;
610
/// Types definition
711
pub mod types;
812

913
pub use alloy_consensus;
10-
pub use alloy_consensus::Transaction;
11-
pub use alloy_primitives;
12-
pub use alloy_primitives::{Address, B256, U256};
1314

14-
/// Blanket trait for block trace extensions.
15-
pub trait Block: std::fmt::Debug {
16-
/// transaction type
17-
type Tx: Transaction;
15+
pub use alloy_primitives;
16+
pub use alloy_primitives::{Address, BlockHash, B256, U256};
1817

18+
/// BlockHeader trait
19+
pub trait BlockHeader: fmt::Debug {
1920
/// Hash of the block
20-
fn block_hash(&self) -> B256;
21+
fn hash(&self) -> BlockHash;
2122
/// State root hash
2223
fn state_root(&self) -> B256;
2324
/// Difficulty
@@ -46,108 +47,63 @@ pub trait Block: std::fmt::Debug {
4647
fn base_fee_per_gas(&self) -> Option<u64>;
4748
/// Withdrawals root hash added by EIP-4895 and is ignored in legacy headers.
4849
fn withdraw_root(&self) -> B256;
49-
/// Block Transactions
50-
fn transactions(&self) -> impl Iterator<Item = &Self::Tx>;
51-
/// Number of transactions
52-
fn num_txs(&self) -> usize;
5350
}
5451

55-
#[cfg(feature = "scroll")]
56-
pub trait BlockScrollExt: Block {
57-
/// start l1 queue index
58-
fn start_l1_queue_index(&self) -> u64;
59-
60-
/// Number of l1 transactions
61-
#[inline]
62-
fn num_l1_txs(&self) -> u64 {
63-
// 0x7e is l1 tx
64-
match self
65-
.transactions()
66-
.filter(|tx| tx.is_l1_tx())
67-
// tx.nonce for l1 tx is the l1 queue index, which is a globally index,
68-
// not per user as suggested by the name...
69-
.map(|tx| tx.nonce())
70-
.max()
71-
{
72-
None => 0, // not l1 tx in this block
73-
Some(end_l1_queue_index) => end_l1_queue_index - self.start_l1_queue_index() + 1,
74-
}
75-
}
76-
77-
/// Number of l2 transactions
78-
#[inline]
79-
fn num_l2_txs(&self) -> u64 {
80-
// 0x7e is l1 tx
81-
self.transactions().filter(|tx| !tx.is_l1_tx()).count() as u64
82-
}
83-
84-
/// Hash the header of the block
85-
#[inline]
86-
fn hash_da_header(&self, hasher: &mut impl tiny_keccak::Hasher) {
87-
let num_txs = (self.num_l1_txs() + self.num_l2_txs()) as u16;
88-
hasher.update(&self.number().to_be_bytes());
89-
hasher.update(&self.timestamp().to::<u64>().to_be_bytes());
90-
hasher.update(
91-
&self
92-
.base_fee_per_gas()
93-
.map(U256::from)
94-
.unwrap_or_default()
95-
.to_be_bytes::<{ U256::BYTES }>(),
96-
);
97-
hasher.update(&self.gas_limit().to::<u64>().to_be_bytes());
98-
hasher.update(&num_txs.to_be_bytes());
99-
}
100-
101-
/// Hash the l1 messages of the block
102-
#[inline]
103-
fn hash_l1_msg(&self, hasher: &mut impl tiny_keccak::Hasher) {
104-
for tx_hash in self
105-
.transactions()
106-
.filter(|tx| tx.is_l1_tx())
107-
.map(|tx| tx.tx_hash())
108-
{
109-
hasher.update(tx_hash.as_slice())
110-
}
111-
}
112-
}
113-
114-
impl<T: Block> Block for &T {
115-
type Tx = T::Tx;
116-
117-
fn block_hash(&self) -> B256 {
118-
(*self).block_hash()
119-
}
120-
fn state_root(&self) -> B256 {
121-
(*self).state_root()
122-
}
123-
fn difficulty(&self) -> U256 {
124-
(*self).difficulty()
125-
}
126-
fn number(&self) -> u64 {
127-
(*self).number()
128-
}
129-
fn gas_limit(&self) -> u64 {
130-
(*self).gas_limit()
131-
}
132-
fn gas_used(&self) -> u64 {
133-
(*self).gas_used()
134-
}
135-
fn timestamp(&self) -> u64 {
136-
(*self).timestamp()
137-
}
138-
fn prevrandao(&self) -> Option<B256> {
139-
(*self).prevrandao()
140-
}
141-
fn base_fee_per_gas(&self) -> Option<u64> {
142-
(*self).base_fee_per_gas()
143-
}
144-
fn withdraw_root(&self) -> B256 {
145-
(*self).withdraw_root()
146-
}
147-
fn transactions(&self) -> impl Iterator<Item = &Self::Tx> {
148-
(*self).transactions()
149-
}
150-
fn num_txs(&self) -> usize {
151-
(*self).num_txs()
152-
}
153-
}
52+
// #[cfg(feature = "scroll")]
53+
// pub trait BlockScrollExt: Block {
54+
// /// start l1 queue index
55+
// fn start_l1_queue_index(&self) -> u64;
56+
//
57+
// /// Number of l1 transactions
58+
// #[inline]
59+
// fn num_l1_txs(&self) -> u64 {
60+
// // 0x7e is l1 tx
61+
// match self
62+
// .transactions()
63+
// .filter(|tx| tx.is_l1_tx())
64+
// // tx.nonce for l1 tx is the l1 queue index, which is a globally index,
65+
// // not per user as suggested by the name...
66+
// .map(|tx| tx.nonce())
67+
// .max()
68+
// {
69+
// None => 0, // not l1 tx in this block
70+
// Some(end_l1_queue_index) => end_l1_queue_index - self.start_l1_queue_index() + 1,
71+
// }
72+
// }
73+
//
74+
// /// Number of l2 transactions
75+
// #[inline]
76+
// fn num_l2_txs(&self) -> u64 {
77+
// // 0x7e is l1 tx
78+
// self.transactions().filter(|tx| !tx.is_l1_tx()).count() as u64
79+
// }
80+
//
81+
// /// Hash the header of the block
82+
// #[inline]
83+
// fn hash_da_header(&self, hasher: &mut impl tiny_keccak::Hasher) {
84+
// let num_txs = (self.num_l1_txs() + self.num_l2_txs()) as u16;
85+
// hasher.update(&self.number().to_be_bytes());
86+
// hasher.update(&self.timestamp().to::<u64>().to_be_bytes());
87+
// hasher.update(
88+
// &self
89+
// .base_fee_per_gas()
90+
// .map(U256::from)
91+
// .unwrap_or_default()
92+
// .to_be_bytes::<{ U256::BYTES }>(),
93+
// );
94+
// hasher.update(&self.gas_limit().to::<u64>().to_be_bytes());
95+
// hasher.update(&num_txs.to_be_bytes());
96+
// }
97+
//
98+
// /// Hash the l1 messages of the block
99+
// #[inline]
100+
// fn hash_l1_msg(&self, hasher: &mut impl tiny_keccak::Hasher) {
101+
// for tx_hash in self
102+
// .transactions()
103+
// .filter(|tx| tx.is_l1_tx())
104+
// .map(|tx| tx.tx_hash())
105+
// {
106+
// hasher.update(tx_hash.as_slice())
107+
// }
108+
// }
109+
// }
+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
use alloy_primitives::{Address, B256};
2+
3+
/// A list of addresses and storage keys that the transaction plans to access.
4+
/// Accesses outside the list are possible, but become more expensive.
5+
#[derive(
6+
Clone, Debug, Default, PartialEq, Eq, Hash, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize,
7+
)]
8+
#[rkyv(derive(Debug, Hash, PartialEq, Eq))]
9+
pub struct AccessListItem {
10+
/// Account addresses that would be loaded at the start of execution
11+
#[rkyv(attr(doc = "Account addresses that would be loaded at the start of execution"))]
12+
pub address: Address,
13+
/// Keys of storage that would be loaded at the start of execution
14+
#[rkyv(attr(doc = "Keys of storage that would be loaded at the start of execution"))]
15+
pub storage_keys: Vec<B256>,
16+
}
17+
18+
/// AccessList as defined in EIP-2930
19+
#[derive(
20+
Clone, Debug, Default, PartialEq, Eq, Hash, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize,
21+
)]
22+
#[rkyv(derive(Debug, Hash, PartialEq, Eq))]
23+
pub struct AccessList(pub Vec<AccessListItem>);
24+
25+
impl From<alloy_eips::eip2930::AccessListItem> for AccessListItem {
26+
fn from(item: alloy_eips::eip2930::AccessListItem) -> Self {
27+
Self {
28+
address: item.address,
29+
storage_keys: item.storage_keys,
30+
}
31+
}
32+
}
33+
34+
impl From<AccessListItem> for alloy_eips::eip2930::AccessListItem {
35+
fn from(item: AccessListItem) -> Self {
36+
Self {
37+
address: item.address,
38+
storage_keys: item.storage_keys,
39+
}
40+
}
41+
}
42+
43+
impl From<alloy_eips::eip2930::AccessList> for AccessList {
44+
fn from(list: alloy_eips::eip2930::AccessList) -> Self {
45+
Self(list.0.into_iter().map(Into::into).collect())
46+
}
47+
}
48+
49+
impl From<AccessList> for alloy_eips::eip2930::AccessList {
50+
fn from(list: AccessList) -> Self {
51+
Self(list.0.into_iter().map(Into::into).collect())
52+
}
53+
}
54+
55+
impl From<&ArchivedAccessListItem> for alloy_eips::eip2930::AccessListItem {
56+
fn from(item: &ArchivedAccessListItem) -> Self {
57+
Self {
58+
address: Address::from(item.address),
59+
storage_keys: item
60+
.storage_keys
61+
.iter()
62+
.map(|key| B256::from(*key))
63+
.collect(),
64+
}
65+
}
66+
}
67+
68+
impl From<&ArchivedAccessList> for alloy_eips::eip2930::AccessList {
69+
fn from(list: &ArchivedAccessList) -> Self {
70+
Self(
71+
list.0
72+
.iter()
73+
.map(|item| alloy_eips::eip2930::AccessListItem::from(item))
74+
.collect(),
75+
)
76+
}
77+
}

0 commit comments

Comments
 (0)