Skip to content

Commit

Permalink
add etched runes in block
Browse files Browse the repository at this point in the history
  • Loading branch information
lugondev committed Mar 25, 2024
1 parent 4990542 commit 65a7e82
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct Block {
pub best_height: u32,
pub height: u32,
pub inscriptions: Vec<InscriptionId>,
pub runes: Vec<RuneEntry>,
}

impl Block {
Expand All @@ -23,13 +24,15 @@ impl Block {
height: Height,
best_height: Height,
inscriptions: Vec<InscriptionId>,
runes: Vec<RuneEntry>,
) -> 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
21 changes: 21 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1689,6 +1689,27 @@ impl Index {
.collect::<Result<Vec<InscriptionId>>>()
}

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

let rune_id_to_rune_entry = rtx.open_table(RUNE_ID_TO_RUNE_ENTRY)?;
let id_min = RuneId {
block: block_height,
tx: 0,
};
let id_max = RuneId {
block: block_height + 1,
tx: 0,
};

let runes = rune_id_to_rune_entry
.range(id_min.store()..=id_max.store())?
.map(|result| result.map(|(_number, entry)| RuneEntry::load(entry.value())))
.collect::<Result<Vec<RuneEntry>, StorageError>>()?;

Ok(runes)
}

pub(crate) fn get_highest_paying_inscriptions_in_block(
&self,
block_height: u32,
Expand Down
3 changes: 3 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(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
7 changes: 7 additions & 0 deletions src/templates/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub(crate) struct BlockHtml {
height: Height,
inscription_count: usize,
featured_inscriptions: Vec<InscriptionId>,
runes: Vec<RuneEntry>,
}

impl BlockHtml {
Expand All @@ -18,6 +19,7 @@ impl BlockHtml {
best_height: Height,
inscription_count: usize,
featured_inscriptions: Vec<InscriptionId>,
runes: Vec<RuneEntry>,
) -> 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 entry in &self.runes {
<li><a href=/rune/{{ entry.spaced_rune }}>{{ entry.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 @@ -397,6 +397,7 @@ fn get_block() {
best_height: 1,
height: 0,
inscriptions: Vec::new(),
runes: Vec::new(),
}
);
}
Expand Down

0 comments on commit 65a7e82

Please sign in to comment.