Skip to content

Commit

Permalink
Show rare sat locations on /sat (#1029)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Dec 22, 2022
1 parent 334830a commit 9cb662f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,21 @@ impl Index {
}
}

pub(crate) fn rare_sat_satpoint(&self, sat: Sat) -> Result<Option<SatPoint>> {
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<Option<BlockHeader>> {
self.client.get_block_header(&hash).into_option()
}
Expand Down
16 changes: 16 additions & 0 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,16 @@ impl Server {
Extension(index): Extension<Arc<Index>>,
Path(DeserializeFromStr(sat)): Path<DeserializeFromStr<Sat>>,
) -> ServerResult<PageHtml> {
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}"))
})?,
Expand Down Expand Up @@ -1490,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(
Expand Down
40 changes: 40 additions & 0 deletions src/subcommand/server/templates/sat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use super::*;
#[derive(Boilerplate)]
pub(crate) struct SatHtml {
pub(crate) sat: Sat,
pub(crate) satpoint: Option<SatPoint>,
pub(crate) blocktime: Blocktime,
pub(crate) inscription: Option<(InscriptionId, Inscription)>,
}
Expand All @@ -22,6 +23,7 @@ mod tests {
pretty_assert_eq!(
SatHtml {
sat: Sat(0),
satpoint: None,
blocktime: Blocktime::Confirmed(0),
inscription: None,
}
Expand Down Expand Up @@ -53,6 +55,7 @@ mod tests {
pretty_assert_eq!(
SatHtml {
sat: Sat(1),
satpoint: None,
blocktime: Blocktime::Confirmed(0),
inscription: None,
}
Expand Down Expand Up @@ -84,6 +87,7 @@ mod tests {
pretty_assert_eq!(
SatHtml {
sat: Sat(0),
satpoint: None,
blocktime: Blocktime::Confirmed(0),
inscription: Some((
InscriptionId::from_str(
Expand Down Expand Up @@ -123,6 +127,7 @@ mod tests {
pretty_assert_eq!(
SatHtml {
sat: Sat(0),
satpoint: None,
blocktime: Blocktime::Confirmed(0),
inscription: Some((
InscriptionId::from_str(
Expand Down Expand Up @@ -165,6 +170,7 @@ mod tests {
pretty_assert_eq!(
SatHtml {
sat: Sat::LAST,
satpoint: None,
blocktime: Blocktime::Confirmed(0),
inscription: None,
}
Expand All @@ -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(),
"
<h1>Sat 0</h1>
<dl>
<dt>decimal</dt><dd>0.0</dd>
<dt>degree</dt><dd>0°0′0″0‴</dd>
<dt>percentile</dt><dd>0%</dd>
<dt>name</dt><dd>nvtdijuwxlp</dd>
<dt>cycle</dt><dd>0</dd>
<dt>epoch</dt><dd>0</dd>
<dt>period</dt><dd>0</dd>
<dt>block</dt><dd>0</dd>
<dt>offset</dt><dd>0</dd>
<dt>rarity</dt><dd><span class=mythic>mythic</span></dd>
<dt>time</dt><dd>1970-01-01 00:00:00</dd>
<dt>location</dt>
<dd class=monospace>1111111111111111111111111111111111111111111111111111111111111111:1:0</dd>
</dl>
prev
<a href=/sat/1>next</a>
"
.unindent()
);
}
}
4 changes: 4 additions & 0 deletions templates/sat.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ <h1>Sat {{ self.sat.n() }}</h1>
<dt>inscription</dt>
<dd><a href=/inscription/{{ inscription_id }}>{{ inscription.content_html(*inscription_id) }}</a></dd>
%% }
%% if let Some(satpoint) = self.satpoint {
<dt>location</dt>
<dd class=monospace>{{ satpoint }}</dd>
%% }
</dl>
%% if self.sat.n() > 0 {
<a href=/sat/{{self.sat.n() - 1}}>prev</a>
Expand Down

0 comments on commit 9cb662f

Please sign in to comment.