Skip to content

Commit

Permalink
Always store script pubkey in output table
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph committed Mar 4, 2025
1 parent ddcdbdb commit d401a37
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ mod utxo_entry;
#[cfg(test)]
pub(crate) mod testing;

const SCHEMA_VERSION: u64 = 30;
const SCHEMA_VERSION: u64 = 31;

define_multimap_table! { SAT_TO_SEQUENCE_NUMBER, u64, u32 }
define_multimap_table! { SEQUENCE_NUMBER_TO_CHILDREN, u32, u32 }
Expand Down
12 changes: 3 additions & 9 deletions src/index/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,9 +574,7 @@ impl Updater<'_> {

let mut entry = UtxoEntryBuf::new();
entry.push_value(txout.value.to_sat(), self.index);
if self.index.index_addresses {
entry.push_script_pubkey(txout.script_pubkey.as_bytes(), self.index);
}
entry.push_script_pubkey(txout.script_pubkey.as_bytes(), self.index);

entry
};
Expand Down Expand Up @@ -632,9 +630,7 @@ impl Updater<'_> {
}
}

if self.index.index_addresses {
self.index_transaction_output_script_pubkeys(tx, &mut output_utxo_entries);
}
self.index_transaction_output_script_pubkeys(tx, &mut output_utxo_entries);

if index_inscriptions {
inscription_updater.index_inscriptions(
Expand Down Expand Up @@ -685,9 +681,7 @@ impl Updater<'_> {

let mut new_utxo_entry = UtxoEntryBuf::new();
new_utxo_entry.push_sat_ranges(&lost_sat_ranges, self.index);
if self.index.index_addresses {
new_utxo_entry.push_script_pubkey(&[], self.index);
}
new_utxo_entry.push_script_pubkey(&[], self.index);

*utxo_entry = UtxoEntryBuf::merged(utxo_entry, &new_utxo_entry, self.index);
}
Expand Down
38 changes: 13 additions & 25 deletions src/index/utxo_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ enum Sats<'a> {
/// by that many 11-byte sat range entries, otherwise the total output value
/// stored as a varint.
///
/// If `--index-addresses`, the script pubkey stored as a varint followed by
/// that many bytes of data.
///
/// If `--index-inscriptions`, the list of inscriptions stored as
/// `(sequence_number, offset)`, with the sequence number stored as a u32 and
/// the offset as a varint.
///
/// The script pubkey stored as a varint followed by that many bytes of data.
///
/// Note that the list of inscriptions doesn't need an explicit length, it
/// continues until the end of the array.
///
Expand All @@ -43,7 +42,7 @@ pub struct UtxoEntry {
impl UtxoEntry {
pub fn parse(&self, index: &Index) -> ParsedUtxoEntry {
let sats;
let mut script_pubkey = None;
let script_pubkey;
let mut inscriptions = None;

let mut offset = 0;
Expand All @@ -61,14 +60,12 @@ impl UtxoEntry {
offset += varint_len;
};

if index.index_addresses {
let (script_pubkey_len, varint_len) = varint::decode(&self.bytes[offset..]).unwrap();
offset += varint_len;
let (script_pubkey_len, varint_len) = varint::decode(&self.bytes[offset..]).unwrap();
offset += varint_len;

let script_pubkey_len: usize = script_pubkey_len.try_into().unwrap();
script_pubkey = Some(&self.bytes[offset..offset + script_pubkey_len]);
offset += script_pubkey_len;
}
let script_pubkey_len: usize = script_pubkey_len.try_into().unwrap();
script_pubkey = Some(&self.bytes[offset..offset + script_pubkey_len]);
offset += script_pubkey_len;

if index.index_inscriptions {
inscriptions = Some(&self.bytes[offset..self.bytes.len()]);
Expand Down Expand Up @@ -229,7 +226,6 @@ impl UtxoEntryBuf {
}

pub fn push_script_pubkey(&mut self, script_pubkey: &[u8], index: &Index) {
assert!(index.index_addresses);
varint::encode_to_vec(script_pubkey.len().try_into().unwrap(), &mut self.vec);
self.vec.extend(script_pubkey);

Expand All @@ -255,13 +251,9 @@ impl UtxoEntryBuf {
}

#[cfg(debug_assertions)]
fn advance_state(&mut self, expected_state: State, new_state: State, index: &Index) {
fn advance_state(&mut self, expected_state: State, new_state: State, _index: &Index) {
assert!(self.state == expected_state);
self.state = new_state;

if self.state == State::NeedScriptPubkey && !index.index_addresses {
self.state = State::Valid;
}
}

pub fn merged(a: &UtxoEntry, b: &UtxoEntry, index: &Index) -> Self {
Expand All @@ -278,11 +270,9 @@ impl UtxoEntryBuf {
merged.push_value(0, index);
}

if index.index_addresses {
assert!(a_parsed.script_pubkey().is_empty());
assert!(b_parsed.script_pubkey().is_empty());
merged.push_script_pubkey(&[], index);
}
assert!(a_parsed.script_pubkey().is_empty());
assert!(b_parsed.script_pubkey().is_empty());
merged.push_script_pubkey(&[], index);

if index.index_inscriptions {
merged.push_inscriptions(a_parsed.inscriptions(), index);
Expand All @@ -301,9 +291,7 @@ impl UtxoEntryBuf {
utxo_entry.push_value(0, index);
}

if index.index_addresses {
utxo_entry.push_script_pubkey(&[], index);
}
utxo_entry.push_script_pubkey(&[], index);

utxo_entry
}
Expand Down

0 comments on commit d401a37

Please sign in to comment.