Skip to content

Commit

Permalink
Update from upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
slowli committed Oct 8, 2024
2 parents f2532ec + 3fd2fb1 commit 449b55a
Show file tree
Hide file tree
Showing 198 changed files with 2,004 additions and 18,694 deletions.
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
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
}
}
1 change: 1 addition & 0 deletions core/lib/config/src/testonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,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
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 @@ -42,6 +42,5 @@ ethabi.workspace = true
[dev-dependencies]
assert_matches.workspace = true
pretty_assertions.workspace = true
tokio = { workspace = true, features = ["time"] }
zksync_test_account.workspace = true
zksync_eth_signer.workspace = true
36 changes: 19 additions & 17 deletions core/lib/multivm/src/versions/vm_1_3_2/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ pub struct Vm<S: WriteStorage, H: HistoryMode> {
pub(crate) system_env: SystemEnv,
}

impl<S: WriteStorage, H: HistoryMode> Vm<S, H> {
pub(crate) fn record_vm_memory_metrics(&self) -> VmMemoryMetrics {
VmMemoryMetrics {
event_sink_inner: self.vm.state.event_sink.get_size(),
event_sink_history: self.vm.state.event_sink.get_history_size(),
memory_inner: self.vm.state.memory.get_size(),
memory_history: self.vm.state.memory.get_history_size(),
decommittment_processor_inner: self.vm.state.decommittment_processor.get_size(),
decommittment_processor_history: self
.vm
.state
.decommittment_processor
.get_history_size(),
storage_inner: self.vm.state.storage.get_size(),
storage_history: self.vm.state.storage.get_history_size(),
}
}
}

impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
type TracerDispatcher = TracerDispatcher;

Expand Down Expand Up @@ -160,23 +179,6 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
}
}

fn record_vm_memory_metrics(&self) -> VmMemoryMetrics {
VmMemoryMetrics {
event_sink_inner: self.vm.state.event_sink.get_size(),
event_sink_history: self.vm.state.event_sink.get_history_size(),
memory_inner: self.vm.state.memory.get_size(),
memory_history: self.vm.state.memory.get_history_size(),
decommittment_processor_inner: self.vm.state.decommittment_processor.get_size(),
decommittment_processor_history: self
.vm
.state
.decommittment_processor
.get_history_size(),
storage_inner: self.vm.state.storage.get_size(),
storage_history: self.vm.state.storage.get_history_size(),
}
}

fn finish_batch(&mut self) -> FinishedL1Batch {
self.vm
.execute_till_block_end(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ pub(super) fn assemble_tx_meta(execution_mode: TxExecutionMode, execute_tx: bool
// Set 0 byte (execution mode)
output[0] = match execution_mode {
TxExecutionMode::VerifyExecute => 0x00,
TxExecutionMode::EstimateFee { .. } => 0x00,
TxExecutionMode::EthCall { .. } => 0x02,
TxExecutionMode::EstimateFee => 0x00,
TxExecutionMode::EthCall => 0x02,
};

// Set 31 byte (marker for tx execution)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<S: WriteStorage, H: HistoryMode> Vm<S, H> {
}

/// Returns the info about all oracles' sizes.
pub(crate) fn record_vm_memory_metrics_inner(&self) -> VmMemoryMetrics {
pub(crate) fn record_vm_memory_metrics(&self) -> VmMemoryMetrics {
VmMemoryMetrics {
event_sink_inner: self.state.event_sink.get_size(),
event_sink_history: self.state.event_sink.get_history_size(),
Expand Down
Loading

0 comments on commit 449b55a

Please sign in to comment.