Skip to content

Commit 7bbcfa8

Browse files
author
Dev Kalra
authored
feat(fortuna): improve logging (#1472)
* update existing logs in keeper * handle provider error * handle implicit errors * address feedback
1 parent 0d6c35f commit 7bbcfa8

File tree

2 files changed

+72
-28
lines changed

2 files changed

+72
-28
lines changed

apps/fortuna/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "fortuna"
3-
version = "4.0.0"
3+
version = "4.0.1"
44
edition = "2021"
55

66
[dependencies]

apps/fortuna/src/keeper.rs

Lines changed: 71 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use {
1515
},
1616
anyhow::Result,
1717
ethers::{
18+
contract::ContractError,
1819
providers::{
1920
Middleware,
2021
Provider,
@@ -58,7 +59,11 @@ async fn get_latest_safe_block(chain_state: &BlockchainState) -> BlockNumber {
5859
return latest_confirmed_block - chain_state.reveal_delay_blocks
5960
}
6061
Err(e) => {
61-
tracing::error!("Error while getting block number. error: {:?}", e);
62+
tracing::error!(
63+
"Chain: {} - error while getting block number. error: {:?}",
64+
&chain_state.id,
65+
e
66+
);
6267
time::sleep(RETRY_INTERVAL).await;
6368
}
6469
}
@@ -72,12 +77,12 @@ pub async fn run_keeper_threads(
7277
chain_eth_config: EthereumConfig,
7378
chain_state: BlockchainState,
7479
) {
75-
tracing::info!("Starting keeper for chain: {}", &chain_state.id);
80+
tracing::info!("Chain: {} - starting keeper", &chain_state.id);
7681

7782
let latest_safe_block = get_latest_safe_block(&chain_state).await;
7883

7984
tracing::info!(
80-
"Latest safe block for chain {}: {} ",
85+
"Chain: {} - latest safe block: {}",
8186
&chain_state.id,
8287
&latest_safe_block
8388
);
@@ -104,7 +109,7 @@ pub async fn run_keeper_threads(
104109
)
105110
.await;
106111
tracing::info!(
107-
"Backlog processing for chain: {} completed",
112+
"Chain: {} - backlog processing completed",
108113
&backlog_chain_state.id
109114
);
110115
});
@@ -124,7 +129,7 @@ pub async fn run_keeper_threads(
124129
.await
125130
{
126131
tracing::error!(
127-
"Error in watching blocks for chain: {}, {:?}",
132+
"Chain: {} - error in watching blocks. error: {:?}",
128133
&watch_blocks_chain_state.id,
129134
e
130135
);
@@ -159,7 +164,8 @@ pub async fn process_event(
159164
Ok(result) => result,
160165
Err(e) => {
161166
tracing::error!(
162-
"Error while revealing for provider: {} and sequence number: {} with error: {:?}",
167+
"Chain: {} - error while revealing for provider: {} and sequence number: {} with error: {:?}",
168+
&chain_config.id,
163169
event.provider_address,
164170
event.sequence_number,
165171
e
@@ -188,28 +194,49 @@ pub async fn process_event(
188194

189195
if gas_estimate > gas_limit {
190196
tracing::error!(
191-
"Gas estimate for reveal with callback is higher than the gas limit for chain: {}",
197+
"Chain: {} - gas estimate for reveal with callback is higher than the gas limit",
192198
&chain_config.id
193199
);
194200
return Ok(());
195201
}
196202

197-
let res = contract
203+
let contract_call = contract
198204
.reveal_with_callback(
199205
event.provider_address,
200206
event.sequence_number,
201207
event.user_random_number,
202208
provider_revelation,
203209
)
204-
.gas(gas_estimate)
205-
.send()
206-
.await?
207-
.await;
210+
.gas(gas_estimate);
211+
212+
let res = contract_call.send().await;
213+
214+
let pending_tx = match res {
215+
Ok(pending_tx) => pending_tx,
216+
Err(e) => match e {
217+
// If there is a provider error, we weren't able to send the transaction.
218+
// We will return an error. So, that the caller can decide what to do (retry).
219+
ContractError::ProviderError { e } => return Err(e.into()),
220+
// For all the other errors, it is likely the case we won't be able to reveal for
221+
// ever. We will return an Ok(()) to signal that we have processed this reveal
222+
// and concluded that its Ok to not reveal.
223+
_ => {
224+
tracing::error!(
225+
"Chain: {} - error while revealing for provider: {} and sequence number: {} with error: {:?}",
226+
&chain_config.id,
227+
event.provider_address,
228+
event.sequence_number,
229+
e
230+
);
231+
return Ok(());
232+
}
233+
},
234+
};
208235

209-
match res {
210-
Ok(_) => {
236+
match pending_tx.await {
237+
Ok(res) => {
211238
tracing::info!(
212-
"Revealed on chain: {} for provider: {} and sequence number: {} with res: {:?}",
239+
"Chain: {} - revealed for provider: {} and sequence number: {} with res: {:?}",
213240
&chain_config.id,
214241
event.provider_address,
215242
event.sequence_number,
@@ -219,7 +246,8 @@ pub async fn process_event(
219246
}
220247
Err(e) => {
221248
tracing::error!(
222-
"Error while revealing for provider: {} and sequence number: {} with error: {:?}",
249+
"Chain: {} - error while revealing for provider: {} and sequence number: {} with error: {:?}",
250+
&chain_config.id,
223251
event.provider_address,
224252
event.sequence_number,
225253
e
@@ -232,7 +260,8 @@ pub async fn process_event(
232260
},
233261
Err(e) => {
234262
tracing::error!(
235-
"Error while simulating reveal for provider: {} and sequence number: {} \n error: {:?}",
263+
"Chain: {} - error while simulating reveal for provider: {} and sequence number: {} \n error: {:?}",
264+
&chain_config.id,
236265
event.provider_address,
237266
event.sequence_number,
238267
e
@@ -252,7 +281,7 @@ pub async fn process_block_range(
252281
chain_state: api::BlockchainState,
253282
) {
254283
tracing::info!(
255-
"Processing blocks for chain: {} from block: {} to block: {}",
284+
"Chain: {} - processing blocks from: {} to: {}",
256285
&chain_state.id,
257286
block_range.from,
258287
block_range.to
@@ -280,7 +309,7 @@ pub async fn process_block_range(
280309
process_event(event.clone(), &chain_state, &contract, gas_limit).await
281310
{
282311
tracing::error!(
283-
"Error while processing event for chain: {} and sequence number: {}. Waiting for {} seconds before retry. error: {:?}",
312+
"Chain: {} - error while processing event for sequence number: {}. Waiting for {} seconds before retry. error: {:?}",
284313
&chain_state.id,
285314
&event.sequence_number,
286315
RETRY_INTERVAL.as_secs(),
@@ -290,7 +319,7 @@ pub async fn process_block_range(
290319
}
291320
}
292321
tracing::info!(
293-
"Backlog processed for chain: {} from block: {} to block: {}",
322+
"Chain: {} - backlog processed from block: {} to block: {}",
294323
&chain_state.id,
295324
&current_block,
296325
&to_block
@@ -299,7 +328,7 @@ pub async fn process_block_range(
299328
}
300329
Err(e) => {
301330
tracing::error!(
302-
"Error while getting events for chain: {} from block: {} to block: {}. Waiting for {} seconds before retry. error: {:?}",
331+
"Chain: {} - error while getting events from block: {} to block: {}. Waiting for {} seconds before retry. error: {:?}",
303332
&chain_state.id,
304333
&current_block,
305334
&to_block,
@@ -328,15 +357,26 @@ pub async fn watch_blocks(
328357
geth_rpc_wss: Option<String>,
329358
) -> Result<()> {
330359
tracing::info!(
331-
"Watching blocks to handle new events for chain: {}",
360+
"Chain: {} - watching blocks to handle new events",
332361
&chain_state.id
333362
);
334363
let mut last_safe_block_processed = latest_safe_block;
335364

336365
let provider_option = match geth_rpc_wss {
337-
Some(wss) => Some(Provider::<Ws>::connect(wss).await?),
366+
Some(wss) => Some(match Provider::<Ws>::connect(wss.clone()).await {
367+
Ok(provider) => provider,
368+
Err(e) => {
369+
tracing::error!(
370+
"Chain: {} - error while connecting to wss: {}. error: {:?}",
371+
&chain_state.id,
372+
wss,
373+
e
374+
);
375+
return Err(e.into());
376+
}
377+
}),
338378
None => {
339-
tracing::info!("No wss provided for chain: {}", &chain_state.id);
379+
tracing::info!("Chain: {} - no wss provided", &chain_state.id);
340380
None
341381
}
342382
};
@@ -367,15 +407,19 @@ pub async fn watch_blocks(
367407
{
368408
Ok(_) => {
369409
tracing::info!(
370-
"Block range sent to handle events for chain {}: {} to {}",
410+
"Chain: {} - block range sent to handle events from: {} to: {}",
371411
&chain_state.id,
372412
&last_safe_block_processed + 1,
373413
&latest_safe_block
374414
);
375415
last_safe_block_processed = latest_safe_block;
376416
}
377417
Err(e) => {
378-
tracing::error!("Error while sending block range to handle events for chain {}. These will be handled in next call. error: {:?}",&chain_state.id,e);
418+
tracing::error!(
419+
"Chain: {} - error while sending block range to handle events. These will be handled in next call. error: {:?}",
420+
&chain_state.id,
421+
e
422+
);
379423
}
380424
};
381425
}
@@ -391,7 +435,7 @@ pub async fn process_new_blocks(
391435
) {
392436
loop {
393437
tracing::info!(
394-
"Waiting for new block ranges to process for chain: {}",
438+
"Chain: {} - waiting for new block ranges to process",
395439
&chain_state.id
396440
);
397441
if let Some(block_range) = rx.recv().await {

0 commit comments

Comments
 (0)