Skip to content

Commit

Permalink
use byteorder instead of le_bytes conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
ukint-vs committed Aug 20, 2023
1 parent ed68e0a commit 586ecf8
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions common/lazy-pages/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ license.workspace = true
[dependencies]
derive_more.workspace = true
log.workspace = true
byteorder.workspace = true

gear-core.workspace = true
gear-backend-common.workspace = true
Expand All @@ -24,4 +25,5 @@ std = [
"gear-runtime-interface/std",
"sp-std/std",
"log/std",
"byteorder/std"
]
6 changes: 4 additions & 2 deletions common/lazy-pages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

extern crate alloc;

use byteorder::{ByteOrder, LittleEndian};
use core::fmt;
use gear_backend_common::{
lazy_pages::{GlobalsAccessConfig, LazyPagesWeights, Status},
Expand Down Expand Up @@ -171,12 +172,13 @@ pub fn pre_process_memory_accesses(
let serialized_reads = serialize_mem_intervals(reads);
let serialized_writes = serialize_mem_intervals(writes);

let mut gas_bytes = gas_counter.to_le_bytes();
let mut gas_bytes = [0u8; 8];
LittleEndian::write_u64(&mut gas_bytes, *gas_counter);

let res =
gear_ri::pre_process_memory_accesses(&serialized_reads, &serialized_writes, &mut gas_bytes);

*gas_counter = u64::from_le_bytes(gas_bytes);
*gas_counter = LittleEndian::read_u64(&gas_bytes);

// if result can be converted to `ProcessAccessError` then it's an error
if let Ok(err) = ProcessAccessError::try_from(res) {
Expand Down
2 changes: 2 additions & 0 deletions runtime-interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ derive_more.workspace = true
static_assertions.workspace = true
region = { workspace = true, optional = true }
gear-sandbox-host = { workspace = true, optional = true }
byteorder.workspace = true

[target.'cfg(windows)'.dependencies]
winapi = { workspace = true, features = ["memoryapi"] }
Expand All @@ -34,6 +35,7 @@ std = [
"sp-runtime-interface/std",
"sp-std/std",
"sp-wasm-interface/std",
"byteorder/std",
"codec/std",
"log",
"gear-lazy-pages",
Expand Down
5 changes: 3 additions & 2 deletions runtime-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#![allow(useless_deprecated, deprecated)]
#![cfg_attr(not(feature = "std"), no_std)]

use byteorder::{ByteOrder, LittleEndian};
use codec::{Decode, Encode};
use gear_backend_common::{
lazy_pages::{GlobalsAccessConfig, Status},
Expand Down Expand Up @@ -127,7 +128,7 @@ pub trait GearRI {
let mut writes_intervals = Vec::with_capacity(writes_len / mem_interval_size);
deserialize_mem_intervals(writes, &mut writes_intervals);

let mut gas_counter = u64::from_le_bytes(*gas_bytes);
let mut gas_counter = LittleEndian::read_u64(gas_bytes);

let res = match lazy_pages::pre_process_memory_accesses(
&reads_intervals,
Expand All @@ -138,7 +139,7 @@ pub trait GearRI {
Err(err) => err.into(),
};

*gas_bytes = gas_counter.to_le_bytes();
LittleEndian::write_u64(gas_bytes, gas_counter);

res
}
Expand Down

0 comments on commit 586ecf8

Please sign in to comment.