Skip to content

Commit

Permalink
chore: improve Indexer API
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Dec 18, 2023
1 parent 044a19d commit 3468e87
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
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

# ----- ns-indexer env -----
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
Expand Down
14 changes: 10 additions & 4 deletions crates/ns-indexer/src/api/inscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,23 @@ impl InscriptionAPI {
Extension(ctx): Extension<Arc<ReqContext>>,
to: PackObject<()>,
State(api): State<Arc<IndexerAPI>>,
) -> Result<PackObject<SuccessResponse<Option<Inscription>>>, HTTPError> {
) -> Result<PackObject<SuccessResponse<Inscription>>, 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<Arc<ReqContext>>,
to: PackObject<()>,
State(api): State<Arc<IndexerAPI>>,
) -> Result<PackObject<SuccessResponse<Option<Inscription>>>, HTTPError> {
) -> Result<PackObject<SuccessResponse<Inscription>>, HTTPError> {
ctx.set("action", "get_best_inscription".into()).await;

let best_inscriptions_state = api.state.best_inscriptions.read().await;
Expand All @@ -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(
Expand Down
15 changes: 9 additions & 6 deletions crates/ns-indexer/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down
9 changes: 0 additions & 9 deletions crates/ns-indexer/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
8 changes: 6 additions & 2 deletions crates/ns-indexer/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,10 @@ pub fn new(state: Arc<api::IndexerAPI>) -> 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),
Expand All @@ -59,6 +57,12 @@ pub fn new(state: Arc<api::IndexerAPI>) -> 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(
Expand Down
21 changes: 20 additions & 1 deletion crates/ns-protocol/src/ns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
))),
}
Expand Down Expand Up @@ -621,6 +621,25 @@ impl<'de> Deserialize<'de> for Signature {
}
}

impl Serialize for PublicKeyParams {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
Value::from(self).serialize(serializer)
}
}

impl<'de> Deserialize<'de> for PublicKeyParams {
fn deserialize<D>(deserializer: D) -> Result<PublicKeyParams, D::Error>
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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down

0 comments on commit 3468e87

Please sign in to comment.