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

Cursed Inscriptions [1/n] #2145

Merged
merged 17 commits into from
Jun 4, 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
550 changes: 520 additions & 30 deletions src/index.rs

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions src/index/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ impl Entry for BlockHash {
}
}

#[derive(Debug)]
pub(crate) struct InscriptionEntry {
pub(crate) fee: u64,
pub(crate) height: u64,
pub(crate) number: u64,
pub(crate) number: i64,
pub(crate) sat: Option<Sat>,
pub(crate) timestamp: u32,
}

pub(crate) type InscriptionEntryValue = (u64, u64, u64, u64, u32);
pub(crate) type InscriptionEntryValue = (u64, u64, i64, u64, u32);

impl Entry for InscriptionEntry {
type Value = InscriptionEntryValue;
Expand Down
243 changes: 173 additions & 70 deletions src/index/updater/inscription_updater.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
use super::*;

#[derive(Debug, Clone)]
pub(super) struct Flotsam {
inscription_id: InscriptionId,
offset: u64,
origin: Origin,
}

#[derive(Debug, Clone)]
enum Origin {
New { fee: u64 },
Old { old_satpoint: SatPoint },
New {
fee: u64,
cursed: bool,
unbound: bool,
},
Old {
old_satpoint: SatPoint,
},
}

pub(super) struct InscriptionUpdater<'a, 'db, 'tx> {
Expand All @@ -18,8 +26,9 @@ pub(super) struct InscriptionUpdater<'a, 'db, 'tx> {
value_receiver: &'a mut Receiver<u64>,
id_to_entry: &'a mut Table<'db, 'tx, &'static InscriptionIdValue, InscriptionEntryValue>,
pub(super) lost_sats: u64,
next_number: u64,
number_to_id: &'a mut Table<'db, 'tx, u64, &'static InscriptionIdValue>,
next_cursed_number: i64,
next_number: i64,
number_to_id: &'a mut Table<'db, 'tx, i64, &'static InscriptionIdValue>,
outpoint_to_value: &'a mut Table<'db, 'tx, &'static OutPointValue, u64>,
reward: u64,
sat_to_inscription_id: &'a mut Table<'db, 'tx, u64, &'static InscriptionIdValue>,
Expand All @@ -36,14 +45,20 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {
value_receiver: &'a mut Receiver<u64>,
id_to_entry: &'a mut Table<'db, 'tx, &'static InscriptionIdValue, InscriptionEntryValue>,
lost_sats: u64,
number_to_id: &'a mut Table<'db, 'tx, u64, &'static InscriptionIdValue>,
number_to_id: &'a mut Table<'db, 'tx, i64, &'static InscriptionIdValue>,
outpoint_to_value: &'a mut Table<'db, 'tx, &'static OutPointValue, u64>,
sat_to_inscription_id: &'a mut Table<'db, 'tx, u64, &'static InscriptionIdValue>,
satpoint_to_id: &'a mut Table<'db, 'tx, &'static SatPointValue, &'static InscriptionIdValue>,
timestamp: u32,
unbound_inscriptions: u64,
value_cache: &'a mut HashMap<OutPoint, u64>,
) -> Result<Self> {
let next_cursed_number = number_to_id
.iter()?
.map(|(number, _id)| number.value() - 1)
.next()
.unwrap_or(-1);

let next_number = number_to_id
.iter()?
.rev()
Expand All @@ -58,6 +73,7 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {
value_receiver,
id_to_entry,
lost_sats,
next_cursed_number,
next_number,
number_to_id,
outpoint_to_value,
Expand All @@ -76,66 +92,129 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {
txid: Txid,
input_sat_ranges: Option<&VecDeque<(u64, u64)>>,
) -> Result {
let mut inscriptions = Vec::new();

let mut new_inscriptions = Inscription::from_transaction(tx).into_iter().peekable();
let mut floating_inscriptions = Vec::new();
let mut inscribed_offsets = BTreeMap::new();
let mut input_value = 0;
for tx_in in &tx.input {
let mut id_counter = 0;

for (input_index, tx_in) in tx.input.iter().enumerate() {
// skip subsidy since no inscriptions possible
if tx_in.previous_output.is_null() {
input_value += Height(self.height).subsidy();
} else {
for (old_satpoint, inscription_id) in
Index::inscriptions_on_output(self.satpoint_to_id, tx_in.previous_output)?
{
inscriptions.push(Flotsam {
offset: input_value + old_satpoint.offset,
inscription_id,
origin: Origin::Old { old_satpoint },
});
}
continue;
}

input_value += if let Some(value) = self.value_cache.remove(&tx_in.previous_output) {
value
} else if let Some(value) = self
.outpoint_to_value
.remove(&tx_in.previous_output.store())?
{
value.value()
} else {
self.value_receiver.blocking_recv().ok_or_else(|| {
anyhow!(
"failed to get transaction for {}",
tx_in.previous_output.txid
)
})?
}
// find existing inscriptions on input aka transfers of inscriptions
for (old_satpoint, inscription_id) in
Index::inscriptions_on_output(self.satpoint_to_id, tx_in.previous_output)?
{
let offset = input_value + old_satpoint.offset;
floating_inscriptions.push(Flotsam {
offset,
inscription_id,
origin: Origin::Old { old_satpoint },
});

inscribed_offsets.insert(offset, inscription_id);
}
}

if inscriptions.iter().all(|flotsam| flotsam.offset != 0)
&& Inscription::from_transaction(tx).is_some()
{
let flotsam = Flotsam {
inscription_id: txid.into(),
offset: 0,
origin: Origin::New {
fee: input_value - tx.output.iter().map(|txout| txout.value).sum::<u64>(),
},
let offset = input_value;

// multi-level cache for UTXO set to get to the input amount
input_value += if let Some(value) = self.value_cache.remove(&tx_in.previous_output) {
value
} else if let Some(value) = self
.outpoint_to_value
.remove(&tx_in.previous_output.store())?
{
value.value()
} else {
self.value_receiver.blocking_recv().ok_or_else(|| {
anyhow!(
"failed to get transaction for {}",
tx_in.previous_output.txid
)
})?
};

if input_value == 0 {
self.update_inscription_location(
input_sat_ranges,
flotsam,
SatPoint {
outpoint: unbound_outpoint(),
offset: self.unbound_inscriptions,
// go through all inscriptions in this input
while let Some(inscription) = new_inscriptions.peek() {
if inscription.tx_in_index != u32::try_from(input_index).unwrap() {
break;
}

let initial_inscription_is_cursed = inscribed_offsets
.get(&offset)
.and_then(
|inscription_id| match self.id_to_entry.get(&inscription_id.store()) {
Ok(option) => option.map(|entry| InscriptionEntry::load(entry.value()).number < 0),
Err(_) => None,
},
)
.unwrap_or(false);

let cursed = !initial_inscription_is_cursed
&& (inscription.tx_in_index != 0
|| inscription.tx_in_offset != 0
|| inscribed_offsets.contains_key(&offset));

// In this first part of the cursed inscriptions implementation we ignore reinscriptions.
// This will change once we implement reinscriptions.
let unbound = inscribed_offsets.contains_key(&offset)
|| inscription.tx_in_offset != 0
|| input_value == 0;

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

floating_inscriptions.push(Flotsam {
inscription_id,
offset,
origin: Origin::New {
fee: 0,
cursed,
unbound,
},
)?;
self.unbound_inscriptions += 1;
} else {
inscriptions.push(flotsam);
});

new_inscriptions.next();
id_counter += 1;
}
};
}

// still have to normalize over inscription size
let total_output_value = tx.output.iter().map(|txout| txout.value).sum::<u64>();
let mut floating_inscriptions = floating_inscriptions
.into_iter()
.map(|flotsam| {
if let Flotsam {
inscription_id,
offset,
origin:
Origin::New {
fee: _,
cursed,
unbound,
},
} = flotsam
{
Flotsam {
inscription_id,
offset,
origin: Origin::New {
fee: (input_value - total_output_value) / u64::from(id_counter),
cursed,
unbound,
},
}
} else {
flotsam
}
})
.collect::<Vec<Flotsam>>();

let is_coinbase = tx
.input
Expand All @@ -144,11 +223,11 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {
.unwrap_or_default();

if is_coinbase {
inscriptions.append(&mut self.flotsam);
floating_inscriptions.append(&mut self.flotsam);
}

inscriptions.sort_by_key(|flotsam| flotsam.offset);
let mut inscriptions = inscriptions.into_iter().peekable();
floating_inscriptions.sort_by_key(|flotsam| flotsam.offset);
let mut inscriptions = floating_inscriptions.into_iter().peekable();

let mut output_value = 0;
for (vout, tx_out) in tx.output.iter().enumerate() {
Expand Down Expand Up @@ -212,15 +291,30 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {
new_satpoint: SatPoint,
) -> Result {
let inscription_id = flotsam.inscription_id.store();

match flotsam.origin {
let unbound = match flotsam.origin {
Origin::Old { old_satpoint } => {
self.satpoint_to_id.remove(&old_satpoint.store())?;

false
}
Origin::New { fee } => {
self
.number_to_id
.insert(&self.next_number, &inscription_id)?;
Origin::New {
fee,
cursed,
unbound,
} => {
let number = if cursed {
let next_cursed_number = self.next_cursed_number;
self.next_cursed_number -= 1;

next_cursed_number
} else {
let next_number = self.next_number;
self.next_number += 1;

next_number
};

self.number_to_id.insert(number, &inscription_id)?;

let mut sat = None;
if let Some(input_sat_ranges) = input_sat_ranges {
Expand All @@ -242,21 +336,30 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {
&InscriptionEntry {
fee,
height: self.height,
number: self.next_number,
number,
sat,
timestamp: self.timestamp,
}
.store(),
)?;

self.next_number += 1;
unbound
}
}
};

let new_satpoint = new_satpoint.store();
let satpoint = if unbound {
let new_unbound_satpoint = SatPoint {
outpoint: unbound_outpoint(),
offset: self.unbound_inscriptions,
};
self.unbound_inscriptions += 1;
new_unbound_satpoint.store()
} else {
new_satpoint.store()
};

self.satpoint_to_id.insert(&new_satpoint, &inscription_id)?;
self.id_to_satpoint.insert(&inscription_id, &new_satpoint)?;
self.satpoint_to_id.insert(&satpoint, &inscription_id)?;
self.id_to_satpoint.insert(&inscription_id, &satpoint)?;

Ok(())
}
Expand Down
Loading