mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-17 03:42:46 +00:00
Ethereum 2.0 Network Specification Upgrade (#510)
Updates lighthouse to the latest networking spec - Sync re-write (#496) - Updates to the latest eth2 networking spec (#495) - Libp2p updates and improvements
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use crate::config::*;
|
||||
use crate::discovery::Discovery;
|
||||
use crate::rpc::{RPCEvent, RPCMessage, RPC};
|
||||
use crate::{error, NetworkConfig};
|
||||
@@ -15,7 +16,6 @@ use libp2p::{
|
||||
NetworkBehaviour, PeerId,
|
||||
};
|
||||
use slog::{debug, o, trace};
|
||||
use ssz::{ssz_encode, Encode};
|
||||
use std::num::NonZeroU32;
|
||||
use std::time::Duration;
|
||||
|
||||
@@ -91,7 +91,7 @@ impl<TSubstream: AsyncRead + AsyncWrite> NetworkBehaviourEventProcess<GossipsubE
|
||||
fn inject_event(&mut self, event: GossipsubEvent) {
|
||||
match event {
|
||||
GossipsubEvent::Message(gs_msg) => {
|
||||
trace!(self.log, "Received GossipEvent"; "msg" => format!("{:?}", gs_msg));
|
||||
trace!(self.log, "Received GossipEvent");
|
||||
|
||||
let msg = PubsubMessage::from_topics(&gs_msg.topics, gs_msg.data);
|
||||
|
||||
@@ -192,10 +192,10 @@ impl<TSubstream: AsyncRead + AsyncWrite> Behaviour<TSubstream> {
|
||||
}
|
||||
|
||||
/// Publishes a message on the pubsub (gossipsub) behaviour.
|
||||
pub fn publish(&mut self, topics: Vec<Topic>, message: PubsubMessage) {
|
||||
let message_bytes = ssz_encode(&message);
|
||||
pub fn publish(&mut self, topics: &[Topic], message: PubsubMessage) {
|
||||
let message_data = message.to_data();
|
||||
for topic in topics {
|
||||
self.gossipsub.publish(topic, message_bytes.clone());
|
||||
self.gossipsub.publish(topic, message_data.clone());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -224,13 +224,20 @@ pub enum BehaviourEvent {
|
||||
},
|
||||
}
|
||||
|
||||
/// Messages that are passed to and from the pubsub (Gossipsub) behaviour.
|
||||
/// Messages that are passed to and from the pubsub (Gossipsub) behaviour. These are encoded and
|
||||
/// decoded upstream.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum PubsubMessage {
|
||||
/// Gossipsub message providing notification of a new block.
|
||||
Block(Vec<u8>),
|
||||
/// Gossipsub message providing notification of a new attestation.
|
||||
Attestation(Vec<u8>),
|
||||
/// Gossipsub message providing notification of a voluntary exit.
|
||||
VoluntaryExit(Vec<u8>),
|
||||
/// Gossipsub message providing notification of a new proposer slashing.
|
||||
ProposerSlashing(Vec<u8>),
|
||||
/// Gossipsub message providing notification of a new attester slashing.
|
||||
AttesterSlashing(Vec<u8>),
|
||||
/// Gossipsub message from an unknown topic.
|
||||
Unknown(Vec<u8>),
|
||||
}
|
||||
@@ -244,29 +251,33 @@ impl PubsubMessage {
|
||||
*/
|
||||
fn from_topics(topics: &Vec<TopicHash>, data: Vec<u8>) -> Self {
|
||||
for topic in topics {
|
||||
match topic.as_str() {
|
||||
BEACON_BLOCK_TOPIC => return PubsubMessage::Block(data),
|
||||
BEACON_ATTESTATION_TOPIC => return PubsubMessage::Attestation(data),
|
||||
_ => {}
|
||||
// compare the prefix and postfix, then match on the topic
|
||||
let topic_parts: Vec<&str> = topic.as_str().split('/').collect();
|
||||
if topic_parts.len() == 4
|
||||
&& topic_parts[1] == TOPIC_PREFIX
|
||||
&& topic_parts[3] == TOPIC_ENCODING_POSTFIX
|
||||
{
|
||||
match topic_parts[2] {
|
||||
BEACON_BLOCK_TOPIC => return PubsubMessage::Block(data),
|
||||
BEACON_ATTESTATION_TOPIC => return PubsubMessage::Attestation(data),
|
||||
VOLUNTARY_EXIT_TOPIC => return PubsubMessage::VoluntaryExit(data),
|
||||
PROPOSER_SLASHING_TOPIC => return PubsubMessage::ProposerSlashing(data),
|
||||
ATTESTER_SLASHING_TOPIC => return PubsubMessage::AttesterSlashing(data),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
PubsubMessage::Unknown(data)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encode for PubsubMessage {
|
||||
fn is_ssz_fixed_len() -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn ssz_append(&self, buf: &mut Vec<u8>) {
|
||||
fn to_data(self) -> Vec<u8> {
|
||||
match self {
|
||||
PubsubMessage::Block(inner)
|
||||
| PubsubMessage::Attestation(inner)
|
||||
| PubsubMessage::Unknown(inner) => {
|
||||
// Encode the gossip as a Vec<u8>;
|
||||
buf.append(&mut inner.as_ssz_bytes());
|
||||
}
|
||||
PubsubMessage::Block(data)
|
||||
| PubsubMessage::Attestation(data)
|
||||
| PubsubMessage::VoluntaryExit(data)
|
||||
| PubsubMessage::ProposerSlashing(data)
|
||||
| PubsubMessage::AttesterSlashing(data)
|
||||
| PubsubMessage::Unknown(data) => data,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user