refactor: break main.rs into separate files
This commit is contained in:
parent
4509b1f9a6
commit
7fa11f3ec2
12
Cargo.lock
generated
12
Cargo.lock
generated
@ -1812,6 +1812,7 @@ dependencies = [
|
||||
"clap",
|
||||
"html-escaper",
|
||||
"neptune-core",
|
||||
"readonly",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tarpc",
|
||||
@ -2443,6 +2444,17 @@ dependencies = [
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "readonly"
|
||||
version = "0.2.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a25d631e41bfb5fdcde1d4e2215f62f7f0afa3ff11e26563765bd6ea1d229aeb"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.58",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.4.1"
|
||||
|
||||
@ -25,6 +25,7 @@ thiserror = "1.0.58"
|
||||
boilerplate = { version = "1.0.0" }
|
||||
html-escaper = "0.2.0"
|
||||
tower-http = { version = "0.5.2", features = ["fs"] }
|
||||
readonly = "0.2.12"
|
||||
|
||||
|
||||
[patch.crates-io]
|
||||
|
||||
10
src/html/component/header.rs
Normal file
10
src/html/component/header.rs
Normal file
@ -0,0 +1,10 @@
|
||||
use crate::model::app_state::AppState;
|
||||
use html_escaper::Escape;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(boilerplate::Boilerplate)]
|
||||
#[boilerplate(filename = "web/html/components/header.html")]
|
||||
pub struct HeaderHtml {
|
||||
pub site_name: String,
|
||||
pub state: Arc<AppState>,
|
||||
}
|
||||
1
src/html/component/mod.rs
Normal file
1
src/html/component/mod.rs
Normal file
@ -0,0 +1 @@
|
||||
pub mod header;
|
||||
2
src/html/mod.rs
Normal file
2
src/html/mod.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub mod component;
|
||||
pub mod page;
|
||||
42
src/html/page/block.rs
Normal file
42
src/html/page/block.rs
Normal file
@ -0,0 +1,42 @@
|
||||
use crate::html::component::header::HeaderHtml;
|
||||
use crate::model::app_state::AppState;
|
||||
use crate::model::path_block_selector::PathBlockSelector;
|
||||
use crate::rpc::block_info::block_info_with_value_worker;
|
||||
use axum::extract::Path;
|
||||
use axum::extract::State;
|
||||
use axum::response::Html;
|
||||
use axum::response::Response;
|
||||
use html_escaper::Escape;
|
||||
use html_escaper::Trusted;
|
||||
use neptune_core::rpc_server::BlockInfo;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub async fn block_page(
|
||||
Path(path_block_selector): Path<PathBlockSelector>,
|
||||
state: State<Arc<AppState>>,
|
||||
) -> Result<Html<String>, Response> {
|
||||
let value_path: Path<(PathBlockSelector, String)> = Path((path_block_selector, "".to_string()));
|
||||
block_page_with_value(value_path, state).await
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
pub async fn block_page_with_value(
|
||||
Path((path_block_selector, value)): Path<(PathBlockSelector, String)>,
|
||||
State(state): State<Arc<AppState>>,
|
||||
) -> Result<Html<String>, Response> {
|
||||
#[derive(boilerplate::Boilerplate)]
|
||||
#[boilerplate(filename = "web/html/page/block_info.html")]
|
||||
pub struct BlockInfoHtmlPage {
|
||||
header: HeaderHtml,
|
||||
block_info: BlockInfo,
|
||||
}
|
||||
|
||||
let header = HeaderHtml {
|
||||
site_name: "Neptune Explorer".to_string(),
|
||||
state: state.clone(),
|
||||
};
|
||||
|
||||
let block_info = block_info_with_value_worker(state, path_block_selector, &value).await?;
|
||||
let block_info_page = BlockInfoHtmlPage { header, block_info };
|
||||
Ok(Html(block_info_page.to_string()))
|
||||
}
|
||||
3
src/html/page/mod.rs
Normal file
3
src/html/page/mod.rs
Normal file
@ -0,0 +1,3 @@
|
||||
pub mod block;
|
||||
pub mod root;
|
||||
pub mod utxo;
|
||||
22
src/html/page/root.rs
Normal file
22
src/html/page/root.rs
Normal file
@ -0,0 +1,22 @@
|
||||
use crate::model::app_state::AppState;
|
||||
use axum::extract::State;
|
||||
use axum::response::Html;
|
||||
use html_escaper::Escape;
|
||||
use std::ops::Deref;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[axum::debug_handler]
|
||||
pub async fn root(State(state): State<Arc<AppState>>) -> Html<String> {
|
||||
#[derive(boilerplate::Boilerplate)]
|
||||
#[boilerplate(filename = "web/html/page/root.html")]
|
||||
pub struct RootHtmlPage(Arc<AppState>);
|
||||
impl Deref for RootHtmlPage {
|
||||
type Target = AppState;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
let root_page = RootHtmlPage(state);
|
||||
Html(root_page.to_string())
|
||||
}
|
||||
49
src/html/page/utxo.rs
Normal file
49
src/html/page/utxo.rs
Normal file
@ -0,0 +1,49 @@
|
||||
use crate::html::component::header::HeaderHtml;
|
||||
use crate::http_util::not_found_err;
|
||||
use crate::http_util::rpc_err;
|
||||
use crate::model::app_state::AppState;
|
||||
use axum::extract::Path;
|
||||
use axum::extract::State;
|
||||
use axum::response::Html;
|
||||
use axum::response::Response;
|
||||
use html_escaper::Escape;
|
||||
use html_escaper::Trusted;
|
||||
use neptune_core::prelude::tasm_lib::Digest;
|
||||
use std::sync::Arc;
|
||||
use tarpc::context;
|
||||
|
||||
#[axum::debug_handler]
|
||||
pub async fn utxo_page(
|
||||
Path(index): Path<u64>,
|
||||
State(state): State<Arc<AppState>>,
|
||||
) -> Result<Html<String>, Response> {
|
||||
#[derive(boilerplate::Boilerplate)]
|
||||
#[boilerplate(filename = "web/html/page/utxo.html")]
|
||||
pub struct UtxoHtmlPage {
|
||||
header: HeaderHtml,
|
||||
index: u64,
|
||||
digest: Digest,
|
||||
}
|
||||
|
||||
let digest = match state
|
||||
.rpc_client
|
||||
.utxo_digest(context::current(), index)
|
||||
.await
|
||||
.map_err(rpc_err)?
|
||||
{
|
||||
Some(digest) => digest,
|
||||
None => return Err(not_found_err()),
|
||||
};
|
||||
|
||||
let header = HeaderHtml {
|
||||
site_name: "Neptune Explorer".to_string(),
|
||||
state: state.clone(),
|
||||
};
|
||||
|
||||
let utxo_page = UtxoHtmlPage {
|
||||
index,
|
||||
header,
|
||||
digest,
|
||||
};
|
||||
Ok(Html(utxo_page.to_string()))
|
||||
}
|
||||
15
src/http_util.rs
Normal file
15
src/http_util.rs
Normal file
@ -0,0 +1,15 @@
|
||||
use axum::http::StatusCode;
|
||||
use axum::response::IntoResponse;
|
||||
use axum::response::Response;
|
||||
use tarpc::client::RpcError;
|
||||
|
||||
// note: http StatusCodes are defined at:
|
||||
// https://docs.rs/http/1.1.0/http/status/struct.StatusCode.html
|
||||
|
||||
pub fn not_found_err() -> Response {
|
||||
(StatusCode::NOT_FOUND, "Not Found".to_string()).into_response()
|
||||
}
|
||||
|
||||
pub fn rpc_err(e: RpcError) -> Response {
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response()
|
||||
}
|
||||
4
src/lib.rs
Normal file
4
src/lib.rs
Normal file
@ -0,0 +1,4 @@
|
||||
pub mod html;
|
||||
pub mod http_util;
|
||||
pub mod model;
|
||||
pub mod rpc;
|
||||
336
src/main.rs
336
src/main.rs
@ -1,328 +1,70 @@
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
response::{Html, IntoResponse, Response},
|
||||
routing::get,
|
||||
Json, Router,
|
||||
};
|
||||
use tower_http::{
|
||||
services::ServeFile,
|
||||
};
|
||||
use axum::routing::get;
|
||||
use axum::routing::Router;
|
||||
use clap::Parser;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::str::FromStr;
|
||||
use std::ops::Deref;
|
||||
use thiserror::Error;
|
||||
use html_escaper::{Escape, Trusted};
|
||||
|
||||
use neptune_core::config_models::network::Network;
|
||||
use neptune_core::models::blockchain::block::block_height::BlockHeight;
|
||||
use neptune_core::rpc_server::{BlockInfo, BlockSelector, RPCClient};
|
||||
use neptune_core::prelude::twenty_first::error::TryFromHexDigestError;
|
||||
use neptune_core::prelude::twenty_first::math::digest::Digest;
|
||||
use std::net::{Ipv4Addr, SocketAddr};
|
||||
use neptune_core::rpc_server::BlockSelector;
|
||||
use neptune_core::rpc_server::RPCClient;
|
||||
use neptune_explorer::html::page::block::block_page;
|
||||
use neptune_explorer::html::page::block::block_page_with_value;
|
||||
use neptune_explorer::html::page::root::root;
|
||||
use neptune_explorer::html::page::utxo::utxo_page;
|
||||
use neptune_explorer::model::app_state::AppState;
|
||||
use neptune_explorer::model::config::Config;
|
||||
use neptune_explorer::rpc::block_digest::block_digest;
|
||||
use neptune_explorer::rpc::block_digest::block_digest_with_value;
|
||||
use neptune_explorer::rpc::block_info::block_info;
|
||||
use neptune_explorer::rpc::block_info::block_info_with_value;
|
||||
use neptune_explorer::rpc::utxo_digest::utxo_digest;
|
||||
use std::net::Ipv4Addr;
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
use tarpc::client;
|
||||
use tarpc::client::RpcError;
|
||||
use tarpc::context;
|
||||
use tarpc::tokio_serde::formats::Json as RpcJson;
|
||||
use tarpc::{client, context};
|
||||
|
||||
// note: http StatusCodes are defined at:
|
||||
// https://docs.rs/http/1.1.0/http/status/struct.StatusCode.html
|
||||
|
||||
#[derive(Debug, clap::Parser, Clone)]
|
||||
#[clap(name = "neptune-explorer", about = "Neptune Block Explorer")]
|
||||
pub struct Config {
|
||||
/// Sets the server address to connect to.
|
||||
#[clap(long, default_value = "9799", value_name = "PORT")]
|
||||
port: u16,
|
||||
}
|
||||
|
||||
pub struct AppState {
|
||||
network: Network,
|
||||
#[allow(dead_code)]
|
||||
config: Config,
|
||||
rpc_client: RPCClient,
|
||||
genesis_digest: Digest,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
|
||||
enum PathBlockSelector {
|
||||
#[serde(rename = "genesis")]
|
||||
Genesis,
|
||||
#[serde(rename = "tip")]
|
||||
Tip,
|
||||
#[serde(rename = "digest")]
|
||||
Digest,
|
||||
#[serde(rename = "height")]
|
||||
Height,
|
||||
#[serde(rename = "height_or_digest")]
|
||||
HeightOrDigest,
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum PathBlockSelectorError {
|
||||
#[error("Genesis does not accept an argument")]
|
||||
GenesisNoArg,
|
||||
|
||||
#[error("Tip does not accept an argument")]
|
||||
TipNoArg,
|
||||
|
||||
#[error("Digest could not be parsed")]
|
||||
DigestNotParsed(#[from] TryFromHexDigestError),
|
||||
|
||||
#[error("Height could not be parsed")]
|
||||
HeightNotParsed(#[from] std::num::ParseIntError),
|
||||
}
|
||||
impl PathBlockSelectorError {
|
||||
fn as_response_tuple(&self) -> (StatusCode, String) {
|
||||
(StatusCode::NOT_FOUND, self.to_string())
|
||||
}
|
||||
}
|
||||
impl IntoResponse for PathBlockSelectorError {
|
||||
fn into_response(self) -> Response {
|
||||
self.as_response_tuple().into_response()
|
||||
}
|
||||
}
|
||||
impl From<PathBlockSelectorError> for Response {
|
||||
fn from(e: PathBlockSelectorError) -> Response {
|
||||
e.as_response_tuple().into_response()
|
||||
}
|
||||
}
|
||||
|
||||
fn not_found_err() -> Response {
|
||||
(StatusCode::NOT_FOUND, "Not Found".to_string()).into_response()
|
||||
}
|
||||
fn rpc_err(e: RpcError) -> Response {
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response()
|
||||
}
|
||||
|
||||
impl PathBlockSelector {
|
||||
fn as_block_selector(&self, value: &str) -> Result<BlockSelector, PathBlockSelectorError> {
|
||||
match self {
|
||||
PathBlockSelector::Genesis if !value.is_empty() => {
|
||||
Err(PathBlockSelectorError::GenesisNoArg)
|
||||
}
|
||||
PathBlockSelector::Genesis => Ok(BlockSelector::Genesis),
|
||||
PathBlockSelector::Tip if !value.is_empty() => Err(PathBlockSelectorError::TipNoArg),
|
||||
PathBlockSelector::Tip => Ok(BlockSelector::Tip),
|
||||
PathBlockSelector::Digest => Ok(BlockSelector::Digest(Digest::try_from_hex(value)?)),
|
||||
PathBlockSelector::Height => Ok(BlockSelector::Height(BlockHeight::from(
|
||||
u64::from_str(value)?,
|
||||
))),
|
||||
PathBlockSelector::HeightOrDigest => {
|
||||
Ok(match u64::from_str(value) {
|
||||
Ok(height) => BlockSelector::Height(BlockHeight::from(height)),
|
||||
Err(_) => BlockSelector::Digest(Digest::try_from_hex(value)?),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn block_digest_with_value_worker(
|
||||
state: Arc<AppState>,
|
||||
path_block_selector: PathBlockSelector,
|
||||
value: &str,
|
||||
) -> Result<Json<Digest>, impl IntoResponse> {
|
||||
let block_selector = path_block_selector.as_block_selector(&value)?;
|
||||
|
||||
match state
|
||||
.rpc_client
|
||||
.block_digest(context::current(), block_selector)
|
||||
.await
|
||||
.map_err(rpc_err)?
|
||||
{
|
||||
Some(digest) => Ok(Json(digest)),
|
||||
None => Err(not_found_err()),
|
||||
}
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
async fn block_digest(
|
||||
Path(path_block_selector): Path<PathBlockSelector>,
|
||||
State(state): State<Arc<AppState>>,
|
||||
) -> Result<Json<Digest>, impl IntoResponse> {
|
||||
block_digest_with_value_worker(state, path_block_selector, "").await
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
async fn block_digest_with_value(
|
||||
Path((path_block_selector, value)): Path<(PathBlockSelector, String)>,
|
||||
State(state): State<Arc<AppState>>,
|
||||
) -> Result<Json<Digest>, impl IntoResponse> {
|
||||
block_digest_with_value_worker(state, path_block_selector, &value).await
|
||||
}
|
||||
|
||||
async fn block_info_with_value_worker(
|
||||
state: Arc<AppState>,
|
||||
path_block_selector: PathBlockSelector,
|
||||
value: &str,
|
||||
) -> Result<BlockInfo, Response> {
|
||||
let block_selector = path_block_selector.as_block_selector(&value)?;
|
||||
|
||||
match state
|
||||
.rpc_client
|
||||
.block_info(context::current(), block_selector)
|
||||
.await
|
||||
.map_err(rpc_err)?
|
||||
{
|
||||
Some(info) => Ok(info),
|
||||
None => Err(not_found_err()),
|
||||
}
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
async fn block_info(
|
||||
Path(path_block_selector): Path<PathBlockSelector>,
|
||||
State(state): State<Arc<AppState>>,
|
||||
) -> Result<Json<BlockInfo>, Response> {
|
||||
let block_info = block_info_with_value_worker(state, path_block_selector, "").await?;
|
||||
Ok(Json(block_info))
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
async fn block_info_with_value(
|
||||
Path((path_block_selector, value)): Path<(PathBlockSelector, String)>,
|
||||
State(state): State<Arc<AppState>>,
|
||||
) -> Result<Json<BlockInfo>, Response> {
|
||||
let block_info = block_info_with_value_worker(state, path_block_selector, &value).await?;
|
||||
Ok(Json(block_info))
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
async fn utxo_digest(
|
||||
Path(index): Path<u64>,
|
||||
State(state): State<Arc<AppState>>,
|
||||
) -> Result<Json<Digest>, impl IntoResponse> {
|
||||
match state
|
||||
.rpc_client
|
||||
.utxo_digest(context::current(), index)
|
||||
.await
|
||||
.map_err(rpc_err)?
|
||||
{
|
||||
Some(digest) => Ok(Json(digest)),
|
||||
None => Err(not_found_err()),
|
||||
}
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
async fn root(
|
||||
State(state): State<Arc<AppState>>,
|
||||
) -> Html<String> {
|
||||
|
||||
#[derive(boilerplate::Boilerplate)]
|
||||
#[boilerplate(filename = "web/html/page/root.html")]
|
||||
pub struct RootHtmlPage(Arc<AppState>);
|
||||
impl Deref for RootHtmlPage {
|
||||
type Target = AppState;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
let root_page = RootHtmlPage(state);
|
||||
Html(root_page.to_string())
|
||||
}
|
||||
|
||||
async fn block_page(
|
||||
Path(path_block_selector): Path<PathBlockSelector>,
|
||||
state: State<Arc<AppState>>,
|
||||
) -> Result<Html<String>, Response> {
|
||||
let value_path: Path<(PathBlockSelector, String)> = Path((path_block_selector, "".to_string()));
|
||||
block_page_with_value(value_path, state).await
|
||||
}
|
||||
|
||||
#[derive(boilerplate::Boilerplate)]
|
||||
#[boilerplate(filename = "web/html/components/header.html")]
|
||||
pub struct HeaderHtml{
|
||||
site_name: String,
|
||||
state: Arc<AppState>,
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
async fn block_page_with_value(
|
||||
Path((path_block_selector, value)): Path<(PathBlockSelector, String)>,
|
||||
State(state): State<Arc<AppState>>,
|
||||
) -> Result<Html<String>, Response> {
|
||||
|
||||
#[derive(boilerplate::Boilerplate)]
|
||||
#[boilerplate(filename = "web/html/page/block_info.html")]
|
||||
pub struct BlockInfoHtmlPage{
|
||||
header: HeaderHtml,
|
||||
block_info: BlockInfo
|
||||
}
|
||||
|
||||
let header = HeaderHtml{site_name: "Neptune Explorer".to_string(), state: state.clone()};
|
||||
|
||||
let block_info = block_info_with_value_worker(state, path_block_selector, &value).await?;
|
||||
let block_info_page = BlockInfoHtmlPage{header, block_info};
|
||||
Ok(Html(block_info_page.to_string()))
|
||||
}
|
||||
|
||||
|
||||
#[axum::debug_handler]
|
||||
async fn utxo_page(
|
||||
Path(index): Path<u64>,
|
||||
State(state): State<Arc<AppState>>,
|
||||
) -> Result<Html<String>, Response> {
|
||||
|
||||
#[derive(boilerplate::Boilerplate)]
|
||||
#[boilerplate(filename = "web/html/page/utxo.html")]
|
||||
pub struct UtxoHtmlPage{
|
||||
header: HeaderHtml,
|
||||
index: u64,
|
||||
digest: Digest,
|
||||
}
|
||||
|
||||
let digest = match state
|
||||
.rpc_client
|
||||
.utxo_digest(context::current(), index)
|
||||
.await
|
||||
.map_err(rpc_err)?
|
||||
{
|
||||
Some(digest) => digest,
|
||||
None => return Err(not_found_err()),
|
||||
};
|
||||
|
||||
let header = HeaderHtml{site_name: "Neptune Explorer".to_string(), state: state.clone()};
|
||||
|
||||
let utxo_page = UtxoHtmlPage{index, header, digest};
|
||||
Ok(Html(utxo_page.to_string()))
|
||||
}
|
||||
use tower_http::services::ServeFile;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), RpcError> {
|
||||
let rpc_client = rpc_client().await;
|
||||
let network = rpc_client.network(context::current()).await?;
|
||||
let genesis_digest = rpc_client.block_digest(context::current(), BlockSelector::Genesis).await?.expect("Genesis block should be found");
|
||||
let genesis_digest = rpc_client
|
||||
.block_digest(context::current(), BlockSelector::Genesis)
|
||||
.await?
|
||||
.expect("Genesis block should be found");
|
||||
|
||||
let shared_state = Arc::new(AppState {
|
||||
rpc_client,
|
||||
config: Config::parse(),
|
||||
let shared_state = Arc::new(AppState::from((
|
||||
network,
|
||||
Config::parse(),
|
||||
rpc_client,
|
||||
genesis_digest,
|
||||
});
|
||||
)));
|
||||
|
||||
let app = Router::new()
|
||||
// -- RPC calls --
|
||||
.route("/rpc/block_info/:selector", get(block_info))
|
||||
.route("/rpc/block_info/:selector/:value", get(block_info_with_value))
|
||||
.route(
|
||||
"/rpc/block_info/:selector/:value",
|
||||
get(block_info_with_value),
|
||||
)
|
||||
.route(
|
||||
"/rpc/block_digest/:selector/:value",
|
||||
get(block_digest_with_value),
|
||||
)
|
||||
.route("/rpc/block_digest/:selector", get(block_digest))
|
||||
.route("/rpc/utxo_digest/:index", get(utxo_digest))
|
||||
|
||||
// -- Dynamic HTML pages --
|
||||
.route("/", get(root))
|
||||
.route("/block/:selector", get(block_page))
|
||||
.route("/block/:selector/:value", get(block_page_with_value))
|
||||
.route("/utxo/:value", get(utxo_page))
|
||||
|
||||
// -- Static files --
|
||||
.route_service("/css/styles.css", ServeFile::new(concat!(env!("CARGO_MANIFEST_DIR"), "/src/web/css/styles.css")))
|
||||
|
||||
.route_service(
|
||||
"/css/styles.css",
|
||||
ServeFile::new(concat!(
|
||||
env!("CARGO_MANIFEST_DIR"),
|
||||
"/src/web/css/styles.css"
|
||||
)),
|
||||
)
|
||||
// add state
|
||||
.with_state(shared_state);
|
||||
|
||||
|
||||
25
src/model/app_state.rs
Normal file
25
src/model/app_state.rs
Normal file
@ -0,0 +1,25 @@
|
||||
use crate::model::config::Config;
|
||||
use neptune_core::config_models::network::Network;
|
||||
use neptune_core::prelude::twenty_first::math::digest::Digest;
|
||||
use neptune_core::rpc_server::RPCClient;
|
||||
|
||||
#[readonly::make]
|
||||
pub struct AppState {
|
||||
pub network: Network,
|
||||
pub config: Config,
|
||||
pub rpc_client: RPCClient,
|
||||
pub genesis_digest: Digest,
|
||||
}
|
||||
|
||||
impl From<(Network, Config, RPCClient, Digest)> for AppState {
|
||||
fn from(
|
||||
(network, config, rpc_client, genesis_digest): (Network, Config, RPCClient, Digest),
|
||||
) -> Self {
|
||||
Self {
|
||||
network,
|
||||
config,
|
||||
rpc_client,
|
||||
genesis_digest,
|
||||
}
|
||||
}
|
||||
}
|
||||
8
src/model/config.rs
Normal file
8
src/model/config.rs
Normal file
@ -0,0 +1,8 @@
|
||||
#[readonly::make]
|
||||
#[derive(Debug, clap::Parser, Clone)]
|
||||
#[clap(name = "neptune-explorer", about = "Neptune Block Explorer")]
|
||||
pub struct Config {
|
||||
/// Sets the server address to connect to.
|
||||
#[clap(long, default_value = "9799", value_name = "PORT")]
|
||||
pub port: u16,
|
||||
}
|
||||
3
src/model/mod.rs
Normal file
3
src/model/mod.rs
Normal file
@ -0,0 +1,3 @@
|
||||
pub mod app_state;
|
||||
pub mod config;
|
||||
pub mod path_block_selector;
|
||||
73
src/model/path_block_selector.rs
Normal file
73
src/model/path_block_selector.rs
Normal file
@ -0,0 +1,73 @@
|
||||
use axum::http::StatusCode;
|
||||
use axum::response::IntoResponse;
|
||||
use axum::response::Response;
|
||||
use neptune_core::models::blockchain::block::block_height::BlockHeight;
|
||||
use neptune_core::prelude::tasm_lib::Digest;
|
||||
use neptune_core::prelude::twenty_first::error::TryFromHexDigestError;
|
||||
use neptune_core::rpc_server::BlockSelector;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::str::FromStr;
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub enum PathBlockSelector {
|
||||
#[serde(rename = "genesis")]
|
||||
Genesis,
|
||||
#[serde(rename = "tip")]
|
||||
Tip,
|
||||
#[serde(rename = "digest")]
|
||||
Digest,
|
||||
#[serde(rename = "height")]
|
||||
Height,
|
||||
#[serde(rename = "height_or_digest")]
|
||||
HeightOrDigest,
|
||||
}
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum PathBlockSelectorError {
|
||||
#[error("Genesis does not accept an argument")]
|
||||
GenesisNoArg,
|
||||
|
||||
#[error("Tip does not accept an argument")]
|
||||
TipNoArg,
|
||||
|
||||
#[error("Digest could not be parsed")]
|
||||
DigestNotParsed(#[from] TryFromHexDigestError),
|
||||
|
||||
#[error("Height could not be parsed")]
|
||||
HeightNotParsed(#[from] std::num::ParseIntError),
|
||||
}
|
||||
impl PathBlockSelectorError {
|
||||
fn as_response_tuple(&self) -> (StatusCode, String) {
|
||||
(StatusCode::NOT_FOUND, self.to_string())
|
||||
}
|
||||
}
|
||||
impl IntoResponse for PathBlockSelectorError {
|
||||
fn into_response(self) -> Response {
|
||||
self.as_response_tuple().into_response()
|
||||
}
|
||||
}
|
||||
impl From<PathBlockSelectorError> for Response {
|
||||
fn from(e: PathBlockSelectorError) -> Response {
|
||||
e.as_response_tuple().into_response()
|
||||
}
|
||||
}
|
||||
impl PathBlockSelector {
|
||||
pub fn as_block_selector(&self, value: &str) -> Result<BlockSelector, PathBlockSelectorError> {
|
||||
match self {
|
||||
PathBlockSelector::Genesis if !value.is_empty() => {
|
||||
Err(PathBlockSelectorError::GenesisNoArg)
|
||||
}
|
||||
PathBlockSelector::Genesis => Ok(BlockSelector::Genesis),
|
||||
PathBlockSelector::Tip if !value.is_empty() => Err(PathBlockSelectorError::TipNoArg),
|
||||
PathBlockSelector::Tip => Ok(BlockSelector::Tip),
|
||||
PathBlockSelector::Digest => Ok(BlockSelector::Digest(Digest::try_from_hex(value)?)),
|
||||
PathBlockSelector::Height => Ok(BlockSelector::Height(BlockHeight::from(
|
||||
u64::from_str(value)?,
|
||||
))),
|
||||
PathBlockSelector::HeightOrDigest => Ok(match u64::from_str(value) {
|
||||
Ok(height) => BlockSelector::Height(BlockHeight::from(height)),
|
||||
Err(_) => BlockSelector::Digest(Digest::try_from_hex(value)?),
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
46
src/rpc/block_digest.rs
Normal file
46
src/rpc/block_digest.rs
Normal file
@ -0,0 +1,46 @@
|
||||
use axum::extract::Path;
|
||||
use axum::extract::State;
|
||||
use axum::response::IntoResponse;
|
||||
use axum::response::Json;
|
||||
use neptune_core::prelude::twenty_first::math::digest::Digest;
|
||||
use std::sync::Arc;
|
||||
use tarpc::context;
|
||||
|
||||
use crate::{
|
||||
http_util::{not_found_err, rpc_err},
|
||||
model::{app_state::AppState, path_block_selector::PathBlockSelector},
|
||||
};
|
||||
|
||||
#[axum::debug_handler]
|
||||
pub async fn block_digest(
|
||||
Path(path_block_selector): Path<PathBlockSelector>,
|
||||
State(state): State<Arc<AppState>>,
|
||||
) -> Result<Json<Digest>, impl IntoResponse> {
|
||||
block_digest_with_value_worker(state, path_block_selector, "").await
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
pub async fn block_digest_with_value(
|
||||
Path((path_block_selector, value)): Path<(PathBlockSelector, String)>,
|
||||
State(state): State<Arc<AppState>>,
|
||||
) -> Result<Json<Digest>, impl IntoResponse> {
|
||||
block_digest_with_value_worker(state, path_block_selector, &value).await
|
||||
}
|
||||
|
||||
async fn block_digest_with_value_worker(
|
||||
state: Arc<AppState>,
|
||||
path_block_selector: PathBlockSelector,
|
||||
value: &str,
|
||||
) -> Result<Json<Digest>, impl IntoResponse> {
|
||||
let block_selector = path_block_selector.as_block_selector(value)?;
|
||||
|
||||
match state
|
||||
.rpc_client
|
||||
.block_digest(context::current(), block_selector)
|
||||
.await
|
||||
.map_err(rpc_err)?
|
||||
{
|
||||
Some(digest) => Ok(Json(digest)),
|
||||
None => Err(not_found_err()),
|
||||
}
|
||||
}
|
||||
48
src/rpc/block_info.rs
Normal file
48
src/rpc/block_info.rs
Normal file
@ -0,0 +1,48 @@
|
||||
use axum::extract::Path;
|
||||
use axum::extract::State;
|
||||
use axum::response::Json;
|
||||
use axum::response::Response;
|
||||
use neptune_core::rpc_server::BlockInfo;
|
||||
use std::sync::Arc;
|
||||
use tarpc::context;
|
||||
|
||||
use crate::{
|
||||
http_util::{not_found_err, rpc_err},
|
||||
model::{app_state::AppState, path_block_selector::PathBlockSelector},
|
||||
};
|
||||
|
||||
#[axum::debug_handler]
|
||||
pub async fn block_info(
|
||||
Path(path_block_selector): Path<PathBlockSelector>,
|
||||
State(state): State<Arc<AppState>>,
|
||||
) -> Result<Json<BlockInfo>, Response> {
|
||||
let block_info = block_info_with_value_worker(state, path_block_selector, "").await?;
|
||||
Ok(Json(block_info))
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
pub async fn block_info_with_value(
|
||||
Path((path_block_selector, value)): Path<(PathBlockSelector, String)>,
|
||||
State(state): State<Arc<AppState>>,
|
||||
) -> Result<Json<BlockInfo>, Response> {
|
||||
let block_info = block_info_with_value_worker(state, path_block_selector, &value).await?;
|
||||
Ok(Json(block_info))
|
||||
}
|
||||
|
||||
pub(crate) async fn block_info_with_value_worker(
|
||||
state: Arc<AppState>,
|
||||
path_block_selector: PathBlockSelector,
|
||||
value: &str,
|
||||
) -> Result<BlockInfo, Response> {
|
||||
let block_selector = path_block_selector.as_block_selector(value)?;
|
||||
|
||||
match state
|
||||
.rpc_client
|
||||
.block_info(context::current(), block_selector)
|
||||
.await
|
||||
.map_err(rpc_err)?
|
||||
{
|
||||
Some(info) => Ok(info),
|
||||
None => Err(not_found_err()),
|
||||
}
|
||||
}
|
||||
3
src/rpc/mod.rs
Normal file
3
src/rpc/mod.rs
Normal file
@ -0,0 +1,3 @@
|
||||
pub mod block_digest;
|
||||
pub mod block_info;
|
||||
pub mod utxo_digest;
|
||||
28
src/rpc/utxo_digest.rs
Normal file
28
src/rpc/utxo_digest.rs
Normal file
@ -0,0 +1,28 @@
|
||||
use axum::extract::Path;
|
||||
use axum::extract::State;
|
||||
use axum::response::IntoResponse;
|
||||
use axum::response::Json;
|
||||
use neptune_core::prelude::twenty_first::math::digest::Digest;
|
||||
use std::sync::Arc;
|
||||
use tarpc::context;
|
||||
|
||||
use crate::{
|
||||
http_util::{not_found_err, rpc_err},
|
||||
model::app_state::AppState,
|
||||
};
|
||||
|
||||
#[axum::debug_handler]
|
||||
pub async fn utxo_digest(
|
||||
Path(index): Path<u64>,
|
||||
State(state): State<Arc<AppState>>,
|
||||
) -> Result<Json<Digest>, impl IntoResponse> {
|
||||
match state
|
||||
.rpc_client
|
||||
.utxo_digest(context::current(), index)
|
||||
.await
|
||||
.map_err(rpc_err)?
|
||||
{
|
||||
Some(digest) => Ok(Json(digest)),
|
||||
None => Err(not_found_err()),
|
||||
}
|
||||
}
|
||||
@ -47,6 +47,8 @@ Quick Lookup:
|
||||
|
||||
<h2>REST RPCs</h2>
|
||||
|
||||
<div class="box">
|
||||
|
||||
<h3>/block_info</h3>
|
||||
<div class="indent">
|
||||
<h4>Examples</h4>
|
||||
@ -57,6 +59,9 @@ Quick Lookup:
|
||||
<a href="/rpc/block_info/digest/{{self.genesis_digest.to_hex()}}">/rpc/block_info/digest/{{self.genesis_digest.to_hex()}}</a><br/>
|
||||
<a href="/rpc/block_info/height_or_digest/1">/rpc/block_info/height_or_digest/1</a><br/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="box">
|
||||
|
||||
<h3>/block_digest</h3>
|
||||
<div class="indent">
|
||||
@ -65,9 +70,12 @@ Quick Lookup:
|
||||
<a href="/rpc/block_digest/genesis">/rpc/block_digest/genesis</a><br/>
|
||||
<a href="/rpc/block_digest/tip">/rpc/block_digest/tip</a><br/>
|
||||
<a href="/rpc/block_digest/height/2">/rpc/block_digest/height/2</a><br/>
|
||||
<a href="/rpc/block_digest/digest/{genesis_block_hex}">/rpc/block_digest/digest/{genesis_block_hex}</a><br/>
|
||||
<a href="/rpc/block_digest/height_or_digest/{genesis_block_hex}">/rpc/block_digest/height_or_digest/{genesis_block_hex}</a><br/>
|
||||
<a href="/rpc/block_digest/digest/{{self.genesis_digest.to_hex()}}">/rpc/block_digest/digest/{{self.genesis_digest.to_hex()}}</a><br/>
|
||||
<a href="/rpc/block_digest/height_or_digest/{{self.genesis_digest.to_hex()}}">/rpc/block_digest/height_or_digest/{{self.genesis_digest.to_hex()}}</a><br/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="box">
|
||||
|
||||
<h3>/utxo_digest</h3>
|
||||
<div class="indent">
|
||||
@ -76,5 +84,7 @@ Quick Lookup:
|
||||
<a href="/rpc/utxo_digest/2">/rpc/utxo_digest/2</a><br/>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user