Skip to content

Commit

Permalink
feat: testnet4 is supported with the latest rust-bitcoin
Browse files Browse the repository at this point in the history
  • Loading branch information
BinChengZhao committed Dec 26, 2024
1 parent ebc9312 commit aca19c3
Show file tree
Hide file tree
Showing 12 changed files with 312 additions and 46 deletions.
138 changes: 123 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ arraydeque = "0.5.1"
arrayref = "0.3.6"
base64 = "0.22"
bincode = "1.3.1"
bitcoin = { version = "0.31", features = ["serde"] }
bitcoin = { version = "0.32.5", features = [ "serde" ] }
bitcoin-io = "0.1.2"
clap = "2.33.3"
crossbeam-channel = "0.5.0"
dirs = "5.0.1"
elements = { version = "0.24", features = ["serde"], optional = true }
elements = { version = "0.25.1", features = ["serde"], optional = true }
error-chain = "0.12.4"
glob = "0.3"
hex = { package = "hex-conservative", version = "0.1.1" }
hex = { package = "hex-conservative", version = "0.2.1" }
itertools = "0.12"
lazy_static = "1.3.0"
libc = "0.2.81"
Expand Down
4 changes: 2 additions & 2 deletions src/bin/tx-fingerprint-stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn main() {
use bitcoin::blockdata::script::ScriptBuf;
use bitcoin::consensus::encode::deserialize;
use electrs::{
chain::Transaction,
chain::{Transaction, TxOperations},
config::Config,
daemon::Daemon,
metrics::Metrics,
Expand Down Expand Up @@ -62,7 +62,7 @@ fn main() {
}

let tx: Transaction = deserialize(&value).expect("failed to parse Transaction");
let txid = tx.txid();
let txid = TxOperations::txid(&tx);

iter.next();

Expand Down
46 changes: 46 additions & 0 deletions src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub enum Network {
#[cfg(not(feature = "liquid"))]
Testnet,
#[cfg(not(feature = "liquid"))]
Testnet4,
#[cfg(not(feature = "liquid"))]
Regtest,
#[cfg(not(feature = "liquid"))]
Signet,
Expand Down Expand Up @@ -97,6 +99,7 @@ impl Network {
return vec![
"mainnet".to_string(),
"testnet".to_string(),
"testnet4".to_string(),
"regtest".to_string(),
"signet".to_string(),
];
Expand All @@ -110,6 +113,27 @@ impl Network {
}
}

/// Because `rust-bitcoin` uses the `txid` function will cause a warning,
/// Need to use `compute_txid` instead,
/// So abstract a trait to handle the access of `txid`
pub trait TxOperations {
fn txid(&self) -> Txid;
}

#[cfg(not(feature = "liquid"))]
impl TxOperations for Transaction {
fn txid(&self) -> Txid {
self.compute_txid()
}
}

#[cfg(feature = "liquid")]
impl TxOperations for Transaction {
fn txid(&self) -> Txid {
Transaction::txid(self)
}
}

pub fn genesis_hash(network: Network) -> BlockHash {
#[cfg(not(feature = "liquid"))]
return bitcoin_genesis_hash(network.into());
Expand All @@ -121,8 +145,12 @@ pub fn bitcoin_genesis_hash(network: BNetwork) -> bitcoin::BlockHash {
lazy_static! {
static ref BITCOIN_GENESIS: bitcoin::BlockHash =
genesis_block(BNetwork::Bitcoin).block_hash();
// TESTNET_GENESIS is BlockHash of testnet3
static ref TESTNET_GENESIS: bitcoin::BlockHash =
genesis_block(BNetwork::Testnet).block_hash();
// TESTNET4_GENESIS is BlockHash of testnet4
static ref TESTNET4_GENESIS: bitcoin::BlockHash =
genesis_block(BNetwork::Testnet4).block_hash();
static ref REGTEST_GENESIS: bitcoin::BlockHash =
genesis_block(BNetwork::Regtest).block_hash();
static ref SIGNET_GENESIS: bitcoin::BlockHash =
Expand All @@ -131,6 +159,7 @@ pub fn bitcoin_genesis_hash(network: BNetwork) -> bitcoin::BlockHash {
match network {
BNetwork::Bitcoin => *BITCOIN_GENESIS,
BNetwork::Testnet => *TESTNET_GENESIS,
BNetwork::Testnet4 => *TESTNET4_GENESIS,
BNetwork::Regtest => *REGTEST_GENESIS,
BNetwork::Signet => *SIGNET_GENESIS,
_ => panic!("unknown network {:?}", network),
Expand Down Expand Up @@ -165,6 +194,8 @@ impl From<&str> for Network {
#[cfg(not(feature = "liquid"))]
"testnet" => Network::Testnet,
#[cfg(not(feature = "liquid"))]
"testnet4" => Network::Testnet4,
#[cfg(not(feature = "liquid"))]
"regtest" => Network::Regtest,
#[cfg(not(feature = "liquid"))]
"signet" => Network::Signet,
Expand All @@ -187,6 +218,7 @@ impl From<Network> for BNetwork {
match network {
Network::Bitcoin => BNetwork::Bitcoin,
Network::Testnet => BNetwork::Testnet,
Network::Testnet4 => BNetwork::Testnet4,
Network::Regtest => BNetwork::Regtest,
Network::Signet => BNetwork::Signet,
}
Expand All @@ -199,9 +231,23 @@ impl From<BNetwork> for Network {
match network {
BNetwork::Bitcoin => Network::Bitcoin,
BNetwork::Testnet => Network::Testnet,
BNetwork::Testnet4 => Network::Testnet4,
BNetwork::Regtest => Network::Regtest,
BNetwork::Signet => Network::Signet,
_ => panic!("unknown network {:?}", network),
}
}
}

#[cfg(not(feature = "liquid"))]
impl From<Network> for &'static bitcoin::params::Params {
fn from(network: Network) -> Self {
match network {
Network::Bitcoin => &bitcoin::params::MAINNET,
Network::Testnet => &bitcoin::params::TESTNET3,
Network::Testnet4 => &bitcoin::params::TESTNET4,
Network::Regtest => &bitcoin::params::REGTEST,
Network::Signet => &bitcoin::params::SIGNET,
}
}
}
Loading

0 comments on commit aca19c3

Please sign in to comment.