From ecc97a1eac4a58ad41570060429017cb61b5702e Mon Sep 17 00:00:00 2001 From: phorkish Date: Mon, 13 May 2024 13:30:33 -0700 Subject: [PATCH 1/2] Added parents recursive endpoint --- docs/src/inscriptions/recursion.md | 2 + src/api.rs | 7 +++ src/subcommand/server.rs | 95 ++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) diff --git a/docs/src/inscriptions/recursion.md b/docs/src/inscriptions/recursion.md index 69365a19b1..98a1a8238a 100644 --- a/docs/src/inscriptions/recursion.md +++ b/docs/src/inscriptions/recursion.md @@ -42,6 +42,8 @@ The recursive endpoints are: - `/r/children//`: the set of 100 child inscription ids on ``. - `/r/inscription/`: information about an inscription - `/r/metadata/`: JSON string containing the hex-encoded CBOR metadata. +- `/r/parents/`: the first 100 parent inscription ids. +- `/r/parents//`: the set of 100 parent inscription ids on ``. - `/r/sat/`: the first 100 inscription ids on a sat. - `/r/sat//`: the set of 100 inscription ids on ``. - `/r/sat//at/`: the inscription id at `` of all inscriptions on a sat. `` may be a negative number to index from the back. `0` being the first and `-1` being the most recent for example. diff --git a/src/api.rs b/src/api.rs index 0cd03d7e1a..424905a512 100644 --- a/src/api.rs +++ b/src/api.rs @@ -162,6 +162,13 @@ impl Output { } } +#[derive(Debug, PartialEq, Serialize, Deserialize)] +pub struct Parents { + pub ids: Vec, + pub more: bool, + pub page: usize, +} + #[derive(Debug, PartialEq, Serialize, Deserialize)] pub struct Sat { pub block: u32, diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 954264a79b..81fe5cbdaf 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -241,6 +241,11 @@ impl Server { get(Self::children_recursive_paginated), ) .route("/r/metadata/:inscription_id", get(Self::metadata)) + .route("/r/parents/:inscription_id", get(Self::parents_recursive)) + .route( + "/r/parents/:inscription_id/:page", + get(Self::parents_recursive_paginated), + ) .route("/r/sat/:sat_number", get(Self::sat_inscriptions)) .route( "/r/sat/:sat_number/:page", @@ -1842,6 +1847,28 @@ impl Server { }) } + async fn parents_recursive( + Extension(index): Extension>, + Path(inscription_id): Path, + ) -> ServerResult { + Self::parents_recursive_paginated(Extension(index), Path((inscription_id, 0))).await + } + + async fn parents_recursive_paginated( + Extension(index): Extension>, + Path((inscription_id, page)): Path<(InscriptionId, usize)>, + ) -> ServerResult { + task::block_in_place(|| { + let child = index + .get_inscription_entry(inscription_id)? + .ok_or_not_found(|| format!("inscription {inscription_id}"))?; + + let (ids, more) = index.get_parents_by_sequence_number_paginated(child.parents, page)?; + + Ok(Json(api::Parents { ids, more, page }).into_response()) + }) + } + async fn sat_inscriptions( Extension(index): Extension>, Path(sat): Path, @@ -5969,6 +5996,74 @@ next assert_eq!(children_json.page, 1); } + #[test] + fn parents_recursive_endpoint() { + let server = TestServer::builder().chain(Chain::Regtest).build(); + server.mine_blocks(1); + + let mut parent_ids = Vec::new(); + let mut inputs = Vec::new(); + for i in 0..111 { + parent_ids.push(InscriptionId { + txid: server.core.broadcast_tx(TransactionTemplate { + inputs: &[(i + 1, 0, 0, inscription("text/plain", "hello").to_witness())], + ..default() + }), + index: 0, + }); + + inputs.push((i + 2, 1, 0, Witness::default())); + + server.mine_blocks(1); + } + + inputs.insert( + 0, + ( + 112, + 0, + 0, + Inscription { + content_type: Some("text/plain".into()), + body: Some("hello".into()), + parents: parent_ids.iter().map(|id| id.value()).collect(), + ..default() + } + .to_witness(), + ), + ); + + let txid = server.core.broadcast_tx(TransactionTemplate { + inputs: &inputs, + ..default() + }); + + server.mine_blocks(1); + + let inscription_id = InscriptionId { txid, index: 0 }; + + let first_parent_inscription_id = parent_ids[0]; + let hundredth_parent_inscription_id = parent_ids[99]; + let hundred_first_parent_inscription_id = parent_ids[100]; + let hundred_eleventh_parent_inscription_id = parent_ids[110]; + + let parents_json = server.get_json::(format!("/r/parents/{inscription_id}")); + + assert_eq!(parents_json.ids.len(), 100); + assert_eq!(parents_json.ids[0], first_parent_inscription_id); + assert_eq!(parents_json.ids[99], hundredth_parent_inscription_id); + assert!(parents_json.more); + assert_eq!(parents_json.page, 0); + + let parents_json = server.get_json::(format!("/r/parents/{inscription_id}/1")); + + assert_eq!(parents_json.ids.len(), 11); + assert_eq!(parents_json.ids[0], hundred_first_parent_inscription_id); + assert_eq!(parents_json.ids[10], hundred_eleventh_parent_inscription_id); + assert!(!parents_json.more); + assert_eq!(parents_json.page, 1); + } + #[test] fn inscriptions_in_block_page() { let server = TestServer::builder() From 18e3affa64963a7978f7bc9e668fa107ec01bad5 Mon Sep 17 00:00:00 2001 From: phorkish Date: Mon, 20 May 2024 13:51:58 -0700 Subject: [PATCH 2/2] replace Parents struct with Inscriptions struct --- src/api.rs | 7 ------- src/subcommand/server.rs | 21 ++++++++++++++++----- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/api.rs b/src/api.rs index 6fa784b2c3..9efdf1e48d 100644 --- a/src/api.rs +++ b/src/api.rs @@ -165,13 +165,6 @@ impl Output { } } -#[derive(Debug, PartialEq, Serialize, Deserialize)] -pub struct Parents { - pub ids: Vec, - pub more: bool, - pub page: usize, -} - #[derive(Debug, PartialEq, Serialize, Deserialize)] pub struct Sat { pub block: u32, diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 7ddaa8f99e..c4d1234d27 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -1891,7 +1891,17 @@ impl Server { let (ids, more) = index.get_parents_by_sequence_number_paginated(child.parents, page)?; - Ok(Json(api::Parents { ids, more, page }).into_response()) + let page_index = + u32::try_from(page).map_err(|_| anyhow!("page index {} out of range", page))?; + + Ok( + Json(api::Inscriptions { + ids, + more, + page_index, + }) + .into_response(), + ) }) } @@ -6073,21 +6083,22 @@ next let hundred_first_parent_inscription_id = parent_ids[100]; let hundred_eleventh_parent_inscription_id = parent_ids[110]; - let parents_json = server.get_json::(format!("/r/parents/{inscription_id}")); + let parents_json = server.get_json::(format!("/r/parents/{inscription_id}")); assert_eq!(parents_json.ids.len(), 100); assert_eq!(parents_json.ids[0], first_parent_inscription_id); assert_eq!(parents_json.ids[99], hundredth_parent_inscription_id); assert!(parents_json.more); - assert_eq!(parents_json.page, 0); + assert_eq!(parents_json.page_index, 0); - let parents_json = server.get_json::(format!("/r/parents/{inscription_id}/1")); + let parents_json = + server.get_json::(format!("/r/parents/{inscription_id}/1")); assert_eq!(parents_json.ids.len(), 11); assert_eq!(parents_json.ids[0], hundred_first_parent_inscription_id); assert_eq!(parents_json.ids[10], hundred_eleventh_parent_inscription_id); assert!(!parents_json.more); - assert_eq!(parents_json.page, 1); + assert_eq!(parents_json.page_index, 1); } #[test]