Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vm): Extend bootloader memory in the new version #1807

Merged
merged 9 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions core/lib/basic_types/src/protocol_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,20 @@ pub enum ProtocolVersionId {
Version20,
Version21,
Version22,
// Version `23` is only present on the internal staging networks.
// All the user-facing environments were switched from 22 to 24 right away.
Version23,
Version24,
Version25,
}

impl ProtocolVersionId {
pub fn latest() -> Self {
Self::Version23
Self::Version24
}

pub fn next() -> Self {
Self::Version24
Self::Version25
}

/// Returns VM version to be used by API for this protocol version.
Expand Down Expand Up @@ -84,8 +87,9 @@ impl ProtocolVersionId {
ProtocolVersionId::Version20 => VmVersion::Vm1_4_1,
ProtocolVersionId::Version21 => VmVersion::Vm1_4_2,
ProtocolVersionId::Version22 => VmVersion::Vm1_4_2,
ProtocolVersionId::Version23 => VmVersion::Vm1_5_0,
ProtocolVersionId::Version24 => VmVersion::Vm1_5_0,
ProtocolVersionId::Version23 => VmVersion::Vm1_5_0SmallBootloaderMemory,
ProtocolVersionId::Version24 => VmVersion::Vm1_5_0IncreasedBootloaderMemory,
ProtocolVersionId::Version25 => VmVersion::Vm1_5_0IncreasedBootloaderMemory,
}
}

Expand Down Expand Up @@ -222,8 +226,9 @@ impl From<ProtocolVersionId> for VmVersion {
ProtocolVersionId::Version20 => VmVersion::Vm1_4_1,
ProtocolVersionId::Version21 => VmVersion::Vm1_4_2,
ProtocolVersionId::Version22 => VmVersion::Vm1_4_2,
ProtocolVersionId::Version23 => VmVersion::Vm1_5_0,
ProtocolVersionId::Version24 => VmVersion::Vm1_5_0,
ProtocolVersionId::Version23 => VmVersion::Vm1_5_0SmallBootloaderMemory,
ProtocolVersionId::Version24 => VmVersion::Vm1_5_0IncreasedBootloaderMemory,
ProtocolVersionId::Version25 => VmVersion::Vm1_5_0IncreasedBootloaderMemory,
}
}
}
5 changes: 3 additions & 2 deletions core/lib/basic_types/src/vm_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ pub enum VmVersion {
VmBoojumIntegration,
Vm1_4_1,
Vm1_4_2,
Vm1_5_0,
Vm1_5_0SmallBootloaderMemory,
Vm1_5_0IncreasedBootloaderMemory,
}

impl VmVersion {
/// Returns the latest supported VM version.
pub const fn latest() -> VmVersion {
Self::Vm1_5_0
Self::Vm1_5_0IncreasedBootloaderMemory
}
}
22 changes: 18 additions & 4 deletions core/lib/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,16 @@ impl BaseSystemContracts {
BaseSystemContracts::load_with_bootloader(bootloader_bytecode)
}

pub fn playground_post_1_5_0() -> Self {
pub fn playground_1_5_0_small_memory() -> Self {
let bootloader_bytecode = read_zbin_bytecode(
"etc/multivm_bootloaders/vm_1_5_0/playground_batch.yul/playground_batch.yul.zbin",
"etc/multivm_bootloaders/vm_1_5_0_small_memory/playground_batch.yul/playground_batch.yul.zbin",
);
BaseSystemContracts::load_with_bootloader(bootloader_bytecode)
}

pub fn playground_post_1_5_0_increased_memory() -> Self {
let bootloader_bytecode = read_zbin_bytecode(
"etc/multivm_bootloaders/vm_1_5_0_increased_memory/playground_batch.yul/playground_batch.yul.zbin",
);
BaseSystemContracts::load_with_bootloader(bootloader_bytecode)
}
Expand Down Expand Up @@ -406,9 +413,16 @@ impl BaseSystemContracts {
BaseSystemContracts::load_with_bootloader(bootloader_bytecode)
}

pub fn estimate_gas_post_1_5_0() -> Self {
pub fn estimate_gas_1_5_0_small_memory() -> Self {
let bootloader_bytecode = read_zbin_bytecode(
"etc/multivm_bootloaders/vm_1_5_0_small_memory/fee_estimate.yul/fee_estimate.yul.zbin",
);
BaseSystemContracts::load_with_bootloader(bootloader_bytecode)
}

pub fn estimate_gas_post_1_5_0_increased_memory() -> Self {
let bootloader_bytecode = read_zbin_bytecode(
"etc/multivm_bootloaders/vm_1_5_0/fee_estimate.yul/fee_estimate.yul.zbin",
"etc/multivm_bootloaders/vm_1_5_0_increased_memory/fee_estimate.yul/fee_estimate.yul.zbin",
);
BaseSystemContracts::load_with_bootloader(bootloader_bytecode)
}
Expand Down
9 changes: 7 additions & 2 deletions core/lib/multivm/src/tracers/validator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use zksync_system_constants::{
};
use zksync_types::{
vm_trace::ViolatedValidationRule, web3::signing::keccak256, AccountTreeId, Address, StorageKey,
H256, U256,
VmVersion, H256, U256,
};
use zksync_utils::{be_bytes_to_safe_address, u256_to_account_address, u256_to_h256};

Expand Down Expand Up @@ -42,14 +42,18 @@ pub struct ValidationTracer<H> {
trusted_address_slots: HashSet<(Address, U256)>,
computational_gas_used: u32,
computational_gas_limit: u32,
vm_version: VmVersion,
pub result: Arc<OnceCell<ViolatedValidationRule>>,
_marker: PhantomData<fn(H) -> H>,
}

type ValidationRoundResult = Result<NewTrustedValidationItems, ViolatedValidationRule>;

impl<H> ValidationTracer<H> {
pub fn new(params: ValidationTracerParams) -> (Self, Arc<OnceCell<ViolatedValidationRule>>) {
pub fn new(
params: ValidationTracerParams,
vm_version: VmVersion,
) -> (Self, Arc<OnceCell<ViolatedValidationRule>>) {
let result = Arc::new(OnceCell::new());
(
Self {
Expand All @@ -64,6 +68,7 @@ impl<H> ValidationTracer<H> {
trusted_address_slots: params.trusted_address_slots,
computational_gas_used: 0,
computational_gas_limit: params.computational_gas_limit,
vm_version,
result: result.clone(),
_marker: Default::default(),
},
Expand Down
2 changes: 1 addition & 1 deletion core/lib/multivm/src/tracers/validator/vm_latest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl<S: WriteStorage, H: HistoryMode> DynTracer<S, SimpleMemory<H::Vm1_5_0>>
self.process_validation_round_result(validation_round_result);
}

let hook = VmHook::from_opcode_memory(&state, &data);
let hook = VmHook::from_opcode_memory(&state, &data, self.vm_version.try_into().unwrap());
let current_mode = self.validation_mode;
match (current_mode, hook) {
(ValidationTracerMode::NoValidation, VmHook::AccountValidationEntered) => {
Expand Down
72 changes: 57 additions & 15 deletions core/lib/multivm/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ pub fn derive_base_fee_and_gas_per_pubdata(
VmVersion::Vm1_4_2 => crate::vm_1_4_2::utils::fee::derive_base_fee_and_gas_per_pubdata(
batch_fee_input.into_pubdata_independent(),
),
VmVersion::Vm1_5_0 => crate::vm_latest::utils::fee::derive_base_fee_and_gas_per_pubdata(
batch_fee_input.into_pubdata_independent(),
),
VmVersion::Vm1_5_0SmallBootloaderMemory | VmVersion::Vm1_5_0IncreasedBootloaderMemory => {
crate::vm_latest::utils::fee::derive_base_fee_and_gas_per_pubdata(
batch_fee_input.into_pubdata_independent(),
)
}
}
}

Expand All @@ -73,7 +75,9 @@ pub fn get_batch_base_fee(l1_batch_env: &L1BatchEnv, vm_version: VmVersion) -> u
}
VmVersion::Vm1_4_1 => crate::vm_1_4_1::utils::fee::get_batch_base_fee(l1_batch_env),
VmVersion::Vm1_4_2 => crate::vm_1_4_2::utils::fee::get_batch_base_fee(l1_batch_env),
VmVersion::Vm1_5_0 => crate::vm_latest::utils::fee::get_batch_base_fee(l1_batch_env),
VmVersion::Vm1_5_0SmallBootloaderMemory | VmVersion::Vm1_5_0IncreasedBootloaderMemory => {
crate::vm_latest::utils::fee::get_batch_base_fee(l1_batch_env)
}
}
}

Expand Down Expand Up @@ -199,7 +203,9 @@ pub fn derive_overhead(
}
VmVersion::Vm1_4_1 => crate::vm_1_4_1::utils::overhead::derive_overhead(encoded_len),
VmVersion::Vm1_4_2 => crate::vm_1_4_2::utils::overhead::derive_overhead(encoded_len),
VmVersion::Vm1_5_0 => crate::vm_latest::utils::overhead::derive_overhead(encoded_len),
VmVersion::Vm1_5_0SmallBootloaderMemory | VmVersion::Vm1_5_0IncreasedBootloaderMemory => {
crate::vm_latest::utils::overhead::derive_overhead(encoded_len)
}
}
}

Expand All @@ -223,7 +229,16 @@ pub fn get_bootloader_encoding_space(version: VmVersion) -> u32 {
}
VmVersion::Vm1_4_1 => crate::vm_1_4_1::constants::BOOTLOADER_TX_ENCODING_SPACE,
VmVersion::Vm1_4_2 => crate::vm_1_4_2::constants::BOOTLOADER_TX_ENCODING_SPACE,
VmVersion::Vm1_5_0 => crate::vm_latest::constants::BOOTLOADER_TX_ENCODING_SPACE,
VmVersion::Vm1_5_0SmallBootloaderMemory => {
crate::vm_latest::constants::get_bootloader_tx_encoding_space(
crate::vm_latest::MultiVMSubversion::SmallBootloaderMemory,
)
}
VmVersion::Vm1_5_0IncreasedBootloaderMemory => {
crate::vm_latest::constants::get_bootloader_tx_encoding_space(
crate::vm_latest::MultiVMSubversion::IncreasedBootloaderMemory,
)
}
}
}

Expand All @@ -243,7 +258,9 @@ pub fn get_bootloader_max_txs_in_batch(version: VmVersion) -> usize {
VmVersion::VmBoojumIntegration => crate::vm_boojum_integration::constants::MAX_TXS_IN_BLOCK,
VmVersion::Vm1_4_1 => crate::vm_1_4_1::constants::MAX_TXS_IN_BATCH,
VmVersion::Vm1_4_2 => crate::vm_1_4_2::constants::MAX_TXS_IN_BATCH,
VmVersion::Vm1_5_0 => crate::vm_latest::constants::MAX_TXS_IN_BATCH,
VmVersion::Vm1_5_0SmallBootloaderMemory | VmVersion::Vm1_5_0IncreasedBootloaderMemory => {
crate::vm_latest::constants::MAX_TXS_IN_BATCH
}
}
}

Expand All @@ -264,7 +281,9 @@ pub fn gas_bootloader_batch_tip_overhead(version: VmVersion) -> u32 {
}
VmVersion::Vm1_4_1 => crate::vm_1_4_1::constants::BOOTLOADER_BATCH_TIP_OVERHEAD,
VmVersion::Vm1_4_2 => crate::vm_1_4_2::constants::BOOTLOADER_BATCH_TIP_OVERHEAD,
VmVersion::Vm1_5_0 => crate::vm_latest::constants::BOOTLOADER_BATCH_TIP_OVERHEAD,
VmVersion::Vm1_5_0SmallBootloaderMemory | VmVersion::Vm1_5_0IncreasedBootloaderMemory => {
crate::vm_latest::constants::BOOTLOADER_BATCH_TIP_OVERHEAD
}
}
}

Expand All @@ -285,7 +304,7 @@ pub fn circuit_statistics_bootloader_batch_tip_overhead(version: VmVersion) -> u
VmVersion::Vm1_4_2 => {
crate::vm_1_4_2::constants::BOOTLOADER_BATCH_TIP_CIRCUIT_STATISTICS_OVERHEAD as usize
}
VmVersion::Vm1_5_0 => {
VmVersion::Vm1_5_0SmallBootloaderMemory | VmVersion::Vm1_5_0IncreasedBootloaderMemory => {
crate::vm_latest::constants::BOOTLOADER_BATCH_TIP_CIRCUIT_STATISTICS_OVERHEAD as usize
}
}
Expand All @@ -308,7 +327,7 @@ pub fn execution_metrics_bootloader_batch_tip_overhead(version: VmVersion) -> us
VmVersion::Vm1_4_2 => {
crate::vm_1_4_2::constants::BOOTLOADER_BATCH_TIP_METRICS_SIZE_OVERHEAD as usize
}
VmVersion::Vm1_5_0 => {
VmVersion::Vm1_5_0SmallBootloaderMemory | VmVersion::Vm1_5_0IncreasedBootloaderMemory => {
crate::vm_latest::constants::BOOTLOADER_BATCH_TIP_METRICS_SIZE_OVERHEAD as usize
}
}
Expand All @@ -332,7 +351,9 @@ pub fn get_max_gas_per_pubdata_byte(version: VmVersion) -> u64 {
}
VmVersion::Vm1_4_1 => crate::vm_1_4_1::constants::MAX_GAS_PER_PUBDATA_BYTE,
VmVersion::Vm1_4_2 => crate::vm_1_4_2::constants::MAX_GAS_PER_PUBDATA_BYTE,
VmVersion::Vm1_5_0 => crate::vm_latest::constants::MAX_GAS_PER_PUBDATA_BYTE,
VmVersion::Vm1_5_0SmallBootloaderMemory | VmVersion::Vm1_5_0IncreasedBootloaderMemory => {
crate::vm_latest::constants::MAX_GAS_PER_PUBDATA_BYTE
}
}
}

Expand All @@ -356,7 +377,16 @@ pub fn get_used_bootloader_memory_bytes(version: VmVersion) -> usize {
}
VmVersion::Vm1_4_1 => crate::vm_1_4_1::constants::USED_BOOTLOADER_MEMORY_BYTES,
VmVersion::Vm1_4_2 => crate::vm_1_4_2::constants::USED_BOOTLOADER_MEMORY_BYTES,
VmVersion::Vm1_5_0 => crate::vm_latest::constants::USED_BOOTLOADER_MEMORY_BYTES,
VmVersion::Vm1_5_0SmallBootloaderMemory => {
crate::vm_latest::constants::get_used_bootloader_memory_bytes(
crate::vm_latest::MultiVMSubversion::SmallBootloaderMemory,
)
}
VmVersion::Vm1_5_0IncreasedBootloaderMemory => {
crate::vm_latest::constants::get_used_bootloader_memory_bytes(
crate::vm_latest::MultiVMSubversion::IncreasedBootloaderMemory,
)
}
}
}

Expand All @@ -380,7 +410,16 @@ pub fn get_used_bootloader_memory_words(version: VmVersion) -> usize {
}
VmVersion::Vm1_4_1 => crate::vm_1_4_1::constants::USED_BOOTLOADER_MEMORY_WORDS,
VmVersion::Vm1_4_2 => crate::vm_1_4_2::constants::USED_BOOTLOADER_MEMORY_WORDS,
VmVersion::Vm1_5_0 => crate::vm_latest::constants::USED_BOOTLOADER_MEMORY_WORDS,
VmVersion::Vm1_5_0SmallBootloaderMemory => {
crate::vm_latest::constants::get_used_bootloader_memory_bytes(
crate::vm_latest::MultiVMSubversion::SmallBootloaderMemory,
)
}
VmVersion::Vm1_5_0IncreasedBootloaderMemory => {
crate::vm_latest::constants::get_used_bootloader_memory_bytes(
crate::vm_latest::MultiVMSubversion::IncreasedBootloaderMemory,
)
}
}
}

Expand All @@ -402,7 +441,8 @@ pub fn get_max_batch_gas_limit(version: VmVersion) -> u64 {
}
VmVersion::Vm1_4_1 => crate::vm_1_4_1::constants::BLOCK_GAS_LIMIT as u64,
VmVersion::Vm1_4_2 => crate::vm_1_4_2::constants::BLOCK_GAS_LIMIT as u64,
VmVersion::Vm1_5_0 => crate::vm_latest::constants::BATCH_GAS_LIMIT,
VmVersion::Vm1_5_0SmallBootloaderMemory => crate::vm_latest::constants::BATCH_GAS_LIMIT,
VmVersion::Vm1_5_0IncreasedBootloaderMemory => crate::vm_latest::constants::BATCH_GAS_LIMIT,
}
}

Expand All @@ -423,6 +463,8 @@ pub fn get_max_batch_base_layer_circuits(version: VmVersion) -> usize {
// We avoid providing `0` for the old versions to avoid potential errors when working with old versions.
crate::vm_1_4_2::constants::MAX_BASE_LAYER_CIRCUITS
}
VmVersion::Vm1_5_0 => crate::vm_latest::constants::MAX_BASE_LAYER_CIRCUITS,
VmVersion::Vm1_5_0SmallBootloaderMemory | VmVersion::Vm1_5_0IncreasedBootloaderMemory => {
crate::vm_latest::constants::MAX_BASE_LAYER_CIRCUITS
}
}
}
40 changes: 31 additions & 9 deletions core/lib/multivm/src/versions/vm_latest/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub use zk_evm_1_5_0::zkevm_opcode_defs::system_params::{
};
use zksync_system_constants::{MAX_L2_TX_GAS_LIMIT, MAX_NEW_FACTORY_DEPS};

use super::vm::MultiVMSubversion;
use crate::vm_latest::old_vm::utils::heap_page_from_base;

/// The amount of ergs to be reserved at the end of the batch to ensure that it has enough ergs to verify compression, etc.
Expand All @@ -19,9 +20,18 @@ pub(crate) const MAX_BASE_LAYER_CIRCUITS: usize = 34100;
/// The size of the bootloader memory in bytes which is used by the protocol.
/// While the maximal possible size is a lot higher, we restrict ourselves to a certain limit to reduce
/// the requirements on RAM.
/// In this version of the VM the used bootloader memory bytes has increased from `30_000_000` to `59_000_000`.
pub(crate) const USED_BOOTLOADER_MEMORY_BYTES: usize = 59_000_000;
pub(crate) const USED_BOOTLOADER_MEMORY_WORDS: usize = USED_BOOTLOADER_MEMORY_BYTES / 32;
/// In this version of the VM the used bootloader memory bytes has increased from `30_000_000` to `59_000_000`,
/// and then to `63_800_000` in a subsequent upgrade.
pub(crate) const fn get_used_bootloader_memory_bytes(subversion: MultiVMSubversion) -> usize {
match subversion {
MultiVMSubversion::SmallBootloaderMemory => 59_000_000,
MultiVMSubversion::IncreasedBootloaderMemory => 63_800_000,
}
}

pub(crate) const fn get_used_bootloader_memory_words(subversion: MultiVMSubversion) -> usize {
get_used_bootloader_memory_bytes(subversion) / 32
}

/// We want `MAX_GAS_PER_PUBDATA_BYTE` multiplied by the u32::MAX (i.e. the maximal possible value of the pubdata counter)
/// to be a safe integer with a good enough margin.
Expand Down Expand Up @@ -94,8 +104,9 @@ pub(crate) const BOOTLOADER_TX_DESCRIPTION_OFFSET: usize =
OPERATOR_PROVIDED_L1_MESSENGER_PUBDATA_OFFSET + OPERATOR_PROVIDED_L1_MESSENGER_PUBDATA_SLOTS;

/// The size of the bootloader memory dedicated to the encodings of transactions
pub(crate) const BOOTLOADER_TX_ENCODING_SPACE: u32 =
(USED_BOOTLOADER_MEMORY_WORDS - TX_DESCRIPTION_OFFSET - MAX_TXS_IN_BATCH) as u32;
pub(crate) const fn get_bootloader_tx_encoding_space(subversion: MultiVMSubversion) -> u32 {
(get_used_bootloader_memory_words(subversion) - TX_DESCRIPTION_OFFSET - MAX_TXS_IN_BATCH) as u32
}

// Size of the bootloader tx description in words
pub(crate) const BOOTLOADER_TX_DESCRIPTION_SIZE: usize = 2;
Expand All @@ -116,13 +127,24 @@ pub const BOOTLOADER_HEAP_PAGE: u32 = heap_page_from_base(MemoryPage(INITIAL_BAS
/// and VM_HOOKS_PARAMS_COUNT parameters (each 32 bytes) are put in the slots before.
/// So the layout looks like this:
/// `[param 0][param 1][param 2][vmhook opcode]`
pub const VM_HOOK_POSITION: u32 = RESULT_SUCCESS_FIRST_SLOT - 1;
pub const VM_HOOK_PARAMS_COUNT: u32 = 3;
pub const VM_HOOK_PARAMS_START_POSITION: u32 = VM_HOOK_POSITION - VM_HOOK_PARAMS_COUNT;
pub(crate) const fn get_vm_hook_position(subversion: MultiVMSubversion) -> u32 {
get_result_success_first_slot(subversion) - 1
}
pub(crate) const fn get_vm_hook_params_start_position(subversion: MultiVMSubversion) -> u32 {
get_vm_hook_position(subversion) - VM_HOOK_PARAMS_COUNT
}

/// Method that provides the start position of the vm hook in the memory for the latest version of v1.5.0.
/// This method is used only in `test_infra` in the bootloader tests and that's why it should be exposed.
pub const fn get_vm_hook_start_position_latest() -> u32 {
get_vm_hook_params_start_position(MultiVMSubversion::IncreasedBootloaderMemory)
}

/// Arbitrary space in memory closer to the end of the page
pub const RESULT_SUCCESS_FIRST_SLOT: u32 =
((USED_BOOTLOADER_MEMORY_BYTES as u32) - (MAX_TXS_IN_BATCH as u32) * 32) / 32;
pub(crate) const fn get_result_success_first_slot(subversion: MultiVMSubversion) -> u32 {
((get_used_bootloader_memory_bytes(subversion) as u32) - (MAX_TXS_IN_BATCH as u32) * 32) / 32
}

/// How many gas bootloader is allowed to spend within one block.
/// Note that this value doesn't correspond to the gas limit of any particular transaction
Expand Down
Loading
Loading