Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Jan 7, 2025
1 parent 1cc589d commit e4cdd4e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 31 deletions.
31 changes: 4 additions & 27 deletions counterparty-rs/src/indexer/bitcoin_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::{collections::HashMap, sync::Arc, thread::JoinHandle};
use crate::b58::b58_encode;
use crate::utils::script_to_address;
use bitcoin::{
consensus::deserialize,
consensus::serialize,
hashes::{hex::prelude::*, ripemd160, sha256, sha256d::Hash as Sha256dHash, Hash},
opcodes::all::{
Expand All @@ -20,7 +19,6 @@ use bitcoincore_rpc::{Auth, Client, RpcApi};
use crossbeam_channel::{bounded, select, unbounded, Receiver, Sender};
use crypto::rc4::Rc4;
use crypto::symmetriccipher::SynchronousStreamCipher;
use hex;

use super::{
block::{
Expand Down Expand Up @@ -106,8 +104,6 @@ impl ParseOutput {
}
}

use std::str;

fn parse_vout(
config: &Config,
key: Vec<u8>,
Expand Down Expand Up @@ -377,7 +373,7 @@ fn parse_vout(
}
}

fn create_transaction(
pub fn parse_transaction(
tx: &bitcoin::blockdata::transaction::Transaction,
config: &Config,
height: u32,
Expand Down Expand Up @@ -503,25 +499,11 @@ fn create_transaction(
}
}

pub fn parse_transaction(
tx_hex: &str,
config: &Config,
height: u32,
parse_vouts: bool,
) -> Transaction {
let decoded_tx = hex::decode(tx_hex).expect("Failed to decode hex string");

let transaction: bitcoin::blockdata::transaction::Transaction =
deserialize(&decoded_tx).expect("Failed to deserialize transaction");

return create_transaction(&transaction, config, height, parse_vouts);
}

impl ToBlock for Block {
fn to_block(&self, config: Config, height: u32) -> CrateBlock {
let mut transactions = Vec::new();
for tx in self.txdata.iter() {
transactions.push(create_transaction(tx, &config, height, true));
transactions.push(parse_transaction(tx, &config, height, true));
}
CrateBlock {
height,
Expand All @@ -539,19 +521,14 @@ impl ToBlock for Block {
}

pub fn parse_block(
hex: &str,
block: Block,
config: &Config,
height: u32,
parse_vouts: bool,
) -> Result<CrateBlock, Error> {
let decoded_block = hex::decode(hex)
.map_err(|e| Error::ParseVout(format!("Failed to decode hex string: {}", e)))?;
let block: Block = deserialize(&decoded_block)
.map_err(|e| Error::ParseVout(format!("Failed to deserialize block: {}", e)))?;

let mut transactions = Vec::new();
for tx in block.txdata.iter() {
transactions.push(create_transaction(tx, config, height, parse_vouts));
transactions.push(parse_transaction(tx, config, height, parse_vouts));
}
Ok(CrateBlock {
height,
Expand Down
23 changes: 19 additions & 4 deletions counterparty-rs/src/indexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ mod workers;

use std::thread::JoinHandle;

use bitcoin;
use bitcoin::consensus::deserialize;

use pyo3::prelude::*;
use types::pipeline::ChanOut;

Expand Down Expand Up @@ -102,8 +105,14 @@ impl Deserializer {
parse_vouts: bool,
py: Python<'_>,
) -> PyResult<PyObject> {
let tx = self::bitcoin_client::parse_transaction(tx_hex, &self.config, height, parse_vouts);
return Ok(tx.into_py(py));
let decoded_tx = hex::decode(tx_hex).expect("Failed to decode hex string");

let transaction: bitcoin::blockdata::transaction::Transaction =
deserialize(&decoded_tx).expect("Failed to deserialize transaction");

let deserialized_transaction = self::bitcoin_client::parse_transaction(&transaction, &self.config, height, parse_vouts);

return Ok(deserialized_transaction.into_py(py));
}

pub fn parse_block(
Expand All @@ -113,8 +122,14 @@ impl Deserializer {
parse_vouts: bool,
py: Python<'_>,
) -> PyResult<PyObject> {
let block = self::bitcoin_client::parse_block(block_hex, &self.config, height, parse_vouts);
return Ok(block?.into_py(py));
let decoded_block = hex::decode(block_hex)
.map_err(|e| Error::ParseVout(format!("Failed to decode hex string: {}", e)))?;

let block: bitcoin::Block = deserialize(&decoded_block)
.map_err(|e| Error::ParseVout(format!("Failed to deserialize block: {}", e)))?;

let deserialized_block = self::bitcoin_client::parse_block(block, &self.config, height, parse_vouts);
return Ok(deserialized_block?.into_py(py));
}
}

Expand Down

0 comments on commit e4cdd4e

Please sign in to comment.