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): add gateway changes to fast vm #3236

Merged
merged 5 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
36 changes: 29 additions & 7 deletions core/lib/multivm/src/versions/vm_fast/bootloader_state/state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::cmp::Ordering;

use once_cell::sync::OnceCell;
use zksync_types::{L2ChainId, U256};
use zksync_types::{L2ChainId, ProtocolVersionId, U256};

use super::{
l2_block::BootloaderL2Block,
Expand All @@ -10,8 +10,11 @@ use super::{
BootloaderStateSnapshot,
};
use crate::{
interface::{BootloaderMemory, CompressedBytecodeInfo, L2BlockEnv, TxExecutionMode},
versions::vm_fast::{pubdata::PubdataInput, transaction_data::TransactionData},
interface::{
pubdata::{PubdataBuilder, PubdataInput},
BootloaderMemory, CompressedBytecodeInfo, L2BlockEnv, TxExecutionMode,
},
versions::vm_fast::transaction_data::TransactionData,
vm_latest::{constants::TX_DESCRIPTION_OFFSET, utils::l2_blocks::assert_next_block},
};

Expand Down Expand Up @@ -42,13 +45,16 @@ pub struct BootloaderState {
free_tx_offset: usize,
/// Information about the pubdata that will be needed to supply to the L1Messenger
pubdata_information: OnceCell<PubdataInput>,
/// Protocol version.
protocol_version: ProtocolVersionId,
}

impl BootloaderState {
pub(crate) fn new(
execution_mode: TxExecutionMode,
initial_memory: BootloaderMemory,
first_l2_block: L2BlockEnv,
protocol_version: ProtocolVersionId,
) -> Self {
let l2_block = BootloaderL2Block::new(first_l2_block, 0);
Self {
Expand All @@ -59,6 +65,7 @@ impl BootloaderState {
execution_mode,
free_tx_offset: 0,
pubdata_information: Default::default(),
protocol_version,
}
}

Expand Down Expand Up @@ -139,12 +146,23 @@ impl BootloaderState {
.expect("Pubdata information is not set")
}

pub(crate) fn settlement_layer_pubdata(&self, pubdata_builder: &dyn PubdataBuilder) -> Vec<u8> {
let pubdata_information = self
.pubdata_information
.get()
.expect("Pubdata information is not set");
pubdata_builder.settlement_layer_pubdata(pubdata_information, self.protocol_version)
}

fn last_mut_l2_block(&mut self) -> &mut BootloaderL2Block {
self.l2_blocks.last_mut().unwrap()
}

/// Apply all bootloader transaction to the initial memory
pub(crate) fn bootloader_memory(&self) -> BootloaderMemory {
pub(crate) fn bootloader_memory(
&self,
pubdata_builder: &dyn PubdataBuilder,
) -> BootloaderMemory {
let mut initial_memory = self.initial_memory.clone();
let mut offset = 0;
let mut compressed_bytecodes_offset = 0;
Expand Down Expand Up @@ -172,11 +190,15 @@ impl BootloaderState {

let pubdata_information = self
.pubdata_information
.clone()
.into_inner()
.get()
.expect("Empty pubdata information");

apply_pubdata_to_memory(&mut initial_memory, pubdata_information);
apply_pubdata_to_memory(
&mut initial_memory,
pubdata_builder,
pubdata_information,
self.protocol_version,
);
initial_memory
}

Expand Down
70 changes: 50 additions & 20 deletions core/lib/multivm/src/versions/vm_fast/bootloader_state/utils.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use zksync_types::{ethabi, h256_to_u256, U256};
use zksync_types::{ethabi, h256_to_u256, ProtocolVersionId, U256};

use super::{l2_block::BootloaderL2Block, tx::BootloaderTx};
use crate::{
interface::{BootloaderMemory, CompressedBytecodeInfo, TxExecutionMode},
interface::{
pubdata::{PubdataBuilder, PubdataInput},
BootloaderMemory, CompressedBytecodeInfo, TxExecutionMode,
},
utils::bytecode,
versions::vm_fast::pubdata::PubdataInput,
vm_latest::constants::{
BOOTLOADER_TX_DESCRIPTION_OFFSET, BOOTLOADER_TX_DESCRIPTION_SIZE,
COMPRESSED_BYTECODES_OFFSET, OPERATOR_PROVIDED_L1_MESSENGER_PUBDATA_OFFSET,
Expand Down Expand Up @@ -118,26 +120,54 @@ fn apply_l2_block_inner(
])
}

fn bootloader_memory_input(
pubdata_builder: &dyn PubdataBuilder,
input: &PubdataInput,
protocol_version: ProtocolVersionId,
) -> Vec<u8> {
let l2_da_validator_address = pubdata_builder.l2_da_validator();
let operator_input = pubdata_builder.l1_messenger_operator_input(input, protocol_version);
ethabi::encode(&[
ethabi::Token::Address(l2_da_validator_address),
ethabi::Token::Bytes(operator_input),
])
}

pub(crate) fn apply_pubdata_to_memory(
memory: &mut BootloaderMemory,
pubdata_information: PubdataInput,
pubdata_builder: &dyn PubdataBuilder,
pubdata_information: &PubdataInput,
protocol_version: ProtocolVersionId,
) {
// Skipping two slots as they will be filled by the bootloader itself:
// - One slot is for the selector of the call to the L1Messenger.
// - The other slot is for the 0x20 offset for the calldata.
let l1_messenger_pubdata_start_slot = OPERATOR_PROVIDED_L1_MESSENGER_PUBDATA_OFFSET + 2;

// Need to skip first word as it represents array offset
// while bootloader expects only [len || data]
let pubdata = ethabi::encode(&[ethabi::Token::Bytes(
pubdata_information.build_pubdata(true),
)])[32..]
.to_vec();

assert!(
pubdata.len() / 32 <= OPERATOR_PROVIDED_L1_MESSENGER_PUBDATA_SLOTS - 2,
"The encoded pubdata is too big"
);
let (l1_messenger_pubdata_start_slot, pubdata) = if protocol_version.is_pre_gateway() {
// Skipping two slots as they will be filled by the bootloader itself:
// - One slot is for the selector of the call to the L1Messenger.
// - The other slot is for the 0x20 offset for the calldata.
let l1_messenger_pubdata_start_slot = OPERATOR_PROVIDED_L1_MESSENGER_PUBDATA_OFFSET + 2;
// Need to skip first word as it represents array offset
// while bootloader expects only [len || data]
let pubdata = ethabi::encode(&[ethabi::Token::Bytes(
pubdata_builder.l1_messenger_operator_input(pubdata_information, protocol_version),
)])[32..]
.to_vec();
assert!(
pubdata.len() / 32 <= OPERATOR_PROVIDED_L1_MESSENGER_PUBDATA_SLOTS - 2,
"The encoded pubdata is too big"
);
(l1_messenger_pubdata_start_slot, pubdata)
} else {
// Skipping the first slot as it will be filled by the bootloader itself:
// It is for the selector of the call to the L1Messenger.
let l1_messenger_pubdata_start_slot = OPERATOR_PROVIDED_L1_MESSENGER_PUBDATA_OFFSET + 1;
let pubdata =
bootloader_memory_input(pubdata_builder, pubdata_information, protocol_version);
assert!(
// Note that unlike the previous version, the difference is `1`, since now it also includes the offset
pubdata.len() / 32 < OPERATOR_PROVIDED_L1_MESSENGER_PUBDATA_SLOTS,
"The encoded pubdata is too big"
);
(l1_messenger_pubdata_start_slot, pubdata)
};

pubdata
.chunks(32)
Expand Down
3 changes: 2 additions & 1 deletion core/lib/multivm/src/versions/vm_fast/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub use zksync_vm2::interface;

pub(crate) use self::version::FastVMVersion;
pub use self::vm::Vm;

mod bootloader_state;
Expand All @@ -10,10 +11,10 @@ mod evm_deploy_tracer;
mod glue;
mod hook;
mod initial_bootloader_memory;
mod pubdata;
mod refund;
#[cfg(test)]
mod tests;
mod transaction_data;
mod utils;
mod version;
mod vm;
123 changes: 0 additions & 123 deletions core/lib/multivm/src/versions/vm_fast/pubdata.rs

This file was deleted.

2 changes: 1 addition & 1 deletion core/lib/multivm/src/versions/vm_fast/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl TestedVm for Vm<ImmutableStorageView<InMemoryStorage>> {
}

fn finish_batch_without_pubdata(&mut self) -> VmExecutionResultAndLogs {
self.inspect_inner(&mut Default::default(), VmExecutionMode::Batch)
self.inspect_inner(&mut Default::default(), VmExecutionMode::Batch, None)
}

fn insert_bytecodes(&mut self, bytecodes: &[&[u8]]) {
Expand Down
28 changes: 28 additions & 0 deletions core/lib/multivm/src/versions/vm_fast/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use crate::{vm_latest::MultiVMSubversion, VmVersion};

#[derive(Debug, Copy, Clone)]
pub(crate) enum FastVMVersion {
perekopskiy marked this conversation as resolved.
Show resolved Hide resolved
IncreasedBootloaderMemory,
Gateway,
}

impl From<FastVMVersion> for MultiVMSubversion {
fn from(value: FastVMVersion) -> Self {
match value {
FastVMVersion::IncreasedBootloaderMemory => Self::IncreasedBootloaderMemory,
FastVMVersion::Gateway => Self::Gateway,
}
}
}

impl TryFrom<VmVersion> for FastVMVersion {
type Error = ();

fn try_from(value: VmVersion) -> Result<Self, Self::Error> {
match value {
VmVersion::Vm1_5_0IncreasedBootloaderMemory => Ok(Self::IncreasedBootloaderMemory),
VmVersion::VmGateway => Ok(Self::Gateway),
_ => Err(()),
}
}
}
Loading
Loading