-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(network): start network impl and rpc handling
- Loading branch information
Showing
19 changed files
with
1,951 additions
and
43 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// This file is part of Rundler. | ||
// | ||
// Rundler is free software: you can redistribute it and/or modify it under the | ||
// terms of the GNU Lesser General Public License as published by the Free Software | ||
// Foundation, either version 3 of the License, or (at your option) any later version. | ||
// | ||
// Rundler is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
// See the GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License along with Rundler. | ||
// If not, see https://www.gnu.org/licenses/. | ||
|
||
use std::{net::SocketAddr, time::Duration}; | ||
|
||
use clap::Parser; | ||
use ethers::types::H256; | ||
use rundler_network::{Config as NetworkConfig, ConnectionConfig, Network}; | ||
use tokio::sync::mpsc; | ||
|
||
const PRIVATE_KEY: &str = "b0ddfec7d365b4599ff8367e960f8c4890364f99e2151beac352338cc0cfe1bc"; | ||
|
||
#[tokio::main] | ||
async fn main() -> anyhow::Result<()> { | ||
env_logger::init(); | ||
|
||
let cli = Cli::parse(); | ||
|
||
let private_key = if cli.bootnode.is_none() { | ||
println!("Starting as a bootnode"); | ||
PRIVATE_KEY.into() | ||
} else { | ||
println!("Starting as a regular node"); | ||
let enr_key = discv5::enr::CombinedKey::generate_secp256k1(); | ||
hex::encode(enr_key.encode()) | ||
}; | ||
|
||
let bootnodes = cli | ||
.bootnode | ||
.map(|b| vec![b.parse().expect("invalid bootnode")]) | ||
.unwrap_or_default(); | ||
let listen_address: SocketAddr = format!("127.0.0.1:{}", cli.port).parse()?; | ||
|
||
let config = NetworkConfig { | ||
private_key, | ||
listen_address, | ||
bootnodes, | ||
network_config: ConnectionConfig { | ||
max_chunk_size: 1048576, | ||
request_timeout: Duration::from_secs(10), | ||
ttfb_timeout: Duration::from_secs(5), | ||
}, | ||
supported_mempools: vec![H256::random()], | ||
metadata_seq_number: 0, | ||
}; | ||
|
||
let (_, action_recv) = mpsc::unbounded_channel(); | ||
let (event_send, _) = mpsc::unbounded_channel(); | ||
|
||
println!("Config: {:?}", config); | ||
let network = Network::new(config, event_send, action_recv).await?; | ||
println!("ENR: {}", network.enr()); | ||
|
||
network.run().await?; | ||
Ok(()) | ||
} | ||
|
||
#[derive(Debug, Parser)] | ||
#[clap(name = "rundler network tester")] | ||
struct Cli { | ||
/// The port used to listen on all interfaces | ||
#[clap(short)] | ||
port: u16, | ||
|
||
#[clap(short)] | ||
bootnode: Option<String>, | ||
} |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// This file is part of Rundler. | ||
// | ||
// Rundler is free software: you can redistribute it and/or modify it under the | ||
// terms of the GNU Lesser General Public License as published by the Free Software | ||
// Foundation, either version 3 of the License, or (at your option) any later version. | ||
// | ||
// Rundler is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
// See the GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License along with Rundler. | ||
// If not, see https://www.gnu.org/licenses/. | ||
|
||
use libp2p::{ | ||
identify, | ||
swarm::{keep_alive, NetworkBehaviour}, | ||
}; | ||
|
||
use crate::rpc; | ||
|
||
#[derive(NetworkBehaviour)] | ||
pub(crate) struct Behaviour { | ||
// TODO(danc): temp, remove when not needed | ||
pub(crate) keep_alive: keep_alive::Behaviour, | ||
// Request/response protocol | ||
pub(crate) rpc: rpc::Behaviour, | ||
// Identity protocol | ||
pub(crate) identify: identify::Behaviour, | ||
} |
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,98 @@ | ||
// This file is part of Rundler. | ||
// | ||
// Rundler is free software: you can redistribute it and/or modify it under the | ||
// terms of the GNU Lesser General Public License as published by the Free Software | ||
// Foundation, either version 3 of the License, or (at your option) any later version. | ||
// | ||
// Rundler is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
// See the GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License along with Rundler. | ||
// If not, see https://www.gnu.org/licenses/. | ||
|
||
// Adapted from https://github.com/sigp/lighthouse/blob/stable/beacon_node/lighthouse_network/src/discovery/enr_ext.rs | ||
|
||
use discv5::{ | ||
enr::{k256, CombinedKey, CombinedPublicKey, EnrBuilder}, | ||
Enr, | ||
}; | ||
use libp2p::{ | ||
identity::{ed25519, secp256k1, PublicKey}, | ||
multiaddr::Protocol, | ||
Multiaddr, PeerId, | ||
}; | ||
|
||
use crate::Config; | ||
|
||
pub(crate) fn build_enr(config: &Config) -> (Enr, CombinedKey) { | ||
let enr_key: CombinedKey = | ||
k256::ecdsa::SigningKey::from_slice(&hex::decode(&config.private_key).unwrap()) | ||
.unwrap() | ||
.into(); | ||
|
||
let enr = EnrBuilder::new("v4") | ||
.ip(config.listen_address.ip()) | ||
.tcp4(config.listen_address.port()) | ||
.udp4(config.listen_address.port() + 1) | ||
.build(&enr_key) | ||
.unwrap(); | ||
|
||
(enr, enr_key) | ||
} | ||
|
||
pub(crate) trait EnrExt { | ||
fn multiaddr(&self) -> Vec<Multiaddr>; | ||
|
||
fn peer_id(&self) -> PeerId; | ||
} | ||
|
||
impl EnrExt for Enr { | ||
fn multiaddr(&self) -> Vec<Multiaddr> { | ||
let mut multiaddrs: Vec<Multiaddr> = Vec::new(); | ||
if let Some(ip) = self.ip4() { | ||
if let Some(udp) = self.udp4() { | ||
let mut multiaddr: Multiaddr = ip.into(); | ||
multiaddr.push(Protocol::Udp(udp)); | ||
multiaddrs.push(multiaddr); | ||
} | ||
if let Some(tcp) = self.tcp4() { | ||
let mut multiaddr: Multiaddr = ip.into(); | ||
multiaddr.push(Protocol::Tcp(tcp)); | ||
multiaddrs.push(multiaddr); | ||
} | ||
} | ||
multiaddrs | ||
} | ||
|
||
fn peer_id(&self) -> PeerId { | ||
self.public_key().as_peer_id() | ||
} | ||
} | ||
|
||
pub(crate) trait CombinedKeyPublicExt { | ||
/// Converts the publickey into a peer id, without consuming the key. | ||
fn as_peer_id(&self) -> PeerId; | ||
} | ||
|
||
impl CombinedKeyPublicExt for CombinedPublicKey { | ||
/// Converts the publickey into a peer id, without consuming the key. | ||
fn as_peer_id(&self) -> PeerId { | ||
match self { | ||
Self::Secp256k1(pk) => { | ||
let pk_bytes = pk.to_sec1_bytes(); | ||
let libp2p_pk: PublicKey = secp256k1::PublicKey::try_from_bytes(&pk_bytes) | ||
.expect("valid public key") | ||
.into(); | ||
PeerId::from_public_key(&libp2p_pk) | ||
} | ||
Self::Ed25519(pk) => { | ||
let pk_bytes = pk.to_bytes(); | ||
let libp2p_pk: PublicKey = ed25519::PublicKey::try_from_bytes(&pk_bytes) | ||
.expect("valid public key") | ||
.into(); | ||
PeerId::from_public_key(&libp2p_pk) | ||
} | ||
} | ||
} | ||
} |
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,19 @@ | ||
// This file is part of Rundler. | ||
// | ||
// Rundler is free software: you can redistribute it and/or modify it under the | ||
// terms of the GNU Lesser General Public License as published by the Free Software | ||
// Foundation, either version 3 of the License, or (at your option) any later version. | ||
// | ||
// Rundler is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
// See the GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License along with Rundler. | ||
// If not, see https://www.gnu.org/licenses/. | ||
|
||
/// Network errors | ||
#[derive(thiserror::Error, Debug)] | ||
pub enum Error {} | ||
|
||
/// Network result | ||
pub type Result<T> = std::result::Result<T, Error>; |
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
Oops, something went wrong.