diff --git a/zebrad/src/commands/connect.rs b/zebrad/src/commands/connect.rs index 29f3b2941..6a8cc2ca6 100644 --- a/zebrad/src/commands/connect.rs +++ b/zebrad/src/commands/connect.rs @@ -1,6 +1,7 @@ //! `connect` subcommand - test stub for talking to zcashd use crate::{components::tokio::TokioComponent, prelude::*}; + use abscissa_core::{Command, Options, Runnable}; use color_eyre::eyre::{eyre, Report, WrapErr}; use futures::{ @@ -9,13 +10,9 @@ use futures::{ }; use std::collections::BTreeSet; use tower::{buffer::Buffer, service_fn, Service, ServiceExt}; -use zebra_chain::{block::BlockHeaderHash, types::BlockHeight}; -// genesis -static GENESIS: BlockHeaderHash = BlockHeaderHash([ - 8, 206, 61, 151, 49, 176, 0, 192, 131, 56, 69, 92, 138, 74, 107, 208, 93, 161, 110, 38, 177, - 29, 170, 27, 145, 113, 132, 236, 232, 15, 4, 0, -]); +use zebra_chain::{block::BlockHeaderHash, types::BlockHeight}; +use zebra_consensus::parameters; /// `connect` subcommand #[derive(Command, Debug, Options)] @@ -33,6 +30,9 @@ impl Runnable for ConnectCmd { fn run(&self) { info!(connect.addr = ?self.addr); + let network = app_config().network.network; + let genesis_hash = parameters::genesis_hash(network); + let rt = app_writer() .state_mut() .components @@ -43,7 +43,7 @@ impl Runnable for ConnectCmd { let result = rt .expect("runtime should not already be taken") - .block_on(self.connect()); + .block_on(self.connect(genesis_hash)); match result { Ok(()) => {} @@ -56,7 +56,7 @@ impl Runnable for ConnectCmd { } impl ConnectCmd { - async fn connect(&self) -> Result<(), Report> { + async fn connect(&self, genesis_hash: BlockHeaderHash) -> Result<(), Report> { info!(?self, "begin tower-based peer handling test stub"); // The service that our node uses to respond to requests by peers @@ -85,7 +85,7 @@ impl ConnectCmd { retry_peer_set, peer_set, state, - tip: GENESIS, + tip: genesis_hash, block_requests: FuturesUnordered::new(), requested_block_heights: 0, downloaded_block_heights, diff --git a/zebrad/src/commands/start.rs b/zebrad/src/commands/start.rs index 9417e0165..502a402f1 100644 --- a/zebrad/src/commands/start.rs +++ b/zebrad/src/commands/start.rs @@ -26,16 +26,8 @@ use abscissa_core::{config, Command, FrameworkError, Options, Runnable}; use color_eyre::eyre::Report; use tower::{buffer::Buffer, service_fn}; -use zebra_chain::block::BlockHeaderHash; - mod sync; -// genesis -const GENESIS: BlockHeaderHash = BlockHeaderHash([ - 8, 206, 61, 151, 49, 176, 0, 192, 131, 56, 69, 92, 138, 74, 107, 208, 93, 161, 110, 38, 177, - 29, 170, 27, 145, 113, 132, 236, 232, 15, 4, 0, -]); - /// `start` subcommand #[derive(Command, Debug, Options)] pub struct StartCmd { @@ -61,7 +53,7 @@ impl StartCmd { let (peer_set, _address_book) = zebra_network::init(config.network.clone(), node).await; let verifier = zebra_consensus::chain::init(config.network.network, state.clone()); - let mut syncer = sync::Syncer::new(peer_set, state, verifier); + let mut syncer = sync::Syncer::new(config.network.network, peer_set, state, verifier); syncer.sync().await } diff --git a/zebrad/src/commands/start/sync.rs b/zebrad/src/commands/start/sync.rs index 389824011..eed6f8fe1 100644 --- a/zebrad/src/commands/start/sync.rs +++ b/zebrad/src/commands/start/sync.rs @@ -11,8 +11,9 @@ use zebra_chain::{ Network, }; use zebra_consensus::checkpoint; +use zebra_consensus::parameters; use zebra_network::{self as zn, RetryLimit}; -use zebra_state::{self as zs}; +use zebra_state as zs; // XXX in the future, we may not be able to access the checkpoint module. const FANOUT: usize = checkpoint::MAX_QUEUED_BLOCKS_PER_HEIGHT; @@ -40,6 +41,7 @@ where prospective_tips: HashSet, pending_blocks: Pin>>>>>, + genesis_hash: BlockHeaderHash, } impl Syncer @@ -51,15 +53,21 @@ where ZV: Service, Response = BlockHeaderHash, Error = Error> + Send + Clone + 'static, ZV::Future: Send, { - pub fn new(network: ZN, state: ZS, verifier: ZV) -> Self { - let retry_network = Retry::new(RetryLimit::new(3), network.clone()); + /// Returns a new syncer instance, using: + /// - chain: the zebra-chain `Network` to download (Mainnet or Testnet) + /// - peers: the zebra-network peers to contact for downloads + /// - state: the zebra-state that stores the chain + /// - verifier: the zebra-consensus verifier that checks the chain + pub fn new(chain: Network, peers: ZN, state: ZS, verifier: ZV) -> Self { + let retry_peers = Retry::new(RetryLimit::new(3), peers.clone()); Self { - tip_network: network, - block_network: retry_network, + tip_network: peers, + block_network: retry_peers, state, verifier, prospective_tips: HashSet::new(), pending_blocks: Box::pin(FuturesUnordered::new()), + genesis_hash: parameters::genesis_hash(chain), } } @@ -133,16 +141,13 @@ where // // Query the current state to construct the sequence of hashes: handled by // the caller - // - // TODO(teor): get the real network - let network = Network::Mainnet; let block_locator = self .state .ready_and() .await .map_err(|e| eyre!(e))? .call(zebra_state::Request::GetBlockLocator { - genesis: zebra_consensus::parameters::genesis_hash(network), + genesis: self.genesis_hash, }) .await .map(|response| match response { @@ -303,7 +308,7 @@ where tracing::debug!("skipping length-1 response, in case it's an unsolicited inv message"); continue; } - (Some(&super::GENESIS), _) => { + (Some(hash), _) if (hash == &self.genesis_hash) => { tracing::debug!("skipping response, peer could not extend the tip"); continue; }