Skip to content

Commit

Permalink
use u8 repr as result and convert to err
Browse files Browse the repository at this point in the history
  • Loading branch information
ukint-vs committed Jul 27, 2023
1 parent 0c2cb53 commit 767e443
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 10 deletions.
21 changes: 21 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion common/lazy-pages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
1 change: 1 addition & 0 deletions core-backend/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }

Expand Down
13 changes: 7 additions & 6 deletions core-backend/common/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
9 changes: 6 additions & 3 deletions runtime-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
}

Expand Down

0 comments on commit 767e443

Please sign in to comment.