Skip to content

Commit

Permalink
Display etched runes on /block (#3366)
Browse files Browse the repository at this point in the history
  • Loading branch information
lugondev committed Apr 15, 2024
1 parent c9f793c commit 22d489b
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ pub use crate::templates::{

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Block {
pub hash: BlockHash,
pub target: BlockHash,
pub best_height: u32,
pub hash: BlockHash,
pub height: u32,
pub inscriptions: Vec<InscriptionId>,
pub runes: Vec<SpacedRune>,
pub target: BlockHash,
}

impl Block {
Expand All @@ -23,13 +24,15 @@ impl Block {
height: Height,
best_height: Height,
inscriptions: Vec<InscriptionId>,
runes: Vec<SpacedRune>,
) -> Self {
Self {
hash: block.header.block_hash(),
target: target_as_block_hash(block.header.target()),
height: height.0,
best_height: best_height.0,
inscriptions,
runes,
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1771,6 +1771,29 @@ impl Index {
.collect::<Result<Vec<InscriptionId>>>()
}

pub(crate) fn get_runes_in_block(&self, block_height: u64) -> Result<Vec<SpacedRune>> {
let rtx = self.database.begin_read()?;

let rune_id_to_rune_entry = rtx.open_table(RUNE_ID_TO_RUNE_ENTRY)?;

let min_id = RuneId {
block: block_height,
tx: 0,
};

let max_id = RuneId {
block: block_height + 1,
tx: 0,
};

let runes = rune_id_to_rune_entry
.range(min_id.store()..max_id.store())?
.map(|result| result.map(|(_, entry)| RuneEntry::load(entry.value()).spaced_rune))
.collect::<Result<Vec<SpacedRune>, StorageError>>()?;

Ok(runes)
}

pub(crate) fn get_highest_paying_inscriptions_in_block(
&self,
block_height: u32,
Expand Down
48 changes: 48 additions & 0 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,13 +844,15 @@ impl Server {
}
};

let runes = index.get_runes_in_block(u64::from(height))?;
Ok(if accept_json {
let inscriptions = index.get_inscriptions_in_block(height)?;
Json(api::Block::new(
block,
Height(height),
Self::index_height(&index)?,
inscriptions,
runes,
))
.into_response()
} else {
Expand All @@ -862,6 +864,7 @@ impl Server {
Self::index_height(&index)?,
total_num,
featured_inscriptions,
runes,
)
.page(server_config)
.into_response()
Expand Down Expand Up @@ -2893,6 +2896,51 @@ mod tests {
);
}

#[test]
fn etched_runes_are_displayed_on_block_page() {
let server = TestServer::builder()
.chain(Chain::Regtest)
.index_runes()
.build();

server.mine_blocks(1);

let rune0 = Rune(RUNE);

let (_txid, id) = server.etch(
Runestone {
edicts: vec![Edict {
id: RuneId::default(),
amount: u128::MAX,
output: 0,
}],
etching: Some(Etching {
rune: Some(rune0),
..default()
}),
..default()
},
1,
None,
);

assert_eq!(
server.index.get_runes_in_block(id.block - 1).unwrap().len(),
0
);
assert_eq!(server.index.get_runes_in_block(id.block).unwrap().len(), 1);
assert_eq!(
server.index.get_runes_in_block(id.block + 1).unwrap().len(),
0
);

server.assert_response_regex(
format!("/block/{}", id.block),
StatusCode::OK,
format!(".*<h2>1 Rune</h2>.*<li><a href=/rune/{rune0}>{rune0}</a></li>.*"),
);
}

#[test]
fn runes_are_spaced() {
let server = TestServer::builder()
Expand Down
13 changes: 10 additions & 3 deletions src/templates/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ use super::*;

#[derive(Boilerplate)]
pub(crate) struct BlockHtml {
hash: BlockHash,
target: BlockHash,
best_height: Height,
block: Block,
featured_inscriptions: Vec<InscriptionId>,
hash: BlockHash,
height: Height,
inscription_count: usize,
featured_inscriptions: Vec<InscriptionId>,
runes: Vec<SpacedRune>,
target: BlockHash,
}

impl BlockHtml {
Expand All @@ -18,6 +19,7 @@ impl BlockHtml {
best_height: Height,
inscription_count: usize,
featured_inscriptions: Vec<InscriptionId>,
runes: Vec<SpacedRune>,
) -> Self {
Self {
hash: block.header.block_hash(),
Expand All @@ -27,6 +29,7 @@ impl BlockHtml {
best_height,
inscription_count,
featured_inscriptions,
runes,
}
}
}
Expand All @@ -49,6 +52,7 @@ mod tests {
Height(0),
Height(0),
0,
Vec::new(),
Vec::new()
),
"
Expand All @@ -64,6 +68,7 @@ mod tests {
prev
next
.*
<h2>0 Runes</h2>
<h2>0 Inscriptions</h2>
<div class=thumbnails>
</div>
Expand All @@ -84,6 +89,7 @@ mod tests {
Height(0),
Height(1),
0,
Vec::new(),
Vec::new()
),
r"<h1>Block 0</h1>.*prev\s*<a class=next href=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/block/1>next</a>.*"
Expand All @@ -98,6 +104,7 @@ mod tests {
Height(1),
Height(1),
0,
Vec::new(),
Vec::new()
),
r"<h1>Block 1</h1>.*<a class=prev href=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/block/0>prev</a>\s*next.*",
Expand Down
8 changes: 8 additions & 0 deletions templates/block.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ <h1>Block {{ self.height }}</h1>
next
%% }
</div>
<h2>{{"Rune".tally(self.runes.len())}}</h2>
%% if self.runes.len() > 0 {
<ul>
%% for spaced_rune in &self.runes {
<li><a href=/rune/{{ spaced_rune }}>{{ spaced_rune }}</a></li>
%% }
</ul>
%% }
<h2>{{"Inscription".tally(self.inscription_count)}}</h2>
<div class=thumbnails>
%% for id in &self.featured_inscriptions {
Expand Down
1 change: 1 addition & 0 deletions tests/json_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ fn get_block() {
best_height: 1,
height: 0,
inscriptions: Vec::new(),
runes: Vec::new(),
}
);
}
Expand Down

0 comments on commit 22d489b

Please sign in to comment.