-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathblock_fetcher.rs
More file actions
605 lines (526 loc) · 21.2 KB
/
Copy pathblock_fetcher.rs
File metadata and controls
605 lines (526 loc) · 21.2 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
use std::{cmp::min, collections::HashMap, sync::Arc, time::Duration};
use ethrex_blockchain::{Blockchain, fork_choice::apply_fork_choice};
use ethrex_common::utils::keccak;
use ethrex_common::{
Address, H160, H256, U256,
types::{
AccountUpdate, Block, BlockNumber, PrivilegedL2Transaction, Transaction, batch::Batch,
},
};
use ethrex_l2_common::{
l1_messages::{L1Message, get_block_l1_messages, get_l1_message_hash},
privileged_transactions::compute_privileged_transactions_hash,
state_diff::prepare_state_diff,
};
use ethrex_l2_sdk::{get_last_committed_batch, get_last_fetched_l1_block};
use ethrex_rlp::decode::RLPDecode;
use ethrex_rpc::{EthClient, types::receipt::RpcLog};
use ethrex_storage::Store;
use ethrex_storage::trie_db::generic_vm::StoreVmDatabase;
use ethrex_storage_rollup::{RollupStoreError, StoreRollup};
use spawned_concurrency::{
error::GenServerError,
messages::Unused,
tasks::{CastResponse, GenServer, GenServerHandle, send_after},
};
use tracing::{debug, error, info};
use crate::{
SequencerConfig,
based::sequencer_state::{SequencerState, SequencerStatus},
sequencer::{l1_committer::generate_blobs_bundle, utils::node_is_up_to_date},
};
#[derive(Debug, thiserror::Error)]
pub enum BlockFetcherError {
#[error("Block Fetcher failed due to an EthClient error: {0}")]
EthClientError(#[from] ethrex_rpc::clients::EthClientError),
#[error("Block Fetcher failed due to a Store error: {0}")]
StoreError(#[from] ethrex_storage::error::StoreError),
#[error("State Updater failed due to a RollupStore error: {0}")]
RollupStoreError(#[from] RollupStoreError),
#[error("Failed to store fetched block: {0}")]
ChainError(#[from] ethrex_blockchain::error::ChainError),
#[error("Failed to apply fork choice for fetched block: {0}")]
InvalidForkChoice(#[from] ethrex_blockchain::error::InvalidForkChoice),
#[error("Failed to push fetched block to execution cache: {0}")]
ExecutionCacheError(#[from] crate::sequencer::errors::ExecutionCacheError),
#[error("Failed to RLP decode fetched block: {0}")]
RLPDecodeError(#[from] ethrex_rlp::error::RLPDecodeError),
#[error("Block Fetcher failed in a helper function: {0}")]
UtilsError(#[from] crate::utils::error::UtilsError),
#[error("Missing bytes from calldata: {0}")]
WrongBatchCalldata(String),
#[error("Failed due to an EVM error: {0}")]
EvmError(#[from] ethrex_vm::EvmError),
#[error("Failed to produce the blob bundle")]
BlobBundleError,
#[error("Failed to compute deposit logs hash: {0}")]
PrivilegedTransactionError(
#[from] ethrex_l2_common::privileged_transactions::PrivilegedTransactionError,
),
#[error("Internal Error: {0}")]
InternalError(#[from] GenServerError),
#[error("Tried to store an empty batch")]
EmptyBatchError,
#[error("Failed to retrieve data: {0}")]
RetrievalError(String),
#[error("Inconsistent Storage: {0}")]
InconsistentStorage(String),
#[error("Conversion Error: {0}")]
ConversionError(String),
#[error("Calculation Error: {0}")]
CalculationError(String),
}
#[derive(Clone)]
pub enum InMessage {
Fetch,
}
#[derive(Clone, PartialEq)]
pub enum OutMessage {
Done,
}
pub struct BlockFetcher {
eth_client: EthClient,
on_chain_proposer_address: Address,
store: Store,
rollup_store: StoreRollup,
blockchain: Arc<Blockchain>,
sequencer_state: SequencerState,
fetch_interval_ms: u64,
last_l1_block_fetched: U256,
fetch_block_step: U256,
}
impl BlockFetcher {
pub async fn new(
cfg: &SequencerConfig,
store: Store,
rollup_store: StoreRollup,
blockchain: Arc<Blockchain>,
sequencer_state: SequencerState,
) -> Result<Self, BlockFetcherError> {
let eth_client = EthClient::new_with_multiple_urls(cfg.eth.rpc_url.clone())?;
let last_l1_block_fetched =
get_last_fetched_l1_block(ð_client, cfg.l1_watcher.bridge_address)
.await?
.into();
Ok(Self {
eth_client,
on_chain_proposer_address: cfg.l1_committer.on_chain_proposer_address,
store,
rollup_store,
blockchain,
sequencer_state,
fetch_interval_ms: cfg.based.block_fetcher.fetch_interval_ms,
last_l1_block_fetched,
fetch_block_step: cfg.based.block_fetcher.fetch_block_step.into(),
})
}
pub async fn spawn(
cfg: &SequencerConfig,
store: Store,
rollup_store: StoreRollup,
blockchain: Arc<Blockchain>,
sequencer_state: SequencerState,
) -> Result<(), BlockFetcherError> {
let state = Self::new(cfg, store, rollup_store, blockchain, sequencer_state).await?;
let mut block_fetcher = state.start();
block_fetcher
.cast(InMessage::Fetch)
.await
.map_err(BlockFetcherError::InternalError)
}
async fn fetch(&mut self) -> Result<(), BlockFetcherError> {
while !node_is_up_to_date::<BlockFetcherError>(
&self.eth_client,
self.on_chain_proposer_address,
&self.rollup_store,
)
.await?
{
info!("Node is not up to date. Syncing via L1");
let last_l2_block_number_known = self.store.get_latest_block_number().await?;
let last_l2_batch_number_known = self
.rollup_store
.get_batch_number_by_block(last_l2_block_number_known)
.await?
.ok_or(BlockFetcherError::RetrievalError(format!(
"Failed to get last batch number known for block {last_l2_block_number_known}"
)))?;
let last_l2_committed_batch_number =
get_last_committed_batch(&self.eth_client, self.on_chain_proposer_address).await?;
let l2_batches_behind = last_l2_committed_batch_number.checked_sub(last_l2_batch_number_known).ok_or(
BlockFetcherError::CalculationError(
"Failed to calculate batches behind. Last batch number known is greater than last committed batch number.".to_string(),
),
)?;
info!(
"Node is {l2_batches_behind} batches behind. Last batch number known: {last_l2_batch_number_known}, last committed batch number: {last_l2_committed_batch_number}"
);
let (batch_committed_logs, batch_verified_logs) = self.get_logs().await?;
self.process_committed_logs(batch_committed_logs, last_l2_batch_number_known)
.await?;
self.process_verified_logs(batch_verified_logs).await?;
}
info!("Node is up to date");
Ok(())
}
/// Fetch logs from the L1 chain for the BatchCommitted and BatchVerified events.
/// This function fetches logs, starting from the last fetched block number (aka the last block that was processed)
/// and going up to the current block number.
async fn get_logs(&mut self) -> Result<(Vec<RpcLog>, Vec<RpcLog>), BlockFetcherError> {
let last_l1_block_number = self.eth_client.get_block_number().await?;
let mut batch_committed_logs = Vec::new();
let mut batch_verified_logs = Vec::new();
while self.last_l1_block_fetched < last_l1_block_number {
let new_last_l1_fetched_block = min(
self.last_l1_block_fetched + self.fetch_block_step,
last_l1_block_number,
);
debug!(
"Fetching logs from block {} to {}",
self.last_l1_block_fetched + 1,
new_last_l1_fetched_block
);
// Fetch logs from the L1 chain for the BatchCommitted event.
let committed_logs = self
.eth_client
.get_logs(
self.last_l1_block_fetched + 1,
new_last_l1_fetched_block,
self.on_chain_proposer_address,
vec![keccak(b"BatchCommitted(uint256,bytes32)")],
)
.await?;
// Fetch logs from the L1 chain for the BatchVerified event.
let verified_logs = self
.eth_client
.get_logs(
self.last_l1_block_fetched + 1,
new_last_l1_fetched_block,
self.on_chain_proposer_address,
vec![keccak(b"BatchVerified(uint256)")],
)
.await?;
// Update the last L1 block fetched.
self.last_l1_block_fetched = new_last_l1_fetched_block;
batch_committed_logs.extend_from_slice(&committed_logs);
batch_verified_logs.extend_from_slice(&verified_logs);
}
Ok((batch_committed_logs, batch_verified_logs))
}
/// Process the logs from the event `BatchCommitted`.
/// Gets the committed batches that are missing in the local store from the logs,
/// and seals the batch in the rollup store.
async fn process_committed_logs(
&mut self,
batch_committed_logs: Vec<RpcLog>,
last_l2_batch_number_known: u64,
) -> Result<(), BlockFetcherError> {
let mut missing_batches_logs =
filter_logs(&batch_committed_logs, last_l2_batch_number_known).await?;
missing_batches_logs.sort_by_key(|(_log, batch_number)| *batch_number);
for (batch_committed_log, batch_number) in missing_batches_logs {
let tx = self
.eth_client
.get_transaction_by_hash(batch_committed_log.transaction_hash)
.await?
.ok_or(BlockFetcherError::RetrievalError(format!(
"Failed to get the receipt for transaction {:x}",
batch_committed_log.transaction_hash
)))?
.tx;
let batch = decode_batch_from_calldata(tx.data())?;
self.store_batch(&batch).await?;
self.seal_batch(&batch, batch_number, batch_committed_log.transaction_hash)
.await?;
}
Ok(())
}
async fn store_batch(&mut self, batch: &[Block]) -> Result<(), BlockFetcherError> {
for block in batch.iter() {
self.blockchain.add_block(block.clone())?;
let block_hash = block.hash();
info!(
"Added fetched block {} with hash {block_hash:#x}",
block.header.number,
);
}
let latest_hash_on_batch = batch
.last()
.ok_or(BlockFetcherError::EmptyBatchError)?
.hash();
apply_fork_choice(
&self.store,
latest_hash_on_batch,
latest_hash_on_batch,
latest_hash_on_batch,
)
.await?;
Ok(())
}
async fn seal_batch(
&mut self,
batch: &[Block],
batch_number: U256,
commit_tx: H256,
) -> Result<(), BlockFetcherError> {
let batch = self.get_batch(batch, batch_number, commit_tx).await?;
self.rollup_store.seal_batch(batch).await?;
info!("Sealed batch {batch_number}.");
Ok(())
}
async fn get_batch(
&mut self,
batch: &[Block],
batch_number: U256,
commit_tx: H256,
) -> Result<Batch, BlockFetcherError> {
let privileged_transactions: Vec<PrivilegedL2Transaction> = batch
.iter()
.flat_map(|block| {
block.body.transactions.iter().filter_map(|tx| {
if let Transaction::PrivilegedL2Transaction(tx) = tx {
Some(tx.clone())
} else {
None
}
})
})
.collect();
let privileged_transaction_hashes = privileged_transactions
.iter()
.filter_map(|tx| tx.get_privileged_hash())
.collect();
let mut messages = Vec::new();
for block in batch {
let block_messages = self.extract_block_messages(block.header.number).await?;
messages.extend(block_messages);
}
let privileged_transactions_hash =
compute_privileged_transactions_hash(privileged_transaction_hashes)?;
let first_block = batch.first().ok_or(BlockFetcherError::RetrievalError(
"Batch is empty. This shouldn't happen.".to_owned(),
))?;
let last_block = batch.last().ok_or(BlockFetcherError::RetrievalError(
"Batch is empty. This shouldn't happen.".to_owned(),
))?;
let new_state_root = self
.store
.state_trie(last_block.hash())?
.ok_or(BlockFetcherError::InconsistentStorage(
"This block should be in the store".to_owned(),
))?
.hash_no_commit();
// This is copied from the L1Committer, this should be reviewed.
let mut acc_account_updates: HashMap<H160, AccountUpdate> = HashMap::new();
for block in batch {
let vm_db = StoreVmDatabase::new(self.store.clone(), block.header.parent_hash);
let mut vm = self.blockchain.new_evm(vm_db)?;
vm.execute_block(block)
.map_err(BlockFetcherError::EvmError)?;
let account_updates = vm
.get_state_transitions()
.map_err(BlockFetcherError::EvmError)?;
for account in account_updates {
let address = account.address;
if let Some(existing) = acc_account_updates.get_mut(&address) {
existing.merge(account);
} else {
acc_account_updates.insert(address, account);
}
}
}
let parent_block_hash = first_block.header.parent_hash;
let parent_db = StoreVmDatabase::new(self.store.clone(), parent_block_hash);
let state_diff = prepare_state_diff(
last_block.header.clone(),
&parent_db,
&messages,
&privileged_transactions,
acc_account_updates.into_values().collect(),
)
.map_err(|_| BlockFetcherError::BlobBundleError)?;
let (blobs_bundle, _) =
generate_blobs_bundle(&state_diff).map_err(|_| BlockFetcherError::BlobBundleError)?;
Ok(Batch {
number: batch_number.as_u64(),
first_block: first_block.header.number,
last_block: last_block.header.number,
state_root: new_state_root,
privileged_transactions_hash,
message_hashes: self.get_batch_message_hashes(batch).await?,
blobs_bundle,
commit_tx: Some(commit_tx),
verify_tx: None,
})
}
async fn get_batch_message_hashes(
&mut self,
batch: &[Block],
) -> Result<Vec<H256>, BlockFetcherError> {
let mut message_hashes = Vec::new();
for block in batch {
let block_messages = self.extract_block_messages(block.header.number).await?;
for msg in &block_messages {
message_hashes.push(get_l1_message_hash(msg));
}
}
Ok(message_hashes)
}
async fn extract_block_messages(
&mut self,
block_number: BlockNumber,
) -> Result<Vec<L1Message>, BlockFetcherError> {
let Some(block_body) = self.store.get_block_body(block_number).await? else {
return Err(BlockFetcherError::InconsistentStorage(format!(
"Block {block_number} is supposed to be in store at this point"
)));
};
let mut txs = vec![];
let mut receipts = vec![];
for (index, tx) in block_body.transactions.iter().enumerate() {
let receipt = self
.store
.get_receipt(
block_number,
index.try_into().map_err(|_| {
BlockFetcherError::ConversionError(
"Failed to convert index to u64".to_owned(),
)
})?,
)
.await?
.ok_or(BlockFetcherError::RetrievalError(
"Transactions in a block should have a receipt".to_owned(),
))?;
txs.push(tx.clone());
receipts.push(receipt);
}
Ok(get_block_l1_messages(&receipts))
}
/// Process the logs from the event `BatchVerified`.
/// Gets the batch number from the logs and stores the verify transaction hash in the rollup store
async fn process_verified_logs(
&mut self,
batch_verified_logs: Vec<RpcLog>,
) -> Result<(), BlockFetcherError> {
for batch_verified_log in batch_verified_logs {
let batch_number = U256::from_big_endian(
batch_verified_log
.log
.topics
.get(1)
.ok_or(BlockFetcherError::RetrievalError(
"Failed to get verified batch number from BatchVerified log".to_string(),
))?
.as_bytes(),
);
let verify_tx_hash = batch_verified_log.transaction_hash;
self.rollup_store
.store_verify_tx_by_batch(batch_number.as_u64(), verify_tx_hash)
.await?;
info!("Stored verify transaction hash {verify_tx_hash:#x} for batch {batch_number}");
}
Ok(())
}
}
impl GenServer for BlockFetcher {
type CallMsg = Unused;
type CastMsg = InMessage;
type OutMsg = OutMessage;
type Error = BlockFetcherError;
async fn handle_cast(
&mut self,
_message: Self::CastMsg,
handle: &GenServerHandle<Self>,
) -> CastResponse {
if let SequencerStatus::Syncing = self.sequencer_state.status().await {
let _ = self.fetch().await.inspect_err(|err| {
error!("Block Fetcher Error: {err}");
});
}
send_after(
Duration::from_millis(self.fetch_interval_ms),
handle.clone(),
Self::CastMsg::Fetch,
);
CastResponse::NoReply
}
}
/// Given the logs from the event `BatchCommitted`,
/// this function gets the committed batches that are missing in the local store.
/// It does that by comparing if the batch number is greater than the last known batch number.
async fn filter_logs(
logs: &[RpcLog],
last_batch_number_known: u64,
) -> Result<Vec<(RpcLog, U256)>, BlockFetcherError> {
let mut filtered_logs = Vec::new();
// Filter missing batches logs
for batch_committed_log in logs.iter().cloned() {
let committed_batch_number = U256::from_big_endian(
batch_committed_log
.log
.topics
.get(1)
.ok_or(BlockFetcherError::RetrievalError(
"Failed to get committed batch number from BatchCommitted log".to_string(),
))?
.as_bytes(),
);
if committed_batch_number > last_batch_number_known.into() {
filtered_logs.push((batch_committed_log, committed_batch_number));
}
}
Ok(filtered_logs)
}
// TODO: Move to calldata module (SDK)
fn decode_batch_from_calldata(calldata: &[u8]) -> Result<Vec<Block>, BlockFetcherError> {
// function commitBatch(
// uint256 batchNumber,
// bytes32 newStateRoot,
// bytes32 stateDiffKZGVersionedHash,
// bytes32 messagesLogsMerkleRoot,
// bytes32 processedPrivilegedTransactionsRollingHash,
// bytes[] calldata _rlpEncodedBlocks
// ) external;
// data = 4 bytes (function selector) 0..4
// || 8 bytes (batch number) 4..36
// || 32 bytes (new state root) 36..68
// || 32 bytes (state diff KZG versioned hash) 68..100
// || 32 bytes (messages logs merkle root) 100..132
// || 32 bytes (processed privileged transactions rolling hash) 132..164
let batch_length_in_blocks = U256::from_big_endian(calldata.get(196..228).ok_or(
BlockFetcherError::WrongBatchCalldata("Couldn't get batch length bytes".to_owned()),
)?)
.as_usize();
let base = 228;
let mut batch = Vec::new();
for block_i in 0..batch_length_in_blocks {
let block_length_offset = base + block_i * 32;
let dynamic_offset = U256::from_big_endian(
calldata
.get(block_length_offset..block_length_offset + 32)
.ok_or(BlockFetcherError::WrongBatchCalldata(
"Couldn't get dynamic offset bytes".to_owned(),
))?,
)
.as_usize();
let block_length_in_bytes = U256::from_big_endian(
calldata
.get(base + dynamic_offset..base + dynamic_offset + 32)
.ok_or(BlockFetcherError::WrongBatchCalldata(
"Couldn't get block length bytes".to_owned(),
))?,
)
.as_usize();
let block_offset = base + dynamic_offset + 32;
let block = Block::decode(
calldata
.get(block_offset..block_offset + block_length_in_bytes)
.ok_or(BlockFetcherError::WrongBatchCalldata(
"Couldn't get block bytes".to_owned(),
))?,
)?;
batch.push(block);
}
Ok(batch)
}