Skip to content

Commit

Permalink
add tx_status_api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
dailinsubjam committed Nov 6, 2024
1 parent 3982bc3 commit 100d1b4
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 6 deletions.
10 changes: 10 additions & 0 deletions crates/builder-api/api/v0_1/submit.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,13 @@ Submit a list of transactions to builder's private mempool."
Returns the corresponding list of transaction hashes
"""

[route.get_txn_stat]
PATH = ["get_txn_stat/:transaction_hash"]
METHOD = "GET"
":transaction_hash" = "TaggedBase64"
DOC = """
Get the transactions's status.
Returns "pending", "sequenced" or "rejected" with error.
"""
32 changes: 32 additions & 0 deletions crates/builder-api/src/v0_1/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ pub enum BuildError {
Error(String),
}

/// Enum to keep track on status of a transactions
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum TransactionStatus {
Pending,
Sequenced { block: u64, offset: u64 },
Rejected { reason: String }, // Rejection reason is in the String format
Unknown,
}

#[derive(Clone, Debug, Error, Deserialize, Serialize)]
pub enum Error {
#[error("Error processing request: {0}")]
Expand All @@ -70,6 +79,8 @@ pub enum Error {
TxnSubmit(BuildError),
#[error("Error getting builder address: {0}")]
BuilderAddress(#[from] BuildError),
#[error("Error getting transaction status: {0}")]
TxnStatGet(BuildError),
#[error("Custom error {status}: {message}")]
Custom { message: String, status: StatusCode },
}
Expand All @@ -95,6 +106,7 @@ impl tide_disco::error::Error for Error {
Error::TxnSubmit { .. } => StatusCode::INTERNAL_SERVER_ERROR,
Error::Custom { .. } => StatusCode::INTERNAL_SERVER_ERROR,
Error::BuilderAddress { .. } => StatusCode::INTERNAL_SERVER_ERROR,
Error::TxnStatGet { .. } => StatusCode::BAD_REQUEST,
}
}
}
Expand Down Expand Up @@ -177,6 +189,12 @@ where
.get("builder_address", |_req, state| {
async move { state.builder_address().await.map_err(|e| e.into()) }.boxed()
})?;
// .get("transaction_status", |req, state| {
// async move {
// let tx_hash = req.blob_param("transaction_hash")?;
// state.claim_tx_status(tx_hash).await.map_err(|e| e.into())
// }.boxed()
// })?
Ok(api)
}

Expand Down Expand Up @@ -216,6 +234,20 @@ where
Ok(hashes)
}
.boxed()
})?
.at("get_txn_stat", |req: RequestParams, state| {
async move {
let tx = req
.body_auto::<<Types as NodeType>::Transaction, Ver>(Ver::instance())
.map_err(Error::TxnUnpack)?;
let hash = tx.commit();
state
.claim_tx_status(hash)
.await
.map_err(Error::TxnStatGet)?;
Ok(hash)
}
.boxed()
})?;
Ok(api)
}
10 changes: 9 additions & 1 deletion crates/builder-api/src/v0_1/data_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use hotshot_types::{

use super::{
block_info::{AvailableBlockData, AvailableBlockHeaderInput, AvailableBlockInfo},
builder::BuildError,
builder::{BuildError, TransactionStatus},
};

#[async_trait]
Expand Down Expand Up @@ -48,6 +48,9 @@ pub trait BuilderDataSource<TYPES: NodeType> {

/// To get the builder's address
async fn builder_address(&self) -> Result<TYPES::BuilderSignatureKey, BuildError>;

// To get the status of submitted transaction
// async fn claim_tx_status(&self, txn_hash: Commitment<TYPES::Transaction>) -> Result<TransactionStatus, BuildError>;
}

#[async_trait]
Expand All @@ -59,4 +62,9 @@ where
&self,
txns: Vec<<I as NodeType>::Transaction>,
) -> Result<Vec<Commitment<<I as NodeType>::Transaction>>, BuildError>;

async fn claim_tx_status(
&self,
txn_hash: Commitment<<I as NodeType>::Transaction>,
) -> Result<TransactionStatus, BuildError>;
}
1 change: 1 addition & 0 deletions crates/task-impls/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl From<BuilderApiError> for BuilderClientError {
BuildError::Missing => Self::BlockMissing,
BuildError::Error(message) => Self::Api(message),
},
BuilderApiError::TxnStatGet(source) => Self::Api(source.to_string()),
}
}
}
Expand Down
16 changes: 12 additions & 4 deletions crates/testing/src/block_builder/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ use async_broadcast::{broadcast, Sender};
use async_compatibility_layer::art::{async_sleep, async_spawn};
use async_lock::RwLock;
use async_trait::async_trait;
use committable::Commitment;
use futures::{future::BoxFuture, Stream, StreamExt};
use hotshot::types::{Event, EventType, SignatureKey};
use hotshot_builder_api::v0_1::{
block_info::{AvailableBlockData, AvailableBlockHeaderInput, AvailableBlockInfo},
builder::BuildError,
data_source::BuilderDataSource,
use hotshot_builder_api::{
v0_1::{
block_info::{AvailableBlockData, AvailableBlockHeaderInput, AvailableBlockInfo},
builder::BuildError,
data_source::BuilderDataSource,
},
v0_2::builder::TransactionStatus,
};
use hotshot_example_types::block_types::TestTransaction;
use hotshot_types::{
Expand Down Expand Up @@ -328,4 +332,8 @@ impl<TYPES: NodeType> BuilderDataSource<TYPES> for RandomBuilderSource<TYPES> {
async fn builder_address(&self) -> Result<TYPES::BuilderSignatureKey, BuildError> {
Ok(self.pub_key.clone())
}

// async fn claim_tx_status(&self, _txn_hash: Commitment<TYPES::Transaction>) -> Result<TransactionStatus, BuildError> {
// Ok(TransactionStatus::Unknown) // Sishan: place holder
// }
}
6 changes: 5 additions & 1 deletion crates/testing/src/block_builder/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ use hotshot::{
types::{Event, EventType, SignatureKey},
};
use hotshot_builder_api::{
v0_1,
v0_1::{
self,
block_info::{AvailableBlockData, AvailableBlockHeaderInput, AvailableBlockInfo},
builder::{BuildError, Error, Options},
},
Expand Down Expand Up @@ -316,6 +316,10 @@ where
async fn builder_address(&self) -> Result<TYPES::BuilderSignatureKey, BuildError> {
Ok(self.pub_key.clone())
}

// async fn claim_tx_status(&self, _txn_hash: Commitment<TYPES::Transaction>) -> Result<TransactionStatus, BuildError> {
// Ok(TransactionStatus::Unknown) // Sishan: place holder
// }
}

impl<TYPES: NodeType> SimpleBuilderSource<TYPES> {
Expand Down

0 comments on commit 100d1b4

Please sign in to comment.