Skip to content

Commit abaab06

Browse files
Improved error logging (#664)
Co-authored-by: Nicolas Rampoldi <58613770+NicolasRampoldi@users.noreply.github.com>
1 parent 0cb4b14 commit abaab06

3 files changed

Lines changed: 55 additions & 28 deletions

File tree

batcher/aligned-batcher/src/eth/mod.rs

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ use std::time::Duration;
66
use aligned_sdk::eth::batcher_payment_service::{BatcherPaymentServiceContract, SignatureData};
77
use ethers::prelude::k256::ecdsa::SigningKey;
88
use ethers::prelude::*;
9-
use log::debug;
9+
use log::{error, info, warn};
1010
use tokio::time::sleep;
1111

12-
const CREATE_NEW_TASK_MAX_RETRIES: usize = 100;
13-
const CREATE_NEW_TASK_MILLISECS_BETWEEN_RETRIES: u64 = 100;
12+
const CREATE_NEW_TASK_MAX_RETRIES: usize = 15;
13+
const CREATE_NEW_TASK_MILLISECS_BETWEEN_RETRIES: u64 = 2000;
1414

15-
use crate::config::ECDSAConfig;
15+
use crate::{config::ECDSAConfig, types::errors::BatcherError};
1616

1717
#[derive(Debug, Clone, EthEvent)]
1818
pub struct BatchVerified {
@@ -34,19 +34,14 @@ pub async fn create_new_task(
3434
signatures: Vec<SignatureData>,
3535
gas_for_aggregator: U256,
3636
gas_per_proof: U256,
37-
) -> Result<TransactionReceipt, anyhow::Error> {
37+
) -> Result<TransactionReceipt, BatcherError> {
3838
// pad leaves to next power of 2
39-
let leaves_len = leaves.len();
40-
let last_leaf = leaves[leaves_len - 1];
41-
let leaves = leaves
42-
.into_iter()
43-
.chain(repeat(last_leaf).take(leaves_len.next_power_of_two() - leaves_len))
44-
.collect::<Vec<[u8; 32]>>();
39+
let padded_leaves = pad_leaves(leaves);
4540

4641
let call = payment_service.create_new_task(
4742
batch_merkle_root,
4843
batch_data_pointer,
49-
leaves,
44+
padded_leaves,
5045
signatures,
5146
gas_for_aggregator,
5247
gas_per_proof,
@@ -55,23 +50,35 @@ pub async fn create_new_task(
5550
// If there was a pending transaction from a previously sent batch, the `call.send()` will
5651
// fail because of the nonce not being updated. We should retry sending and not returning an error
5752
// immediatly.
58-
for _ in 0..CREATE_NEW_TASK_MAX_RETRIES {
59-
if let Ok(pending_tx) = call.send().await {
60-
match pending_tx.await? {
61-
Some(receipt) => return Ok(receipt),
62-
None => return Err(anyhow::anyhow!("Receipt not found")),
53+
info!("Creating task for: {:x?}", batch_merkle_root);
54+
55+
for i in 0..CREATE_NEW_TASK_MAX_RETRIES {
56+
match call.send().await {
57+
Ok(pending_tx) => match pending_tx.await {
58+
Ok(Some(receipt)) => return Ok(receipt),
59+
Ok(None) => return Err(BatcherError::ReceiptNotFoundError),
60+
Err(_) => return Err(BatcherError::TransactionSendError),
61+
},
62+
Err(error) => {
63+
if i != CREATE_NEW_TASK_MAX_RETRIES - 1 {
64+
warn!(
65+
"Error when trying to create a task: {}\n Retrying ...",
66+
error
67+
);
68+
} else {
69+
error!("Error when trying to create a task on last retry. Batch task {:x?} will be lost", batch_merkle_root);
70+
return Err(BatcherError::TaskCreationError(error.to_string()));
71+
}
6372
}
64-
}
65-
debug!("createNewTask transaction not sent, retrying in {CREATE_NEW_TASK_MILLISECS_BETWEEN_RETRIES} milliseconds...");
73+
};
74+
6675
sleep(Duration::from_millis(
6776
CREATE_NEW_TASK_MILLISECS_BETWEEN_RETRIES,
6877
))
6978
.await;
7079
}
7180

72-
Err(anyhow::anyhow!(
73-
"Maximum tries reached. Could not send createNewTask call"
74-
))
81+
Err(BatcherError::MaxRetriesReachedError)
7582
}
7683

7784
pub async fn get_batcher_payment_service(
@@ -95,3 +102,12 @@ pub async fn get_batcher_payment_service(
95102

96103
Ok(service_manager)
97104
}
105+
106+
fn pad_leaves(leaves: Vec<[u8; 32]>) -> Vec<[u8; 32]> {
107+
let leaves_len = leaves.len();
108+
let last_leaf = leaves[leaves_len - 1];
109+
leaves
110+
.into_iter()
111+
.chain(repeat(last_leaf).take(leaves_len.next_power_of_two() - leaves_len))
112+
.collect()
113+
}

batcher/aligned-batcher/src/lib.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ impl Batcher {
547547
.map(|(i, signature)| SignatureData::new(signature, nonces[i]))
548548
.collect();
549549

550-
if let Err(e) = eth::create_new_task(
550+
eth::create_new_task(
551551
payment_service,
552552
*batch_merkle_root,
553553
batch_data_pointer,
@@ -556,11 +556,7 @@ impl Batcher {
556556
AGGREGATOR_COST.into(), // FIXME(uri): This value should be read from aligned_layer/contracts/script/deploy/config/devnet/batcher-payment-service.devnet.config.json
557557
gas_per_proof.into(), //FIXME(uri): This value should be read from aligned_layer/contracts/script/deploy/config/devnet/batcher-payment-service.devnet.config.json
558558
)
559-
.await
560-
{
561-
error!("Failed to create batch verification task: {}", e);
562-
return Err(BatcherError::TaskCreationError(e.to_string()));
563-
}
559+
.await?;
564560

565561
info!("Batch verification task created on Aligned contract");
566562
Ok(())

batcher/aligned-batcher/src/types/errors.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ pub enum BatcherError {
99
EthereumSubscriptionError(String),
1010
SignatureError(SignatureError),
1111
TaskCreationError(String),
12+
ReceiptNotFoundError,
13+
TransactionSendError,
14+
MaxRetriesReachedError,
1215
}
1316

1417
impl From<tungstenite::Error> for BatcherError {
@@ -41,6 +44,18 @@ impl fmt::Debug for BatcherError {
4144
BatcherError::TaskCreationError(e) => {
4245
write!(f, "Task creation error: {}", e)
4346
}
47+
BatcherError::ReceiptNotFoundError => {
48+
write!(f, "Receipt not found")
49+
}
50+
BatcherError::TransactionSendError => {
51+
write!(f, "Error sending tx")
52+
}
53+
BatcherError::MaxRetriesReachedError => {
54+
write!(
55+
f,
56+
"Maximum tries reached. Could not send createNewTask call"
57+
)
58+
}
4459
}
4560
}
4661
}

0 commit comments

Comments
 (0)