Skip to content

Commit

Permalink
Allow sub-contexts to have different Error types with functions to …
Browse files Browse the repository at this point in the history
…inject errors into `Self::Error` (#2932)

* Use explicit injection for runtime error

* Allow Src/Dst chain Error type to be different from Relay::Error

Only remaining fix needed is the same error constraint in BatchContext,
which can be fixed in #2816.

* Remove From<TokioError> which is no longer needed
  • Loading branch information
soareschen authored Dec 12, 2022
1 parent 751a41f commit 05d635c
Show file tree
Hide file tree
Showing 45 changed files with 242 additions and 186 deletions.
6 changes: 0 additions & 6 deletions crates/relayer-cosmos/src/base/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,3 @@ impl Clone for Error {
Error(self.detail().clone(), ErrorMessageTracer::new_message(self))
}
}

impl From<TokioError> for Error {
fn from(e: TokioError) -> Error {
Error::tokio(e)
}
}
11 changes: 8 additions & 3 deletions crates/relayer-cosmos/src/base/impls/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use ibc_relayer_framework::base::one_for_all::traits::chain::{
OfaBaseChain, OfaChainTypes, OfaIbcChain,
};
use ibc_relayer_framework::base::one_for_all::traits::runtime::OfaRuntimeContext;
use ibc_relayer_runtime::tokio::context::TokioRuntimeContext;
use ibc_relayer_runtime::tokio::error::Error as TokioError;
use ibc_relayer_types::clients::ics07_tendermint::consensus_state::ConsensusState;
use ibc_relayer_types::core::ics04_channel::events::WriteAcknowledgement;
use ibc_relayer_types::core::ics04_channel::packet::Sequence;
Expand All @@ -27,7 +29,6 @@ use crate::base::error::Error;
use crate::base::traits::chain::CosmosChain;
use crate::base::types::chain::CosmosChainWrapper;
use crate::base::types::message::CosmosIbcMessage;
use crate::base::types::runtime::CosmosRuntimeContext;

impl<Chain> OfaChainTypes for CosmosChainWrapper<Chain>
where
Expand All @@ -37,7 +38,7 @@ where

type Error = Error;

type Runtime = CosmosRuntimeContext;
type Runtime = TokioRuntimeContext;

type Height = Height;

Expand Down Expand Up @@ -73,6 +74,10 @@ impl<Chain> OfaBaseChain for CosmosChainWrapper<Chain>
where
Chain: CosmosChain,
{
fn runtime_error(e: TokioError) -> Error {
Error::tokio(e)
}

fn encode_raw_message(message: &CosmosIbcMessage, signer: &Signer) -> Result<Any, Error> {
(message.to_protobuf_fn)(signer).map_err(Error::encode)
}
Expand Down Expand Up @@ -108,7 +113,7 @@ where
}
}

fn runtime(&self) -> &OfaRuntimeContext<CosmosRuntimeContext> {
fn runtime(&self) -> &OfaRuntimeContext<TokioRuntimeContext> {
&self.runtime
}

Expand Down
19 changes: 16 additions & 3 deletions crates/relayer-cosmos/src/base/impls/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ use crate::base::traits::relay::CosmosRelay;
use crate::base::types::chain::CosmosChainWrapper;
use crate::base::types::message::CosmosIbcMessage;
use crate::base::types::relay::CosmosRelayWrapper;
use crate::base::types::runtime::CosmosRuntimeContext;
use ibc_relayer_runtime::tokio::context::TokioRuntimeContext;
use ibc_relayer_runtime::tokio::error::Error as TokioError;

impl<Relay> OfaRelayTypes for CosmosRelayWrapper<Relay>
where
Expand All @@ -33,7 +34,7 @@ where

type Error = Error;

type Runtime = CosmosRuntimeContext;
type Runtime = TokioRuntimeContext;

type Packet = Packet;

Expand All @@ -47,6 +48,18 @@ impl<Relay> OfaBaseRelay for CosmosRelayWrapper<Relay>
where
Relay: CosmosRelay,
{
fn runtime_error(e: TokioError) -> Error {
Error::tokio(e)
}

fn src_chain_error(e: Error) -> Error {
e
}

fn dst_chain_error(e: Error) -> Error {
e
}

fn mismatch_ibc_events_count_error(expected: usize, actual: usize) -> Self::Error {
Error::mismatch_ibc_events_count(expected, actual)
}
Expand Down Expand Up @@ -90,7 +103,7 @@ where
&packet.timeout_timestamp
}

fn runtime(&self) -> &OfaRuntimeContext<CosmosRuntimeContext> {
fn runtime(&self) -> &OfaRuntimeContext<TokioRuntimeContext> {
&self.runtime
}

Expand Down
7 changes: 3 additions & 4 deletions crates/relayer-cosmos/src/base/types/chain.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use alloc::sync::Arc;
use ibc_relayer_framework::base::one_for_all::traits::runtime::OfaRuntimeContext;

use crate::base::types::runtime::CosmosRuntimeContext;
use ibc_relayer_runtime::tokio::context::TokioRuntimeContext;

#[derive(Clone)]
pub struct CosmosChainWrapper<Chain> {
pub chain: Arc<Chain>,
pub runtime: OfaRuntimeContext<CosmosRuntimeContext>,
pub runtime: OfaRuntimeContext<TokioRuntimeContext>,
}

impl<Chain> CosmosChainWrapper<Chain> {
pub fn new(chain: Arc<Chain>, runtime: CosmosRuntimeContext) -> Self {
pub fn new(chain: Arc<Chain>, runtime: TokioRuntimeContext) -> Self {
Self {
chain,
runtime: OfaRuntimeContext::new(runtime),
Expand Down
1 change: 0 additions & 1 deletion crates/relayer-cosmos/src/base/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod chain;
pub mod message;
pub mod relay;
pub mod runtime;
6 changes: 3 additions & 3 deletions crates/relayer-cosmos/src/base/types/relay.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
use alloc::sync::Arc;
use ibc_relayer_framework::base::one_for_all::traits::runtime::OfaRuntimeContext;
use ibc_relayer_framework::common::one_for_all::types::chain::OfaChainWrapper;
use ibc_relayer_runtime::tokio::context::TokioRuntimeContext;

use crate::base::traits::relay::CosmosRelay;
use crate::base::types::chain::CosmosChainWrapper;
use crate::base::types::runtime::CosmosRuntimeContext;

#[derive(Clone)]
pub struct CosmosRelayWrapper<Relay: CosmosRelay> {
pub relay: Arc<Relay>,
pub src_chain: OfaChainWrapper<CosmosChainWrapper<Relay::SrcChain>>,
pub dst_chain: OfaChainWrapper<CosmosChainWrapper<Relay::DstChain>>,
pub runtime: OfaRuntimeContext<CosmosRuntimeContext>,
pub runtime: OfaRuntimeContext<TokioRuntimeContext>,
}

impl<Relay: CosmosRelay> CosmosRelayWrapper<Relay> {
pub fn new(relay: Arc<Relay>, runtime: CosmosRuntimeContext) -> Self {
pub fn new(relay: Arc<Relay>, runtime: TokioRuntimeContext) -> Self {
let src_chain = OfaChainWrapper::new(CosmosChainWrapper::new(
relay.src_chain().clone(),
runtime.clone(),
Expand Down
5 changes: 0 additions & 5 deletions crates/relayer-cosmos/src/base/types/runtime.rs

This file was deleted.

4 changes: 2 additions & 2 deletions crates/relayer-cosmos/src/contexts/full/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use ibc_relayer::chain::handle::ChainHandle;
use ibc_relayer::keyring::KeyEntry;
use ibc_relayer_framework::common::one_for_all::presets::FullPreset;
use ibc_relayer_framework::full::batch::context::new_batch_channel;
use ibc_relayer_framework::full::one_for_all::traits::batch::OfaBatchContext;
use ibc_relayer_framework::full::one_for_all::traits::batch::OfaBatchWrapper;
use ibc_relayer_framework::full::one_for_all::traits::telemetry::OfaTelemetryWrapper;
use ibc_relayer_types::signer::Signer;

Expand Down Expand Up @@ -31,7 +31,7 @@ impl<Handle: ChainHandle> CosmosChainContext<Handle> {
key_entry: KeyEntry,
telemetry: CosmosTelemetry,
) -> Self {
let batch_channel = new_batch_channel::<OfaBatchContext<CosmosChainWrapper<Self>>>();
let batch_channel = new_batch_channel::<OfaBatchWrapper<CosmosChainWrapper<Self>>>();

let chain = Self {
handle,
Expand Down
4 changes: 2 additions & 2 deletions crates/relayer-cosmos/src/contexts/full/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ use ibc_relayer_framework::full::batch::config::BatchConfig;
use ibc_relayer_framework::full::batch::spawn::{
BatchMessageWorkerSpawner, CanSpawnBatchMessageWorker,
};
use ibc_relayer_runtime::tokio::context::TokioRuntimeContext;

use crate::base::traits::chain::CosmosChain;
use crate::base::traits::relay::CosmosRelay;
use crate::base::types::relay::CosmosRelayWrapper;
use crate::base::types::runtime::CosmosRuntimeContext;
use crate::contexts::full::chain::CosmosChainContext;
use crate::full::traits::relay::CosmosFullRelay;

Expand Down Expand Up @@ -66,7 +66,7 @@ where
}

pub fn new_relay_context_with_batch<SrcChain, DstChain>(
runtime: CosmosRuntimeContext,
runtime: TokioRuntimeContext,
src_chain: CosmosChainContext<SrcChain>,
dst_chain: CosmosChainContext<DstChain>,
src_to_dst_client: ForeignClient<DstChain, SrcChain>,
Expand Down
4 changes: 2 additions & 2 deletions crates/relayer-cosmos/src/full/impls/chain.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use ibc_relayer_framework::full::one_for_all::traits::chain::OfaFullChain;
use ibc_relayer_framework::full::one_for_all::traits::telemetry::OfaTelemetryWrapper;
use ibc_relayer_runtime::tokio::context::TokioRuntimeContext;

use crate::base::types::chain::CosmosChainWrapper;
use crate::base::types::runtime::CosmosRuntimeContext;
use crate::full::traits::chain::CosmosFullChain;
use crate::full::types::batch::CosmosBatchChannel;
use crate::full::types::telemetry::CosmosTelemetry;
Expand All @@ -11,7 +11,7 @@ impl<Chain> OfaFullChain for CosmosChainWrapper<Chain>
where
Chain: CosmosFullChain,
{
type BatchContext = CosmosRuntimeContext;
type BatchContext = TokioRuntimeContext;

type Telemetry = CosmosTelemetry;

Expand Down
4 changes: 2 additions & 2 deletions crates/relayer-cosmos/src/transaction/impls/broadcast.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use async_trait::async_trait;
use ibc_proto::cosmos::tx::v1beta1::TxRaw;
use ibc_relayer::chain::cosmos::types::tx::SignedTx;
use ibc_relayer_framework::base::core::traits::error::{HasError, InjectError};
use ibc_relayer_framework::base::core::traits::error::{HasErrorType, InjectError};
use prost::EncodeError;
use tendermint_rpc::endpoint::broadcast::tx_sync::Response;
use tendermint_rpc::error::Error as RpcError;
Expand All @@ -10,7 +10,7 @@ use tendermint_rpc::Client;
use crate::transaction::traits::fields::{HasRpcAddress, HasRpcClient};

#[async_trait]
pub trait CanBroadcastTxSync: HasError {
pub trait CanBroadcastTxSync: HasErrorType {
async fn broadcast_tx_sync(&self, tx: SignedTx) -> Result<Response, Self::Error>;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use ibc_proto::cosmos::auth::v1beta1::{BaseAccount, EthAccount};
use ibc_proto::google::protobuf::Any;
use ibc_relayer_framework::base::core::traits::error::HasError;
use ibc_relayer_framework::base::core::traits::error::HasErrorType;
use prost::{DecodeError, Message};

use crate::transaction::traits::decoders::account::AccountDecoder;

pub struct DecodeCosmosOrEthAccount;

pub trait InjectDecodeCosmosOrEthAccountError: HasError {
pub trait InjectDecodeCosmosOrEthAccountError: HasErrorType {
fn unknown_account_type_error(type_url: &str) -> Self::Error;

fn empty_base_account_error() -> Self::Error;
Expand Down
20 changes: 10 additions & 10 deletions crates/relayer-cosmos/src/transaction/impls/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ use ibc_relayer::chain::cosmos::types::account::AccountSequence;
use ibc_relayer::chain::cosmos::types::tx::SignedTx;
use ibc_relayer::config::AddressType;
use ibc_relayer::keyring::errors::Error as KeyringError;
use ibc_relayer_framework::base::core::traits::error::{HasError, InjectError};
use ibc_relayer_framework::base::core::traits::error::{HasErrorType, InjectError};
use prost::EncodeError;

use crate::transaction::traits::fields::{
HasAccountNumber, HasAddressType, HasChainId, HasExtensionOptions, HasKeyEntry, HasMemo,
};

#[async_trait]
pub trait CanSignTx: HasError {
pub trait CanSignTx: HasErrorType {
async fn sign_tx(
&self,
fee: &Fee,
Expand All @@ -23,36 +23,36 @@ pub trait CanSignTx: HasError {
) -> Result<SignedTx, Self::Error>;
}

trait CanEncodeKeyBytes: HasError {
trait CanEncodeKeyBytes: HasErrorType {
fn encode_key_bytes(&self) -> Result<Vec<u8>, Self::Error>;
}

#[async_trait]
trait CanEncodeSignerInfo: HasError {
trait CanEncodeSignerInfo: HasErrorType {
async fn encode_signer_info(
&self,
account_sequence: &AccountSequence,
key_bytes: Vec<u8>,
) -> Result<SignerInfo, Self::Error>;
}

trait CanEncodeTxBodyAndBytes: HasError {
trait CanEncodeTxBodyAndBytes: HasErrorType {
fn encode_tx_body_and_bytes(&self, messages: &[Any]) -> Result<(TxBody, Vec<u8>), Self::Error>;
}

trait CanEncodeAuthInfoAndBytes: HasError {
trait CanEncodeAuthInfoAndBytes: HasErrorType {
fn encode_auth_info_and_bytes(
signer_info: SignerInfo,
fee: Fee,
) -> Result<(AuthInfo, Vec<u8>), Self::Error>;
}

trait CanSignMessage: HasError {
trait CanSignMessage: HasErrorType {
fn sign_message(&self, message: Vec<u8>) -> Result<Vec<u8>, Self::Error>;
}

#[async_trait]
trait CanEncodeSignDoc: HasError {
trait CanEncodeSignDoc: HasErrorType {
async fn encode_sign_doc(
&self,
auth_info_bytes: Vec<u8>,
Expand All @@ -63,7 +63,7 @@ trait CanEncodeSignDoc: HasError {
#[async_trait]
impl<Context> CanSignTx for Context
where
Context: HasError
Context: HasErrorType
+ CanEncodeKeyBytes
+ CanEncodeSignerInfo
+ CanEncodeTxBodyAndBytes
Expand Down Expand Up @@ -119,7 +119,7 @@ where
#[async_trait]
impl<Context> CanEncodeSignerInfo for Context
where
Context: HasError + HasAddressType,
Context: HasErrorType + HasAddressType,
{
async fn encode_signer_info(
&self,
Expand Down
10 changes: 5 additions & 5 deletions crates/relayer-cosmos/src/transaction/impls/estimate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use core::marker::PhantomData;
use ibc_proto::cosmos::tx::v1beta1::Fee;
use ibc_relayer::chain::cosmos::gas::gas_amount_to_fee;
use ibc_relayer::chain::cosmos::types::tx::SignedTx;
use ibc_relayer_framework::base::core::traits::error::{HasError, InjectError};
use ibc_relayer_framework::base::core::traits::error::{HasErrorType, InjectError};
use ibc_relayer_types::core::ics24_host::identifier::ChainId;
use tracing::{debug, error, warn};

use crate::transaction::impls::simulate::CanSendTxSimulate;
use crate::transaction::traits::fields::{HasChainId, HasDefaultGas, HasGasConfig, HasMaxGas};

#[async_trait]
pub trait CanEstimateTxFees: HasError {
pub trait CanEstimateTxFees: HasErrorType {
async fn estimate_tx_fees(&self, tx: SignedTx) -> Result<Fee, Self::Error>;
}

Expand All @@ -33,12 +33,12 @@ where
}

#[async_trait]
pub trait TxGasEstimator<Context: HasError> {
pub trait TxGasEstimator<Context: HasErrorType> {
async fn estimate_gas_with_tx(context: &Context, tx: SignedTx) -> Result<u64, Context::Error>;
}

#[async_trait]
pub trait CanEstimateTxGas: HasError {
pub trait CanEstimateTxGas: HasErrorType {
async fn estimate_gas_with_tx(&self, tx: SignedTx) -> Result<u64, Self::Error>;
}

Expand Down Expand Up @@ -69,7 +69,7 @@ where

pub struct RecoverableTxGasEstimator<InEstimator>(PhantomData<InEstimator>);

pub trait HasRecoverableErrorForSimulation: HasError {
pub trait HasRecoverableErrorForSimulation: HasErrorType {
fn can_recover_from_simulation_failure(e: &Self::Error) -> bool;
}

Expand Down
Loading

0 comments on commit 05d635c

Please sign in to comment.