Skip to content

Commit

Permalink
Merge branch 'main' into matias-consensus-ports
Browse files Browse the repository at this point in the history
  • Loading branch information
matias-gonz authored Oct 8, 2024
2 parents 7ddc653 + 3fd2fb1 commit cc467c5
Show file tree
Hide file tree
Showing 211 changed files with 2,230 additions and 18,765 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ prover/data/keys/setup_*

# Zk Toolbox
chains/era/configs/*
chains/gateway/*
configs/*
era-observability/
core/tests/ts-integration/deployments-zk
Expand Down
13 changes: 6 additions & 7 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ zk_evm_1_4_1 = { package = "zk_evm", version = "0.141" }
zk_evm_1_5_0 = { package = "zk_evm", version = "=0.150.5" }

# New VM; pinned to a specific commit because of instability
zksync_vm2 = { git = "https://github.com/matter-labs/vm2.git", rev = "74577d9be13b1bff9d1a712389731f669b179e47" }
zksync_vm2 = { git = "https://github.com/matter-labs/vm2.git", rev = "a233d44bbe61dc6a758a754c3b78fe4f83e56699" }

# Consensus dependencies.
zksync_concurrency = "=0.3.0"
Expand Down
8 changes: 8 additions & 0 deletions core/bin/external_node/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,8 @@ pub(crate) struct OptionalENConfig {
/// Gateway RPC URL, needed for operating during migration.
#[allow(dead_code)]
pub gateway_url: Option<SensitiveUrl>,
/// Interval for bridge addresses refreshing in seconds.
bridge_addresses_refresh_interval_sec: Option<NonZeroU64>,
}

impl OptionalENConfig {
Expand Down Expand Up @@ -675,6 +677,7 @@ impl OptionalENConfig {
api_namespaces,
contracts_diamond_proxy_addr: None,
gateway_url: enconfig.gateway_url.clone(),
bridge_addresses_refresh_interval_sec: enconfig.bridge_addresses_refresh_interval_sec,
})
}

Expand Down Expand Up @@ -901,6 +904,11 @@ impl OptionalENConfig {
Duration::from_secs(self.pruning_data_retention_sec)
}

pub fn bridge_addresses_refresh_interval(&self) -> Option<Duration> {
self.bridge_addresses_refresh_interval_sec
.map(|n| Duration::from_secs(n.get()))
}

#[cfg(test)]
fn mock() -> Self {
// Set all values to their defaults
Expand Down
4 changes: 4 additions & 0 deletions core/bin/external_node/src/node_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,10 @@ impl ExternalNodeBuilder {
response_body_size_limit: Some(self.config.optional.max_response_body_size()),
with_extended_tracing: self.config.optional.extended_rpc_tracing,
pruning_info_refresh_interval: Some(pruning_info_refresh_interval),
bridge_addresses_refresh_interval: self
.config
.optional
.bridge_addresses_refresh_interval(),
polling_interval: Some(self.config.optional.polling_interval()),
websocket_requests_per_minute_limit: None, // To be set by WS server layer method if required.
replication_lag_limit: None, // TODO: Support replication lag limit
Expand Down
3 changes: 2 additions & 1 deletion core/lib/config/src/configs/en_config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::num::NonZeroUsize;
use std::num::{NonZeroU64, NonZeroUsize};

use serde::Deserialize;
use zksync_basic_types::{
Expand All @@ -19,4 +19,5 @@ pub struct ENConfig {
pub main_node_rate_limit_rps: Option<NonZeroUsize>,

pub gateway_url: Option<SensitiveUrl>,
pub bridge_addresses_refresh_interval_sec: Option<NonZeroU64>,
}
22 changes: 22 additions & 0 deletions core/lib/config/src/configs/eth_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl EthConfig {
pubdata_sending_mode: PubdataSendingMode::Calldata,
tx_aggregation_paused: false,
tx_aggregation_only_prove_and_execute: false,
time_in_mempool_in_l1_blocks_cap: 1800,
}),
gas_adjuster: Some(GasAdjusterConfig {
default_priority_fee_per_gas: 1000000000,
Expand Down Expand Up @@ -127,6 +128,10 @@ pub struct SenderConfig {
/// special mode specifically for gateway migration to decrease number of non-executed batches
#[serde(default = "SenderConfig::default_tx_aggregation_only_prove_and_execute")]
pub tx_aggregation_only_prove_and_execute: bool,

/// Cap of time in mempool for price calculations
#[serde(default = "SenderConfig::default_time_in_mempool_in_l1_blocks_cap")]
pub time_in_mempool_in_l1_blocks_cap: u32,
}

impl SenderConfig {
Expand Down Expand Up @@ -168,6 +173,13 @@ impl SenderConfig {
const fn default_tx_aggregation_only_prove_and_execute() -> bool {
false
}

pub const fn default_time_in_mempool_in_l1_blocks_cap() -> u32 {
let blocks_per_hour = 3600 / 12;
// we cap it at 6h to not allow nearly infinite values when a tx is stuck for a long time
// 1,001 ^ 1800 ~= 6, so by default we cap exponential price formula at roughly median * 6
blocks_per_hour * 6
}
}

#[derive(Debug, Deserialize, Copy, Clone, PartialEq, Default)]
Expand All @@ -177,8 +189,10 @@ pub struct GasAdjusterConfig {
/// Number of blocks collected by GasAdjuster from which base_fee median is taken
pub max_base_fee_samples: usize,
/// Parameter of the transaction base_fee_per_gas pricing formula
#[serde(default = "GasAdjusterConfig::default_pricing_formula_parameter_a")]
pub pricing_formula_parameter_a: f64,
/// Parameter of the transaction base_fee_per_gas pricing formula
#[serde(default = "GasAdjusterConfig::default_pricing_formula_parameter_b")]
pub pricing_formula_parameter_b: f64,
/// Parameter by which the base fee will be multiplied for internal purposes
pub internal_l1_pricing_multiplier: f64,
Expand Down Expand Up @@ -225,4 +239,12 @@ impl GasAdjusterConfig {
pub const fn default_internal_pubdata_pricing_multiplier() -> f64 {
1.0
}

pub const fn default_pricing_formula_parameter_a() -> f64 {
1.1
}

pub const fn default_pricing_formula_parameter_b() -> f64 {
1.001
}
}
2 changes: 2 additions & 0 deletions core/lib/config/src/testonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ impl Distribution<configs::eth_sender::SenderConfig> for EncodeDist {
pubdata_sending_mode: PubdataSendingMode::Calldata,
tx_aggregation_paused: false,
tx_aggregation_only_prove_and_execute: false,
time_in_mempool_in_l1_blocks_cap: self.sample(rng),
}
}
}
Expand Down Expand Up @@ -934,6 +935,7 @@ impl Distribution<configs::en_config::ENConfig> for EncodeDist {
main_node_rate_limit_rps: self.sample_opt(|| rng.gen()),
gateway_url: self
.sample_opt(|| format!("localhost:{}", rng.gen::<u16>()).parse().unwrap()),
bridge_addresses_refresh_interval_sec: self.sample_opt(|| rng.gen()),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions core/lib/env_config/src/eth_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ mod tests {
pubdata_sending_mode: PubdataSendingMode::Calldata,
tx_aggregation_only_prove_and_execute: false,
tx_aggregation_paused: false,
time_in_mempool_in_l1_blocks_cap: 2000,
}),
gas_adjuster: Some(GasAdjusterConfig {
default_priority_fee_per_gas: 20000000000,
Expand Down Expand Up @@ -131,6 +132,7 @@ mod tests {
ETH_SENDER_SENDER_TIMESTAMP_CRITERIA_MAX_ALLOWED_LAG="30"
ETH_SENDER_SENDER_MAX_AGGREGATED_TX_GAS="4000000"
ETH_SENDER_SENDER_MAX_ETH_TX_DATA_SIZE="120000"
ETH_SENDER_SENDER_TIME_IN_MEMPOOL_IN_L1_BLOCKS_CAP="2000"
ETH_SENDER_SENDER_L1_BATCH_MIN_AGE_BEFORE_EXECUTE_SECONDS="1000"
ETH_SENDER_SENDER_MAX_ACCEPTABLE_PRIORITY_FEE_IN_GWEI="100000000000"
ETH_SENDER_SENDER_PUBDATA_SENDING_MODE="Calldata"
Expand Down
12 changes: 3 additions & 9 deletions core/lib/eth_client/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ pub struct FailureInfo {

#[cfg(test)]
mod tests {
use zksync_eth_signer::{EthereumSigner, PrivateKeySigner, TransactionParameters};
use zksync_eth_signer::{PrivateKeySigner, TransactionParameters};
use zksync_types::{
eth_sender::{EthTxBlobSidecarV1, SidecarBlobV1},
web3, K256PrivateKey, EIP_4844_TX_TYPE, H256, U256, U64,
Expand Down Expand Up @@ -384,10 +384,7 @@ mod tests {
.as_ref(),
)]),
};
let raw_tx = signer
.sign_transaction(raw_transaction.clone())
.await
.unwrap();
let raw_tx = signer.sign_transaction(raw_transaction.clone());

let hash = web3::keccak256(&raw_tx).into();
// Transaction generated with https://github.com/inphi/blob-utils with
Expand Down Expand Up @@ -493,10 +490,7 @@ mod tests {
blob_versioned_hashes: Some(vec![versioned_hash_1, versioned_hash_2]),
};

let raw_tx = signer
.sign_transaction(raw_transaction.clone())
.await
.unwrap();
let raw_tx = signer.sign_transaction(raw_transaction);

let hash = web3::keccak256(&raw_tx).into();
// Transaction generated with https://github.com/inphi/blob-utils with
Expand Down
9 changes: 4 additions & 5 deletions core/lib/eth_signer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ keywords.workspace = true
categories.workspace = true

[dependencies]
zksync_types.workspace = true
zksync_basic_types.workspace = true
zksync_crypto_primitives.workspace = true

async-trait.workspace = true
rlp.workspace = true
thiserror.workspace = true
async-trait.workspace = true

[dev-dependencies]
tokio = { workspace = true, features = ["full"] }
1 change: 0 additions & 1 deletion core/lib/eth_signer/src/error.rs

This file was deleted.

3 changes: 2 additions & 1 deletion core/lib/eth_signer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use async_trait::async_trait;
use zksync_types::{Address, EIP712TypedStructure, Eip712Domain, PackedEthSignature};
use zksync_basic_types::Address;
use zksync_crypto_primitives::{EIP712TypedStructure, Eip712Domain, PackedEthSignature};

pub use crate::{pk_signer::PrivateKeySigner, raw_ethereum_tx::TransactionParameters};

Expand Down
65 changes: 39 additions & 26 deletions core/lib/eth_signer/src/pk_signer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use zksync_types::{
Address, EIP712TypedStructure, Eip712Domain, K256PrivateKey, PackedEthSignature,
use async_trait::async_trait;
use zksync_basic_types::Address;
use zksync_crypto_primitives::{
EIP712TypedStructure, Eip712Domain, K256PrivateKey, PackedEthSignature,
};

use crate::{
Expand All @@ -12,22 +14,20 @@ pub struct PrivateKeySigner {
private_key: K256PrivateKey,
}

// We define inherent methods duplicating `EthereumSigner` ones because they are sync and (other than `sign_typed_data`) infallible.
impl PrivateKeySigner {
pub fn new(private_key: K256PrivateKey) -> Self {
Self { private_key }
}
}

#[async_trait::async_trait]
impl EthereumSigner for PrivateKeySigner {
/// Get Ethereum address that matches the private key.
async fn get_address(&self) -> Result<Address, SignerError> {
Ok(self.private_key.address())
/// Gets an Ethereum address that matches this private key.
pub fn address(&self) -> Address {
self.private_key.address()
}

/// Signs typed struct using Ethereum private key by EIP-712 signature standard.
/// Result of this function is the equivalent of RPC calling `eth_signTypedData`.
async fn sign_typed_data<S: EIP712TypedStructure + Sync>(
pub fn sign_typed_data<S: EIP712TypedStructure + Sync>(
&self,
domain: &Eip712Domain,
typed_struct: &S,
Expand All @@ -39,16 +39,11 @@ impl EthereumSigner for PrivateKeySigner {
}

/// Signs and returns the RLP-encoded transaction.
async fn sign_transaction(
&self,
raw_tx: TransactionParameters,
) -> Result<Vec<u8>, SignerError> {
pub fn sign_transaction(&self, raw_tx: TransactionParameters) -> Vec<u8> {
// According to the code in web3 <https://docs.rs/web3/latest/src/web3/api/accounts.rs.html#86>
// We should use `max_fee_per_gas` as `gas_price` if we use EIP1559
let gas_price = raw_tx.max_fee_per_gas;

let max_priority_fee_per_gas = raw_tx.max_priority_fee_per_gas;

let tx = Transaction {
to: raw_tx.to,
nonce: raw_tx.nonce,
Expand All @@ -62,21 +57,42 @@ impl EthereumSigner for PrivateKeySigner {
max_fee_per_blob_gas: raw_tx.max_fee_per_blob_gas,
blob_versioned_hashes: raw_tx.blob_versioned_hashes,
};

let signed = tx.sign(&self.private_key, raw_tx.chain_id);
Ok(signed.raw_transaction.0)
signed.raw_transaction.0
}
}

#[async_trait]
impl EthereumSigner for PrivateKeySigner {
async fn get_address(&self) -> Result<Address, SignerError> {
Ok(self.address())
}

async fn sign_typed_data<S: EIP712TypedStructure + Sync>(
&self,
domain: &Eip712Domain,
typed_struct: &S,
) -> Result<PackedEthSignature, SignerError> {
self.sign_typed_data(domain, typed_struct)
}

async fn sign_transaction(
&self,
raw_tx: TransactionParameters,
) -> Result<Vec<u8>, SignerError> {
Ok(self.sign_transaction(raw_tx))
}
}

#[cfg(test)]
mod test {
use zksync_types::{K256PrivateKey, H160, H256, U256, U64};
use zksync_basic_types::{H160, H256, U256, U64};
use zksync_crypto_primitives::K256PrivateKey;

use super::PrivateKeySigner;
use crate::{raw_ethereum_tx::TransactionParameters, EthereumSigner};
use super::*;

#[tokio::test]
async fn test_generating_signed_raw_transaction() {
#[test]
fn test_generating_signed_raw_transaction() {
let private_key = K256PrivateKey::from_bytes(H256::from([5; 32])).unwrap();
let signer = PrivateKeySigner::new(private_key);
let raw_transaction = TransactionParameters {
Expand All @@ -94,10 +110,7 @@ mod test {
blob_versioned_hashes: None,
max_fee_per_blob_gas: None,
};
let raw_tx = signer
.sign_transaction(raw_transaction.clone())
.await
.unwrap();
let raw_tx = signer.sign_transaction(raw_transaction);
assert_ne!(raw_tx.len(), 1);
// pre-calculated signature with right algorithm implementation
let precalculated_raw_tx: Vec<u8> = vec![
Expand Down
6 changes: 3 additions & 3 deletions core/lib/eth_signer/src/raw_ethereum_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
//! Link to @Deniallugo's PR to web3: https://github.com/tomusdrw/rust-web3/pull/630
use rlp::RlpStream;
use zksync_types::{
ethabi::Address,
use zksync_basic_types::{
web3::{keccak256, AccessList, Signature, SignedTransaction},
K256PrivateKey, H256, U256, U64,
Address, H256, U256, U64,
};
use zksync_crypto_primitives::K256PrivateKey;

const LEGACY_TX_ID: u64 = 0;
const ACCESSLISTS_TX_ID: u64 = 1;
Expand Down
1 change: 0 additions & 1 deletion core/lib/multivm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ vise.workspace = true
[dev-dependencies]
assert_matches.workspace = true
pretty_assertions.workspace = true
tokio = { workspace = true, features = ["time"] }
zksync_test_account.workspace = true
ethabi.workspace = true
zksync_eth_signer.workspace = true
Loading

0 comments on commit cc467c5

Please sign in to comment.