From 3468e87a0566ae037907422b0009ff174aac42c0 Mon Sep 17 00:00:00 2001 From: 0xZensh Date: Mon, 18 Dec 2023 20:26:58 +0800 Subject: [PATCH] chore: improve Indexer API --- .env.sample | 3 ++- crates/ns-indexer/src/api/inscription.rs | 14 ++++++++++---- crates/ns-indexer/src/bin/main.rs | 15 +++++++++------ crates/ns-indexer/src/indexer.rs | 9 --------- crates/ns-indexer/src/router.rs | 8 ++++++-- crates/ns-protocol/src/ns.rs | 21 ++++++++++++++++++++- 6 files changed, 47 insertions(+), 23 deletions(-) diff --git a/.env.sample b/.env.sample index 13a35bf..4da167f 100644 --- a/.env.sample +++ b/.env.sample @@ -1,4 +1,4 @@ -BITCOIN_RPC_URL=http://127.0.0.1:8332 +BITCOIN_RPC_URL=http://127.0.0.1:18443 BITCOIN_RPC_USER=test BITCOIN_RPC_PASSWORD=123456 @@ -6,6 +6,7 @@ BITCOIN_RPC_PASSWORD=123456 LOG_LEVEL=info INDEXER_SERVER_WORKER_THREADS=0 # defaults to the number of cpus on the system INDEXER_SERVER_ADDR=0.0.0.0:8080 +INDEXER_SERVER_NOSCAN=false # run as API server INDEXER_START_HEIGHT=0 # more nodes split by comma SCYLLA_NODES=127.0.0.1:9042 diff --git a/crates/ns-indexer/src/api/inscription.rs b/crates/ns-indexer/src/api/inscription.rs index 118164d..061f24c 100644 --- a/crates/ns-indexer/src/api/inscription.rs +++ b/crates/ns-indexer/src/api/inscription.rs @@ -22,20 +22,23 @@ impl InscriptionAPI { Extension(ctx): Extension>, to: PackObject<()>, State(api): State>, - ) -> Result>>, HTTPError> { + ) -> Result>, HTTPError> { ctx.set("action", "get_last_accepted_inscription".into()) .await; let last_accepted_state = api.state.last_accepted.read().await; - Ok(to.with(SuccessResponse::new(last_accepted_state.clone()))) + match last_accepted_state.clone() { + Some(inscription) => Ok(to.with(SuccessResponse::new(inscription))), + None => Err(HTTPError::new(404, "not found".to_string())), + } } pub async fn get_best( Extension(ctx): Extension>, to: PackObject<()>, State(api): State>, - ) -> Result>>, HTTPError> { + ) -> Result>, HTTPError> { ctx.set("action", "get_best_inscription".into()).await; let best_inscriptions_state = api.state.best_inscriptions.read().await; @@ -45,7 +48,10 @@ impl InscriptionAPI { inscription = last_accepted_state.clone(); } - Ok(to.with(SuccessResponse::new(inscription))) + match inscription { + Some(inscription) => Ok(to.with(SuccessResponse::new(inscription))), + None => Err(HTTPError::new(404, "not found".to_string())), + } } pub async fn get( diff --git a/crates/ns-indexer/src/bin/main.rs b/crates/ns-indexer/src/bin/main.rs index c46027c..ecb9e6d 100644 --- a/crates/ns-indexer/src/bin/main.rs +++ b/crates/ns-indexer/src/bin/main.rs @@ -99,13 +99,16 @@ fn main() -> anyhow::Result<()> { Ok::<(), anyhow::Error>(()) }; + let scanning = std::env::var("INDEXER_SERVER_NOSCAN").unwrap_or_default() != "true"; let background_job = async { - match scanner.run(shutdown.clone(), start_height).await { - Ok(_) => log::info!(target: "server", "scanner finished"), - Err(err) => { - log::error!(target: "server", "scanner error: {}", err); - // should exit the process and restart - return Err(err); + if scanning { + match scanner.run(shutdown.clone(), start_height).await { + Ok(_) => log::info!(target: "server", "scanner finished"), + Err(err) => { + log::error!(target: "server", "scanner error: {}", err); + // should exit the process and restart + return Err(err); + } } } diff --git a/crates/ns-indexer/src/indexer.rs b/crates/ns-indexer/src/indexer.rs index 6178f3d..bbe2072 100644 --- a/crates/ns-indexer/src/indexer.rs +++ b/crates/ns-indexer/src/indexer.rs @@ -105,15 +105,6 @@ impl Indexer { for envelope in envelopes { for name in envelope.payload { - log::info!(target: "ns-indexer", - action = "index_name", - block_height = block_height, - block_time = block_time, - txid = envelope.txid.to_string(), - name = name.name, - sequence = name.sequence; - "", - ); match self.index_name(block_height, block_time, &name).await { Err(err) => { if !name.name.is_empty() { diff --git a/crates/ns-indexer/src/router.rs b/crates/ns-indexer/src/router.rs index 84806db..620d4eb 100644 --- a/crates/ns-indexer/src/router.rs +++ b/crates/ns-indexer/src/router.rs @@ -44,12 +44,10 @@ pub fn new(state: Arc) -> Router { "/get_last_accepted", routing::get(api::InscriptionAPI::get_last_accepted), ) - .route("/get_best", routing::get(api::InscriptionAPI::get_best)) .route( "/get_by_height", routing::get(api::InscriptionAPI::get_by_height), ) - .route("/list_best", routing::get(api::InscriptionAPI::list_best)) .route( "/list_by_block_height", routing::get(api::InscriptionAPI::list_by_block_height), @@ -59,6 +57,12 @@ pub fn new(state: Arc) -> Router { routing::get(api::InscriptionAPI::list_by_name), ), ) + .nest( + "/best/inscription", + Router::new() + .route("/", routing::get(api::InscriptionAPI::get_best)) + .route("/list", routing::get(api::InscriptionAPI::list_best)), + ) .nest( "/v1/invalid_inscription", Router::new().route( diff --git a/crates/ns-protocol/src/ns.rs b/crates/ns-protocol/src/ns.rs index 05d7cf1..36eed71 100644 --- a/crates/ns-protocol/src/ns.rs +++ b/crates/ns-protocol/src/ns.rs @@ -556,7 +556,7 @@ impl TryFrom<&Value> for PublicKeyParams { Ok(params) } v => Err(Error::Custom(format!( - "expected array of length 4, got {:?}", + "expected array of length [1, 3], got {:?}", v ))), } @@ -621,6 +621,25 @@ impl<'de> Deserialize<'de> for Signature { } } +impl Serialize for PublicKeyParams { + fn serialize(&self, serializer: S) -> Result + where + S: ser::Serializer, + { + Value::from(self).serialize(serializer) + } +} + +impl<'de> Deserialize<'de> for PublicKeyParams { + fn deserialize(deserializer: D) -> Result + where + D: de::Deserializer<'de>, + { + let val = Value::deserialize(deserializer)?; + PublicKeyParams::try_from(&val).map_err(de::Error::custom) + } +} + impl Serialize for Service { fn serialize(&self, serializer: S) -> Result where