Skip to content

Commit

Permalink
Add /r/children recursive endpoint (ordinals#2431)
Browse files Browse the repository at this point in the history
  • Loading branch information
elocremarc authored Nov 24, 2023
1 parent 94350b1 commit 69e0e02
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 8 deletions.
113 changes: 106 additions & 7 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ use {
page_config::PageConfig,
runes::Rune,
templates::{
BlockHtml, BlockJson, BlocksHtml, ChildrenHtml, ClockSvg, CollectionsHtml, HomeHtml,
InputHtml, InscriptionHtml, InscriptionJson, InscriptionsBlockHtml, InscriptionsHtml,
InscriptionsJson, OutputHtml, OutputJson, PageContent, PageHtml, PreviewAudioHtml,
PreviewCodeHtml, PreviewFontHtml, PreviewImageHtml, PreviewMarkdownHtml, PreviewModelHtml,
PreviewPdfHtml, PreviewTextHtml, PreviewUnknownHtml, PreviewVideoHtml, RangeHtml, RareTxt,
RuneHtml, RunesHtml, SatHtml, SatInscriptionJson, SatInscriptionsJson, SatJson,
TransactionHtml,
BlockHtml, BlockJson, BlocksHtml, ChildrenHtml, ChildrenJson, ClockSvg, CollectionsHtml,
HomeHtml, InputHtml, InscriptionHtml, InscriptionJson, InscriptionsBlockHtml,
InscriptionsHtml, InscriptionsJson, OutputHtml, OutputJson, PageContent, PageHtml,
PreviewAudioHtml, PreviewCodeHtml, PreviewFontHtml, PreviewImageHtml, PreviewMarkdownHtml,
PreviewModelHtml, PreviewPdfHtml, PreviewTextHtml, PreviewUnknownHtml, PreviewVideoHtml,
RangeHtml, RareTxt, RuneHtml, RunesHtml, SatHtml, SatInscriptionJson, SatInscriptionsJson,
SatJson, TransactionHtml,
},
},
axum::{
Expand Down Expand Up @@ -238,6 +238,11 @@ impl Server {
)
.route("/r/blockheight", get(Self::block_height))
.route("/r/blocktime", get(Self::block_time))
.route("/r/children/:inscription_id", get(Self::children_recursive))
.route(
"/r/children/:inscription_id/:page",
get(Self::children_recursive_paginated),
)
.route("/r/metadata/:inscription_id", get(Self::metadata))
.route("/r/sat/:sat_number", get(Self::sat_inscriptions))
.route(
Expand Down Expand Up @@ -1352,6 +1357,28 @@ impl Server {
)
}

async fn children_recursive(
Extension(index): Extension<Arc<Index>>,
Path(inscription_id): Path<InscriptionId>,
) -> ServerResult<Response> {
Self::children_recursive_paginated(Extension(index), Path((inscription_id, 0))).await
}

async fn children_recursive_paginated(
Extension(index): Extension<Arc<Index>>,
Path((parent, page)): Path<(InscriptionId, usize)>,
) -> ServerResult<Response> {
let parent_sequence_number = index
.get_inscription_entry(parent)?
.ok_or_not_found(|| format!("inscription {parent}"))?
.sequence_number;

let (ids, more) =
index.get_children_by_sequence_number_paginated(parent_sequence_number, 100, page)?;

Ok(Json(ChildrenJson { ids, more, page }).into_response())
}

async fn inscriptions(
Extension(page_config): Extension<Arc<PageConfig>>,
Extension(index): Extension<Arc<Index>>,
Expand Down Expand Up @@ -4496,4 +4523,76 @@ next
.id
.is_none());
}

#[test]
fn children_recursive_endpoint() {
let server = TestServer::new_with_regtest_with_json_api();
server.mine_blocks(1);

let parent_txid = server.bitcoin_rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(1, 0, 0, inscription("text/plain", "hello").to_witness())],
..Default::default()
});

let parent_inscription_id = InscriptionId {
txid: parent_txid,
index: 0,
};

server.assert_response(
format!("/r/children/{parent_inscription_id}"),
StatusCode::NOT_FOUND,
&format!("inscription {parent_inscription_id} not found"),
);

server.mine_blocks(1);

let children_json =
server.get_json::<ChildrenJson>(format!("/r/children/{parent_inscription_id}"));
assert_eq!(children_json.ids.len(), 0);

let mut builder = script::Builder::new();
for _ in 0..111 {
builder = Inscription {
content_type: Some("text/plain".into()),
body: Some("hello".into()),
parent: Some(parent_inscription_id.parent_value()),
unrecognized_even_field: false,
..Default::default()
}
.append_reveal_script_to_builder(builder);
}

let witness = Witness::from_slice(&[builder.into_bytes(), Vec::new()]);

let txid = server.bitcoin_rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(2, 0, 0, witness), (2, 1, 0, Default::default())],
..Default::default()
});

server.mine_blocks(1);

let first_child_inscription_id = InscriptionId { txid, index: 0 };
let hundredth_child_inscription_id = InscriptionId { txid, index: 99 };
let hundred_first_child_inscription_id = InscriptionId { txid, index: 100 };
let hundred_eleventh_child_inscription_id = InscriptionId { txid, index: 110 };

let children_json =
server.get_json::<ChildrenJson>(format!("/r/children/{parent_inscription_id}"));

assert_eq!(children_json.ids.len(), 100);
assert_eq!(children_json.ids[0], first_child_inscription_id);
assert_eq!(children_json.ids[99], hundredth_child_inscription_id);
assert!(children_json.more);
assert_eq!(children_json.page, 0);

let children_json =
server.get_json::<ChildrenJson>(format!("/r/children/{parent_inscription_id}/1"));

assert_eq!(children_json.ids.len(), 11);
assert_eq!(children_json.ids[0], hundred_first_child_inscription_id);
assert_eq!(children_json.ids[10], hundred_eleventh_child_inscription_id);
assert!(!children_json.more);
assert_eq!(children_json.page, 1);
}
}
2 changes: 1 addition & 1 deletion src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use {super::*, boilerplate::Boilerplate};
pub(crate) use {
block::{BlockHtml, BlockJson},
blocks::BlocksHtml,
children::ChildrenHtml,
children::{ChildrenHtml, ChildrenJson},
clock::ClockSvg,
collections::CollectionsHtml,
home::HomeHtml,
Expand Down
7 changes: 7 additions & 0 deletions src/templates/children.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ pub(crate) struct ChildrenHtml {
pub(crate) next_page: Option<usize>,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct ChildrenJson {
pub ids: Vec<InscriptionId>,
pub more: bool,
pub page: usize,
}

impl PageContent for ChildrenHtml {
fn title(&self) -> String {
format!("Inscription {} Children", self.parent_number)
Expand Down

0 comments on commit 69e0e02

Please sign in to comment.