Skip to content

Commit

Permalink
Merge branch 'main' into gprusak-or-canceled
Browse files Browse the repository at this point in the history
  • Loading branch information
pompon0 authored Nov 30, 2023
2 parents 1ff01e2 + da015d4 commit 13c2e8e
Show file tree
Hide file tree
Showing 23 changed files with 156 additions and 65 deletions.
2 changes: 2 additions & 0 deletions node/actors/bft/src/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub(crate) struct ConsensusInner {
pub(crate) secret_key: validator::SecretKey,
/// A vector of public keys for all the validators in the network.
pub(crate) validator_set: validator::ValidatorSet,
/// Current protocol version for the consensus messages.
pub(crate) protocol_version: validator::ProtocolVersion,
}

impl ConsensusInner {
Expand Down
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 @@ -148,7 +148,7 @@ impl StateMachine {
.secret_key
.sign_msg(validator::ConsensusMsg::LeaderCommit(
validator::LeaderCommit {
protocol_version: validator::CURRENT_VERSION,
protocol_version: consensus.protocol_version,
justification,
},
)),
Expand Down
2 changes: 1 addition & 1 deletion node/actors/bft/src/leader/replica_prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ impl StateMachine {
.secret_key
.sign_msg(validator::ConsensusMsg::LeaderPrepare(
validator::LeaderPrepare {
protocol_version: validator::CURRENT_VERSION,
protocol_version: consensus.protocol_version,
view: self.view,
proposal,
proposal_payload: payload,
Expand Down
8 changes: 6 additions & 2 deletions node/actors/bft/src/leader/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ async fn replica_commit() {
let rng = &mut StdRng::seed_from_u64(6516565651);

let keys: Vec<_> = (0..1).map(|_| rng.gen()).collect();
let (genesis, val_set) = testonly::make_genesis(&keys, validator::Payload(vec![]));
let (genesis, val_set) = testonly::make_genesis(
&keys,
validator::ProtocolVersion::EARLIEST,
validator::Payload(vec![]),
);
let (mut consensus, _) = testonly::make_consensus(ctx, &keys[0], &val_set, &genesis).await;

consensus.leader.view = validator::ViewNumber(3);
Expand All @@ -22,7 +26,7 @@ async fn replica_commit() {
.secret_key
.sign_msg(validator::ConsensusMsg::ReplicaCommit(
validator::ReplicaCommit {
protocol_version: validator::CURRENT_VERSION,
protocol_version: validator::ProtocolVersion::EARLIEST,
view: consensus.leader.view,
proposal: rng.gen(),
},
Expand Down
6 changes: 4 additions & 2 deletions node/actors/bft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl Consensus {
pub async fn new(
ctx: &ctx::Ctx,
pipe: ActorPipe<InputMessage, OutputMessage>,
protocol_version: validator::ProtocolVersion,
secret_key: validator::SecretKey,
validator_set: validator::ValidatorSet,
storage: ReplicaStore,
Expand All @@ -60,6 +61,7 @@ impl Consensus {
pipe,
secret_key,
validator_set,
protocol_version,
},
replica: replica::StateMachine::new(ctx, storage).await?,
leader: leader::StateMachine::new(ctx),
Expand Down Expand Up @@ -99,10 +101,10 @@ impl Consensus {

match input {
Some(InputMessage::Network(req)) => {
if req.msg.msg.protocol_version() != validator::CURRENT_VERSION {
if req.msg.msg.protocol_version() != self.inner.protocol_version {
tracing::warn!(
"bad protocol version (expected: {:?}, received: {:?})",
validator::CURRENT_VERSION,
self.inner.protocol_version,
req.msg.msg.protocol_version()
);
continue;
Expand Down
2 changes: 1 addition & 1 deletion node/actors/bft/src/replica/leader_prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl StateMachine {

// Create our commit vote.
let commit_vote = validator::ReplicaCommit {
protocol_version: validator::CURRENT_VERSION,
protocol_version: consensus.protocol_version,
view,
proposal: message.proposal,
};
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 @@ -34,7 +34,7 @@ impl StateMachine {
.secret_key
.sign_msg(validator::ConsensusMsg::ReplicaPrepare(
validator::ReplicaPrepare {
protocol_version: validator::CURRENT_VERSION,
protocol_version: consensus.protocol_version,
view: next_view,
high_vote: self.high_vote,
high_qc: self.high_qc.clone(),
Expand Down
8 changes: 6 additions & 2 deletions node/actors/bft/src/replica/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ async fn start_new_view_not_leader() {
let rng = &mut ctx.rng();

let keys: Vec<_> = (0..4).map(|_| rng.gen()).collect();
let (genesis, val_set) = testonly::make_genesis(&keys, validator::Payload(vec![]));
let (genesis, val_set) = testonly::make_genesis(
&keys,
validator::ProtocolVersion::EARLIEST,
validator::Payload(vec![]),
);
let (mut consensus, mut pipe) =
testonly::make_consensus(ctx, &keys[0], &val_set, &genesis).await;
// TODO: this test assumes a specific implementation of the leader schedule.
Expand Down Expand Up @@ -40,7 +44,7 @@ async fn start_new_view_not_leader() {
.secret_key
.sign_msg(validator::ConsensusMsg::ReplicaPrepare(
validator::ReplicaPrepare {
protocol_version: validator::CURRENT_VERSION,
protocol_version: validator::ProtocolVersion::EARLIEST,
view: consensus.replica.view,
high_vote: consensus.replica.high_vote,
high_qc: consensus.replica.high_qc.clone(),
Expand Down
4 changes: 3 additions & 1 deletion node/actors/bft/src/testonly/make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub async fn make_consensus(
let consensus = Consensus::new(
ctx,
consensus_pipe,
genesis_block.justification.message.protocol_version,
key.clone(),
validator_set.clone(),
ReplicaStore::from_store(Arc::new(storage)),
Expand All @@ -39,6 +40,7 @@ pub async fn make_consensus(
/// and a validator set for the chain.
pub fn make_genesis(
keys: &[validator::SecretKey],
protocol_version: validator::ProtocolVersion,
payload: validator::Payload,
) -> (validator::FinalBlock, validator::ValidatorSet) {
let header = validator::BlockHeader::genesis(payload.hash());
Expand All @@ -47,7 +49,7 @@ pub fn make_genesis(
.iter()
.map(|sk| {
sk.sign_msg(validator::ReplicaCommit {
protocol_version: validator::CURRENT_VERSION,
protocol_version,
view: validator::ViewNumber(0),
proposal: header,
})
Expand Down
7 changes: 6 additions & 1 deletion node/actors/bft/src/testonly/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ impl Test {
.iter()
.map(|node| node.consensus_config().key.clone())
.collect();
let (genesis_block, _) = testonly::make_genesis(&keys, validator::Payload(vec![]));
let (genesis_block, _) = testonly::make_genesis(
&keys,
validator::ProtocolVersion::EARLIEST,
validator::Payload(vec![]),
);
let nodes: Vec<_> = nodes
.into_iter()
.enumerate()
Expand Down Expand Up @@ -102,6 +106,7 @@ async fn run_nodes(ctx: &ctx::Ctx, network: Network, nodes: &[Node]) -> anyhow::
let consensus = Consensus::new(
ctx,
consensus_actor_pipe,
validator::ProtocolVersion::EARLIEST,
node.net.consensus_config().key.clone(),
validator_set,
storage,
Expand Down
24 changes: 11 additions & 13 deletions node/actors/executor/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
};
use zksync_consensus_bft::misc::consensus_threshold;
use zksync_consensus_crypto::{read_required_text, Text, TextFmt};
use zksync_consensus_network::{consensus, gossip};
use zksync_consensus_network::gossip;
use zksync_consensus_roles::{node, validator};
use zksync_protobuf::{read_required, required, ProtoFmt};

Expand All @@ -23,31 +23,29 @@ pub struct ConsensusConfig {
/// Public TCP address that other validators are expected to connect to.
/// It is announced over gossip network.
pub public_addr: net::SocketAddr,
}

impl From<consensus::Config> for ConsensusConfig {
fn from(config: consensus::Config) -> Self {
Self {
key: config.key.public(),
public_addr: config.public_addr,
}
}
/// Currently used protocol version for consensus messages.
pub protocol_version: validator::ProtocolVersion,
}

impl ProtoFmt for ConsensusConfig {
type Proto = proto::ConsensusConfig;

fn read(r: &Self::Proto) -> anyhow::Result<Self> {
fn read(proto: &Self::Proto) -> anyhow::Result<Self> {
Ok(Self {
key: read_required_text(&r.key).context("key")?,
public_addr: read_required_text(&r.public_addr).context("public_addr")?,
key: read_required_text(&proto.key).context("key")?,
public_addr: read_required_text(&proto.public_addr).context("public_addr")?,
protocol_version: required(&proto.protocol_version)
.copied()
.and_then(validator::ProtocolVersion::try_from)
.context("protocol_version")?,
})
}

fn build(&self) -> Self::Proto {
Self::Proto {
key: Some(self.key.encode()),
public_addr: Some(self.public_addr.encode()),
protocol_version: Some(self.protocol_version.as_u32()),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions node/actors/executor/src/config/proto/mod.proto
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ message NodeAddr {
message ConsensusConfig {
optional string key = 1; // [required] ValidatorPublicKey
optional string public_addr = 2; // [required] IpAddr
// Currently used protocol version for consensus messages.
optional uint32 protocol_version = 3; // [required]
}

// Config of the gossip network.
Expand Down
1 change: 1 addition & 0 deletions node/actors/executor/src/config/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ impl Distribution<ConsensusConfig> for Standard {
ConsensusConfig {
key: rng.gen::<validator::SecretKey>().public(),
public_addr: make_addr(rng),
protocol_version: validator::ProtocolVersion::EARLIEST,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions node/actors/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ impl<S: WriteBlockStore + 'static> Executor<S> {
let consensus = Consensus::new(
ctx,
consensus_actor_pipe,
validator.config.protocol_version,
validator.key.clone(),
validator_set.clone(),
consensus_storage,
Expand Down
33 changes: 28 additions & 5 deletions node/actors/executor/src/testonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,25 @@ use rand::Rng;
use std::collections::HashMap;
use zksync_concurrency::net;
use zksync_consensus_bft::testonly::make_genesis;
use zksync_consensus_network::testonly::Instance;
use zksync_consensus_network::{consensus, testonly::Instance};
use zksync_consensus_roles::{
node,
validator::{self, Payload},
};

impl ConsensusConfig {
fn from_network_config(
src: consensus::Config,
protocol_version: validator::ProtocolVersion,
) -> Self {
Self {
key: src.key.public(),
public_addr: src.public_addr,
protocol_version,
}
}
}

/// Full validator configuration.
#[derive(Debug)]
#[non_exhaustive]
Expand All @@ -26,16 +39,26 @@ pub struct FullValidatorConfig {

impl FullValidatorConfig {
/// Generates a validator config for a network with a single validator.
pub fn for_single_validator(rng: &mut impl Rng, genesis_block_payload: Payload) -> Self {
///
/// `protocol_version` is used both for the genesis block and as the current protocol version.
pub fn for_single_validator(
rng: &mut impl Rng,
protocol_version: validator::ProtocolVersion,
genesis_block_payload: Payload,
) -> Self {
let mut net_configs = Instance::new_configs(rng, 1, 0);
assert_eq!(net_configs.len(), 1);
let net_config = net_configs.pop().unwrap();
let consensus_config = net_config.consensus.unwrap();
let validator_key = consensus_config.key.clone();
let consensus_config = ConsensusConfig::from(consensus_config);
let consensus_config =
ConsensusConfig::from_network_config(consensus_config, protocol_version);

let (genesis_block, validators) =
make_genesis(&[validator_key.clone()], genesis_block_payload);
let (genesis_block, validators) = make_genesis(
&[validator_key.clone()],
protocol_version,
genesis_block_payload,
);
let node_key = net_config.gossip.key.clone();
let node_config = ExecutorConfig {
server_addr: *net_config.server_addr,
Expand Down
32 changes: 26 additions & 6 deletions node/actors/executor/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl FullValidatorConfig {
payload: payload.hash(),
};
let commit = self.validator_key.sign_msg(validator::ReplicaCommit {
protocol_version: validator::CURRENT_VERSION,
protocol_version: validator::ProtocolVersion::EARLIEST,
view: validator::ViewNumber(header.number.0),
proposal: header,
});
Expand Down Expand Up @@ -67,7 +67,11 @@ async fn executor_misconfiguration(name: &str, mutation: fn(&mut FinalBlock)) {
let ctx = &ctx::root();
let rng = &mut ctx.rng();

let mut validator = FullValidatorConfig::for_single_validator(rng, Payload(vec![]));
let mut validator = FullValidatorConfig::for_single_validator(
rng,
validator::ProtocolVersion::EARLIEST,
Payload(vec![]),
);
let genesis_block = &mut validator.node_config.genesis_block;
mutation(genesis_block);
let storage = Arc::new(InMemoryStorage::new(genesis_block.clone()));
Expand All @@ -83,7 +87,11 @@ async fn genesis_block_mismatch() {
let ctx = &ctx::root();
let rng = &mut ctx.rng();

let validator = FullValidatorConfig::for_single_validator(rng, Payload(vec![]));
let validator = FullValidatorConfig::for_single_validator(
rng,
validator::ProtocolVersion::EARLIEST,
Payload(vec![]),
);
let mut genesis_block = validator.node_config.genesis_block.clone();
genesis_block.header.number = BlockNumber(1);
let storage = Arc::new(InMemoryStorage::new(genesis_block.clone()));
Expand All @@ -99,7 +107,11 @@ async fn executing_single_validator() {
let ctx = &ctx::root();
let rng = &mut ctx.rng();

let validator = FullValidatorConfig::for_single_validator(rng, Payload(vec![]));
let validator = FullValidatorConfig::for_single_validator(
rng,
validator::ProtocolVersion::EARLIEST,
Payload(vec![]),
);
let genesis_block = &validator.node_config.genesis_block;
let storage = InMemoryStorage::new(genesis_block.clone());
let storage = Arc::new(storage);
Expand All @@ -124,7 +136,11 @@ async fn executing_validator_and_full_node() {
let ctx = &ctx::test_root(&ctx::AffineClock::new(20.0));
let rng = &mut ctx.rng();

let mut validator = FullValidatorConfig::for_single_validator(rng, Payload(vec![]));
let mut validator = FullValidatorConfig::for_single_validator(
rng,
validator::ProtocolVersion::EARLIEST,
Payload(vec![]),
);
let full_node = validator.connect_full_node(rng);

let genesis_block = &validator.node_config.genesis_block;
Expand Down Expand Up @@ -166,7 +182,11 @@ async fn syncing_full_node_from_snapshot(delay_block_storage: bool) {
let ctx = &ctx::test_root(&ctx::AffineClock::new(20.0));
let rng = &mut ctx.rng();

let mut validator = FullValidatorConfig::for_single_validator(rng, Payload(vec![]));
let mut validator = FullValidatorConfig::for_single_validator(
rng,
validator::ProtocolVersion::EARLIEST,
Payload(vec![]),
);
let mut full_node = validator.connect_full_node(rng);

let genesis_block = &validator.node_config.genesis_block;
Expand Down
Loading

0 comments on commit 13c2e8e

Please sign in to comment.