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 5 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
2 changes: 2 additions & 0 deletions docs/src/inscriptions/recursion.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ A few other endpoints that inscriptions may access are the following:
- `/blockhash`: latest block hash.
- `/blockhash/<HEIGHT>`: block hash at given block height.
- `/blocktime`: UNIX time stamp of latest block.
- `/children/<INSCRIPTION_ID>`: list of children that have the given
inscription as their parent.
58 changes: 57 additions & 1 deletion src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ impl Server {
.route("/install.sh", get(Self::install_script))
.route("/ordinal/:sat", get(Self::ordinal))
.route("/output/:output", get(Self::output))
.route("/children/:inscription_id", get(Self::children))
.route("/preview/:inscription_id", get(Self::preview))
.route("/range/:start/:end", get(Self::range))
.route("/rare.txt", get(Self::rare_txt))
Expand Down Expand Up @@ -833,6 +834,14 @@ impl Server {
)
}

async fn children(
Extension(index): Extension<Arc<Index>>,
Path(inscription_id): Path<InscriptionId>,
) -> ServerResult<Json<Vec<InscriptionId>>> {
let children = index.get_children_by_inscription_id(inscription_id)?;
Ok(Json(children))
}

async fn input(
Extension(page_config): Extension<Arc<PageConfig>>,
Extension(index): Extension<Arc<Index>>,
Expand Down Expand Up @@ -903,7 +912,7 @@ impl Server {
);
headers.append(
header::CONTENT_SECURITY_POLICY,
HeaderValue::from_static("default-src *:*/content/ *:*/blockheight *:*/blockhash *:*/blockhash/ *:*/blocktime 'unsafe-eval' 'unsafe-inline' data: blob:"),
HeaderValue::from_static("default-src *:*/content/ *:*/blockheight *:*/blockhash *:*/blockhash/ *:*/blocktime *:*/children/ 'unsafe-eval' 'unsafe-inline' data: blob:"),
);
headers.insert(
header::CACHE_CONTROL,
Expand Down Expand Up @@ -1762,6 +1771,53 @@ mod tests {
assert_eq!(response.text().unwrap(), "1231006505");
}

#[test]
fn children_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 txid = server.bitcoin_rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[
(
2,
0,
0,
Inscription {
content_type: Some("text/plain".into()),
body: Some("hello".into()),
parent: Some(parent_inscription_id.parent_value()),
unrecognized_even_field: false,
}
.to_witness(),
),
(2, 1, 0, Default::default()),
],
..Default::default()
});

server.mine_blocks(1);

let child_inscription_id = InscriptionId { txid, index: 0 };

server.assert_response_regex(
format!("/inscription/{parent_inscription_id}"),
StatusCode::OK,
format!(".*[{child_inscription_id}].*"),
);
}

#[test]
fn range_end_before_range_start_returns_400() {
TestServer::new().assert_response(
Expand Down
2 changes: 1 addition & 1 deletion tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ fn inscription_content() {
.collect::<Vec<&http::HeaderValue>>(),
&[
"default-src 'self' 'unsafe-eval' 'unsafe-inline' data: blob:",
"default-src *:*/content/ *:*/blockheight *:*/blockhash *:*/blockhash/ *:*/blocktime 'unsafe-eval' 'unsafe-inline' data: blob:",
"default-src *:*/content/ *:*/blockheight *:*/blockhash *:*/blockhash/ *:*/blocktime *:*/children/ 'unsafe-eval' 'unsafe-inline' data: blob:",
]
);
assert_eq!(response.bytes().unwrap(), "FOO");
Expand Down