Skip to content

Commit

Permalink
Merge branch 'master' into dn-refactor-storage-and-maybehash
Browse files Browse the repository at this point in the history
  • Loading branch information
breathx committed Oct 24, 2024
2 parents 112c872 + 40ae438 commit 4f9a17a
Show file tree
Hide file tree
Showing 44 changed files with 716 additions and 569 deletions.
332 changes: 106 additions & 226 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,12 @@ url = "2.5.2"
# wasmer 4.3.4 for some reason have wat's version "=1.0.71" nailed down, so we have to do the same
wat = "1.0.71"
wabt = "0.10.0"
wasmer = "4.3.4"
wasmer = { version = "4.3.4", default-features = false, features = ["singlepass"] }
wasmer-cache = "4.3.4"
wasmer-types = "4.3.4"
wasmer-vm = "4.3.4"
wasmer-compiler = "4.3.4"
gear-wasmer-cache = { path = "utils/gear-wasmer-cache" }
wasmtime = "8.0.1"
wasmparser = { package = "wasmparser-nostd", version = "0.100.1", default-features = false }
which = "4.4.2"
Expand Down
18 changes: 9 additions & 9 deletions core-backend/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{
BackendExternalities,
};
use alloc::{collections::BTreeSet, format, string::String};
use core::{any::Any, fmt::Debug};
use core::{any::Any, fmt::Debug, marker::Send};
use gear_core::{
env::Externalities,
gas::GasAmount,
Expand Down Expand Up @@ -60,7 +60,7 @@ macro_rules! wrap_syscall {
};
}

fn store_host_state_mut<Ext>(
fn store_host_state_mut<Ext: Send + 'static>(
store: &mut Store<HostState<Ext, BackendMemory<ExecutorMemory>>>,
) -> &mut State<Ext, BackendMemory<ExecutorMemory>> {
store.data_mut().as_mut().unwrap_or_else(|| {
Expand Down Expand Up @@ -123,7 +123,7 @@ struct EnvBuilder<Ext: BackendExternalities> {

impl<Ext> EnvBuilder<Ext>
where
Ext: BackendExternalities + 'static,
Ext: BackendExternalities + Send + 'static,
Ext::UnrecoverableError: BackendSyscallError,
RunFallibleError: From<Ext::FallibleError>,
Ext::AllocError: BackendAllocSyscallError<ExtError = Ext::UnrecoverableError>,
Expand Down Expand Up @@ -159,7 +159,7 @@ impl<Ext: BackendExternalities> From<EnvBuilder<Ext>>

impl<Ext, EntryPoint> Environment<Ext, EntryPoint>
where
Ext: BackendExternalities + 'static,
Ext: BackendExternalities + Send + 'static,
Ext::UnrecoverableError: BackendSyscallError,
RunFallibleError: From<Ext::FallibleError>,
Ext::AllocError: BackendAllocSyscallError<ExtError = Ext::UnrecoverableError>,
Expand Down Expand Up @@ -232,9 +232,9 @@ struct GlobalsAccessProvider<Ext: Externalities> {
store: Option<Store<HostState<Ext, BackendMemory<ExecutorMemory>>>>,
}

impl<Ext: Externalities + 'static> GlobalsAccessor for GlobalsAccessProvider<Ext> {
fn get_i64(&self, name: &LimitedStr) -> Result<i64, GlobalsAccessError> {
let store = self.store.as_ref().ok_or(GlobalsAccessError)?;
impl<Ext: Externalities + Send + 'static> GlobalsAccessor for GlobalsAccessProvider<Ext> {
fn get_i64(&mut self, name: &LimitedStr) -> Result<i64, GlobalsAccessError> {
let store = self.store.as_mut().ok_or(GlobalsAccessError)?;
self.instance
.get_global_val(store, name.as_str())
.and_then(i64::try_from_value)
Expand All @@ -255,7 +255,7 @@ impl<Ext: Externalities + 'static> GlobalsAccessor for GlobalsAccessProvider<Ext

impl<EnvExt, EntryPoint> Environment<EnvExt, EntryPoint>
where
EnvExt: BackendExternalities + 'static,
EnvExt: BackendExternalities + Send + 'static,
EnvExt::UnrecoverableError: BackendSyscallError,
RunFallibleError: From<EnvExt::FallibleError>,
EnvExt::AllocError: BackendAllocSyscallError<ExtError = EnvExt::UnrecoverableError>,
Expand Down Expand Up @@ -440,7 +440,7 @@ where

// Fetching global value.
let gas = instance
.get_global_val(&store, GLOBAL_NAME_GAS)
.get_global_val(&mut store, GLOBAL_NAME_GAS)
.and_then(i64::try_from_value)
.ok_or(System(WrongInjectedGas))? as u64;

Expand Down
154 changes: 85 additions & 69 deletions core-backend/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ use crate::{
},
BackendExternalities,
};
use alloc::{collections::BTreeSet, rc::Rc, vec, vec::Vec};
use alloc::{collections::BTreeSet, vec::Vec};
use codec::{Decode, Encode};
use core::{cell::RefCell, fmt, fmt::Debug, mem};
use core::{fmt, fmt::Debug, mem};
use gear_core::{
costs::CostToken,
env::{Externalities, PayloadSliceLock, UnlockPayloadBound},
env_vars::{EnvVars, EnvVarsV1},
gas::{ChargeError, CounterType, CountersOwner, GasAmount, GasCounter, GasLeft},
ids::{MessageId, ProgramId, ReservationId},
memory::{HostPointer, Memory, MemoryError, MemoryInterval},
memory::{Memory, MemoryInterval},
message::{HandlePacket, InitPacket, ReplyPacket},
pages::{WasmPage, WasmPagesAmount},
pages::WasmPage,
};
use gear_core_errors::{ReplyCode, SignalCode};
use gear_lazy_pages_common::ProcessAccessError;
Expand Down Expand Up @@ -310,97 +310,113 @@ impl BackendExternalities for MockExt {
}
}

#[derive(Debug)]
struct InnerMockMemory {
pages: Vec<u8>,
read_attempt_count: u32,
write_attempt_count: u32,
}
#[cfg(feature = "std")]
pub use with_std_feature::*;

impl InnerMockMemory {
fn grow(&mut self, pages: WasmPagesAmount) -> u32 {
let size = self.pages.len() as u32;
let new_size = size + pages.offset() as u32;
self.pages.resize(new_size as usize, 0);
#[cfg(feature = "std")]
mod with_std_feature {
use gear_core::{
memory::{HostPointer, Memory, MemoryError},
pages::{WasmPage, WasmPagesAmount},
};
use std::sync::{Arc, Mutex, MutexGuard};

size / WasmPage::SIZE
#[derive(Debug)]
struct InnerMockMemory {
pages: Vec<u8>,
read_attempt_count: u32,
write_attempt_count: u32,
}

fn write(&mut self, offset: u32, buffer: &[u8]) -> Result<(), MemoryError> {
self.write_attempt_count += 1;
impl InnerMockMemory {
fn grow(&mut self, pages: WasmPagesAmount) -> u32 {
let size = self.pages.len() as u32;
let new_size = size + pages.offset() as u32;
self.pages.resize(new_size as usize, 0);

let offset = offset as usize;
if offset + buffer.len() > self.pages.len() {
return Err(MemoryError::AccessOutOfBounds);
size / WasmPage::SIZE
}

self.pages[offset..offset + buffer.len()].copy_from_slice(buffer);
fn write(&mut self, offset: u32, buffer: &[u8]) -> Result<(), MemoryError> {
self.write_attempt_count += 1;

Ok(())
}
let offset = offset as usize;
if offset + buffer.len() > self.pages.len() {
return Err(MemoryError::AccessOutOfBounds);
}

fn read(&mut self, offset: u32, buffer: &mut [u8]) -> Result<(), MemoryError> {
self.read_attempt_count += 1;
self.pages[offset..offset + buffer.len()].copy_from_slice(buffer);

let offset = offset as usize;
if offset + buffer.len() > self.pages.len() {
return Err(MemoryError::AccessOutOfBounds);
Ok(())
}

buffer.copy_from_slice(&self.pages[offset..(offset + buffer.len())]);
fn read(&mut self, offset: u32, buffer: &mut [u8]) -> Result<(), MemoryError> {
self.read_attempt_count += 1;

Ok(())
}
let offset = offset as usize;
if offset + buffer.len() > self.pages.len() {
return Err(MemoryError::AccessOutOfBounds);
}

buffer.copy_from_slice(&self.pages[offset..(offset + buffer.len())]);

Ok(())
}

fn size(&self) -> WasmPagesAmount {
WasmPage::from_offset(self.pages.len() as u32).into()
fn size(&self) -> WasmPagesAmount {
WasmPage::from_offset(self.pages.len() as u32).into()
}
}
}

#[derive(Debug, Clone)]
pub struct MockMemory(Rc<RefCell<InnerMockMemory>>);
#[derive(Debug, Clone)]
pub struct MockMemory(Arc<Mutex<InnerMockMemory>>);

impl MockMemory {
pub fn new(initial_pages: u32) -> Self {
let pages = vec![0; initial_pages as usize * WasmPage::SIZE as usize];
impl MockMemory {
pub fn new(initial_pages: u32) -> Self {
let pages = vec![0; initial_pages as usize * WasmPage::SIZE as usize];

Self(Rc::new(RefCell::new(InnerMockMemory {
pages,
read_attempt_count: 0,
write_attempt_count: 0,
})))
}
Self(Arc::new(Mutex::new(InnerMockMemory {
pages,
read_attempt_count: 0,
write_attempt_count: 0,
})))
}

pub fn read_attempt_count(&self) -> u32 {
self.0.borrow().read_attempt_count
}
fn lock(&self) -> MutexGuard<'_, InnerMockMemory> {
self.0.lock().unwrap()
}

pub fn write_attempt_count(&self) -> u32 {
self.0.borrow().write_attempt_count
pub fn read_attempt_count(&self) -> u32 {
self.lock().read_attempt_count
}

pub fn write_attempt_count(&self) -> u32 {
self.lock().write_attempt_count
}
}
}

impl<Context> Memory<Context> for MockMemory {
type GrowError = &'static str;
impl<Context> Memory<Context> for MockMemory {
type GrowError = &'static str;

fn grow(&self, _ctx: &mut Context, pages: WasmPagesAmount) -> Result<(), Self::GrowError> {
let _ = self.0.borrow_mut().grow(pages);
Ok(())
}
fn grow(&self, _ctx: &mut Context, pages: WasmPagesAmount) -> Result<(), Self::GrowError> {
let _ = self.lock().grow(pages);
Ok(())
}

fn size(&self, _ctx: &Context) -> WasmPagesAmount {
self.0.borrow_mut().size()
}
fn size(&self, _ctx: &Context) -> WasmPagesAmount {
self.lock().size()
}

fn write(&self, _ctx: &mut Context, offset: u32, buffer: &[u8]) -> Result<(), MemoryError> {
self.0.borrow_mut().write(offset, buffer)
}
fn write(&self, _ctx: &mut Context, offset: u32, buffer: &[u8]) -> Result<(), MemoryError> {
self.lock().write(offset, buffer)
}

fn read(&self, _ctx: &Context, offset: u32, buffer: &mut [u8]) -> Result<(), MemoryError> {
self.0.borrow_mut().read(offset, buffer)
}
fn read(&self, _ctx: &Context, offset: u32, buffer: &mut [u8]) -> Result<(), MemoryError> {
self.lock().read(offset, buffer)
}

unsafe fn get_buffer_host_addr_unsafe(&self, _ctx: &Context) -> HostPointer {
unimplemented!()
unsafe fn get_buffer_host_addr_unsafe(&self, _ctx: &Context) -> HostPointer {
unimplemented!()
}
}
}
4 changes: 2 additions & 2 deletions core-processor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub(crate) fn execute_wasm<Ext>(
msg_ctx_settings: ContextSettings,
) -> Result<DispatchResult, ExecutionError>
where
Ext: ProcessorExternalities + BackendExternalities + 'static,
Ext: ProcessorExternalities + BackendExternalities + Send + 'static,
<Ext as Externalities>::AllocError:
BackendAllocSyscallError<ExtError = Ext::UnrecoverableError>,
RunFallibleError: From<Ext::FallibleError>,
Expand Down Expand Up @@ -266,7 +266,7 @@ pub fn execute_for_reply<Ext, EP>(
block_info: BlockInfo,
) -> Result<Vec<u8>, String>
where
Ext: ProcessorExternalities + BackendExternalities + 'static,
Ext: ProcessorExternalities + BackendExternalities + Send + 'static,
<Ext as Externalities>::AllocError:
BackendAllocSyscallError<ExtError = Ext::UnrecoverableError>,
RunFallibleError: From<Ext::FallibleError>,
Expand Down
2 changes: 1 addition & 1 deletion core-processor/src/processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub fn process<Ext>(
random_data: (Vec<u8>, u32),
) -> Result<Vec<JournalNote>, SystemExecutionError>
where
Ext: ProcessorExternalities + BackendExternalities + 'static,
Ext: ProcessorExternalities + BackendExternalities + Send + 'static,
<Ext as Externalities>::AllocError:
BackendAllocSyscallError<ExtError = Ext::UnrecoverableError>,
RunFallibleError: From<Ext::FallibleError>,
Expand Down
8 changes: 4 additions & 4 deletions core/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ impl From<MemoryInterval> for (u32, u32) {

impl Debug for MemoryInterval {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&format!(
"[offset: {:#x}, size: {:#x}]",
self.offset, self.size
))
f.debug_struct("MemoryInterval")
.field("offset", &format_args!("{:#x}", self.offset))
.field("size", &format_args!("{:#x}", self.size))
.finish()
}
}

Expand Down
2 changes: 1 addition & 1 deletion ethexe/network/src/custom_connection_limits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ mod tests {

assert_eq!(
map.inner.into_keys().collect::<HashSet<PeerId>>(),
Default::default()
HashSet::default()
);
}

Expand Down
2 changes: 0 additions & 2 deletions ethexe/processor/src/host/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ pub(crate) struct InstanceCreator {

impl InstanceCreator {
pub fn new(runtime: Vec<u8>) -> Result<Self> {
gear_runtime_interface::sandbox_init();

let engine = wasmtime::Engine::default();

let module = wasmtime::Module::new(&engine, runtime)?;
Expand Down
9 changes: 7 additions & 2 deletions ethexe/runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,18 @@ impl<'a, S: Storage> TransitionOperator<'a, S> {
}
}

pub fn process_next_message<S: Storage, RI: RuntimeInterface<S>>(
pub fn process_next_message<S, RI>(
program_id: ProgramId,
program_state: ProgramState,
instrumented_code: Option<InstrumentedCode>,
code_id: CodeId,
ri: &RI,
) -> Vec<JournalNote> {
) -> Vec<JournalNote>
where
S: Storage,
RI: RuntimeInterface<S>,
<RI as RuntimeInterface<S>>::LazyPages: Send,
{
let block_info = ri.block_info();

log::trace!("Processing next message for program {program_id}");
Expand Down
1 change: 1 addition & 0 deletions ethexe/runtime/common/src/transitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ impl InBlockTransitions {
.modifications
.entry(actor_id)
.or_insert(NonFinalTransition {
initial_state: *initial_state,
initial_state: *initial_state,
..Default::default()
});
Expand Down
4 changes: 2 additions & 2 deletions gbuiltins/staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ homepage.workspace = true
repository.workspace = true

[dependencies]
gprimitives.workspace = true
derive_more.workspace = true
parity-scale-codec = { workspace = true, features = ["derive"] }
scale-info = { workspace = true, features = ["derive"] }
gprimitives = { workspace = true, features = ["codec"] }
Loading

0 comments on commit 4f9a17a

Please sign in to comment.