Skip to content

Commit

Permalink
Display graphical inscriptions on homepage (ordinals#1008)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Dec 19, 2022
1 parent 69a5fd5 commit a3b9daa
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 14 deletions.
34 changes: 34 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,40 @@ impl Index {
.collect(),
)
}

pub(crate) fn get_graphical_inscriptions(
&self,
n: Option<usize>,
) -> Result<Vec<(Inscription, InscriptionId)>> {
let mut inscriptions = Vec::new();

for (_satpoint, id) in self
.database
.begin_read()?
.open_table(SATPOINT_TO_INSCRIPTION_ID)?
.iter()?
{
let id = decode_inscription_id(*id);

let Some((inscription, _satpoint)) = self.get_inscription_by_inscription_id(id)? else {
continue;
};

if !inscription.is_graphical() {
continue;
}

inscriptions.push((inscription, id));

if let Some(n) = n {
if inscriptions.len() == n {
break;
}
}
}

Ok(inscriptions)
}
}

#[cfg(test)]
Expand Down
11 changes: 11 additions & 0 deletions src/inscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ impl Inscription {
pub(crate) fn content_type(&self) -> Option<&str> {
str::from_utf8(self.content_type.as_ref()?).ok()
}

pub(crate) fn is_graphical(&self) -> bool {
matches!(self.content_type(), Some("image/png"))
}
}

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -704,4 +708,11 @@ mod tests {
}
);
}

#[test]
fn is_graphical() {
assert!(inscription("image/png", []).is_graphical());
assert!(!inscription("foo", []).is_graphical());
assert!(!Inscription::new(None, Some(Vec::new())).is_graphical());
}
}
16 changes: 2 additions & 14 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,20 +415,8 @@ impl Server {
.blocks(100)
.map_err(|err| ServerError::Internal(anyhow!("error getting blocks: {err}")))?,
index
.get_inscriptions(Some(8))
.map_err(|err| ServerError::Internal(anyhow!("error getting inscriptions: {err}")))?
.values()
.map(|inscription_id| {
Ok((
index
.get_inscription_by_inscription_id(*inscription_id)
.map_err(|err| ServerError::Internal(anyhow!("error getting inscriptions: {err}")))?
.unwrap()
.0,
*inscription_id,
))
})
.collect::<ServerResult<Vec<(Inscription, InscriptionId)>>>()?,
.get_graphical_inscriptions(Some(8))
.map_err(|err| ServerError::Internal(anyhow!("error getting inscriptions: {err}")))?,
)
.page(
chain,
Expand Down
18 changes: 18 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ fn reveal_txid_from_inscribe_stdout(stdout: &str) -> Txid {
.unwrap()
}

fn create_inscription(rpc_server: &test_bitcoincore_rpc::Handle, filename: &str) -> Txid {
let txid = rpc_server.mine_blocks(1)[0].txdata[0].txid();

let stdout = CommandBuilder::new(format!(
"--chain regtest wallet inscribe --satpoint {txid}:0:0 --file {filename}"
))
.write(filename, "HELLOWORLD")
.rpc_server(rpc_server)
.stdout_regex("commit\t[[:xdigit:]]{64}\nreveal\t[[:xdigit:]]{64}\n")
.run();

let inscription_id = reveal_txid_from_inscribe_stdout(&stdout);

rpc_server.mine_blocks(1);

inscription_id
}

mod command_builder;
mod epochs;
mod expected;
Expand Down
35 changes: 35 additions & 0 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,41 @@ fn inscription_content() {
assert_eq!(response.bytes().unwrap(), "HELLOWORLD");
}

#[test]
fn home_page_includes_latest_inscriptions() {
let rpc_server = test_bitcoincore_rpc::spawn_with(Network::Regtest, "ord");

let inscription_id = create_inscription(&rpc_server, "foo.png");

TestServer::spawn_with_args(&rpc_server, &[]).assert_response_regex(
"/",
&format!(
".*<h2>Latest Inscriptions</h2>
<div class=inscriptions>
<a href=/inscription/{inscription_id}><img .*></a>
</div>.*"
),
);
}

#[test]
fn home_page_only_includes_graphical_inscriptions() {
let rpc_server = test_bitcoincore_rpc::spawn_with(Network::Regtest, "ord");

create_inscription(&rpc_server, "hello.txt");
let inscription_id = create_inscription(&rpc_server, "foo.png");

TestServer::spawn_with_args(&rpc_server, &[]).assert_response_regex(
"/",
&format!(
".*<h2>Latest Inscriptions</h2>
<div class=inscriptions>
<a href=/inscription/{inscription_id}><img .*></a>
</div>.*"
),
);
}

#[test]
fn inscriptions_page() {
let rpc_server = test_bitcoincore_rpc::spawn_with(Network::Regtest, "ord");
Expand Down

0 comments on commit a3b9daa

Please sign in to comment.