Skip to content

Commit

Permalink
fix: streamline txid handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Ludo Galabru committed Jun 22, 2023
1 parent 810407d commit ad48351
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 31 deletions.
4 changes: 2 additions & 2 deletions components/chainhook-sdk/src/chainhooks/bitcoin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ pub fn serialize_bitcoin_transactions_to_json<'a>(
.iter()
.map(|input| {
json!({
"txin": format!("0x{}", input.previous_output.txid),
"txin": input.previous_output.txid.hash.to_string(),
"vout": input.previous_output.vout,
"sequence": input.sequence,
})
Expand Down Expand Up @@ -366,7 +366,7 @@ impl BitcoinPredicateType {
BitcoinPredicateType::Inputs(InputPredicate::Txid(predicate)) => {
// TODO(lgalabru): add support for transaction chainhing, if enabled
for input in tx.metadata.inputs.iter() {
if input.previous_output.txid.eq(&predicate.txid)
if input.previous_output.txid.hash.eq(&predicate.txid)
&& input.previous_output.vout.eq(&predicate.vout)
{
return true;
Expand Down
19 changes: 4 additions & 15 deletions components/chainhook-sdk/src/hord/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -949,9 +949,7 @@ pub fn format_satpoint_to_watch(

pub fn parse_satpoint_to_watch(outpoint_to_watch: &str) -> (TransactionIdentifier, usize, u64) {
let comps: Vec<&str> = outpoint_to_watch.split(":").collect();
let tx = TransactionIdentifier {
hash: format!("0x{}", comps[0]),
};
let tx = TransactionIdentifier::new(comps[0]);
let output_index = comps[1].to_string().parse::<usize>().unwrap();
let offset = comps[2].to_string().parse::<u64>().unwrap();
(tx, output_index, offset)
Expand All @@ -970,18 +968,14 @@ pub fn format_outpoint_to_watch(

pub fn parse_inscription_id(inscription_id: &str) -> (TransactionIdentifier, usize) {
let comps: Vec<&str> = inscription_id.split("i").collect();
let tx = TransactionIdentifier {
hash: format!("0x{}", comps[0]),
};
let tx = TransactionIdentifier::new(&comps[0]);
let output_index = comps[1].to_string().parse::<usize>().unwrap();
(tx, output_index)
}

pub fn parse_outpoint_to_watch(outpoint_to_watch: &str) -> (TransactionIdentifier, usize) {
let comps: Vec<&str> = outpoint_to_watch.split(":").collect();
let tx = TransactionIdentifier {
hash: format!("0x{}", comps[0]),
};
let tx = TransactionIdentifier::new(&comps[0]);
let output_index = comps[1].to_string().parse::<usize>().unwrap();
(tx, output_index)
}
Expand Down Expand Up @@ -1552,12 +1546,7 @@ impl LazyBlock {
// For each transaction input:
for input in tx.metadata.inputs.iter() {
// txin - 8 first bytes
let txin = {
let txid = hex::decode(&input.previous_output.txid[2..]).unwrap();
[
txid[0], txid[1], txid[2], txid[3], txid[4], txid[5], txid[6], txid[7],
]
};
let txin = input.previous_output.txid.get_8_hash_bytes();
buffer.write_all(&txin)?;
// txin's block height
let block_height = input.previous_output.block_height as u32;
Expand Down
20 changes: 9 additions & 11 deletions components/chainhook-sdk/src/hord/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ use crate::{

use self::db::{
find_inscription_with_id, find_latest_cursed_inscription_number_at_block_height,
find_latest_inscription_number_at_block_height, parse_satpoint_to_watch,
remove_entry_from_blocks, remove_entry_from_inscriptions, LazyBlock, LazyBlockTransaction,
TraversalResult, WatchedSatpoint,
find_latest_inscription_number_at_block_height, format_satpoint_to_watch,
parse_satpoint_to_watch, remove_entry_from_blocks, remove_entry_from_inscriptions, LazyBlock,
LazyBlockTransaction, TraversalResult, WatchedSatpoint,
};
use self::inscription::InscriptionParser;
use self::ord::inscription_id::InscriptionId;
Expand Down Expand Up @@ -482,11 +482,10 @@ pub fn update_storage_and_augment_bitcoin_block_with_inscription_reveal_data(
inscription.inscription_number = traversal.inscription_number;
inscription.transfers_pre_inscription = traversal.transfers;
inscription.inscription_fee = new_tx.metadata.fee;
inscription.satpoint_post_inscription = format!(
"{}:{}:{}",
traversal.transaction_identifier.hash,
inscription.satpoint_post_inscription = format_satpoint_to_watch(
&traversal.transaction_identifier,
traversal.output_index,
traversal.inscription_offset_intra_output
traversal.inscription_offset_intra_output,
);
if let Some(output) = new_tx.metadata.outputs.get(traversal.output_index) {
inscription.inscription_output_value = output.value;
Expand Down Expand Up @@ -642,10 +641,9 @@ pub fn update_storage_and_augment_bitcoin_block_with_inscription_transfer_data(

for input in new_tx.metadata.inputs.iter() {
// input.previous_output.txid
let outpoint_pre_transfer = format!(
"{}:{}",
&input.previous_output.txid[2..],
input.previous_output.vout
let outpoint_pre_transfer = format_outpoint_to_watch(
&input.previous_output.txid,
input.previous_output.vout as usize,
);

let entries = match storage {
Expand Down
2 changes: 1 addition & 1 deletion components/chainhook-sdk/src/indexer/bitcoin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ pub fn standardize_bitcoin_block(
sats_in += prevout.value.to_sat();
inputs.push(TxIn {
previous_output: OutPoint {
txid: format!("0x{}", txid.to_string()),
txid: TransactionIdentifier::new(&txid.to_string()),
vout,
block_height: prevout.height,
value: prevout.value.to_sat(),
Expand Down
4 changes: 3 additions & 1 deletion components/chainhook-types-rs/src/bitcoin.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::TransactionIdentifier;

/// A transaction input, which defines old coins to be consumed
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash, Serialize, Deserialize)]
pub struct TxIn {
Expand Down Expand Up @@ -32,7 +34,7 @@ pub struct TxOut {
#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct OutPoint {
/// The referenced transaction's txid.
pub txid: String,
pub txid: TransactionIdentifier,
/// The index of the referenced output in its transaction's vout.
pub vout: u32,
/// The value of the referenced.
Expand Down
12 changes: 11 additions & 1 deletion components/chainhook-types-rs/src/rosetta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,14 +407,24 @@ pub struct LockSTXData {

/// The transaction_identifier uniquely identifies a transaction in a particular
/// network and block or in the mempool.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, Hash, PartialOrd, Ord)]
pub struct TransactionIdentifier {
/// Any transactions that are attributable only to a block (ex: a block
/// event) should use the hash of the block as the identifier.
pub hash: String,
}

impl TransactionIdentifier {
pub fn new(txid: &str) -> Self {
let lowercased_txid = txid.to_lowercase();
Self {
hash: match lowercased_txid.starts_with("0x") {
true => lowercased_txid,
false => format!("0x{}", lowercased_txid),
},
}
}

pub fn get_hash_bytes_str(&self) -> &str {
&self.hash[2..]
}
Expand Down

0 comments on commit ad48351

Please sign in to comment.