Skip to content

Commit 5a24c5e

Browse files
JulianVenturaJulian VenturaMarcosNicolauavilagaston9
authored
feat: add simulate_create_new_task (#1506)
Co-authored-by: Julian Ventura <[email protected]> Co-authored-by: Marcos Nicolau <[email protected]> Co-authored-by: Avila Gastón <[email protected]> Co-authored-by: avilagaston9 <[email protected]>
1 parent 052ac25 commit 5a24c5e

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

batcher/aligned-batcher/src/lib.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use ethers::contract::ContractError;
88
use ethers::signers::Signer;
99
use retry::batcher_retryables::{
1010
cancel_create_new_task_retryable, create_new_task_retryable, get_user_balance_retryable,
11-
get_user_nonce_from_ethereum_retryable, user_balance_is_unlocked_retryable,
11+
get_user_nonce_from_ethereum_retryable, simulate_create_new_task_retryable,
12+
user_balance_is_unlocked_retryable,
1213
};
1314
use retry::{retry_function, RetryError};
1415
use tokio::time::timeout;
@@ -1479,6 +1480,27 @@ impl Batcher {
14791480
proof_submitters: Vec<Address>,
14801481
fee_params: CreateNewTaskFeeParams,
14811482
) -> Result<TransactionReceipt, BatcherError> {
1483+
// First, we simulate the tx
1484+
retry_function(
1485+
|| {
1486+
simulate_create_new_task_retryable(
1487+
batch_merkle_root,
1488+
batch_data_pointer.clone(),
1489+
proof_submitters.clone(),
1490+
fee_params.clone(),
1491+
&self.payment_service,
1492+
&self.payment_service_fallback,
1493+
)
1494+
},
1495+
ETHEREUM_CALL_MIN_RETRY_DELAY,
1496+
ETHEREUM_CALL_BACKOFF_FACTOR,
1497+
ETHEREUM_CALL_MAX_RETRIES,
1498+
ETHEREUM_CALL_MAX_RETRY_DELAY,
1499+
)
1500+
.await
1501+
.map_err(|e| e.inner())?;
1502+
1503+
// Then, we send the real tx
14821504
let result = retry_function(
14831505
|| {
14841506
create_new_task_retryable(

batcher/aligned-batcher/src/retry/batcher_retryables.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,63 @@ pub async fn create_new_task_retryable(
175175
.ok_or(RetryError::Permanent(BatcherError::ReceiptNotFoundError))
176176
}
177177

178+
pub async fn simulate_create_new_task_retryable(
179+
batch_merkle_root: [u8; 32],
180+
batch_data_pointer: String,
181+
proofs_submitters: Vec<Address>,
182+
fee_params: CreateNewTaskFeeParams,
183+
payment_service: &BatcherPaymentService,
184+
payment_service_fallback: &BatcherPaymentService,
185+
) -> Result<(), RetryError<BatcherError>> {
186+
info!("Simulating task for: 0x{}", hex::encode(batch_merkle_root));
187+
let simulation_fallback;
188+
let simulation = payment_service
189+
.create_new_task(
190+
batch_merkle_root,
191+
batch_data_pointer.clone(),
192+
proofs_submitters.clone(),
193+
fee_params.fee_for_aggregator,
194+
fee_params.fee_per_proof,
195+
fee_params.respond_to_task_fee_limit,
196+
)
197+
.gas_price(fee_params.gas_price);
198+
// sends an `eth_call` request to the node
199+
match simulation.call().await {
200+
Ok(_) => Ok(()),
201+
Err(ContractError::Revert(err)) => {
202+
// Since transaction was reverted, we don't want to retry with fallback.
203+
warn!("Simulated transaction reverted {:?}", err);
204+
Err(RetryError::Permanent(BatcherError::TransactionSendError(
205+
TransactionSendError::from(err),
206+
)))
207+
}
208+
_ => {
209+
simulation_fallback = payment_service_fallback
210+
.create_new_task(
211+
batch_merkle_root,
212+
batch_data_pointer,
213+
proofs_submitters,
214+
fee_params.fee_for_aggregator,
215+
fee_params.fee_per_proof,
216+
fee_params.respond_to_task_fee_limit,
217+
)
218+
.gas_price(fee_params.gas_price);
219+
match simulation_fallback.call().await {
220+
Ok(_) => Ok(()),
221+
Err(ContractError::Revert(err)) => {
222+
warn!("Simulated transaction reverted {:?}", err);
223+
Err(RetryError::Permanent(BatcherError::TransactionSendError(
224+
TransactionSendError::from(err),
225+
)))
226+
}
227+
Err(err) => Err(RetryError::Transient(BatcherError::TransactionSendError(
228+
TransactionSendError::Generic(err.to_string()),
229+
))),
230+
}
231+
}
232+
}
233+
}
234+
178235
pub async fn cancel_create_new_task_retryable(
179236
batcher_signer: &SignerMiddlewareT,
180237
batcher_signer_fallback: &SignerMiddlewareT,

0 commit comments

Comments
 (0)