Skip to content

Commit 1b49724

Browse files
store pending withdrawals always before delivery (#74)
* store pending withdrawals always before delivery * store receipts before auto accumulating * rework log messages * cargo fmt --------- Co-authored-by: David Salami <wizdave97@gmail.com>
1 parent ea2aa4b commit 1b49724

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

messaging/src/lib.rs

+16-15
Original file line numberDiff line numberDiff line change
@@ -267,17 +267,20 @@ where
267267
// We should not store messages when they are delivered to hyperbridge
268268
if chain_a.state_machine_id().state_id != coprocessor.state_machine() {
269269
if !receipts.is_empty() {
270+
// Store receipts in database before auto accumulation
271+
tracing::trace!(target: "tesseract", "Persisting {} deliveries from {}->{} to the db", receipts.len(), chain_b.name(), chain_a.name());
272+
if let Err(err) = tx_payment.store_messages(receipts.clone()).await {
273+
tracing::error!(
274+
"Failed to persist {} deliveries to database: {err:?}",
275+
receipts.len()
276+
)
277+
}
270278
// Send receipts to the fee accumulation task
271279
match fee_acc_sender.send(receipts).await {
272-
Err(sent) => {
273-
// If for aome reason the receiver has been dropped, store receipts
274-
// in db so they can be accumulated manually
275-
tracing::info!(target: "tesseract", "Storing {} deliveries from {} to {} inside the database", sent.0.len(), chain_b.name(), chain_a.name());
276-
if let Err(err) = tx_payment.store_messages(sent.0).await {
277-
tracing::error!(
278-
"Failed to store some delivered messages to database: {err:?}"
279-
)
280-
}
280+
Err(_sent) => {
281+
tracing::error!(
282+
"Fee auto accumulation failed You can try again manually"
283+
)
281284
},
282285
_ => {},
283286
}
@@ -378,23 +381,21 @@ async fn fee_accumulation<
378381
.await?;
379382

380383
observe_challenge_period(&dest, &hyperbridge, dest_height).await?;
384+
let mut commitments = vec![];
381385
for proof in proofs {
386+
commitments.extend_from_slice(&proof.commitments);
382387
hyperbridge.accumulate_fees(proof).await?;
383388
}
384389
tracing::info!("Fee accumulation was sucessful");
385-
390+
// If delete fails, not an issue, they'll be deleted whenever manual accumulation is triggered
391+
let _ = tx_payment.delete_claimed_entries(commitments).await;
386392
Ok::<_, anyhow::Error>(())
387393
};
388394

389395
match lambda().await {
390396
Ok(()) => {},
391397
Err(err) => {
392398
tracing::error!("Error accummulating some fees, receipts have been stored in the db, you can try again manually \n{err:?}");
393-
if let Err(err) = tx_payment.store_messages(receipts).await {
394-
tracing::error!(
395-
"Failed to store some delivered messages to database: {err:?}"
396-
)
397-
}
398399
}
399400
}
400401
}

relayer/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tesseract"
3-
version = "0.2.8"
3+
version = "0.2.9"
44
edition = "2021"
55
description = "Chain agnostic relayer implementation for ISMP"
66
authors = ["Polytope Labs <hello@polytope.technology>"]

relayer/src/fees.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ where
288288
let frequency = Duration::from_secs(config.withdrawal_frequency.unwrap_or(86_400));
289289
tracing::info!("Auto-withdraw frequency set to {:?}", frequency);
290290
// default to $100
291-
let min_amount: U256 = (config.minimum_withdrawal_amount.unwrap_or(100) * 10u64.pow(18)).into();
291+
let min_amount: U256 =
292+
(config.minimum_withdrawal_amount.unwrap_or(100) as u128 * 10u128.pow(18)).into();
292293

293294
tracing::info!("Minimum auto-withdrawal amount set to ${:?}", Cost(min_amount));
294295
let mut interval = interval(frequency);
@@ -326,13 +327,18 @@ where
326327
.await?;
327328
tracing::info!("Request submitted to hyperbridge successfully");
328329
tracing::info!("Starting delivery of withdrawal message to {:?}", chain);
330+
331+
// persist the withdrawal in-case delivery fails, so it's not lost forever
332+
let ids = moved_db.store_pending_withdrawals(vec![result.clone()]).await?;
333+
329334
match deliver_post_request(&client, &hyperbridge, result.clone()).await {
330-
Ok(_) => {},
335+
Ok(_) => {
336+
if let Err(e) = moved_db.delete_pending_withdrawals(ids).await {
337+
tracing::error!("Error encountered while deleting pending withdrawals from the db: {e:?}, \n NOTE: The withdrawal request was successfully delivered.");
338+
}
339+
},
331340
Err(err) => {
332-
// persist the withdrawal in-case delivery fails, so it's not lost forever
333-
tracing::error!("Encountered an error while delivering withdrawal request: {err:?}");
334-
moved_db.store_pending_withdrawals(vec![result]).await?;
335-
tracing::info!("Stored the failed withdrawal request in the db, so they can be retried later.");
341+
tracing::info!("Failed to deliver withdrawal request: {err:?}, they will be retried.");
336342
}
337343
};
338344
Ok(())

0 commit comments

Comments
 (0)