Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add children recursive endpoint #2431

Merged
merged 16 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 104 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))
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
.route("/r/sat/:sat", get(Self::sat_inscriptions))
.route("/r/sat/:sat/:page", get(Self::sat_inscriptions_paginated))
Expand Down Expand Up @@ -1346,6 +1351,35 @@ 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}"))?
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
.sequence_number;

let (children, more_children) =
index.get_children_by_sequence_number_paginated(parent_sequence_number, 100, page)?;

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

async fn inscriptions(
Extension(page_config): Extension<Arc<PageConfig>>,
Extension(index): Extension<Arc<Index>>,
Expand Down Expand Up @@ -2496,6 +2530,69 @@ mod tests {
assert_eq!(response.text().unwrap(), "1231006505");
}

#[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()
});

server.mine_blocks(1);

let parent_inscription_id = InscriptionId {
txid: parent_txid,
index: 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);
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
}

#[test]
fn range_end_before_range_start_returns_400() {
TestServer::new().assert_response(
Expand Down
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