-
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.
Create base project for starknet-p2p-tests
- Loading branch information
0 parents
commit f036158
Showing
9 changed files
with
136 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
[package] | ||
name = "starknet-p2p-tests" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
libp2p = { version = "0.53.2", features = [ | ||
"autonat", | ||
"dcutr", | ||
"dns", | ||
"gossipsub", | ||
"identify", | ||
"kad", | ||
"macros", | ||
"noise", | ||
"ping", | ||
"relay", | ||
"request-response", | ||
"serde", | ||
"tcp", | ||
"tokio", | ||
"yamux", | ||
] } | ||
|
||
tokio = { version = "1.28.0", features = ["full", "test-util"] } | ||
anyhow = "1.0" |
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,3 @@ | ||
pub mod protocol; | ||
pub mod tools; | ||
pub mod tests; |
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,3 @@ | ||
fn main() { | ||
println!("Hello, world!"); | ||
} |
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 @@ | ||
pub mod starknet; |
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,73 @@ | ||
use libp2p::{ | ||
core::{ | ||
muxing::StreamMuxerBox, | ||
transport::OrTransport, | ||
upgrade, | ||
}, dns, futures, identity, noise, relay, swarm, tcp, yamux, Multiaddr, PeerId, Swarm, Transport | ||
}; | ||
use libp2p::swarm::SwarmEvent; | ||
use futures::StreamExt; | ||
|
||
use std::{error::Error, time::Duration}; | ||
|
||
pub async fn initialize_p2p(addr: &str) -> Result<(), Box<dyn Error>> { | ||
// Generate a keypair for the local peer | ||
let local_key = identity::Keypair::generate_ed25519(); | ||
let local_peer_id = local_key.public().to_peer_id(); | ||
|
||
// Create a multiaddr for the local peer | ||
let local_multiaddr: Multiaddr = format!("/ip4/127.0.0.1/tcp/7777/p2p/{}", local_peer_id).parse()?; | ||
println!("Local peer address: {}", local_multiaddr); | ||
|
||
// Initialize the relay transport | ||
let (relay_transport, relay_behaviour) = relay::client::new(local_peer_id.clone()); | ||
|
||
// Create the transport | ||
let transport = create_transport(&local_key, relay_transport); | ||
|
||
// Create the Swarm | ||
let mut swarm = Swarm::new( | ||
transport, | ||
relay_behaviour, | ||
local_peer_id, | ||
swarm::Config::with_tokio_executor() | ||
.with_idle_connection_timeout(Duration::from_secs(3600 * 365)) | ||
); | ||
|
||
let multiaddr: Multiaddr = addr.parse()?; | ||
|
||
swarm.dial(multiaddr.clone())?; | ||
println!("Dialing peer at address: {}", multiaddr); | ||
|
||
// Handle events | ||
loop { | ||
match swarm.next().await { | ||
Some(SwarmEvent::ConnectionEstablished { peer_id, .. }) => { | ||
println!("Connected to peer: {}", peer_id); | ||
return Ok(()); | ||
} | ||
Some(SwarmEvent::OutgoingConnectionError { error, .. }) => { | ||
return Err(format!("Failed to connect to peer: {:?}", error).into()); | ||
} | ||
_ => {} | ||
} | ||
} | ||
} | ||
|
||
pub fn create_transport( | ||
keypair: &libp2p::identity::Keypair, | ||
relay_transport: libp2p::relay::client::Transport, | ||
) -> libp2p::core::transport::Boxed<(PeerId, StreamMuxerBox)> { | ||
let transport = tcp::tokio::Transport::new(tcp::Config::new()); | ||
let transport = OrTransport::new(transport, relay_transport); | ||
let transport = dns::tokio::Transport::system(transport).unwrap(); | ||
|
||
let noise_config = | ||
noise::Config::new(keypair).expect("Signing libp2p-noise static DH keypair failed."); | ||
|
||
transport | ||
.upgrade(upgrade::Version::V1) | ||
.authenticate(noise_config) | ||
.multiplex(yamux::Config::default()) | ||
.boxed() | ||
} |
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 @@ | ||
pub mod tests_p2p; |
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,10 @@ | ||
|
||
#[test] | ||
fn test_connect_to_peer() { | ||
let node = crate::tools::synthetic_node::SyntheticNode::new(); | ||
let peer_address = "/ip4/35.237.66.77/tcp/7777/p2p/12D3KooWR8ikUDiinyE5wgdYiqsdLfJRsBDYKGii6L3oyoipVEaV"; | ||
|
||
let result = node.connect(peer_address); | ||
|
||
assert!(result.is_ok(), "Failed to connect: {:?}", result.err().unwrap()); | ||
} |
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 @@ | ||
pub mod synthetic_node; |
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,18 @@ | ||
use crate::protocol::starknet; | ||
use tokio::runtime::Runtime; | ||
use std::error::Error; | ||
|
||
pub struct SyntheticNode; | ||
|
||
impl SyntheticNode { | ||
pub fn new() -> Self { | ||
SyntheticNode | ||
} | ||
|
||
pub fn connect(&self, address: &str) -> Result<(), Box<dyn Error>> { | ||
let runtime = Runtime::new().expect("Failed to create Tokio runtime"); | ||
runtime.block_on(async { | ||
starknet::initialize_p2p(address).await | ||
}) | ||
} | ||
} |