Skip to content

Commit

Permalink
made validator data extensible in a backward compatible way
Browse files Browse the repository at this point in the history
  • Loading branch information
pompon0 committed Sep 6, 2024
1 parent 4459f81 commit 25b9cf8
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 51 deletions.
17 changes: 10 additions & 7 deletions node/actors/network/src/consensus/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,16 @@ async fn test_genesis_mismatch() {
.validator_addrs
.update(
&setup.genesis.validators,
&[Arc::new(setup.validator_keys[1].sign_msg(
validator::NetAddress {
addr: *cfgs[1].server_addr,
version: 0,
timestamp: ctx.now_utc(),
},
))],
&[Arc::new(
setup.validator_keys[1].sign_msg(
validator::NetAddress {
addr: *cfgs[1].server_addr,
version: 0,
timestamp: ctx.now_utc(),
}
.into(),
),
)],
)
.await
.unwrap();
Expand Down
32 changes: 20 additions & 12 deletions node/actors/network/src/gossip/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,25 +90,30 @@ fn mk_version<R: Rng>(rng: &mut R) -> u64 {
}

#[derive(Default)]
struct View(im::HashMap<validator::PublicKey, Arc<validator::Signed<validator::NetAddress>>>);
struct View(
im::HashMap<validator::PublicKey, Arc<validator::Signed<validator::EncodedNetAddress>>>,
);

fn mk_netaddr(
key: &validator::SecretKey,
addr: std::net::SocketAddr,
version: u64,
timestamp: time::Utc,
) -> validator::Signed<validator::NetAddress> {
key.sign_msg(validator::NetAddress {
addr,
version,
timestamp,
})
) -> validator::Signed<validator::EncodedNetAddress> {
key.sign_msg(
validator::NetAddress {
addr,
version,
timestamp,
}
.into(),
)
}

fn random_netaddr<R: Rng>(
rng: &mut R,
key: &validator::SecretKey,
) -> Arc<validator::Signed<validator::NetAddress>> {
) -> Arc<validator::Signed<validator::EncodedNetAddress>> {
Arc::new(mk_netaddr(
key,
mk_addr(rng),
Expand All @@ -123,7 +128,7 @@ fn update_netaddr<R: Rng>(
key: &validator::SecretKey,
version_diff: i64,
timestamp_diff: time::Duration,
) -> Arc<validator::Signed<validator::NetAddress>> {
) -> Arc<validator::Signed<validator::EncodedNetAddress>> {
Arc::new(mk_netaddr(
key,
mk_addr(rng),
Expand All @@ -133,15 +138,18 @@ fn update_netaddr<R: Rng>(
}

impl View {
fn insert(&mut self, entry: Arc<validator::Signed<validator::NetAddress>>) {
fn insert(&mut self, entry: Arc<validator::Signed<validator::EncodedNetAddress>>) {
self.0.insert(entry.key.clone(), entry);
}

fn get(&mut self, key: &validator::SecretKey) -> Arc<validator::Signed<validator::NetAddress>> {
fn get(
&mut self,
key: &validator::SecretKey,
) -> Arc<validator::Signed<validator::EncodedNetAddress>> {
self.0.get(&key.public()).unwrap().clone()
}

fn as_vec(&self) -> Vec<Arc<validator::Signed<validator::NetAddress>>> {
fn as_vec(&self) -> Vec<Arc<validator::Signed<validator::EncodedNetAddress>>> {
self.0.values().cloned().collect()
}
}
Expand Down
36 changes: 19 additions & 17 deletions node/actors/network/src/gossip/validator_addrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@ use std::{collections::HashSet, sync::Arc};
use zksync_concurrency::{sync, time};
use zksync_consensus_roles::validator;

/// Mapping from validator::PublicKey to a signed validator::NetAddress.
type SignedAddr = Arc<validator::Signed<validator::EncodedNetAddress>>;

/// Mapping from validator::PublicKey to a signed validator::EncodedNetAddress.
/// Represents the currents state of node's knowledge about the validator endpoints.
#[derive(Clone, Default, PartialEq, Eq)]
pub(crate) struct ValidatorAddrs(
pub(super) im::HashMap<validator::PublicKey, Arc<validator::Signed<validator::NetAddress>>>,
);
pub(crate) struct ValidatorAddrs(pub(super) im::HashMap<validator::PublicKey, SignedAddr>);

impl ValidatorAddrs {
/// Gets a NetAddress for a given key.
pub(crate) fn get(
&self,
key: &validator::PublicKey,
) -> Option<&Arc<validator::Signed<validator::NetAddress>>> {
/// Gets a EncodedNetAddress for a given key.
pub(crate) fn get(&self, key: &validator::PublicKey) -> Option<&SignedAddr> {
self.0.get(key)
}

/// Returns a set of entries of `self` which are newer than the entries in `b`.
pub(super) fn get_newer(&self, b: &Self) -> Vec<Arc<validator::Signed<validator::NetAddress>>> {
pub(super) fn get_newer(&self, b: &Self) -> Vec<SignedAddr> {
let mut newer = vec![];
for (k, v) in &self.0 {
if let Some(bv) = b.0.get(k) {
Expand All @@ -42,7 +39,7 @@ impl ValidatorAddrs {
pub(super) fn update(
&mut self,
validators: &validator::Committee,
data: &[Arc<validator::Signed<validator::NetAddress>>],
data: &[SignedAddr],
) -> anyhow::Result<bool> {
let mut changed = false;

Expand Down Expand Up @@ -103,11 +100,16 @@ impl ValidatorAddrsWatch {
.get(&key.public())
.map(|x| x.msg.version + 1)
.unwrap_or(0);
let d = Arc::new(key.sign_msg(validator::NetAddress {
addr,
version,
timestamp,
}));
let d = Arc::new(
key.sign_msg(
validator::NetAddress {
addr,
version,
timestamp,
}
.into(),
),
);
validator_addrs.0.insert(d.key.clone(), d);
this.send_replace(validator_addrs);
}
Expand All @@ -120,7 +122,7 @@ impl ValidatorAddrsWatch {
pub(crate) async fn update(
&self,
validators: &validator::Committee,
data: &[Arc<validator::Signed<validator::NetAddress>>],
data: &[SignedAddr],
) -> anyhow::Result<()> {
let this = self.0.lock().await;
let mut validator_addrs = this.borrow().clone();
Expand Down
2 changes: 1 addition & 1 deletion node/actors/network/src/rpc/push_validator_addrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl super::Rpc for Rpc {

/// Contains a batch of new ValidatorAddrs that the sender has learned about.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Req(pub(crate) Vec<Arc<validator::Signed<validator::NetAddress>>>);
pub(crate) struct Req(pub(crate) Vec<Arc<validator::Signed<validator::EncodedNetAddress>>>);

impl ProtoFmt for Req {
type Proto = proto::PushValidatorAddrs;
Expand Down
2 changes: 1 addition & 1 deletion node/actors/network/src/rpc/testonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Distribution<rpc::push_validator_addrs::Req> for Standard {
(0..n)
.map(|_| {
let key: validator::SecretKey = rng.gen();
let addr: validator::NetAddress = rng.gen();
let addr: validator::EncodedNetAddress = rng.gen();
Arc::new(key.sign_msg(addr))
})
.collect(),
Expand Down
2 changes: 1 addition & 1 deletion node/libs/roles/src/proto/validator/messages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ message Msg {
oneof t {// required
ConsensusMsg consensus = 1;
bytes session_id = 2;
NetAddress net_address = 3;
bytes net_address = 3; // encoded NetAddress.
}
}

Expand Down
13 changes: 8 additions & 5 deletions node/libs/roles/src/validator/conv.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use super::{
AggregateSignature, BlockHeader, BlockNumber, ChainId, CommitQC, Committee, ConsensusMsg,
FinalBlock, ForkNumber, Genesis, GenesisHash, GenesisRaw, LeaderCommit, LeaderPrepare, Msg,
MsgHash, NetAddress, Payload, PayloadHash, Phase, PrepareQC, ProtocolVersion, PublicKey,
ReplicaCommit, ReplicaPrepare, Signature, Signed, Signers, View, ViewNumber, WeightedValidator,
EncodedNetAddress, FinalBlock, ForkNumber, Genesis, GenesisHash, GenesisRaw, LeaderCommit,
LeaderPrepare, Msg, MsgHash, NetAddress, Payload, PayloadHash, Phase, PrepareQC,
ProtocolVersion, PublicKey, ReplicaCommit, ReplicaPrepare, Signature, Signed, Signers, View,
ViewNumber, WeightedValidator,
};
use crate::{
attester::{self, WeightedAttester},
Expand Down Expand Up @@ -373,7 +374,9 @@ impl ProtoFmt for Msg {
Ok(match r.t.as_ref().context("missing")? {
T::Consensus(r) => Self::Consensus(ProtoFmt::read(r).context("Consensus")?),
T::SessionId(r) => Self::SessionId(SessionId(r.clone())),
T::NetAddress(r) => Self::NetAddress(ProtoFmt::read(r).context("NetAddress")?),
T::NetAddress(r) => {
Self::NetAddress(EncodedNetAddress::new(r.to_vec()).context("NetAddress")?)
}
})
}

Expand All @@ -383,7 +386,7 @@ impl ProtoFmt for Msg {
let t = match self {
Self::Consensus(x) => T::Consensus(x.build()),
Self::SessionId(x) => T::SessionId(x.0.clone()),
Self::NetAddress(x) => T::NetAddress(x.build()),
Self::NetAddress(x) => T::NetAddress(x.encoded().to_vec()),
};

Self::Proto { t: Some(t) }
Expand Down
33 changes: 33 additions & 0 deletions node/libs/roles/src/validator/messages/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,39 @@ pub struct NetAddress {
pub timestamp: time::Utc,
}

/// Protobuf encoded NetAddress.
/// It is used to avoid reencoding data and
/// therefore losing the unknown fields.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct EncodedNetAddress(NetAddress, Vec<u8>);

impl std::ops::Deref for EncodedNetAddress {
type Target = NetAddress;
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl From<NetAddress> for EncodedNetAddress {
fn from(x: NetAddress) -> Self {
let bytes = zksync_protobuf::canonical(&x);
Self(x, bytes)
}
}

impl EncodedNetAddress {
/// Constructs EncodedNetAddress from
/// encoded representation.
pub fn new(bytes: Vec<u8>) -> anyhow::Result<Self> {
let x = zksync_protobuf::decode(&bytes)?;
Ok(Self(x, bytes))
}
/// Reference to encoded represetation.
pub fn encoded(&self) -> &[u8] {
&self.1
}
}

impl NetAddress {
/// Checks if `self` is a newer version than `b`.
pub fn is_newer(&self, b: &Self) -> bool {
Expand Down
6 changes: 3 additions & 3 deletions node/libs/roles/src/validator/messages/msg.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Generic message types.
use super::{ConsensusMsg, NetAddress};
use super::{ConsensusMsg, EncodedNetAddress};
use crate::{node::SessionId, validator};
use std::fmt;
use zksync_consensus_crypto::{keccak256, ByteFmt, Text, TextFmt};
Expand All @@ -13,7 +13,7 @@ pub enum Msg {
/// authentication
SessionId(SessionId),
/// validator discovery
NetAddress(NetAddress),
NetAddress(EncodedNetAddress),
}

impl Msg {
Expand Down Expand Up @@ -47,7 +47,7 @@ impl Variant<Msg> for SessionId {
}
}

impl Variant<Msg> for NetAddress {
impl Variant<Msg> for EncodedNetAddress {
fn insert(self) -> Msg {
Msg::NetAddress(self)
}
Expand Down
14 changes: 10 additions & 4 deletions node/libs/roles/src/validator/testonly.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! Test-only utilities.
use super::{
AggregateSignature, BlockHeader, BlockNumber, ChainId, CommitQC, Committee, ConsensusMsg,
FinalBlock, ForkNumber, Genesis, GenesisHash, GenesisRaw, LeaderCommit, LeaderPrepare, Msg,
MsgHash, NetAddress, Payload, PayloadHash, Phase, PrepareQC, ProofOfPossession,
ProtocolVersion, PublicKey, ReplicaCommit, ReplicaPrepare, SecretKey, Signature, Signed,
Signers, View, ViewNumber, WeightedValidator,
EncodedNetAddress, FinalBlock, ForkNumber, Genesis, GenesisHash, GenesisRaw, LeaderCommit,
LeaderPrepare, Msg, MsgHash, NetAddress, Payload, PayloadHash, Phase, PrepareQC,
ProofOfPossession, ProtocolVersion, PublicKey, ReplicaCommit, ReplicaPrepare, SecretKey,
Signature, Signed, Signers, View, ViewNumber, WeightedValidator,
};
use crate::{attester, validator::LeaderSelectionMode};
use bit_vec::BitVec;
Expand Down Expand Up @@ -472,6 +472,12 @@ impl Distribution<Phase> for Standard {
}
}

impl Distribution<EncodedNetAddress> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> EncodedNetAddress {
rng.gen::<NetAddress>().into()
}
}

impl Distribution<NetAddress> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> NetAddress {
NetAddress {
Expand Down

0 comments on commit 25b9cf8

Please sign in to comment.