Skip to content

Commit

Permalink
fix: p2p encoding, metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Vid201 committed Dec 8, 2023
1 parent 78d9a5e commit 96d8054
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 72 deletions.
26 changes: 19 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/p2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ ssz_rs = { workspace = true }
ssz_rs_derive = { workspace = true }
thiserror = "1"
tokio = { workspace = true }
tokio-util = { version = "0.7.10", features = ["codec"] }
tracing = { workspace = true }
unsigned-varint = { version = "0.8.0", features = ["codec"] }

[dependencies.libp2p]
version = "0.52.3"
Expand Down
7 changes: 7 additions & 0 deletions crates/p2p/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
use discv5::ListenConfig;
use libp2p::{multiaddr::Protocol, Multiaddr};
use ssz_rs::Bitvector;
use std::net::{Ipv4Addr, Ipv6Addr};

const DEFAULT_UDP_PORT: u16 = 9000;
const DEFAULT_TCP_PORT: u16 = 9000;

#[derive(ssz_rs_derive::Serializable, Clone, Debug, PartialEq, Default)]
pub struct Metadata {
pub seq_number: u64,
pub mempool_nets: Bitvector<64>,
}

/// The address to listen on for incoming connections.
/// Ip could be ipv4 or ipv6
#[derive(Clone, Debug)]
Expand Down
29 changes: 23 additions & 6 deletions crates/p2p/src/network.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::{
behaviour::Behaviour,
config::Config,
config::{Config, Metadata},
discovery,
enr::{keypair_to_combined, EnrExt},
gossipsub::topic,
peer_manager::PeerManagerEvent,
request_response::{self, Ping, Request, RequestId, Response},
request_response::{self, Ping, Pong, Request, RequestId, Response},
};
use alloy_chains::Chain;
use discv5::Enr;
Expand Down Expand Up @@ -83,11 +83,13 @@ pub type EntrypointChannels = Vec<(
UnboundedReceiver<(UserOperation, U256)>,
UnboundedSender<UserOperation>,
)>;

/// P2P network struct that holds the libp2p Swarm
/// Other components should interact with Network directly instead of behaviour
pub struct Network {
swarm: Swarm<Behaviour>,
entrypoint_channels: EntrypointChannels,
metadata: Metadata,
}

impl From<Network> for Swarm<Behaviour> {
Expand Down Expand Up @@ -139,9 +141,17 @@ impl Network {
.with_behaviour(|_| behaviour)
.expect("building p2p behaviour failed")
.build();

// metadata
let metadata = Metadata {
seq_number: 0,
..Default::default() // FIXME: when mempool id of canonical will be known
};

Ok(Self {
swarm,
entrypoint_channels,
metadata,
})
}

Expand Down Expand Up @@ -205,7 +215,15 @@ impl Network {
..
} => match request {
Request::Ping(_ping) => {
let response = Response::Pong(Default::default());
// TODO: need to update metadata of peer (based on ping value)
let response = Response::Pong(Pong::new(self.metadata.seq_number));
response_sender
.send(response)
.expect("channel should exist");
None
}
Request::GetMetadata(_) => {
let response = Response::Metadata(self.metadata.clone());
response_sender
.send(response)
.expect("channel should exist");
Expand Down Expand Up @@ -233,8 +251,7 @@ impl Network {
fn handler_peer_manager_event(&mut self, event: PeerManagerEvent) -> Option<NetworkEvent> {
match event {
PeerManagerEvent::Ping(peer) => {
// FIXME: seq number should be a counter
self.send_request(&peer, Request::Ping(Ping::new(1)));
self.send_request(&peer, Request::Ping(Ping::new(self.metadata.seq_number)));
None
}
PeerManagerEvent::PeerConnectedIncoming(peer)
Expand Down Expand Up @@ -282,7 +299,7 @@ impl Network {
}

while let Poll::Ready(Some(swarm_event)) = self.swarm.poll_next_unpin(cx) {
info!("Swarm get event {swarm_event:?}");
info!("Swarm event {swarm_event:?}");
let event_opt = match swarm_event {
SwarmEvent::Behaviour(e) => match e {
crate::behaviour::Event::GossipSub(event) => self.handle_gossipsub_event(event),
Expand Down
4 changes: 4 additions & 0 deletions crates/p2p/src/peer_manager/peerdb.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use crate::config::Metadata;
use libp2p::PeerId;
use std::collections::HashMap;

pub enum ConnectionStatus {
Connected,
Disconnected,
}

pub struct PeerInfo {
connection_status: ConnectionStatus,
_metadata: Option<Metadata>, // TODO: need to handle metadata updates
}

#[derive(Default)]
Expand All @@ -27,6 +30,7 @@ impl PeerDB {
.and_modify(|info| info.connection_status = ConnectionStatus::Connected)
.or_insert(PeerInfo {
connection_status: ConnectionStatus::Connected,
_metadata: None,
});
}

Expand Down
2 changes: 1 addition & 1 deletion crates/p2p/src/request_response/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub enum InboundFailure {
/// due to the [`ResponseChannel`] being dropped instead of
/// being passed to [`Behaviour::send_response`].
ResponseOmission,
/// Error happended while handleing the inbound
/// Error happended while handling the inbound
BoundError(BoundError),
}
#[derive(Debug)]
Expand Down
Loading

0 comments on commit 96d8054

Please sign in to comment.