From bcc738cb9d1e7049c74d2d5110a104cf7433da65 Mon Sep 17 00:00:00 2001 From: Daniel Knopik Date: Sat, 17 Sep 2022 14:31:57 +0200 Subject: [PATCH] progress on gossip stuff --- .../src/behaviour/gossip_cache.rs | 7 +++ .../lighthouse_network/src/types/pubsub.rs | 4 ++ .../lighthouse_network/src/types/topics.rs | 6 +- consensus/types/src/blob.rs | 53 +++++++++++++++++- consensus/types/src/blobs_sidecar.rs | 16 ++++++ consensus/types/src/bls_field_element.rs | 56 ++++++++++++++++++- consensus/types/src/lib.rs | 2 + consensus/types/src/signed_blobs_sidecar.rs | 13 +++++ 8 files changed, 153 insertions(+), 4 deletions(-) create mode 100644 consensus/types/src/blobs_sidecar.rs create mode 100644 consensus/types/src/signed_blobs_sidecar.rs diff --git a/beacon_node/lighthouse_network/src/behaviour/gossip_cache.rs b/beacon_node/lighthouse_network/src/behaviour/gossip_cache.rs index 4842605f7a..1c6ffd022d 100644 --- a/beacon_node/lighthouse_network/src/behaviour/gossip_cache.rs +++ b/beacon_node/lighthouse_network/src/behaviour/gossip_cache.rs @@ -20,6 +20,8 @@ pub struct GossipCache { topic_msgs: HashMap, Key>>, /// Timeout for blocks. beacon_block: Option, + /// Timeout for blobs. + blobs_sidecar: Option, /// Timeout for aggregate attestations. aggregates: Option, /// Timeout for attestations. @@ -41,6 +43,8 @@ pub struct GossipCacheBuilder { default_timeout: Option, /// Timeout for blocks. beacon_block: Option, + /// Timeout for blob sidecars. + blobs_sidecar: Option, /// Timeout for aggregate attestations. aggregates: Option, /// Timeout for attestations. @@ -117,6 +121,7 @@ impl GossipCacheBuilder { let GossipCacheBuilder { default_timeout, beacon_block, + blobs_sidecar, aggregates, attestation, voluntary_exit, @@ -129,6 +134,7 @@ impl GossipCacheBuilder { expirations: DelayQueue::default(), topic_msgs: HashMap::default(), beacon_block: beacon_block.or(default_timeout), + blobs_sidecar: blobs_sidecar.or(default_timeout), aggregates: aggregates.or(default_timeout), attestation: attestation.or(default_timeout), voluntary_exit: voluntary_exit.or(default_timeout), @@ -151,6 +157,7 @@ impl GossipCache { pub fn insert(&mut self, topic: GossipTopic, data: Vec) { let expire_timeout = match topic.kind() { GossipKind::BeaconBlock => self.beacon_block, + GossipKind::BlobsSidecar => self.blobs_sidecar, GossipKind::BeaconAggregateAndProof => self.aggregates, GossipKind::Attestation(_) => self.attestation, GossipKind::VoluntaryExit => self.voluntary_exit, diff --git a/beacon_node/lighthouse_network/src/types/pubsub.rs b/beacon_node/lighthouse_network/src/types/pubsub.rs index ef3b53abfb..3519dafcfd 100644 --- a/beacon_node/lighthouse_network/src/types/pubsub.rs +++ b/beacon_node/lighthouse_network/src/types/pubsub.rs @@ -14,11 +14,14 @@ use types::{ SignedBeaconBlockMerge, SignedBeaconBlockEip4844, SignedContributionAndProof, SignedVoluntaryExit, SubnetId, SyncCommitteeMessage, SyncSubnetId, }; +use types::signed_blobs_sidecar::SignedBlobsSidecar; #[derive(Debug, Clone, PartialEq)] pub enum PubsubMessage { /// Gossipsub message providing notification of a new block. BeaconBlock(Arc>), + /// Gossipsub message providing notification of a new blobs sidecar. + BlobsSidecars(Arc>), /// Gossipsub message providing notification of a Aggregate attestation and associated proof. AggregateAndProofAttestation(Box>), /// Gossipsub message providing notification of a raw un-aggregated attestation with its shard id. @@ -106,6 +109,7 @@ impl PubsubMessage { pub fn kind(&self) -> GossipKind { match self { PubsubMessage::BeaconBlock(_) => GossipKind::BeaconBlock, + PubsubMessage::BlobsSidecars(_) => GossipKind::BlobsSidecar, PubsubMessage::AggregateAndProofAttestation(_) => GossipKind::BeaconAggregateAndProof, PubsubMessage::Attestation(attestation_data) => { GossipKind::Attestation(attestation_data.0) diff --git a/beacon_node/lighthouse_network/src/types/topics.rs b/beacon_node/lighthouse_network/src/types/topics.rs index 825b1088b2..901a193e32 100644 --- a/beacon_node/lighthouse_network/src/types/topics.rs +++ b/beacon_node/lighthouse_network/src/types/topics.rs @@ -11,6 +11,7 @@ use crate::Subnet; pub const TOPIC_PREFIX: &str = "eth2"; pub const SSZ_SNAPPY_ENCODING_POSTFIX: &str = "ssz_snappy"; pub const BEACON_BLOCK_TOPIC: &str = "beacon_block"; +pub const BLOBS_SIDECAR_TOPIC: &str = "blobs_sidecar"; pub const BEACON_AGGREGATE_AND_PROOF_TOPIC: &str = "beacon_aggregate_and_proof"; pub const BEACON_ATTESTATION_PREFIX: &str = "beacon_attestation_"; pub const VOLUNTARY_EXIT_TOPIC: &str = "voluntary_exit"; @@ -19,8 +20,9 @@ pub const ATTESTER_SLASHING_TOPIC: &str = "attester_slashing"; pub const SIGNED_CONTRIBUTION_AND_PROOF_TOPIC: &str = "sync_committee_contribution_and_proof"; pub const SYNC_COMMITTEE_PREFIX_TOPIC: &str = "sync_committee_"; -pub const CORE_TOPICS: [GossipKind; 6] = [ +pub const CORE_TOPICS: [GossipKind; 7] = [ GossipKind::BeaconBlock, + GossipKind::BlobsSidecar, GossipKind::BeaconAggregateAndProof, GossipKind::VoluntaryExit, GossipKind::ProposerSlashing, @@ -47,6 +49,8 @@ pub struct GossipTopic { pub enum GossipKind { /// Topic for publishing beacon blocks. BeaconBlock, + /// Topic for publishing blob sidecars. + BlobsSidecar, /// Topic for publishing aggregate attestations and proofs. BeaconAggregateAndProof, /// Topic for publishing raw attestations on a particular subnet. diff --git a/consensus/types/src/blob.rs b/consensus/types/src/blob.rs index efe243859d..f44fbdd26d 100644 --- a/consensus/types/src/blob.rs +++ b/consensus/types/src/blob.rs @@ -1,6 +1,7 @@ use ssz_types::VariableList; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use ssz::{Decode, DecodeError, Encode}; +use tree_hash::TreeHash; use crate::test_utils::RngCore; use crate::bls_field_element::BlsFieldElement; use crate::{EthSpec, Uint256}; @@ -20,4 +21,54 @@ impl TestRandom for Blob { } res } -} \ No newline at end of file +} + +impl Encode for Blob { + fn is_ssz_fixed_len() -> bool { + as Encode>::is_ssz_fixed_len() + } + + fn ssz_fixed_len() -> usize { + as Encode>::ssz_fixed_len() + } + + fn ssz_bytes_len(&self) -> usize { + self.0.ssz_bytes_len() + } + + fn ssz_append(&self, buf: &mut Vec) { + self.0.ssz_append(buf) + } +} + +impl Decode for Blob { + fn is_ssz_fixed_len() -> bool { + as Decode>::is_ssz_fixed_len() + } + + fn ssz_fixed_len() -> usize { + as Decode>::ssz_fixed_len() + } + + fn from_ssz_bytes(bytes: &[u8]) -> Result { + >::from_ssz_bytes(bytes).map(Self) + } +} + +impl TreeHash for Blob { + fn tree_hash_type() -> tree_hash::TreeHashType { + >::tree_hash_type() + } + + fn tree_hash_packed_encoding(&self) -> Vec { + self.0.tree_hash_packed_encoding() + } + + fn tree_hash_packing_factor() -> usize { + >::tree_hash_packing_factor() + } + + fn tree_hash_root(&self) -> tree_hash::Hash256 { + self.0.tree_hash_root() + } +} diff --git a/consensus/types/src/blobs_sidecar.rs b/consensus/types/src/blobs_sidecar.rs new file mode 100644 index 0000000000..4d39c1af89 --- /dev/null +++ b/consensus/types/src/blobs_sidecar.rs @@ -0,0 +1,16 @@ +use ssz_types::VariableList; +use crate::{EthSpec, Hash256, Slot}; +use crate::blob::Blob; +use crate::kzg_proof::KzgProof; +use serde::{Serialize, Deserialize}; +use ssz_derive::{Encode, Decode}; +use tree_hash_derive::TreeHash; +use derivative::Derivative; + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode, TreeHash, Derivative)] +pub struct BlobsSidecar { + beacon_block_root: Hash256, + beacon_block_slot: Slot, + blobs: VariableList, T::MaxBlobsPerBlock>, + kzg_aggregate_proof: KzgProof, +} \ No newline at end of file diff --git a/consensus/types/src/bls_field_element.rs b/consensus/types/src/bls_field_element.rs index 6693b5765b..5dea91e07b 100644 --- a/consensus/types/src/bls_field_element.rs +++ b/consensus/types/src/bls_field_element.rs @@ -1,7 +1,59 @@ -use crate::Uint256; +use crate::{EthSpec, Uint256}; use serde::{Deserialize, Serialize}; use ssz::{Decode, DecodeError, Encode}; +use tree_hash::TreeHash; #[derive(Default, Debug, PartialEq, Hash, Clone, Copy, Serialize, Deserialize)] #[serde(transparent)] -pub struct BlsFieldElement(pub Uint256); \ No newline at end of file +pub struct BlsFieldElement(pub Uint256); + + +impl Encode for BlsFieldElement { + fn is_ssz_fixed_len() -> bool { + ::is_ssz_fixed_len() + } + + fn ssz_fixed_len() -> usize { + ::ssz_fixed_len() + } + + fn ssz_bytes_len(&self) -> usize { + self.0.ssz_bytes_len() + } + + fn ssz_append(&self, buf: &mut Vec) { + self.0.ssz_append(buf) + } +} + +impl Decode for BlsFieldElement { + fn is_ssz_fixed_len() -> bool { + ::is_ssz_fixed_len() + } + + fn ssz_fixed_len() -> usize { + ::ssz_fixed_len() + } + + fn from_ssz_bytes(bytes: &[u8]) -> Result { + ::from_ssz_bytes(bytes).map(Self) + } +} + +impl TreeHash for BlsFieldElement { + fn tree_hash_type() -> tree_hash::TreeHashType { + ::tree_hash_type() + } + + fn tree_hash_packed_encoding(&self) -> Vec { + self.0.tree_hash_packed_encoding() + } + + fn tree_hash_packing_factor() -> usize { + ::tree_hash_packing_factor() + } + + fn tree_hash_root(&self) -> tree_hash::Hash256 { + self.0.tree_hash_root() + } +} diff --git a/consensus/types/src/lib.rs b/consensus/types/src/lib.rs index 93c9b39afb..87f1b6f76b 100644 --- a/consensus/types/src/lib.rs +++ b/consensus/types/src/lib.rs @@ -96,6 +96,8 @@ pub mod kzg_commitment; pub mod kzg_proof; pub mod bls_field_element; pub mod blob; +pub mod signed_blobs_sidecar; +pub mod blobs_sidecar; use ethereum_types::{H160, H256}; diff --git a/consensus/types/src/signed_blobs_sidecar.rs b/consensus/types/src/signed_blobs_sidecar.rs new file mode 100644 index 0000000000..da9af06c76 --- /dev/null +++ b/consensus/types/src/signed_blobs_sidecar.rs @@ -0,0 +1,13 @@ +use bls::Signature; +use crate::blobs_sidecar::BlobsSidecar; +use crate::EthSpec; +use serde::{Serialize, Deserialize}; +use ssz_derive::{Encode, Decode}; +use tree_hash_derive::TreeHash; +use derivative::Derivative; + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode, TreeHash, Derivative)] +pub struct SignedBlobsSidecar { + pub message: BlobsSidecar, + pub signature: Signature, +} \ No newline at end of file