From c810e818410caba9ca956f193045c7db0e865020 Mon Sep 17 00:00:00 2001 From: Gianmarco Fraccaroli Date: Mon, 30 Dec 2024 17:02:42 +0100 Subject: [PATCH] transaction: add status endpoint --- webserver/src/app.rs | 4 +++ webserver/src/handler/transaction.rs | 39 ++++++++++++++++++++++++++- webserver/src/response/transaction.rs | 16 +++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/webserver/src/app.rs b/webserver/src/app.rs index 7151795d..c59c7f4f 100644 --- a/webserver/src/app.rs +++ b/webserver/src/app.rs @@ -116,6 +116,10 @@ impl ApplicationServer { "/chain/wrapper/:id", get(transaction_handlers::get_wrapper_tx), ) + .route( + "/chain/tx/:id/status", + get(transaction_handlers::get_tx_status), + ) .route( "/chain/inner/:id", get(transaction_handlers::get_inner_tx), diff --git a/webserver/src/handler/transaction.rs b/webserver/src/handler/transaction.rs index 4f094383..9238193c 100644 --- a/webserver/src/handler/transaction.rs +++ b/webserver/src/handler/transaction.rs @@ -8,7 +8,8 @@ use crate::dto::transaction::TransactionHistoryQueryParams; use crate::error::api::ApiError; use crate::error::transaction::TransactionError; use crate::response::transaction::{ - InnerTransaction, TransactionHistory, WrapperTransaction, + InnerTransaction, TransactionHistory, TransactionResult, TransactionStatus, + TransactionStructKind, WrapperTransaction, }; use crate::response::utils::PaginatedResponse; use crate::state::common::CommonState; @@ -81,6 +82,42 @@ pub async fn get_transaction_history( Ok(Json(response)) } +#[debug_handler] +pub async fn get_tx_status( + _headers: HeaderMap, + Path(tx_id): Path, + State(state): State, +) -> Result, ApiError> { + is_valid_hash(&tx_id)?; + + let wrapper_tx = state + .transaction_service + .get_wrapper_tx(tx_id.clone()) + .await?; + + let tx_status = if let Some(tx) = wrapper_tx { + TransactionStatus { + kind: TransactionStructKind::Wrapper, + status: tx.exit_code, + } + } else { + let tx = state.transaction_service.get_inner_tx(tx_id).await?; + + match tx { + Some(tx) => TransactionStatus { + kind: TransactionStructKind::Inner, + status: tx.exit_code, + }, + None => TransactionStatus { + kind: TransactionStructKind::Unknown, + status: TransactionResult::Unknown, + }, + } + }; + + Ok(Json(tx_status)) +} + fn is_valid_hash(hash: &str) -> Result<(), TransactionError> { if hash.len().eq(&64) { Ok(()) diff --git a/webserver/src/response/transaction.rs b/webserver/src/response/transaction.rs index 156161f4..9300b548 100644 --- a/webserver/src/response/transaction.rs +++ b/webserver/src/response/transaction.rs @@ -9,6 +9,7 @@ use serde::{Deserialize, Serialize}; pub enum TransactionResult { Applied, Rejected, + Unknown, } #[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq, Hash)] @@ -183,3 +184,18 @@ impl TransactionHistory { } } } + +#[derive(Clone, Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub enum TransactionStructKind { + Wrapper, + Inner, + Unknown, +} + +#[derive(Clone, Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct TransactionStatus { + pub kind: TransactionStructKind, + pub status: TransactionResult, +}