From 07aa3f926a15a65b5a0c368d03a1f51972c5287a Mon Sep 17 00:00:00 2001 From: Joel Nordell <94570446+joel-u410@users.noreply.github.com> Date: Mon, 19 Feb 2024 06:21:24 -0600 Subject: [PATCH] Add reason string to InvalidTxData error. (#137) There are several different places where `Error::InvalidTxData` can be produced; this adds a `String` value to it so that every place that results in this error can also provide a helpful explanation. --- src/database.rs | 8 +++++--- src/error.rs | 6 +++--- src/server/tx.rs | 11 +++++++---- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/database.rs b/src/database.rs index 398e260..5812a72 100644 --- a/src/database.rs +++ b/src/database.rs @@ -596,7 +596,7 @@ impl Database { let mut i: usize = 0; for t in txs.iter() { - let tx = Tx::try_from(t.as_slice()).map_err(|_| Error::InvalidTxData)?; + let tx = Tx::try_from(t.as_slice()).map_err(|e| Error::InvalidTxData(e.to_string()))?; let mut code = Default::default(); let mut txid_wrapper: Vec = vec![]; @@ -642,7 +642,7 @@ impl Database { .get_section(tx.code_sechash()) .and_then(|s| s.code_sec()) .map(|s| s.code.hash().0) - .ok_or(Error::InvalidTxData)?; + .ok_or(Error::InvalidTxData("no code hash".into()))?; let code_hex = hex::encode(code.as_slice()); let unknown_type = "unknown".to_string(); @@ -653,7 +653,9 @@ impl Database { // decode tx_transfer, tx_bond and tx_unbound to store the decoded data in their tables // if the transaction has failed don't try to decode because the changes are not included and the data might not be correct if return_code.unwrap() == 0 { - let data = tx.data().ok_or(Error::InvalidTxData)?; + let data = tx + .data() + .ok_or(Error::InvalidTxData("tx has no data".into()))?; match type_tx.as_str() { "tx_transfer" => { diff --git a/src/error.rs b/src/error.rs index c33d49c..99f8cae 100644 --- a/src/error.rs +++ b/src/error.rs @@ -18,8 +18,8 @@ use tokio::sync::mpsc::error::SendError; pub enum Error { #[error("Invalid Block data")] InvalidBlockData, - #[error("Invalid Transaction data")] - InvalidTxData, + #[error("Invalid Transaction data (reason: {0})")] + InvalidTxData(String), #[error("Tendermint error: {0}")] TendermintError(#[from] TError), #[error("Tendermint rpc_error: {0}")] @@ -71,7 +71,7 @@ impl IntoResponse for Error { fn into_response(self) -> Response { let status = match self { Error::InvalidBlockData => StatusCode::EXPECTATION_FAILED, - Error::InvalidTxData => StatusCode::EXPECTATION_FAILED, + Error::InvalidTxData(_) => StatusCode::EXPECTATION_FAILED, Error::DB(_) => StatusCode::NOT_FOUND, Error::HexError(_) => StatusCode::BAD_REQUEST, Error::TendermintError(_) => StatusCode::EXPECTATION_FAILED, diff --git a/src/server/tx.rs b/src/server/tx.rs index 9dc72ec..e120741 100644 --- a/src/server/tx.rs +++ b/src/server/tx.rs @@ -113,7 +113,7 @@ impl TxInfo { pub fn decode_tx(&mut self, checksums: &HashMap) -> Result<(), Error> { if self.is_decrypted() { let Some(type_tx) = checksums.get(&self.code()) else { - return Err(Error::InvalidTxData); + return Err(Error::InvalidTxData("failed to get checksum".into())); }; let decoded = match type_tx.as_str() { @@ -152,7 +152,10 @@ impl TxInfo { PendingTransfer::try_from_slice(&self.data()).map(TxDecoded::EthPoolBridge)? } _ => { - return Err(Error::InvalidTxData); + return Err(Error::InvalidTxData(format!( + "unsupported type_tx {}", + type_tx + ))); } }; @@ -160,11 +163,11 @@ impl TxInfo { return Ok(()); } - Err(Error::InvalidTxData) + Err(Error::InvalidTxData("tx is not decrypted".into())) } fn decode_ibc(tx_data: &[u8]) -> Result { - let msg = Any::decode(tx_data).map_err(|_| Error::InvalidTxData)?; + let msg = Any::decode(tx_data).map_err(|e| Error::InvalidTxData(e.to_string()))?; if msg.type_url.as_str() == MSG_TRANSFER_TYPE_URL && MsgTransfer::try_from(msg.clone()).is_ok() {