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

Add links between output and inscription #1162

Merged
merged 4 commits into from
Jan 5, 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
105 changes: 105 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,23 @@ impl Index {
Ok(Some((inscription, satpoint)))
}

pub(crate) fn get_inscriptions_on_output(
&self,
outpoint: OutPoint,
) -> Result<Vec<InscriptionId>> {
Ok(
Self::inscriptions_on_output(
&self
.database
.begin_read()?
.open_table(SATPOINT_TO_INSCRIPTION_ID)?,
outpoint,
)?
.map(|(_satpoint, inscription_id)| inscription_id)
.collect(),
)
}

pub(crate) fn get_transaction(&self, txid: Txid) -> Result<Option<Transaction>> {
if txid == self.genesis_block_coinbase_txid {
Ok(Some(self.genesis_block_coinbase_transaction.clone()))
Expand Down Expand Up @@ -743,6 +760,28 @@ impl Index {
);
}
}

fn inscriptions_on_output<'a: 'tx, 'tx>(
satpoint_to_id: &'a impl ReadableTable<&'tx SatPointArray, &'tx InscriptionIdArray>,
outpoint: OutPoint,
) -> Result<impl Iterator<Item = (SatPoint, InscriptionId)> + 'tx> {
let start = encode_satpoint(SatPoint {
outpoint,
offset: 0,
});

let end = encode_satpoint(SatPoint {
outpoint,
offset: u64::MAX,
});

Ok(satpoint_to_id.range(start..=end)?.map(|(satpoint, id)| {
(
decode_satpoint(*satpoint.value()),
InscriptionId::from_inner(*id.value()),
)
}))
}
}

#[cfg(test)]
Expand Down Expand Up @@ -1749,4 +1788,70 @@ mod tests {
Context::builder().tempdir(tempdir).try_build().err().unwrap().to_string(),
format!("index at `{}{delimiter}regtest{delimiter}index.redb` appears to have been built with a newer, incompatible version of ord, consider updating ord: index schema {}, ord schema {SCHEMA_VERSION}", path.display(), u64::MAX));
}

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

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

assert_eq!(
context
.index
.get_inscriptions_on_output(OutPoint {
txid: inscription_id,
vout: 0,
})
.unwrap(),
[]
);

context.mine_blocks(1);

assert_eq!(
context
.index
.get_inscriptions_on_output(OutPoint {
txid: inscription_id,
vout: 0,
})
.unwrap(),
[inscription_id]
);

let send_id = context.rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(2, 1, 0)],
..Default::default()
});

context.mine_blocks(1);

assert_eq!(
context
.index
.get_inscriptions_on_output(OutPoint {
txid: inscription_id,
vout: 0,
})
.unwrap(),
[]
);

assert_eq!(
context
.index
.get_inscriptions_on_output(OutPoint {
txid: send_id,
vout: 0,
})
.unwrap(),
[inscription_id]
);
}
}
}
20 changes: 3 additions & 17 deletions src/index/updater/inscription_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,26 +82,12 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {
if tx_in.previous_output.is_null() {
input_value += Height(self.height).subsidy();
} else {
let outpoint = tx_in.previous_output;
let start = encode_satpoint(SatPoint {
outpoint,
offset: 0,
});

let end = encode_satpoint(SatPoint {
outpoint,
offset: u64::MAX,
});

for (old_satpoint, inscription_id) in self
.satpoint_to_id
.range(start..=end)?
.map(|(satpoint, id)| (*satpoint.value(), *id.value()))
for (old_satpoint, inscription_id) in
Index::inscriptions_on_output(self.satpoint_to_id, tx_in.previous_output)?
{
let old_satpoint = decode_satpoint(old_satpoint);
inscriptions.push(Flotsam {
offset: input_value + old_satpoint.offset,
inscription_id: InscriptionId::from_inner(inscription_id),
inscription_id,
origin: Origin::Old(old_satpoint),
});
}
Expand Down
3 changes: 3 additions & 0 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,12 @@ impl Server {
.nth(outpoint.vout as usize)
.ok_or_else(|| ServerError::NotFound(format!("output {outpoint} unknown")))?;

let inscriptions = index.get_inscriptions_on_output(outpoint).unwrap();

Ok(
OutputHtml {
outpoint,
inscriptions,
list: if index.has_sat_index().map_err(ServerError::Internal)? {
Some(
index
Expand Down
4 changes: 4 additions & 0 deletions src/templates/inscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ mod tests {
<dd><a class=monospace href=/tx/1{64}>1{64}</a></dd>
<dt>location</dt>
<dd class=monospace>1{64}:1:0</dd>
<dt>output</dt>
<dd><a class=monospace href=/output/1{64}:1>1{64}:1</a></dd>
<dt>offset</dt>
<dd>0</dd>
</dl>
"
.unindent()
Expand Down
4 changes: 4 additions & 0 deletions src/templates/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub(crate) struct OutputHtml {
pub(crate) list: Option<List>,
pub(crate) chain: Chain,
pub(crate) output: TxOut,
pub(crate) inscriptions: Vec<InscriptionId>,
}

impl PageContent for OutputHtml {
Expand All @@ -25,6 +26,7 @@ mod tests {
fn unspent_output() {
pretty_assert_eq!(
OutputHtml {
inscriptions: Vec::new(),
outpoint: "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b:0"
.parse()
.unwrap(),
Expand Down Expand Up @@ -57,6 +59,7 @@ mod tests {
fn spent_output() {
pretty_assert_eq!(
OutputHtml {
inscriptions: Vec::new(),
outpoint: "0000000000000000000000000000000000000000000000000000000000000000:0"
.parse()
.unwrap(),
Expand Down Expand Up @@ -84,6 +87,7 @@ mod tests {
fn no_list() {
pretty_assert_eq!(
OutputHtml {
inscriptions: Vec::new(),
outpoint: "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b:0"
.parse()
.unwrap(),
Expand Down
4 changes: 4 additions & 0 deletions templates/inscription.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ <h1>Inscription {{ self.inscription_id }}</h1>
<dd><a class=monospace href=/tx/{{ self.inscription_id }}>{{ self.inscription_id }}</a></dd>
<dt>location</dt>
<dd class=monospace>{{ self.satpoint }}</dd>
<dt>output</dt>
<dd><a class=monospace href=/output/{{ self.satpoint.outpoint }}>{{ self.satpoint.outpoint }}</a></dd>
<dt>offset</dt>
<dd>{{ self.satpoint.offset }}</dd>
</dl>
6 changes: 6 additions & 0 deletions templates/output.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<h1>Output <span class=monospace>{{self.outpoint}}</span></h1>
<dl>
%% if !self.inscriptions.is_empty() {
<dt>inscriptions</dt>
%% for inscription in &self.inscriptions {
<dd>{{Iframe::preview(*inscription)}}</dd>
%% }
%% }
<dt>value</dt><dd>{{ self.output.value }}</dd>
<dt>script pubkey</dt><dd class=data>{{ self.output.script_pubkey.asm() }}</dd>
%% if let Ok(address) = self.chain.address_from_script(&self.output.script_pubkey ) {
Expand Down
2 changes: 1 addition & 1 deletion templates/transaction.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h1>Transaction <span class=monospace>{{self.txid}}</span></h1>
%% if let Some(inscription) = &self.inscription {
<h2>Inscription</h2>
<h2>Inscription Geneses</h2>
{{ Iframe::preview(self.txid) }}
%% }
<h2>{{"Output".tally(self.transaction.output.len())}}</h2>
Expand Down
29 changes: 29 additions & 0 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ fn inscription_page() {
<dd><a class=monospace href=/tx/{reveal_tx}>{reveal_tx}</a></dd>
<dt>location</dt>
<dd class=monospace>{reveal_tx}:0:0</dd>
<dt>output</dt>
<dd><a class=monospace href=/output/{reveal_tx}:0>{reveal_tx}:0</a></dd>
<dt>offset</dt>
<dd>0</dd>
</dl>.*",
),
);
Expand Down Expand Up @@ -100,6 +104,31 @@ fn inscription_appears_on_reveal_transaction_page() {
);
}

#[test]
fn inscription_appears_on_output_page() {
let rpc_server = test_bitcoincore_rpc::builder()
.network(Network::Regtest)
.build();
let txid = rpc_server.mine_blocks(1)[0].txdata[0].txid();

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

let reveal_tx = reveal_txid_from_inscribe_stdout(&stdout);

rpc_server.mine_blocks(1);

TestServer::spawn_with_args(&rpc_server, &[]).assert_response_regex(
format!("/output/{reveal_tx}:0"),
format!(".*<h1>Output <span class=monospace>{reveal_tx}:0</span></h1>.*<a href=/inscription/{reveal_tx}.*"),
);
}

#[test]
fn inscription_page_after_send() {
let rpc_server = test_bitcoincore_rpc::builder()
Expand Down
1 change: 1 addition & 0 deletions tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ fn send_works_on_signet() {
.*
<dt>location</dt>
<dd class=monospace>{send_txid}:0:0</dd>
.*
</dl>
.*",
),
Expand Down