Skip to content

Commit

Permalink
fix: bincode deserialization for VM run data (#3044)
Browse files Browse the repository at this point in the history
## What ❔

Implement custom deserialize for VM run data

## Why ❔

serde(deserialize_if) is not working with bincode

## 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_supervisor fmt` and `zk_supervisor
lint`.
  • Loading branch information
Artemka374 authored Oct 9, 2024
1 parent ebf9604 commit b0ec79f
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions core/lib/prover_interface/src/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{collections::HashMap, convert::TryInto, fmt::Debug};
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, Bytes};
use zksync_multivm::interface::{L1BatchEnv, SystemEnv};
use zksync_object_store::{serialize_using_bincode, Bucket, StoredObject};
use zksync_object_store::{_reexports::BoxedError, serialize_using_bincode, Bucket, StoredObject};
use zksync_types::{
basic_fri_types::Eip4844Blobs, block::L2BlockExecutionData,
witness_block_state::WitnessStorageState, L1BatchNumber, ProtocolVersionId, H256, U256,
Expand Down Expand Up @@ -151,6 +151,38 @@ pub struct VMRunWitnessInputData {
pub witness_block_state: WitnessStorageState,
}

// skip_serializing_if for field evm_emulator_code_hash doesn't work fine with bincode,
// so we are implementing custom deserialization for it
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VMRunWitnessInputDataLegacy {
pub l1_batch_number: L1BatchNumber,
pub used_bytecodes: HashMap<U256, Vec<[u8; 32]>>,
pub initial_heap_content: Vec<(usize, U256)>,
pub protocol_version: ProtocolVersionId,
pub bootloader_code: Vec<[u8; 32]>,
pub default_account_code_hash: U256,
pub storage_refunds: Vec<u32>,
pub pubdata_costs: Vec<i32>,
pub witness_block_state: WitnessStorageState,
}

impl From<VMRunWitnessInputDataLegacy> for VMRunWitnessInputData {
fn from(value: VMRunWitnessInputDataLegacy) -> Self {
Self {
l1_batch_number: value.l1_batch_number,
used_bytecodes: value.used_bytecodes,
initial_heap_content: value.initial_heap_content,
protocol_version: value.protocol_version,
bootloader_code: value.bootloader_code,
default_account_code_hash: value.default_account_code_hash,
evm_emulator_code_hash: None,
storage_refunds: value.storage_refunds,
pubdata_costs: value.pubdata_costs,
witness_block_state: value.witness_block_state,
}
}
}

impl StoredObject for VMRunWitnessInputData {
const BUCKET: Bucket = Bucket::WitnessInput;

Expand All @@ -160,7 +192,17 @@ impl StoredObject for VMRunWitnessInputData {
format!("vm_run_data_{key}.bin")
}

serialize_using_bincode!();
fn serialize(&self) -> Result<Vec<u8>, BoxedError> {
zksync_object_store::bincode::serialize(self).map_err(Into::into)
}

fn deserialize(bytes: Vec<u8>) -> Result<Self, BoxedError> {
zksync_object_store::bincode::deserialize::<VMRunWitnessInputData>(&bytes).or_else(|_| {
zksync_object_store::bincode::deserialize::<VMRunWitnessInputDataLegacy>(&bytes)
.map(Into::into)
.map_err(Into::into)
})
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down

0 comments on commit b0ec79f

Please sign in to comment.