Skip to content

Commit e3240dc

Browse files
authored
Merge pull request Blockstream#91 from mempool/wiz/add-testnet4
Add testnet4 support
2 parents cd9efdf + 17dc10e commit e3240dc

File tree

7 files changed

+52
-8
lines changed

7 files changed

+52
-8
lines changed

src/bin/electrs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ fn run_server(config: Arc<Config>) -> Result<()> {
5050
config.daemon_rpc_addr,
5151
config.cookie_getter(),
5252
config.network_type,
53+
config.magic,
5354
signal.clone(),
5455
&metrics,
5556
)?);

src/bin/tx-fingerprint-stats.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ fn main() {
3535
config.daemon_rpc_addr,
3636
config.cookie_getter(),
3737
config.network_type,
38+
config.magic,
3839
signal,
3940
&metrics,
4041
)

src/chain.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::str::FromStr;
2+
13
#[cfg(not(feature = "liquid"))] // use regular Bitcoin data structures
24
pub use bitcoin::{
35
blockdata::{opcodes, script, witness::Witness},
@@ -32,6 +34,8 @@ pub enum Network {
3234
#[cfg(not(feature = "liquid"))]
3335
Testnet,
3436
#[cfg(not(feature = "liquid"))]
37+
Testnet4,
38+
#[cfg(not(feature = "liquid"))]
3539
Regtest,
3640
#[cfg(not(feature = "liquid"))]
3741
Signet,
@@ -129,22 +133,25 @@ pub fn genesis_hash(network: Network) -> BlockHash {
129133
return liquid_genesis_hash(network);
130134
}
131135

132-
pub fn bitcoin_genesis_hash(network: BNetwork) -> bitcoin::BlockHash {
136+
pub fn bitcoin_genesis_hash(network: Network) -> bitcoin::BlockHash {
133137
lazy_static! {
134138
static ref BITCOIN_GENESIS: bitcoin::BlockHash =
135139
genesis_block(BNetwork::Bitcoin).block_hash();
136140
static ref TESTNET_GENESIS: bitcoin::BlockHash =
137141
genesis_block(BNetwork::Testnet).block_hash();
142+
static ref TESTNET4_GENESIS: bitcoin::BlockHash =
143+
BlockHash::from_str("00000000da84f2bafbbc53dee25a72ae507ff4914b867c565be350b0da8bf043").unwrap();
138144
static ref REGTEST_GENESIS: bitcoin::BlockHash =
139145
genesis_block(BNetwork::Regtest).block_hash();
140146
static ref SIGNET_GENESIS: bitcoin::BlockHash =
141147
genesis_block(BNetwork::Signet).block_hash();
142148
}
143149
match network {
144-
BNetwork::Bitcoin => *BITCOIN_GENESIS,
145-
BNetwork::Testnet => *TESTNET_GENESIS,
146-
BNetwork::Regtest => *REGTEST_GENESIS,
147-
BNetwork::Signet => *SIGNET_GENESIS,
150+
Network::Bitcoin => *BITCOIN_GENESIS,
151+
Network::Testnet => *TESTNET_GENESIS,
152+
Network::Testnet4 => *TESTNET4_GENESIS,
153+
Network::Regtest => *REGTEST_GENESIS,
154+
Network::Signet => *SIGNET_GENESIS,
148155
}
149156
}
150157

@@ -174,6 +181,8 @@ impl From<&str> for Network {
174181
#[cfg(not(feature = "liquid"))]
175182
"testnet" => Network::Testnet,
176183
#[cfg(not(feature = "liquid"))]
184+
"testnet4" => Network::Testnet4,
185+
#[cfg(not(feature = "liquid"))]
177186
"regtest" => Network::Regtest,
178187
#[cfg(not(feature = "liquid"))]
179188
"signet" => Network::Signet,
@@ -196,6 +205,7 @@ impl From<Network> for BNetwork {
196205
match network {
197206
Network::Bitcoin => BNetwork::Bitcoin,
198207
Network::Testnet => BNetwork::Testnet,
208+
Network::Testnet4 => BNetwork::Testnet,
199209
Network::Regtest => BNetwork::Regtest,
200210
Network::Signet => BNetwork::Signet,
201211
}

src/config.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub struct Config {
3333
// See below for the documentation of each field:
3434
pub log: stderrlog::StdErrLog,
3535
pub network_type: Network,
36+
pub magic: Option<u32>,
3637
pub db_path: PathBuf,
3738
pub daemon_dir: PathBuf,
3839
pub blocks_dir: PathBuf,
@@ -137,6 +138,12 @@ impl Config {
137138
.help(&network_help)
138139
.takes_value(true),
139140
)
141+
.arg(
142+
Arg::with_name("magic")
143+
.long("magic")
144+
.default_value("")
145+
.takes_value(true),
146+
)
140147
.arg(
141148
Arg::with_name("electrum_rpc_addr")
142149
.long("electrum-rpc-addr")
@@ -328,6 +335,10 @@ impl Config {
328335

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

@@ -353,6 +364,8 @@ impl Config {
353364
Network::Regtest => 18443,
354365
#[cfg(not(feature = "liquid"))]
355366
Network::Signet => 38332,
367+
#[cfg(not(feature = "liquid"))]
368+
Network::Testnet4 => 48332,
356369

357370
#[cfg(feature = "liquid")]
358371
Network::Liquid => 7041,
@@ -365,6 +378,8 @@ impl Config {
365378
#[cfg(not(feature = "liquid"))]
366379
Network::Testnet => 60001,
367380
#[cfg(not(feature = "liquid"))]
381+
Network::Testnet4 => 40001,
382+
#[cfg(not(feature = "liquid"))]
368383
Network::Regtest => 60401,
369384
#[cfg(not(feature = "liquid"))]
370385
Network::Signet => 60601,
@@ -385,6 +400,8 @@ impl Config {
385400
Network::Regtest => 3002,
386401
#[cfg(not(feature = "liquid"))]
387402
Network::Signet => 3003,
403+
#[cfg(not(feature = "liquid"))]
404+
Network::Testnet4 => 3004,
388405

389406
#[cfg(feature = "liquid")]
390407
Network::Liquid => 3000,
@@ -401,6 +418,8 @@ impl Config {
401418
#[cfg(not(feature = "liquid"))]
402419
Network::Regtest => 24224,
403420
#[cfg(not(feature = "liquid"))]
421+
Network::Testnet4 => 44224,
422+
#[cfg(not(feature = "liquid"))]
404423
Network::Signet => 54224,
405424

406425
#[cfg(feature = "liquid")]
@@ -449,6 +468,8 @@ impl Config {
449468
#[cfg(not(feature = "liquid"))]
450469
Network::Testnet => daemon_dir.push("testnet3"),
451470
#[cfg(not(feature = "liquid"))]
471+
Network::Testnet4 => daemon_dir.push("testnet4"),
472+
#[cfg(not(feature = "liquid"))]
452473
Network::Regtest => daemon_dir.push("regtest"),
453474
#[cfg(not(feature = "liquid"))]
454475
Network::Signet => daemon_dir.push("signet"),
@@ -486,6 +507,7 @@ impl Config {
486507
let config = Config {
487508
log,
488509
network_type,
510+
magic,
489511
db_path,
490512
daemon_dir,
491513
blocks_dir,

src/daemon.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ pub struct Daemon {
284284
daemon_dir: PathBuf,
285285
blocks_dir: PathBuf,
286286
network: Network,
287+
magic: Option<u32>,
287288
conn: Mutex<Connection>,
288289
message_id: Counter, // for monotonic JSONRPC 'id'
289290
signal: Waiter,
@@ -300,13 +301,15 @@ impl Daemon {
300301
daemon_rpc_addr: SocketAddr,
301302
cookie_getter: Arc<dyn CookieGetter>,
302303
network: Network,
304+
magic: Option<u32>,
303305
signal: Waiter,
304306
metrics: &Metrics,
305307
) -> Result<Daemon> {
306308
let daemon = Daemon {
307309
daemon_dir,
308310
blocks_dir,
309311
network,
312+
magic,
310313
conn: Mutex::new(Connection::new(
311314
daemon_rpc_addr,
312315
cookie_getter,
@@ -367,6 +370,7 @@ impl Daemon {
367370
daemon_dir: self.daemon_dir.clone(),
368371
blocks_dir: self.blocks_dir.clone(),
369372
network: self.network,
373+
magic: self.magic,
370374
conn: Mutex::new(self.conn.lock().unwrap().reconnect()?),
371375
message_id: Counter::new(),
372376
signal: self.signal.clone(),
@@ -387,7 +391,7 @@ impl Daemon {
387391
}
388392

389393
pub fn magic(&self) -> u32 {
390-
self.network.magic()
394+
self.magic.unwrap_or_else(|| self.network.magic())
391395
}
392396

393397
fn call_jsonrpc(&self, method: &str, request: &Value) -> Result<Value> {

src/rest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,7 @@ fn address_to_scripthash(addr: &str, network: Network) -> Result<FullHash, HttpE
16681668
// `addr_network` will be detected as Testnet for all of them.
16691669
addr_network == network
16701670
|| (addr_network == Network::Testnet
1671-
&& matches!(network, Network::Regtest | Network::Signet))
1671+
&& matches!(network, Network::Regtest | Network::Signet | Network::Testnet4))
16721672
};
16731673

16741674
#[cfg(feature = "liquid")]

start

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ case "${1}" in
4343
NETWORK=testnet
4444
THREADS=$((NPROC / 6))
4545
;;
46+
testnet4)
47+
NETWORK=testnet4
48+
MAGIC=283f161c
49+
THREADS=$((NPROC / 6))
50+
;;
4651
signet)
4752
NETWORK=signet
4853
THREADS=$((NPROC / 6))
@@ -60,7 +65,7 @@ case "${1}" in
6065
THREADS=$((NPROC / 6))
6166
;;
6267
*)
63-
echo "Usage: $0 (mainnet|testnet|signet|liquid|liquidtestnet)"
68+
echo "Usage: $0 (mainnet|testnet|testnet4|signet|liquid|liquidtestnet)"
6469
exit 1
6570
;;
6671
esac
@@ -148,6 +153,7 @@ do
148153
--precache-threads "${THREADS}" \
149154
--cookie "${RPC_USER}:${RPC_PASS}" \
150155
--cors '*' \
156+
--magic "${MAGIC}" \
151157
--address-search \
152158
--utxos-limit "${UTXOS_LIMIT}" \
153159
--electrum-txs-limit "${ELECTRUM_TXS_LIMIT}" \

0 commit comments

Comments
 (0)