@@ -6,13 +6,13 @@ use std::time::Duration;
66use aligned_sdk:: eth:: batcher_payment_service:: { BatcherPaymentServiceContract , SignatureData } ;
77use ethers:: prelude:: k256:: ecdsa:: SigningKey ;
88use ethers:: prelude:: * ;
9- use log:: debug ;
9+ use log:: { error , info , warn } ;
1010use 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 ) ]
1818pub 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
7784pub 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+ }
0 commit comments