From e193fca7bb4a76d9d681a6abccdc4429dd5625e1 Mon Sep 17 00:00:00 2001 From: Michael Snoyman Date: Wed, 19 Jun 2024 08:21:36 +0300 Subject: [PATCH] Handle TxInCache --- packages/cosmos/src/client.rs | 2 +- packages/cosmos/src/error.rs | 29 ++++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/packages/cosmos/src/client.rs b/packages/cosmos/src/client.rs index ad9a0fc..bc10097 100644 --- a/packages/cosmos/src/client.rs +++ b/packages/cosmos/src/client.rs @@ -1712,7 +1712,7 @@ impl TxBuilder { // when using all-node broadcasting. if !(self.skip_code_check || res.code == 0 - || (res.codespace == "sdk" && res.code == 19)) + || CosmosSdkError::from_code(res.code, &res.codespace).is_successful_broadcast()) { return Err(crate::Error::TransactionFailed { code: CosmosSdkError::from_code(res.code, &res.codespace), diff --git a/packages/cosmos/src/error.rs b/packages/cosmos/src/error.rs index 8c8f077..1c52ca2 100644 --- a/packages/cosmos/src/error.rs +++ b/packages/cosmos/src/error.rs @@ -439,6 +439,8 @@ pub enum CosmosSdkError { TxTimeoutHeight, /// Code 32 IncorrectAccountSequence, + /// Codespace mempool, Code 3 + TxInCache, /// Some other error code Other { code: u32, codespace: String }, } @@ -451,6 +453,7 @@ impl Display for CosmosSdkError { CosmosSdkError::OutOfGas => f.write_str("out of gas (11)"), CosmosSdkError::InsufficientFee => f.write_str("insufficient fee (13)"), CosmosSdkError::TxInMempool => f.write_str("tx already in mempool (19)"), + CosmosSdkError::TxInCache => f.write_str("tx already in cache (mempool:3)"), CosmosSdkError::TxTooLarge => f.write_str("tx too large (21)"), CosmosSdkError::InvalidChainId => f.write_str("invalid chain ID (28)"), CosmosSdkError::TxTimeoutHeight => f.write_str("tx timeout height (30)"), @@ -482,6 +485,8 @@ impl CosmosSdkError { codespace: codespace.to_owned(), }, } + } else if codespace == "mempool" && code == 3 { + Self::TxInCache } else { Self::Other { code, @@ -489,6 +494,25 @@ impl CosmosSdkError { } } } + + /// Do we consider a broadcast successful? + pub(crate) fn is_successful_broadcast(&self) -> bool { + match self { + CosmosSdkError::TxInMempool | CosmosSdkError::TxInCache => true, + CosmosSdkError::Unauthorized + | CosmosSdkError::InsufficientFunds + | CosmosSdkError::OutOfGas + | CosmosSdkError::InsufficientFee + | CosmosSdkError::TxTooLarge + | CosmosSdkError::InvalidChainId + | CosmosSdkError::TxTimeoutHeight + | CosmosSdkError::IncorrectAccountSequence + | CosmosSdkError::Other { + code: _, + codespace: _, + } => false, + } + } } pub(crate) enum QueryErrorCategory { @@ -514,11 +538,6 @@ impl QueryErrorDetails { QueryErrorDetails::NotFound(_) => ConnectionIsFine, QueryErrorDetails::CosmosSdk { error_code, .. } => { match *error_code { - // tx already in mempool usually indicates some kind of a - // node sync issue is occurring, where the node isn't seeing - // new blocks already containing the transaction/sequence - // number. - CosmosSdkError::TxInMempool => NetworkIssue, // Treat account sequence issue as a transitent issue CosmosSdkError::IncorrectAccountSequence => ConnectionIsFine, // Invalid chain ID, we should try a different node if possible