Skip to content

Commit

Permalink
fix(config): Implement proper tests
Browse files Browse the repository at this point in the history
Signed-off-by: Danil <[email protected]>
  • Loading branch information
Deniallugo committed Jul 3, 2024
1 parent 45c7a0a commit d3053fc
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 17 deletions.
10 changes: 5 additions & 5 deletions core/lib/config/src/configs/wallets.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use zksync_basic_types::{Address, H160, H256};
use zksync_crypto_primitives::K256PrivateKey;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct AddressWallet {
address: Address,
}
Expand All @@ -16,7 +16,7 @@ impl AddressWallet {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct Wallet {
address: Address,
private_key: K256PrivateKey,
Expand Down Expand Up @@ -58,18 +58,18 @@ impl Wallet {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct EthSender {
pub operator: Wallet,
pub blob_operator: Option<Wallet>,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct StateKeeper {
pub fee_account: AddressWallet,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct Wallets {
pub eth_sender: Option<EthSender>,
pub state_keeper: Option<StateKeeper>,
Expand Down
64 changes: 59 additions & 5 deletions core/lib/config/src/testonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use zksync_basic_types::{
L1BatchNumber, L1ChainId, L2ChainId,
};
use zksync_consensus_utils::EncodeDist;
use zksync_crypto_primitives::K256PrivateKey;

use crate::configs::{self, eth_sender::PubdataSendingMode};

Expand Down Expand Up @@ -681,11 +682,11 @@ impl Distribution<configs::GenesisConfig> for EncodeDist {
.unwrap(),
patch: VersionPatch(rng.gen()),
}),
genesis_root_hash: rng.gen(),
rollup_last_leaf_index: self.sample(rng),
genesis_commitment: rng.gen(),
bootloader_hash: rng.gen(),
default_aa_hash: rng.gen(),
genesis_root_hash: Some(rng.gen()),
rollup_last_leaf_index: Some(self.sample(rng)),
genesis_commitment: Some(rng.gen()),
bootloader_hash: Some(rng.gen()),
default_aa_hash: Some(rng.gen()),
fee_account: rng.gen(),
l1_chain_id: L1ChainId(self.sample(rng)),
l2_chain_id: L2ChainId::default(),
Expand Down Expand Up @@ -804,3 +805,56 @@ impl Distribution<configs::secrets::Secrets> for EncodeDist {
}
}
}

impl Distribution<configs::wallets::Wallet> for EncodeDist {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> configs::wallets::Wallet {
configs::wallets::Wallet::new(K256PrivateKey::from_bytes(rng.gen()).unwrap())
}
}

impl Distribution<configs::wallets::AddressWallet> for EncodeDist {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> configs::wallets::AddressWallet {
configs::wallets::AddressWallet::from_address(rng.gen())
}
}

impl Distribution<configs::wallets::StateKeeper> for EncodeDist {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> configs::wallets::StateKeeper {
configs::wallets::StateKeeper {
fee_account: self.sample(rng),
}
}
}

impl Distribution<configs::wallets::EthSender> for EncodeDist {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> configs::wallets::EthSender {
configs::wallets::EthSender {
operator: self.sample(rng),
blob_operator: self.sample_opt(|| self.sample(rng)),
}
}
}

impl Distribution<configs::wallets::Wallets> for EncodeDist {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> configs::wallets::Wallets {
configs::wallets::Wallets {
state_keeper: self.sample_opt(|| self.sample(rng)),
eth_sender: self.sample_opt(|| self.sample(rng)),
}
}
}

impl Distribution<configs::en_config::ENConfig> for EncodeDist {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> configs::en_config::ENConfig {
configs::en_config::ENConfig {
l2_chain_id: L2ChainId::default(),
l1_chain_id: L1ChainId(rng.gen()),
main_node_url: format!("localhost:{}", rng.gen::<u16>()).parse().unwrap(),
l1_batch_commit_data_generator_mode: match rng.gen_range(0..2) {
0 => L1BatchCommitmentMode::Rollup,
_ => L1BatchCommitmentMode::Validium,
},
main_node_rate_limit_rps: self.sample_opt(|| rng.gen()),
}
}
}
2 changes: 1 addition & 1 deletion core/lib/crypto_primitives/src/ecdsa_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Public = H512;
///
/// Provides a safe to use `Debug` implementation (outputting the address corresponding to the key).
/// The key is zeroized on drop.
#[derive(Clone)]
#[derive(Clone, PartialEq)]
pub struct K256PrivateKey(SecretKey);

impl fmt::Debug for K256PrivateKey {
Expand Down
2 changes: 1 addition & 1 deletion core/lib/protobuf_config/src/en.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl ProtoRepr for proto::ExternalNode {
)
.into(),
),
main_node_rate_limit_rps: this.main_node_rate_limit_rps.map(|a| a.get() as u32),
main_node_rate_limit_rps: this.main_node_rate_limit_rps.map(|a| a.get() as u64),
}
}
}
1 change: 0 additions & 1 deletion core/lib/protobuf_config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ mod secrets;
mod snapshots_creator;

mod snapshot_recovery;
pub mod testonly;
#[cfg(test)]
mod tests;
mod utils;
Expand Down
2 changes: 1 addition & 1 deletion core/lib/protobuf_config/src/proto/config/en.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ message ExternalNode {
optional string main_node_url = 1; // required
optional uint64 l2_chain_id = 2; // required
optional uint64 l1_chain_id = 3; // required
optional uint32 main_node_rate_limit_rps = 6; // optional
optional uint64 main_node_rate_limit_rps = 6; // optional
optional config.genesis.L1BatchCommitDataGeneratorMode l1_batch_commit_data_generator_mode = 7; // optional, default to rollup
}
1 change: 0 additions & 1 deletion core/lib/protobuf_config/src/testonly.rs

This file was deleted.

3 changes: 3 additions & 0 deletions core/lib/protobuf_config/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ fn test_encoding() {
test_encode_all_formats::<ReprConv<proto::prover::ProofDataHandler>>(rng);
test_encode_all_formats::<ReprConv<proto::snapshot_creator::SnapshotsCreator>>(rng);
test_encode_all_formats::<ReprConv<proto::observability::Observability>>(rng);
test_encode_all_formats::<ReprConv<proto::wallets::Wallets>>(rng);
test_encode_all_formats::<ReprConv<proto::genesis::Genesis>>(rng);
test_encode_all_formats::<ReprConv<proto::en::ExternalNode>>(rng);
}

#[test]
Expand Down
12 changes: 10 additions & 2 deletions core/lib/protobuf_config/src/wallets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,20 @@ impl ProtoRepr for proto::Wallets {
.as_ref()
.map(|blob| proto::PrivateKeyWallet {
address: Some(format!("{:?}", blob.address())),
private_key: Some(format!("{:?}", blob.private_key())),
private_key: Some(hex::encode(
blob.private_key().expose_secret().secret_bytes(),
)),
});
(
Some(proto::PrivateKeyWallet {
address: Some(format!("{:?}", eth_sender.operator.address())),
private_key: Some(format!("{:?}", eth_sender.operator.private_key())),
private_key: Some(hex::encode(
eth_sender
.operator
.private_key()
.expose_secret()
.secret_bytes(),
)),
}),
blob,
)
Expand Down

0 comments on commit d3053fc

Please sign in to comment.