This repository was archived by the owner on Jan 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 212
feat(net/test): add integration test skeleton #2679
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| //! Integration tests for the node actors. | ||
|
|
||
| mod network; | ||
|
|
||
| pub(crate) mod utils; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| //! Tests interactions with sequencer actor's inputs channels. | ||
|
|
||
| use alloy_chains::Chain; | ||
| use alloy_signer::k256; | ||
|
|
||
| use alloy_primitives::Address; | ||
| use discv5::{ConfigBuilder, Enr, ListenConfig}; | ||
| use kona_disc::LocalNode; | ||
| use kona_genesis::RollupConfig; | ||
| use kona_gossip::{P2pRpcRequest, PeerDump, PeerInfo}; | ||
| use kona_node_service::{ | ||
| NetworkActor, NetworkActorError, NetworkBuilder, NetworkContext, NetworkInboundData, NodeActor, | ||
| }; | ||
| use libp2p::{Multiaddr, identity::Keypair, multiaddr::Protocol}; | ||
| use op_alloy_rpc_types_engine::OpExecutionPayloadEnvelope; | ||
| use std::str::FromStr; | ||
| use tokio::{ | ||
| sync::{mpsc, oneshot}, | ||
| task::JoinHandle, | ||
| }; | ||
| use tokio_util::sync::CancellationToken; | ||
|
|
||
| use crate::actors::utils::SeedGenerator; | ||
|
|
||
| pub(super) struct TestNetwork { | ||
| inbound_data: NetworkInboundData, | ||
| /// We'll remove those fields as we add more tests. | ||
| #[allow(dead_code)] | ||
| blocks_rx: mpsc::Receiver<OpExecutionPayloadEnvelope>, | ||
| #[allow(dead_code)] | ||
| handle: JoinHandle<Result<(), NetworkActorError>>, | ||
| } | ||
|
|
||
| #[derive(Debug, thiserror::Error)] | ||
| pub(super) enum TestNetworkError { | ||
| #[error("P2p receiver closed")] | ||
| P2pReceiverClosed, | ||
| #[error("P2p receiver closed before sending response: {0}")] | ||
| OneshotError(#[from] oneshot::error::RecvError), | ||
| #[error("Peer info missing ENR")] | ||
| PeerInfoMissingEnr, | ||
| #[error("Invalid ENR: {0}")] | ||
| InvalidEnr(String), | ||
| #[error("Peer not connected")] | ||
| PeerNotConnected, | ||
| } | ||
|
|
||
| fn rollup_config() -> RollupConfig { | ||
| RollupConfig { l2_chain_id: Chain::from_id(19_934_000), ..Default::default() } | ||
| } | ||
|
|
||
| impl TestNetwork { | ||
| pub(super) fn new(bootnodes: Vec<Enr>, seed_generator: &mut SeedGenerator) -> Self { | ||
| let unsafe_block_signer = Address::ZERO; | ||
| let keypair = Keypair::generate_secp256k1(); | ||
| let secp256k1_key = keypair.clone().try_into_secp256k1() | ||
| .map_err(|e| anyhow::anyhow!("Impossible to convert keypair to secp256k1. This is a bug since we only support secp256k1 keys: {e}")).unwrap() | ||
| .secret().to_bytes(); | ||
|
theochap marked this conversation as resolved.
|
||
| let local_node_key = k256::ecdsa::SigningKey::from_bytes(&secp256k1_key.into()) | ||
| .map_err(|e| anyhow::anyhow!("Impossible to convert keypair to k256 signing key. This is a bug since we only support secp256k1 keys: {e}")).unwrap(); | ||
|
|
||
| let node_addr = seed_generator.next_loopback_address(); | ||
|
|
||
| let discovery_port = seed_generator.next_port(); | ||
|
|
||
| let discovery_config = ConfigBuilder::new(ListenConfig::from_ip(node_addr, discovery_port)) | ||
| // Only allow loopback addresses. | ||
| .table_filter(|enr| { | ||
| let Some(ip) = enr.ip4() else { | ||
| return false; | ||
| }; | ||
|
|
||
| ip.is_loopback() | ||
| }) | ||
| .build(); | ||
|
|
||
| let gossip_port = seed_generator.next_port(); | ||
| let mut gossip_multiaddr = Multiaddr::from(node_addr); | ||
| gossip_multiaddr.push(Protocol::Tcp(gossip_port)); | ||
|
|
||
| let gossip_config = kona_gossip::default_config_builder().build().unwrap(); | ||
|
|
||
| // Create a new network actor. No external connections | ||
| let builder = NetworkBuilder::new( | ||
| // Create a new rollup config. We don't need to specify any of the fields. | ||
| rollup_config(), | ||
| unsafe_block_signer, | ||
| gossip_multiaddr, | ||
| keypair, | ||
| LocalNode::new(local_node_key, node_addr, gossip_port, discovery_port), | ||
| discovery_config, | ||
| ) | ||
| .with_bootnodes(bootnodes) | ||
| .with_gossip_config(gossip_config); | ||
|
|
||
| let (inbound_data, actor) = NetworkActor::new(builder); | ||
|
|
||
| let (blocks_tx, blocks_rx) = mpsc::channel(1024); | ||
| let cancellation = CancellationToken::new(); | ||
|
|
||
| let context = NetworkContext { blocks: blocks_tx, cancellation }; | ||
|
|
||
| let handle = tokio::spawn(async move { actor.start(context).await }); | ||
|
|
||
| Self { inbound_data, blocks_rx, handle } | ||
| } | ||
|
|
||
| pub(super) async fn peer_info(&self) -> Result<PeerInfo, TestNetworkError> { | ||
| // Try to get the peer info. Send a peer info request to the network actor. | ||
| let (peer_info_tx, peer_info_rx) = oneshot::channel(); | ||
| let peer_info_request = P2pRpcRequest::PeerInfo(peer_info_tx); | ||
| self.inbound_data | ||
| .p2p_rpc | ||
| .send(peer_info_request) | ||
| .await | ||
| .map_err(|_| TestNetworkError::P2pReceiverClosed)?; | ||
|
|
||
| let info = peer_info_rx.await?; | ||
|
|
||
| Ok(info) | ||
| } | ||
|
|
||
| pub(super) async fn peers(&self) -> Result<PeerDump, TestNetworkError> { | ||
| let (peers_tx, peers_rx) = oneshot::channel(); | ||
| let peers_request = P2pRpcRequest::Peers { out: peers_tx, connected: true }; | ||
| self.inbound_data | ||
| .p2p_rpc | ||
| .send(peers_request) | ||
| .await | ||
| .map_err(|_| TestNetworkError::P2pReceiverClosed)?; | ||
| let peers = peers_rx.await?; | ||
| Ok(peers) | ||
| } | ||
|
|
||
| pub(super) async fn is_connected_to(&self, other: &Self) -> Result<(), TestNetworkError> { | ||
| let other_peer_id = other.peer_id().await?; | ||
| let peers = self.peers().await?; | ||
| if !peers.peers.contains_key(&other_peer_id) { | ||
| return Err(TestNetworkError::PeerNotConnected); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| pub(super) async fn peer_enr(&self) -> Result<Enr, TestNetworkError> { | ||
| let enr = self.peer_info().await?.enr.ok_or(TestNetworkError::PeerInfoMissingEnr)?; | ||
| // Parse the ENR | ||
| let enr = Enr::from_str(&enr).map_err(TestNetworkError::InvalidEnr)?; | ||
| Ok(enr) | ||
| } | ||
|
|
||
| pub(super) async fn peer_id(&self) -> Result<String, TestNetworkError> { | ||
| Ok(self.peer_info().await?.peer_id) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| //! Integration tests for the network actor. | ||
|
|
||
| use std::time::Duration; | ||
|
|
||
| use backon::{ExponentialBuilder, Retryable}; | ||
|
|
||
| use crate::actors::{ | ||
| network::mocks::{TestNetwork, TestNetworkError}, | ||
| utils::SEED_GENERATOR_BUILDER, | ||
| }; | ||
|
|
||
| pub(super) mod mocks; | ||
|
|
||
| #[tokio::test(flavor = "multi_thread")] | ||
| async fn test_p2p_network_conn() -> anyhow::Result<()> { | ||
| let mut seed_generator = SEED_GENERATOR_BUILDER.next_generator(); | ||
|
|
||
| let network_1 = TestNetwork::new(vec![], &mut seed_generator); | ||
| let enr_1 = network_1.peer_enr().await?; | ||
|
|
||
| let network_2 = TestNetwork::new(vec![enr_1], &mut seed_generator); | ||
|
|
||
| (async || network_2.is_connected_to(&network_1).await) | ||
| .retry(ExponentialBuilder::default().with_total_delay(Some(Duration::from_secs(10)))) | ||
| // When to retry | ||
| .when(|e| matches!(e, TestNetworkError::PeerNotConnected)) | ||
| .await?; | ||
|
|
||
| (async || network_1.is_connected_to(&network_2).await) | ||
| .retry(ExponentialBuilder::default().with_total_delay(Some(Duration::from_secs(10)))) | ||
| // When to retry | ||
| .when(|e| matches!(e, TestNetworkError::PeerNotConnected)) | ||
| .await?; | ||
|
|
||
| Ok(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| use std::{ | ||
| net::{IpAddr, Ipv4Addr}, | ||
| sync::{ | ||
| LazyLock, | ||
| atomic::{AtomicU64, Ordering}, | ||
| }, | ||
| }; | ||
|
|
||
| use rand::{RngCore, SeedableRng}; | ||
|
|
||
| pub(crate) static SEED_GENERATOR_BUILDER: LazyLock<SeedGeneratorBuilder> = | ||
| LazyLock::new(SeedGeneratorBuilder::new); | ||
|
|
||
| pub(crate) struct SeedGeneratorBuilder(AtomicU64); | ||
|
|
||
| impl SeedGeneratorBuilder { | ||
| pub(crate) const fn new() -> Self { | ||
| Self(AtomicU64::new(0)) | ||
| } | ||
|
|
||
| fn next(&self) -> u64 { | ||
| self.0.fetch_add(1, Ordering::Relaxed) | ||
| } | ||
|
|
||
| pub(crate) fn next_generator(&self) -> SeedGenerator { | ||
| SeedGenerator(rand::rngs::StdRng::seed_from_u64(self.next())) | ||
| } | ||
| } | ||
|
|
||
| pub(crate) struct SeedGenerator(rand::rngs::StdRng); | ||
|
|
||
| impl SeedGenerator { | ||
| pub(crate) fn next_loopback_address(&mut self) -> IpAddr { | ||
| let next_u64 = self.0.next_u64(); | ||
|
|
||
| IpAddr::V4(Ipv4Addr::new( | ||
| 127, | ||
| next_u64 as u8, | ||
| (next_u64 >> 8) as u8, | ||
| (next_u64 >> 16) as u8, | ||
| )) | ||
| } | ||
|
|
||
| pub(crate) fn next_port(&mut self) -> u16 { | ||
| let next_u32 = self.0.next_u32(); | ||
|
|
||
| next_u32 as u16 | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| //! Integration tests for the node service crate. | ||
|
|
||
| /// Tests for the node actors. | ||
| mod actors; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this just be removed now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will remove it in a follow up PR if we don't need it. I would like to make sure that we kill the handle if we drop the network - this will probably become some sort of drop guard