Update srml-contract to Rust 2018. (#1510)

* Fix imports.

* Remove redundant binding

* Clean extern crates.

* Add comment

* Re-export macros from prelude

* Build fixes

* Update core/sr-std/src/lib.rs

Co-Authored-By: pepyakin <s.pepyakin@gmail.com>
This commit is contained in:
Sergei Pepyakin
2019-01-24 16:26:53 +01:00
committed by GitHub
parent 5be237030d
commit a5cafa68b1
13 changed files with 95 additions and 108 deletions
@@ -25,12 +25,12 @@
//! this guarantees that every instrumented contract code in cache cannot have the version equal to the current one.
//! Thus, before executing a contract it should be reinstrument with new schedule.
use gas::{GasMeter, Token};
use crate::gas::{GasMeter, Token};
use crate::wasm::{prepare, runtime::Env, PrefabWasmModule};
use crate::{CodeHash, CodeStorage, PristineCode, Schedule, Trait};
use rstd::prelude::*;
use runtime_primitives::traits::{As, CheckedMul, Hash, Bounded};
use runtime_support::StorageMap;
use wasm::{prepare, runtime::Env, PrefabWasmModule};
use {CodeHash, CodeStorage, PristineCode, Schedule, Trait};
/// Gas metering token that used for charging storing code into the code storage.
///
@@ -29,13 +29,13 @@ macro_rules! convert_args {
macro_rules! gen_signature {
( ( $( $params: ty ),* ) ) => (
{
$crate::parity_wasm::elements::FunctionType::new(convert_args!($($params),*), None)
parity_wasm::elements::FunctionType::new(convert_args!($($params),*), None)
}
);
( ( $( $params: ty ),* ) -> $returns: ty ) => (
{
$crate::parity_wasm::elements::FunctionType::new(convert_args!($($params),*), Some({
parity_wasm::elements::FunctionType::new(convert_args!($($params),*), Some({
use $crate::wasm::env_def::ConvertibleToWasm; <$returns>::VALUE_TYPE
}))
}
@@ -96,7 +96,7 @@ macro_rules! unmarshall_then_body {
#[inline(always)]
pub fn constrain_closure<R, F>(f: F) -> F
where
F: FnOnce() -> Result<R, ::sandbox::HostError>,
F: FnOnce() -> Result<R, sandbox::HostError>,
{
f
}
@@ -110,14 +110,14 @@ macro_rules! unmarshall_then_body_then_marshall {
unmarshall_then_body!($body, $ctx, $args_iter, $( $names : $params ),*)
});
let r = body()?;
return Ok($crate::sandbox::ReturnValue::Value({ use $crate::wasm::env_def::ConvertibleToWasm; r.to_typed_value() }))
return Ok(sandbox::ReturnValue::Value({ use $crate::wasm::env_def::ConvertibleToWasm; r.to_typed_value() }))
});
( $args_iter:ident, $ctx:ident, ( $( $names:ident : $params:ty ),* ) => $body:tt ) => ({
let body = $crate::wasm::env_def::macros::constrain_closure::<(), _>(|| {
unmarshall_then_body!($body, $ctx, $args_iter, $( $names : $params ),*)
});
body()?;
return Ok($crate::sandbox::ReturnValue::Unit)
return Ok(sandbox::ReturnValue::Unit)
})
}
@@ -126,7 +126,7 @@ macro_rules! define_func {
( < E: $ext_ty:tt > $name:ident ( $ctx: ident $(, $names:ident : $params:ty)*) $(-> $returns:ty)* => $body:tt ) => {
fn $name< E: $ext_ty >(
$ctx: &mut $crate::wasm::Runtime<E>,
args: &[$crate::sandbox::TypedValue],
args: &[sandbox::TypedValue],
) -> Result<sandbox::ReturnValue, sandbox::HostError> {
#[allow(unused)]
let mut args = args.iter();
@@ -176,7 +176,7 @@ macro_rules! define_env {
pub struct $init_name;
impl $crate::wasm::env_def::ImportSatisfyCheck for $init_name {
fn can_satisfy(name: &[u8], func_type: &$crate::parity_wasm::elements::FunctionType) -> bool {
fn can_satisfy(name: &[u8], func_type: &parity_wasm::elements::FunctionType) -> bool {
gen_signature_dispatch!( name, func_type ; $( $name ( $ctx $(, $names : $params )* ) $( -> $returns )* , )* );
return false;
@@ -197,10 +197,10 @@ mod tests {
use parity_wasm::elements::ValueType;
use runtime_primitives::traits::{As, Zero};
use sandbox::{self, ReturnValue, TypedValue};
use wasm::tests::MockExt;
use wasm::Runtime;
use exec::Ext;
use Trait;
use crate::wasm::tests::MockExt;
use crate::wasm::Runtime;
use crate::exec::Ext;
use crate::Trait;
#[test]
fn macro_unmarshall_then_body_then_marshall_value_or_trap() {
@@ -304,7 +304,7 @@ mod tests {
#[test]
fn macro_define_env() {
use wasm::env_def::ImportSatisfyCheck;
use crate::wasm::env_def::ImportSatisfyCheck;
define_env!(Env, <E: Ext>,
ext_gas( _ctx, amount: u32 ) => {
@@ -15,9 +15,10 @@
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
use super::Runtime;
use exec::Ext;
use parity_wasm::elements::{FunctionType, ValueType};
use crate::exec::Ext;
use sandbox::{self, TypedValue};
use parity_wasm::elements::{FunctionType, ValueType};
#[macro_use]
pub(crate) mod macros;
@@ -26,7 +27,7 @@ pub trait ConvertibleToWasm: Sized {
const VALUE_TYPE: ValueType;
type NativeType;
fn to_typed_value(self) -> TypedValue;
fn from_typed_value(TypedValue) -> Option<Self>;
fn from_typed_value(_: TypedValue) -> Option<Self>;
}
impl ConvertibleToWasm for i32 {
type NativeType = i32;
+15 -15
View File
@@ -17,13 +17,14 @@
//! This module provides a means for executing contracts
//! represented in wasm.
use codec::Compact;
use exec::{Ext, EmptyOutputBuf, VmExecResult};
use gas::GasMeter;
use crate::{CodeHash, Schedule, Trait};
use crate::wasm::env_def::FunctionImplProvider;
use crate::exec::{Ext, EmptyOutputBuf, VmExecResult};
use crate::gas::GasMeter;
use rstd::prelude::*;
use sandbox;
use wasm::env_def::FunctionImplProvider;
use {CodeHash, Schedule, Trait};
#[macro_use]
mod env_def;
@@ -72,7 +73,7 @@ impl<'a, T: Trait> WasmLoader<'a, T> {
}
}
impl<'a, T: Trait> ::exec::Loader<T> for WasmLoader<'a, T> {
impl<'a, T: Trait> crate::exec::Loader<T> for WasmLoader<'a, T> {
type Executable = WasmExecutable;
fn load_init(&self, code_hash: &CodeHash<T>) -> Result<WasmExecutable, &'static str> {
@@ -102,7 +103,7 @@ impl<'a, T: Trait> WasmVm<'a, T> {
}
}
impl<'a, T: Trait> ::exec::Vm<T> for WasmVm<'a, T> {
impl<'a, T: Trait> crate::exec::Vm<T> for WasmVm<'a, T> {
type Executable = WasmExecutable;
fn execute<E: Ext<T = T>>(
@@ -173,13 +174,12 @@ mod tests {
use super::*;
use std::collections::HashMap;
use substrate_primitives::H256;
use exec::{CallReceipt, Ext, InstantiateReceipt, EmptyOutputBuf};
use balances;
use gas::GasMeter;
use tests::{Test, Call};
use crate::exec::{CallReceipt, Ext, InstantiateReceipt, EmptyOutputBuf};
use crate::gas::GasMeter;
use crate::tests::{Test, Call};
use wabt;
use wasm::prepare::prepare_contract;
use CodeHash;
use crate::wasm::prepare::prepare_contract;
use crate::CodeHash;
#[derive(Debug, PartialEq, Eq)]
struct DispatchEntry(Call);
@@ -276,10 +276,10 @@ mod tests {
ext: &mut E,
gas_meter: &mut GasMeter<E::T>,
) -> Result<(), &'static str> {
use exec::Vm;
use crate::exec::Vm;
let wasm = wabt::wat2wasm(wat).unwrap();
let schedule = ::Schedule::<u64>::default();
let schedule = crate::Schedule::<u64>::default();
let prefab_module =
prepare_contract::<Test, super::runtime::Env>(&wasm, &schedule).unwrap();
+7 -5
View File
@@ -18,14 +18,15 @@
//! wasm module before execution. It also extracts some essential information
//! from a module.
use crate::wasm::env_def::ImportSatisfyCheck;
use crate::wasm::PrefabWasmModule;
use crate::{Schedule, Trait};
use parity_wasm::elements::{self, Internal, External, MemoryType, Type};
use pwasm_utils;
use pwasm_utils::rules;
use rstd::prelude::*;
use runtime_primitives::traits::As;
use wasm::env_def::ImportSatisfyCheck;
use wasm::PrefabWasmModule;
use {Schedule, Trait};
struct ContractModule<'a, Gas: 'a> {
// An `Option` is used here for loaning (`take()`-ing) the module.
@@ -316,10 +317,11 @@ pub fn prepare_contract<T: Trait, C: ImportSatisfyCheck>(
#[cfg(test)]
mod tests {
use super::*;
use crate::tests::Test;
use crate::exec::Ext;
use std::fmt;
use tests::Test;
use exec::Ext;
use wabt;
use assert_matches::assert_matches;
impl fmt::Debug for PrefabWasmModule {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+5 -6
View File
@@ -16,16 +16,15 @@
//! Environment definition of the wasm smart-contract runtime.
use super::{Schedule};
use exec::{Ext, BalanceOf, VmExecResult, OutputBuf, EmptyOutputBuf, CallReceipt, InstantiateReceipt};
use crate::{Schedule, Trait, CodeHash, ComputeDispatchFee};
use crate::exec::{Ext, BalanceOf, VmExecResult, OutputBuf, EmptyOutputBuf, CallReceipt, InstantiateReceipt};
use crate::gas::{GasMeter, Token, GasMeterResult, approx_gas_for_balance};
use sandbox;
use system;
use rstd::prelude::*;
use rstd::mem;
use codec::{Decode, Encode};
use gas::{GasMeter, Token, GasMeterResult, approx_gas_for_balance};
use runtime_primitives::traits::{As, CheckedMul, Bounded};
use sandbox;
use system;
use {Trait, CodeHash, ComputeDispatchFee};
/// Enumerates all possible *special* trap conditions.
///