Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweaks to front-end #2381

Merged
merged 6 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/index/block_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,23 +218,24 @@ impl BlockIndex {
index: &Index,
block_height: u64,
n: usize,
) -> Result<Vec<InscriptionId>> {
) -> Result<(Vec<InscriptionId>, usize)> {
let inscription_ids = self.get_inscriptions_in_block(index, block_height)?;

let mut inscription_to_fee: Vec<(InscriptionId, u64)> = Vec::new();
for id in inscription_ids {
inscription_to_fee.push((id, index.get_inscription_entry(id)?.unwrap().fee));
for id in &inscription_ids {
inscription_to_fee.push((*id, index.get_inscription_entry(*id)?.unwrap().fee));
}

inscription_to_fee.sort_by_key(|(_, fee)| *fee);

Ok(
Ok((
inscription_to_fee
.iter()
.map(|(id, _)| *id)
.rev()
.take(n)
.collect(),
)
inscription_ids.len(),
))
}
}
44 changes: 29 additions & 15 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,10 +565,10 @@ impl Server {
let blocks = index.blocks(100)?;
let mut featured_blocks = BTreeMap::new();
for (height, hash) in blocks.iter().take(5) {
let inscriptions = block_index_state
let (inscriptions, _total_num) = block_index_state
.block_index
.read()
.unwrap()
.map_err(|err| anyhow!("block index RwLock poisoned: {}", err))?
.get_highest_paying_inscriptions_in_block(&index, *height, 8)?;
featured_blocks.insert(*hash, inscriptions);
}
Expand Down Expand Up @@ -607,15 +607,19 @@ impl Server {
}
};

let inscriptions =
index.get_inscriptions_in_block(&block_index_state.block_index.read().unwrap(), height)?;
let (featured_inscriptions, total_num) = block_index_state
.block_index
.read()
.map_err(|err| anyhow!("block index RwLock poisoned: {}", err))?
.get_highest_paying_inscriptions_in_block(&index, height, 8)?;

Ok(
BlockHtml::new(
block,
Height(height),
Self::index_height(&index)?,
inscriptions,
total_num,
featured_inscriptions,
)
.page(page_config, index.has_sat_index()?),
)
Expand Down Expand Up @@ -1086,22 +1090,32 @@ impl Server {
Path((block_height, page_index)): Path<(u64, usize)>,
accept_json: AcceptJson,
) -> ServerResult<Response> {
let block_index = block_index_state
.block_index
.read()
.map_err(|err| anyhow!("block index RwLock poisoned: {}", err))?;

let inscriptions = index
.get_inscriptions_in_block(&block_index_state.block_index.read().unwrap(), block_height)
.get_inscriptions_in_block(&block_index, block_height)
.map_err(|e| ServerError::NotFound(format!("Failed to get inscriptions in block: {}", e)))?;

Ok(if accept_json.0 {
Json(InscriptionsJson::new(inscriptions, None, None, None, None)).into_response()
} else {
InscriptionsBlockHtml::new(block_height, index.block_count()?, inscriptions, page_index)
.map_err(|e| {
ServerError::NotFound(format!(
"Failed to get inscriptions in inscriptions block page: {}",
e
))
})?
.page(page_config, index.has_sat_index()?)
.into_response()
InscriptionsBlockHtml::new(
block_height,
index.block_height()?.unwrap_or(Height(0)).n(),
inscriptions,
page_index,
)
.map_err(|e| {
ServerError::NotFound(format!(
"Failed to get inscriptions in inscriptions block page: {}",
e
))
})?
.page(page_config, index.has_sat_index()?)
.into_response()
})
}

Expand Down
28 changes: 13 additions & 15 deletions src/templates/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,26 @@ pub(crate) struct BlockHtml {
best_height: Height,
block: Block,
height: Height,
num_inscriptions: usize,
txid_to_inscription_ids: BTreeMap<Txid, Vec<InscriptionId>>,
total_num_inscriptions: usize,
featured_inscriptions: Vec<InscriptionId>,
}

impl BlockHtml {
pub(crate) fn new(
block: Block,
height: Height,
best_height: Height,
inscriptions: Vec<InscriptionId>,
total_num_inscriptions: usize,
featured_inscriptions: Vec<InscriptionId>,
) -> Self {
Self {
hash: block.header.block_hash(),
target: BlockHash::from_raw_hash(Hash::from_byte_array(block.header.target().to_be_bytes())),
block,
height,
best_height,
num_inscriptions: inscriptions.len(),
txid_to_inscription_ids: inscriptions.into_iter().fold(
BTreeMap::<Txid, Vec<InscriptionId>>::new(),
|mut acc, inscription_id| {
acc
.entry(inscription_id.txid)
.and_modify(|inscription_ids| inscription_ids.push(inscription_id))
.or_insert(vec![inscription_id]);
acc
},
),
total_num_inscriptions,
featured_inscriptions,
}
}
}
Expand All @@ -56,6 +48,7 @@ mod tests {
Chain::Mainnet.genesis_block(),
Height(0),
Height(0),
0,
Vec::new()
),
"
Expand All @@ -71,7 +64,10 @@ mod tests {
prev
next
.*
<h2>1 Transaction and 0 Inscriptions</h2>
<h2>0 Inscriptions</h2>
<div class=thumbnails>
</div>
<h2>1 Transaction</h2>
<ul class=monospace>
<li><a href=/tx/[[:xdigit:]]{64}>[[:xdigit:]]{64}</a></li>
</ul>
Expand All @@ -87,6 +83,7 @@ mod tests {
Chain::Mainnet.genesis_block(),
Height(0),
Height(1),
0,
Vec::new()
),
r"<h1>Block 0</h1>.*prev\s*<a class=next href=/block/1>next</a>.*"
Expand All @@ -100,6 +97,7 @@ mod tests {
Chain::Mainnet.genesis_block(),
Height(1),
Height(1),
0,
Vec::new()
),
r"<h1>Block 1</h1>.*<a class=prev href=/block/0>prev</a>\s*next.*",
Expand Down
8 changes: 4 additions & 4 deletions src/templates/inscriptions_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ impl InscriptionsBlockHtml {
Ok(Self {
block,
inscriptions,
prev_block: if block == 0 { None } else { Some(block - 1) },
next_block: if block >= current_blockheight {
None
} else {
prev_block: block.checked_sub(1),
next_block: if current_blockheight > block {
Some(block + 1)
} else {
None
},
prev_page: if page_index > 0 {
Some(page_index - 1)
Expand Down
2 changes: 1 addition & 1 deletion static/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ ol, ul {

.block {
background-color: var(--light-bg);
box-shadow: rgba(0, 0, 0, 0.16) 0px 1px 4px, var(--light-bg) 0px 0px 0px 3px;
border-radius: 0.5%;
margin: 3%;
padding: 0.5%;
text-align: center;
Expand Down
20 changes: 12 additions & 8 deletions templates/block.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,21 @@ <h1>Block {{ self.height }}</h1>
next
%% }
</div>
<h2>{{"Transaction".tally(self.block.txdata.len())}} and {{"Inscription".tally(self.num_inscriptions)}}</h2>
<h2>{{"Inscription".tally(self.total_num_inscriptions)}}</h2>
<div class=thumbnails>
%% for id in &self.featured_inscriptions {
{{ Iframe::thumbnail(*id) }}
%% }
</div>
%% if &self.total_num_inscriptions > &self.featured_inscriptions.len() {
<div class=center>
<a href="/inscriptions/block/{{ &self.height }}">more</a>
</div>
%% }
<h2>{{"Transaction".tally(self.block.txdata.len())}}</h2>
<ul class=monospace>
%% for tx in &self.block.txdata {
%% let txid = tx.txid();
<li><a href=/tx/{{txid}}>{{txid}}</a></li>
%% if let Some(inscription_ids) = &self.txid_to_inscription_ids.get(&txid) {
<ul>
%% for inscription_id in *inscription_ids {
<li><a href=/inscription/{{inscription_id}}>{{inscription_id}}</a></li>
%% }
</ul>
%% }
%% }
</ul>
4 changes: 2 additions & 2 deletions templates/inscriptions-block.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ <h1 class=light-fg>Inscriptions in <a href=/block/{{ &self.block }}>Block {{ &se
<div class=center>
%% if let Some(prev_block) = &self.prev_block {
<a class=prev href=/inscriptions/block/{{ prev_block }}>{{ prev_block }}</a>
%% }
&bull;
%% }
%% if let Some(prev_page) = &self.prev_page {
<a class=prev href=/inscriptions/block/{{ &self.block }}/{{ prev_page }}>prev</a>
%% } else {
Expand All @@ -19,8 +19,8 @@ <h1 class=light-fg>Inscriptions in <a href=/block/{{ &self.block }}>Block {{ &se
%% } else {
next
%% }
&bull;
%% if let Some(next_block) = &self.next_block {
&bull;
<a class=next href=/inscriptions/block/{{ next_block }}>{{ next_block }}</a>
%% }
</div>