Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
4517: Implementation for 'Part 2 : Update Gossipers' r=jacek-casper a=jacek-casper

Some notes:

- I've updated tests that were explicitly creating signatures with specific values to create V2 variants of them, I can update them to randomly create either V1 or v2 (that'll complicate some of the them a bit though)
- ~~`BlockSignatures::merge` can combine a mixture of V1 and V2, but I am not sure if that can ever happen, it might make sense to just reject mismatching versions (atm it only checks height and chain name hash when we have 2 V2 signatures)~~
- ~~`BlockAcceptor` and many other places in the code have no access to the chain name, so I left them returning the V1 of BlockSignatures~~

Co-authored-by: Jacek Malec <[email protected]>
  • Loading branch information
casperlabs-bors-ng[bot] and jacek-casper authored Feb 23, 2024
2 parents 0e5c843 + 61bedb7 commit f5e1836
Show file tree
Hide file tree
Showing 41 changed files with 3,350 additions and 1,204 deletions.
33 changes: 21 additions & 12 deletions node/src/components/block_accumulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use prometheus::Registry;
use tracing::{debug, error, info, warn};

use casper_types::{
ActivationPoint, Block, BlockHash, BlockSignatures, EraId, FinalitySignature, TimeDiff,
ActivationPoint, Block, BlockHash, BlockSignaturesV2, EraId, FinalitySignatureV2, TimeDiff,
Timestamp,
};

Expand Down Expand Up @@ -294,7 +294,8 @@ impl BlockAccumulator {
match acceptor.register_block(meta_block, sender) {
Ok(_) => match self.validator_matrix.validator_weights(era_id) {
Some(evw) => {
let (should_store, faulty_senders) = acceptor.should_store_block(&evw);
let (should_store, faulty_senders) =
acceptor.should_store_block(&evw, self.validator_matrix.chain_name_hash());
self.store_block_and_finality_signatures(
effect_builder,
should_store,
Expand Down Expand Up @@ -366,7 +367,7 @@ impl BlockAccumulator {
fn register_finality_signature<REv>(
&mut self,
effect_builder: EffectBuilder<REv>,
finality_signature: FinalitySignature,
finality_signature: FinalitySignatureV2,
sender: Option<NodeId>,
) -> Effects<Event>
where
Expand Down Expand Up @@ -404,7 +405,8 @@ impl BlockAccumulator {
),
Ok(None) => match self.validator_matrix.validator_weights(era_id) {
Some(evw) => {
let (should_store, faulty_senders) = acceptor.should_store_block(&evw);
let (should_store, faulty_senders) =
acceptor.should_store_block(&evw, self.validator_matrix.chain_name_hash());
self.store_block_and_finality_signatures(
effect_builder,
should_store,
Expand Down Expand Up @@ -480,7 +482,7 @@ impl BlockAccumulator {
&self,
effect_builder: EffectBuilder<REv>,
maybe_meta_block: Option<ForwardMetaBlock>,
maybe_block_signatures: Option<BlockSignatures>,
maybe_block_signatures: Option<BlockSignaturesV2>,
) -> Effects<Event>
where
REv: From<BlockAccumulatorAnnouncement>
Expand Down Expand Up @@ -698,7 +700,9 @@ impl BlockAccumulator {
let block: Block = (*meta_block.block).clone().into();
effect_builder
.put_block_to_storage(Arc::new(block))
.then(move |_| effect_builder.put_signatures_to_storage(cloned_signatures))
.then(move |_| {
effect_builder.put_signatures_to_storage(cloned_signatures.into())
})
.event(move |_| Event::Stored {
maybe_meta_block: Some(meta_block),
maybe_block_signatures: Some(block_signatures),
Expand All @@ -716,7 +720,7 @@ impl BlockAccumulator {
// we mark it as complete.
let block_height = meta_block.block.height();
effect_builder
.put_signatures_to_storage(block_signatures.clone())
.put_signatures_to_storage(block_signatures.clone().into())
.then(move |_| effect_builder.mark_block_completed(block_height))
.event(move |_| Event::Stored {
maybe_meta_block: Some(meta_block),
Expand All @@ -736,11 +740,16 @@ impl BlockAccumulator {
}
ShouldStore::SingleSignature(signature) => {
debug!(%signature, "storing finality signature");
let mut block_signatures =
BlockSignatures::new(*signature.block_hash(), signature.era_id());
block_signatures.insert_signature(signature.clone());
let mut block_signatures = BlockSignaturesV2::new(
*signature.block_hash(),
signature.block_height(),
signature.era_id(),
signature.chain_name_hash(),
);
block_signatures
.insert_signature(signature.public_key().clone(), *signature.signature());
effect_builder
.put_finality_signature_to_storage(signature)
.put_finality_signature_to_storage(signature.into())
.event(move |_| Event::Stored {
maybe_meta_block: None,
maybe_block_signatures: Some(block_signatures),
Expand Down Expand Up @@ -860,7 +869,7 @@ impl<REv: ReactorEvent> ValidatorBoundComponent<REv> for BlockAccumulator {
.filter_map(|acceptor| {
let era_id = acceptor.era_id()?;
let evw = validator_matrix.validator_weights(era_id)?;
Some(acceptor.should_store_block(&evw))
Some(acceptor.should_store_block(&evw, validator_matrix.chain_name_hash()))
})
.collect_vec();
should_stores
Expand Down
49 changes: 29 additions & 20 deletions node/src/components/block_accumulator/block_acceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use itertools::Itertools;
use tracing::{debug, error, warn};

use casper_types::{
ActivationPoint, BlockHash, BlockSignatures, EraId, FinalitySignature, PublicKey, Timestamp,
ActivationPoint, BlockHash, BlockSignaturesV2, ChainNameDigest, EraId, FinalitySignatureV2,
PublicKey, Timestamp,
};

use crate::{
Expand All @@ -17,25 +18,25 @@ use crate::{
pub(super) struct BlockAcceptor {
block_hash: BlockHash,
meta_block: Option<ForwardMetaBlock>,
signatures: BTreeMap<PublicKey, (FinalitySignature, BTreeSet<NodeId>)>,
signatures: BTreeMap<PublicKey, (FinalitySignatureV2, BTreeSet<NodeId>)>,
peers: BTreeSet<NodeId>,
last_progress: Timestamp,
our_signature: Option<FinalitySignature>,
our_signature: Option<FinalitySignatureV2>,
}

#[derive(Debug, PartialEq)]
#[allow(clippy::large_enum_variant)]
pub(super) enum ShouldStore {
SufficientlySignedBlock {
meta_block: ForwardMetaBlock,
block_signatures: BlockSignatures,
block_signatures: BlockSignaturesV2,
},
CompletedBlock {
meta_block: ForwardMetaBlock,
block_signatures: BlockSignatures,
block_signatures: BlockSignaturesV2,
},
MarkComplete(ForwardMetaBlock),
SingleSignature(FinalitySignature),
SingleSignature(FinalitySignatureV2),
Nothing,
}

Expand Down Expand Up @@ -110,10 +111,10 @@ impl BlockAcceptor {

pub(super) fn register_finality_signature(
&mut self,
finality_signature: FinalitySignature,
finality_signature: FinalitySignatureV2,
peer: Option<NodeId>,
validator_slots: u32,
) -> Result<Option<FinalitySignature>, AcceptorError> {
) -> Result<Option<FinalitySignatureV2>, AcceptorError> {
if self.block_hash != *finality_signature.block_hash() {
return Err(AcceptorError::BlockHashMismatch {
expected: self.block_hash,
Expand Down Expand Up @@ -206,6 +207,7 @@ impl BlockAcceptor {
pub(super) fn should_store_block(
&mut self,
era_validator_weights: &EraValidatorWeights,
chain_name_hash: ChainNameDigest,
) -> (ShouldStore, Vec<(NodeId, AcceptorError)>) {
let block_hash = self.block_hash;
let no_block = self.meta_block.is_none();
Expand Down Expand Up @@ -240,10 +242,15 @@ impl BlockAcceptor {
if SignatureWeight::Strict == signature_weight {
self.touch();
if let Some(meta_block) = self.meta_block.as_mut() {
let mut block_signatures =
BlockSignatures::new(*meta_block.block.hash(), meta_block.block.era_id());
let mut block_signatures = BlockSignaturesV2::new(
*meta_block.block.hash(),
meta_block.block.height(),
meta_block.block.era_id(),
chain_name_hash,
);
self.signatures.values().for_each(|(signature, _)| {
block_signatures.insert_signature(signature.clone());
block_signatures
.insert_signature(signature.public_key().clone(), *signature.signature());
});
if meta_block
.state
Expand Down Expand Up @@ -361,11 +368,11 @@ impl BlockAcceptor {
self.last_progress
}

pub(super) fn our_signature(&self) -> Option<&FinalitySignature> {
pub(super) fn our_signature(&self) -> Option<&FinalitySignatureV2> {
self.our_signature.as_ref()
}

pub(super) fn set_our_signature(&mut self, signature: FinalitySignature) {
pub(super) fn set_our_signature(&mut self, signature: FinalitySignatureV2) {
self.our_signature = Some(signature);
}

Expand Down Expand Up @@ -427,7 +434,7 @@ impl BlockAcceptor {
fn check_signatures_from_peer_bound(
limit: u32,
peer: NodeId,
signatures: &BTreeMap<PublicKey, (FinalitySignature, BTreeSet<NodeId>)>,
signatures: &BTreeMap<PublicKey, (FinalitySignatureV2, BTreeSet<NodeId>)>,
) -> Result<(), AcceptorError> {
let signatures_for_peer = signatures
.values()
Expand Down Expand Up @@ -469,13 +476,15 @@ impl BlockAcceptor {
}
}

pub(super) fn signatures(&self) -> &BTreeMap<PublicKey, (FinalitySignature, BTreeSet<NodeId>)> {
pub(super) fn signatures(
&self,
) -> &BTreeMap<PublicKey, (FinalitySignatureV2, BTreeSet<NodeId>)> {
&self.signatures
}

pub(super) fn signatures_mut(
&mut self,
) -> &mut BTreeMap<PublicKey, (FinalitySignature, BTreeSet<NodeId>)> {
) -> &mut BTreeMap<PublicKey, (FinalitySignatureV2, BTreeSet<NodeId>)> {
&mut self.signatures
}
}
Expand All @@ -497,7 +506,7 @@ mod tests {
// Insert only the peer to check:
signatures.insert(
PublicKey::random(rng),
(FinalitySignature::random(rng), {
(FinalitySignatureV2::random(rng), {
let mut nodes = BTreeSet::new();
nodes.insert(peer_to_check);
nodes
Expand All @@ -506,7 +515,7 @@ mod tests {
// Insert an unrelated peer:
signatures.insert(
PublicKey::random(rng),
(FinalitySignature::random(rng), {
(FinalitySignatureV2::random(rng), {
let mut nodes = BTreeSet::new();
nodes.insert(NodeId::random(rng));
nodes
Expand All @@ -515,7 +524,7 @@ mod tests {
// Insert both the peer to check and an unrelated one:
signatures.insert(
PublicKey::random(rng),
(FinalitySignature::random(rng), {
(FinalitySignatureV2::random(rng), {
let mut nodes = BTreeSet::new();
nodes.insert(NodeId::random(rng));
nodes.insert(peer_to_check);
Expand All @@ -532,7 +541,7 @@ mod tests {
// Let's insert once again both the peer to check and an unrelated one:
signatures.insert(
PublicKey::random(rng),
(FinalitySignature::random(rng), {
(FinalitySignatureV2::random(rng), {
let mut nodes = BTreeSet::new();
nodes.insert(NodeId::random(rng));
nodes.insert(peer_to_check);
Expand Down
8 changes: 4 additions & 4 deletions node/src/components/block_accumulator/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{

use derive_more::From;

use casper_types::{BlockHash, BlockSignatures, BlockV2, EraId, FinalitySignature};
use casper_types::{BlockHash, BlockSignaturesV2, BlockV2, EraId, FinalitySignatureV2};

use crate::{
effect::requests::BlockAccumulatorRequest,
Expand All @@ -26,18 +26,18 @@ pub(crate) enum Event {
sender: NodeId,
},
CreatedFinalitySignature {
finality_signature: Box<FinalitySignature>,
finality_signature: Box<FinalitySignatureV2>,
},
ReceivedFinalitySignature {
finality_signature: Box<FinalitySignature>,
finality_signature: Box<FinalitySignatureV2>,
sender: NodeId,
},
ExecutedBlock {
meta_block: ForwardMetaBlock,
},
Stored {
maybe_meta_block: Option<ForwardMetaBlock>,
maybe_block_signatures: Option<BlockSignatures>,
maybe_block_signatures: Option<BlockSignaturesV2>,
},
}

Expand Down
Loading

0 comments on commit f5e1836

Please sign in to comment.