Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions crates/blockchain/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
12 changes: 8 additions & 4 deletions crates/common/types/src/attestation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use ssz_derive::{Decode, Encode};
use tree_hash_derive::TreeHash;

use crate::{
signature::Signature,
signature::SignatureSize,
state::{Checkpoint, ValidatorRegistryLimit},
};

Expand Down Expand Up @@ -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<u8, SignatureSize>;

/// Aggregated attestation consisting of participation bits and message.
#[derive(Debug, Clone, Encode, Decode, TreeHash)]
pub struct AggregatedAttestation {
Expand Down
5 changes: 1 addition & 4 deletions crates/common/types/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -68,8 +67,6 @@ pub type AttestationSignatures =

pub type NaiveAggregatedSignature = ssz_types::VariableList<XmssSignature, ValidatorRegistryLimit>;

pub type XmssSignature = ssz_types::FixedVector<u8, SignatureSize>;

/// Cryptographic proof that a set of validators signed a message.
///
/// This container encapsulates the output of the leanVM signature aggregation,
Expand Down
21 changes: 16 additions & 5 deletions crates/net/p2p/src/gossipsub.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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);
Expand Down