Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
into gshep/issue-1088-2

Conflicts:
	gtest/src/manager.rs
	pallets/gear/src/lib.rs
  • Loading branch information
gshep committed Jul 21, 2022
2 parents cfbf941 + 094f8fc commit f7b5925
Show file tree
Hide file tree
Showing 24 changed files with 1,805 additions and 1,763 deletions.
358 changes: 52 additions & 306 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ members = [
"common/codegen",
"core",
"core-backend/common",
"core-backend/wasmtime",
"core-backend/sandbox",
"core-backend/wasmi",
"core-processor",
"core-errors",
"examples/binaries/*",
Expand Down
80 changes: 54 additions & 26 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,24 @@ pub enum Program {
Terminated,
}

#[derive(Clone, Copy, Debug)]
pub enum ProgramError {
CodeHashNotFound,
#[derive(Clone, Debug, derive_more::Display)]
pub enum CommonError {
#[display(fmt = "Program is terminated")]
IsTerminated,
DoesNotExist,
#[display(fmt = "Program does not exist for id = {}", _0)]
DoesNotExist(H256),
#[display(fmt = "Cannot find data for {:?}, program {}", page, program_id)]
CannotFindDataForPage {
program_id: H256,
page: PageNumber,
},
MemoryError(MemoryError),
}

impl From<MemoryError> for CommonError {
fn from(err: MemoryError) -> Self {
Self::MemoryError(err)
}
}

impl Program {
Expand Down Expand Up @@ -209,12 +222,12 @@ impl Program {
}

impl core::convert::TryFrom<Program> for ActiveProgram {
type Error = ProgramError;
type Error = CommonError;

fn try_from(prog_with_status: Program) -> Result<ActiveProgram, Self::Error> {
match prog_with_status {
Program::Active(p) => Ok(p),
Program::Terminated => Err(ProgramError::IsTerminated),
Program::Terminated => Err(CommonError::IsTerminated),
}
}
}
Expand Down Expand Up @@ -297,18 +310,18 @@ pub fn set_program_initialized(id: H256) {
}
}

pub fn set_program_terminated_status(id: H256) -> Result<(), ProgramError> {
pub fn set_program_terminated_status(id: H256) -> Result<(), CommonError> {
if let Some(program) = get_program(id) {
if program.is_terminated() {
return Err(ProgramError::IsTerminated);
return Err(CommonError::IsTerminated);
}

sp_io::storage::clear_prefix(&pages_prefix(id), None);
sp_io::storage::set(&program_key(id), &Program::Terminated.encode());

Ok(())
} else {
Err(ProgramError::DoesNotExist)
Err(CommonError::DoesNotExist(id))
}
}

Expand All @@ -318,34 +331,49 @@ pub fn get_program(id: H256) -> Option<Program> {
}

/// Returns mem page data from storage for program `id` and `page_idx`
pub fn get_program_page_data(
id: H256,
page_idx: PageNumber,
) -> Option<Result<PageBuf, MemoryError>> {
let key = page_key(id, page_idx);
let data = sp_io::storage::get(&key)?;
Some(PageBuf::new_from_vec(data))
pub fn get_program_page_data(program_id: H256, page: PageNumber) -> Result<PageBuf, CommonError> {
let key = page_key(program_id, page);
let data =
sp_io::storage::get(&key).ok_or(CommonError::CannotFindDataForPage { program_id, page })?;
PageBuf::new_from_vec(data).map_err(Into::into)
}

/// Returns data for all program pages, that have data in storage.
pub fn get_program_pages_data(
id: H256,
program_id: H256,
program: &ActiveProgram,
) -> Result<BTreeMap<PageNumber, PageBuf>, MemoryError> {
get_program_data_for_pages(id, program.pages_with_data.iter())
) -> Result<BTreeMap<PageNumber, PageBuf>, CommonError> {
get_program_data_for_pages(program_id, program.pages_with_data.iter())
}

/// Returns data for all pages from `pages` arg, which has data in storage.
/// Returns program data for each page from `pages`
pub fn get_program_data_for_pages<'a>(
id: H256,
program_id: H256,
pages: impl Iterator<Item = &'a PageNumber>,
) -> Result<BTreeMap<PageNumber, PageBuf>, MemoryError> {
) -> Result<BTreeMap<PageNumber, PageBuf>, CommonError> {
let mut pages_data = BTreeMap::new();
for &page in pages {
let key = page_key(program_id, page);
let data = sp_io::storage::get(&key)
.ok_or(CommonError::CannotFindDataForPage { program_id, page })?;
let page_buf = PageBuf::new_from_vec(data)?;
pages_data.insert(page, page_buf);
}
Ok(pages_data)
}

/// Returns program data for each page from `pages`,
/// which has data in storage.
pub fn get_program_data_for_pages_optional(
program_id: H256,
pages: impl Iterator<Item = PageNumber>,
) -> Result<BTreeMap<PageNumber, PageBuf>, CommonError> {
let mut pages_data = BTreeMap::new();
for page in pages {
let key = page_key(id, *page);
let data = sp_io::storage::get(&key);
if let Some(data) = data {
let key = page_key(program_id, page);
if let Some(data) = sp_io::storage::get(&key) {
let page_buf = PageBuf::new_from_vec(data)?;
pages_data.insert(*page, page_buf);
pages_data.insert(page, page_buf);
}
}
Ok(pages_data)
Expand Down
1 change: 1 addition & 0 deletions core-backend/sandbox/src/funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub(crate) fn return_i32<T: TryInto<i32>>(val: T) -> SyscallOutput {
}

pub(crate) fn return_i64<T: TryInto<i64>>(val: T) -> SyscallOutput {
// Issue (#1208)
val.try_into()
.map(|v| Value::I64(v).into())
.map_err(|_| HostError)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "gear-backend-wasmtime"
name = "gear-backend-wasmi"
version = "0.1.0"
authors = ["Gear Technologies"]
edition = "2018"
Expand All @@ -10,8 +10,12 @@ gear-core = { path = "../../core" }
gear-core-errors = { path = "../../core-errors", features = ["codec"] }
gear-backend-common = { path = "../common" }

wasmtime = { version = "0.35.1", default-features = false, features = ["parallel-compilation", "cranelift"] }
anyhow = { version = "1.0.57", default-features = false }
codec = { package = "parity-scale-codec", version = "3.1.2", default-features = false, features = ["derive"] }
log = "0.4.17"
parity-wasm = { version = "0.42.2", default-features = false }
wasmi = { version = "0.11.0", default-features = false }
log = { version = "0.4.17", default-features = false }
derive_more = "0.99.17"
codec = { package = "parity-scale-codec", version = "3.1.2", default-features = false }

[features]
default = ["std"]
std = ["wasmi/std", "parity-wasm/std", "log/std"]
Loading

0 comments on commit f7b5925

Please sign in to comment.