mirror of
https://gitlab.com/pulsechaincom/lighthouse-pulse.git
synced 2025-01-04 02:04:28 +00:00
Correct gossipsub message encoding. Add extended topics
This commit is contained in:
parent
d83fa67068
commit
80f15f5d70
@ -1,8 +1,8 @@
|
|||||||
|
use crate::config::*;
|
||||||
use crate::discovery::Discovery;
|
use crate::discovery::Discovery;
|
||||||
use crate::rpc::{RPCEvent, RPCMessage, RPC};
|
use crate::rpc::{RPCEvent, RPCMessage, RPC};
|
||||||
use crate::{error, NetworkConfig};
|
use crate::{error, NetworkConfig};
|
||||||
use crate::{Topic, TopicHash};
|
use crate::{Topic, TopicHash};
|
||||||
use crate::{BEACON_ATTESTATION_TOPIC, BEACON_BLOCK_TOPIC};
|
|
||||||
use futures::prelude::*;
|
use futures::prelude::*;
|
||||||
use libp2p::{
|
use libp2p::{
|
||||||
core::identity::Keypair,
|
core::identity::Keypair,
|
||||||
@ -15,7 +15,6 @@ use libp2p::{
|
|||||||
NetworkBehaviour, PeerId,
|
NetworkBehaviour, PeerId,
|
||||||
};
|
};
|
||||||
use slog::{debug, o, trace};
|
use slog::{debug, o, trace};
|
||||||
use ssz::{ssz_encode, Encode};
|
|
||||||
use std::num::NonZeroU32;
|
use std::num::NonZeroU32;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
@ -189,9 +188,9 @@ impl<TSubstream: AsyncRead + AsyncWrite> Behaviour<TSubstream> {
|
|||||||
|
|
||||||
/// Publishes a message on the pubsub (gossipsub) behaviour.
|
/// Publishes a message on the pubsub (gossipsub) behaviour.
|
||||||
pub fn publish(&mut self, topics: Vec<Topic>, message: PubsubMessage) {
|
pub fn publish(&mut self, topics: Vec<Topic>, message: PubsubMessage) {
|
||||||
let message_bytes = ssz_encode(&message);
|
let message_data = message.to_data();
|
||||||
for topic in topics {
|
for topic in topics {
|
||||||
self.gossipsub.publish(topic, message_bytes.clone());
|
self.gossipsub.publish(topic, message_data.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -220,13 +219,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)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum PubsubMessage {
|
pub enum PubsubMessage {
|
||||||
/// Gossipsub message providing notification of a new block.
|
/// Gossipsub message providing notification of a new block.
|
||||||
Block(Vec<u8>),
|
Block(Vec<u8>),
|
||||||
/// Gossipsub message providing notification of a new attestation.
|
/// Gossipsub message providing notification of a new attestation.
|
||||||
Attestation(Vec<u8>),
|
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.
|
/// Gossipsub message from an unknown topic.
|
||||||
Unknown(Vec<u8>),
|
Unknown(Vec<u8>),
|
||||||
}
|
}
|
||||||
@ -240,29 +246,33 @@ impl PubsubMessage {
|
|||||||
*/
|
*/
|
||||||
fn from_topics(topics: &Vec<TopicHash>, data: Vec<u8>) -> Self {
|
fn from_topics(topics: &Vec<TopicHash>, data: Vec<u8>) -> Self {
|
||||||
for topic in topics {
|
for topic in topics {
|
||||||
match topic.as_str() {
|
// compare the prefix and postfix, then match on the topic
|
||||||
BEACON_BLOCK_TOPIC => return PubsubMessage::Block(data),
|
let topic_parts: Vec<&str> = topic.as_str().split('/').collect();
|
||||||
BEACON_ATTESTATION_TOPIC => return PubsubMessage::Attestation(data),
|
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)
|
PubsubMessage::Unknown(data)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl Encode for PubsubMessage {
|
fn to_data(self) -> Vec<u8> {
|
||||||
fn is_ssz_fixed_len() -> bool {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
|
|
||||||
fn ssz_append(&self, buf: &mut Vec<u8>) {
|
|
||||||
match self {
|
match self {
|
||||||
PubsubMessage::Block(inner)
|
PubsubMessage::Block(data)
|
||||||
| PubsubMessage::Attestation(inner)
|
| PubsubMessage::Attestation(data)
|
||||||
| PubsubMessage::Unknown(inner) => {
|
| PubsubMessage::VoluntaryExit(data)
|
||||||
// Encode the gossip as a Vec<u8>;
|
| PubsubMessage::ProposerSlashing(data)
|
||||||
buf.append(&mut inner.as_ssz_bytes());
|
| PubsubMessage::AttesterSlashing(data)
|
||||||
}
|
| PubsubMessage::Unknown(data) => data,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user