Skip to content

Commit 09b86d0

Browse files
sayongithub-actions[bot]0xVolosnikov
authored
refactor: expand error location into metadata (#174)
## What ❔ Instead of including error location directly, includes it as a part of otherwise empty error metadata. Prepares for inclusion of error contexts in the next PR. ## Why ❔ <!-- Why are these changes done? What goal do they contribute to? What are the principles behind them? --> <!-- The `Why` has to be clear to non-Matter Labs entities running their own ZK Chain --> <!-- Example: PR templates ensure PR reviewers, observers, and future iterators are in context about the evolution of repos. --> ## Is this a breaking change? - [ ] Yes - [x] No ## Checklist <!-- Check your PR fulfills the following items. --> <!-- For draft PRs check the boxes as you complete them. --> - [x] PR title corresponds to the body of PR (we generate changelog entries from PRs). - [ ] Tests for the changes have been added / updated. - [x] Documentation comments have been added / updated. - [x] Code has been formatted. --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Vladislav Volosnikov <Volosnikov.apmath@gmail.com>
1 parent 847bcb3 commit 09b86d0

File tree

16 files changed

+113
-24
lines changed

16 files changed

+113
-24
lines changed

basic_bootloader/src/bootloader/runner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ impl<'external, S: EthereumLikeTypes> Run<'_, 'external, S> {
453453
}
454454
}
455455
SubsystemError::LeafDefect(_) => return Err(wrap_error!(e)),
456-
SubsystemError::LeafRuntime(runtime_error) => match runtime_error {
456+
SubsystemError::LeafRuntime(ref runtime_error) => match runtime_error {
457457
RuntimeError::OutOfNativeResources(_) => return Err(wrap_error!(e)),
458458
RuntimeError::OutOfErgs(_) => {
459459
return Err(internal_error!("Out of ergs on infinite ergs").into())

evm_interpreter/src/ee_trait_impl.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use zk_ee::system::errors::runtime::RuntimeError;
1313
use zk_ee::system::errors::subsystem::SubsystemError;
1414
use zk_ee::system::*;
1515
use zk_ee::types_config::SystemIOTypesConfig;
16+
use zk_ee::utils::cheap_clone::CheapCloneRiscV;
1617
use zk_ee::utils::{b160_to_u256, Bytes32};
1718
use zk_ee::{interface_error, internal_error, wrap_error};
1819

@@ -434,7 +435,7 @@ impl<'ee, S: EthereumLikeTypes> ExecutionEnvironment<'ee, S, EvmErrors> for Inte
434435
.map_err(|e| -> EvmSubsystemError {
435436
match e.root_cause() {
436437
RootCause::Runtime(e @ RuntimeError::OutOfNativeResources(_)) => {
437-
(*e).into()
438+
e.clone_or_copy().into()
438439
}
439440
_ => internal_error!("Keccak in create2 cannot fail").into(),
440441
}

system_hooks/src/precompiles.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use zk_ee::{
2626
},
2727
CallModifier, Resources, System,
2828
},
29+
utils::cheap_clone::CheapCloneRiscV as _,
2930
};
3031

3132
///
@@ -85,7 +86,7 @@ where
8586
Ok((make_error_return_state(resources), rest))
8687
}
8788
RootCause::Runtime(e @ RuntimeError::OutOfNativeResources(_)) => {
88-
Err(Into::<SystemError>::into(*e))
89+
Err(Into::<SystemError>::into(e.clone_or_copy()))
8990
}
9091
},
9192
}

zk_ee/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@
2727
#![allow(clippy::len_without_is_empty)]
2828
#![allow(clippy::needless_return)]
2929
#![allow(clippy::wrong_self_convention)]
30+
#![cfg_attr(
31+
any(feature = "error_origins", not(target_arch = "riscv32")),
32+
allow(clippy::result_large_err)
33+
)]
34+
#![cfg_attr(
35+
any(feature = "error_origins", not(target_arch = "riscv32")),
36+
allow(clippy::large_enum_variant)
37+
)]
3038

3139
extern crate alloc;
3240

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
use super::location::{ErrorLocation, Localizable};
1+
use super::{
2+
location::{ErrorLocation, Localizable},
3+
metadata::Metadata,
4+
};
25

36
pub trait InterfaceErrorKind: Clone + core::fmt::Debug + Eq + Sized + Into<&'static str> {
47
fn get_name(&self) -> &'static str;
58
}
69

7-
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
8-
pub struct InterfaceError<T: InterfaceErrorKind>(pub T, pub ErrorLocation);
10+
#[derive(Debug, PartialEq, Eq, Clone)]
11+
pub struct InterfaceError<T: InterfaceErrorKind>(pub T, pub Metadata);
912

1013
#[macro_export]
1114
macro_rules! interface_error {
1215
($instance:expr) => {
13-
$crate::system::errors::interface::InterfaceError($instance, $crate::location!()).into()
16+
$crate::system::errors::interface::InterfaceError($instance, $crate::location!().into())
17+
.into()
1418
};
1519
}
1620

@@ -19,7 +23,7 @@ pub struct AsInterfaceError<E>(pub E);
1923

2024
impl<T: InterfaceErrorKind> Localizable for InterfaceError<T> {
2125
fn get_location(&self) -> ErrorLocation {
22-
let InterfaceError(_, location) = self;
23-
*location
26+
let InterfaceError(_, meta) = self;
27+
meta.location
2428
}
2529
}
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
use super::location::{ErrorLocation, Localizable};
1+
use super::{
2+
location::{ErrorLocation, Localizable},
3+
metadata::Metadata,
4+
};
25

36
///
47
/// Internal error, should not be triggered by user input.
58
/// Do not construct it explicitly; instead, use the macro [`internal_error`].
69
///
7-
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
8-
pub struct InternalError(pub &'static str, pub ErrorLocation);
10+
#[cfg_attr(target_arch = "riscv32", derive(Copy))]
11+
#[derive(Debug, PartialEq, Eq, Clone)]
12+
pub struct InternalError(pub &'static str, pub Metadata);
913

1014
#[macro_export]
1115
macro_rules! internal_error {
1216
($msg:expr $(,)?) => {
13-
$crate::system::errors::internal::InternalError($msg, $crate::location!())
17+
$crate::system::errors::internal::InternalError($msg, $crate::location!().into())
1418
};
1519
}
1620

1721
impl Localizable for InternalError {
1822
fn get_location(&self) -> ErrorLocation {
19-
let InternalError(_, location) = self;
20-
*location
23+
let InternalError(_, meta) = self;
24+
meta.location
2125
}
2226
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use super::location::ErrorLocation;
2+
3+
#[cfg_attr(target_arch = "riscv32", derive(Copy))]
4+
#[derive(Debug, PartialEq, Eq, Clone)]
5+
pub struct Metadata {
6+
pub location: ErrorLocation,
7+
}
8+
9+
impl Metadata {
10+
pub fn new(location: ErrorLocation) -> Self {
11+
Self { location }
12+
}
13+
}
14+
15+
impl From<ErrorLocation> for Metadata {
16+
fn from(location: ErrorLocation) -> Self {
17+
Self::new(location)
18+
}
19+
}
20+
21+
impl core::fmt::Display for Metadata {
22+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
23+
let Self { location } = self;
24+
writeln!(f, "-- at {location}")?;
25+
Ok(())
26+
}
27+
}

zk_ee/src/system/errors/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub mod display;
33
pub mod interface;
44
pub mod internal;
55
pub mod location;
6+
pub mod metadata;
67
pub mod no_errors;
78
pub mod root_cause;
89
pub mod runtime;

zk_ee/src/system/errors/runtime.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
use strum_macros::IntoStaticStr;
22

3-
use super::location::ErrorLocation;
3+
use super::location::{ErrorLocation, Localizable};
4+
use super::metadata::Metadata;
45

5-
#[derive(Copy, Clone, Debug, PartialEq, Eq, IntoStaticStr)]
6+
#[cfg_attr(target_arch = "riscv32", derive(Copy))]
7+
#[derive(Clone, Debug, PartialEq, Eq, IntoStaticStr)]
68
pub enum RuntimeError {
7-
OutOfNativeResources(ErrorLocation),
8-
OutOfErgs(ErrorLocation),
9+
OutOfNativeResources(Metadata),
10+
OutOfErgs(Metadata),
911
}
1012

1113
#[macro_export]
1214
macro_rules! out_of_native_resources {
1315
() => {
14-
$crate::system::errors::runtime::RuntimeError::OutOfNativeResources($crate::location!())
16+
$crate::system::errors::runtime::RuntimeError::OutOfNativeResources(
17+
$crate::location!().into(),
18+
)
1519
};
1620
}
21+
impl Localizable for RuntimeError {
22+
fn get_location(&self) -> ErrorLocation {
23+
match self {
24+
RuntimeError::OutOfNativeResources(metadata) | RuntimeError::OutOfErgs(metadata) => {
25+
metadata.location
26+
}
27+
}
28+
}
29+
}

zk_ee/src/system/errors/system.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use super::{
55
subsystem::{Subsystem, SubsystemError},
66
};
77

8-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
8+
#[cfg_attr(target_arch = "riscv32", derive(Copy))]
9+
#[derive(Clone, Debug, PartialEq, Eq)]
910
pub enum SystemError {
1011
LeafDefect(InternalError),
1112
LeafRuntime(RuntimeError),
@@ -50,9 +51,7 @@ where
5051
macro_rules! out_of_ergs_error {
5152
() => {
5253
$crate::system::errors::system::SystemError::LeafRuntime(
53-
$crate::system::errors::runtime::RuntimeError::OutOfErgs(
54-
$crate::system::errors::location::ErrorLocation::new(file!(), line!()),
55-
),
54+
$crate::system::errors::runtime::RuntimeError::OutOfErgs($crate::location!().into()),
5655
)
5756
};
5857
}

0 commit comments

Comments
 (0)