Skip to content

Commit

Permalink
feat: resync nonce before each batch
Browse files Browse the repository at this point in the history
  • Loading branch information
frolvanya committed Feb 11, 2025
1 parent d08902e commit 6605596
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
25 changes: 25 additions & 0 deletions omni-relayer/src/utils/nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ impl NonceManager {
}
}

pub async fn resync_nonce(&self) -> Result<()> {
let current_nonce = self.get_current_nonce().await?;
let mut local_nonce = self
.nonce
.lock()
.map_err(|_| anyhow::anyhow!("Mutex lock error during nonce update"))?;
*local_nonce = current_nonce;

Ok(())
}

pub async fn reserve_nonce(&self) -> Result<u64> {
let current_nonce = self.get_current_nonce().await?;

Expand Down Expand Up @@ -151,6 +162,20 @@ impl EvmNonceManagers {
}
}

pub async fn resync_nonces(&self) -> Result<()> {
if let Some(eth) = self.eth.as_ref() {
eth.resync_nonce().await?;
}
if let Some(base) = self.base.as_ref() {
base.resync_nonce().await?;
}
if let Some(arb) = self.arb.as_ref() {
arb.resync_nonce().await?;
}

Ok(())
}

pub async fn reserve_nonce(&self, chain_kind: ChainKind) -> Result<u64> {
match chain_kind {
ChainKind::Eth => {
Expand Down
4 changes: 4 additions & 0 deletions omni-relayer/src/workers/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ pub async fn finalize_transfer(
continue;
};

if let Err(err) = near_nonce.resync_nonce().await {
warn!("Failed to resync nonce: {}", err);
}

let mut handlers = Vec::new();

for (key, event) in events {
Expand Down
18 changes: 18 additions & 0 deletions omni-relayer/src/workers/near.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ pub async fn sign_transfer(
continue;
};

if let Err(err) = near_nonce.resync_nonce().await {
warn!("Failed to resync nonce: {}", err);
}

let mut handlers = Vec::new();

for (key, event) in events {
if let Ok(init_transfer_with_timestamp) =
serde_json::from_str::<InitTransferWithTimestamp>(&event)
Expand Down Expand Up @@ -228,7 +233,12 @@ pub async fn finalize_transfer(
continue;
};

if let Err(err) = evm_nonces.resync_nonces().await {
warn!("Failed to resync nonces: {}", err);
}

let mut handlers = Vec::new();

for (key, event) in events {
if let Ok(event) = serde_json::from_str::<OmniBridgeEvent>(&event) {
handlers.push(tokio::spawn({
Expand Down Expand Up @@ -417,6 +427,10 @@ pub async fn claim_fee(
continue;
};

if let Err(err) = near_nonce.resync_nonce().await {
warn!("Failed to resync nonce: {}", err);
}

let mut handlers = Vec::new();

for (key, event) in events {
Expand Down Expand Up @@ -686,6 +700,10 @@ pub async fn bind_token(
continue;
};

if let Err(err) = near_nonce.resync_nonce().await {
warn!("Failed to resync nonce: {}", err);
}

let mut handlers = Vec::new();

for (key, event) in events {
Expand Down
4 changes: 4 additions & 0 deletions omni-relayer/src/workers/solana.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ pub async fn finalize_transfer(
continue;
};

if let Err(err) = near_nonce.resync_nonce().await {
warn!("Failed to resync nonce: {}", err);
}

let mut handlers = Vec::new();

for (key, event) in events {
Expand Down

0 comments on commit 6605596

Please sign in to comment.