Skip to content

Commit

Permalink
feat: add more best states API for NS-Indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Jan 3, 2024
1 parent 2c59f86 commit 81d50d2
Show file tree
Hide file tree
Showing 8 changed files with 322 additions and 50 deletions.
2 changes: 1 addition & 1 deletion crates/ns-indexer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ns-indexer"
version = "0.3.1"
version = "0.4.0"
edition = "2021"
rust-version = "1.64"
description = "Name & Service Protocol indexer service in Rust"
Expand Down
198 changes: 161 additions & 37 deletions crates/ns-indexer/doc/ns-indexer-api-insomnia-v1.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
_type: export
__export_format: 4
__export_date: 2024-01-01T08:44:27.315Z
__export_date: 2024-01-03T04:26:57.860Z
__export_source: insomnia.desktop.app:v8.5.1
resources:
- _id: req_b1a618c6a5b7424f82a71b4d04a382db
Expand Down Expand Up @@ -30,9 +30,9 @@ resources:
_type: request
- _id: wrk_6740d036556540e7972210367d3cbc69
parentId: null
modified: 1702698428340
modified: 1704255390271
created: 1702698428340
name: Indexer API
name: NS-Indexer API
description: ""
scope: collection
_type: workspace
Expand Down Expand Up @@ -63,10 +63,10 @@ resources:
_type: request
- _id: req_c4a8deb0e59343708f6540522b0772f6
parentId: wrk_6740d036556540e7972210367d3cbc69
modified: 1702901552482
modified: 1704255617700
created: 1702871420490
url: "{{ _.api_host }}/best/inscription"
name: best_inscription/get
url: "{{ _.api_host }}/best/inscription/get_last"
name: best_inscription/get_last
description: ""
method: GET
body: {}
Expand All @@ -86,6 +86,68 @@ resources:
settingRebuildPath: true
settingFollowRedirects: global
_type: request
- _id: req_363bdc0bd5fc427a92083954b4b75286
parentId: wrk_6740d036556540e7972210367d3cbc69
modified: 1704255791206
created: 1704255639766
url: "{{ _.api_host }}/best/inscription"
name: best_inscription/get
description: ""
method: GET
body: {}
parameters:
- id: pair_6c07f58e12cd4368bf9ac052134f175e
name: name
value: 𝚡
description: ""
- id: pair_c69e64df93ba4498b209cf77e39cd013
name: sequence
value: "0"
description: ""
headers:
- name: User-Agent
value: insomnia/8.4.5
- name: Accept
value: application/json
authentication: {}
metaSortKey: -1701649934666.5781
isPrivate: false
settingStoreCookies: true
settingSendCookies: true
settingDisableRenderRequestBody: false
settingEncodeUrl: true
settingRebuildPath: true
settingFollowRedirects: global
_type: request
- _id: req_e7b6cf526bb648cfab8f05c5d49bfeb8
parentId: wrk_6740d036556540e7972210367d3cbc69
modified: 1704255740036
created: 1704255718590
url: "{{ _.api_host }}/best/inscription/get_by_height"
name: best_inscription/get_by_height
description: ""
method: GET
body: {}
parameters:
- id: pair_6c07f58e12cd4368bf9ac052134f175e
name: height
value: "1997"
description: ""
headers:
- name: User-Agent
value: insomnia/8.4.5
- name: Accept
value: application/json
authentication: {}
metaSortKey: -1701609605064.5234
isPrivate: false
settingStoreCookies: true
settingSendCookies: true
settingDisableRenderRequestBody: false
settingEncodeUrl: true
settingRebuildPath: true
settingFollowRedirects: global
_type: request
- _id: req_56d3cfbe6d7943098e89d202c0edd557
parentId: wrk_6740d036556540e7972210367d3cbc69
modified: 1702901563095
Expand All @@ -111,6 +173,97 @@ resources:
settingRebuildPath: true
settingFollowRedirects: global
_type: request
- _id: req_93b0153eebef4b37b1e55d3d92a65356
parentId: wrk_6740d036556540e7972210367d3cbc69
modified: 1704255803344
created: 1704255784178
url: "{{ _.api_host }}/best/name"
name: best_name/get
description: ""
method: GET
body: {}
parameters:
- id: pair_6c07f58e12cd4368bf9ac052134f175e
name: name
value: 𝚡
description: ""
headers:
- name: User-Agent
value: insomnia/8.4.5
- name: Accept
value: application/json
authentication: {}
metaSortKey: -1701488616258.3594
isPrivate: false
settingStoreCookies: true
settingSendCookies: true
settingDisableRenderRequestBody: false
settingEncodeUrl: true
settingRebuildPath: true
settingFollowRedirects: global
_type: request
- _id: req_1a7760f6eb3c4a36ad1c194412e42775
parentId: wrk_6740d036556540e7972210367d3cbc69
modified: 1704255862830
created: 1704255851797
url: "{{ _.api_host }}/best/service"
name: best_service/get
description: ""
method: GET
body: {}
parameters:
- id: pair_6c07f58e12cd4368bf9ac052134f175e
name: name
value: 𝚡
description: ""
- id: pair_4706db882d41468397746d4ede78a07b
name: code
value: "0"
description: ""
headers:
- name: User-Agent
value: insomnia/8.4.5
- name: Accept
value: application/json
authentication: {}
metaSortKey: -1701448286656.3047
isPrivate: false
settingStoreCookies: true
settingSendCookies: true
settingDisableRenderRequestBody: false
settingEncodeUrl: true
settingRebuildPath: true
settingFollowRedirects: global
_type: request
- _id: req_1b571fe4b2c349eeb27f02211c72cfc3
parentId: wrk_6740d036556540e7972210367d3cbc69
modified: 1704255928462
created: 1703043892732
url: "{{ _.api_host }}/best/utxo/list"
name: best_utxo/list_by_address
description: ""
method: GET
body: {}
parameters:
- id: pair_8352d8e48b92475fb3c8f48593275b6e
name: address
value: bc1q6dukpvmcxae0pdh95zgh793l5ept8fluhqqnyc
description: ""
headers:
- name: User-Agent
value: insomnia/8.4.5
- name: Accept
value: application/json
authentication: {}
metaSortKey: -1701428121855.2773
isPrivate: false
settingStoreCookies: true
settingSendCookies: true
settingDisableRenderRequestBody: false
settingEncodeUrl: true
settingRebuildPath: true
settingFollowRedirects: global
_type: request
- _id: req_f7592cdb232f44468a3031caedeaf11f
parentId: wrk_6740d036556540e7972210367d3cbc69
modified: 1702866762535
Expand Down Expand Up @@ -287,7 +440,7 @@ resources:
_type: request
- _id: req_c1217c2f6388434a88c0d2f0cf2d7d49
parentId: wrk_6740d036556540e7972210367d3cbc69
modified: 1703302708284
modified: 1704255831362
created: 1702889481164
url: "{{ _.api_host }}/v1/name"
name: name/get
Expand All @@ -297,7 +450,7 @@ resources:
parameters:
- id: pair_8352d8e48b92475fb3c8f48593275b6e
name: name
value: ab
value: 𝚡
description: ""
headers:
- name: User-Agent
Expand Down Expand Up @@ -434,35 +587,6 @@ resources:
settingRebuildPath: true
settingFollowRedirects: global
_type: request
- _id: req_1b571fe4b2c349eeb27f02211c72cfc3
parentId: wrk_6740d036556540e7972210367d3cbc69
modified: 1703481569875
created: 1703043892732
url: "{{ _.api_host }}/best/utxo/list"
name: utxo/list_by_address
description: ""
method: GET
body: {}
parameters:
- id: pair_8352d8e48b92475fb3c8f48593275b6e
name: address
value: bc1q6dukpvmcxae0pdh95zgh793l5ept8fluhqqnyc
description: ""
headers:
- name: User-Agent
value: insomnia/8.4.5
- name: Accept
value: application/json
authentication: {}
metaSortKey: -1700119930355.816
isPrivate: false
settingStoreCookies: true
settingSendCookies: true
settingDisableRenderRequestBody: false
settingEncodeUrl: true
settingRebuildPath: true
settingFollowRedirects: global
_type: request
- _id: env_521c603fd2195d973072305ede8865ff09f1a5e4
parentId: wrk_6740d036556540e7972210367d3cbc69
modified: 1702698491521
Expand Down
95 changes: 86 additions & 9 deletions crates/ns-indexer/src/api/inscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,30 @@ impl InscriptionAPI {
pub async fn get_last_accepted(
Extension(ctx): Extension<Arc<ReqContext>>,
to: PackObject<()>,
State(api): State<Arc<IndexerAPI>>,
State(app): State<Arc<IndexerAPI>>,
) -> Result<PackObject<SuccessResponse<Inscription>>, HTTPError> {
ctx.set("action", "get_last_accepted_inscription".into())
.await;

let last_accepted_state = api.state.last_accepted.read().await;
let last_accepted_state = app.state.last_accepted.read().await;

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(
pub async fn get_last_best(
Extension(ctx): Extension<Arc<ReqContext>>,
to: PackObject<()>,
State(api): State<Arc<IndexerAPI>>,
State(app): State<Arc<IndexerAPI>>,
) -> Result<PackObject<SuccessResponse<Inscription>>, HTTPError> {
ctx.set("action", "get_best_inscription".into()).await;
ctx.set("action", "get_last_best_inscription".into()).await;

let best_inscriptions_state = api.state.best_inscriptions.read().await;
let best_inscriptions_state = app.state.best_inscriptions.read().await;
let mut inscription = best_inscriptions_state.back().cloned();
if inscription.is_none() {
let last_accepted_state = api.state.last_accepted.read().await;
let last_accepted_state = app.state.last_accepted.read().await;
inscription = last_accepted_state.clone();
}

Expand All @@ -54,6 +54,83 @@ impl InscriptionAPI {
}
}

pub async fn get_best(
State(app): State<Arc<IndexerAPI>>,
Extension(ctx): Extension<Arc<ReqContext>>,
to: PackObject<()>,
input: Query<QueryName>,
) -> Result<PackObject<SuccessResponse<Inscription>>, HTTPError> {
input.validate()?;
if input.sequence.is_none() {
return Err(HTTPError::new(400, "sequence is required".to_string()));
}

let name = input.name.clone();
let sequence = input.sequence.unwrap() as u64;
ctx.set_kvs(vec![
("action", "get_best_inscription".into()),
("name", name.clone().into()),
("sequence", sequence.into()),
])
.await;

{
let best_inscriptions_state = app.state.best_inscriptions.read().await;
for i in best_inscriptions_state.iter() {
if i.name == name && i.sequence == sequence {
return Ok(to.with(SuccessResponse::new(i.clone())));
}
}
}

{
let last_accepted_state = app.state.last_accepted.read().await;
if let Some(ins) = last_accepted_state.as_ref() {
if ins.name == name && ins.sequence == sequence {
return Ok(to.with(SuccessResponse::new(ins.clone())));
}
}
}

Err(HTTPError::new(404, "not found".to_string()))
}

pub async fn get_best_by_height(
State(app): State<Arc<IndexerAPI>>,
Extension(ctx): Extension<Arc<ReqContext>>,
to: PackObject<()>,
input: Query<QueryHeight>,
) -> Result<PackObject<SuccessResponse<Inscription>>, HTTPError> {
input.validate()?;

let height = input.height;
ctx.set_kvs(vec![
("action", "get_best_inscription_by_height".into()),
("height", height.into()),
])
.await;

{
let best_inscriptions_state = app.state.best_inscriptions.read().await;
for i in best_inscriptions_state.iter() {
if i.height == height as u64 {
return Ok(to.with(SuccessResponse::new(i.clone())));
}
}
}

{
let last_accepted_state = app.state.last_accepted.read().await;
if let Some(ins) = last_accepted_state.as_ref() {
if ins.height == height as u64 {
return Ok(to.with(SuccessResponse::new(ins.clone())));
}
}
}

Err(HTTPError::new(404, "not found".to_string()))
}

pub async fn get(
State(app): State<Arc<IndexerAPI>>,
Extension(ctx): Extension<Arc<ReqContext>>,
Expand Down Expand Up @@ -103,10 +180,10 @@ impl InscriptionAPI {
pub async fn list_best(
Extension(ctx): Extension<Arc<ReqContext>>,
to: PackObject<()>,
State(api): State<Arc<IndexerAPI>>,
State(app): State<Arc<IndexerAPI>>,
) -> Result<PackObject<SuccessResponse<Vec<Inscription>>>, HTTPError> {
ctx.set("action", "list_best_inscriptions".into()).await;
let best_inscriptions_state = api.state.best_inscriptions.read().await;
let best_inscriptions_state = app.state.best_inscriptions.read().await;
Ok(to.with(SuccessResponse::new(
best_inscriptions_state.iter().cloned().collect(),
)))
Expand Down
Loading

0 comments on commit 81d50d2

Please sign in to comment.