From 767e443a9a4fbe72bf44e35d88d4e8aaee067494 Mon Sep 17 00:00:00 2001 From: Vadim Smirnov Date: Thu, 27 Jul 2023 15:52:53 +0300 Subject: [PATCH] use u8 repr as result and convert to err --- Cargo.lock | 21 +++++++++++++++++++++ Cargo.toml | 1 + common/lazy-pages/src/lib.rs | 4 +++- core-backend/common/Cargo.toml | 1 + core-backend/common/src/memory.rs | 13 +++++++------ runtime-interface/src/lib.rs | 9 ++++++--- 6 files changed, 39 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b5ed2324e62..c2bc2c15896 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3735,6 +3735,7 @@ dependencies = [ "gear-wasm-instrument", "gsys", "log", + "num_enum", "parity-scale-codec", "rand 0.8.5", "scale-info", @@ -6865,6 +6866,26 @@ dependencies = [ "libc", ] +[[package]] +name = "num_enum" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a015b430d3c108a207fd776d2e2196aaf8b1cf8cf93253e3a097ff3085076a1" +dependencies = [ + "num_enum_derive", +] + +[[package]] +name = "num_enum_derive" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96667db765a921f7b295ffee8b60472b686a51d4f21c2ee4ffdb94c7013b65a6" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.25", +] + [[package]] name = "number_prefix" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 021ac1dbbb6..d31f7b79459 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -136,6 +136,7 @@ jsonrpsee = { version = "^0.16" } lazy_static = "1.4.0" libc = { version = "0.2", default-features = false } log = { version = "0.4.17", default-features = false } +num_enum = { version = "0.6.1", default-features = false } once_cell = "1.17.1" parity-scale-codec = { version = "3.6.4", default-features = false } parity-wasm = "0.45.0" diff --git a/common/lazy-pages/src/lib.rs b/common/lazy-pages/src/lib.rs index e605ce625e9..d080c97bd4e 100644 --- a/common/lazy-pages/src/lib.rs +++ b/common/lazy-pages/src/lib.rs @@ -184,7 +184,9 @@ pub fn pre_process_memory_accesses( gas_left.allowance, ); *gas_left = gas_left_new; - if let Err(err) = res { + + // if result can be converted to `ProcessAccessError` then it's an error + if let Ok(err) = ProcessAccessError::try_from(res) { return Err(err); } Ok(()) diff --git a/core-backend/common/Cargo.toml b/core-backend/common/Cargo.toml index 76c33ec6920..2eb69797185 100644 --- a/core-backend/common/Cargo.toml +++ b/core-backend/common/Cargo.toml @@ -15,6 +15,7 @@ blake2-rfc.workspace = true derive_more.workspace = true gsys.workspace = true log.workspace = true +num_enum.workspace = true parity-scale-codec.workspace = true scale-info = { workspace = true, features = ["derive"] } diff --git a/core-backend/common/src/memory.rs b/core-backend/common/src/memory.rs index 5406361de10..3f63a5f83b0 100644 --- a/core-backend/common/src/memory.rs +++ b/core-backend/common/src/memory.rs @@ -37,15 +37,16 @@ use gear_core::{ memory::{Memory, MemoryError, MemoryInterval}, }; use gear_core_errors::MemoryError as FallibleMemoryError; -use scale_info::scale::{self, Decode, DecodeAll, Encode, MaxEncodedLen}; +use num_enum::{IntoPrimitive, TryFromPrimitive}; +use scale_info::scale::{Decode, DecodeAll, Encode, MaxEncodedLen}; /// Memory access error during sys-call that lazy-pages have caught. -#[derive(Debug, Clone, Encode, Decode)] -#[codec(crate = scale)] +#[derive(Debug, Clone, Encode, Decode, IntoPrimitive, TryFromPrimitive)] +#[repr(u8)] pub enum ProcessAccessError { - OutOfBounds, - GasLimitExceeded, - GasAllowanceExceeded, + OutOfBounds = 1, + GasLimitExceeded = 2, + GasAllowanceExceeded = 3, } #[derive(Debug, Clone, derive_more::From)] diff --git a/runtime-interface/src/lib.rs b/runtime-interface/src/lib.rs index b5d285d9913..c1828e5503e 100644 --- a/runtime-interface/src/lib.rs +++ b/runtime-interface/src/lib.rs @@ -101,7 +101,7 @@ pub trait GearRI { writes: &[u8], gas_left: u64, gas_allowance: u64, - ) -> (GasLeft, Result<(), ProcessAccessError>) { + ) -> (GasLeft, u8) { let reads_len = reads.len(); let writes_len = writes.len(); assert!(reads_len % 8 == 0); @@ -114,11 +114,14 @@ pub trait GearRI { gas: gas_left, allowance: gas_allowance, }; - let res = lazy_pages::pre_process_memory_accesses( + let res = match lazy_pages::pre_process_memory_accesses( &reads_intervals, &writes_intervals, &mut gas_left, - ); + ) { + Ok(_) => 0, + Err(err) => err.into(), + }; (gas_left, res) }