From 9479dbc7789c2325de7f4e4cf5ae79ccf6a4cbb6 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Tue, 20 Dec 2022 16:37:48 -0800 Subject: [PATCH 1/2] Show location of rare sats on /sat --- src/index.rs | 15 ++++++++++ src/subcommand/server.rs | 7 +++++ src/subcommand/server/templates/sat.rs | 40 ++++++++++++++++++++++++++ templates/sat.html | 4 +++ 4 files changed, 66 insertions(+) diff --git a/src/index.rs b/src/index.rs index d9b608bec3..c72c504073 100644 --- a/src/index.rs +++ b/src/index.rs @@ -408,6 +408,21 @@ impl Index { } } + pub(crate) fn rare_sat_satpoint(&self, sat: Sat) -> Result> { + if self.has_satoshi_index()? { + Ok( + self + .database + .begin_read()? + .open_table(SAT_TO_SATPOINT)? + .get(&sat.n())? + .map(|satpoint| decode_satpoint(*satpoint)), + ) + } else { + Ok(None) + } + } + pub(crate) fn block_header(&self, hash: BlockHash) -> Result> { self.client.get_block_header(&hash).into_option() } diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 4633c65491..3ad7e431c6 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -322,9 +322,16 @@ impl Server { Extension(index): Extension>, Path(DeserializeFromStr(sat)): Path>, ) -> ServerResult { + let satpoint = index.rare_sat_satpoint(sat).map_err(|err| { + ServerError::Internal(anyhow!( + "failed to satpoint for sat {sat} from index: {err}" + )) + })?; + Ok( SatHtml { sat, + satpoint, blocktime: index.blocktime(sat.height()).map_err(|err| { ServerError::Internal(anyhow!("failed to retrieve blocktime from index: {err}")) })?, diff --git a/src/subcommand/server/templates/sat.rs b/src/subcommand/server/templates/sat.rs index bfd586f434..5eaa72c50e 100644 --- a/src/subcommand/server/templates/sat.rs +++ b/src/subcommand/server/templates/sat.rs @@ -3,6 +3,7 @@ use super::*; #[derive(Boilerplate)] pub(crate) struct SatHtml { pub(crate) sat: Sat, + pub(crate) satpoint: Option, pub(crate) blocktime: Blocktime, pub(crate) inscription: Option<(InscriptionId, Inscription)>, } @@ -22,6 +23,7 @@ mod tests { pretty_assert_eq!( SatHtml { sat: Sat(0), + satpoint: None, blocktime: Blocktime::Confirmed(0), inscription: None, } @@ -53,6 +55,7 @@ mod tests { pretty_assert_eq!( SatHtml { sat: Sat(1), + satpoint: None, blocktime: Blocktime::Confirmed(0), inscription: None, } @@ -84,6 +87,7 @@ mod tests { pretty_assert_eq!( SatHtml { sat: Sat(0), + satpoint: None, blocktime: Blocktime::Confirmed(0), inscription: Some(( InscriptionId::from_str( @@ -123,6 +127,7 @@ mod tests { pretty_assert_eq!( SatHtml { sat: Sat(0), + satpoint: None, blocktime: Blocktime::Confirmed(0), inscription: Some(( InscriptionId::from_str( @@ -165,6 +170,7 @@ mod tests { pretty_assert_eq!( SatHtml { sat: Sat::LAST, + satpoint: None, blocktime: Blocktime::Confirmed(0), inscription: None, } @@ -190,4 +196,38 @@ mod tests { .unindent() ); } + + #[test] + fn sat_with_satpoint() { + pretty_assert_eq!( + SatHtml { + sat: Sat(0), + satpoint: Some(satpoint(1, 0)), + blocktime: Blocktime::Confirmed(0), + inscription: None, + } + .to_string(), + " +

Sat 0

+
+
decimal
0.0
+
degree
0°0′0″0‴
+
percentile
0%
+
name
nvtdijuwxlp
+
cycle
0
+
epoch
0
+
period
0
+
block
0
+
offset
0
+
rarity
mythic
+
time
1970-01-01 00:00:00
+
location
+
1111111111111111111111111111111111111111111111111111111111111111:1:0
+
+ prev + next + " + .unindent() + ); + } } diff --git a/templates/sat.html b/templates/sat.html index 11f1293cb0..9cc453b28f 100644 --- a/templates/sat.html +++ b/templates/sat.html @@ -15,6 +15,10 @@

Sat {{ self.sat.n() }}

inscription
{{ inscription.content_html(*inscription_id) }}
%% } +%% if let Some(satpoint) = self.satpoint { +
location
+
{{ satpoint }}
+%% } %% if self.sat.n() > 0 { prev From 918ddfb2c4aa8c91b560fb93048a30cf0a58b3ad Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Tue, 20 Dec 2022 16:42:19 -0800 Subject: [PATCH 2/2] Test that location shows up on /sat --- src/subcommand/server.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 3ad7e431c6..2f02088013 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -1497,6 +1497,15 @@ next.*", ); } + #[test] + fn rare_sat_location() { + TestServer::new_with_args(&["--index-sats"]).assert_response_regex( + "/sat/0", + StatusCode::OK, + ".*>4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b:0:0<.*", + ); + } + #[test] fn dont_show_rare_txt_in_header_without_satoshi_index() { TestServer::new().assert_response_regex(