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

Start working on paging through inscriptions #1279

Merged
merged 16 commits into from
Jan 25, 2023
119 changes: 117 additions & 2 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,20 +658,66 @@ impl Index {
)
}

pub(crate) fn get_latest_inscriptions(&self, n: usize) -> Result<Vec<InscriptionId>> {
pub(crate) fn get_homepage_inscriptions(&self) -> Result<Vec<InscriptionId>> {
Ok(
self
.database
.begin_read()?
.open_table(INSCRIPTION_NUMBER_TO_INSCRIPTION_ID)?
.iter()?
.rev()
.take(n)
.take(8)
.map(|(_number, id)| Entry::load(*id.value()))
.collect(),
)
}

pub(crate) fn get_latest_inscriptions_with_prev_and_next(
&self,
n: usize,
from: Option<u64>,
) -> Result<(Vec<InscriptionId>, Option<u64>, Option<u64>)> {
let rtx = self.database.begin_read()?;

let inscription_number_to_inscription_id =
rtx.open_table(INSCRIPTION_NUMBER_TO_INSCRIPTION_ID)?;

let latest = match inscription_number_to_inscription_id.iter()?.rev().next() {
Some((number, _id)) => number.value(),
None => return Ok(Default::default()),
};

let from = from.unwrap_or(latest);

let prev = if let Some(prev) = from.checked_sub(n.try_into()?) {
inscription_number_to_inscription_id
.get(&prev)?
.map(|_| prev)
} else {
None
};

let next = if from < latest {
Some(
from
.checked_add(n.try_into()?)
.unwrap_or(latest)
.min(latest),
)
} else {
None
};

let inscriptions = inscription_number_to_inscription_id
.range(..=from)?
.rev()
.take(n)
.map(|(_number, id)| Entry::load(*id.value()))
.collect();

Ok((inscriptions, prev, next))
}

pub(crate) fn get_feed_inscriptions(&self, n: usize) -> Result<Vec<(u64, InscriptionId)>> {
Ok(
self
Expand Down Expand Up @@ -1934,4 +1980,73 @@ mod tests {
.is_none());
}
}

#[test]
fn get_latest_inscriptions_with_no_prev_and_next() {
for context in Context::configurations() {
context.mine_blocks(1);

let txid = context.rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(1, 0, 0)],
witness: inscription("text/plain", "hello").to_witness(),
..Default::default()
});
let inscription_id = InscriptionId::from(txid);

context.mine_blocks(1);

let (inscriptions, prev, next) = context
.index
.get_latest_inscriptions_with_prev_and_next(100, None)
.unwrap();
assert_eq!(inscriptions, &[inscription_id]);
assert_eq!(prev, None);
assert_eq!(next, None);
}
}

#[test]
fn get_latest_inscriptions_with_prev_and_next() {
for context in Context::configurations() {
context.mine_blocks(1);

let mut ids = Vec::new();

for i in 0..103 {
let txid = context.rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(i + 1, 0, 0)],
witness: inscription("text/plain", "hello").to_witness(),
..Default::default()
});
ids.push(InscriptionId::from(txid));
context.mine_blocks(1);
}

ids.reverse();

let (inscriptions, prev, next) = context
.index
.get_latest_inscriptions_with_prev_and_next(100, None)
.unwrap();
assert_eq!(inscriptions, &ids[..100]);
assert_eq!(prev, Some(2));
assert_eq!(next, None);

let (inscriptions, prev, next) = context
.index
.get_latest_inscriptions_with_prev_and_next(100, Some(101))
.unwrap();
assert_eq!(inscriptions, &ids[1..101]);
assert_eq!(prev, Some(1));
assert_eq!(next, Some(102));

let (inscriptions, prev, next) = context
.index
.get_latest_inscriptions_with_prev_and_next(100, Some(0))
.unwrap();
assert_eq!(inscriptions, &ids[102..103]);
assert_eq!(prev, None);
assert_eq!(next, Some(100));
}
}
}
77 changes: 75 additions & 2 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ impl Server {
.route("/input/:block/:transaction/:input", get(Self::input))
.route("/inscription/:inscription_id", get(Self::inscription))
.route("/inscriptions", get(Self::inscriptions))
.route("/inscriptions/:from", get(Self::inscriptions_from))
.route("/install.sh", get(Self::install_script))
.route("/ordinal/:sat", get(Self::ordinal))
.route("/output/:output", get(Self::output))
Expand Down Expand Up @@ -457,7 +458,7 @@ impl Server {
Extension(index): Extension<Arc<Index>>,
) -> ServerResult<PageHtml<HomeHtml>> {
Ok(
HomeHtml::new(index.blocks(100)?, index.get_latest_inscriptions(8)?)
HomeHtml::new(index.blocks(100)?, index.get_homepage_inscriptions()?)
.page(chain, index.has_sat_index()?),
)
}
Expand Down Expand Up @@ -849,9 +850,28 @@ impl Server {
Extension(chain): Extension<Chain>,
Extension(index): Extension<Arc<Index>>,
) -> ServerResult<PageHtml<InscriptionsHtml>> {
Self::inscriptions_inner(chain, index, None).await
}

async fn inscriptions_from(
Extension(chain): Extension<Chain>,
Extension(index): Extension<Arc<Index>>,
Path(from): Path<u64>,
) -> ServerResult<PageHtml<InscriptionsHtml>> {
Self::inscriptions_inner(chain, index, Some(from)).await
}

async fn inscriptions_inner(
chain: Chain,
index: Arc<Index>,
from: Option<u64>,
) -> ServerResult<PageHtml<InscriptionsHtml>> {
let (inscriptions, prev, next) = index.get_latest_inscriptions_with_prev_and_next(100, from)?;
Ok(
InscriptionsHtml {
inscriptions: index.get_latest_inscriptions(100)?,
inscriptions,
next,
prev,
}
.page(chain, index.has_sat_index()?),
)
Expand Down Expand Up @@ -2280,4 +2300,57 @@ mod tests {
"max-age=31536000, immutable"
);
}

#[test]
fn inscriptions_page_with_no_prev_or_next() {
TestServer::new_with_sat_index().assert_response_regex(
"/inscriptions",
StatusCode::OK,
".*prev\nnext.*",
);
}

#[test]
fn inscriptions_page_with_no_next() {
let server = TestServer::new_with_sat_index();

for i in 0..101 {
server.mine_blocks(1);
server.bitcoin_rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(i + 1, 0, 0)],
witness: inscription("text/foo", "hello").to_witness(),
..Default::default()
});
}

server.mine_blocks(1);

server.assert_response_regex(
"/inscriptions",
StatusCode::OK,
".*<a class=prev href=/inscriptions/0>prev</a>\nnext.*",
);
}

#[test]
fn inscriptions_page_with_no_prev() {
let server = TestServer::new_with_sat_index();

for i in 0..101 {
server.mine_blocks(1);
server.bitcoin_rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(i + 1, 0, 0)],
witness: inscription("text/foo", "hello").to_witness(),
..Default::default()
});
}

server.mine_blocks(1);

server.assert_response_regex(
"/inscriptions/0",
StatusCode::OK,
".*prev\n<a class=next href=/inscriptions/100>next</a>.*",
);
}
}
15 changes: 8 additions & 7 deletions src/templates/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,24 @@ mod tests {

#[test]
fn html() {
pretty_assert_eq!(
BlockHtml::new( Chain::Mainnet.genesis_block() , Height(0), Height(0))
.to_string(),
assert_regex_match!(
BlockHtml::new(Chain::Mainnet.genesis_block(), Height(0), Height(0)),
"
<h1>Block 0</h1>
<dl>
<dt>hash</dt><dd class=monospace>000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f</dd>
<dt>target</dt><dd class=monospace>00000000ffff0000000000000000000000000000000000000000000000000000</dd>
<dt>hash</dt><dd class=monospace>[[:xdigit:]]{64}</dd>
<dt>target</dt><dd class=monospace>[[:xdigit:]]{64}</dd>
<dt>timestamp</dt><dd><time>2009-01-03 18:15:05 UTC</time></dd>
<dt>size</dt><dd>285</dd>
<dt>weight</dt><dd>1140</dd>
</dl>
.*
prev
next
.*
<h2>1 Transaction</h2>
<ul class=monospace>
<li><a href=/tx/4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b>4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b</a></li>
<li><a href=/tx/[[:xdigit:]]{64}>[[:xdigit:]]{64}</a></li>
</ul>
"
.unindent()
Expand All @@ -70,7 +71,7 @@ mod tests {
fn prev_active_when_not_first() {
assert_regex_match!(
BlockHtml::new(Chain::Mainnet.genesis_block(), Height(1), Height(1)),
r"<h1>Block 1</h1>.*<a class=previous href=/block/0>prev</a>\s*next.*",
r"<h1>Block 1</h1>.*<a class=prev href=/block/0>prev</a>\s*next.*",
);
}
}
2 changes: 1 addition & 1 deletion src/templates/inscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ mod tests {
"
<h1>Inscription 1</h1>
<div class=inscription>
<a class=previous href=/inscription/1{64}i1>❮</a>
<a class=prev href=/inscription/1{64}i1>❮</a>
<iframe .* src=/preview/2{64}i2></iframe>
<a class=next href=/inscription/3{64}i3>❯</a>
</div>
Expand Down
33 changes: 32 additions & 1 deletion src/templates/inscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use super::*;
#[derive(Boilerplate)]
pub(crate) struct InscriptionsHtml {
pub(crate) inscriptions: Vec<InscriptionId>,
pub(crate) prev: Option<u64>,
pub(crate) next: Option<u64>,
}

impl PageContent for InscriptionsHtml {
Expand All @@ -16,17 +18,46 @@ mod tests {
use super::*;

#[test]
fn inscriptions() {
fn without_prev_and_next() {
assert_regex_match!(
InscriptionsHtml {
inscriptions: vec![inscription_id(1), inscription_id(2)],
prev: None,
next: None,
},
"
<h1>Inscriptions</h1>
<div class=thumbnails>
<a href=/inscription/1{64}i1><iframe .* src=/preview/1{64}i1></iframe></a>
<a href=/inscription/2{64}i2><iframe .* src=/preview/2{64}i2></iframe></a>
</div>
.*
prev
next
.*
"
.unindent()
);
}

#[test]
fn with_prev_and_next() {
assert_regex_match!(
InscriptionsHtml {
inscriptions: vec![inscription_id(1), inscription_id(2)],
prev: Some(1),
next: Some(2),
},
"
<h1>Inscriptions</h1>
<div class=thumbnails>
<a href=/inscription/1{64}i1><iframe .* src=/preview/1{64}i1></iframe></a>
<a href=/inscription/2{64}i2><iframe .* src=/preview/2{64}i2></iframe></a>
</div>
.*
<a class=prev href=/inscriptions/1>prev</a>
<a class=next href=/inscriptions/2>next</a>
.*
"
.unindent()
);
Expand Down
Loading