Skip to content

Commit

Permalink
Implement mempool/transaction endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
lrubasze committed Nov 11, 2024
1 parent 338e6ce commit c5d5b36
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
4 changes: 4 additions & 0 deletions core-rust/mesh-api-server/src/mesh_api/conversions/common.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use crate::prelude::*;

pub fn from_hex<T: AsRef<[u8]>>(v: T) -> Result<Vec<u8>, ExtractionError> {
hex::decode(v).map_err(|_| ExtractionError::InvalidHex)
}

pub fn to_mesh_api_operation(
mapping_context: &MappingContext,
database: &StateManagerDatabase<impl ReadableRocks>,
Expand Down
17 changes: 17 additions & 0 deletions core-rust/mesh-api-server/src/mesh_api/conversions/hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,20 @@ pub fn to_api_transaction_hash_bech32m<T: IsTransactionHash>(
.encode(hash)
.map_err(|err| MappingError::InvalidTransactionHash { encode_error: err })
}

pub fn extract_transaction_intent_hash(
context: &ExtractionContext,
hash_str: String,
) -> Result<TransactionIntentHash, ExtractionError> {
from_hex(&hash_str)
.ok()
.and_then(|bytes| Hash::try_from(bytes.as_slice()).ok())
.map(TransactionIntentHash::from_hash)
.or_else(|| {
context
.transaction_hash_decoder
.validate_and_decode(&hash_str)
.ok()
})
.ok_or(ExtractionError::InvalidHash)
}
40 changes: 40 additions & 0 deletions core-rust/mesh-api-server/src/mesh_api/handlers/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,43 @@ pub(crate) async fn handle_mempool(
.collect::<Result<Vec<_>, MappingError>>()?,
)))
}

pub(crate) async fn handle_mempool_transaction(
state: State<MeshApiState>,
Json(request): Json<models::MempoolTransactionRequest>,
) -> Result<Json<models::MempoolTransactionResponse>, ResponseError> {
assert_matching_network(&request.network_identifier, &state.network)?;

let extraction_context = ExtractionContext::new(&state.network);
let mapping_context = MappingContext::new(&state.network);
let mempool = state.state_manager.mempool.read();

let intent_hash = extract_transaction_intent_hash(
&extraction_context,
request.transaction_identifier.hash.clone(),
)
.map_err(|err| err.into_response_error("intent_hash"))?;

if mempool
.get_notarized_transaction_hashes_for_intent(&intent_hash)
.is_empty()
{
return Err(
ResponseError::from(ApiError::TransactionNotFound).with_details(format!(
"transaction {} not found in mempool transactions",
&request.transaction_identifier.hash
)),
);
}

// TODO:MESH prepare transaction estimates
let transaction = models::Transaction::new(
models::TransactionIdentifier::new(to_api_transaction_hash_bech32m(
&mapping_context,
&intent_hash,
)?),
vec![],
);

Ok(Json(models::MempoolTransactionResponse::new(transaction)))
}
2 changes: 1 addition & 1 deletion core-rust/mesh-api-server/src/mesh_api/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub async fn create_server<F>(
.route("/block/transaction", post(handle_block_transaction))
// TODO:MESH mempool
.route("/mempool", post(handle_mempool))
.route("/mempool/transaction", post(handle_endpoint_todo))
.route("/mempool/transaction", post(handle_mempool_transaction))
.route("/construction/derive", post(handle_endpoint_todo))
.route("/construction/preprocess", post(handle_endpoint_todo))
.route("/construction/metadata", post(handle_endpoint_todo))
Expand Down

0 comments on commit c5d5b36

Please sign in to comment.