Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update sov-rollup-starter #215

Merged
merged 10 commits into from
Mar 14, 2024
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod event;
pub mod poll_timeout;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use core::time::Duration;

use hermes_relayer_components::transaction::components::poll_tx_response::PollTimeoutGetter;

pub struct DefaultPollTimeout;

impl<Chain> PollTimeoutGetter<Chain> for DefaultPollTimeout {
fn poll_timeout(_chain: &Chain) -> Duration {
Duration::from_secs(300)
}

fn poll_backoff(_chain: &Chain) -> Duration {
Duration::from_millis(200)
}
}
11 changes: 11 additions & 0 deletions crates/cosmos/cosmos-relayer/src/impls/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::convert::Infallible;
use core::num::ParseIntError;

use crate::contexts::transaction::CosmosTxContext;
use crate::types::error::{HandleInfallible, ProvideCosmosError};
use alloc::string::FromUtf8Error;
use cgp_core::prelude::*;
Expand All @@ -11,6 +12,8 @@ use hermes_cosmos_client_components::impls::queries::abci::AbciQueryError;
use hermes_protobuf_components::impls::any::TypeUrlMismatchError;
use hermes_relayer_components::chain::traits::types::chain_id::HasChainIdType;
use hermes_relayer_components::relay::impls::create_client::MissingCreateClientEventError;
use hermes_relayer_components::transaction::components::poll_tx_response::TxNoResponseError;
use hermes_relayer_components::transaction::traits::types::HasTransactionHashType;
use hermes_relayer_runtime::types::error::TokioRuntimeError;
use hermes_test_components::chain::impls::assert::poll_assert_eventual_amount::EventualAmountTimeoutError;
use hermes_test_components::chain::impls::ibc_transfer::MissingSendPacketEventError;
Expand All @@ -34,6 +37,7 @@ pub trait CheckErrorRaiser<Context>:
ErrorRaiser<Context, TokioRuntimeError>
+ for<'a> ErrorRaiser<Context, &'a str>
+ for<'a> ErrorRaiser<Context, EventualAmountTimeoutError<'a, CosmosChain>>
+ for<'a> ErrorRaiser<Context, TxNoResponseError<'a, CosmosTxContext>>
where
Context: HasErrorType<Error = Error>,
{
Expand Down Expand Up @@ -96,6 +100,13 @@ where
type Delegate = DebugError;
}

impl<'a, Chain> DelegateComponent<TxNoResponseError<'a, Chain>> for HandleCosmosError
where
Chain: HasTransactionHashType,
{
type Delegate = DebugError;
}

impl<'a, Chain, Counterparty>
DelegateComponent<MissingCreateClientEventError<'a, Chain, Counterparty>> for HandleCosmosError
where
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use cgp_core::{
HasComponents,
};
use hermes_cosmos_client_components::impls::transaction::event::ParseCosmosTxResponseAsEvents;
use hermes_cosmos_client_components::impls::transaction::poll_timeout::DefaultPollTimeout;
use hermes_cosmos_client_components::impls::types::chain::ProvideCosmosChainTypes;
use hermes_cosmos_client_components::impls::types::transaction::ProvideCosmosTransactionTypes;
use hermes_relayer_components::chain::traits::types::chain_id::ChainIdTypeComponent;
Expand All @@ -15,6 +16,7 @@ use hermes_relayer_components::logger::traits::has_logger::{
LoggerFieldComponent, LoggerTypeComponent,
};
use hermes_relayer_components::runtime::traits::runtime::RuntimeTypeComponent;
use hermes_relayer_components::transaction::components::poll_tx_response::PollTimeoutGetterComponent;
use hermes_relayer_components::transaction::traits::event::TxResponseAsEventsParserComponent;
use hermes_relayer_components::transaction::traits::types::{
FeeTypeComponent, NonceTypeComponent, SignerTypeComponent, TransactionHashTypeComponent,
Expand Down Expand Up @@ -69,6 +71,8 @@ delegate_components! {
LoggerFieldComponent,
]:
ProvideTracingLogger,
PollTimeoutGetterComponent:
DefaultPollTimeout,
TxResponseAsEventsParserComponent:
ParseCosmosTxResponseAsEvents,
}
Expand Down
12 changes: 0 additions & 12 deletions crates/cosmos/cosmos-relayer/src/impls/transaction/error.rs

This file was deleted.

13 changes: 0 additions & 13 deletions crates/cosmos/cosmos-relayer/src/impls/transaction/fields.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use core::time::Duration;

use futures::lock::Mutex;
use hermes_relayer_components::chain::traits::types::chain_id::ChainIdGetter;
use hermes_relayer_components::runtime::traits::mutex::MutexGuardOf;
use hermes_relayer_components::transaction::components::poll_tx_response::HasPollTimeout;
use hermes_relayer_components::transaction::traits::fee::HasFeeForSimulation;
use hermes_relayer_components::transaction::traits::nonce::mutex::HasMutexForNonceAllocation;
use hermes_relayer_components::transaction::traits::signer::HasDefaultSigner;
Expand Down Expand Up @@ -32,16 +29,6 @@ impl HasFeeForSimulation for CosmosTxContext {
}
}

impl HasPollTimeout for CosmosTxContext {
fn poll_timeout(&self) -> Duration {
Duration::from_secs(300)
}

fn poll_backoff(&self) -> Duration {
Duration::from_millis(200)
}
}

impl HasMutexForNonceAllocation for CosmosTxContext {
fn mutex_for_nonce_allocation(&self, _signer: &Secp256k1KeyPair) -> &Mutex<()> {
&self.nonce_mutex
Expand Down
1 change: 0 additions & 1 deletion crates/cosmos/cosmos-relayer/src/impls/transaction/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod component;
pub mod components;
pub mod error;
pub mod fields;
pub mod log;
pub mod types;
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cgp_core::prelude::*;
use cgp_core::CanRaiseError;

use crate::chain::traits::send_message::{CanSendMessages, MessageSenderComponent};
use crate::chain::traits::types::chain_id::HasChainId;
Expand All @@ -13,7 +14,7 @@ use crate::transaction::components::allocate_nonce_and_send_messages::AllocateNo
use crate::transaction::components::allocate_nonce_with_mutex::AllocateNonceWithMutex;
use crate::transaction::components::estimate_fees_and_send_tx::EstimateFeesAndSendTx;
use crate::transaction::components::poll_tx_response::{
CanRaiseNoTxResponseError, HasPollTimeout, PollTxResponse,
HasPollTimeout, PollTxResponse, TxNoResponseError,
};
use crate::transaction::components::send_messages_with_default_signer::SendMessagesWithDefaultSigner;
use crate::transaction::traits::components::nonce_allocater::{
Expand Down Expand Up @@ -96,7 +97,7 @@ where
+ HasLogger
+ CanLogNonce
+ CanParseTxResponseAsEvents
+ CanRaiseNoTxResponseError
+ for<'a> CanRaiseError<TxNoResponseError<'a, Chain>>
+ HasComponents<Components = Components>,
Chain::Runtime: HasMutex + HasTime + CanSleep,
Chain::Logger: HasBaseLogLevels,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use core::fmt::Debug;
use core::time::Duration;

use cgp_core::HasErrorType;
use cgp_core::prelude::*;
use cgp_core::CanRaiseError;

use crate::logger::traits::level::HasBaseLogLevels;
use crate::runtime::traits::runtime::HasRuntime;
Expand All @@ -11,66 +13,86 @@ use crate::transaction::traits::components::tx_response_querier::CanQueryTxRespo
use crate::transaction::traits::logs::logger::CanLogTx;
use crate::transaction::traits::types::HasTransactionHashType;

pub trait CanRaiseNoTxResponseError: HasTransactionHashType + HasErrorType {
fn tx_no_response_error(tx_hash: &Self::TxHash) -> Self::Error;
pub struct PollTxResponse;

pub struct TxNoResponseError<'a, Chain>
where
Chain: HasTransactionHashType,
{
pub chain: &'a Chain,
pub tx_hash: &'a Chain::TxHash,
}

impl<'a, Chain> Debug for TxNoResponseError<'a, Chain>
where
Chain: HasTransactionHashType,
Chain::TxHash: Debug,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("TxNoResponseError")
.field("tx_hash", &self.tx_hash)
.finish()
}
}

#[derive_component(PollTimeoutGetterComponent, PollTimeoutGetter<Chain>)]
pub trait HasPollTimeout {
fn poll_timeout(&self) -> Duration;

fn poll_backoff(&self) -> Duration;
}

pub struct PollTxResponse;

impl<Context> TxResponsePoller<Context> for PollTxResponse
impl<Chain> TxResponsePoller<Chain> for PollTxResponse
where
Context:
CanLogTx + CanQueryTxResponse + HasPollTimeout + HasRuntime + CanRaiseNoTxResponseError,
Context::Runtime: HasTime + CanSleep,
Chain: CanLogTx
+ CanQueryTxResponse
+ HasPollTimeout
+ HasRuntime
+ for<'a> CanRaiseError<TxNoResponseError<'a, Chain>>,
Chain::Runtime: HasTime + CanSleep,
{
async fn poll_tx_response(
context: &Context,
tx_hash: &Context::TxHash,
) -> Result<Context::TxResponse, Context::Error> {
let runtime = context.runtime();
let wait_timeout = context.poll_timeout();
let wait_backoff = context.poll_backoff();
chain: &Chain,
tx_hash: &Chain::TxHash,
) -> Result<Chain::TxResponse, Chain::Error> {
let runtime = chain.runtime();
let wait_timeout = chain.poll_timeout();
let wait_backoff = chain.poll_backoff();

let start_time = runtime.now();

loop {
let response = context.query_tx_response(tx_hash).await;
let response = chain.query_tx_response(tx_hash).await;

match response {
Ok(None) => {
let elapsed = Context::Runtime::duration_since(&start_time, &runtime.now());
let elapsed = Chain::Runtime::duration_since(&start_time, &runtime.now());
if elapsed > wait_timeout {
context.log_tx(
Context::Logger::LEVEL_ERROR,
chain.log_tx(
Chain::Logger::LEVEL_ERROR,
"no tx response received, and poll timeout has recached. returning error",
|log| {
log.debug("elapsed", &elapsed).debug("wait_timeout", &wait_timeout);
}
);

return Err(Context::tx_no_response_error(tx_hash));
return Err(Chain::raise_error(TxNoResponseError { chain, tx_hash }));
} else {
runtime.sleep(wait_backoff).await;
}
}
Ok(Some(response)) => {
context.log_tx(
Context::Logger::LEVEL_TRACE,
chain.log_tx(
Chain::Logger::LEVEL_TRACE,
"received tx response, finish polling",
|_| {},
);

return Ok(response);
}
Err(e) => {
context.log_tx(
Context::Logger::LEVEL_ERROR,
chain.log_tx(
Chain::Logger::LEVEL_ERROR,
"query_tx_response returned error",
|log| {
log.debug("error", &e);
Expand All @@ -85,9 +107,11 @@ where

However, if the query still returns error after the wait timeout exceeded,
we return the error we get from the query.

TODO: check whether the error is retryable before re-polling.
*/

let elapsed = Context::Runtime::duration_since(&start_time, &runtime.now());
let elapsed = Chain::Runtime::duration_since(&start_time, &runtime.now());
if elapsed > wait_timeout {
return Err(e);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cgp_core::prelude::*;
use hermes_cosmos_client_components::impls::transaction::poll_timeout::DefaultPollTimeout;
use hermes_relayer_components::chain::traits::types::chain_id::ChainIdTypeComponent;
use hermes_relayer_components::chain::traits::types::channel::{
ChannelHandshakePayloadTypeComponent, InitChannelOptionsTypeComponent,
Expand All @@ -16,6 +17,10 @@ use hermes_relayer_components::chain::traits::types::message::MessageTypeCompone
use hermes_relayer_components::chain::traits::types::packet::IbcPacketTypesProviderComponent;
use hermes_relayer_components::chain::traits::types::timestamp::TimestampTypeComponent;
use hermes_relayer_components::chain::traits::types::update_client::UpdateClientPayloadTypeComponent;
use hermes_relayer_components::transaction::components::poll_tx_response::{
PollTimeoutGetterComponent, PollTxResponse,
};
use hermes_relayer_components::transaction::traits::components::tx_response_poller::TxResponsePollerComponent;
use hermes_relayer_components::transaction::traits::components::tx_response_querier::TxResponseQuerierComponent;
use hermes_relayer_components::transaction::traits::event::TxResponseAsEventsParserComponent;
use hermes_relayer_components::transaction::traits::types::{
Expand Down Expand Up @@ -73,6 +78,10 @@ delegate_components! {
PublishSovereignTransactionBatch,
TxResponseQuerierComponent:
QuerySovereignTxResponse,
TxResponsePollerComponent:
PollTxResponse,
PollTimeoutGetterComponent:
DefaultPollTimeout,
TxResponseAsEventsParserComponent:
ParseSovTxResponseAsEvents,
}
Expand Down

This file was deleted.

This file was deleted.

Loading
Loading