Skip to content

Commit

Permalink
Return prettified json objects with ErasedJson
Browse files Browse the repository at this point in the history
  • Loading branch information
raychu86 committed May 10, 2023
1 parent ecc8f29 commit ef3de8a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 48 deletions.
1 change: 1 addition & 0 deletions node/rest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use axum::{
routing::{get, post},
Json,
};
use axum_extra::response::ErasedJson;
use parking_lot::Mutex;
use std::{net::SocketAddr, str::FromStr, sync::Arc};
use tokio::task::JoinHandle;
Expand Down
93 changes: 45 additions & 48 deletions node/rest/src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ use super::*;

use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use snarkos_node_messages::NodeType;
use snarkvm::prelude::{Block, StatePath, Transaction, Transactions};
use snarkvm::prelude::Transaction;

/// The `get_blocks` query object.
#[derive(Deserialize, Serialize)]
Expand All @@ -32,31 +31,31 @@ pub(crate) struct BlockRange {

impl<N: Network, C: ConsensusStorage<N>, R: Routing<N>> Rest<N, C, R> {
// GET /testnet3/latest/height
pub(crate) async fn latest_height(State(rest): State<Self>) -> Json<u32> {
Json(rest.ledger.latest_height())
pub(crate) async fn latest_height(State(rest): State<Self>) -> ErasedJson {
ErasedJson::pretty(rest.ledger.latest_height())
}

// GET /testnet3/latest/hash
pub(crate) async fn latest_hash(State(rest): State<Self>) -> Json<N::BlockHash> {
Json(rest.ledger.latest_hash())
pub(crate) async fn latest_hash(State(rest): State<Self>) -> ErasedJson {
ErasedJson::pretty(rest.ledger.latest_hash())
}

// GET /testnet3/latest/block
pub(crate) async fn latest_block(State(rest): State<Self>) -> Json<Block<N>> {
Json(rest.ledger.latest_block())
pub(crate) async fn latest_block(State(rest): State<Self>) -> ErasedJson {
ErasedJson::pretty(rest.ledger.latest_block())
}

// GET /testnet3/latest/stateRoot
pub(crate) async fn latest_state_root(State(rest): State<Self>) -> Json<N::StateRoot> {
Json(rest.ledger.latest_state_root())
pub(crate) async fn latest_state_root(State(rest): State<Self>) -> ErasedJson {
ErasedJson::pretty(rest.ledger.latest_state_root())
}

// GET /testnet3/block/{height}
// GET /testnet3/block/{blockHash}
pub(crate) async fn get_block(
State(rest): State<Self>,
Path(height_or_hash): Path<String>,
) -> Result<Json<Block<N>>, RestError> {
) -> Result<ErasedJson, RestError> {
// Manually parse the height or the height or the hash, axum doesn't support different types
// for the same path param.
let block = if let Ok(height) = height_or_hash.parse::<u32>() {
Expand All @@ -69,14 +68,14 @@ impl<N: Network, C: ConsensusStorage<N>, R: Routing<N>> Rest<N, C, R> {
rest.ledger.get_block_by_hash(&hash)?
};

Ok(Json(block))
Ok(ErasedJson::pretty(block))
}

// GET /testnet3/blocks?start={start_height}&end={end_height}
pub(crate) async fn get_blocks(
State(rest): State<Self>,
Query(block_range): Query<BlockRange>,
) -> Result<Json<Vec<Block<N>>>, RestError> {
) -> Result<ErasedJson, RestError> {
let start_height = block_range.start;
let end_height = block_range.end;

Expand All @@ -99,39 +98,37 @@ impl<N: Network, C: ConsensusStorage<N>, R: Routing<N>> Rest<N, C, R> {
.map(|height| rest.ledger.get_block(height))
.collect::<Result<Vec<_>, _>>()?;

Ok(Json(blocks))
Ok(ErasedJson::pretty(blocks))
}

// GET /testnet3/height/{blockHash}
pub(crate) async fn get_height(
State(rest): State<Self>,
Path(hash): Path<N::BlockHash>,
) -> Result<Json<u32>, RestError> {
Ok(Json(rest.ledger.get_height(&hash)?))
) -> Result<ErasedJson, RestError> {
Ok(ErasedJson::pretty(rest.ledger.get_height(&hash)?))
}

// GET /testnet3/block/{height}/transactions
pub(crate) async fn get_block_transactions(
State(rest): State<Self>,
Path(height): Path<u32>,
) -> Result<Json<Transactions<N>>, RestError> {
Ok(Json(rest.ledger.get_transactions(height)?))
) -> Result<ErasedJson, RestError> {
Ok(ErasedJson::pretty(rest.ledger.get_transactions(height)?))
}

// GET /testnet3/transaction/{transactionID}
pub(crate) async fn get_transaction(
State(rest): State<Self>,
Path(tx_id): Path<N::TransactionID>,
) -> Result<Json<Transaction<N>>, RestError> {
Ok(Json(rest.ledger.get_transaction(tx_id)?))
) -> Result<ErasedJson, RestError> {
Ok(ErasedJson::pretty(rest.ledger.get_transaction(tx_id)?))
}

// GET /testnet3/memoryPool/transactions
pub(crate) async fn get_memory_pool_transactions(
State(rest): State<Self>,
) -> Result<Json<Vec<Transaction<N>>>, RestError> {
pub(crate) async fn get_memory_pool_transactions(State(rest): State<Self>) -> Result<ErasedJson, RestError> {
match rest.consensus {
Some(consensus) => Ok(Json(consensus.memory_pool().unconfirmed_transactions())),
Some(consensus) => Ok(ErasedJson::pretty(consensus.memory_pool().unconfirmed_transactions())),
None => Err(RestError("route isn't available for this node type".to_string())),
}
}
Expand All @@ -140,89 +137,89 @@ impl<N: Network, C: ConsensusStorage<N>, R: Routing<N>> Rest<N, C, R> {
pub(crate) async fn get_program(
State(rest): State<Self>,
Path(id): Path<ProgramID<N>>,
) -> Result<Json<Program<N>>, RestError> {
) -> Result<ErasedJson, RestError> {
let program = if id == ProgramID::<N>::from_str("creadits.aleo")? {
Program::<N>::credits()?
} else {
rest.ledger.get_program(id)?
};

Ok(Json(program))
Ok(ErasedJson::pretty(program))
}

// GET /testnet3/statePath/{commitment}
pub(crate) async fn get_state_path_for_commitment(
State(rest): State<Self>,
Path(commitment): Path<Field<N>>,
) -> Result<Json<StatePath<N>>, RestError> {
Ok(Json(rest.ledger.get_state_path_for_commitment(&commitment)?))
) -> Result<ErasedJson, RestError> {
Ok(ErasedJson::pretty(rest.ledger.get_state_path_for_commitment(&commitment)?))
}

// GET /testnet3/beacons
pub(crate) async fn get_beacons(State(rest): State<Self>) -> Result<Json<Vec<Address<N>>>, RestError> {
pub(crate) async fn get_beacons(State(rest): State<Self>) -> Result<ErasedJson, RestError> {
match rest.consensus {
Some(consensus) => Ok(Json(consensus.beacons().keys().copied().collect())),
Some(consensus) => Ok(ErasedJson::pretty(consensus.beacons().keys().copied().collect::<Vec<Address<N>>>())),
None => Err(RestError("route isn't available for this node type".to_string())),
}
}

// GET /testnet3/peers/count
pub(crate) async fn get_peers_count(State(rest): State<Self>) -> Json<usize> {
Json(rest.routing.router().number_of_connected_peers())
pub(crate) async fn get_peers_count(State(rest): State<Self>) -> ErasedJson {
ErasedJson::pretty(rest.routing.router().number_of_connected_peers())
}

// GET /testnet3/peers/all
pub(crate) async fn get_peers_all(State(rest): State<Self>) -> Json<Vec<SocketAddr>> {
Json(rest.routing.router().connected_peers())
pub(crate) async fn get_peers_all(State(rest): State<Self>) -> ErasedJson {
ErasedJson::pretty(rest.routing.router().connected_peers())
}

// GET /testnet3/peers/all/metrics
pub(crate) async fn get_peers_all_metrics(State(rest): State<Self>) -> Json<Vec<(SocketAddr, NodeType)>> {
Json(rest.routing.router().connected_metrics())
pub(crate) async fn get_peers_all_metrics(State(rest): State<Self>) -> ErasedJson {
ErasedJson::pretty(rest.routing.router().connected_metrics())
}

// GET /testnet3/node/address
pub(crate) async fn get_node_address(State(rest): State<Self>) -> Json<Address<N>> {
Json(rest.routing.router().address())
pub(crate) async fn get_node_address(State(rest): State<Self>) -> ErasedJson {
ErasedJson::pretty(rest.routing.router().address())
}

// GET /testnet3/find/blockHash/{transactionID}
pub(crate) async fn find_block_hash(
State(rest): State<Self>,
Path(tx_id): Path<N::TransactionID>,
) -> Result<Json<Option<N::BlockHash>>, RestError> {
Ok(Json(rest.ledger.find_block_hash(&tx_id)?))
) -> Result<ErasedJson, RestError> {
Ok(ErasedJson::pretty(rest.ledger.find_block_hash(&tx_id)?))
}

// GET /testnet3/find/transactionID/deployment/{programID}
pub(crate) async fn find_transaction_id_from_program_id(
State(rest): State<Self>,
Path(program_id): Path<ProgramID<N>>,
) -> Result<Json<Option<N::TransactionID>>, RestError> {
Ok(Json(rest.ledger.find_transaction_id_from_program_id(&program_id)?))
) -> Result<ErasedJson, RestError> {
Ok(ErasedJson::pretty(rest.ledger.find_transaction_id_from_program_id(&program_id)?))
}

// GET /testnet3/find/transactionID/{transitionID}
pub(crate) async fn find_transaction_id_from_transition_id(
State(rest): State<Self>,
Path(transition_id): Path<N::TransitionID>,
) -> Result<Json<Option<N::TransactionID>>, RestError> {
Ok(Json(rest.ledger.find_transaction_id_from_transition_id(&transition_id)?))
) -> Result<ErasedJson, RestError> {
Ok(ErasedJson::pretty(rest.ledger.find_transaction_id_from_transition_id(&transition_id)?))
}

// GET /testnet3/find/transitionID/{inputOrOutputID}
pub(crate) async fn find_transition_id(
State(rest): State<Self>,
Path(input_or_output_id): Path<Field<N>>,
) -> Result<Json<N::TransitionID>, RestError> {
Ok(Json(rest.ledger.find_transition_id(&input_or_output_id)?))
) -> Result<ErasedJson, RestError> {
Ok(ErasedJson::pretty(rest.ledger.find_transition_id(&input_or_output_id)?))
}

// POST /testnet3/transaction/broadcast
pub(crate) async fn transaction_broadcast(
State(rest): State<Self>,
Json(tx): Json<Transaction<N>>,
) -> Result<Json<N::TransactionID>, RestError> {
) -> Result<ErasedJson, RestError> {
// If the consensus module is enabled, add the unconfirmed transaction to the memory pool.
if let Some(consensus) = rest.consensus {
// Add the unconfirmed transaction to the memory pool.
Expand All @@ -239,6 +236,6 @@ impl<N: Network, C: ConsensusStorage<N>, R: Routing<N>> Rest<N, C, R> {
// Broadcast the transaction.
rest.routing.propagate(message, &[]);

Ok(Json(tx_id))
Ok(ErasedJson::pretty(tx_id))
}
}

0 comments on commit ef3de8a

Please sign in to comment.