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

transaction: add status endpoint #241

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions webserver/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
39 changes: 38 additions & 1 deletion webserver/src/handler/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String>,
State(state): State<CommonState>,
) -> Result<Json<TransactionStatus>, 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(())
Expand Down
16 changes: 16 additions & 0 deletions webserver/src/response/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use serde::{Deserialize, Serialize};
pub enum TransactionResult {
Applied,
Rejected,
Unknown,
}

#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq, Hash)]
Expand Down Expand Up @@ -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,
}
Loading