Skip to content

Commit 3548e3d

Browse files
committed
Add mainnet block
- Allow specifying activation block height without its hash as it can be fetched from core - Custom data dir now appends the chain in a sub directory - Add mainnet and mainnet alpha blocks - Report more helpful error messages for non successful http status codes
1 parent ba5b2ff commit 3548e3d

File tree

5 files changed

+50
-12
lines changed

5 files changed

+50
-12
lines changed

node/src/bin/spaced.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl Composer {
128128
}
129129

130130
async fn run(&mut self) -> anyhow::Result<()> {
131-
let spaced = Args::configure()?;
131+
let spaced = Args::configure().await?;
132132
self.setup_rpc_services(&spaced).await;
133133
self.setup_sync_service(spaced).await;
134134

node/src/config.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ pub struct Args {
7979
#[serde(rename_all = "lowercase")]
8080
pub enum ExtendedNetwork {
8181
Mainnet,
82+
MainnetAlpha,
8283
Testnet,
8384
Testnet4,
8485
Signet,
@@ -88,7 +89,7 @@ pub enum ExtendedNetwork {
8889
impl ExtendedNetwork {
8990
pub fn fallback_network(&self) -> Network {
9091
match self {
91-
ExtendedNetwork::Mainnet => Network::Bitcoin,
92+
ExtendedNetwork::Mainnet | ExtendedNetwork::MainnetAlpha => Network::Bitcoin,
9293
ExtendedNetwork::Testnet => Network::Testnet,
9394
ExtendedNetwork::Signet => Network::Signet,
9495
ExtendedNetwork::Regtest => Network::Regtest,
@@ -100,7 +101,7 @@ impl ExtendedNetwork {
100101
impl Args {
101102
/// Configures spaced node by processing command line arguments
102103
/// and configuration files
103-
pub fn configure() -> anyhow::Result<Spaced> {
104+
pub async fn configure() -> anyhow::Result<Spaced> {
104105
let mut args = Args::merge_args_config(None);
105106
let default_dirs = get_default_node_dirs();
106107

@@ -126,9 +127,9 @@ impl Args {
126127
}
127128

128129
let data_dir = match args.data_dir {
129-
None => default_dirs.data_dir().join(args.chain.to_string()),
130+
None => default_dirs.data_dir().to_path_buf(),
130131
Some(data_dir) => data_dir,
131-
};
132+
}.join(args.chain.to_string());
132133

133134
let default_port = args.rpc_port.unwrap();
134135
let rpc_bind_addresses: Vec<SocketAddr> = args
@@ -144,7 +145,7 @@ impl Args {
144145
})
145146
.collect();
146147

147-
let genesis = Spaced::genesis(args.chain);
148+
148149
let bitcoin_rpc_auth = if let Some(cookie) = args.bitcoin_rpc_cookie {
149150
let cookie = std::fs::read_to_string(cookie)?;
150151
BitcoinRpcAuth::Cookie(cookie)
@@ -159,6 +160,8 @@ impl Args {
159160
bitcoin_rpc_auth,
160161
);
161162

163+
let genesis = Spaced::genesis(&rpc, args.chain).await?;
164+
162165
fs::create_dir_all(data_dir.clone())?;
163166

164167
let proto_db_path = data_dir.join("protocol.sdb");
@@ -261,7 +264,7 @@ pub fn safe_exit(code: i32) -> ! {
261264

262265
fn default_bitcoin_rpc_url(network: &ExtendedNetwork) -> &'static str {
263266
match network {
264-
ExtendedNetwork::Mainnet => "http://127.0.0.1:8332",
267+
ExtendedNetwork::Mainnet | ExtendedNetwork::MainnetAlpha => "http://127.0.0.1:8332",
265268
ExtendedNetwork::Testnet4 => "http://127.0.0.1:48332",
266269
ExtendedNetwork::Signet => "http://127.0.0.1:38332",
267270
ExtendedNetwork::Testnet => "http://127.0.0.1:18332",
@@ -359,6 +362,7 @@ impl Display for ExtendedNetwork {
359362
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
360363
let str = match self {
361364
ExtendedNetwork::Mainnet => "mainnet".to_string(),
365+
ExtendedNetwork::MainnetAlpha => "mainnet-alpha".to_string(),
362366
ExtendedNetwork::Testnet => "testnet".to_string(),
363367
ExtendedNetwork::Testnet4 => "testnet4".to_string(),
364368
ExtendedNetwork::Signet => "signet".to_string(),
@@ -371,6 +375,7 @@ impl Display for ExtendedNetwork {
371375
pub fn default_spaces_rpc_port(chain: &ExtendedNetwork) -> u16 {
372376
match chain {
373377
ExtendedNetwork::Mainnet => 7225,
378+
ExtendedNetwork::MainnetAlpha => 7225,
374379
ExtendedNetwork::Testnet4 => 7224,
375380
ExtendedNetwork::Testnet => 7223,
376381
ExtendedNetwork::Signet => 7221,

node/src/source.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,8 @@ impl std::error::Error for BitcoinRpcError {
694694

695695
impl ErrorForRpc for reqwest::Response {
696696
async fn error_for_rpc<T: DeserializeOwned>(self) -> Result<T, BitcoinRpcError> {
697-
let rpc_res: JsonRpcResponse<T> = self.json().await?;
697+
let res = self.error_for_status()?;
698+
let rpc_res: JsonRpcResponse<T> = res.json().await?;
698699
if let Some(e) = rpc_res.error {
699700
return Err(BitcoinRpcError::Rpc(e));
700701
}
@@ -705,7 +706,8 @@ impl ErrorForRpc for reqwest::Response {
705706

706707
impl ErrorForRpcBlocking for reqwest::blocking::Response {
707708
fn error_for_rpc<T: DeserializeOwned>(self) -> Result<T, BitcoinRpcError> {
708-
let rpc_res: JsonRpcResponse<T> = self.json()?;
709+
let res = self.error_for_status()?;
710+
let rpc_res: JsonRpcResponse<T> = res.json()?;
709711
if let Some(e) = rpc_res.error {
710712
return Err(BitcoinRpcError::Rpc(e));
711713
}

node/src/sync.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use protocol::{
88
hasher::BaseHash,
99
};
1010
use tokio::sync::broadcast;
11-
11+
use protocol::bitcoin::hashes::Hash;
1212
use crate::{
1313
config::ExtendedNetwork,
1414
node::{BlockMeta, BlockSource, Node},
@@ -190,12 +190,29 @@ impl Spaced {
190190
Ok(())
191191
}
192192

193-
pub fn genesis(network: ExtendedNetwork) -> ChainAnchor {
194-
match network {
193+
pub async fn genesis(rpc : &BitcoinRpc, network: ExtendedNetwork) -> anyhow::Result<ChainAnchor> {
194+
195+
let mut anchor = match network {
195196
ExtendedNetwork::Testnet => ChainAnchor::TESTNET(),
196197
ExtendedNetwork::Testnet4 => ChainAnchor::TESTNET4(),
197198
ExtendedNetwork::Regtest => ChainAnchor::REGTEST(),
199+
ExtendedNetwork::Mainnet => ChainAnchor::MAINNET(),
200+
ExtendedNetwork::MainnetAlpha => ChainAnchor::MAINNET_ALPHA(),
198201
_ => panic!("unsupported network"),
202+
};
203+
204+
if anchor.hash == BlockHash::all_zeros() {
205+
let client = reqwest::Client::new();
206+
207+
anchor.hash = match rpc.send_json(&client, &rpc.get_block_hash(anchor.height)).await {
208+
Ok(hash) => hash,
209+
Err(e) => {
210+
return Err(anyhow!("Could not retrieve activation block at height {}: {}",
211+
anchor.height, e));
212+
}
213+
}
199214
}
215+
216+
Ok(anchor)
200217
}
201218
}

protocol/src/constants.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ impl ChainAnchor {
6767
)
6868
};
6969

70+
pub const MAINNET_ALPHA: fn() -> Self = || {
71+
ChainAnchor {
72+
hash: BlockHash::all_zeros(),
73+
height: 869_000,
74+
}
75+
};
76+
77+
pub const MAINNET: fn() -> Self = || {
78+
ChainAnchor {
79+
hash: BlockHash::all_zeros(),
80+
height: 871_222,
81+
}
82+
};
83+
7084
// Testnet activation block
7185
pub const TESTNET: fn() -> Self = || {
7286
Self::new(

0 commit comments

Comments
 (0)