Skip to content

Commit

Permalink
feat(vm)!: Update Refund model (#181)
Browse files Browse the repository at this point in the history
# What ❔

<!-- What are the changes this PR brings about? -->
<!-- Example: This PR adds a PR template to the repo. -->
<!-- (For bigger PRs adding more context is appreciated) -->

## Why ❔

<!-- Why are these changes done? What goal do they contribute to? What
are the principles behind them? -->
<!-- Example: PR templates ensure PR reviewers, observers, and future
iterators are in context about the evolution of repos. -->

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [ ] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [ ] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [ ] Code has been formatted via `zk fmt` and `zk lint`.

Signed-off-by: Danil <deniallugo@gmail.com>
  • Loading branch information
Deniallugo authored Oct 9, 2023
1 parent fe2d6ad commit 92b6f59
Show file tree
Hide file tree
Showing 137 changed files with 11,929 additions and 166 deletions.
24 changes: 24 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions core/lib/multivm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ categories = ["cryptography"]
vm_m5 = { path = "../../multivm_deps/vm_m5" }
vm_m6 = { path = "../../multivm_deps/vm_m6" }
vm_1_3_2 = { path = "../../multivm_deps/vm_1_3_2" }
vm_virtual_blocks= { path = "../../multivm_deps/vm_virtual_blocks" }
vm_latest = { path = "../vm", package = "vm" }

zksync_types = { path = "../types" }
Expand Down
4 changes: 2 additions & 2 deletions core/lib/multivm/src/glue/block_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ impl BlockProperties {
};
Self::Vm1_3_2(inner)
}
VmVersion::VmVirtualBlocks => {
VmVersion::VmVirtualBlocks | VmVersion::VmVirtualBlocksRefundsEnhancement => {
unreachable!(
"Vm with virtual blocks has another initialization logic, \
"From VmVirtualBlocks we have another initialization logic, \
so it's not required to have BlockProperties for it"
)
}
Expand Down
22 changes: 19 additions & 3 deletions core/lib/multivm/src/glue/history_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ pub trait HistoryMode:
+ GlueInto<Self::VmM6Mode>
+ GlueInto<Self::Vm1_3_2Mode>
+ GlueInto<Self::VmVirtualBlocksMode>
+ GlueInto<Self::VmVirtualBlocksRefundsEnhancement>
{
type VmM6Mode: vm_m6::HistoryMode;
type Vm1_3_2Mode: vm_1_3_2::HistoryMode;
type VmVirtualBlocksMode: vm_latest::HistoryMode;
type VmVirtualBlocksMode: vm_virtual_blocks::HistoryMode;
type VmVirtualBlocksRefundsEnhancement: vm_latest::HistoryMode;
}

impl GlueFrom<vm_latest::HistoryEnabled> for vm_m6::HistoryEnabled {
Expand All @@ -23,6 +25,12 @@ impl GlueFrom<vm_latest::HistoryEnabled> for vm_1_3_2::HistoryEnabled {
}
}

impl GlueFrom<vm_latest::HistoryEnabled> for vm_virtual_blocks::HistoryEnabled {
fn glue_from(_: vm_latest::HistoryEnabled) -> Self {
Self
}
}

impl GlueFrom<vm_latest::HistoryDisabled> for vm_m6::HistoryDisabled {
fn glue_from(_: vm_latest::HistoryDisabled) -> Self {
Self
Expand All @@ -35,14 +43,22 @@ impl GlueFrom<vm_latest::HistoryDisabled> for vm_1_3_2::HistoryDisabled {
}
}

impl GlueFrom<vm_latest::HistoryDisabled> for vm_virtual_blocks::HistoryDisabled {
fn glue_from(_: vm_latest::HistoryDisabled) -> Self {
Self
}
}

impl HistoryMode for vm_latest::HistoryEnabled {
type VmM6Mode = vm_m6::HistoryEnabled;
type Vm1_3_2Mode = vm_1_3_2::HistoryEnabled;
type VmVirtualBlocksMode = vm_latest::HistoryEnabled;
type VmVirtualBlocksMode = vm_virtual_blocks::HistoryEnabled;
type VmVirtualBlocksRefundsEnhancement = vm_latest::HistoryEnabled;
}

impl HistoryMode for vm_latest::HistoryDisabled {
type VmM6Mode = vm_m6::HistoryDisabled;
type Vm1_3_2Mode = vm_1_3_2::HistoryDisabled;
type VmVirtualBlocksMode = vm_latest::HistoryDisabled;
type VmVirtualBlocksMode = vm_virtual_blocks::HistoryDisabled;
type VmVirtualBlocksRefundsEnhancement = vm_latest::HistoryDisabled;
}
18 changes: 16 additions & 2 deletions core/lib/multivm/src/glue/init_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ impl<'a, S: ReadStorage, H: HistoryMode> VmInstance<'a, S, H> {
}
}
VmInstanceData::VmVirtualBlocks(data) => {
let vm = vm_latest::Vm::new(
let vm = vm_virtual_blocks::Vm::new(
l1_batch_env.glue_into(),
system_env.clone(),
system_env.clone().glue_into(),
data.storage_view.clone(),
H::VmVirtualBlocksMode::default(),
);
Expand All @@ -82,6 +82,20 @@ impl<'a, S: ReadStorage, H: HistoryMode> VmInstance<'a, S, H> {
last_tx_compressed_bytecodes: vec![],
}
}
VmInstanceData::VmVirtualBlocksRefundsEnhancement(data) => {
let vm = vm_latest::Vm::new(
l1_batch_env.glue_into(),
system_env.clone(),
data.storage_view.clone(),
H::VmVirtualBlocksRefundsEnhancement::default(),
);
let vm = VmInstanceVersion::VmVirtualBlocksRefundsEnhancement(Box::new(vm));
Self {
vm,
system_env,
last_tx_compressed_bytecodes: vec![],
}
}
}
}
}
9 changes: 4 additions & 5 deletions core/lib/multivm/src/glue/oracle_tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ where
let oracle_tools = vm_m6::OracleTools::new(state, history.glue_into());
OracleTools::M6(oracle_tools)
}
VmVersion::Vm1_3_2 => {
panic!("oracle tools for vm1.3.2 do not exist")
}
VmVersion::VmVirtualBlocks => {
panic!("oracle tools for VmVirtualBlocks do not exist")
VmVersion::VmVirtualBlocks
| VmVersion::VmVirtualBlocksRefundsEnhancement
| VmVersion::Vm1_3_2 => {
panic!("oracle tools for after VM1.3.2 do not exist")
}
}
}
Expand Down
36 changes: 26 additions & 10 deletions core/lib/multivm/src/glue/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
//! into a form compatible with the latest VM version.
//! It defines a method `latest` for obtaining a boxed tracer.
//!
//! - `IntoVmVirtualBlocksTracer<S, H>`:This trait is responsible for converting a tracer
//! into a form compatible with the vm_virtual_blocks version.
//! It defines a method `vm_virtual_blocks` for obtaining a boxed tracer.
//!
//! For `MultivmTracer` to be implemented, Tracer must implement all N currently
//! existing sub-traits.
//!
Expand All @@ -30,11 +34,15 @@
//! - Create a new trait performing conversion to the specified VM tracer, e.g. `Into<VmVersion>Tracer`.
//! - Provide implementations of this trait for all the structures that currently implement `MultivmTracer`.
//! - Add this trait as a trait bound to the `MultivmTracer`.
//! - Add this trait as a trait bound for `T` in `MultivmTracer` implementation.
//! - Integrate the newly added method to the MultiVM itself (e.g. add required tracer conversions where applicable).
mod implementations;

use crate::HistoryMode;
use zksync_state::WriteStorage;

pub trait MultivmTracer<S: WriteStorage, H: vm_latest::HistoryMode>:
IntoLatestTracer<S, H>
pub trait MultivmTracer<S: WriteStorage, H: HistoryMode>:
IntoLatestTracer<S, H> + IntoVmVirtualBlocksTracer<S, H>
{
fn into_boxed(self) -> Box<dyn MultivmTracer<S, H>>
where
Expand All @@ -44,25 +52,33 @@ pub trait MultivmTracer<S: WriteStorage, H: vm_latest::HistoryMode>:
}
}

pub trait IntoLatestTracer<S: WriteStorage, H: vm_latest::HistoryMode> {
fn latest(&self) -> Box<dyn vm_latest::VmTracer<S, H>>;
pub trait IntoLatestTracer<S: WriteStorage, H: HistoryMode> {
fn latest(&self) -> Box<dyn vm_latest::VmTracer<S, H::VmVirtualBlocksRefundsEnhancement>>;
}

pub trait IntoVmVirtualBlocksTracer<S: WriteStorage, H: HistoryMode> {
fn vm_virtual_blocks(&self) -> Box<dyn vm_virtual_blocks::VmTracer<S, H::VmVirtualBlocksMode>>;
}

impl<S, H, T> IntoLatestTracer<S, H> for T
impl<S, T, H> IntoLatestTracer<S, H> for T
where
S: WriteStorage,
H: vm_latest::HistoryMode,
T: vm_latest::VmTracer<S, H> + Clone + 'static,
H: HistoryMode,
T: vm_latest::VmTracer<S, H::VmVirtualBlocksRefundsEnhancement> + Clone + 'static,
{
fn latest(&self) -> Box<dyn vm_latest::VmTracer<S, H>> {
fn latest(&self) -> Box<dyn vm_latest::VmTracer<S, H::VmVirtualBlocksRefundsEnhancement>> {
Box::new(self.clone())
}
}

impl<S, H, T> MultivmTracer<S, H> for T
where
S: WriteStorage,
H: vm_latest::HistoryMode,
T: vm_latest::VmTracer<S, H> + Clone + 'static,
H: HistoryMode,
T: vm_latest::VmTracer<S, H::VmVirtualBlocksRefundsEnhancement>
+ IntoLatestTracer<S, H>
+ IntoVmVirtualBlocksTracer<S, H>
+ Clone
+ 'static,
{
}
48 changes: 48 additions & 0 deletions core/lib/multivm/src/glue/tracer/implementations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use crate::glue::tracer::IntoVmVirtualBlocksTracer;
use vm_latest::{CallTracer, StorageInvocations, ValidationTracer};
use zksync_state::WriteStorage;

impl<S, H> IntoVmVirtualBlocksTracer<S, H> for StorageInvocations
where
H: crate::HistoryMode,
S: WriteStorage,
{
fn vm_virtual_blocks(&self) -> Box<dyn vm_virtual_blocks::VmTracer<S, H::VmVirtualBlocksMode>> {
Box::new(vm_virtual_blocks::StorageInvocations::new(self.limit))
}
}

impl<S, H> IntoVmVirtualBlocksTracer<S, H> for CallTracer<H::VmVirtualBlocksRefundsEnhancement>
where
H: crate::HistoryMode + 'static,
S: WriteStorage,
{
fn vm_virtual_blocks(&self) -> Box<dyn vm_virtual_blocks::VmTracer<S, H::VmVirtualBlocksMode>> {
Box::new(vm_virtual_blocks::CallTracer::new(
self.result.clone(),
H::VmVirtualBlocksMode::default(),
))
}
}

impl<S, H> IntoVmVirtualBlocksTracer<S, H>
for ValidationTracer<H::VmVirtualBlocksRefundsEnhancement>
where
H: crate::HistoryMode + 'static,
S: WriteStorage,
{
fn vm_virtual_blocks(&self) -> Box<dyn vm_virtual_blocks::VmTracer<S, H::VmVirtualBlocksMode>> {
let params = self.params();
Box::new(vm_virtual_blocks::ValidationTracer::new(
vm_virtual_blocks::ValidationTracerParams {
user_address: params.user_address,
paymaster_address: params.paymaster_address,
trusted_slots: params.trusted_slots,
trusted_addresses: params.trusted_addresses,
trusted_address_slots: params.trusted_address_slots,
computational_gas_limit: params.computational_gas_limit,
},
self.result.clone(),
))
}
}
33 changes: 33 additions & 0 deletions core/lib/multivm/src/glue/types/vm/bytecompression_result.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::glue::{GlueFrom, GlueInto};
use vm_latest::{BytecodeCompressionError, VmExecutionResultAndLogs};

impl GlueFrom<vm_virtual_blocks::BytecodeCompressionError> for BytecodeCompressionError {
fn glue_from(value: vm_virtual_blocks::BytecodeCompressionError) -> Self {
match value {
vm_virtual_blocks::BytecodeCompressionError::BytecodeCompressionFailed => {
Self::BytecodeCompressionFailed
}
}
}
}

impl
GlueFrom<
Result<
vm_virtual_blocks::VmExecutionResultAndLogs,
vm_virtual_blocks::BytecodeCompressionError,
>,
> for Result<VmExecutionResultAndLogs, BytecodeCompressionError>
{
fn glue_from(
value: Result<
vm_virtual_blocks::VmExecutionResultAndLogs,
vm_virtual_blocks::BytecodeCompressionError,
>,
) -> Self {
match value {
Ok(result) => Ok(result.glue_into()),
Err(err) => Err(err.glue_into()),
}
}
}
15 changes: 15 additions & 0 deletions core/lib/multivm/src/glue/types/vm/current_execution_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use crate::glue::GlueFrom;
use vm_latest::CurrentExecutionState;

impl GlueFrom<vm_virtual_blocks::CurrentExecutionState> for CurrentExecutionState {
fn glue_from(value: vm_virtual_blocks::CurrentExecutionState) -> Self {
Self {
events: value.events,
storage_log_queries: value.storage_log_queries,
used_contract_hashes: value.used_contract_hashes,
l2_to_l1_logs: value.l2_to_l1_logs,
total_log_queries: value.total_log_queries,
cycles_used: value.cycles_used,
}
}
}
18 changes: 18 additions & 0 deletions core/lib/multivm/src/glue/types/vm/execution_result.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::glue::{GlueFrom, GlueInto};
use vm_latest::ExecutionResult;

impl GlueFrom<vm_virtual_blocks::ExecutionResult> for ExecutionResult {
fn glue_from(value: vm_virtual_blocks::ExecutionResult) -> Self {
match value {
vm_virtual_blocks::ExecutionResult::Success { output } => {
ExecutionResult::Success { output }
}
vm_virtual_blocks::ExecutionResult::Revert { output } => ExecutionResult::Revert {
output: output.glue_into(),
},
vm_virtual_blocks::ExecutionResult::Halt { reason } => ExecutionResult::Halt {
reason: reason.glue_into(),
},
}
}
}
44 changes: 44 additions & 0 deletions core/lib/multivm/src/glue/types/vm/halt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::glue::{GlueFrom, GlueInto};
use vm_latest::Halt;

impl GlueFrom<vm_virtual_blocks::Halt> for Halt {
fn glue_from(value: vm_virtual_blocks::Halt) -> Self {
match value {
vm_virtual_blocks::Halt::ValidationFailed(reason) => {
Self::ValidationFailed(reason.glue_into())
}
vm_virtual_blocks::Halt::PaymasterValidationFailed(reason) => {
Self::PaymasterValidationFailed(reason.glue_into())
}
vm_virtual_blocks::Halt::PrePaymasterPreparationFailed(reason) => {
Self::PrePaymasterPreparationFailed(reason.glue_into())
}
vm_virtual_blocks::Halt::PayForTxFailed(reason) => {
Self::PayForTxFailed(reason.glue_into())
}
vm_virtual_blocks::Halt::FailedToMarkFactoryDependencies(reason) => {
Self::FailedToMarkFactoryDependencies(reason.glue_into())
}
vm_virtual_blocks::Halt::FailedToChargeFee(reason) => {
Self::FailedToChargeFee(reason.glue_into())
}
vm_virtual_blocks::Halt::FromIsNotAnAccount => Self::FromIsNotAnAccount,
vm_virtual_blocks::Halt::InnerTxError => Self::InnerTxError,
vm_virtual_blocks::Halt::Unknown(reason) => Self::Unknown(reason.glue_into()),
vm_virtual_blocks::Halt::UnexpectedVMBehavior(reason) => {
Self::UnexpectedVMBehavior(reason)
}
vm_virtual_blocks::Halt::BootloaderOutOfGas => Self::BootloaderOutOfGas,
vm_virtual_blocks::Halt::TooBigGasLimit => Self::TooBigGasLimit,
vm_virtual_blocks::Halt::NotEnoughGasProvided => Self::NotEnoughGasProvided,
vm_virtual_blocks::Halt::MissingInvocationLimitReached => {
Self::MissingInvocationLimitReached
}
vm_virtual_blocks::Halt::FailedToSetL2Block(reason) => Self::FailedToSetL2Block(reason),
vm_virtual_blocks::Halt::FailedToAppendTransactionToL2Block(reason) => {
Self::FailedToAppendTransactionToL2Block(reason)
}
vm_virtual_blocks::Halt::VMPanic => Self::VMPanic,
}
}
}
Loading

0 comments on commit 92b6f59

Please sign in to comment.