Skip to content

Commit

Permalink
Display genesis height on inscription page (ordinals#1026)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph authored Dec 20, 2022
1 parent c2aab88 commit 11d6b3b
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type SatPointArray = [u8; 44];

const HEIGHT_TO_BLOCK_HASH: TableDefinition<u64, &BlockHashArray> =
TableDefinition::new("HEIGHT_TO_BLOCK_HASH");
const INSCRIPTION_ID_TO_HEIGHT: TableDefinition<&InscriptionIdArray, u64> =
TableDefinition::new("INSCRIPTION_ID_TO_HEIGHT");
const INSCRIPTION_ID_TO_SATPOINT: TableDefinition<&InscriptionIdArray, &SatPointArray> =
TableDefinition::new("INSCRIPTION_ID_TO_SATPOINT");
const INSCRIPTION_NUMBER_TO_INSCRIPTION_ID: TableDefinition<u64, &InscriptionIdArray> =
Expand Down Expand Up @@ -205,6 +207,7 @@ impl Index {
};

tx.open_table(HEIGHT_TO_BLOCK_HASH)?;
tx.open_table(INSCRIPTION_ID_TO_HEIGHT)?;
tx.open_table(INSCRIPTION_ID_TO_SATPOINT)?;
tx.open_table(INSCRIPTION_NUMBER_TO_INSCRIPTION_ID)?;
tx.open_table(SATPOINT_TO_INSCRIPTION_ID)?;
Expand Down Expand Up @@ -642,6 +645,15 @@ impl Index {

Ok(inscriptions)
}

pub(crate) fn get_genesis_height(&self, inscription_id: InscriptionId) -> Result<u64> {
self
.database
.begin_read()?
.open_table(INSCRIPTION_ID_TO_HEIGHT)?
.get(inscription_id.as_inner())?
.ok_or_else(|| anyhow!("no height for inscription"))
}
}

#[cfg(test)]
Expand Down
3 changes: 3 additions & 0 deletions src/index/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ impl Updater {
}
}

let mut inscription_id_to_height = wtx.open_table(INSCRIPTION_ID_TO_HEIGHT)?;
let mut inscription_id_to_satpoint = wtx.open_table(INSCRIPTION_ID_TO_SATPOINT)?;
let mut satpoint_to_inscription_id = wtx.open_table(SATPOINT_TO_INSCRIPTION_ID)?;
let mut inscription_number_to_inscription_id =
Expand All @@ -282,6 +283,8 @@ impl Updater {
.unwrap_or(0);

let mut inscription_updater = InscriptionUpdater {
height: self.height,
id_to_height: &mut inscription_id_to_height,
id_to_satpoint: &mut inscription_id_to_satpoint,
next_number: &mut next_inscription_number,
number_to_id: &mut inscription_number_to_inscription_id,
Expand Down
3 changes: 3 additions & 0 deletions src/index/updater/inscription_updater.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use super::*;

pub(super) struct InscriptionUpdater<'a, 'db, 'tx> {
pub(super) height: u64,
pub(super) id_to_height: &'a mut Table<'db, 'tx, &'tx InscriptionIdArray, u64>,
pub(super) id_to_satpoint: &'a mut Table<'db, 'tx, &'tx InscriptionIdArray, &'tx SatPointArray>,
pub(super) next_number: &'a mut u64,
pub(super) number_to_id: &'a mut Table<'db, 'tx, u64, &'tx InscriptionIdArray>,
Expand All @@ -23,6 +25,7 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {

let inscription_id = txid.as_inner();

self.id_to_height.insert(inscription_id, &self.height)?;
self.id_to_satpoint.insert(inscription_id, &satpoint)?;
self.satpoint_to_id.insert(&satpoint, inscription_id)?;
self.number_to_id.insert(self.next_number, inscription_id)?;
Expand Down
7 changes: 7 additions & 0 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,8 +697,15 @@ impl Server {
ServerError::NotFound(format!("transaction {inscription_id} has no inscription"))
})?;

let genesis_height = index.get_genesis_height(inscription_id).map_err(|err| {
ServerError::Internal(anyhow!(
"failed to retrieve height for inscriptiom with inscription id {inscription_id} from index: {err}"
))
})?;

Ok(
InscriptionHtml {
genesis_height,
inscription_id,
inscription,
satpoint,
Expand Down
10 changes: 10 additions & 0 deletions src/subcommand/server/templates/inscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::*;

#[derive(Boilerplate)]
pub(crate) struct InscriptionHtml {
pub(crate) genesis_height: u64,
pub(crate) inscription_id: InscriptionId,
pub(crate) inscription: Inscription,
pub(crate) satpoint: SatPoint,
Expand All @@ -25,6 +26,7 @@ mod tests {
fn txt_inscription() {
pretty_assert_eq!(
InscriptionHtml {
genesis_height: 0,
inscription_id: InscriptionId::from_str(
"ec90757eb3b164aa43fc548faa2fa0c52025494f2c15d5ddf11260b4034ac6dc"
)
Expand All @@ -41,6 +43,8 @@ mod tests {
<dd>10 bytes</dd>
<dt>content type</dt>
<dd>text/plain;charset=utf-8</dd>
<dt>genesis height</dt>
<dd>0</dd>
<dt>location</dt>
<dd>1111111111111111111111111111111111111111111111111111111111111111:1:0</dd>
</dl>
Expand All @@ -53,6 +57,7 @@ mod tests {
fn png_inscription() {
pretty_assert_eq!(
InscriptionHtml {
genesis_height: 0,
inscription_id: InscriptionId::from_str(
"ec90757eb3b164aa43fc548faa2fa0c52025494f2c15d5ddf11260b4034ac6dc"
)
Expand All @@ -69,6 +74,8 @@ mod tests {
<dd>100 bytes</dd>
<dt>content type</dt>
<dd>image/png</dd>
<dt>genesis height</dt>
<dd>0</dd>
<dt>location</dt>
<dd>1111111111111111111111111111111111111111111111111111111111111111:1:0</dd>
</dl>
Expand All @@ -81,6 +88,7 @@ mod tests {
fn empty_inscription() {
pretty_assert_eq!(
InscriptionHtml {
genesis_height: 0,
inscription_id: InscriptionId::from_str(
"ec90757eb3b164aa43fc548faa2fa0c52025494f2c15d5ddf11260b4034ac6dc"
)
Expand All @@ -93,6 +101,8 @@ mod tests {
<h1>Inscription ec90757eb3b164aa43fc548faa2fa0c52025494f2c15d5ddf11260b4034ac6dc</h1>
<p>UNKNOWN</p>
<dl>
<dt>genesis height</dt>
<dd>0</dd>
<dt>location</dt>
<dd>1111111111111111111111111111111111111111111111111111111111111111:1:0</dd>
</dl>
Expand Down
2 changes: 2 additions & 0 deletions templates/inscription.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ <h1>Inscription {{ self.inscription_id }}</h1>
<dt>content type</dt>
<dd>{{ content_type }}</dd>
%% }
<dt>genesis height</dt>
<dd>{{ self.genesis_height }}</dd>
<dt>location</dt>
<dd>{{ self.satpoint }}</dd>
</dl>
6 changes: 6 additions & 0 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ fn inscription_page() {
<dd>10 bytes</dd>
<dt>content type</dt>
<dd>text/plain;charset=utf-8</dd>
<dt>genesis height</dt>
<dd>2</dd>
<dt>location</dt>
<dd>{reveal_tx}:0:0</dd>
</dl>.*",
Expand Down Expand Up @@ -126,6 +128,8 @@ fn inscription_page_after_send() {
<dd>10 bytes</dd>
<dt>content type</dt>
<dd>text/plain;charset=utf-8</dd>
<dt>genesis height</dt>
<dd>2</dd>
<dt>location</dt>
<dd>{reveal_txid}:0:0</dd>
</dl>.*",
Expand Down Expand Up @@ -153,6 +157,8 @@ fn inscription_page_after_send() {
<dd>10 bytes</dd>
<dt>content type</dt>
<dd>text/plain;charset=utf-8</dd>
<dt>genesis height</dt>
<dd>2</dd>
<dt>location</dt>
<dd>{}:0:0</dd>
</dl>.*",
Expand Down
4 changes: 4 additions & 0 deletions tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ fn send_works_on_signet() {
<dd>520 bytes</dd>
<dt>content type</dt>
<dd>image/png</dd>
<dt>genesis height</dt>
<dd>2</dd>
<dt>location</dt>
<dd>{send_txid}:0:0</dd>
</dl>
Expand Down Expand Up @@ -158,6 +160,8 @@ fn send_inscribed_sat() {
<dd>520 bytes</dd>
<dt>content type</dt>
<dd>image/png</dd>
<dt>genesis height</dt>
<dd>2</dd>
<dt>location</dt>
<dd>{send_txid}:0:0</dd>
</dl>
Expand Down

0 comments on commit 11d6b3b

Please sign in to comment.