Skip to content

Commit

Permalink
feat:use listen config in network driver
Browse files Browse the repository at this point in the history
Signed-off-by: Chen Kai <[email protected]>
  • Loading branch information
GrapeBaBa committed Aug 29, 2024
1 parent e7e8b66 commit 3f991bb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
25 changes: 20 additions & 5 deletions crates/net/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Network Builder Module.
use alloy::primitives::Address;
use eyre::Result;
use eyre::{Report, Result};
use std::net::{IpAddr, SocketAddr};
use discv5::ListenConfig;
use discv5::ListenConfig::{DualStack, Ipv4, Ipv6};
Expand Down Expand Up @@ -159,6 +159,16 @@ impl NetworkDriverBuilder {
/// .with_socket(socket)
/// .build()
/// .unwrap();
///
/// Or if you want to use a listen config instead of a socket
///
/// let listen_config = ListenConfig::from_ip(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 9099);
/// let driver = NetworkDriverBuilder::new()
/// .with_unsafe_block_signer(signer)
/// .with_chain_id(chain_id)
/// .with_listen_config(listen_config)
/// .build()
/// .unwrap();
/// ```
pub fn build(&mut self) -> Result<NetworkDriver> {
// Build the config for gossipsub.
Expand Down Expand Up @@ -192,10 +202,15 @@ impl NetworkDriverBuilder {
)?
.with_behaviour(|_| behaviour)?
.build();
let listen_config = self.listen_config.take().ok_or_else(|| {
let addr = self.socket.ok_or_else(|| eyre::eyre!("address not set"))?;
Ok(ListenConfig::from_ip(addr.ip(), addr.port()))
})?;

// Build the gossip driver.
let listen_config = self.listen_config.take().map_or_else(
|| {
let addr = self.socket.ok_or(eyre::eyre!("socket not set"))?;
Ok::<ListenConfig, Report>(ListenConfig::from(addr))
},
Ok,
)?;
let mut multiaddr = Multiaddr::empty();
match listen_config {
Ipv4 { ip: addr, port } => {
Expand Down
13 changes: 8 additions & 5 deletions crates/net/src/discovery/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use discv5::{
enr::{CombinedKey, Enr},
ConfigBuilder, Discv5, ListenConfig,
};
use eyre::Result;
use eyre::{Report, Result};
use std::net::SocketAddr;

use crate::types::enr::OP_CL_KEY;
Expand Down Expand Up @@ -55,10 +55,13 @@ impl DiscoveryBuilder {

let key = CombinedKey::generate_secp256k1();
let enr = Enr::builder().add_value_rlp(OP_CL_KEY, opstack_data.into()).build(&key)?;
let listen_config = self.listen_config.take().unwrap_or_else(|| {
let addr = self.address.expect("address not set");
ListenConfig::from_ip(addr.ip(), addr.port())
});
let listen_config = self.listen_config.take().map_or_else(
|| {
let addr = self.address.ok_or(eyre::eyre!("address not set"))?;
Ok::<ListenConfig, Report>(ListenConfig::from(addr))
},
Ok,
)?;
let config = ConfigBuilder::new(listen_config).build();

let disc = Discv5::new(enr, key, config)
Expand Down

0 comments on commit 3f991bb

Please sign in to comment.