Skip to content

Commit

Permalink
Merge pull request #91 from mempool/wiz/add-testnet4
Browse files Browse the repository at this point in the history
Add testnet4 support
  • Loading branch information
wiz authored May 6, 2024
2 parents cd9efdf + 17dc10e commit e3240dc
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/bin/electrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ fn run_server(config: Arc<Config>) -> Result<()> {
config.daemon_rpc_addr,
config.cookie_getter(),
config.network_type,
config.magic,
signal.clone(),
&metrics,
)?);
Expand Down
1 change: 1 addition & 0 deletions src/bin/tx-fingerprint-stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ fn main() {
config.daemon_rpc_addr,
config.cookie_getter(),
config.network_type,
config.magic,
signal,
&metrics,
)
Expand Down
20 changes: 15 additions & 5 deletions src/chain.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::str::FromStr;

#[cfg(not(feature = "liquid"))] // use regular Bitcoin data structures
pub use bitcoin::{
blockdata::{opcodes, script, witness::Witness},
Expand Down Expand Up @@ -32,6 +34,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 @@ -129,22 +133,25 @@ pub fn genesis_hash(network: Network) -> BlockHash {
return liquid_genesis_hash(network);
}

pub fn bitcoin_genesis_hash(network: BNetwork) -> bitcoin::BlockHash {
pub fn bitcoin_genesis_hash(network: Network) -> bitcoin::BlockHash {
lazy_static! {
static ref BITCOIN_GENESIS: bitcoin::BlockHash =
genesis_block(BNetwork::Bitcoin).block_hash();
static ref TESTNET_GENESIS: bitcoin::BlockHash =
genesis_block(BNetwork::Testnet).block_hash();
static ref TESTNET4_GENESIS: bitcoin::BlockHash =
BlockHash::from_str("00000000da84f2bafbbc53dee25a72ae507ff4914b867c565be350b0da8bf043").unwrap();
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::Regtest => *REGTEST_GENESIS,
BNetwork::Signet => *SIGNET_GENESIS,
Network::Bitcoin => *BITCOIN_GENESIS,
Network::Testnet => *TESTNET_GENESIS,
Network::Testnet4 => *TESTNET4_GENESIS,
Network::Regtest => *REGTEST_GENESIS,
Network::Signet => *SIGNET_GENESIS,
}
}

Expand Down Expand Up @@ -174,6 +181,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 @@ -196,6 +205,7 @@ impl From<Network> for BNetwork {
match network {
Network::Bitcoin => BNetwork::Bitcoin,
Network::Testnet => BNetwork::Testnet,
Network::Testnet4 => BNetwork::Testnet,
Network::Regtest => BNetwork::Regtest,
Network::Signet => BNetwork::Signet,
}
Expand Down
22 changes: 22 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct Config {
// See below for the documentation of each field:
pub log: stderrlog::StdErrLog,
pub network_type: Network,
pub magic: Option<u32>,
pub db_path: PathBuf,
pub daemon_dir: PathBuf,
pub blocks_dir: PathBuf,
Expand Down Expand Up @@ -137,6 +138,12 @@ impl Config {
.help(&network_help)
.takes_value(true),
)
.arg(
Arg::with_name("magic")
.long("magic")
.default_value("")
.takes_value(true),
)
.arg(
Arg::with_name("electrum_rpc_addr")
.long("electrum-rpc-addr")
Expand Down Expand Up @@ -328,6 +335,10 @@ impl Config {

let network_name = m.value_of("network").unwrap_or("mainnet");
let network_type = Network::from(network_name);
let magic: Option<u32> = m
.value_of("magic")
.filter(|s| !s.is_empty())
.map(|s| u32::from_str_radix(s, 16).expect("invalid network magic"));
let db_dir = Path::new(m.value_of("db_dir").unwrap_or("./db"));
let db_path = db_dir.join(network_name);

Expand All @@ -353,6 +364,8 @@ impl Config {
Network::Regtest => 18443,
#[cfg(not(feature = "liquid"))]
Network::Signet => 38332,
#[cfg(not(feature = "liquid"))]
Network::Testnet4 => 48332,

#[cfg(feature = "liquid")]
Network::Liquid => 7041,
Expand All @@ -365,6 +378,8 @@ impl Config {
#[cfg(not(feature = "liquid"))]
Network::Testnet => 60001,
#[cfg(not(feature = "liquid"))]
Network::Testnet4 => 40001,
#[cfg(not(feature = "liquid"))]
Network::Regtest => 60401,
#[cfg(not(feature = "liquid"))]
Network::Signet => 60601,
Expand All @@ -385,6 +400,8 @@ impl Config {
Network::Regtest => 3002,
#[cfg(not(feature = "liquid"))]
Network::Signet => 3003,
#[cfg(not(feature = "liquid"))]
Network::Testnet4 => 3004,

#[cfg(feature = "liquid")]
Network::Liquid => 3000,
Expand All @@ -401,6 +418,8 @@ impl Config {
#[cfg(not(feature = "liquid"))]
Network::Regtest => 24224,
#[cfg(not(feature = "liquid"))]
Network::Testnet4 => 44224,
#[cfg(not(feature = "liquid"))]
Network::Signet => 54224,

#[cfg(feature = "liquid")]
Expand Down Expand Up @@ -449,6 +468,8 @@ impl Config {
#[cfg(not(feature = "liquid"))]
Network::Testnet => daemon_dir.push("testnet3"),
#[cfg(not(feature = "liquid"))]
Network::Testnet4 => daemon_dir.push("testnet4"),
#[cfg(not(feature = "liquid"))]
Network::Regtest => daemon_dir.push("regtest"),
#[cfg(not(feature = "liquid"))]
Network::Signet => daemon_dir.push("signet"),
Expand Down Expand Up @@ -486,6 +507,7 @@ impl Config {
let config = Config {
log,
network_type,
magic,
db_path,
daemon_dir,
blocks_dir,
Expand Down
6 changes: 5 additions & 1 deletion src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ pub struct Daemon {
daemon_dir: PathBuf,
blocks_dir: PathBuf,
network: Network,
magic: Option<u32>,
conn: Mutex<Connection>,
message_id: Counter, // for monotonic JSONRPC 'id'
signal: Waiter,
Expand All @@ -300,13 +301,15 @@ impl Daemon {
daemon_rpc_addr: SocketAddr,
cookie_getter: Arc<dyn CookieGetter>,
network: Network,
magic: Option<u32>,
signal: Waiter,
metrics: &Metrics,
) -> Result<Daemon> {
let daemon = Daemon {
daemon_dir,
blocks_dir,
network,
magic,
conn: Mutex::new(Connection::new(
daemon_rpc_addr,
cookie_getter,
Expand Down Expand Up @@ -367,6 +370,7 @@ impl Daemon {
daemon_dir: self.daemon_dir.clone(),
blocks_dir: self.blocks_dir.clone(),
network: self.network,
magic: self.magic,
conn: Mutex::new(self.conn.lock().unwrap().reconnect()?),
message_id: Counter::new(),
signal: self.signal.clone(),
Expand All @@ -387,7 +391,7 @@ impl Daemon {
}

pub fn magic(&self) -> u32 {
self.network.magic()
self.magic.unwrap_or_else(|| self.network.magic())
}

fn call_jsonrpc(&self, method: &str, request: &Value) -> Result<Value> {
Expand Down
2 changes: 1 addition & 1 deletion src/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1668,7 +1668,7 @@ fn address_to_scripthash(addr: &str, network: Network) -> Result<FullHash, HttpE
// `addr_network` will be detected as Testnet for all of them.
addr_network == network
|| (addr_network == Network::Testnet
&& matches!(network, Network::Regtest | Network::Signet))
&& matches!(network, Network::Regtest | Network::Signet | Network::Testnet4))
};

#[cfg(feature = "liquid")]
Expand Down
8 changes: 7 additions & 1 deletion start
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ case "${1}" in
NETWORK=testnet
THREADS=$((NPROC / 6))
;;
testnet4)
NETWORK=testnet4
MAGIC=283f161c
THREADS=$((NPROC / 6))
;;
signet)
NETWORK=signet
THREADS=$((NPROC / 6))
Expand All @@ -60,7 +65,7 @@ case "${1}" in
THREADS=$((NPROC / 6))
;;
*)
echo "Usage: $0 (mainnet|testnet|signet|liquid|liquidtestnet)"
echo "Usage: $0 (mainnet|testnet|testnet4|signet|liquid|liquidtestnet)"
exit 1
;;
esac
Expand Down Expand Up @@ -148,6 +153,7 @@ do
--precache-threads "${THREADS}" \
--cookie "${RPC_USER}:${RPC_PASS}" \
--cors '*' \
--magic "${MAGIC}" \
--address-search \
--utxos-limit "${UTXOS_LIMIT}" \
--electrum-txs-limit "${ELECTRUM_TXS_LIMIT}" \
Expand Down

0 comments on commit e3240dc

Please sign in to comment.