-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathbatcher_retryables.rs
More file actions
332 lines (309 loc) · 11.4 KB
/
batcher_retryables.rs
File metadata and controls
332 lines (309 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
use std::time::Duration;
use ethers::prelude::*;
use ethers::providers::Http;
use log::{info, warn};
use tokio::time::timeout;
use crate::{
eth::{
payment_service::{BatcherPaymentService, CreateNewTaskFeeParams, SignerMiddlewareT},
utils::get_current_nonce,
},
retry::RetryError,
types::errors::{BatcherError, TransactionSendError},
};
pub async fn get_user_balance_retryable(
payment_service: &BatcherPaymentService,
payment_service_fallback: &BatcherPaymentService,
addr: &Address,
) -> Result<U256, RetryError<String>> {
if let Ok(balance) = payment_service.user_balances(*addr).call().await {
return Ok(balance);
};
payment_service_fallback
.user_balances(*addr)
.call()
.await
.map_err(|e| {
warn!("Failed to get balance for address {:?}. Error: {e}", addr);
RetryError::Transient(e.to_string())
})
}
pub async fn get_user_nonce_from_ethereum_retryable(
payment_service: &BatcherPaymentService,
payment_service_fallback: &BatcherPaymentService,
addr: Address,
) -> Result<U256, RetryError<String>> {
if let Ok(nonce) = payment_service.user_nonces(addr).call().await {
return Ok(nonce);
}
payment_service_fallback
.user_nonces(addr)
.call()
.await
.map_err(|e| {
warn!("Error getting user nonce: {e}");
RetryError::Transient(e.to_string())
})
}
pub async fn get_current_nonce_retryable(
eth_http_provider: &Provider<Http>,
eth_http_provider_fallback: &Provider<Http>,
addr: Address,
) -> Result<U256, RetryError<ProviderError>> {
match eth_http_provider.get_transaction_count(addr, None).await {
Ok(current_nonce) => Ok(current_nonce),
Err(_) => eth_http_provider_fallback
.get_transaction_count(addr, None)
.await
.map_err(|e| {
warn!("Error getting user nonce: {e}");
RetryError::Transient(e)
}),
}
}
pub async fn user_balance_is_unlocked_retryable(
payment_service: &BatcherPaymentService,
payment_service_fallback: &BatcherPaymentService,
addr: &Address,
) -> Result<bool, RetryError<()>> {
if let Ok(unlock_block) = payment_service.user_unlock_block(*addr).call().await {
return Ok(unlock_block != U256::zero());
}
if let Ok(unlock_block) = payment_service_fallback
.user_unlock_block(*addr)
.call()
.await
{
return Ok(unlock_block != U256::zero());
}
warn!("Failed to get user locking state {:?}", addr);
Err(RetryError::Transient(()))
}
pub async fn get_gas_price_retryable(
eth_http_provider: &Provider<Http>,
eth_http_provider_fallback: &Provider<Http>,
) -> Result<U256, RetryError<ProviderError>> {
match eth_http_provider.get_gas_price().await {
Ok(gas_price) => Ok(gas_price),
Err(_) => eth_http_provider_fallback
.get_gas_price()
.await
.map_err(|e| {
warn!("Failed to get fallback gas price: {e:?}");
RetryError::Transient(e)
}),
}
}
pub async fn create_new_task_retryable(
batch_merkle_root: [u8; 32],
batch_data_pointer: String,
proofs_submitters: Vec<Address>,
fee_params: CreateNewTaskFeeParams,
transaction_wait_timeout: u64,
payment_service: &BatcherPaymentService,
payment_service_fallback: &BatcherPaymentService,
) -> Result<TransactionReceipt, RetryError<BatcherError>> {
info!("Creating task for: 0x{}", hex::encode(batch_merkle_root));
let call_fallback;
let call = payment_service
.create_new_task(
batch_merkle_root,
batch_data_pointer.clone(),
proofs_submitters.clone(),
fee_params.fee_for_aggregator,
fee_params.fee_per_proof,
fee_params.respond_to_task_fee_limit,
)
.gas_price(fee_params.gas_price);
let pending_tx = match call.send().await {
Ok(pending_tx) => pending_tx,
Err(ContractError::Revert(err)) => {
// Since transaction was reverted, we don't want to retry with fallback.
warn!("Transaction reverted {:?}", err);
return Err(RetryError::Permanent(BatcherError::TransactionSendError(
TransactionSendError::from(err),
)));
}
_ => {
call_fallback = payment_service_fallback
.create_new_task(
batch_merkle_root,
batch_data_pointer,
proofs_submitters,
fee_params.fee_for_aggregator,
fee_params.fee_per_proof,
fee_params.respond_to_task_fee_limit,
)
.gas_price(fee_params.gas_price);
match call_fallback.send().await {
Ok(pending_tx) => pending_tx,
Err(ContractError::Revert(err)) => {
warn!("Transaction reverted {:?}", err);
return Err(RetryError::Permanent(BatcherError::TransactionSendError(
TransactionSendError::from(err),
)));
}
Err(err) => {
return Err(RetryError::Transient(BatcherError::TransactionSendError(
TransactionSendError::Generic(err.to_string()),
)))
}
}
}
};
// timeout to prevent a deadlock while waiting for the transaction to be included in a block.
timeout(Duration::from_millis(transaction_wait_timeout), pending_tx)
.await
.map_err(|e| {
warn!("Error while waiting for batch inclusion: {e}");
RetryError::Permanent(BatcherError::ReceiptNotFoundError)
})?
.map_err(|e| {
warn!("Error while waiting for batch inclusion: {e}");
RetryError::Permanent(BatcherError::ReceiptNotFoundError)
})?
.ok_or(RetryError::Permanent(BatcherError::ReceiptNotFoundError))
}
pub async fn simulate_create_new_task_retryable(
batch_merkle_root: [u8; 32],
batch_data_pointer: String,
proofs_submitters: Vec<Address>,
fee_params: CreateNewTaskFeeParams,
payment_service: &BatcherPaymentService,
payment_service_fallback: &BatcherPaymentService,
) -> Result<(), RetryError<BatcherError>> {
info!("Simulating task for: 0x{}", hex::encode(batch_merkle_root));
let simulation_fallback;
let simulation = payment_service
.create_new_task(
batch_merkle_root,
batch_data_pointer.clone(),
proofs_submitters.clone(),
fee_params.fee_for_aggregator,
fee_params.fee_per_proof,
fee_params.respond_to_task_fee_limit,
)
.gas_price(fee_params.gas_price);
// sends an `eth_call` request to the node
match simulation.call().await {
Ok(_) => {
info!(
"Simulation task for: 0x{} succeeded.",
hex::encode(batch_merkle_root)
);
Ok(())
}
Err(ContractError::Revert(err)) => {
// Since transaction was reverted, we don't want to retry with fallback.
warn!("Simulated transaction reverted {:?}", err);
Err(RetryError::Permanent(BatcherError::TransactionSendError(
TransactionSendError::from(err),
)))
}
_ => {
simulation_fallback = payment_service_fallback
.create_new_task(
batch_merkle_root,
batch_data_pointer,
proofs_submitters,
fee_params.fee_for_aggregator,
fee_params.fee_per_proof,
fee_params.respond_to_task_fee_limit,
)
.gas_price(fee_params.gas_price);
match simulation_fallback.call().await {
Ok(_) => Ok(()),
Err(ContractError::Revert(err)) => {
warn!("Simulated transaction reverted {:?}", err);
Err(RetryError::Permanent(BatcherError::TransactionSendError(
TransactionSendError::from(err),
)))
}
Err(err) => Err(RetryError::Transient(BatcherError::TransactionSendError(
TransactionSendError::Generic(err.to_string()),
))),
}
}
}
}
pub async fn cancel_create_new_task_retryable(
batcher_signer: &SignerMiddlewareT,
batcher_signer_fallback: &SignerMiddlewareT,
bumped_gas_price: U256,
transaction_wait_timeout: u64,
) -> Result<TransactionReceipt, RetryError<ProviderError>> {
let batcher_addr = batcher_signer.address();
let current_nonce = get_current_nonce(
batcher_signer.provider(),
batcher_signer_fallback.provider(),
batcher_addr,
)
.await
.map_err(RetryError::Transient)?;
let tx = TransactionRequest::new()
.to(batcher_addr)
.value(U256::zero())
.nonce(current_nonce)
.gas_price(bumped_gas_price);
let pending_tx = match batcher_signer.send_transaction(tx.clone(), None).await {
Ok(pending_tx) => pending_tx,
Err(_) => batcher_signer_fallback
.send_transaction(tx.clone(), None)
.await
.map_err(|e| RetryError::Transient(ProviderError::CustomError(e.to_string())))?,
};
// timeout to prevent a deadlock while waiting for the transaction to be included in a block.
timeout(Duration::from_millis(transaction_wait_timeout), pending_tx)
.await
.map_err(|e| {
warn!("Timeout while waiting for transaction inclusion: {e}");
RetryError::Transient(ProviderError::CustomError(format!(
"Timeout while waiting for transaction inclusion: {e}"
)))
})?
.map_err(|e| {
warn!("Error while waiting for tx inclusion: {e}");
RetryError::Transient(e)
})?
.ok_or(RetryError::Transient(ProviderError::CustomError(
"Receipt not found".to_string(),
)))
}
pub async fn get_current_block_number_retryable(
eth_http_provider: &Provider<Http>,
eth_http_provider_fallback: &Provider<Http>,
) -> Result<U64, RetryError<String>> {
if let Ok(block_number) = eth_http_provider.get_block_number().await {
return Ok(block_number);
}
eth_http_provider_fallback
.get_block_number()
.await
.map_err(|e| {
warn!("Failed to get current block number: {e}");
RetryError::Transient(e.to_string())
})
}
pub async fn query_balance_unlocked_events_retryable(
payment_service: &BatcherPaymentService,
payment_service_fallback: &BatcherPaymentService,
from_block: U64,
to_block: U64,
) -> Result<Vec<aligned_sdk::eth::batcher_payment_service::BalanceUnlockedFilter>, RetryError<String>>
{
let filter = payment_service
.balance_unlocked_filter()
.from_block(from_block)
.to_block(to_block);
if let Ok(events) = filter.query().await {
return Ok(events);
}
let filter_fallback = payment_service_fallback
.balance_unlocked_filter()
.from_block(from_block)
.to_block(to_block);
filter_fallback.query().await.map_err(|e| {
warn!("Failed to query BalanceUnlocked events: {e}");
RetryError::Transient(e.to_string())
})
}