Skip to content

Commit

Permalink
Display inscription on /rune (#2542)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Oct 24, 2023
1 parent 15faa09 commit 805b949
Show file tree
Hide file tree
Showing 11 changed files with 264 additions and 208 deletions.
16 changes: 16 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ define_multimap_table! { SAT_TO_INSCRIPTION_ID, u64, &InscriptionIdValue }
define_table! { HEIGHT_TO_BLOCK_HASH, u64, &BlockHashValue }
define_table! { HEIGHT_TO_LAST_SEQUENCE_NUMBER, u64, u64 }
define_table! { INSCRIPTION_ID_TO_INSCRIPTION_ENTRY, &InscriptionIdValue, InscriptionEntryValue }
define_table! { INSCRIPTION_ID_TO_RUNE, &InscriptionIdValue, u128 }
define_table! { INSCRIPTION_ID_TO_SATPOINT, &InscriptionIdValue, &SatPointValue }
define_table! { INSCRIPTION_NUMBER_TO_INSCRIPTION_ID, i64, &InscriptionIdValue }
define_table! { OUTPOINT_TO_RUNE_BALANCES, &OutPointValue, &[u8] }
Expand Down Expand Up @@ -273,6 +274,7 @@ impl Index {
tx.open_table(HEIGHT_TO_BLOCK_HASH)?;
tx.open_table(HEIGHT_TO_LAST_SEQUENCE_NUMBER)?;
tx.open_table(INSCRIPTION_ID_TO_INSCRIPTION_ENTRY)?;
tx.open_table(INSCRIPTION_ID_TO_RUNE)?;
tx.open_table(INSCRIPTION_ID_TO_SATPOINT)?;
tx.open_table(INSCRIPTION_NUMBER_TO_INSCRIPTION_ID)?;
tx.open_table(OUTPOINT_TO_RUNE_BALANCES)?;
Expand Down Expand Up @@ -795,6 +797,20 @@ impl Index {
.collect()
}

pub(crate) fn get_rune_by_inscription_id(
&self,
inscription_id: InscriptionId,
) -> Result<Option<Rune>> {
Ok(
self
.database
.begin_read()?
.open_table(INSCRIPTION_ID_TO_RUNE)?
.get(&inscription_id.store())?
.map(|entry| Rune(entry.value())),
)
}

pub(crate) fn get_inscription_ids_by_sat(&self, sat: Sat) -> Result<Vec<InscriptionId>> {
let rtx = &self.database.begin_read()?;

Expand Down
270 changes: 138 additions & 132 deletions src/index/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,23 +376,6 @@ impl<'index> Updater<'_> {
}
}

if index.index_runes {
let mut outpoint_to_rune_balances = wtx.open_table(OUTPOINT_TO_RUNE_BALANCES)?;
let mut rune_id_to_rune_entry = wtx.open_table(RUNE_ID_TO_RUNE_ENTRY)?;
let mut rune_to_rune_id = wtx.open_table(RUNE_TO_RUNE_ID)?;
let mut statistic_to_count = wtx.open_table(STATISTIC_TO_COUNT)?;
let mut rune_updater = RuneUpdater::new(
self.height,
&mut outpoint_to_rune_balances,
&mut rune_id_to_rune_entry,
&mut rune_to_rune_id,
&mut statistic_to_count,
)?;
for (i, (tx, txid)) in block.txdata.iter().enumerate() {
rune_updater.index_runes(i, tx, *txid)?;
}
}

let mut height_to_block_hash = wtx.open_table(HEIGHT_TO_BLOCK_HASH)?;
let mut height_to_last_sequence_number = wtx.open_table(HEIGHT_TO_LAST_SEQUENCE_NUMBER)?;
let mut inscription_id_to_inscription_entry =
Expand Down Expand Up @@ -427,143 +410,166 @@ impl<'index> Updater<'_> {
.map(|unbound_inscriptions| unbound_inscriptions.value())
.unwrap_or(0);

let mut inscription_updater = InscriptionUpdater::new(
self.height,
&mut inscription_id_to_children,
&mut inscription_id_to_satpoint,
value_receiver,
&mut inscription_id_to_inscription_entry,
lost_sats,
&mut inscription_number_to_inscription_id,
cursed_inscription_count,
blessed_inscription_count,
&mut sequence_number_to_inscription_id,
&mut outpoint_to_value,
&mut sat_to_inscription_id,
&mut satpoint_to_inscription_id,
block.header.time,
unbound_inscriptions,
value_cache,
)?;
{
let mut inscription_updater = InscriptionUpdater::new(
self.height,
&mut inscription_id_to_children,
&mut inscription_id_to_satpoint,
value_receiver,
&mut inscription_id_to_inscription_entry,
lost_sats,
&mut inscription_number_to_inscription_id,
cursed_inscription_count,
blessed_inscription_count,
&mut sequence_number_to_inscription_id,
&mut outpoint_to_value,
&mut sat_to_inscription_id,
&mut satpoint_to_inscription_id,
block.header.time,
unbound_inscriptions,
value_cache,
)?;

if self.index.index_sats {
let mut sat_to_satpoint = wtx.open_table(SAT_TO_SATPOINT)?;
let mut outpoint_to_sat_ranges = wtx.open_table(OUTPOINT_TO_SAT_RANGES)?;
if self.index.index_sats {
let mut sat_to_satpoint = wtx.open_table(SAT_TO_SATPOINT)?;
let mut outpoint_to_sat_ranges = wtx.open_table(OUTPOINT_TO_SAT_RANGES)?;

let mut coinbase_inputs = VecDeque::new();
let mut coinbase_inputs = VecDeque::new();

let h = Height(self.height);
if h.subsidy() > 0 {
let start = h.starting_sat();
coinbase_inputs.push_front((start.n(), (start + h.subsidy()).n()));
self.sat_ranges_since_flush += 1;
}
let h = Height(self.height);
if h.subsidy() > 0 {
let start = h.starting_sat();
coinbase_inputs.push_front((start.n(), (start + h.subsidy()).n()));
self.sat_ranges_since_flush += 1;
}

for (tx_offset, (tx, txid)) in block.txdata.iter().enumerate().skip(1) {
log::trace!("Indexing transaction {tx_offset}…");
for (tx_offset, (tx, txid)) in block.txdata.iter().enumerate().skip(1) {
log::trace!("Indexing transaction {tx_offset}…");

let mut input_sat_ranges = VecDeque::new();
let mut input_sat_ranges = VecDeque::new();

for input in &tx.input {
let key = input.previous_output.store();
for input in &tx.input {
let key = input.previous_output.store();

let sat_ranges = match self.range_cache.remove(&key) {
Some(sat_ranges) => {
self.outputs_cached += 1;
sat_ranges
}
None => outpoint_to_sat_ranges
.remove(&key)?
.ok_or_else(|| anyhow!("Could not find outpoint {} in index", input.previous_output))?
.value()
.to_vec(),
};
let sat_ranges = match self.range_cache.remove(&key) {
Some(sat_ranges) => {
self.outputs_cached += 1;
sat_ranges
}
None => outpoint_to_sat_ranges
.remove(&key)?
.ok_or_else(|| {
anyhow!("Could not find outpoint {} in index", input.previous_output)
})?
.value()
.to_vec(),
};

for chunk in sat_ranges.chunks_exact(11) {
input_sat_ranges.push_back(SatRange::load(chunk.try_into().unwrap()));
for chunk in sat_ranges.chunks_exact(11) {
input_sat_ranges.push_back(SatRange::load(chunk.try_into().unwrap()));
}
}

self.index_transaction_sats(
tx,
*txid,
&mut sat_to_satpoint,
&mut input_sat_ranges,
&mut sat_ranges_written,
&mut outputs_in_block,
&mut inscription_updater,
index_inscriptions,
)?;

coinbase_inputs.extend(input_sat_ranges);
}

self.index_transaction_sats(
tx,
*txid,
&mut sat_to_satpoint,
&mut input_sat_ranges,
&mut sat_ranges_written,
&mut outputs_in_block,
&mut inscription_updater,
index_inscriptions,
)?;

coinbase_inputs.extend(input_sat_ranges);
}
if let Some((tx, txid)) = block.txdata.get(0) {
self.index_transaction_sats(
tx,
*txid,
&mut sat_to_satpoint,
&mut coinbase_inputs,
&mut sat_ranges_written,
&mut outputs_in_block,
&mut inscription_updater,
index_inscriptions,
)?;
}

if let Some((tx, txid)) = block.txdata.get(0) {
self.index_transaction_sats(
tx,
*txid,
&mut sat_to_satpoint,
&mut coinbase_inputs,
&mut sat_ranges_written,
&mut outputs_in_block,
&mut inscription_updater,
index_inscriptions,
)?;
}
if !coinbase_inputs.is_empty() {
let mut lost_sat_ranges = outpoint_to_sat_ranges
.remove(&OutPoint::null().store())?
.map(|ranges| ranges.value().to_vec())
.unwrap_or_default();

for (start, end) in coinbase_inputs {
if !Sat(start).is_common() {
sat_to_satpoint.insert(
&start,
&SatPoint {
outpoint: OutPoint::null(),
offset: lost_sats,
}
.store(),
)?;
}

if !coinbase_inputs.is_empty() {
let mut lost_sat_ranges = outpoint_to_sat_ranges
.remove(&OutPoint::null().store())?
.map(|ranges| ranges.value().to_vec())
.unwrap_or_default();

for (start, end) in coinbase_inputs {
if !Sat(start).is_common() {
sat_to_satpoint.insert(
&start,
&SatPoint {
outpoint: OutPoint::null(),
offset: lost_sats,
}
.store(),
)?;
}
lost_sat_ranges.extend_from_slice(&(start, end).store());

lost_sat_ranges.extend_from_slice(&(start, end).store());
lost_sats += end - start;
}

lost_sats += end - start;
outpoint_to_sat_ranges.insert(&OutPoint::null().store(), lost_sat_ranges.as_slice())?;
}
} else {
for (tx, txid) in block.txdata.iter().skip(1).chain(block.txdata.first()) {
inscription_updater.index_envelopes(tx, *txid, None)?;
}

outpoint_to_sat_ranges.insert(&OutPoint::null().store(), lost_sat_ranges.as_slice())?;
}
} else {
for (tx, txid) in block.txdata.iter().skip(1).chain(block.txdata.first()) {
inscription_updater.index_envelopes(tx, *txid, None)?;
}
}

self.index_block_inscription_numbers(
&mut height_to_last_sequence_number,
&inscription_updater,
index_inscriptions,
)?;
self.index_block_inscription_numbers(
&mut height_to_last_sequence_number,
&inscription_updater,
index_inscriptions,
)?;

statistic_to_count.insert(&Statistic::LostSats.key(), &inscription_updater.lost_sats)?;

statistic_to_count.insert(&Statistic::LostSats.key(), &inscription_updater.lost_sats)?;
statistic_to_count.insert(
&Statistic::CursedInscriptions.key(),
&inscription_updater.cursed_inscription_count,
)?;

statistic_to_count.insert(
&Statistic::CursedInscriptions.key(),
&inscription_updater.cursed_inscription_count,
)?;
statistic_to_count.insert(
&Statistic::BlessedInscriptions.key(),
&inscription_updater.blessed_inscription_count,
)?;

statistic_to_count.insert(
&Statistic::BlessedInscriptions.key(),
&inscription_updater.blessed_inscription_count,
)?;
statistic_to_count.insert(
&Statistic::UnboundInscriptions.key(),
&inscription_updater.unbound_inscriptions,
)?;
}

statistic_to_count.insert(
&Statistic::UnboundInscriptions.key(),
&inscription_updater.unbound_inscriptions,
)?;
if index.index_runes {
let mut outpoint_to_rune_balances = wtx.open_table(OUTPOINT_TO_RUNE_BALANCES)?;
let mut rune_id_to_rune_entry = wtx.open_table(RUNE_ID_TO_RUNE_ENTRY)?;
let mut rune_to_rune_id = wtx.open_table(RUNE_TO_RUNE_ID)?;
let mut inscription_id_to_rune = wtx.open_table(INSCRIPTION_ID_TO_RUNE)?;
let mut rune_updater = RuneUpdater::new(
self.height,
&mut outpoint_to_rune_balances,
&mut rune_id_to_rune_entry,
&inscription_id_to_inscription_entry,
&mut inscription_id_to_rune,
&mut rune_to_rune_id,
&mut statistic_to_count,
)?;
for (i, (tx, txid)) in block.txdata.iter().enumerate() {
rune_updater.index_runes(i, tx, *txid)?;
}
}

height_to_block_hash.insert(&self.height, &block.header.block_hash().store())?;

Expand Down
24 changes: 24 additions & 0 deletions src/index/updater/rune_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ struct Allocation {
pub(super) struct RuneUpdater<'a, 'db, 'tx> {
height: u64,
id_to_entry: &'a mut Table<'db, 'tx, RuneIdValue, RuneEntryValue>,
inscription_id_to_inscription_entry:
&'a Table<'db, 'tx, &'static InscriptionIdValue, InscriptionEntryValue>,
inscription_id_to_rune: &'a mut Table<'db, 'tx, &'static InscriptionIdValue, u128>,
minimum: Rune,
outpoint_to_balances: &'a mut Table<'db, 'tx, &'static OutPointValue, &'static [u8]>,
rune_to_id: &'a mut Table<'db, 'tx, u128, RuneIdValue>,
Expand All @@ -26,6 +29,13 @@ impl<'a, 'db, 'tx> RuneUpdater<'a, 'db, 'tx> {
height: u64,
outpoint_to_balances: &'a mut Table<'db, 'tx, &'static OutPointValue, &'static [u8]>,
id_to_entry: &'a mut Table<'db, 'tx, RuneIdValue, RuneEntryValue>,
inscription_id_to_inscription_entry: &'a Table<
'db,
'tx,
&'static InscriptionIdValue,
InscriptionEntryValue,
>,
inscription_id_to_rune: &'a mut Table<'db, 'tx, &'static InscriptionIdValue, u128>,
rune_to_id: &'a mut Table<'db, 'tx, u128, RuneIdValue>,
statistic_to_count: &'a mut Table<'db, 'tx, u64, u64>,
) -> Result<Self> {
Expand All @@ -38,6 +48,8 @@ impl<'a, 'db, 'tx> RuneUpdater<'a, 'db, 'tx> {
id_to_entry,
minimum: Rune::minimum_at_height(Height(height)),
outpoint_to_balances,
inscription_id_to_inscription_entry,
inscription_id_to_rune,
rune_to_id,
runes,
statistic_to_count,
Expand Down Expand Up @@ -204,6 +216,18 @@ impl<'a, 'db, 'tx> RuneUpdater<'a, 'db, 'tx> {
}
.store(),
)?;

let inscription_id = InscriptionId { txid, index: 0 };

if self
.inscription_id_to_inscription_entry
.get(&inscription_id.store())?
.is_some()
{
self
.inscription_id_to_rune
.insert(&inscription_id.store(), rune.0)?;
}
}
}

Expand Down
Loading

0 comments on commit 805b949

Please sign in to comment.