Skip to content

Commit

Permalink
fix(types): Add LegacyMixedCall (#1773)
Browse files Browse the repository at this point in the history
## What ❔

Adds `LegacyMixedCall` struct

## Why ❔

Previous versions of the node saved such call traces, and they should be
deserializable

## 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`.
- [ ] Spellcheck has been run via `zk spellcheck`.
- [ ] Linkcheck has been run via `zk linkcheck`.
  • Loading branch information
perekopskiy authored Apr 23, 2024
1 parent 6144a91 commit 2b236fe
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 6 deletions.
13 changes: 9 additions & 4 deletions core/lib/dal/src/models/storage_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use zksync_types::{
l2::TransactionType,
protocol_upgrade::ProtocolUpgradeTxCommonData,
transaction_request::PaymasterParams,
vm_trace::{Call, LegacyCall},
vm_trace::{Call, LegacyCall, LegacyMixedCall},
web3::types::U64,
Address, Bytes, Execute, ExecuteTransactionCommon, L1TxCommonData, L2ChainId, L2TxCommonData,
Nonce, PackedEthSignature, PriorityOpId, ProtocolVersionId, Transaction, EIP_1559_TX_TYPE,
Expand Down Expand Up @@ -546,9 +546,14 @@ pub(crate) struct CallTrace {
impl CallTrace {
pub(crate) fn into_call(self, protocol_version: ProtocolVersionId) -> Call {
if protocol_version.is_pre_1_5_0() {
let legacy_call_trace: LegacyCall = bincode::deserialize(&self.call_trace).unwrap();

legacy_call_trace.into()
if let Ok(legacy_call_trace) = bincode::deserialize::<LegacyCall>(&self.call_trace) {
legacy_call_trace.into()
} else {
let legacy_mixed_call_trace =
bincode::deserialize::<LegacyMixedCall>(&self.call_trace)
.expect("Failed to deserialize call trace");
legacy_mixed_call_trace.into()
}
} else {
bincode::deserialize(&self.call_trace).unwrap()
}
Expand Down
83 changes: 81 additions & 2 deletions core/lib/types/src/vm_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ pub enum CallType {
NearCall,
}

#[derive(Clone, Serialize, Deserialize)]
/// Represents a call in the VM trace.
/// This version of the call represents the call structure before the 1.5.0 protocol version, where
/// all the gas-related fields were represented as `u32` instead of `u64`.
#[derive(Clone, Serialize, Deserialize)]
pub struct LegacyCall {
/// Type of the call.
pub r#type: CallType,
Expand Down Expand Up @@ -103,8 +103,38 @@ pub struct LegacyCall {
pub calls: Vec<LegacyCall>,
}

#[derive(Clone, Serialize, Deserialize)]
/// Represents a call in the VM trace.
/// This version has subcalls in the form of "new" calls.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LegacyMixedCall {
/// Type of the call.
pub r#type: CallType,
/// Address of the caller.
pub from: Address,
/// Address of the callee.
pub to: Address,
/// Gas from the parent call.
pub parent_gas: u32,
/// Gas provided for the call.
pub gas: u32,
/// Gas used by the call.
pub gas_used: u32,
/// Value transferred.
pub value: U256,
/// Input data.
pub input: Vec<u8>,
/// Output data.
pub output: Vec<u8>,
/// Error message provided by vm or some unexpected errors.
pub error: Option<String>,
/// Revert reason.
pub revert_reason: Option<String>,
/// Subcalls.
pub calls: Vec<Call>,
}

/// Represents a call in the VM trace.
#[derive(Clone, Serialize, Deserialize)]
pub struct Call {
/// Type of the call.
pub r#type: CallType,
Expand Down Expand Up @@ -151,6 +181,25 @@ impl From<LegacyCall> for Call {
}
}

impl From<LegacyMixedCall> for Call {
fn from(legacy_call: LegacyMixedCall) -> Self {
Self {
r#type: legacy_call.r#type,
from: legacy_call.from,
to: legacy_call.to,
parent_gas: legacy_call.parent_gas as u64,
gas: legacy_call.gas as u64,
gas_used: legacy_call.gas_used as u64,
value: legacy_call.value,
input: legacy_call.input,
output: legacy_call.output,
error: legacy_call.error,
revert_reason: legacy_call.revert_reason,
calls: legacy_call.calls,
}
}
}

#[derive(Debug, Clone)]
pub struct LegacyCallConversionOverflowError;

Expand Down Expand Up @@ -186,6 +235,36 @@ impl TryFrom<Call> for LegacyCall {
}
}

impl TryFrom<Call> for LegacyMixedCall {
type Error = LegacyCallConversionOverflowError;

fn try_from(call: Call) -> Result<Self, LegacyCallConversionOverflowError> {
Ok(Self {
r#type: call.r#type,
from: call.from,
to: call.to,
parent_gas: call
.parent_gas
.try_into()
.map_err(|_| LegacyCallConversionOverflowError)?,
gas: call
.gas
.try_into()
.map_err(|_| LegacyCallConversionOverflowError)?,
gas_used: call
.gas_used
.try_into()
.map_err(|_| LegacyCallConversionOverflowError)?,
value: call.value,
input: call.input,
output: call.output,
error: call.error,
revert_reason: call.revert_reason,
calls: call.calls,
})
}
}

impl Call {
pub fn new_high_level(
gas: u64,
Expand Down

0 comments on commit 2b236fe

Please sign in to comment.