Skip to content

Commit

Permalink
fix: default output selection without a runestone (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr authored Jul 8, 2024
1 parent 69559e0 commit 016583c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
13 changes: 12 additions & 1 deletion src/db/cache/index_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ impl IndexCache {
ctx,
)
.await;
#[cfg(not(feature = "release"))]
{
for (rune_id, balances) in input_runes.iter() {
try_debug!(ctx, "INPUT {rune_id} {balances:?} {location}");
}
if input_runes.len() > 0 {
try_debug!(ctx, "First output: {first_eligible_output:?}, total_outputs: {total_outputs}");
}
}
self.tx_cache = TransactionCache::new(
location,
input_runes,
Expand All @@ -126,7 +135,9 @@ impl IndexCache {
ctx: &Context,
) {
try_debug!(ctx, "{:?} {}", runestone, self.tx_cache.location);
self.tx_cache.apply_runestone_pointer(runestone, ctx);
if let Some(new_pointer) = runestone.pointer {
self.tx_cache.output_pointer = Some(new_pointer);
}
}

pub async fn apply_cenotaph(
Expand Down
37 changes: 13 additions & 24 deletions src/db/cache/transaction_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{

use bitcoin::ScriptBuf;
use chainhook_sdk::utils::Context;
use ordinals::{Cenotaph, Edict, Etching, Rune, RuneId, Runestone};
use ordinals::{Cenotaph, Edict, Etching, Rune, RuneId};

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

Expand All @@ -59,25 +59,13 @@ impl TransactionCache {
location,
next_event_index: 0,
etching: None,
pointer: None,
output_pointer: first_eligible_output,
input_runes,
eligible_outputs,
first_eligible_output,
total_outputs,
}
}

/// Takes the runestone's output pointer and keeps a record of eligible outputs to send runes to.
pub fn apply_runestone_pointer(&mut self, runestone: &Runestone, _ctx: &Context) {
self.pointer = if runestone.pointer.is_some() {
runestone.pointer
} else if self.first_eligible_output.is_some() {
self.first_eligible_output
} else {
None
};
}

/// Burns the rune balances input to this transaction.
pub fn apply_cenotaph_input_burn(&mut self, _cenotaph: &Cenotaph) -> Vec<DbLedgerEntry> {
let mut results = vec![];
Expand All @@ -104,20 +92,21 @@ impl TransactionCache {
pub fn allocate_remaining_balances(&mut self, ctx: &Context) -> Vec<DbLedgerEntry> {
let mut results = vec![];
for (rune_id, unallocated) in self.input_runes.iter_mut() {
#[cfg(feature = "debug")]
#[cfg(not(feature = "release"))]
for input in unallocated.iter() {
try_debug!(
ctx,
"Assign unallocated {} {:?} ({}) {}",
"Assign unallocated {} to pointer {:?} {:?} ({}) {}",
rune_id,
self.output_pointer,
input.address,
input.amount,
self.location
);
}
results.extend(move_rune_balance_to_output(
&self.location,
self.pointer,
self.output_pointer,
rune_id,
unallocated,
&self.eligible_outputs,
Expand Down
4 changes: 2 additions & 2 deletions src/db/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ pub fn get_rune_genesis_block_height(network: Network) -> u64 {
}
}

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

0 comments on commit 016583c

Please sign in to comment.