Skip to content

Commit

Permalink
Add recursive directory and make all endpoints JSON (ordinals#2493)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph committed Oct 30, 2023
1 parent fe926e1 commit 8409ce9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
30 changes: 29 additions & 1 deletion src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ impl Server {

let router = Router::new()
.route("/", get(Self::home))
.route("/r/blockheight", get(Self::block_height))
.route("/r/blockhash", get(Self::block_hash_json))
.route(
"/r/blockhash/:height",
get(Self::block_hash_from_height_json),
)
.route("/r/blocktime", get(Self::block_time))
.route("/block/:query", get(Self::block))
.route("/blockcount", get(Self::block_count))
.route("/blockheight", get(Self::block_height))
Expand Down Expand Up @@ -863,6 +870,15 @@ impl Server {
)
}

async fn block_hash_json(Extension(index): Extension<Arc<Index>>) -> ServerResult<Json<String>> {
Ok(Json(
index
.block_hash(None)?
.ok_or_not_found(|| "blockhash")?
.to_string(),
))
}

async fn block_hash_from_height(
Extension(index): Extension<Arc<Index>>,
Path(height): Path<u64>,
Expand All @@ -875,6 +891,18 @@ impl Server {
)
}

async fn block_hash_from_height_json(
Extension(index): Extension<Arc<Index>>,
Path(height): Path<u64>,
) -> ServerResult<Json<String>> {
Ok(Json(
index
.block_hash(Some(height))?
.ok_or_not_found(|| "blockhash")?
.to_string(),
))
}

async fn block_time(Extension(index): Extension<Arc<Index>>) -> ServerResult<String> {
Ok(
index
Expand Down Expand Up @@ -954,7 +982,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 *:*/r/ 'unsafe-eval' 'unsafe-inline' data: blob:"),
);
headers.insert(
header::CACHE_CONTROL,
Expand Down
30 changes: 29 additions & 1 deletion tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,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 *:*/r/ 'unsafe-eval' 'unsafe-inline' data: blob:",
]
);
assert_eq!(response.bytes().unwrap(), "FOO");
Expand Down Expand Up @@ -329,3 +329,31 @@ fn missing_credentials() {
.expected_stderr("error: no bitcoind rpc user specified\n")
.run_and_extract_stdout();
}

#[test]
fn all_endpoints_in_recursive_directory_return_json() {
let rpc_server = test_bitcoincore_rpc::spawn();
create_wallet(&rpc_server);

rpc_server.mine_blocks(2);

let server = TestServer::spawn_with_args(&rpc_server, &[]);

assert_eq!(server.request("/r/blockheight").json::<u64>().unwrap(), 2);

assert_eq!(server.request("/r/blocktime").json::<u64>().unwrap(), 2);

assert_eq!(
server.request("/r/blockhash").json::<String>().unwrap(),
"70a93647a8d559c7e7ff2df9bd875f5b726a2ff8ca3562003d257df5a4c47ae2"
);

assert_eq!(
server.request("/r/blockhash/0").json::<String>().unwrap(),
"000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"
);

assert!(server.request("/blockhash").json::<String>().is_err());

assert!(server.request("/blockhash/2").json::<String>().is_err());
}

0 comments on commit 8409ce9

Please sign in to comment.