mirror of
https://github.com/alexgo-io/bitcoin-indexer.git
synced 2026-06-14 16:39:27 +08:00
fix: streamline txid handling
This commit is contained in:
@@ -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,
|
||||
})
|
||||
@@ -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;
|
||||
|
||||
@@ -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)
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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 {
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -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 {
|
||||
@@ -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.
|
||||
|
||||
@@ -407,7 +407,7 @@ 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.
|
||||
@@ -415,6 +415,16 @@ pub struct TransactionIdentifier {
|
||||
}
|
||||
|
||||
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..]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user