Skip to content

Commit

Permalink
Remove PageSizes parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
ark0f committed Nov 7, 2023
1 parent c71d827 commit 7c7b012
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 57 deletions.
16 changes: 4 additions & 12 deletions gtest/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use gear_core::{
pages::GearPage,
program::MemoryInfix,
};
use gear_lazy_pages::{LazyPagesStorage, LazyPagesVersion, PagePrefix, PageSizes};
use gear_lazy_pages::{LazyPagesStorage, LazyPagesVersion, PagePrefix};
use gear_lazy_pages_common::LazyPagesInitContext;
use path_clean::PathClean;
use std::{
Expand Down Expand Up @@ -112,19 +112,11 @@ impl LazyPagesStorage for PagesStorage {
self.pages_data.get_page(program_id, page).is_some()
}

fn load_page(
&mut self,
_page_sizes: &PageSizes,
prefix: &PagePrefix,
page: GearPage,
buffer: &mut [u8],
) -> Result<bool, String> {
fn load_page(&mut self, prefix: &PagePrefix, page: GearPage, buffer: &mut [u8]) -> Option<u32> {
let (program_id, _infix) = Self::parse_key(prefix.as_slice());
let Some(page_buf) = self.pages_data.get_page(program_id, page) else {
return Ok(false);
};
let page_buf = self.pages_data.get_page(program_id, page)?;
buffer.copy_from_slice(&page_buf);
Ok(true)
Some(page_buf.len() as u32)
}
}

Expand Down
26 changes: 14 additions & 12 deletions lazy-pages/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ pub(crate) enum Error {
ReadAccessSignalFromAccessedPage,
#[display(fmt = "WASM memory begin address is not set")]
WasmMemAddrIsNotSet,
#[display(fmt = "Pages store error: {_0}")]
PagesStorage(String),
#[display(fmt = "Page data in storage must contain {expected} bytes, actually has {actual}")]
InvalidPageDataSize { expected: u32, actual: u32 },
#[display(fmt = "Any page cannot be write accessed twice: {_0:?}")]
DoubleWriteAccess(GearPage),
#[display(fmt = "Any page cannot be read charged twice: {_0:?}")]
Expand Down Expand Up @@ -154,22 +154,24 @@ impl LazyPagesRuntimeContext {
page: GearPage,
buffer: &mut [u8],
) -> Result<bool, Error> {
self.program_storage
.load_page(&self.page_sizes, prefix, page, buffer)
.map_err(Error::PagesStorage)
if let Some(size) = self.program_storage.load_page(prefix, page, buffer) {
if size != GearPage::size(self) {
return Err(Error::InvalidPageDataSize {
expected: GearPage::size(self),
actual: size,
});
}
Ok(true)
} else {
Ok(false)
}
}
}

pub trait LazyPagesStorage: fmt::Debug {
fn page_exists(&self, prefix: &PagePrefix, page: GearPage) -> bool;

fn load_page(
&mut self,
page_sizes: &PageSizes,
prefix: &PagePrefix,
page: GearPage,
buffer: &mut [u8],
) -> Result<bool, String>;
fn load_page(&mut self, prefix: &PagePrefix, page: GearPage, buffer: &mut [u8]) -> Option<u32>;
}

#[derive(Debug)]
Expand Down
9 changes: 3 additions & 6 deletions lazy-pages/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::{
common::{Error, PageSizes},
init_with_handler, mprotect,
signal::ExceptionInfo,
LazyPagesStorage, LazyPagesVersion, PagePrefix, UserSignalHandler,
common::Error, init_with_handler, mprotect, signal::ExceptionInfo, LazyPagesStorage,
LazyPagesVersion, PagePrefix, UserSignalHandler,
};
use gear_core::{
pages::{GearPage, PageDynSize, PageU32Size, WasmPage},
Expand All @@ -39,11 +37,10 @@ impl LazyPagesStorage for NoopStorage {

fn load_page(
&mut self,
_page_sizes: &PageSizes,
_prefix: &PagePrefix,
_page: GearPage,
_buffer: &mut [u8],
) -> Result<bool, String> {
) -> Option<u32> {
unreachable!()
}
}
Expand Down
31 changes: 4 additions & 27 deletions runtime-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ use codec::{Decode, Encode};
use gear_core::{
gas::GasLeft,
memory::{HostPointer, MemoryInterval},
pages::{GearPage, PageDynSize},
pages::GearPage,
str::LimitedStr,
};
#[cfg(feature = "std")]
use gear_lazy_pages::{LazyPagesStorage, PagePrefix, PageSizes};
use gear_lazy_pages::{LazyPagesStorage, PagePrefix};
use gear_lazy_pages_common::{GlobalsAccessConfig, ProcessAccessError, Status};
use sp_runtime_interface::{
pass_by::{Codec, PassBy},
Expand Down Expand Up @@ -116,12 +116,6 @@ impl PassBy for LazyPagesInitContext {
type PassBy = Codec<LazyPagesInitContext>;
}

#[derive(Debug, derive_more::Display)]
enum SpIoProgramStorageError {
#[display(fmt = "Page data in storage must contain {expected} bytes, actually has {actual}")]
InvalidPageDataSize { expected: u32, actual: u32 },
}

#[derive(Debug, Default)]
struct SpIoProgramStorage;

Expand All @@ -132,26 +126,9 @@ impl LazyPagesStorage for SpIoProgramStorage {
sp_io::storage::exists(&key)
}

fn load_page(
&mut self,
page_sizes: &PageSizes,
prefix: &PagePrefix,
page: GearPage,
buffer: &mut [u8],
) -> Result<bool, String> {
fn load_page(&mut self, prefix: &PagePrefix, page: GearPage, buffer: &mut [u8]) -> Option<u32> {
let key = prefix.key_for_page(page);
if let Some(size) = sp_io::storage::read(&key, buffer, 0) {
if size != GearPage::size(page_sizes) {
return Err(SpIoProgramStorageError::InvalidPageDataSize {
expected: GearPage::size(page_sizes),
actual: size,
}
.to_string());
}
Ok(true)
} else {
Ok(false)
}
sp_io::storage::read(&key, buffer, 0)
}
}

Expand Down

0 comments on commit 7c7b012

Please sign in to comment.