Skip to content

Commit 8a568ca

Browse files
iovoidtomip01
andauthored
refactor(l2): remove expects in L2 monitor (#3615)
**Motivation** We want to handle errors gracefully. **Description** Removes usage of .expect **How to test** - Add `--monitor` to the `init-l2-no-metrics` target in `crates/l2/Makefile`. - Run a Sequencer (I suggest `make restart` in `crates/l2`). - Run the prover with `make init-prover` in `crates/l2`. - Run `make test` in `crates/l2`. Closes #3535 --------- Co-authored-by: Tomás Paradelo <[email protected]>
1 parent ea331e0 commit 8a568ca

File tree

11 files changed

+141
-184
lines changed

11 files changed

+141
-184
lines changed

crates/l2/monitor/app.rs

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ impl EthrexMonitor {
6262
rollup_store: StoreRollup,
6363
cfg: &SequencerConfig,
6464
) -> Result<Self, MonitorError> {
65-
let eth_client = EthClient::new(cfg.eth.rpc_url.first().expect("No RPC URLs provided"))
66-
.expect("Failed to create EthClient");
65+
let eth_client = EthClient::new(cfg.eth.rpc_url.first().ok_or(MonitorError::RPCListEmpty)?)
66+
.map_err(MonitorError::EthClientError)?;
6767
// TODO: De-hardcode the rollup client URL
6868
let rollup_client =
69-
EthClient::new("http://localhost:1729").expect("Failed to create RollupClient");
69+
EthClient::new("http://localhost:1729").map_err(MonitorError::EthClientError)?;
7070

71-
Ok(EthrexMonitor {
71+
let mut monitor = EthrexMonitor {
7272
title: if cfg.based.based {
7373
"Based Ethrex Monitor".to_string()
7474
} else {
@@ -77,41 +77,22 @@ impl EthrexMonitor {
7777
should_quit: false,
7878
tabs: TabsState::default(),
7979
tick_rate: cfg.monitor.tick_rate,
80-
global_chain_status: GlobalChainStatusTable::new(
81-
&eth_client,
82-
cfg,
83-
&store,
84-
&rollup_store,
85-
)
86-
.await,
80+
global_chain_status: GlobalChainStatusTable::new(cfg),
8781
logger: TuiWidgetState::new().set_default_display_level(tui_logger::LevelFilter::Info),
88-
node_status: NodeStatusTable::new(sequencer_state.clone(), &store).await,
89-
mempool: MempoolTable::new(&rollup_client).await,
90-
batches_table: BatchesTable::new(
91-
cfg.l1_committer.on_chain_proposer_address,
92-
&eth_client,
93-
&rollup_store,
94-
)
95-
.await?,
96-
blocks_table: BlocksTable::new(&store).await?,
97-
l1_to_l2_messages: L1ToL2MessagesTable::new(
98-
cfg.l1_watcher.bridge_address,
99-
&eth_client,
100-
&store,
101-
)
102-
.await?,
103-
l2_to_l1_messages: L2ToL1MessagesTable::new(
104-
cfg.l1_watcher.bridge_address,
105-
&eth_client,
106-
&rollup_client,
107-
)
108-
.await?,
82+
node_status: NodeStatusTable::new(sequencer_state.clone()),
83+
mempool: MempoolTable::new(),
84+
batches_table: BatchesTable::new(cfg.l1_committer.on_chain_proposer_address),
85+
blocks_table: BlocksTable::new(),
86+
l1_to_l2_messages: L1ToL2MessagesTable::new(cfg.l1_watcher.bridge_address),
87+
l2_to_l1_messages: L2ToL1MessagesTable::new(cfg.l1_watcher.bridge_address),
10988
eth_client,
11089
rollup_client,
11190
store,
11291
rollup_store,
11392
last_scroll: Instant::now(),
114-
})
93+
};
94+
monitor.on_tick().await?;
95+
Ok(monitor)
11596
}
11697

11798
pub async fn start(mut self) -> Result<(), MonitorError> {
@@ -219,11 +200,11 @@ impl EthrexMonitor {
219200
}
220201

221202
pub async fn on_tick(&mut self) -> Result<(), MonitorError> {
222-
self.node_status.on_tick(&self.store).await;
203+
self.node_status.on_tick(&self.store).await?;
223204
self.global_chain_status
224205
.on_tick(&self.eth_client, &self.store, &self.rollup_store)
225-
.await;
226-
self.mempool.on_tick(&self.rollup_client).await;
206+
.await?;
207+
self.mempool.on_tick(&self.rollup_client).await?;
227208
self.batches_table
228209
.on_tick(&self.eth_client, &self.rollup_store)
229210
.await?;

crates/l2/monitor/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// TODO: Handle this expects
2-
#![expect(clippy::expect_used)]
32
#![expect(clippy::indexing_slicing)]
43
#[expect(clippy::result_large_err)]
54
pub(crate) mod app;

crates/l2/monitor/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub async fn get_logs(
1515
let last_block_number = client
1616
.get_block_number()
1717
.await
18-
.expect("Failed to get latest L1 block");
18+
.map_err(|_| MonitorError::GetLatestBlock)?;
1919

2020
let mut batch_committed_logs = Vec::new();
2121
while *last_block_fetched < last_block_number {

crates/l2/monitor/widget/batches.rs

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::{
1414
sequencer::errors::MonitorError,
1515
};
1616

17+
#[derive(Default)]
1718
pub struct BatchesTable {
1819
pub state: TableState,
1920
// batch number | # blocks | # messages | commit tx hash | verify tx hash
@@ -24,25 +25,11 @@ pub struct BatchesTable {
2425
}
2526

2627
impl BatchesTable {
27-
pub async fn new(
28-
on_chain_proposer_address: Address,
29-
eth_client: &EthClient,
30-
rollup_store: &StoreRollup,
31-
) -> Result<Self, MonitorError> {
32-
let mut last_l1_block_fetched = 0;
33-
let items = Self::fetch_new_items(
34-
&mut last_l1_block_fetched,
28+
pub fn new(on_chain_proposer_address: Address) -> Self {
29+
Self {
3530
on_chain_proposer_address,
36-
eth_client,
37-
rollup_store,
38-
)
39-
.await?;
40-
Ok(Self {
41-
state: TableState::default(),
42-
items,
43-
last_l1_block_fetched,
44-
on_chain_proposer_address,
45-
})
31+
..Default::default()
32+
}
4633
}
4734

4835
pub async fn on_tick(
@@ -73,11 +60,11 @@ impl BatchesTable {
7360
return Ok(());
7461
}
7562

76-
let mut from = self.items.last().expect("Expected items in the table").0 - 1;
63+
let mut from = self.items.last().ok_or(MonitorError::NoItemsInTable)?.0 - 1;
7764

7865
let refreshed_batches = Self::get_batches(
7966
&mut from,
80-
self.items.first().expect("Expected items in the table").0,
67+
self.items.first().ok_or(MonitorError::NoItemsInTable)?.0,
8168
rollup_store,
8269
)
8370
.await?;
@@ -98,7 +85,7 @@ impl BatchesTable {
9885
let last_l2_batch_number = eth_client
9986
.get_last_committed_batch(on_chain_proposer_address)
10087
.await
101-
.expect("Failed to get latest L2 batch");
88+
.map_err(|_| MonitorError::GetLatestBatch)?;
10289

10390
let new_batches =
10491
Self::get_batches(last_l2_batch_fetched, last_l2_batch_number, rollup_store).await?;
@@ -117,7 +104,7 @@ impl BatchesTable {
117104
let batch = rollup_store
118105
.get_batch(batch_number)
119106
.await
120-
.map_err(|e| MonitorError::RollupStore(batch_number, e))?
107+
.map_err(|e| MonitorError::GetBatchByNumber(batch_number, e))?
121108
.ok_or(MonitorError::BatchNotFound(batch_number))?;
122109

123110
*from = batch_number;

crates/l2/monitor/widget/blocks.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::{
1919
sequencer::errors::MonitorError,
2020
};
2121

22+
#[derive(Default)]
2223
pub struct BlocksTable {
2324
pub state: TableState,
2425
// block number | #transactions | hash | coinbase | gas | blob gas | size
@@ -27,14 +28,8 @@ pub struct BlocksTable {
2728
}
2829

2930
impl BlocksTable {
30-
pub async fn new(store: &Store) -> Result<Self, MonitorError> {
31-
let mut last_l2_block_known = 0;
32-
let items = Self::refresh_items(&mut last_l2_block_known, store).await?;
33-
Ok(Self {
34-
state: TableState::default(),
35-
items,
36-
last_l2_block_known,
37-
})
31+
pub fn new() -> Self {
32+
Default::default()
3833
}
3934

4035
pub async fn on_tick(&mut self, store: &Store) -> Result<(), MonitorError> {
@@ -80,7 +75,7 @@ impl BlocksTable {
8075
let last_l2_block_number = store
8176
.get_latest_block_number()
8277
.await
83-
.expect("Failed to get latest L2 block");
78+
.map_err(|_| MonitorError::GetLatestBlock)?;
8479

8580
let mut new_blocks = Vec::new();
8681
while *last_l2_block_known < last_l2_block_number {

crates/l2/monitor/widget/chain_status.rs

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ use ratatui::{
1111
widgets::{Block, Row, StatefulWidget, Table, TableState},
1212
};
1313

14-
use crate::SequencerConfig;
14+
use crate::{SequencerConfig, sequencer::errors::MonitorError};
1515

16+
#[derive(Default)]
1617
pub struct GlobalChainStatusTable {
1718
pub state: TableState,
1819
pub items: Vec<(String, String)>,
@@ -21,30 +22,17 @@ pub struct GlobalChainStatusTable {
2122
}
2223

2324
impl GlobalChainStatusTable {
24-
pub async fn new(
25-
eth_client: &EthClient,
26-
cfg: &SequencerConfig,
27-
store: &Store,
28-
rollup_store: &StoreRollup,
29-
) -> Self {
25+
pub fn new(cfg: &SequencerConfig) -> Self {
3026
let sequencer_registry_address =
3127
if cfg.based.state_updater.sequencer_registry == Address::default() {
3228
None
3329
} else {
3430
Some(cfg.based.state_updater.sequencer_registry)
3531
};
3632
Self {
37-
state: TableState::default(),
38-
items: Self::refresh_items(
39-
eth_client,
40-
cfg.l1_committer.on_chain_proposer_address,
41-
sequencer_registry_address,
42-
store,
43-
rollup_store,
44-
)
45-
.await,
4633
on_chain_proposer_address: cfg.l1_committer.on_chain_proposer_address,
4734
sequencer_registry_address,
35+
..Default::default()
4836
}
4937
}
5038

@@ -53,15 +41,16 @@ impl GlobalChainStatusTable {
5341
eth_client: &EthClient,
5442
store: &Store,
5543
rollup_store: &StoreRollup,
56-
) {
44+
) -> Result<(), MonitorError> {
5745
self.items = Self::refresh_items(
5846
eth_client,
5947
self.on_chain_proposer_address,
6048
self.sequencer_registry_address,
6149
store,
6250
rollup_store,
6351
)
64-
.await;
52+
.await?;
53+
Ok(())
6554
}
6655

6756
async fn refresh_items(
@@ -70,12 +59,12 @@ impl GlobalChainStatusTable {
7059
sequencer_registry_address: Option<Address>,
7160
store: &Store,
7261
rollup_store: &StoreRollup,
73-
) -> Vec<(String, String)> {
62+
) -> Result<Vec<(String, String)>, MonitorError> {
7463
let last_update = chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string();
7564

7665
let lead_sequencer = if let Some(sequencer_registry_address) = sequencer_registry_address {
7766
let calldata = encode_calldata("leaderSequencer()", &[])
78-
.expect("Failed to encode leadSequencer calldata");
67+
.map_err(MonitorError::CalldataEncodeError)?;
7968

8069
let raw_lead_sequencer: H256 = eth_client
8170
.call(
@@ -84,7 +73,7 @@ impl GlobalChainStatusTable {
8473
Overrides::default(),
8574
)
8675
.await
87-
.expect("Failed to call leaderSequencer")
76+
.map_err(MonitorError::EthClientError)?
8877
.parse()
8978
.unwrap_or_default();
9079

@@ -96,20 +85,20 @@ impl GlobalChainStatusTable {
9685
let last_committed_batch = eth_client
9786
.get_last_committed_batch(on_chain_proposer_address)
9887
.await
99-
.expect("Failed to get last committed batch");
88+
.map_err(|_| MonitorError::GetLatestCommittedBatch)?;
10089

10190
let last_verified_batch = eth_client
10291
.get_last_verified_batch(on_chain_proposer_address)
10392
.await
104-
.expect("Failed to get last verified batch");
93+
.map_err(|_| MonitorError::GetLatestVerifiedBatch)?;
10594

10695
let last_committed_block = if last_committed_batch == 0 {
10796
0
10897
} else {
10998
match rollup_store
11099
.get_block_numbers_by_batch(last_committed_batch)
111100
.await
112-
.expect("Failed to get blocks by batch")
101+
.map_err(|e| MonitorError::GetBlocksByBatch(last_committed_batch, e))?
113102
{
114103
Some(block_numbers) => block_numbers.last().copied().unwrap_or(0),
115104
None => 0,
@@ -122,7 +111,7 @@ impl GlobalChainStatusTable {
122111
match rollup_store
123112
.get_block_numbers_by_batch(last_verified_batch)
124113
.await
125-
.expect("Failed to get blocks by batch")
114+
.map_err(|e| MonitorError::GetBlocksByBatch(last_verified_batch, e))?
126115
{
127116
Some(block_numbers) => block_numbers.last().copied().unwrap_or(0),
128117
None => 0,
@@ -132,7 +121,7 @@ impl GlobalChainStatusTable {
132121
let current_block = store
133122
.get_latest_block_number()
134123
.await
135-
.expect("Failed to get latest L1 block")
124+
.map_err(|_| MonitorError::GetLatestBlock)?
136125
+ 1;
137126

138127
let current_batch = if sequencer_registry_address.is_some() {
@@ -141,7 +130,7 @@ impl GlobalChainStatusTable {
141130
(last_committed_batch + 1).to_string()
142131
};
143132

144-
if sequencer_registry_address.is_some() {
133+
let items = if sequencer_registry_address.is_some() {
145134
vec![
146135
("Last Update:".to_string(), last_update),
147136
(
@@ -189,7 +178,8 @@ impl GlobalChainStatusTable {
189178
last_verified_block.to_string(),
190179
),
191180
]
192-
}
181+
};
182+
Ok(items)
193183
}
194184
}
195185

0 commit comments

Comments
 (0)