forked from paradigmxyz/reth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(net): support multiple eth protocol versions. (paradigmxyz#1152)
- Loading branch information
1 parent
4f375a1
commit c5e6673
Showing
4 changed files
with
112 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod connect; | ||
mod requests; | ||
mod session; | ||
|
||
fn main() {} |
This file contains 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,87 @@ | ||
//! Session tests | ||
|
||
use futures::StreamExt; | ||
use reth_eth_wire::{capability::Capability, EthVersion}; | ||
use reth_network::{ | ||
test_utils::{PeerConfig, Testnet}, | ||
NetworkEvent, | ||
}; | ||
use reth_network_api::{NetworkInfo, Peers}; | ||
use reth_provider::test_utils::NoopProvider; | ||
use std::sync::Arc; | ||
|
||
#[tokio::test(flavor = "multi_thread")] | ||
async fn test_session_established_with_highest_version() { | ||
reth_tracing::init_test_tracing(); | ||
|
||
let net = Testnet::create(2).await; | ||
|
||
net.for_each(|peer| assert_eq!(0, peer.num_peers())); | ||
|
||
let mut handles = net.handles(); | ||
let handle0 = handles.next().unwrap(); | ||
let handle1 = handles.next().unwrap(); | ||
drop(handles); | ||
|
||
let handle = net.spawn(); | ||
|
||
handle0.add_peer(*handle1.peer_id(), handle1.local_addr()); | ||
|
||
let mut events = handle0.event_listener().take(2); | ||
while let Some(event) = events.next().await { | ||
match event { | ||
NetworkEvent::PeerAdded(peer_id) => { | ||
assert_eq!(handle1.peer_id(), &peer_id); | ||
} | ||
NetworkEvent::SessionEstablished { peer_id, status, .. } => { | ||
assert_eq!(handle1.peer_id(), &peer_id); | ||
assert_eq!(status.version, EthVersion::Eth67 as u8); | ||
} | ||
_ => { | ||
panic!("unexpected event") | ||
} | ||
} | ||
} | ||
|
||
handle.terminate().await; | ||
} | ||
|
||
#[tokio::test(flavor = "multi_thread")] | ||
async fn test_session_established_with_different_capability() { | ||
reth_tracing::init_test_tracing(); | ||
|
||
let mut net = Testnet::create(1).await; | ||
|
||
let capabilities = vec![Capability::new("eth".into(), EthVersion::Eth66 as usize)]; | ||
let p1 = PeerConfig::with_capabilities(Arc::new(NoopProvider::default()), capabilities); | ||
net.add_peer_with_config(p1).await.unwrap(); | ||
|
||
net.for_each(|peer| assert_eq!(0, peer.num_peers())); | ||
|
||
let mut handles = net.handles(); | ||
let handle0 = handles.next().unwrap(); | ||
let handle1 = handles.next().unwrap(); | ||
drop(handles); | ||
|
||
let handle = net.spawn(); | ||
|
||
handle0.add_peer(*handle1.peer_id(), handle1.local_addr()); | ||
|
||
let mut events = handle0.event_listener().take(2); | ||
while let Some(event) = events.next().await { | ||
match event { | ||
NetworkEvent::PeerAdded(peer_id) => { | ||
assert_eq!(handle1.peer_id(), &peer_id); | ||
} | ||
NetworkEvent::SessionEstablished { peer_id, status, .. } => { | ||
assert_eq!(handle1.peer_id(), &peer_id); | ||
assert_eq!(status.version, EthVersion::Eth66 as u8); | ||
} | ||
_ => { | ||
panic!("unexpected event") | ||
} | ||
} | ||
} | ||
|
||
handle.terminate().await; | ||
} |