From d36d97f97fc774142bf6f1ad6b7ae125bebf7059 Mon Sep 17 00:00:00 2001 From: Rob Date: Mon, 8 Apr 2024 11:08:03 +0300 Subject: [PATCH] changes for new builder --- builder/src/bin/permissioned-builder.rs | 34 ++++++---- builder/src/permissioned.rs | 88 ++++++++++++++++--------- 2 files changed, 79 insertions(+), 43 deletions(-) diff --git a/builder/src/bin/permissioned-builder.rs b/builder/src/bin/permissioned-builder.rs index af52f4f97..6b9bca1ac 100644 --- a/builder/src/bin/permissioned-builder.rs +++ b/builder/src/bin/permissioned-builder.rs @@ -13,6 +13,7 @@ use hotshot_types::traits::metrics::NoMetrics; use hotshot_types::traits::node_implementation::ConsensusTime; use sequencer::{BuilderParams, L1Params, NetworkParams}; use snafu::Snafu; +use std::net::SocketAddr; use std::num::NonZeroUsize; use std::{collections::HashMap, path::PathBuf, str::FromStr, time::Duration}; use url::Url; @@ -32,23 +33,34 @@ pub struct PermissionedBuilderOptions { )] pub orchestrator_url: Url, - /// URL of the HotShot DA web server. + /// The socket address of the HotShot CDN's main entry point (the marshal) + /// in `IP:port` form #[clap( short, long, - env = "ESPRESSO_SEQUENCER_DA_SERVER_URL", - default_value = "http://localhost:8081" + env = "ESPRESSO_SEQUENCER_CDN_ENDPOINT", + default_value = "127.0.0.1:8081" )] - pub da_server_url: Url, + pub cdn_endpoint: String, - /// URL of the HotShot consensus web server. + /// The address to bind to for Libp2p (in `IP:port` form) #[clap( short, long, - env = "ESPRESSO_SEQUENCER_CONSENSUS_SERVER_URL", - default_value = "http://localhost:8082" + env = "ESPRESSO_SEQUENCER_LIBP2P_BIND_ADDRESS", + default_value = "0.0.0.0:1769" )] - pub consensus_server_url: Url, + pub libp2p_bind_address: SocketAddr, + + /// The address we advertise to other nodes as being a Libp2p endpoint. + /// Should be supplied in `IP:port` form. + #[clap( + short, + long, + env = "ESPRESSO_SEQUENCER_LIBP2P_ADVERTISE_ADDRESS", + default_value = "127.0.0.1:1769" + )] + pub libp2p_advertise_address: SocketAddr, /// URL of the Light Client State Relay Server #[clap( @@ -198,11 +210,11 @@ async fn main() -> anyhow::Result<()> { let builder_pub_key = BLSPubKey::from_private(&private_staking_key); let network_params = NetworkParams { - da_server_url: opt.da_server_url, - consensus_server_url: opt.consensus_server_url, + cdn_endpoint: opt.cdn_endpoint, + libp2p_advertise_address: opt.libp2p_advertise_address, + libp2p_bind_address: opt.libp2p_bind_address, orchestrator_url: opt.orchestrator_url, state_relay_server_url: opt.state_relay_server_url, - webserver_poll_interval: opt.webserver_poll_interval, private_staking_key: private_staking_key.clone(), private_state_key, state_peers: opt.state_peers, diff --git a/builder/src/permissioned.rs b/builder/src/permissioned.rs index 6cb645949..d7f0e8021 100644 --- a/builder/src/permissioned.rs +++ b/builder/src/permissioned.rs @@ -1,3 +1,4 @@ +use anyhow::Context; use ethers::{ core::k256::ecdsa::SigningKey, signers::{coins_bip39::English, MnemonicBuilder, Signer as _, Wallet}, @@ -10,7 +11,10 @@ use futures::{ use hotshot::{ traits::{ election::static_committee::{GeneralStaticCommittee, StaticElectionConfig}, - implementations::{NetworkingMetricsValue, WebServerNetwork}, + implementations::{ + derive_libp2p_peer_id, CombinedNetworks, KeyPair, Libp2pNetwork, + NetworkingMetricsValue, PushCdnNetwork, WebServerNetwork, WrappedSignatureKey, + }, }, types::{SignatureKey, SystemContextHandle}, HotShotInitializer, Memberships, Networks, SystemContext, @@ -128,39 +132,35 @@ pub async fn init_node( bootstrapped_view: ViewNumber, channel_capacity: NonZeroUsize, bind_version: Ver, -) -> anyhow::Result> { +) -> anyhow::Result> { + // Orchestrator client let validator_args = ValidatorArgs { url: network_params.orchestrator_url, - advertise_address: None, + advertise_address: Some(network_params.libp2p_advertise_address), network_config_file: None, }; - // This "public" IP only applies to libp2p network configurations, so we can supply any value here - let _public_ip = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)); - // Orchestrator client let orchestrator_client = OrchestratorClient::new(validator_args); - - let private_staking_key = network_params.private_staking_key.clone(); - let _public_staking_key = BLSPubKey::from_private(&private_staking_key); let state_key_pair = StateKeyPair::from_sign_key(network_params.private_state_key); - let my_config = ValidatorConfig { public_key: BLSPubKey::from_private(&network_params.private_staking_key), private_key: network_params.private_staking_key, stake_value: 1, - state_key_pair: state_key_pair.clone(), + state_key_pair, }; - // Wait for orchestrator to start the node - let _wait_for_orchestrator = true; + // Derive our Libp2p public key from our private key + let libp2p_public_key = + derive_libp2p_peer_id::<::SignatureKey>(&my_config.private_key) + .with_context(|| "Failed to derive Libp2p peer ID")?; - // Load the network configuration from the orchestrator - tracing::info!("loading network config from orchestrator"); let config = NetworkConfig::get_complete_config( &orchestrator_client, None, my_config.clone(), - None, - None, + // Register in our Libp2p advertise address and public key so other nodes + // can contact us on startup + Some(network_params.libp2p_advertise_address), + Some(libp2p_public_key), ) .await? .0; @@ -173,22 +173,46 @@ pub async fn init_node( let node_index = config.node_index; - tracing::info!("loaded config, we are node {}", config.node_index); - - // Initialize networking. + // Initialize the push CDN network (and perform the initial connection) + let cdn_network = PushCdnNetwork::new( + network_params.cdn_endpoint, + vec!["Global".into(), "DA".into()], + KeyPair { + public_key: WrappedSignatureKey(my_config.public_key), + private_key: my_config.private_key.clone(), + }, + ) + .await + .with_context(|| "Failed to create CDN network")?; + + // Initialize the Libp2p network + let p2p_network = Libp2pNetwork::from_config::( + config.clone(), + network_params.libp2p_bind_address, + &my_config.public_key, + // We need the private key so we can derive our Libp2p keypair + // (using https://docs.rs/blake3/latest/blake3/fn.derive_key.html) + &my_config.private_key, + ) + .await + .with_context(|| "Failed to create libp2p network")?; + + // Combine the two communication channels + let da_network = Arc::from(CombinedNetworks::new( + cdn_network.clone(), + p2p_network.clone(), + Duration::from_secs(1), + )); + let quorum_network = Arc::from(CombinedNetworks::new( + cdn_network, + p2p_network, + Duration::from_secs(1), + )); + + // Convert to the sequencer-compatible type let networks = Networks { - da_network: Arc::new(WebServerNetwork::create( - network_params.da_server_url, - network_params.webserver_poll_interval, - my_config.public_key, - true, - )), - quorum_network: Arc::new(WebServerNetwork::create( - network_params.consensus_server_url, - network_params.webserver_poll_interval, - my_config.public_key, - false, - )), + da_network, + quorum_network, _pd: Default::default(), };