2021-08-06 00:47:31 +00:00
|
|
|
use beacon_chain::{
|
|
|
|
test_utils::{BeaconChainHarness, EphemeralHarnessType},
|
|
|
|
BeaconChain, BeaconChainTypes,
|
|
|
|
};
|
|
|
|
use eth2::{BeaconNodeHttpClient, Timeouts};
|
Rename eth2_libp2p to lighthouse_network (#2702)
## Description
The `eth2_libp2p` crate was originally named and designed to incorporate a simple libp2p integration into lighthouse. Since its origins the crates purpose has expanded dramatically. It now houses a lot more sophistication that is specific to lighthouse and no longer just a libp2p integration.
As of this writing it currently houses the following high-level lighthouse-specific logic:
- Lighthouse's implementation of the eth2 RPC protocol and specific encodings/decodings
- Integration and handling of ENRs with respect to libp2p and eth2
- Lighthouse's discovery logic, its integration with discv5 and logic about searching and handling peers.
- Lighthouse's peer manager - This is a large module handling various aspects of Lighthouse's network, such as peer scoring, handling pings and metadata, connection maintenance and recording, etc.
- Lighthouse's peer database - This is a collection of information stored for each individual peer which is specific to lighthouse. We store connection state, sync state, last seen ips and scores etc. The data stored for each peer is designed for various elements of the lighthouse code base such as syncing and the http api.
- Gossipsub scoring - This stores a collection of gossipsub 1.1 scoring mechanisms that are continuously analyssed and updated based on the ethereum 2 networks and how Lighthouse performs on these networks.
- Lighthouse specific types for managing gossipsub topics, sync status and ENR fields
- Lighthouse's network HTTP API metrics - A collection of metrics for lighthouse network monitoring
- Lighthouse's custom configuration of all networking protocols, RPC, gossipsub, discovery, identify and libp2p.
Therefore it makes sense to rename the crate to be more akin to its current purposes, simply that it manages the majority of Lighthouse's network stack. This PR renames this crate to `lighthouse_network`
Co-authored-by: Paul Hauner <paul@paulhauner.com>
2021-10-19 00:30:39 +00:00
|
|
|
use http_api::{Config, Context};
|
|
|
|
use lighthouse_network::{
|
2021-08-06 00:47:31 +00:00
|
|
|
discv5::enr::{CombinedKey, EnrBuilder},
|
2021-11-08 00:01:10 +00:00
|
|
|
libp2p::{core::connection::ConnectionId, swarm::NetworkBehaviour},
|
2021-08-06 00:47:31 +00:00
|
|
|
rpc::methods::{MetaData, MetaDataV2},
|
|
|
|
types::{EnrAttestationBitfield, EnrSyncCommitteeBitfield, SyncState},
|
2021-11-03 23:44:44 +00:00
|
|
|
ConnectedPoint, Enr, NetworkGlobals, PeerId, PeerManager,
|
2021-08-06 00:47:31 +00:00
|
|
|
};
|
|
|
|
use network::NetworkMessage;
|
|
|
|
use sensitive_url::SensitiveUrl;
|
|
|
|
use slog::Logger;
|
|
|
|
use std::future::Future;
|
2022-03-24 00:04:49 +00:00
|
|
|
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
2021-08-06 00:47:31 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::time::Duration;
|
|
|
|
use tokio::sync::{mpsc, oneshot};
|
2021-10-14 02:58:10 +00:00
|
|
|
use types::{ChainSpec, EthSpec};
|
2021-08-06 00:47:31 +00:00
|
|
|
|
|
|
|
pub const TCP_PORT: u16 = 42;
|
|
|
|
pub const UDP_PORT: u16 = 42;
|
|
|
|
pub const SEQ_NUMBER: u64 = 0;
|
|
|
|
pub const EXTERNAL_ADDR: &str = "/ip4/0.0.0.0/tcp/9000";
|
|
|
|
|
|
|
|
/// HTTP API tester that allows interaction with the underlying beacon chain harness.
|
|
|
|
pub struct InteractiveTester<E: EthSpec> {
|
|
|
|
pub harness: BeaconChainHarness<EphemeralHarnessType<E>>,
|
|
|
|
pub client: BeaconNodeHttpClient,
|
|
|
|
pub network_rx: mpsc::UnboundedReceiver<NetworkMessage<E>>,
|
|
|
|
_server_shutdown: oneshot::Sender<()>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The result of calling `create_api_server`.
|
|
|
|
///
|
|
|
|
/// Glue-type between `tests::ApiTester` and `InteractiveTester`.
|
|
|
|
pub struct ApiServer<E: EthSpec, SFut: Future<Output = ()>> {
|
|
|
|
pub server: SFut,
|
|
|
|
pub listening_socket: SocketAddr,
|
|
|
|
pub shutdown_tx: oneshot::Sender<()>,
|
|
|
|
pub network_rx: tokio::sync::mpsc::UnboundedReceiver<NetworkMessage<E>>,
|
|
|
|
pub local_enr: Enr,
|
|
|
|
pub external_peer_id: PeerId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<E: EthSpec> InteractiveTester<E> {
|
2021-10-11 02:45:06 +00:00
|
|
|
pub async fn new(spec: Option<ChainSpec>, validator_count: usize) -> Self {
|
2021-10-14 02:58:10 +00:00
|
|
|
let harness = BeaconChainHarness::builder(E::default())
|
|
|
|
.spec_or_default(spec)
|
|
|
|
.deterministic_keypairs(validator_count)
|
|
|
|
.fresh_ephemeral_store()
|
|
|
|
.build();
|
2021-08-06 00:47:31 +00:00
|
|
|
|
|
|
|
let ApiServer {
|
|
|
|
server,
|
|
|
|
listening_socket,
|
|
|
|
shutdown_tx: _server_shutdown,
|
|
|
|
network_rx,
|
|
|
|
..
|
2021-10-11 02:45:06 +00:00
|
|
|
} = create_api_server(harness.chain.clone(), harness.logger().clone()).await;
|
2021-08-06 00:47:31 +00:00
|
|
|
|
|
|
|
tokio::spawn(server);
|
|
|
|
|
|
|
|
let client = BeaconNodeHttpClient::new(
|
|
|
|
SensitiveUrl::parse(&format!(
|
|
|
|
"http://{}:{}",
|
|
|
|
listening_socket.ip(),
|
|
|
|
listening_socket.port()
|
|
|
|
))
|
|
|
|
.unwrap(),
|
|
|
|
Timeouts::set_all(Duration::from_secs(1)),
|
|
|
|
);
|
|
|
|
|
|
|
|
Self {
|
|
|
|
harness,
|
|
|
|
client,
|
|
|
|
network_rx,
|
|
|
|
_server_shutdown,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-11 02:45:06 +00:00
|
|
|
pub async fn create_api_server<T: BeaconChainTypes>(
|
2021-08-06 00:47:31 +00:00
|
|
|
chain: Arc<BeaconChain<T>>,
|
|
|
|
log: Logger,
|
2022-07-30 00:22:37 +00:00
|
|
|
) -> ApiServer<T::EthSpec, impl Future<Output = ()>> {
|
|
|
|
// Get a random unused port.
|
|
|
|
let port = unused_port::unused_tcp_port().unwrap();
|
|
|
|
create_api_server_on_port(chain, log, port).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn create_api_server_on_port<T: BeaconChainTypes>(
|
|
|
|
chain: Arc<BeaconChain<T>>,
|
|
|
|
log: Logger,
|
|
|
|
port: u16,
|
2021-08-06 00:47:31 +00:00
|
|
|
) -> ApiServer<T::EthSpec, impl Future<Output = ()>> {
|
|
|
|
let (network_tx, network_rx) = mpsc::unbounded_channel();
|
|
|
|
|
|
|
|
// Default metadata
|
|
|
|
let meta_data = MetaData::V2(MetaDataV2 {
|
|
|
|
seq_number: SEQ_NUMBER,
|
|
|
|
attnets: EnrAttestationBitfield::<T::EthSpec>::default(),
|
|
|
|
syncnets: EnrSyncCommitteeBitfield::<T::EthSpec>::default(),
|
|
|
|
});
|
|
|
|
let enr_key = CombinedKey::generate_secp256k1();
|
|
|
|
let enr = EnrBuilder::new("v4").build(&enr_key).unwrap();
|
2021-10-11 02:45:06 +00:00
|
|
|
let network_globals = Arc::new(NetworkGlobals::new(
|
|
|
|
enr.clone(),
|
|
|
|
TCP_PORT,
|
|
|
|
UDP_PORT,
|
|
|
|
meta_data,
|
|
|
|
vec![],
|
|
|
|
&log,
|
|
|
|
));
|
2021-08-06 00:47:31 +00:00
|
|
|
|
2021-10-11 02:45:06 +00:00
|
|
|
// Only a peer manager can add peers, so we create a dummy manager.
|
2021-11-03 23:44:44 +00:00
|
|
|
let config = lighthouse_network::peer_manager::config::Config::default();
|
|
|
|
let mut pm = PeerManager::new(config, network_globals.clone(), &log)
|
2021-10-11 02:45:06 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// add a peer
|
2021-08-06 00:47:31 +00:00
|
|
|
let peer_id = PeerId::random();
|
|
|
|
|
2021-10-11 02:45:06 +00:00
|
|
|
let connected_point = ConnectedPoint::Listener {
|
|
|
|
local_addr: EXTERNAL_ADDR.parse().unwrap(),
|
|
|
|
send_back_addr: EXTERNAL_ADDR.parse().unwrap(),
|
|
|
|
};
|
2021-11-08 00:01:10 +00:00
|
|
|
let con_id = ConnectionId::new(1);
|
2022-02-10 04:12:03 +00:00
|
|
|
pm.inject_connection_established(&peer_id, &con_id, &connected_point, None, 0);
|
2021-08-06 00:47:31 +00:00
|
|
|
*network_globals.sync_state.write() = SyncState::Synced;
|
|
|
|
|
|
|
|
let eth1_service = eth1::Service::new(eth1::Config::default(), log.clone(), chain.spec.clone());
|
|
|
|
|
|
|
|
let context = Arc::new(Context {
|
|
|
|
config: Config {
|
|
|
|
enabled: true,
|
2022-03-24 00:04:49 +00:00
|
|
|
listen_addr: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
|
2022-07-30 00:22:37 +00:00
|
|
|
listen_port: port,
|
2021-08-06 00:47:31 +00:00
|
|
|
allow_origin: None,
|
|
|
|
serve_legacy_spec: true,
|
2021-10-12 03:35:49 +00:00
|
|
|
tls_config: None,
|
2021-10-02 19:57:23 +00:00
|
|
|
allow_sync_stalled: false,
|
2021-08-06 00:47:31 +00:00
|
|
|
},
|
|
|
|
chain: Some(chain.clone()),
|
|
|
|
network_tx: Some(network_tx),
|
2021-10-11 02:45:06 +00:00
|
|
|
network_globals: Some(network_globals),
|
2021-08-06 00:47:31 +00:00
|
|
|
eth1_service: Some(eth1_service),
|
|
|
|
log,
|
|
|
|
});
|
|
|
|
let ctx = context.clone();
|
|
|
|
let (shutdown_tx, shutdown_rx) = oneshot::channel();
|
|
|
|
let server_shutdown = async {
|
|
|
|
// It's not really interesting why this triggered, just that it happened.
|
|
|
|
let _ = shutdown_rx.await;
|
|
|
|
};
|
|
|
|
let (listening_socket, server) = http_api::serve(ctx, server_shutdown).unwrap();
|
|
|
|
|
|
|
|
ApiServer {
|
|
|
|
server,
|
|
|
|
listening_socket,
|
|
|
|
shutdown_tx,
|
|
|
|
network_rx,
|
|
|
|
local_enr: enr,
|
|
|
|
external_peer_id: peer_id,
|
|
|
|
}
|
|
|
|
}
|