Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit aca19c3

Browse files
committedDec 26, 2024
feat: testnet4 is supported with the latest rust-bitcoin
1 parent ebc9312 commit aca19c3

12 files changed

+312
-46
lines changed
 

‎Cargo.lock

+123-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ arraydeque = "0.5.1"
2222
arrayref = "0.3.6"
2323
base64 = "0.22"
2424
bincode = "1.3.1"
25-
bitcoin = { version = "0.31", features = ["serde"] }
25+
bitcoin = { version = "0.32.5", features = [ "serde" ] }
26+
bitcoin-io = "0.1.2"
2627
clap = "2.33.3"
2728
crossbeam-channel = "0.5.0"
2829
dirs = "5.0.1"
29-
elements = { version = "0.24", features = ["serde"], optional = true }
30+
elements = { version = "0.25.1", features = ["serde"], optional = true }
3031
error-chain = "0.12.4"
3132
glob = "0.3"
32-
hex = { package = "hex-conservative", version = "0.1.1" }
33+
hex = { package = "hex-conservative", version = "0.2.1" }
3334
itertools = "0.12"
3435
lazy_static = "1.3.0"
3536
libc = "0.2.81"

‎src/bin/tx-fingerprint-stats.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn main() {
1212
use bitcoin::blockdata::script::ScriptBuf;
1313
use bitcoin::consensus::encode::deserialize;
1414
use electrs::{
15-
chain::Transaction,
15+
chain::{Transaction, TxOperations},
1616
config::Config,
1717
daemon::Daemon,
1818
metrics::Metrics,
@@ -62,7 +62,7 @@ fn main() {
6262
}
6363

6464
let tx: Transaction = deserialize(&value).expect("failed to parse Transaction");
65-
let txid = tx.txid();
65+
let txid = TxOperations::txid(&tx);
6666

6767
iter.next();
6868

‎src/chain.rs

+46
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ pub enum Network {
2929
#[cfg(not(feature = "liquid"))]
3030
Testnet,
3131
#[cfg(not(feature = "liquid"))]
32+
Testnet4,
33+
#[cfg(not(feature = "liquid"))]
3234
Regtest,
3335
#[cfg(not(feature = "liquid"))]
3436
Signet,
@@ -97,6 +99,7 @@ impl Network {
9799
return vec![
98100
"mainnet".to_string(),
99101
"testnet".to_string(),
102+
"testnet4".to_string(),
100103
"regtest".to_string(),
101104
"signet".to_string(),
102105
];
@@ -110,6 +113,27 @@ impl Network {
110113
}
111114
}
112115

116+
/// Because `rust-bitcoin` uses the `txid` function will cause a warning,
117+
/// Need to use `compute_txid` instead,
118+
/// So abstract a trait to handle the access of `txid`
119+
pub trait TxOperations {
120+
fn txid(&self) -> Txid;
121+
}
122+
123+
#[cfg(not(feature = "liquid"))]
124+
impl TxOperations for Transaction {
125+
fn txid(&self) -> Txid {
126+
self.compute_txid()
127+
}
128+
}
129+
130+
#[cfg(feature = "liquid")]
131+
impl TxOperations for Transaction {
132+
fn txid(&self) -> Txid {
133+
Transaction::txid(self)
134+
}
135+
}
136+
113137
pub fn genesis_hash(network: Network) -> BlockHash {
114138
#[cfg(not(feature = "liquid"))]
115139
return bitcoin_genesis_hash(network.into());
@@ -121,8 +145,12 @@ pub fn bitcoin_genesis_hash(network: BNetwork) -> bitcoin::BlockHash {
121145
lazy_static! {
122146
static ref BITCOIN_GENESIS: bitcoin::BlockHash =
123147
genesis_block(BNetwork::Bitcoin).block_hash();
148+
// TESTNET_GENESIS is BlockHash of testnet3
124149
static ref TESTNET_GENESIS: bitcoin::BlockHash =
125150
genesis_block(BNetwork::Testnet).block_hash();
151+
// TESTNET4_GENESIS is BlockHash of testnet4
152+
static ref TESTNET4_GENESIS: bitcoin::BlockHash =
153+
genesis_block(BNetwork::Testnet4).block_hash();
126154
static ref REGTEST_GENESIS: bitcoin::BlockHash =
127155
genesis_block(BNetwork::Regtest).block_hash();
128156
static ref SIGNET_GENESIS: bitcoin::BlockHash =
@@ -131,6 +159,7 @@ pub fn bitcoin_genesis_hash(network: BNetwork) -> bitcoin::BlockHash {
131159
match network {
132160
BNetwork::Bitcoin => *BITCOIN_GENESIS,
133161
BNetwork::Testnet => *TESTNET_GENESIS,
162+
BNetwork::Testnet4 => *TESTNET4_GENESIS,
134163
BNetwork::Regtest => *REGTEST_GENESIS,
135164
BNetwork::Signet => *SIGNET_GENESIS,
136165
_ => panic!("unknown network {:?}", network),
@@ -165,6 +194,8 @@ impl From<&str> for Network {
165194
#[cfg(not(feature = "liquid"))]
166195
"testnet" => Network::Testnet,
167196
#[cfg(not(feature = "liquid"))]
197+
"testnet4" => Network::Testnet4,
198+
#[cfg(not(feature = "liquid"))]
168199
"regtest" => Network::Regtest,
169200
#[cfg(not(feature = "liquid"))]
170201
"signet" => Network::Signet,
@@ -187,6 +218,7 @@ impl From<Network> for BNetwork {
187218
match network {
188219
Network::Bitcoin => BNetwork::Bitcoin,
189220
Network::Testnet => BNetwork::Testnet,
221+
Network::Testnet4 => BNetwork::Testnet4,
190222
Network::Regtest => BNetwork::Regtest,
191223
Network::Signet => BNetwork::Signet,
192224
}
@@ -199,9 +231,23 @@ impl From<BNetwork> for Network {
199231
match network {
200232
BNetwork::Bitcoin => Network::Bitcoin,
201233
BNetwork::Testnet => Network::Testnet,
234+
BNetwork::Testnet4 => Network::Testnet4,
202235
BNetwork::Regtest => Network::Regtest,
203236
BNetwork::Signet => Network::Signet,
204237
_ => panic!("unknown network {:?}", network),
205238
}
206239
}
207240
}
241+
242+
#[cfg(not(feature = "liquid"))]
243+
impl From<Network> for &'static bitcoin::params::Params {
244+
fn from(network: Network) -> Self {
245+
match network {
246+
Network::Bitcoin => &bitcoin::params::MAINNET,
247+
Network::Testnet => &bitcoin::params::TESTNET3,
248+
Network::Testnet4 => &bitcoin::params::TESTNET4,
249+
Network::Regtest => &bitcoin::params::REGTEST,
250+
Network::Signet => &bitcoin::params::SIGNET,
251+
}
252+
}
253+
}

0 commit comments

Comments
 (0)
Please sign in to comment.