Skip to content

Commit

Permalink
address review
Browse files Browse the repository at this point in the history
Co-authored-by: João Oliveira <[email protected]>
  • Loading branch information
dknopik and jxs committed Feb 17, 2025
1 parent 97ce099 commit a2c4e43
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0x00000001
00000001
3 changes: 1 addition & 2 deletions anchor/common/ssv_types/src/domain_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ impl FromStr for DomainType {
type Err = String;

fn from_str(hex_str: &str) -> Result<Self, Self::Err> {
let bytes = hex::decode(hex_str.strip_prefix("0x").unwrap_or(hex_str))
.map_err(|e| format!("Invalid domain type hex: {}", e))?;
let bytes = hex::decode(hex_str).map_err(|e| format!("Invalid domain type hex: {}", e))?;
if bytes.len() != 4 {
return Err("Domain type must be 4 bytes".into());
}
Expand Down
1 change: 1 addition & 0 deletions anchor/network/src/handshake/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl Codec {
let envelope = node_info.seal(&self.keypair)?;
let raw = envelope.encode_to_vec()?;
io.write_all(&raw).await?;
io.flush().await?;
io.close().await?;
Ok(())
}
Expand Down
19 changes: 14 additions & 5 deletions anchor/network/src/handshake/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,35 @@ pub type Event = <Behaviour as NetworkBehaviour>::ToSwarm;

#[derive(Debug)]
pub enum Error {
/// We are not on the same network as the remote
NetworkMismatch { ours: String, theirs: String },
/// Serialization/Deserialization of the Node Info.
NodeInfo(node_info::Error),
/// Error occurred while handling an incoming handshake.
Inbound(InboundFailure),
/// Error occurred while handling an outgoing handshake.
Outbound(OutboundFailure),
}

/// Event emitted on handshake completion or failure.
/// We successfully completed a handshake.
#[derive(Debug)]
pub struct Completed {
pub peer_id: PeerId,
pub their_info: NodeInfo,
}

/// The handshake either failed because of shaking with an incompatible peer or because of some
/// network failure.
#[derive(Debug)]
pub struct Failed {
pub peer_id: PeerId,
pub error: Box<Error>,
}

/// Create a libp2p Behaviour to handle handshake requests. Events emitted from this event must be
/// fed into [`handle_event`].
pub fn create_behaviour(keypair: Keypair) -> Behaviour {
// NodeInfoProtocol is the protocol.ID used for handshake
const NODE_INFO_PROTOCOL: &str = "/ssv/info/0.0.1";

let protocol = StreamProtocol::new(NODE_INFO_PROTOCOL);
let protocol = StreamProtocol::new("/ssv/info/0.0.1");
Behaviour::with_codec(
Codec::new(keypair),
[(protocol, ProtocolSupport::Full)],
Expand All @@ -59,6 +64,8 @@ fn verify_node_info(ours: &NodeInfo, theirs: &NodeInfo) -> Result<(), Error> {
Ok(())
}

/// Handle an [`Event`] emitted by the passed [`Behaviour`]. The passed [`NodeInfo`] is used for
/// validating the remote peer's data and for responding to incoming requests.
pub fn handle_event(
our_node_info: &NodeInfo,
behaviour: &mut Behaviour,
Expand Down Expand Up @@ -148,6 +155,8 @@ fn handle_response(
})
}

/// Send a handshake request to a specified peer. Should be called after establishing an outgoing
/// connection.
pub fn initiate(our_node_info: &NodeInfo, behaviour: &mut Behaviour, peer_id: PeerId) {
trace!(?peer_id, "initiating handshake");
behaviour.send_request(&peer_id, our_node_info.clone());
Expand Down
36 changes: 19 additions & 17 deletions anchor/network/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,13 @@ impl Network {
}
}
AnchorBehaviourEvent::Handshake(event) => {
handshake::handle_event(
if let Some(result) = handshake::handle_event(
&self.node_info,
&mut self.swarm.behaviour_mut().handshake,
event,
);
) {
self.handle_handshake_result(result);
}
}
// TODO handle other behaviour events
_ => {
Expand Down Expand Up @@ -237,27 +239,27 @@ impl Network {
}
}
}

fn handle_handshake_result(&mut self, result: Result<handshake::Completed, handshake::Failed>) {
match result {
Ok(handshake::Completed {
peer_id,
their_info,
}) => {
debug!(%peer_id, ?their_info, "Handshake completed");
// Update peer store with their_info
}
Err(handshake::Failed { peer_id, error }) => {
debug!(%peer_id, ?error, "Handshake failed");
}
}
}
}

fn subnet_to_topic(subnet: SubnetId) -> IdentTopic {
IdentTopic::new(format!("ssv.{}", *subnet))
}

fn handle_handshake_result(result: Result<handshake::Completed, handshake::Failed>) {
match result {
Ok(handshake::Completed {
peer_id,
their_info,
}) => {
debug!(%peer_id, ?their_info, "Handshake completed");
// Update peer store with their_info
}
Err(handshake::Failed { peer_id, error }) => {
debug!(%peer_id, ?error, "Handshake failed");
}
}
}

async fn build_anchor_behaviour(
local_keypair: Keypair,
network_config: &Config,
Expand Down

0 comments on commit a2c4e43

Please sign in to comment.