Skip to content

Commit

Permalink
Removed attester set from genesis.
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoffranca committed Dec 11, 2024
1 parent 9fd93df commit fb655ff
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 217 deletions.
27 changes: 9 additions & 18 deletions node/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 0 additions & 86 deletions node/libs/roles/src/attester/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::*;
use crate::validator::testonly::Setup;
use assert_matches::assert_matches;
use rand::Rng;
use zksync_concurrency::ctx;
Expand Down Expand Up @@ -134,91 +133,6 @@ fn test_agg_signature_verify() {
.is_err());
}

#[test]
fn test_batch_qc() {
use BatchQCVerifyError as Error;
let ctx = ctx::test_root(&ctx::RealClock);
let rng = &mut ctx.rng();

let setup1 = Setup::new(rng, 6);
// Completely different genesis.
let setup2 = Setup::new(rng, 6);
// Genesis with only a subset of the attesters.
let genesis3 = {
let mut genesis3 = (*setup1.genesis).clone();
genesis3.attesters = Committee::new(
setup1
.genesis
.attesters
.as_ref()
.unwrap()
.iter()
.take(3)
.cloned(),
)
.unwrap()
.into();
genesis3.with_hash()
};

let attesters = setup1.genesis.attesters.as_ref().unwrap();

// Create QCs with increasing number of attesters.
for i in 0..setup1.attester_keys.len() + 1 {
let mut qc = BatchQC::new(Batch {
genesis: setup1.genesis.hash(),
number: rng.gen(),
hash: rng.gen(),
});
for key in &setup1.attester_keys[0..i] {
qc.add(&key.sign_msg(qc.message.clone()), attesters)
.unwrap();
}

let expected_weight: u64 = attesters.iter().take(i).map(|w| w.weight).sum();
if expected_weight >= attesters.threshold() {
qc.verify(setup1.genesis.hash(), attesters)
.expect("failed to verify QC");
} else {
assert_matches!(
qc.verify(setup1.genesis.hash(), attesters),
Err(Error::NotEnoughSigners { .. })
);
}

// Mismatching attesters sets.
assert!(qc
.verify(
setup1.genesis.hash(),
setup2.genesis.attesters.as_ref().unwrap()
)
.is_err());
assert!(qc
.verify(setup1.genesis.hash(), genesis3.attesters.as_ref().unwrap())
.is_err());
}
}

#[test]
fn test_attester_committee_weights() {
let ctx = ctx::test_root(&ctx::RealClock);
let rng = &mut ctx.rng();

// Attesters with non-uniform weights
let setup = Setup::new_with_weights(rng, vec![1000, 600, 800, 6000, 900, 700]);
// Expected sum of the attesters weights
let sums = [1000, 1600, 2400, 8400, 9300, 10000];
let attesters = setup.genesis.attesters.as_ref().unwrap();

let msg: Batch = rng.gen();
let mut qc = BatchQC::new(msg.clone());
for (i, weight) in sums.iter().enumerate() {
let key = &setup.attester_keys[i];
qc.add(&key.sign_msg(msg.clone()), attesters).unwrap();
assert_eq!(attesters.weight_of_keys(qc.signatures.keys()), *weight);
}
}

#[test]
fn test_committee_weights_overflow_check() {
let ctx = ctx::test_root(&ctx::RealClock);
Expand Down
5 changes: 2 additions & 3 deletions node/libs/roles/src/proto/validator/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ message LeaderSelectionMode {
}

message Genesis {
reserved 1,2;
reserved "fork","validators";
reserved 1,2,9;
reserved "fork","validators", "attesters";
optional uint64 chain_id = 5; // required
optional uint64 fork_number = 6; // required; ForkNumber
optional uint64 first_block = 7; // required; BlockNumber
Expand All @@ -33,7 +33,6 @@ message Genesis {
// We will either remove them entirely, or keep them for the initial epoch.
optional uint32 protocol_version = 8; // required; ProtocolVersion
repeated WeightedValidator validators_v1 = 3;
repeated attester.WeightedAttester attesters = 9; // optional
optional LeaderSelectionMode leader_selection = 4; // required
}

Expand Down
24 changes: 1 addition & 23 deletions node/libs/roles/src/validator/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ use super::{
ProposalJustification, ProtocolVersion, PublicKey, ReplicaCommit, ReplicaNewView,
ReplicaTimeout, Signature, Signed, Signers, TimeoutQC, View, ViewNumber, WeightedValidator,
};
use crate::{
attester::{self, WeightedAttester},
node::SessionId,
proto::validator as proto,
validator::LeaderSelectionMode,
};
use crate::{node::SessionId, proto::validator as proto, validator::LeaderSelectionMode};
use anyhow::Context as _;
use std::collections::BTreeMap;
use zksync_consensus_crypto::ByteFmt;
Expand All @@ -27,25 +22,13 @@ impl ProtoFmt for GenesisRaw {
.map(|(i, v)| WeightedValidator::read(v).context(i))
.collect::<Result<_, _>>()
.context("validators_v1")?;
let attesters: Vec<_> = r
.attesters
.iter()
.enumerate()
.map(|(i, v)| WeightedAttester::read(v).context(i))
.collect::<Result<_, _>>()
.context("attesters")?;
Ok(GenesisRaw {
chain_id: ChainId(*required(&r.chain_id).context("chain_id")?),
fork_number: ForkNumber(*required(&r.fork_number).context("fork_number")?),
first_block: BlockNumber(*required(&r.first_block).context("first_block")?),

protocol_version: ProtocolVersion(r.protocol_version.context("protocol_version")?),
validators: Committee::new(validators.into_iter()).context("validators_v1")?,
attesters: if attesters.is_empty() {
None
} else {
Some(attester::Committee::new(attesters.into_iter()).context("attesters")?)
},
leader_selection: read_required(&r.leader_selection).context("leader_selection")?,
})
}
Expand All @@ -57,11 +40,6 @@ impl ProtoFmt for GenesisRaw {

protocol_version: Some(self.protocol_version.0),
validators_v1: self.validators.iter().map(|v| v.build()).collect(),
attesters: self
.attesters
.as_ref()
.map(|c| c.iter().map(|v| v.build()).collect())
.unwrap_or_default(),
leader_selection: Some(self.leader_selection.build()),
}
}
Expand Down
4 changes: 1 addition & 3 deletions node/libs/roles/src/validator/messages/genesis.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Messages related to the consensus protocol.
use super::{BlockNumber, LeaderSelectionMode, ViewNumber};
use crate::{attester, validator};
use crate::validator;
use std::{fmt, hash::Hash};
use zksync_consensus_crypto::{keccak256::Keccak256, ByteFmt, Text, TextFmt};

Expand All @@ -18,8 +18,6 @@ pub struct GenesisRaw {
pub first_block: BlockNumber,
/// Set of validators of the chain.
pub validators: validator::Committee,
/// Set of attesters of the chain.
pub attesters: Option<attester::Committee>,
/// The mode used for selecting leader for a given view.
pub leader_selection: LeaderSelectionMode,
}
Expand Down
3 changes: 1 addition & 2 deletions node/libs/roles/src/validator/messages/tests/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@ fn test_view_prev() {

#[test]
fn test_view_verify() {
let genesis = genesis_with_attesters();
let genesis = genesis();
let view = View {
genesis: genesis.hash(),
number: ViewNumber(1),
};
assert!(view.verify(&genesis).is_ok());
assert!(view.verify(&genesis_empty_attesters()).is_err());
let view = View {
genesis: GenesisHash::default(),
number: ViewNumber(1),
Expand Down
Loading

0 comments on commit fb655ff

Please sign in to comment.