Skip to content

Commit f0bf303

Browse files
committed
Check for balances spent to op_return in assert_runes
1 parent f780624 commit f0bf303

File tree

6 files changed

+313
-105
lines changed

6 files changed

+313
-105
lines changed

src/index.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,23 @@ impl Index {
499499
)
500500
}
501501

502+
pub fn get_output_script(&self, output: &OutPoint) -> Result<Option<ScriptBuf>> {
503+
let rtx = self.database.begin_read()?;
504+
let outpoint_to_utxo_entry = rtx.open_table(OUTPOINT_TO_UTXO_ENTRY)?;
505+
506+
let entry = outpoint_to_utxo_entry
507+
.get(&output.store())?
508+
.ok_or_else(|| anyhow!("UTXO entry not found for output: {:?}", output))?;
509+
510+
let parsed = entry.value().parse(self);
511+
512+
Ok(
513+
parsed
514+
.script_pubkey()
515+
.map(|bytes| ScriptBuf::from_bytes(bytes.to_vec())),
516+
)
517+
}
518+
502519
pub fn has_address_index(&self) -> bool {
503520
self.index_addresses
504521
}
@@ -744,6 +761,7 @@ impl Index {
744761
.value()
745762
.parse(self)
746763
.script_pubkey()
764+
.unwrap()
747765
.to_vec(),
748766
)
749767
} else {
@@ -6621,7 +6639,7 @@ mod tests {
66216639

66226640
let (event_sender, mut event_receiver) = tokio::sync::mpsc::channel(1024);
66236641
let context = Context::builder()
6624-
.arg("--index-runes")
6642+
.args(["--index-runes", "--index-addresses"])
66256643
.event_sender(event_sender)
66266644
.build();
66276645

src/index/testing.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,22 @@ impl Context {
128128
let balances = balances.as_mut();
129129
balances.sort_by_key(|(outpoint, _)| *outpoint);
130130

131-
for (_, balances) in balances.iter_mut() {
131+
let mut burned_balances = 0;
132+
133+
for (outpoint, balances) in balances.iter_mut() {
134+
if self
135+
.index
136+
.get_output_script(outpoint)
137+
.ok()
138+
.flatten()
139+
.map_or(false, |script| script.is_op_return())
140+
{
141+
for (_, balance) in balances.iter() {
142+
println!("burned_balances: {}", burned_balances);
143+
burned_balances += *balance;
144+
}
145+
}
146+
132147
balances.sort_by_key(|(id, _)| *id);
133148
}
134149

@@ -146,7 +161,7 @@ impl Context {
146161

147162
for (id, entry) in runes {
148163
pretty_assert_eq!(
149-
outstanding.get(id).copied().unwrap_or_default(),
164+
outstanding.get(id).copied().unwrap_or_default() - burned_balances,
150165
entry.supply() - entry.burned
151166
);
152167
}

src/index/updater.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ impl Updater<'_> {
556556
entry
557557
} else if let Some(entry) = outpoint_to_utxo_entry.remove(&outpoint)? {
558558
if self.index.index_addresses {
559-
let script_pubkey = entry.value().parse(self.index).script_pubkey();
559+
let script_pubkey = entry.value().parse(self.index).script_pubkey().unwrap();
560560
if !script_pubkey_to_outpoint.remove(script_pubkey, outpoint)? {
561561
panic!("script pubkey entry ({script_pubkey:?}, {outpoint:?}) not found");
562562
}
@@ -845,7 +845,7 @@ impl Updater<'_> {
845845

846846
let utxo_entry = utxo_entry.parse(self.index);
847847
if self.index.index_addresses {
848-
let script_pubkey = utxo_entry.script_pubkey();
848+
let script_pubkey = utxo_entry.script_pubkey().unwrap();
849849
script_pubkey_to_outpoint.insert(script_pubkey, &outpoint.store())?;
850850
}
851851

src/index/utxo_entry.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ impl<'a> ParsedUtxoEntry<'a> {
153153
ranges
154154
}
155155

156-
pub fn script_pubkey(&self) -> &'a [u8] {
157-
self.script_pubkey.unwrap()
156+
pub fn script_pubkey(&self) -> Option<&'a [u8]> {
157+
self.script_pubkey
158158
}
159159

160160
pub fn inscriptions(&self) -> &'a [u8] {
@@ -279,8 +279,8 @@ impl UtxoEntryBuf {
279279
}
280280

281281
if index.index_addresses {
282-
assert!(a_parsed.script_pubkey().is_empty());
283-
assert!(b_parsed.script_pubkey().is_empty());
282+
assert!(a_parsed.script_pubkey().unwrap().is_empty());
283+
assert!(b_parsed.script_pubkey().unwrap().is_empty());
284284
merged.push_script_pubkey(&[], index);
285285
}
286286

0 commit comments

Comments
 (0)