Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into apu-base-token-cache-…
Browse files Browse the repository at this point in the history
…update-interval
  • Loading branch information
cytadela8 committed Jul 4, 2024
2 parents 5a8eeb1 + 2ec494b commit 8044103
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 21 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

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 @@ -682,11 +683,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 @@ -805,3 +806,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 @@ -30,7 +30,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
1 change: 1 addition & 0 deletions core/lib/state/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ tracing.workspace = true
itertools.workspace = true
chrono.workspace = true
once_cell.workspace = true
backon.workspace = true

[dev-dependencies]
assert_matches.workspace = true
Expand Down
25 changes: 21 additions & 4 deletions core/lib/state/src/postgres/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::{
mem,
sync::{Arc, RwLock},
time::Duration,
};

use anyhow::Context as _;
use backon::{BlockingRetryable, ConstantBuilder};
use tokio::{
runtime::Handle,
sync::{
Expand Down Expand Up @@ -489,11 +491,26 @@ impl ReadStorage for PostgresStorage<'_> {
values_cache.and_then(|cache| cache.get(self.l2_block_number, hashed_key));

let value = cached_value.unwrap_or_else(|| {
const RETRY_INTERVAL: Duration = Duration::from_millis(500);
const MAX_TRIES: usize = 20;

let mut dal = self.connection.storage_web3_dal();
let value = self
.rt_handle
.block_on(dal.get_historical_value_unchecked(hashed_key, self.l2_block_number))
.expect("Failed executing `read_value`");
let value = (|| {
self.rt_handle
.block_on(dal.get_historical_value_unchecked(hashed_key, self.l2_block_number))
})
.retry(
&ConstantBuilder::default()
.with_delay(RETRY_INTERVAL)
.with_max_times(MAX_TRIES),
)
.when(|e| {
e.inner()
.as_database_error()
.is_some_and(|e| e.message() == "canceling statement due to statement timeout")
})
.call()
.expect("Failed executing `read_value`");
if let Some(cache) = self.values_cache() {
cache.insert(self.l2_block_number, hashed_key, value);
}
Expand Down
13 changes: 13 additions & 0 deletions prover/Cargo.lock

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

0 comments on commit 8044103

Please sign in to comment.