|
| 1 | +use bitcoin::{blockdata::locktime::absolute::LockTime, Txid}; |
| 2 | +use dftx_rs::{Block, Transaction}; |
| 3 | +use log::debug; |
| 4 | + |
| 5 | +use super::BlockContext; |
| 6 | +use crate::{ |
| 7 | + indexer::Result, |
| 8 | + model::{ |
| 9 | + Transaction as TrasnactionMapper, TransactionVin, TransactionVinScript, TransactionVinVout, |
| 10 | + TransactionVinVoutScript, TransactionVout, TransactionVoutScript, |
| 11 | + }, |
| 12 | + repository::RepositoryOps, |
| 13 | + SERVICES, |
| 14 | +}; |
| 15 | + |
| 16 | +pub fn index_transactions(ctx: &BlockContext, tx: Transaction) -> Result<()> { |
| 17 | + debug!("[CreateTransaction] Indexing..."); |
| 18 | + let tx_id = tx.txid(); |
| 19 | + |
| 20 | + let lock_time_as_i32 = match tx.lock_time { |
| 21 | + LockTime::Blocks(value) => value.to_consensus_u32(), |
| 22 | + LockTime::Seconds(value) => value.to_consensus_u32(), |
| 23 | + }; |
| 24 | + let total_vout_value: u64 = tx.output.iter().map(|output| output.value.to_sat()).sum(); |
| 25 | + let weight = tx.weight(); |
| 26 | + let weight_i32 = weight.to_vbytes_ceil() as i32; |
| 27 | + |
| 28 | + let trx = TrasnactionMapper { |
| 29 | + id: tx_id, |
| 30 | + order: 0, |
| 31 | + block: ctx.clone(), |
| 32 | + txid: tx_id.to_string(), |
| 33 | + hash: ctx.hash.to_string(), |
| 34 | + version: tx.version.0, |
| 35 | + size: tx.total_size() as i32, |
| 36 | + v_size: tx.vsize() as i32, |
| 37 | + weight: weight_i32, |
| 38 | + total_vout_value: total_vout_value.to_string(), |
| 39 | + lock_time: lock_time_as_i32 as i32, |
| 40 | + vin_count: tx.input.len() as i32, |
| 41 | + vout_count: tx.output.len() as i32, |
| 42 | + }; |
| 43 | + // Index transaction |
| 44 | + SERVICES.transaction.by_id.put(&tx_id, &trx)?; |
| 45 | + // Indexing transaction vin |
| 46 | + for (vin_idx, vin) in tx.input.iter().enumerate() { |
| 47 | + let trx_vin = TransactionVin { |
| 48 | + id: format!("{}-{}", tx_id, vin_idx), |
| 49 | + txid: tx_id.to_string(), |
| 50 | + coinbase: vin.script_sig.to_string(), |
| 51 | + vout: TransactionVinVout { |
| 52 | + id: format!("{}-{}-vout", tx_id, vin_idx), |
| 53 | + txid: tx_id.to_string(), |
| 54 | + n: vin.previous_output.vout as i32, |
| 55 | + value: "0".to_string(), |
| 56 | + token_id: 0, |
| 57 | + script: TransactionVinVoutScript { |
| 58 | + hex: vin.script_sig.to_string(), |
| 59 | + }, |
| 60 | + }, |
| 61 | + script: TransactionVinScript { |
| 62 | + hex: vin.script_sig.to_string(), |
| 63 | + }, |
| 64 | + tx_in_witness: vec![], |
| 65 | + sequence: vin.sequence.to_string(), |
| 66 | + }; |
| 67 | + |
| 68 | + SERVICES.transaction.vin_by_id.put(&tx_id, &trx_vin)?; |
| 69 | + } |
| 70 | + // Index transaction vout |
| 71 | + for (vout_idx, vout) in tx.output.iter().enumerate() { |
| 72 | + let trx_vout = TransactionVout { |
| 73 | + id: format!("{}-{}", tx_id, vout_idx), |
| 74 | + txid: tx_id.to_string(), |
| 75 | + n: vout_idx as i32, |
| 76 | + value: vout.value.to_string(), |
| 77 | + token_id: 0, |
| 78 | + script: TransactionVoutScript { |
| 79 | + hex: vout.script_pubkey.to_string(), |
| 80 | + r#type: "pubkey".to_string(), |
| 81 | + }, |
| 82 | + }; |
| 83 | + SERVICES.transaction.vout_by_id.put(&tx_id, &trx_vout)?; |
| 84 | + // .put(&format!("{}-{}", tx_id, vout_idx), &trx_vout)?; //need |
| 85 | + } |
| 86 | + |
| 87 | + Ok(()) |
| 88 | +} |
| 89 | + |
| 90 | +pub fn invalidate_transaction(ctx: &BlockContext, tx: Txid, idx: usize) -> Result<()> { |
| 91 | + debug!("[CreateMasternode] Invalidating..."); |
| 92 | + SERVICES.transaction.by_id.delete(&tx)?; |
| 93 | + Ok(()) |
| 94 | +} |
0 commit comments