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 Jul 4, 2024
1 parent e11cd56 commit 25a891d
Show file tree
Hide file tree
Showing 15 changed files with 290 additions and 57 deletions.
113 changes: 107 additions & 6 deletions Cargo.lock

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

21 changes: 19 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,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.0-rc1", 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 }
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 Expand Up @@ -77,3 +78,19 @@ rev = "d3792352992a539afffbe11501d1aff9fd5b919d" # add-peer branch
[patch.crates-io.electrumd]
git = "https://github.com/shesek/electrumd"
rev = "996fe2a8e563bc1bde6bbc2e0c2a2f4421abcdbc"

[patch.crates-io.bitcoin]
git = "https://github.com/rust-bitcoin/rust-bitcoin.git"
rev = "refs/pull/2945/head"

[patch.crates-io.bitcoin-units]
git = "https://github.com/rust-bitcoin/rust-bitcoin.git"
rev = "refs/pull/2945/head"

[patch.crates-io.bitcoin_hashes]
git = "https://github.com/rust-bitcoin/rust-bitcoin.git"
rev = "refs/pull/2945/head"

[patch.crates-io.bitcoin-internals]
git = "https://github.com/rust-bitcoin/rust-bitcoin.git"
rev = "refs/pull/2945/head"
2 changes: 1 addition & 1 deletion src/bin/tx-fingerprint-stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn main() {
}

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

iter.next();

Expand Down
46 changes: 39 additions & 7 deletions src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub use {
},
};

use bitcoin::blockdata::constants::genesis_block;
pub use bitcoin::network::Network as BNetwork;
use bitcoin::{blockdata::constants::genesis_block, TestnetVersion as BTestnetVersion};

#[cfg(not(feature = "liquid"))]
pub type Value = u64;
Expand All @@ -27,7 +27,7 @@ pub enum Network {
#[cfg(not(feature = "liquid"))]
Bitcoin,
#[cfg(not(feature = "liquid"))]
Testnet,
Testnet(TestnetVersion),
#[cfg(not(feature = "liquid"))]
Regtest,
#[cfg(not(feature = "liquid"))]
Expand All @@ -41,6 +41,15 @@ pub enum Network {
LiquidRegtest,
}

#[derive(Debug, Copy, Clone, PartialEq, Hash, Serialize, Ord, PartialOrd, Eq)]
pub enum TestnetVersion {
/// Testnet version 3.
V3,
/// Testnet version 4.
/// This is the latest testnet version.
V4,
}

impl Network {
#[cfg(not(feature = "liquid"))]
pub fn magic(self) -> u32 {
Expand Down Expand Up @@ -97,6 +106,7 @@ impl Network {
return vec![
"mainnet".to_string(),
"testnet".to_string(),
"testnet4".to_string(),
"regtest".to_string(),
"signet".to_string(),
];
Expand All @@ -121,16 +131,21 @@ 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();
genesis_block(BNetwork::Testnet(BTestnetVersion::V3)).block_hash();
// TESTNET4_GENESIS is BlockHash of testnet4
static ref TESTNET4_GENESIS: bitcoin::BlockHash =
genesis_block(BNetwork::Testnet(BTestnetVersion::V4)).block_hash();
static ref REGTEST_GENESIS: bitcoin::BlockHash =
genesis_block(BNetwork::Regtest).block_hash();
static ref SIGNET_GENESIS: bitcoin::BlockHash =
genesis_block(BNetwork::Signet).block_hash();
}
match network {
BNetwork::Bitcoin => *BITCOIN_GENESIS,
BNetwork::Testnet => *TESTNET_GENESIS,
BNetwork::Testnet(BTestnetVersion::V3) => *TESTNET_GENESIS,
BNetwork::Testnet(BTestnetVersion::V4) => *TESTNET4_GENESIS,
BNetwork::Regtest => *REGTEST_GENESIS,
BNetwork::Signet => *SIGNET_GENESIS,
_ => panic!("unknown network {:?}", network),
Expand Down Expand Up @@ -163,7 +178,9 @@ impl From<&str> for Network {
#[cfg(not(feature = "liquid"))]
"mainnet" => Network::Bitcoin,
#[cfg(not(feature = "liquid"))]
"testnet" => Network::Testnet,
"testnet" => Network::Testnet(TestnetVersion::V3),
#[cfg(not(feature = "liquid"))]
"testnet4" => Network::Testnet(TestnetVersion::V4),
#[cfg(not(feature = "liquid"))]
"regtest" => Network::Regtest,
#[cfg(not(feature = "liquid"))]
Expand All @@ -186,7 +203,8 @@ impl From<Network> for BNetwork {
fn from(network: Network) -> Self {
match network {
Network::Bitcoin => BNetwork::Bitcoin,
Network::Testnet => BNetwork::Testnet,
Network::Testnet(TestnetVersion::V3) => BNetwork::Testnet(BTestnetVersion::V3),
Network::Testnet(TestnetVersion::V4) => BNetwork::Testnet(BTestnetVersion::V4),
Network::Regtest => BNetwork::Regtest,
Network::Signet => BNetwork::Signet,
}
Expand All @@ -198,10 +216,24 @@ impl From<BNetwork> for Network {
fn from(network: BNetwork) -> Self {
match network {
BNetwork::Bitcoin => Network::Bitcoin,
BNetwork::Testnet => Network::Testnet,
BNetwork::Testnet(BTestnetVersion::V3) => Network::Testnet(TestnetVersion::V3),
BNetwork::Testnet(BTestnetVersion::V4) => Network::Testnet(TestnetVersion::V4),
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(TestnetVersion::V3) => &bitcoin::params::TESTNET,
Network::Testnet(TestnetVersion::V4) => &bitcoin::params::TESTNET4,
Network::Regtest => &bitcoin::params::REGTEST,
Network::Signet => &bitcoin::params::SIGNET,
}
}
}
Loading

0 comments on commit 25a891d

Please sign in to comment.