This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
lib.rs
245 lines (229 loc) · 8.18 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
use alloy::primitives::B256;
use alloy::rpc::types::eth::BlockNumberOrTag;
use alloy::{
providers::Provider,
rpc::types::eth::{Block, BlockId, Withdrawal},
transports::Transport,
};
use anyhow::Context as _;
use common::block_interval::BlockInterval;
use evm_arithmetization::proof::{BlockHashes, BlockMetadata};
use futures::{StreamExt as _, TryStreamExt as _};
use itertools::{Either, Itertools as _};
use prover::{BlockProverInput, ProverInput};
use serde::Deserialize;
use serde_json::json;
use trace_decoder::{
trace_protocol::{BlockTrace, BlockTraceTriePreImages, TxnInfo},
types::{BlockLevelData, OtherBlockData},
};
#[derive(Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
#[allow(clippy::large_enum_variant)]
enum ZeroTrace {
Result(TxnInfo),
BlockWitness(BlockTraceTriePreImages),
}
/// When [fetching a block over RPC](https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_getblockbynumber),
/// we can choose the transaction format, between:
/// - Full JSON.
/// - Just the hash.
///
/// We only need the latter.
const BLOCK_WITH_FULL_TRANSACTIONS: bool = false;
/// Retrieve block information from the provider
pub async fn get_block<ProviderT, TransportT>(
provider: &mut ProviderT,
target_block_id: BlockId,
full_transaction_data: bool,
) -> anyhow::Result<Block>
where
ProviderT: Provider<TransportT>,
TransportT: Transport + Clone,
{
provider
.get_block(target_block_id, full_transaction_data)
.await?
.context("block does not exist")
}
pub async fn block_prover_input<ProviderT, TransportT>(
provider: ProviderT,
target_block_id: BlockId,
checkpoint_state_trie_root: B256,
) -> anyhow::Result<BlockProverInput>
where
ProviderT: Provider<TransportT>,
TransportT: Transport + Clone,
{
// Grab trace information
/////////////////////////
let traces = provider
.raw_request::<_, Vec<ZeroTrace>>(
"debug_traceBlockByNumber".into(),
(target_block_id, json!({"tracer": "zeroTracer"})),
)
.await?;
let (txn_info, mut pre_images) =
traces
.into_iter()
.partition_map::<Vec<_>, Vec<_>, _, _, _>(|it| match it {
ZeroTrace::Result(it) => Either::Left(it),
ZeroTrace::BlockWitness(it) => Either::Right(it),
});
// Grab block info
//////////////////
let target_block = provider
.get_block(target_block_id, BLOCK_WITH_FULL_TRANSACTIONS)
.await?
.context("target block does not exist")?;
let target_block_number = target_block
.header
.number
.context("target block is missing field `number`")?;
let chain_id = provider.get_chain_id().await?;
let mut prev_hashes = [alloy::primitives::B256::ZERO; 256];
let concurrency = prev_hashes.len();
futures::stream::iter(
prev_hashes
.iter_mut()
.rev() // fill RTL
.zip(std::iter::successors(Some(target_block_number), |it| {
it.checked_sub(1)
}))
.map(|(dst, n)| {
let provider = &provider;
async move {
let block = provider
.get_block(n.into(), BLOCK_WITH_FULL_TRANSACTIONS)
.await
.context("couldn't get block")?
.context("no such block")?;
*dst = block.header.parent_hash;
anyhow::Ok(())
}
}),
)
.buffered(concurrency)
.try_collect::<()>()
.await
.context("couldn't fill previous hashes")?;
// Assemble
///////////
Ok(BlockProverInput {
block_trace: BlockTrace {
trie_pre_images: pre_images.pop().context("trace had no BlockWitness")?,
txn_info,
},
other_data: OtherBlockData {
b_data: BlockLevelData {
b_meta: BlockMetadata {
block_beneficiary: target_block.header.miner.compat(),
block_timestamp: target_block.header.timestamp.into(),
block_number: target_block_number.into(),
block_difficulty: target_block.header.difficulty.into(),
block_random: target_block
.header
.mix_hash
.context("target block is missing field `mix_hash`")?
.compat(),
block_gaslimit: target_block.header.gas_limit.into(),
block_chain_id: chain_id.into(),
block_base_fee: target_block
.header
.base_fee_per_gas
.context("target block is missing field `base_fee_per_gas`")?
.into(),
block_gas_used: target_block.header.gas_used.into(),
block_bloom: target_block.header.logs_bloom.compat(),
},
b_hashes: BlockHashes {
prev_hashes: prev_hashes.map(|it| it.compat()).into(),
cur_hash: target_block
.header
.hash
.context("target block is missing field `hash`")?
.compat(),
},
withdrawals: target_block
.withdrawals
.into_iter()
.flatten()
.map(
|Withdrawal {
address, amount, ..
}| { (address.compat(), amount.into()) },
)
.collect(),
},
checkpoint_state_trie_root: checkpoint_state_trie_root.compat(),
},
})
}
/// Obtain the prover input for a given block interval
pub async fn prover_input<ProviderT, TransportT>(
mut provider: ProviderT,
block_interval: BlockInterval,
checkpoint_block_id: BlockId,
) -> anyhow::Result<ProverInput>
where
ProviderT: Provider<TransportT>,
TransportT: Transport + Clone,
{
// Grab interval checkpoint block state trie
let checkpoint_state_trie_root = get_block(
&mut provider,
checkpoint_block_id,
BLOCK_WITH_FULL_TRANSACTIONS,
)
.await?
.header
.state_root;
let mut block_proofs = Vec::new();
let mut block_interval = block_interval.into_bounded_stream()?;
while let Some(block_num) = block_interval.next().await {
let block_id = BlockId::Number(BlockNumberOrTag::Number(block_num));
let block_prover_input =
block_prover_input(&provider, block_id, checkpoint_state_trie_root).await?;
block_proofs.push(block_prover_input);
}
Ok(ProverInput {
blocks: block_proofs,
})
}
trait Compat<Out> {
fn compat(self) -> Out;
}
impl Compat<__compat_primitive_types::H160> for alloy::primitives::Address {
fn compat(self) -> __compat_primitive_types::H160 {
let alloy::primitives::Address(alloy::primitives::FixedBytes(arr)) = self;
__compat_primitive_types::H160(arr)
}
}
impl Compat<__compat_primitive_types::H256> for alloy::primitives::B256 {
fn compat(self) -> __compat_primitive_types::H256 {
let alloy::primitives::FixedBytes(arr) = self;
__compat_primitive_types::H256(arr)
}
}
impl Compat<[__compat_primitive_types::U256; 8]> for alloy::primitives::Bloom {
fn compat(self) -> [__compat_primitive_types::U256; 8] {
let alloy::primitives::Bloom(alloy::primitives::FixedBytes(src)) = self;
// have u8 * 256
// want U256 * 8
// (no unsafe, no unstable)
let mut chunks = src.chunks_exact(32);
let dst = core::array::from_fn(|_ix| {
// This is a bit spicy because we're going from an uninterpeted array of bytes
// to wide integers, but we trust this `From` impl to do the right thing
__compat_primitive_types::U256::from(
<[u8; 32]>::try_from(chunks.next().unwrap()).unwrap(),
)
});
assert_eq!(chunks.len(), 0);
dst
}
}
#[test]
fn bloom() {
let _did_not_panic = alloy::primitives::Bloom::ZERO.compat();
}