|
1 | 1 | //! Stateless Block Verifier primitives library.
|
2 | 2 |
|
| 3 | +extern crate core; |
| 4 | + |
| 5 | +use core::fmt; |
| 6 | + |
3 | 7 | /// Predeployed contracts
|
4 | 8 | #[cfg(feature = "scroll")]
|
5 | 9 | pub mod predeployed;
|
6 | 10 | /// Types definition
|
7 | 11 | pub mod types;
|
8 | 12 |
|
9 | 13 | pub use alloy_consensus;
|
10 |
| -pub use alloy_consensus::Transaction; |
11 |
| -pub use alloy_primitives; |
12 |
| -pub use alloy_primitives::{Address, B256, U256}; |
13 | 14 |
|
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}; |
18 | 17 |
|
| 18 | +/// BlockHeader trait |
| 19 | +pub trait BlockHeader: fmt::Debug { |
19 | 20 | /// Hash of the block
|
20 |
| - fn block_hash(&self) -> B256; |
| 21 | + fn hash(&self) -> BlockHash; |
21 | 22 | /// State root hash
|
22 | 23 | fn state_root(&self) -> B256;
|
23 | 24 | /// Difficulty
|
@@ -46,108 +47,63 @@ pub trait Block: std::fmt::Debug {
|
46 | 47 | fn base_fee_per_gas(&self) -> Option<u64>;
|
47 | 48 | /// Withdrawals root hash added by EIP-4895 and is ignored in legacy headers.
|
48 | 49 | 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; |
53 | 50 | }
|
54 | 51 |
|
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 | +// } |
0 commit comments