Skip to content

Commit

Permalink
removed multifork history
Browse files Browse the repository at this point in the history
  • Loading branch information
pompon0 committed Feb 21, 2024
1 parent 0d10e99 commit 11de1a4
Show file tree
Hide file tree
Showing 23 changed files with 51 additions and 110 deletions.
2 changes: 1 addition & 1 deletion node/actors/bft/src/leader/replica_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl StateMachine {
signed_message.verify().map_err(Error::InvalidSignature)?;

message
.verify(self.config.genesis(), /*allow_past_forks=*/ false)
.verify(self.config.genesis())
.map_err(Error::InvalidMessage)?;

// ----------- All checks finished. Now we process the message. --------------
Expand Down
2 changes: 1 addition & 1 deletion node/actors/bft/src/leader/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl StateMachine {
}
// The previous block was finalized, so we can propose a new block.
_ => {
let fork = cfg.genesis().forks.current();
let fork = &cfg.genesis().fork;
let (parent, number) = match high_qc {
Some(qc) => (Some(qc.header().hash()), qc.header().number.next()),
None => (fork.first_parent, fork.first_block),
Expand Down
2 changes: 1 addition & 1 deletion node/actors/bft/src/replica/new_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl StateMachine {
validator::ReplicaPrepare {
view: validator::View {
protocol_version: crate::PROTOCOL_VERSION,
fork: self.config.genesis().forks.current().number,
fork: self.config.genesis().fork.number,
number: self.view,
},
high_vote: self.high_vote.clone(),
Expand Down
2 changes: 1 addition & 1 deletion node/actors/bft/src/testonly/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Test {
s.spawn_bg(run_nodes(ctx, self.network, &nodes));

// Run the nodes until all honest nodes store enough finalized blocks.
let first = setup.genesis.forks.current().first_block;
let first = setup.genesis.fork.first_block;
let want_next = validator::BlockNumber(first.0 + self.blocks_to_finalize as u64);
for store in &honest {
sync::wait_for(ctx, &mut store.subscribe(), |state| {
Expand Down
4 changes: 2 additions & 2 deletions node/actors/bft/src/testonly/ut_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl UTHarness {
let want = ReplicaPrepare {
view: validator::View {
protocol_version: self.protocol_version(),
fork: self.genesis().forks.current().number,
fork: self.genesis().fork.number,
number: self.replica.view.next(),
},
high_qc: self.replica.high_qc.clone(),
Expand Down Expand Up @@ -153,7 +153,7 @@ impl UTHarness {
pub(crate) fn replica_view(&self) -> validator::View {
validator::View {
protocol_version: self.protocol_version(),
fork: self.genesis().forks.current().number,
fork: self.genesis().fork.number,
number: self.replica.view,
}
}
Expand Down
6 changes: 4 additions & 2 deletions node/actors/executor/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ async fn executing_validator_and_full_node() {
.unwrap();
}

/*
/// * finalize some blocks
/// * revert bunch of blocks
/// * restart validators and make sure that new blocks get produced
Expand Down Expand Up @@ -125,7 +126,7 @@ async fn test_block_revert() {
tracing::info!("Revert blocks");
let first = BlockNumber(first.0 + 3);
let fork = validator::Fork {
number: setup.genesis.forks.current().number.next(),
number: setup.genesis.fork.number.next(),
first_block: first,
first_parent: persistent_stores[0]
.block(ctx, first)
Expand All @@ -135,7 +136,7 @@ async fn test_block_revert() {
.parent,
};
let mut genesis = setup.genesis.clone();
genesis.forks.push(fork.clone()).unwrap();
genesis.fork = fork.clone();
// Update configs and persistent storage.
for store in &mut persistent_stores {
*store = store.fork(fork.clone()).unwrap();
Expand Down Expand Up @@ -175,3 +176,4 @@ async fn test_block_revert() {
.await
.unwrap();
}
*/
2 changes: 1 addition & 1 deletion node/actors/sync_blocks/src/peers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl PeerStates {
let Some(last) = &state.last else {
return Ok(());
};
last.verify(self.genesis(), /*allow_past_forks=*/ true)
last.verify(self.genesis())
.context("state.last.verify()")?;
let mut peers = self.peers.lock().unwrap();
match peers.entry(peer.clone()) {
Expand Down
7 changes: 1 addition & 6 deletions node/actors/sync_blocks/src/peers/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,7 @@ async fn test_peer_states<T: Test>(test: T) {
let ctx = &ctx::test_root(&clock);
let rng = &mut ctx.rng();
let mut setup = validator::testonly::Setup::new(rng, 4);
for _ in 0..T::BLOCK_COUNT {
if rng.gen_range(0..3) == 0 {
setup.fork();
}
setup.push_block(rng.gen());
}
setup.push_blocks(rng,T::BLOCK_COUNT);
let (store, store_run) = new_store(ctx, &setup.genesis).await;
test.initialize_storage(ctx, store.as_ref(), &setup).await;

Expand Down
7 changes: 1 addition & 6 deletions node/actors/sync_blocks/src/tests/end_to_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,7 @@ async fn test_sync_blocks<T: GossipNetworkTest>(test: T) {
let (node_count, gossip_peers) = test.network_params();

let mut setup = validator::testonly::Setup::new(rng, node_count);
setup.push_blocks(rng, 1);
setup.fork();
setup.push_blocks(rng, 9);
setup.fork();
setup.push_blocks(rng, 10);

scope::run!(ctx, |ctx, s| async {
let mut nodes = vec![];
for (i, net) in network::testonly::new_configs(rng, &setup, gossip_peers)
Expand Down Expand Up @@ -189,7 +184,7 @@ impl GossipNetworkTest for BasicSynchronization {
for node in &nodes {
node.start();
let state = node.store.subscribe().borrow().clone();
assert_eq!(state.first, setup.genesis.forks.root().first_block);
assert_eq!(state.first, setup.genesis.fork.first_block);
assert_eq!(state.last, None);
}

Expand Down
2 changes: 1 addition & 1 deletion node/actors/sync_blocks/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const TEST_TIMEOUT: time::Duration = time::Duration::seconds(20);

pub(crate) fn sync_state(setup: &Setup, last: Option<&validator::FinalBlock>) -> BlockStoreState {
BlockStoreState {
first: setup.genesis.forks.root().first_block,
first: setup.genesis.fork.first_block,
last: last.map(|b| b.justification.clone()),
}
}
Expand Down
6 changes: 1 addition & 5 deletions node/libs/roles/src/proto/validator.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ message Fork {
optional BlockHeaderHash first_parent = 3; // optional
}

message ForkSet {
repeated Fork forks = 1;
}

message Genesis {
optional ForkSet forks = 1; // required
optional Fork fork = 1; // required
repeated PublicKey validators = 2;
}

Expand Down
2 changes: 1 addition & 1 deletion node/libs/roles/src/validator/messages/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl FinalBlock {
});
}
self.justification
.verify(genesis, /*allow_past_forks=*/ true)
.verify(genesis)
.map_err(BlockValidationError::Justification)
}
}
Expand Down
1 change: 0 additions & 1 deletion node/libs/roles/src/validator/messages/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use super::{
BlockHeaderHash, BlockNumber, LeaderCommit, LeaderPrepare, Msg, ReplicaCommit, ReplicaPrepare,
};
use crate::validator;
use anyhow::Context as _;
use bit_vec::BitVec;
use std::{
collections::{BTreeMap, BTreeSet},
Expand Down
5 changes: 2 additions & 3 deletions node/libs/roles/src/validator/messages/leader_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl LeaderCommit {
/// Verifies LeaderCommit.
pub fn verify(&self, genesis: &Genesis) -> Result<(), CommitQCVerifyError> {
self.justification
.verify(genesis, /*allow_past_forks=*/ false)
.verify(genesis)
}

/// View of this message.
Expand Down Expand Up @@ -94,11 +94,10 @@ impl CommitQC {
pub fn verify(
&self,
genesis: &Genesis,
allow_past_forks: bool,
) -> Result<(), CommitQCVerifyError> {
use CommitQCVerifyError as Error;
self.message
.verify(genesis, allow_past_forks)
.verify(genesis)
.map_err(Error::InvalidMessage)?;
if self.signers.len() != genesis.validators.len() {
return Err(Error::BadSignersSet);
Expand Down
4 changes: 2 additions & 2 deletions node/libs/roles/src/validator/messages/leader_prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ impl LeaderPrepare {
let (want_parent, want_number) = match high_qc {
Some(qc) => (Some(qc.header().hash()), qc.header().number.next()),
None => (
genesis.forks.current().first_parent,
genesis.forks.current().first_block,
genesis.fork.first_parent,
genesis.fork.first_block,
),
};
if self.proposal.parent != want_parent {
Expand Down
27 changes: 5 additions & 22 deletions node/libs/roles/src/validator/messages/replica_commit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::{BlockHeader, Genesis, View};
use anyhow::Context as _;

/// A Commit message from a replica.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand All @@ -12,28 +11,12 @@ pub struct ReplicaCommit {

impl ReplicaCommit {
/// Verifies the message.
pub fn verify(&self, genesis: &Genesis, allow_past_forks: bool) -> anyhow::Result<()> {
if !allow_past_forks {
anyhow::ensure!(self.view.fork == genesis.forks.current().number);
}
// Currently we process CommitQCs from past forks when verifying FinalBlocks
// during synchronization. Eventually we will switch to synchronization without
// CommitQCs for blocks from the past forks, and then we can always enforce
// `genesis.forks.current()` instead.
let fork = genesis
.forks
.find(self.proposal.number)
.context("doesn't belong to any fork")?;
anyhow::ensure!(
fork.number == self.view.fork,
"bad fork: got {:?} want {:?} for block {}",
self.view.fork,
fork.number,
self.proposal.number
);
if self.proposal.number == fork.first_block {
pub fn verify(&self, genesis: &Genesis) -> anyhow::Result<()> {
anyhow::ensure!(self.view.fork == genesis.fork.number);
anyhow::ensure!(self.proposal.number >= genesis.fork.first_block);
if self.proposal.number == genesis.fork.first_block {
anyhow::ensure!(
self.proposal.parent == fork.first_parent,
self.proposal.parent == genesis.fork.first_parent,
"bad parent of the first block of the fork"
);
}
Expand Down
9 changes: 4 additions & 5 deletions node/libs/roles/src/validator/messages/replica_prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,24 @@ impl ReplicaPrepare {
/// Verifies the message.
pub fn verify(&self, genesis: &Genesis) -> Result<(), ReplicaPrepareVerifyError> {
use ReplicaPrepareVerifyError as Error;
let fork = genesis.forks.current();
if self.view.fork != fork.number {
if self.view.fork != genesis.fork.number {
return Err(Error::BadFork {
got: self.view.fork,
want: fork.number,
want: genesis.fork.number,
});
}
if let Some(v) = &self.high_vote {
if self.view.number <= v.view.number {
return Err(Error::HighVoteFutureView);
}
v.verify(genesis, /*allow_past_forks=*/ false)
v.verify(genesis)
.map_err(Error::HighVote)?;
}
if let Some(qc) = &self.high_qc {
if self.view.number <= qc.view().number {
return Err(Error::HighQCFutureView);
}
qc.verify(genesis, /*allow_past_forks=*/ false)
qc.verify(genesis)
.map_err(Error::HighQC)?;
}
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions node/libs/roles/src/validator/testonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use zksync_consensus_utils::enum_util::Variant;
pub struct Setup(SetupInner);

impl Setup {
/// New `Setup` with a given `fork`.
pub fn new_with_fork(rng: &mut impl Rng, validators: usize, fork: Fork) -> Self {
let keys: Vec<SecretKey> = (0..validators).map(|_| rng.gen()).collect();
let genesis = Genesis {
Expand All @@ -33,12 +34,12 @@ impl Setup {
})
}

/// New Setup builder.
/// New `Setup`.
pub fn new(rng: &mut impl Rng, validators: usize) -> Self {
let fork = Fork {
number: ForkNumber(rng.gen_range(0..100)),
first_block: BlockNumber(rng.gen_range(0..100)),
first_parent: None,
first_parent: Some(rng.gen()),
};
Self::new_with_fork(rng,validators,fork)
}
Expand All @@ -53,7 +54,6 @@ impl Setup {

/// Pushes the next block with the given payload.
pub fn push_block(&mut self, payload: Payload) {
let number = self.next();
let view = View {
protocol_version: ProtocolVersion::EARLIEST,
fork: self.genesis.fork.number,
Expand Down
16 changes: 7 additions & 9 deletions node/libs/roles/src/validator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ fn test_schema_encoding() {
test_encode_random::<Signature>(rng);
test_encode_random::<AggregateSignature>(rng);
test_encode_random::<Fork>(rng);
test_encode_random::<ForkSet>(rng);
test_encode_random::<Genesis>(rng);
test_encode_random::<GenesisHash>(rng);
}
Expand Down Expand Up @@ -173,7 +172,7 @@ fn test_agg_signature_verify() {
fn make_view(number: ViewNumber, setup: &Setup) -> View {
View {
protocol_version: ProtocolVersion::EARLIEST,
fork: setup.genesis.forks.current().number,
fork: setup.genesis.fork.number,
number,
}
}
Expand Down Expand Up @@ -217,28 +216,27 @@ fn test_commit_qc() {
let setup2 = Setup::new(rng, 6);
let genesis3 = Genesis {
validators: ValidatorSet::new(setup1.genesis.validators.iter().take(3).cloned()).unwrap(),
forks: setup1.genesis.forks.clone(),
fork: setup1.genesis.fork.clone(),
};

let allow_past_forks = false;
for i in 0..setup1.keys.len() + 1 {
let view = rng.gen();
let mut qc = CommitQC::new(make_replica_commit(rng, view, &setup1), &setup1.genesis);
for key in &setup1.keys[0..i] {
qc.add(&key.sign_msg(qc.message.clone()), &setup1.genesis);
}
if i >= setup1.genesis.validators.threshold() {
qc.verify(&setup1.genesis, allow_past_forks).unwrap();
qc.verify(&setup1.genesis).unwrap();
} else {
assert_matches!(
qc.verify(&setup1.genesis, allow_past_forks),
qc.verify(&setup1.genesis),
Err(Error::NotEnoughSigners { .. })
);
}

// Mismatching validator sets.
assert!(qc.verify(&setup2.genesis, allow_past_forks).is_err());
assert!(qc.verify(&genesis3, allow_past_forks).is_err());
assert!(qc.verify(&setup2.genesis).is_err());
assert!(qc.verify(&genesis3).is_err());
}
}

Expand All @@ -252,7 +250,7 @@ fn test_prepare_qc() {
let setup2 = Setup::new(rng, 6);
let genesis3 = Genesis {
validators: ValidatorSet::new(setup1.genesis.validators.iter().take(3).cloned()).unwrap(),
forks: setup1.genesis.forks.clone(),
fork: setup1.genesis.fork.clone(),
};

let view: ViewNumber = rng.gen();
Expand Down
Loading

0 comments on commit 11de1a4

Please sign in to comment.