Skip to content

Commit

Permalink
refactor(IndexMode): remove IndexRange, use default index mode per …
Browse files Browse the repository at this point in the history
…env (#2562)

### Description

Removes the `index_mode` config field, default to whichever mode is
currently supported by our existing envs. Doing this allows for safely
removing runtime checks to see if the correct index mode was configured.
Additional work is required to support multiple indexing modes per env.

Check this discord thread for the full discussion:
https://discord.com/channels/935678348330434570/961711527092682783/1133347733411987568

### Drive-by changes

- `IndexRange` is removed entirely and replaced by `RangeInclusive<u32>`
- Moves `is_arbitrum_nitro` into a bigger existing `impl
HyperlaneDomain` block for clarity

### Related issues

This is part 1 of
#2511

### Backward compatibility

Yes

### Testing

Relies on existing e2e tests
  • Loading branch information
daniel-savu authored Aug 1, 2023
1 parent 018f018 commit 127294d
Show file tree
Hide file tree
Showing 15 changed files with 368 additions and 213 deletions.
12 changes: 8 additions & 4 deletions rust/agents/relayer/src/relayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,11 @@ impl Relayer {
&self,
origin: &HyperlaneDomain,
) -> Instrumented<JoinHandle<eyre::Result<()>>> {
let index_settings = self.as_ref().settings.chains[origin.name()].index.clone();
let (index_settings, index_mode) =
self.as_ref().settings.chains[origin.name()].index_settings_and_mode();
let contract_sync = self.message_syncs.get(origin).unwrap().clone();
let cursor = contract_sync
.forward_backward_message_sync_cursor(index_settings)
.forward_backward_message_sync_cursor(index_settings, index_mode)
.await;
tokio::spawn(async move {
contract_sync
Expand All @@ -284,13 +285,16 @@ impl Relayer {
&self,
origin: &HyperlaneDomain,
) -> Instrumented<JoinHandle<eyre::Result<()>>> {
let index_settings = self.as_ref().settings.chains[origin.name()].index.clone();
let (index_settings, index_mode) =
self.as_ref().settings.chains[origin.name()].index_settings_and_mode();
let contract_sync = self
.interchain_gas_payment_syncs
.get(origin)
.unwrap()
.clone();
let cursor = contract_sync.rate_limited_cursor(index_settings).await;
let cursor = contract_sync
.rate_limited_cursor(index_settings, index_mode)
.await;
tokio::spawn(async move { contract_sync.clone().sync("gas_payments", cursor).await })
.instrument(info_span!("ContractSync"))
}
Expand Down
8 changes: 6 additions & 2 deletions rust/agents/scraper/src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ macro_rules! spawn_sync_task {
.await
.unwrap();
let cursor = sync
.$cursor(index_settings.clone())
.$cursor(index_settings.clone(), domain.clone().index_mode())
.await;
tokio::spawn(async move {
sync
Expand Down Expand Up @@ -275,7 +275,11 @@ impl Scraper {
.unwrap_or(None)
.unwrap_or(0);
let cursor = sync
.forward_message_sync_cursor(index_settings.clone(), latest_nonce.saturating_sub(1))
.forward_message_sync_cursor(
index_settings.clone(),
domain.index_mode(),
latest_nonce.saturating_sub(1),
)
.await;
tokio::spawn(async move { sync.sync("message_dispatch", cursor).await }).instrument(
info_span!("ChainContractSync", chain=%domain.name(), event="message_dispatch"),
Expand Down
7 changes: 3 additions & 4 deletions rust/agents/validator/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,11 @@ impl BaseAgent for Validator {

impl Validator {
async fn run_message_sync(&self) -> Instrumented<JoinHandle<Result<()>>> {
let index_settings = self.as_ref().settings.chains[self.origin_chain.name()]
.index
.clone();
let (index_settings, index_mode) =
self.as_ref().settings.chains[self.origin_chain.name()].index_settings_and_mode();
let contract_sync = self.message_sync.clone();
let cursor = contract_sync
.forward_backward_message_sync_cursor(index_settings)
.forward_backward_message_sync_cursor(index_settings, index_mode)
.await;
tokio::spawn(async move {
contract_sync
Expand Down
36 changes: 23 additions & 13 deletions rust/chains/hyperlane-ethereum/src/interchain_gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

use std::collections::HashMap;
use std::fmt::Display;
use std::ops::RangeInclusive;
use std::sync::Arc;

use async_trait::async_trait;
use ethers::prelude::Middleware;
use tracing::instrument;

use hyperlane_core::{
BlockRange, ChainCommunicationError, ChainResult, ContractLocator, HyperlaneAbi,
HyperlaneChain, HyperlaneContract, HyperlaneDomain, HyperlaneProvider, IndexRange, Indexer,
InterchainGasPaymaster, InterchainGasPayment, LogMeta, H160, H256,
ChainCommunicationError, ChainResult, ContractLocator, HyperlaneAbi, HyperlaneChain,
HyperlaneContract, HyperlaneDomain, HyperlaneProvider, Indexer, InterchainGasPaymaster,
InterchainGasPayment, LogMeta, SequenceIndexer, H160, H256,
};
use tracing::instrument;

use crate::contracts::i_interchain_gas_paymaster::{
IInterchainGasPaymaster as EthereumInterchainGasPaymasterInternal, IINTERCHAINGASPAYMASTER_ABI,
Expand All @@ -36,7 +36,7 @@ pub struct InterchainGasPaymasterIndexerBuilder {

#[async_trait]
impl BuildableWithProvider for InterchainGasPaymasterIndexerBuilder {
type Output = Box<dyn Indexer<InterchainGasPayment>>;
type Output = Box<dyn SequenceIndexer<InterchainGasPayment>>;

async fn build_with_provider<M: Middleware + 'static>(
&self,
Expand Down Expand Up @@ -87,14 +87,8 @@ where
#[instrument(err, skip(self))]
async fn fetch_logs(
&self,
range: IndexRange,
range: RangeInclusive<u32>,
) -> ChainResult<Vec<(InterchainGasPayment, LogMeta)>> {
let BlockRange(range) = range else {
return Err(ChainCommunicationError::from_other_str(
"EthereumInterchainGasPaymasterIndexer only supports block-based indexing",
));
};

let events = self
.contract
.gas_payment_filter()
Expand Down Expand Up @@ -130,6 +124,22 @@ where
}
}

#[async_trait]
impl<M> SequenceIndexer<InterchainGasPayment> for EthereumInterchainGasPaymasterIndexer<M>
where
M: Middleware + 'static,
{
async fn sequence_at_tip(&self) -> ChainResult<(u32, u32)> {
// The InterchainGasPaymasterIndexerBuilder must return a `SequenceIndexer` type.
// It's fine if only a blanket implementation is provided for EVM chains, since their
// indexing only uses the `Index` trait, which is a supertrait of `SequenceIndexer`.
// TODO: if `SequenceIndexer` turns out to not depend on `Indexer` at all, then the supertrait
// dependency could be removed, even if the builder would still need to return a type that is both
// ``SequenceIndexer` and `Indexer`.
panic!("Gas payment nonce indexing not implemented");
}
}

pub struct InterchainGasPaymasterBuilder {}

#[async_trait]
Expand Down
42 changes: 23 additions & 19 deletions rust/chains/hyperlane-ethereum/src/mailbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@

use std::collections::HashMap;
use std::num::NonZeroU64;
use std::ops::RangeInclusive;
use std::sync::Arc;

use async_trait::async_trait;
use ethers::abi::AbiEncode;
use ethers::prelude::Middleware;
use ethers_contract::builders::ContractCall;
use hyperlane_core::SequenceIndexer;
use tracing::instrument;

use hyperlane_core::accumulator::incremental::IncrementalMerkle;
use hyperlane_core::accumulator::TREE_DEPTH;
use hyperlane_core::{
utils::fmt_bytes, BlockRange, ChainCommunicationError, ChainResult, Checkpoint,
ContractLocator, HyperlaneAbi, HyperlaneChain, HyperlaneContract, HyperlaneDomain,
HyperlaneMessage, HyperlaneProtocolError, HyperlaneProvider, IndexRange, Indexer, LogMeta,
Mailbox, MessageIndexer, RawHyperlaneMessage, TxCostEstimate, TxOutcome, H160, H256, U256,
utils::fmt_bytes, ChainCommunicationError, ChainResult, Checkpoint, ContractLocator,
HyperlaneAbi, HyperlaneChain, HyperlaneContract, HyperlaneDomain, HyperlaneMessage,
HyperlaneProtocolError, HyperlaneProvider, Indexer, LogMeta, Mailbox, MessageIndexer,
RawHyperlaneMessage, TxCostEstimate, TxOutcome, H160, H256, U256,
};

use crate::contracts::arbitrum_node_interface::ArbitrumNodeInterface;
Expand Down Expand Up @@ -65,7 +67,7 @@ pub struct DeliveryIndexerBuilder {

#[async_trait]
impl BuildableWithProvider for DeliveryIndexerBuilder {
type Output = Box<dyn Indexer<H256>>;
type Output = Box<dyn SequenceIndexer<H256>>;

async fn build_with_provider<M: Middleware + 'static>(
&self,
Expand Down Expand Up @@ -130,13 +132,10 @@ where
}

#[instrument(err, skip(self))]
async fn fetch_logs(&self, range: IndexRange) -> ChainResult<Vec<(HyperlaneMessage, LogMeta)>> {
let BlockRange(range) = range else {
return Err(ChainCommunicationError::from_other_str(
"EthereumMailboxIndexer only supports block-based indexing",
))
};

async fn fetch_logs(
&self,
range: RangeInclusive<u32>,
) -> ChainResult<Vec<(HyperlaneMessage, LogMeta)>> {
let mut events: Vec<(HyperlaneMessage, LogMeta)> = self
.contract
.dispatch_filter()
Expand Down Expand Up @@ -178,13 +177,7 @@ where
}

#[instrument(err, skip(self))]
async fn fetch_logs(&self, range: IndexRange) -> ChainResult<Vec<(H256, LogMeta)>> {
let BlockRange(range) = range else {
return Err(ChainCommunicationError::from_other_str(
"EthereumMailboxIndexer only supports block-based indexing",
))
};

async fn fetch_logs(&self, range: RangeInclusive<u32>) -> ChainResult<Vec<(H256, LogMeta)>> {
Ok(self
.contract
.process_id_filter()
Expand All @@ -197,6 +190,17 @@ where
.collect())
}
}

#[async_trait]
impl<M> SequenceIndexer<H256> for EthereumMailboxIndexer<M>
where
M: Middleware + 'static,
{
async fn sequence_at_tip(&self) -> ChainResult<(u32, u32)> {
panic!("Message delivery sequence indexing not implemented");
}
}

pub struct MailboxBuilder {}

#[async_trait]
Expand Down
6 changes: 4 additions & 2 deletions rust/chains/hyperlane-fuel/src/interchain_gas.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::ops::RangeInclusive;

use async_trait::async_trait;

use hyperlane_core::{
ChainResult, HyperlaneChain, HyperlaneContract, IndexRange, Indexer, InterchainGasPaymaster,
ChainResult, HyperlaneChain, HyperlaneContract, Indexer, InterchainGasPaymaster,
};
use hyperlane_core::{HyperlaneDomain, HyperlaneProvider, InterchainGasPayment, LogMeta, H256};

Expand Down Expand Up @@ -35,7 +37,7 @@ pub struct FuelInterchainGasPaymasterIndexer {}
impl Indexer<InterchainGasPayment> for FuelInterchainGasPaymasterIndexer {
async fn fetch_logs(
&self,
range: IndexRange,
range: RangeInclusive<u32>,
) -> ChainResult<Vec<(InterchainGasPayment, LogMeta)>> {
todo!()
}
Expand Down
11 changes: 7 additions & 4 deletions rust/chains/hyperlane-fuel/src/mailbox.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::fmt::{Debug, Formatter};
use std::num::NonZeroU64;
use std::ops::RangeInclusive;

use async_trait::async_trait;
use fuels::prelude::{Bech32ContractId, WalletUnlocked};
Expand All @@ -10,8 +11,7 @@ use tracing::instrument;
use hyperlane_core::{
utils::fmt_bytes, ChainCommunicationError, ChainResult, Checkpoint, ContractLocator,
HyperlaneAbi, HyperlaneChain, HyperlaneContract, HyperlaneDomain, HyperlaneMessage,
HyperlaneProvider, IndexRange, Indexer, LogMeta, Mailbox, TxCostEstimate, TxOutcome, H256,
U256,
HyperlaneProvider, Indexer, LogMeta, Mailbox, TxCostEstimate, TxOutcome, H256, U256,
};

use crate::{
Expand Down Expand Up @@ -154,7 +154,10 @@ pub struct FuelMailboxIndexer {}

#[async_trait]
impl Indexer<HyperlaneMessage> for FuelMailboxIndexer {
async fn fetch_logs(&self, range: IndexRange) -> ChainResult<Vec<(HyperlaneMessage, LogMeta)>> {
async fn fetch_logs(
&self,
range: RangeInclusive<u32>,
) -> ChainResult<Vec<(HyperlaneMessage, LogMeta)>> {
todo!()
}

Expand All @@ -165,7 +168,7 @@ impl Indexer<HyperlaneMessage> for FuelMailboxIndexer {

#[async_trait]
impl Indexer<H256> for FuelMailboxIndexer {
async fn fetch_logs(&self, range: IndexRange) -> ChainResult<Vec<(H256, LogMeta)>> {
async fn fetch_logs(&self, range: RangeInclusive<u32>) -> ChainResult<Vec<(H256, LogMeta)>> {
todo!()
}

Expand Down
16 changes: 13 additions & 3 deletions rust/chains/hyperlane-sealevel/src/interchain_gas.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::ops::RangeInclusive;

use async_trait::async_trait;
use hyperlane_core::{
ChainResult, ContractLocator, HyperlaneChain, HyperlaneContract, HyperlaneDomain,
HyperlaneProvider, IndexRange, Indexer, InterchainGasPaymaster, InterchainGasPayment, LogMeta,
H256,
HyperlaneProvider, Indexer, InterchainGasPaymaster, InterchainGasPayment, LogMeta,
SequenceIndexer, H256,
};
use tracing::{info, instrument};

Expand Down Expand Up @@ -61,7 +63,7 @@ impl Indexer<InterchainGasPayment> for SealevelInterchainGasPaymasterIndexer {
#[instrument(err, skip(self))]
async fn fetch_logs(
&self,
_range: IndexRange,
_range: RangeInclusive<u32>,
) -> ChainResult<Vec<(InterchainGasPayment, LogMeta)>> {
info!("Gas payment indexing not implemented for Sealevel");
Ok(vec![])
Expand All @@ -74,3 +76,11 @@ impl Indexer<InterchainGasPayment> for SealevelInterchainGasPaymasterIndexer {
Ok(1)
}
}

#[async_trait]
impl SequenceIndexer<InterchainGasPayment> for SealevelInterchainGasPaymasterIndexer {
async fn sequence_at_tip(&self) -> ChainResult<(u32, u32)> {
info!("Gas payment indexing not implemented for Sealevel");
Ok((1, 1))
}
}
31 changes: 19 additions & 12 deletions rust/chains/hyperlane-sealevel/src/mailbox.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(warnings)] // FIXME remove

use std::{collections::HashMap, num::NonZeroU64, str::FromStr as _};
use std::{collections::HashMap, num::NonZeroU64, ops::RangeInclusive, str::FromStr as _};

use async_trait::async_trait;
use borsh::{BorshDeserialize, BorshSerialize};
Expand All @@ -10,8 +10,8 @@ use tracing::{debug, info, instrument, warn};
use hyperlane_core::{
accumulator::incremental::IncrementalMerkle, ChainCommunicationError, ChainResult, Checkpoint,
ContractLocator, Decode as _, Encode as _, HyperlaneAbi, HyperlaneChain, HyperlaneContract,
HyperlaneDomain, HyperlaneMessage, HyperlaneProvider, IndexRange, Indexer, LogMeta, Mailbox,
MessageIndexer, SequenceRange, TxCostEstimate, TxOutcome, H256, U256,
HyperlaneDomain, HyperlaneMessage, HyperlaneProvider, Indexer, LogMeta, Mailbox,
MessageIndexer, SequenceIndexer, TxCostEstimate, TxOutcome, H256, U256,
};
use hyperlane_sealevel_interchain_security_module_interface::{
InterchainSecurityModuleInstruction, VerifyInstruction,
Expand Down Expand Up @@ -701,19 +701,17 @@ impl MessageIndexer for SealevelMailboxIndexer {

#[async_trait]
impl Indexer<HyperlaneMessage> for SealevelMailboxIndexer {
async fn fetch_logs(&self, range: IndexRange) -> ChainResult<Vec<(HyperlaneMessage, LogMeta)>> {
let SequenceRange(range) = range else {
return Err(ChainCommunicationError::from_other_str(
"SealevelMailboxIndexer only supports sequence-based indexing",
))
};

async fn fetch_logs(
&self,
range: RangeInclusive<u32>,
) -> ChainResult<Vec<(HyperlaneMessage, LogMeta)>> {
info!(
?range,
"Fetching SealevelMailboxIndexer HyperlaneMessage logs"
);

let mut messages = Vec::with_capacity((range.end() - range.start()) as usize);
let message_capacity = range.end().saturating_sub(*range.start());
let mut messages = Vec::with_capacity(message_capacity as usize);
for nonce in range {
messages.push(self.get_message_with_nonce(nonce).await?);
}
Expand All @@ -727,7 +725,7 @@ impl Indexer<HyperlaneMessage> for SealevelMailboxIndexer {

#[async_trait]
impl Indexer<H256> for SealevelMailboxIndexer {
async fn fetch_logs(&self, _range: IndexRange) -> ChainResult<Vec<(H256, LogMeta)>> {
async fn fetch_logs(&self, _range: RangeInclusive<u32>) -> ChainResult<Vec<(H256, LogMeta)>> {
todo!()
}

Expand All @@ -736,6 +734,15 @@ impl Indexer<H256> for SealevelMailboxIndexer {
}
}

#[async_trait]
impl SequenceIndexer<H256> for SealevelMailboxIndexer {
async fn sequence_at_tip(&self) -> ChainResult<(u32, u32)> {
// TODO: implement when sealevel scraper support is implemented
info!("Message delivery indexing not implemented");
Ok((1, 1))
}
}

struct SealevelMailboxAbi;

// TODO figure out how this is used and if we can support it for sealevel.
Expand Down
Loading

0 comments on commit 127294d

Please sign in to comment.