diff --git a/README.md b/README.md index 7c8738587..548e1551d 100644 --- a/README.md +++ b/README.md @@ -71,8 +71,8 @@ This milestone focuses on computing the chain state from the gossiped by peers r This milestone focuses on choosing the head of the chain based on gossiped attestations. -- Listen for attestations in gossipsub 🏗️ -- Implement fork-choice rule +- Listen for attestations in gossipsub ✅ +- Implement fork-choice rule 🏗️ - Apply fork-choice rule based on received attestations ### Produce blocks and attestations diff --git a/crates/blockchain/src/lib.rs b/crates/blockchain/src/lib.rs index f51eabd7b..2822145c1 100644 --- a/crates/blockchain/src/lib.rs +++ b/crates/blockchain/src/lib.rs @@ -1,7 +1,6 @@ use ethlambda_storage::Store; use ethlambda_types::{ - block::SignedBlockWithAttestation, - primitives::{Encode, TreeHash}, + attestation::SignedAttestation, block::SignedBlockWithAttestation, primitives::TreeHash, }; use spawned_concurrency::tasks::{CallResponse, CastResponse, GenServer, GenServerHandle}; use tracing::{error, info, warn}; @@ -27,6 +26,17 @@ impl BlockChain { .await .inspect_err(|err| error!(%err, "Failed to notify BlockChain of new block")); } + + /// Sends an attestation to the BlockChain for processing. + /// + /// Note that this is *NOT* `async`, since the internal [`GenServerHandle::cast`] is non-blocking. + pub async fn notify_new_attestation(&mut self, attestation: SignedAttestation) { + let _ = self + .handle + .cast(CastMessage::NewAttestation(attestation)) + .await + .inspect_err(|err| error!(%err, "Failed to notify BlockChain of new attestation")); + } } struct BlockChainServer { @@ -70,11 +80,14 @@ impl BlockChainServer { info!(%slot, %block_root, %state_root, "Processed new block"); update_head_slot(slot); } + + fn on_attestation(&mut self, attestation: SignedAttestation) {} } #[derive(Clone, Debug)] enum CastMessage { NewBlock(SignedBlockWithAttestation), + NewAttestation(SignedAttestation), } impl GenServer for BlockChainServer { @@ -103,6 +116,7 @@ impl GenServer for BlockChainServer { CastMessage::NewBlock(signed_block) => { self.on_block(signed_block); } + CastMessage::NewAttestation(attestation) => self.on_attestation(attestation), } CastResponse::NoReply } diff --git a/crates/common/types/src/attestation.rs b/crates/common/types/src/attestation.rs index 841bf6fda..504010719 100644 --- a/crates/common/types/src/attestation.rs +++ b/crates/common/types/src/attestation.rs @@ -2,7 +2,7 @@ use ssz_derive::{Decode, Encode}; use tree_hash_derive::TreeHash; use crate::{ - signature::Signature, + signature::SignatureSize, state::{Checkpoint, ValidatorRegistryLimit}, }; @@ -33,14 +33,18 @@ pub struct AttestationData { } /// Validator attestation bundled with its signature. -#[derive(Clone, Encode, Decode)] +#[derive(Debug, Clone, Encode, Decode)] pub struct SignedAttestation { + /// The index of the validator making the attestation. + pub validator_id: u64, /// The attestation message signed by the validator. - message: Attestation, + pub message: AttestationData, /// Signature aggregation produced by the leanVM (SNARKs in the future). - signature: Signature, + pub signature: XmssSignature, } +pub type XmssSignature = ssz_types::FixedVector; + /// Aggregated attestation consisting of participation bits and message. #[derive(Debug, Clone, Encode, Decode, TreeHash)] pub struct AggregatedAttestation { diff --git a/crates/common/types/src/block.rs b/crates/common/types/src/block.rs index 3cf515061..21884d5dc 100644 --- a/crates/common/types/src/block.rs +++ b/crates/common/types/src/block.rs @@ -3,9 +3,8 @@ use ssz_types::typenum::U1048576; use tree_hash_derive::TreeHash; use crate::{ - attestation::{AggregatedAttestation, Attestation}, + attestation::{AggregatedAttestation, Attestation, XmssSignature}, primitives::{ByteList, H256}, - signature::SignatureSize, state::ValidatorRegistryLimit, }; @@ -68,8 +67,6 @@ pub type AttestationSignatures = pub type NaiveAggregatedSignature = ssz_types::VariableList; -pub type XmssSignature = ssz_types::FixedVector; - /// Cryptographic proof that a set of validators signed a message. /// /// This container encapsulates the output of the leanVM signature aggregation, diff --git a/crates/net/p2p/src/gossipsub.rs b/crates/net/p2p/src/gossipsub.rs index 2007e51fc..b3bc31b2e 100644 --- a/crates/net/p2p/src/gossipsub.rs +++ b/crates/net/p2p/src/gossipsub.rs @@ -1,5 +1,5 @@ use ethlambda_blockchain::BlockChain; -use ethlambda_types::block::SignedBlockWithAttestation; +use ethlambda_types::{attestation::SignedAttestation, block::SignedBlockWithAttestation}; use libp2p::gossipsub::Event; use ssz::Decode; use tracing::{error, info, trace}; @@ -36,10 +36,21 @@ pub async fn handle_gossipsub_message(blockchain: &mut BlockChain, event: Event) blockchain.notify_new_block(signed_block).await; } Some(ATTESTATION_TOPIC_KIND) => { - info!( - "Received attestation gossip message of size {} bytes", - message.data.len() - ); + let Ok(uncompressed_data) = decompress_message(&message.data) + .inspect_err(|err| error!(%err, "Failed to decompress gossipped attestation")) + else { + return; + }; + + let Ok(signed_attestation) = SignedAttestation::from_ssz_bytes(&uncompressed_data) + .inspect_err(|err| error!(?err, "Failed to decode gossipped attestation")) + else { + return; + }; + let slot = signed_attestation.message.slot; + let validator = signed_attestation.validator_id; + info!(%slot, %validator, "Received new attestation from gossipsub, sending for processing"); + blockchain.notify_new_attestation(signed_attestation).await; } _ => { trace!("Received message on unknown topic: {}", message.topic);