Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
Add reason string to InvalidTxData error. (#137)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
joel-u410 authored Feb 19, 2024
1 parent 7d52663 commit 07aa3f9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
8 changes: 5 additions & 3 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> = vec![];
Expand Down Expand Up @@ -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();
Expand All @@ -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" => {
Expand Down
6 changes: 3 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}")]
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 7 additions & 4 deletions src/server/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl TxInfo {
pub fn decode_tx(&mut self, checksums: &HashMap<String, String>) -> 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() {
Expand Down Expand Up @@ -152,19 +152,22 @@ 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
)));
}
};

self.set_tx(decoded);

return Ok(());
}
Err(Error::InvalidTxData)
Err(Error::InvalidTxData("tx is not decrypted".into()))
}

fn decode_ibc(tx_data: &[u8]) -> Result<IbcTx, Error> {
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()
{
Expand Down

0 comments on commit 07aa3f9

Please sign in to comment.