diff --git a/rust/main/chains/hyperlane-cosmos/src/cw/mailbox/dispatch_indexer.rs b/rust/main/chains/hyperlane-cosmos/src/cw/mailbox/dispatch_indexer.rs index 93614e9fd8..71bd34ac55 100644 --- a/rust/main/chains/hyperlane-cosmos/src/cw/mailbox/dispatch_indexer.rs +++ b/rust/main/chains/hyperlane-cosmos/src/cw/mailbox/dispatch_indexer.rs @@ -156,9 +156,12 @@ impl SequenceAwareIndexer for CwMailboxDispatchIndexer { async fn latest_sequence_count_and_tip(&self) -> ChainResult<(Option, u32)> { let tip = Indexer::::get_finalized_block_number(&self).await?; - let sequence = self.mailbox.nonce_at_block(tip.into()).await?; + let nonce = self.mailbox.nonce_at_block(tip.into()).await?; + // Convert nonce (next message count) to the last indexed sequence (nonce - 1) + // If nonce is 0, there are no messages sent yet + let sequence = nonce.checked_sub(1); - Ok((Some(sequence), tip)) + Ok((sequence, tip)) } } diff --git a/rust/main/chains/hyperlane-cosmos/src/cw/merkle_tree_hook.rs b/rust/main/chains/hyperlane-cosmos/src/cw/merkle_tree_hook.rs index 78c5c50b0a..de6f009cf7 100644 --- a/rust/main/chains/hyperlane-cosmos/src/cw/merkle_tree_hook.rs +++ b/rust/main/chains/hyperlane-cosmos/src/cw/merkle_tree_hook.rs @@ -142,7 +142,7 @@ impl MerkleTreeHook for CwMerkleTreeHook { merkle_tree_hook_address: self.address, mailbox_domain: self.domain.id(), root: response.root.parse()?, - index: response.count, + index: response.count.saturating_sub(1), }, block_height: Some(height), }) @@ -355,9 +355,12 @@ impl Indexer for CwMerkleTreeHookIndexer { impl SequenceAwareIndexer for CwMerkleTreeHookIndexer { async fn latest_sequence_count_and_tip(&self) -> ChainResult<(Option, u32)> { let tip = CosmosEventIndexer::get_finalized_block_number(self).await?; - let sequence = self.merkle_tree_hook.count_at_block(tip.into()).await?; + let count = self.merkle_tree_hook.count_at_block(tip.into()).await?; + // Convert count to the last indexed sequence (count - 1) + // If count is 0, there are no indexed sequences yet + let sequence = count.checked_sub(1); - Ok((Some(sequence), tip)) + Ok((sequence, tip)) } } diff --git a/rust/main/chains/hyperlane-cosmos/src/native/indexers/dispatch.rs b/rust/main/chains/hyperlane-cosmos/src/native/indexers/dispatch.rs index 13009e4065..b756ebcccc 100644 --- a/rust/main/chains/hyperlane-cosmos/src/native/indexers/dispatch.rs +++ b/rust/main/chains/hyperlane-cosmos/src/native/indexers/dispatch.rs @@ -118,7 +118,9 @@ impl SequenceAwareIndexer for CosmosNativeDispatchIndexer { .mailbox(self.address.encode_hex(), Some(tip as u64)) .await?; match mailbox.mailbox { - Some(mailbox) => Ok((Some(mailbox.message_sent), tip)), + // Convert count to the last indexed sequence (count - 1) + // If count is 0, there are no messages sent yet + Some(mailbox) => Ok((mailbox.message_sent.checked_sub(1), tip)), _ => Ok((None, tip)), } } diff --git a/rust/main/chains/hyperlane-cosmos/src/native/indexers/merkle_tree_hook.rs b/rust/main/chains/hyperlane-cosmos/src/native/indexers/merkle_tree_hook.rs index 3b20fca6e6..5e6a2ac0fd 100644 --- a/rust/main/chains/hyperlane-cosmos/src/native/indexers/merkle_tree_hook.rs +++ b/rust/main/chains/hyperlane-cosmos/src/native/indexers/merkle_tree_hook.rs @@ -255,6 +255,9 @@ impl SequenceAwareIndexer for CosmosNativeMerkleTreeHook { .as_ref() .and_then(|m| m.merkle_tree.as_ref()) .map(|merkle_tree| merkle_tree.count); - Ok((merkle_tree_count, tip)) + // Convert count to the last indexed sequence (count - 1) + // If count is 0, there are no indexed sequences yet + let merkle_tree_sequence = merkle_tree_count.and_then(|count| count.checked_sub(1)); + Ok((merkle_tree_sequence, tip)) } } diff --git a/rust/main/chains/hyperlane-ethereum/src/contracts/mailbox.rs b/rust/main/chains/hyperlane-ethereum/src/contracts/mailbox.rs index 84232dcbe3..4a58d9b9c7 100644 --- a/rust/main/chains/hyperlane-ethereum/src/contracts/mailbox.rs +++ b/rust/main/chains/hyperlane-ethereum/src/contracts/mailbox.rs @@ -200,8 +200,11 @@ where { async fn latest_sequence_count_and_tip(&self) -> ChainResult<(Option, u32)> { let tip = Indexer::::get_finalized_block_number(self).await?; - let sequence = self.contract.nonce().block(u64::from(tip)).call().await?; - Ok((Some(sequence), tip)) + let nonce = self.contract.nonce().block(u64::from(tip)).call().await?; + // Convert nonce (next message count) to the last indexed sequence (nonce - 1) + // If nonce is 0, there are no messages sent yet + let sequence = nonce.checked_sub(1); + Ok((sequence, tip)) } } diff --git a/rust/main/chains/hyperlane-ethereum/src/contracts/merkle_tree_hook.rs b/rust/main/chains/hyperlane-ethereum/src/contracts/merkle_tree_hook.rs index 333c0a51c8..c4ffa9c4ba 100644 --- a/rust/main/chains/hyperlane-ethereum/src/contracts/merkle_tree_hook.rs +++ b/rust/main/chains/hyperlane-ethereum/src/contracts/merkle_tree_hook.rs @@ -185,8 +185,11 @@ where // `SequenceAwareIndexer` and `Indexer`. async fn latest_sequence_count_and_tip(&self) -> ChainResult<(Option, u32)> { let tip = self.get_finalized_block_number().await?; - let sequence = self.contract.count().block(u64::from(tip)).call().await?; - Ok((Some(sequence), tip)) + let count = self.contract.count().block(u64::from(tip)).call().await?; + // Convert count to the last indexed sequence (count - 1) + // If count is 0, there are no messages indexed yet + let sequence = count.checked_sub(1); + Ok((sequence, tip)) } }