Skip to content
This repository was archived by the owner on Mar 14, 2025. It is now read-only.

Commit 016583c

Browse files
authored
fix: default output selection without a runestone (#23)
1 parent 69559e0 commit 016583c

File tree

3 files changed

+27
-27
lines changed

3 files changed

+27
-27
lines changed

src/db/cache/index_cache.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ impl IndexCache {
100100
ctx,
101101
)
102102
.await;
103+
#[cfg(not(feature = "release"))]
104+
{
105+
for (rune_id, balances) in input_runes.iter() {
106+
try_debug!(ctx, "INPUT {rune_id} {balances:?} {location}");
107+
}
108+
if input_runes.len() > 0 {
109+
try_debug!(ctx, "First output: {first_eligible_output:?}, total_outputs: {total_outputs}");
110+
}
111+
}
103112
self.tx_cache = TransactionCache::new(
104113
location,
105114
input_runes,
@@ -126,7 +135,9 @@ impl IndexCache {
126135
ctx: &Context,
127136
) {
128137
try_debug!(ctx, "{:?} {}", runestone, self.tx_cache.location);
129-
self.tx_cache.apply_runestone_pointer(runestone, ctx);
138+
if let Some(new_pointer) = runestone.pointer {
139+
self.tx_cache.output_pointer = Some(new_pointer);
140+
}
130141
}
131142

132143
pub async fn apply_cenotaph(

src/db/cache/transaction_cache.rs

+13-24
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55

66
use bitcoin::ScriptBuf;
77
use chainhook_sdk::utils::Context;
8-
use ordinals::{Cenotaph, Edict, Etching, Rune, RuneId, Runestone};
8+
use ordinals::{Cenotaph, Edict, Etching, Rune, RuneId};
99

1010
use crate::{
1111
db::{
@@ -30,20 +30,20 @@ pub struct InputRuneBalance {
3030
/// Holds cached data relevant to a single transaction during indexing.
3131
pub struct TransactionCache {
3232
pub location: TransactionLocation,
33-
/// Index of the ledger entry we're inserting next for this transaction.
33+
/// Sequential index of the ledger entry we're inserting next for this transaction. Will be increased with each generated
34+
/// entry.
3435
next_event_index: u32,
35-
/// Rune etched during this transaction
36+
/// Rune etched during this transaction, if any.
3637
pub etching: Option<DbRune>,
37-
/// The output where all unallocated runes will be transferred to.
38-
pointer: Option<u32>,
38+
/// The output where all unallocated runes will be transferred to. Set to the first eligible output by default but can be
39+
/// overridden by a Runestone.
40+
pub output_pointer: Option<u32>,
3941
/// Holds input runes for the current transaction (input to this tx, premined or minted). Balances in the vector are in the
4042
/// order in which they were input to this transaction.
4143
input_runes: HashMap<RuneId, VecDeque<InputRuneBalance>>,
4244
/// Non-OP_RETURN outputs in this transaction
4345
eligible_outputs: HashMap<u32, ScriptBuf>,
44-
/// Index of the output that should receive unallocated runes if there is no `pointer` present.
45-
first_eligible_output: Option<u32>,
46-
/// Total outputs contained in this transaction, including OP_RETURN outputs
46+
/// Total outputs contained in this transaction, including non-eligible outputs.
4747
total_outputs: u32,
4848
}
4949

@@ -59,25 +59,13 @@ impl TransactionCache {
5959
location,
6060
next_event_index: 0,
6161
etching: None,
62-
pointer: None,
62+
output_pointer: first_eligible_output,
6363
input_runes,
6464
eligible_outputs,
65-
first_eligible_output,
6665
total_outputs,
6766
}
6867
}
6968

70-
/// Takes the runestone's output pointer and keeps a record of eligible outputs to send runes to.
71-
pub fn apply_runestone_pointer(&mut self, runestone: &Runestone, _ctx: &Context) {
72-
self.pointer = if runestone.pointer.is_some() {
73-
runestone.pointer
74-
} else if self.first_eligible_output.is_some() {
75-
self.first_eligible_output
76-
} else {
77-
None
78-
};
79-
}
80-
8169
/// Burns the rune balances input to this transaction.
8270
pub fn apply_cenotaph_input_burn(&mut self, _cenotaph: &Cenotaph) -> Vec<DbLedgerEntry> {
8371
let mut results = vec![];
@@ -104,20 +92,21 @@ impl TransactionCache {
10492
pub fn allocate_remaining_balances(&mut self, ctx: &Context) -> Vec<DbLedgerEntry> {
10593
let mut results = vec![];
10694
for (rune_id, unallocated) in self.input_runes.iter_mut() {
107-
#[cfg(feature = "debug")]
95+
#[cfg(not(feature = "release"))]
10896
for input in unallocated.iter() {
10997
try_debug!(
11098
ctx,
111-
"Assign unallocated {} {:?} ({}) {}",
99+
"Assign unallocated {} to pointer {:?} {:?} ({}) {}",
112100
rune_id,
101+
self.output_pointer,
113102
input.address,
114103
input.amount,
115104
self.location
116105
);
117106
}
118107
results.extend(move_rune_balance_to_output(
119108
&self.location,
120-
self.pointer,
109+
self.output_pointer,
121110
rune_id,
122111
unallocated,
123112
&self.eligible_outputs,

src/db/index.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ pub fn get_rune_genesis_block_height(network: Network) -> u64 {
2727
}
2828
}
2929

30-
/// Transforms a Bitcoin transaction from a Chainhook format to a rust bitcoin format so it can be consumed by ord. Also, takes
31-
/// all non-OP_RETURN outputs and returns them so they can be used later to receive runes.
30+
/// Transforms a Bitcoin transaction from a Chainhook format to a rust bitcoin crate format so it can be parsed by the ord crate
31+
/// to look for `Artifact`s. Also, takes all non-OP_RETURN outputs and returns them so they can be used later to receive runes.
3232
fn bitcoin_tx_from_chainhook_tx(
3333
block: &BitcoinBlockData,
3434
tx: &BitcoinTransactionData,

0 commit comments

Comments
 (0)