Skip to content

Commit

Permalink
List blocks on root page (ordinals#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
terror committed Aug 5, 2022
1 parent c7da679 commit 3ae1cac
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,22 @@ impl Index {
)
}

pub(crate) fn all(&self) -> Result<Vec<sha256d::Hash>> {
let mut blocks = Vec::new();

let tx = self.database.begin_read()?;

let height_to_hash = tx.open_table(HEIGHT_TO_HASH)?;

let mut cursor = height_to_hash.range(0..)?;

while let Some(next) = cursor.next() {
blocks.push(sha256d::Hash::from_slice(next.1)?);
}

Ok(blocks)
}

fn index_transaction(
&self,
txid: Txid,
Expand Down
9 changes: 5 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use {
options::Options, ordinal::Ordinal, sat_point::SatPoint, subcommand::Subcommand,
},
anyhow::{anyhow, bail, Context, Error},
axum::{extract, http::StatusCode, response::IntoResponse, routing::get, Json, Router},
axum::{
extract, http::StatusCode, response::Html, response::IntoResponse, routing::get, Json, Router,
},
axum_server::Handle,
bdk::{
database::SqliteDatabase,
Expand All @@ -17,9 +19,8 @@ use {
},
bitcoin::{
blockdata::constants::COIN_VALUE,
consensus::Decodable,
consensus::Encodable,
hashes::{sha256, Hash, HashEngine},
consensus::{Decodable, Encodable},
hashes::{sha256, sha256d, Hash, HashEngine},
secp256k1::{
self,
rand::{self, thread_rng},
Expand Down
29 changes: 29 additions & 0 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ impl Server {
});

let app = Router::new()
.route("/", get(Self::root))
.route("/list/:outpoint", get(Self::list))
.route("/status", get(Self::status))
.layer(extract::Extension(index))
Expand Down Expand Up @@ -124,6 +125,34 @@ impl Server {
})
}

async fn root(index: extract::Extension<Arc<Index>>) -> impl IntoResponse {
match index.all() {
Ok(blocks) => (
StatusCode::OK,
Html(format!(
"<ul>\n{}</ul>",
blocks
.iter()
.enumerate()
.map(|(height, hash)| format!(" <li>{height} - {hash}</li>\n"))
.collect::<String>(),
)),
),
Err(error) => {
eprintln!("Error serving request for root: {error}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Html(
StatusCode::INTERNAL_SERVER_ERROR
.canonical_reason()
.unwrap_or_default()
.to_string(),
),
)
}
}
}

async fn list(
extract::Path(outpoint): extract::Path<OutPoint>,
index: extract::Extension<Arc<Index>>,
Expand Down
2 changes: 1 addition & 1 deletion tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ impl Test {
.get(&format!("https://127.0.0.1:{port}/{request}"))
.send()?;
assert_eq!(response.status().as_u16(), *status);
assert_eq!(response.text()?, *expected_response);
assert_eq!(response.text()?, *expected_response.unindent().trim_end());
successful_requests += 1;
}
}
Expand Down
22 changes: 22 additions & 0 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,28 @@ fn continuously_index_ranges() -> Result {
.run_server(port)
}

#[test]
fn root() -> Result {
let port = free_port()?;

Test::new()?
.command(&format!("server --address 127.0.0.1 --http-port {port}"))
.request("/", 200, "<ul>\n</ul>")
.block()
.block()
.request(
"/",
200,
"
<ul>
<li>0 - 14508459b221041eab257d2baaa7459775ba748246c8403609eb708f0e57e74b</li>
<li>1 - 467a86f0642b1d284376d13a98ef58310caa49502b0f9a560ee222e0a122fe16</li>
</ul>
",
)
.run_server(port)
}

#[test]
fn http_or_https_port_is_required() -> Result {
Test::new()?
Expand Down

0 comments on commit 3ae1cac

Please sign in to comment.