Phase 1 of repo reorg (#719)

* Remove unneeded script

* Rename Substrate Demo -> Substrate

* Rename demo -> node

* Build wasm from last rename.

* Merge ed25519 into substrate-primitives

* Minor tweak

* Rename substrate -> core

* Move substrate-runtime-support to core/runtime/support

* Rename/move substrate-runtime-version

* Move codec up a level

* Rename substrate-codec -> parity-codec

* Move environmental up a level

* Move pwasm-* up to top, ready for removal

* Remove requirement of s-r-support from s-r-primitives

* Move core/runtime/primitives into core/runtime-primitives

* Remove s-r-support dep from s-r-version

* Remove dep of s-r-support from bft

* Remove dep of s-r-support from node/consensus

* Sever all other core deps from s-r-support

* Forgot the no_std directive

* Rename non-SRML modules to sr-* to avoid match clashes

* Move runtime/* to srml/*

* Rename substrate-runtime-* -> srml-*

* Move srml to top-level
This commit is contained in:
Gav Wood
2018-09-12 11:13:31 +02:00
committed by Arkadiy Paronyan
parent 8fe5aa4c81
commit 1e01162505
374 changed files with 2845 additions and 2902 deletions
+180
View File
@@ -0,0 +1,180 @@
// Copyright 2018 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Auxilliaries to help with managing partial changes to accounts state.
use super::{CodeOf, StorageOf, Trait};
use double_map::StorageDoubleMap;
use rstd::cell::RefCell;
use rstd::collections::btree_map::{BTreeMap, Entry};
use rstd::prelude::*;
use runtime_support::StorageMap;
use {balances, system};
pub struct ChangeEntry<T: Trait> {
balance: Option<T::Balance>,
code: Option<Vec<u8>>,
storage: BTreeMap<Vec<u8>, Option<Vec<u8>>>,
}
// Cannot derive(Default) since it erroneously bounds T by Default.
impl<T: Trait> Default for ChangeEntry<T> {
fn default() -> Self {
ChangeEntry {
balance: Default::default(),
code: Default::default(),
storage: Default::default(),
}
}
}
pub type ChangeSet<T> = BTreeMap<<T as system::Trait>::AccountId, ChangeEntry<T>>;
pub trait AccountDb<T: Trait> {
fn get_storage(&self, account: &T::AccountId, location: &[u8]) -> Option<Vec<u8>>;
fn get_code(&self, account: &T::AccountId) -> Vec<u8>;
fn get_balance(&self, account: &T::AccountId) -> T::Balance;
fn commit(&mut self, change_set: ChangeSet<T>);
}
pub struct DirectAccountDb;
impl<T: Trait> AccountDb<T> for DirectAccountDb {
fn get_storage(&self, account: &T::AccountId, location: &[u8]) -> Option<Vec<u8>> {
<StorageOf<T>>::get(account.clone(), location.to_vec())
}
fn get_code(&self, account: &T::AccountId) -> Vec<u8> {
<CodeOf<T>>::get(account)
}
fn get_balance(&self, account: &T::AccountId) -> T::Balance {
balances::Module::<T>::free_balance(account)
}
fn commit(&mut self, s: ChangeSet<T>) {
for (address, changed) in s.into_iter() {
if let Some(balance) = changed.balance {
if let balances::UpdateBalanceOutcome::AccountKilled =
balances::Module::<T>::set_free_balance_creating(&address, balance)
{
// Account killed. This will ultimately lead to calling `OnFreeBalanceZero` callback
// which will make removal of CodeOf and StorageOf for this account.
// In order to avoid writing over the deleted properties we `continue` here.
continue;
}
}
if let Some(code) = changed.code {
<CodeOf<T>>::insert(&address, &code);
}
for (k, v) in changed.storage.into_iter() {
if let Some(value) = v {
<StorageOf<T>>::insert(address.clone(), k, value);
} else {
<StorageOf<T>>::remove(address.clone(), k);
}
}
}
}
}
pub struct OverlayAccountDb<'a, T: Trait + 'a> {
local: RefCell<ChangeSet<T>>,
underlying: &'a AccountDb<T>,
}
impl<'a, T: Trait> OverlayAccountDb<'a, T> {
pub fn new(underlying: &'a AccountDb<T>) -> OverlayAccountDb<'a, T> {
OverlayAccountDb {
local: RefCell::new(ChangeSet::new()),
underlying,
}
}
pub fn into_change_set(self) -> ChangeSet<T> {
self.local.into_inner()
}
pub fn set_storage(
&mut self,
account: &T::AccountId,
location: Vec<u8>,
value: Option<Vec<u8>>,
) {
self.local
.borrow_mut()
.entry(account.clone())
.or_insert(Default::default())
.storage
.insert(location, value);
}
pub fn set_code(&mut self, account: &T::AccountId, code: Vec<u8>) {
self.local
.borrow_mut()
.entry(account.clone())
.or_insert(Default::default())
.code = Some(code);
}
pub fn set_balance(&mut self, account: &T::AccountId, balance: T::Balance) {
self.local
.borrow_mut()
.entry(account.clone())
.or_insert(Default::default())
.balance = Some(balance);
}
}
impl<'a, T: Trait> AccountDb<T> for OverlayAccountDb<'a, T> {
fn get_storage(&self, account: &T::AccountId, location: &[u8]) -> Option<Vec<u8>> {
self.local
.borrow()
.get(account)
.and_then(|a| a.storage.get(location))
.cloned()
.unwrap_or_else(|| self.underlying.get_storage(account, location))
}
fn get_code(&self, account: &T::AccountId) -> Vec<u8> {
self.local
.borrow()
.get(account)
.and_then(|a| a.code.clone())
.unwrap_or_else(|| self.underlying.get_code(account))
}
fn get_balance(&self, account: &T::AccountId) -> T::Balance {
self.local
.borrow()
.get(account)
.and_then(|a| a.balance)
.unwrap_or_else(|| self.underlying.get_balance(account))
}
fn commit(&mut self, s: ChangeSet<T>) {
let mut local = self.local.borrow_mut();
for (address, changed) in s.into_iter() {
match local.entry(address) {
Entry::Occupied(e) => {
let mut value = e.into_mut();
if changed.balance.is_some() {
value.balance = changed.balance;
}
if changed.code.is_some() {
value.code = changed.code;
}
value.storage.extend(changed.storage.into_iter());
}
Entry::Vacant(e) => {
e.insert(changed);
}
}
}
}
}
+90
View File
@@ -0,0 +1,90 @@
// Copyright 2017 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! An implementation of double map backed by storage.
//!
//! This implementation is somewhat specialized to the tracking of the storage of accounts.
use rstd::prelude::*;
use codec::{Codec, Encode};
use runtime_support::storage::unhashed;
use runtime_io::{blake2_256, twox_128};
/// Returns only a first part of the storage key.
///
/// Hashed by XX.
fn first_part_of_key<M: StorageDoubleMap + ?Sized>(k1: M::Key1) -> [u8; 16] {
let mut raw_prefix = Vec::new();
raw_prefix.extend(M::PREFIX);
raw_prefix.extend(Encode::encode(&k1));
twox_128(&raw_prefix)
}
/// Returns a compound key that consist of the two parts: (prefix, `k1`) and `k2`.
///
/// The first part is hased by XX and then concatenated with a blake2 hash of `k2`.
fn full_key<M: StorageDoubleMap + ?Sized>(k1: M::Key1, k2: M::Key2) -> Vec<u8> {
let first_part = first_part_of_key::<M>(k1);
let second_part = blake2_256(&Encode::encode(&k2));
let mut k = Vec::new();
k.extend(&first_part);
k.extend(&second_part);
k
}
/// An implementation of a map with a two keys.
///
/// It provides an important ability to efficiently remove all entries
/// that have a common first key.
///
/// # Mapping of keys to a storage path
///
/// The storage key (i.e. the key under which the `Value` will be stored) is created from two parts.
/// The first part is a XX hash of a concatenation of the `PREFIX` and `Key1`. And the second part
/// is a blake2 hash of a `Key2`.
///
/// Blake2 is used for `Key2` is because it will be used as a key for contract's storage and
/// thus will be susceptible for a untrusted input.
pub trait StorageDoubleMap {
type Key1: Codec;
type Key2: Codec;
type Value: Codec + Default;
const PREFIX: &'static [u8];
/// Insert an entry into this map.
fn insert(k1: Self::Key1, k2: Self::Key2, val: Self::Value) {
unhashed::put(&full_key::<Self>(k1, k2)[..], &val);
}
/// Remove an entry from this map.
fn remove(k1: Self::Key1, k2: Self::Key2) {
unhashed::kill(&full_key::<Self>(k1, k2)[..]);
}
/// Get an entry from this map.
///
/// If there is entry stored under the given keys, returns `None`.
fn get(k1: Self::Key1, k2: Self::Key2) -> Option<Self::Value> {
unhashed::get(&full_key::<Self>(k1, k2)[..])
}
/// Removes all entries that shares the `k1` as the first key.
fn remove_prefix(k1: Self::Key1) {
unhashed::kill_prefix(&first_part_of_key::<Self>(k1))
}
}
+266
View File
@@ -0,0 +1,266 @@
// Copyright 2018 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
use super::{CodeOf, MaxDepth, ContractAddressFor, Module, Trait};
use account_db::{AccountDb, OverlayAccountDb};
use gas::GasMeter;
use vm;
use rstd::prelude::*;
use runtime_primitives::traits::{Zero, CheckedAdd, CheckedSub};
use runtime_support::{StorageMap, StorageValue};
use balances::{self, EnsureAccountLiquid};
pub struct CreateReceipt<T: Trait> {
pub address: T::AccountId,
}
pub struct CallReceipt {
pub return_data: Vec<u8>,
}
pub struct ExecutionContext<'a, T: Trait + 'a> {
// typically should be dest
pub self_account: T::AccountId,
pub overlay: OverlayAccountDb<'a, T>,
pub depth: usize,
}
impl<'a, T: Trait> ExecutionContext<'a, T> {
/// Make a call to the specified address.
pub fn call(
&mut self,
caller: T::AccountId,
dest: T::AccountId,
value: T::Balance,
gas_meter: &mut GasMeter<T>,
data: &[u8],
) -> Result<CallReceipt, &'static str> {
if self.depth == <MaxDepth<T>>::get() as usize {
return Err("reached maximum depth, cannot make a call");
}
let call_base_fee = <Module<T>>::call_base_fee();
if gas_meter.charge(call_base_fee).is_out_of_gas() {
return Err("not enough gas to pay base call fee");
}
let dest_code = <CodeOf<T>>::get(&dest);
let (exec_result, change_set) = {
let mut overlay = OverlayAccountDb::new(&self.overlay);
if value > T::Balance::zero() {
transfer(
gas_meter,
false,
&self.self_account,
&dest,
value,
&mut overlay,
)?;
}
let mut nested = ExecutionContext {
overlay: overlay,
self_account: dest.clone(),
depth: self.depth + 1,
};
let exec_result = if !dest_code.is_empty() {
vm::execute(
&dest_code,
data,
&mut CallContext {
ctx: &mut nested,
_caller: caller,
},
gas_meter,
).map_err(|_| "vm execute returned error while call")?
} else {
// that was a plain transfer
vm::ExecutionResult {
return_data: Vec::new(),
}
};
(exec_result, nested.overlay.into_change_set())
};
self.overlay.commit(change_set);
Ok(CallReceipt {
return_data: exec_result.return_data,
})
}
pub fn create(
&mut self,
caller: T::AccountId,
endowment: T::Balance,
gas_meter: &mut GasMeter<T>,
ctor: &[u8],
data: &[u8],
) -> Result<CreateReceipt<T>, &'static str> {
if self.depth == <MaxDepth<T>>::get() as usize {
return Err("reached maximum depth, cannot create");
}
let create_base_fee = <Module<T>>::create_base_fee();
if gas_meter.charge(create_base_fee).is_out_of_gas() {
return Err("not enough gas to pay base create fee");
}
let dest = T::DetermineContractAddress::contract_address_for(ctor, data, &self.self_account);
if <CodeOf<T>>::exists(&dest) {
// TODO: Is it enough?
return Err("contract already exists");
}
let change_set = {
let mut overlay = OverlayAccountDb::new(&self.overlay);
if endowment > T::Balance::zero() {
transfer(
gas_meter,
true,
&self.self_account,
&dest,
endowment,
&mut overlay,
)?;
}
let mut nested = ExecutionContext {
overlay: overlay,
self_account: dest.clone(),
depth: self.depth + 1,
};
let exec_result = {
vm::execute(
ctor,
data,
&mut CallContext {
ctx: &mut nested,
_caller: caller,
},
gas_meter,
).map_err(|_| "vm execute returned error while create")?
};
nested.overlay.set_code(&dest, exec_result.return_data);
nested.overlay.into_change_set()
};
self.overlay.commit(change_set);
Ok(CreateReceipt {
address: dest,
})
}
}
fn transfer<T: Trait>(
gas_meter: &mut GasMeter<T>,
contract_create: bool,
transactor: &T::AccountId,
dest: &T::AccountId,
value: T::Balance,
overlay: &mut OverlayAccountDb<T>,
) -> Result<(), &'static str> {
let would_create = overlay.get_balance(transactor).is_zero();
let fee: T::Balance = if contract_create {
<Module<T>>::contract_fee()
} else {
if would_create {
<balances::Module<T>>::creation_fee()
} else {
<balances::Module<T>>::transfer_fee()
}
};
if gas_meter.charge_by_balance(fee).is_out_of_gas() {
return Err("not enough gas to pay transfer fee");
}
let from_balance = overlay.get_balance(transactor);
let new_from_balance = match from_balance.checked_sub(&value) {
Some(b) => b,
None => return Err("balance too low to send value"),
};
if would_create && value < <balances::Module<T>>::existential_deposit() {
return Err("value too low to create account");
}
<T as balances::Trait>::EnsureAccountLiquid::ensure_account_liquid(transactor)?;
let to_balance = overlay.get_balance(dest);
let new_to_balance = match to_balance.checked_add(&value) {
Some(b) => b,
None => return Err("destination balance too high to receive value"),
};
if transactor != dest {
overlay.set_balance(transactor, new_from_balance);
overlay.set_balance(dest, new_to_balance);
}
Ok(())
}
struct CallContext<'a, 'b: 'a, T: Trait + 'b> {
ctx: &'a mut ExecutionContext<'b, T>,
_caller: T::AccountId,
}
impl<'a, 'b: 'a, T: Trait + 'b> vm::Ext for CallContext<'a, 'b, T> {
type T = T;
fn get_storage(&self, key: &[u8]) -> Option<Vec<u8>> {
self.ctx.overlay.get_storage(&self.ctx.self_account, key)
}
fn set_storage(&mut self, key: &[u8], value: Option<Vec<u8>>) {
self.ctx
.overlay
.set_storage(&self.ctx.self_account, key.to_vec(), value)
}
fn create(
&mut self,
code: &[u8],
endowment: T::Balance,
gas_meter: &mut GasMeter<T>,
data: &[u8],
) -> Result<CreateReceipt<T>, ()> {
let caller = self.ctx.self_account.clone();
self.ctx
.create(caller, endowment, gas_meter, code, &data)
.map_err(|_| ())
}
fn call(
&mut self,
to: &T::AccountId,
value: T::Balance,
gas_meter: &mut GasMeter<T>,
data: &[u8],
) -> Result<CallReceipt, ()> {
let caller = self.ctx.self_account.clone();
self.ctx
.call(caller, to.clone(), value, gas_meter, data)
.map_err(|_| ())
}
}
+178
View File
@@ -0,0 +1,178 @@
// Copyright 2018 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
use {Trait, Module, GasSpent};
use runtime_primitives::traits::{As, CheckedMul, CheckedSub, Zero};
use runtime_support::StorageValue;
use balances;
#[must_use]
#[derive(Debug, PartialEq, Eq)]
pub enum GasMeterResult {
Proceed,
OutOfGas,
}
impl GasMeterResult {
pub fn is_out_of_gas(&self) -> bool {
match *self {
GasMeterResult::OutOfGas => true,
GasMeterResult::Proceed => false,
}
}
}
pub struct GasMeter<T: Trait> {
limit: T::Gas,
/// Amount of gas left from initial gas limit. Can reach zero.
gas_left: T::Gas,
gas_price: T::Balance,
}
impl<T: Trait> GasMeter<T> {
#[cfg(test)]
pub fn with_limit(gas_limit: T::Gas, gas_price: T::Balance) -> GasMeter<T> {
GasMeter {
limit: gas_limit,
gas_left: gas_limit,
gas_price,
}
}
/// Account for used gas.
///
/// Returns `OutOfGas` if there is not enough gas or addition of the specified
/// amount of gas has lead to overflow. On success returns `Proceed`.
///
/// NOTE that `amount` is always consumed, i.e. if there is not enough gas
/// then the counter will be set to zero.
pub fn charge(&mut self, amount: T::Gas) -> GasMeterResult {
let new_value = match self.gas_left.checked_sub(&amount) {
None => None,
Some(val) if val.is_zero() => None,
Some(val) => Some(val),
};
// We always consume the gas even if there is not enough gas.
self.gas_left = new_value.unwrap_or_else(Zero::zero);
match new_value {
Some(_) => GasMeterResult::Proceed,
None => GasMeterResult::OutOfGas,
}
}
/// Account for used gas expressed in balance units.
///
/// Same as [`charge`], but amount to be charged is converted from units of balance to
/// units of gas.
///
/// [`charge`]: #method.charge
pub fn charge_by_balance(&mut self, amount: T::Balance) -> GasMeterResult {
let amount_in_gas: T::Balance = amount / self.gas_price;
let amount_in_gas: T::Gas = <T::Gas as As<T::Balance>>::sa(amount_in_gas);
self.charge(amount_in_gas)
}
/// Allocate some amount of gas and perform some work with
/// a newly created nested gas meter.
///
/// Invokes `f` with either the gas meter that has `amount` gas left or
/// with `None`, if this gas meter has not enough gas to allocate given `amount`.
///
/// All unused gas in the nested gas meter is returned to this gas meter.
pub fn with_nested<R, F: FnOnce(Option<&mut GasMeter<T>>) -> R>(
&mut self,
amount: T::Gas,
f: F,
) -> R {
// NOTE that it is ok to allocate all available gas since it still ensured
// by `charge` that it doesn't reach zero.
if self.gas_left < amount {
f(None)
} else {
self.gas_left = self.gas_left - amount;
let mut nested = GasMeter {
limit: amount,
gas_left: amount,
gas_price: self.gas_price,
};
let r = f(Some(&mut nested));
self.gas_left = self.gas_left + nested.gas_left;
r
}
}
/// Returns how much gas left from the initial budget.
pub fn gas_left(&self) -> T::Gas {
self.gas_left
}
/// Returns how much gas was spent.
fn spent(&self) -> T::Gas {
self.limit - self.gas_left
}
}
/// Buy the given amount of gas.
///
/// Cost is calculated by multiplying the gas cost (taken from the storage) by the `gas_limit`.
/// The funds are deducted from `transactor`.
pub fn buy_gas<T: Trait>(
transactor: &T::AccountId,
gas_limit: T::Gas,
) -> Result<GasMeter<T>, &'static str> {
// Check if the specified amount of gas is available in the current block.
// This cannot underflow since `gas_spent` is never greater than `block_gas_limit`.
let gas_available = <Module<T>>::block_gas_limit() - <Module<T>>::gas_spent();
if gas_limit > gas_available {
return Err("block gas limit is reached");
}
// Buy the specified amount of gas.
let gas_price = <Module<T>>::gas_price();
let b = <balances::Module<T>>::free_balance(transactor);
let cost = <T::Gas as As<T::Balance>>::as_(gas_limit.clone())
.checked_mul(&gas_price)
.ok_or("overflow multiplying gas limit by price")?;
if b < cost + <balances::Module<T>>::existential_deposit() {
return Err("not enough funds for transaction fee");
}
<balances::Module<T>>::set_free_balance(transactor, b - cost);
<balances::Module<T>>::decrease_total_stake_by(cost);
Ok(GasMeter {
limit: gas_limit,
gas_left: gas_limit,
gas_price,
})
}
/// Refund the unused gas.
pub fn refund_unused_gas<T: Trait>(transactor: &T::AccountId, gas_meter: GasMeter<T>) {
// Increase total spent gas.
// This cannot overflow, since `gas_spent` is never greater than `block_gas_limit`, which
// also has T::Gas type.
let gas_spent = <Module<T>>::gas_spent() + gas_meter.spent();
<GasSpent<T>>::put(gas_spent);
// Refund gas left by the price it was bought.
let b = <balances::Module<T>>::free_balance(transactor);
let refund = <T::Gas as As<T::Balance>>::as_(gas_meter.gas_left) * gas_meter.gas_price;
<balances::Module<T>>::set_free_balance(transactor, b + refund);
<balances::Module<T>>::increase_total_stake_by(refund);
}
@@ -0,0 +1,68 @@
// Copyright 2018 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Build the contract module part of the genesis block storage.
#![cfg(feature = "std")]
use {Trait, ContractFee, CallBaseFee, CreateBaseFee, GasPrice, MaxDepth, BlockGasLimit};
use runtime_primitives;
use runtime_primitives::traits::As;
use runtime_io::{self, twox_128};
use runtime_support::StorageValue;
use codec::Encode;
use std::collections::HashMap;
use substrate_primitives::Blake2Hasher;
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct GenesisConfig<T: Trait> {
pub contract_fee: T::Balance,
pub call_base_fee: T::Gas,
pub create_base_fee: T::Gas,
pub gas_price: T::Balance,
pub max_depth: u32,
pub block_gas_limit: T::Gas,
}
impl<T: Trait> Default for GenesisConfig<T> {
fn default() -> Self {
GenesisConfig {
contract_fee: T::Balance::sa(21),
call_base_fee: T::Gas::sa(135),
create_base_fee: T::Gas::sa(175),
gas_price: T::Balance::sa(1),
max_depth: 100,
block_gas_limit: T::Gas::sa(1_000_000),
}
}
}
impl<T: Trait> runtime_primitives::BuildStorage for GenesisConfig<T> {
fn build_storage(self) -> ::std::result::Result<HashMap<Vec<u8>, Vec<u8>>, String> {
let r: runtime_io::TestExternalities<Blake2Hasher> = map![
twox_128(<ContractFee<T>>::key()).to_vec() => self.contract_fee.encode(),
twox_128(<CallBaseFee<T>>::key()).to_vec() => self.call_base_fee.encode(),
twox_128(<CreateBaseFee<T>>::key()).to_vec() => self.create_base_fee.encode(),
twox_128(<GasPrice<T>>::key()).to_vec() => self.gas_price.encode(),
twox_128(<MaxDepth<T>>::key()).to_vec() => self.max_depth.encode(),
twox_128(<BlockGasLimit<T>>::key()).to_vec() => self.block_gas_limit.encode()
];
Ok(r.into())
}
}
+304
View File
@@ -0,0 +1,304 @@
// Copyright 2018 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Smart-contract module for runtime; Allows deployment and execution of smart-contracts
//! expressed in WebAssembly.
//!
//! This module provides an ability to create smart-contract accounts and send them messages.
//! A smart-contract is an account with associated code and storage. When such an account receives a message,
//! the code associated with that account gets executed.
//!
//! The code is allowed to alter the storage entries of the associated account,
//! create smart-contracts or send messages to existing smart-contracts.
//!
//! For any actions invoked by the smart-contracts fee must be paid. The fee is paid in gas.
//! Gas is bought upfront up to the, specified in transaction, limit. Any unused gas is refunded
//! after the transaction (regardless of the execution outcome). If all gas is used,
//! then changes made for the specific call or create are reverted (including balance transfers).
//!
//! Failures are typically not cascading. That, for example, means that if contract A calls B and B errors
//! somehow, then A can decide if it should proceed or error.
//!
//! # Interaction with the system
//!
//! ## Finalization
//!
//! This module requires performing some finalization steps at the end of the block. If not performed
//! the module will have incorrect behavior.
//!
//! Call [`Module::execute`] at the end of the block. The order in relation to
//! the other module doesn't matter.
//!
//! ## Account killing
//!
//! When `staking` module determines that account is dead (e.g. account's balance fell below
//! exsistential deposit) then it reaps the account. That will lead to deletion of the associated
//! code and storage of the account.
//!
//! [`Module::execute`]: struct.Module.html#impl-OnFinalise
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "std")]
#[macro_use]
extern crate serde_derive;
#[cfg(feature = "std")]
extern crate serde;
extern crate parity_wasm;
extern crate pwasm_utils;
extern crate parity_codec as codec;
extern crate sr_io as runtime_io;
extern crate sr_sandbox as sandbox;
#[macro_use]
extern crate sr_std as rstd;
extern crate srml_balances as balances;
extern crate srml_system as system;
#[macro_use]
extern crate srml_support as runtime_support;
extern crate sr_primitives as runtime_primitives;
extern crate substrate_primitives;
#[cfg(test)]
#[macro_use]
extern crate assert_matches;
#[cfg(test)]
extern crate wabt;
mod account_db;
mod double_map;
mod exec;
mod vm;
mod gas;
mod genesis_config;
#[cfg(test)]
mod tests;
#[cfg(feature = "std")]
pub use genesis_config::GenesisConfig;
use exec::ExecutionContext;
use account_db::{AccountDb, OverlayAccountDb};
use double_map::StorageDoubleMap;
use rstd::prelude::*;
use rstd::marker::PhantomData;
use codec::Codec;
use runtime_primitives::traits::{Hash, As, SimpleArithmetic, OnFinalise};
use runtime_support::dispatch::Result;
use runtime_support::{Parameter, StorageMap, StorageValue};
use system::ensure_signed;
pub trait Trait: balances::Trait {
/// Function type to get the contract address given the creator.
type DetermineContractAddress: ContractAddressFor<Self::AccountId>;
// As<u32> is needed for wasm-utils
type Gas: Parameter + Default + Codec + SimpleArithmetic + Copy + As<Self::Balance> + As<u64> + As<u32>;
}
pub trait ContractAddressFor<AccountId: Sized> {
fn contract_address_for(code: &[u8], data: &[u8], origin: &AccountId) -> AccountId;
}
/// Simple contract address determintator.
///
/// Address calculated from the code (of the constructor), input data to the constructor
/// and account id which requested the account creation.
///
/// Formula: `blake2_256(blake2_256(code) + blake2_256(data) + origin)`
pub struct SimpleAddressDeterminator<T: Trait>(PhantomData<T>);
impl<T: Trait> ContractAddressFor<T::AccountId> for SimpleAddressDeterminator<T>
where
T::AccountId: From<T::Hash> + AsRef<[u8]>
{
fn contract_address_for(code: &[u8], data: &[u8], origin: &T::AccountId) -> T::AccountId {
let code_hash = T::Hashing::hash(code);
let data_hash = T::Hashing::hash(data);
let mut buf = Vec::new();
buf.extend_from_slice(code_hash.as_ref());
buf.extend_from_slice(data_hash.as_ref());
buf.extend_from_slice(origin.as_ref());
T::Hashing::hash(&buf[..]).into()
}
}
decl_module! {
/// Contracts module.
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
// TODO: Change AccountId to staking::Address
fn call(
origin,
dest: T::AccountId,
value: T::Balance,
gas_limit: T::Gas,
data: Vec<u8>
) -> Result;
fn create(
origin,
value: T::Balance,
gas_limit: T::Gas,
ctor: Vec<u8>,
data: Vec<u8>
) -> Result;
}
}
decl_storage! {
trait Store for Module<T: Trait> as Contract {
/// The fee required to create a contract. At least as big as staking's ReclaimRebate.
ContractFee get(contract_fee): required T::Balance;
/// The fee charged for a call into a contract.
CallBaseFee get(call_base_fee): required T::Gas;
/// The fee charged for a create of a contract.
CreateBaseFee get(create_base_fee): required T::Gas;
/// The price of one unit of gas.
GasPrice get(gas_price): required T::Balance;
/// The maximum nesting level of a call/create stack.
MaxDepth get(max_depth): required u32;
/// The maximum amount of gas that could be expended per block.
BlockGasLimit get(block_gas_limit): required T::Gas;
/// Gas spent so far in this block.
GasSpent get(gas_spent): default T::Gas;
/// The code associated with an account.
pub CodeOf: default map [ T::AccountId => Vec<u8> ]; // TODO Vec<u8> values should be optimised to not do a length prefix.
}
}
// TODO: consider storing upper-bound for contract's gas limit in fixed-length runtime
// code in contract itself and use that.
/// The storage items associated with an account/key.
///
/// TODO: keys should also be able to take AsRef<KeyType> to ensure Vec<u8>s can be passed as &[u8]
pub(crate) struct StorageOf<T>(::rstd::marker::PhantomData<T>);
impl<T: Trait> double_map::StorageDoubleMap for StorageOf<T> {
const PREFIX: &'static [u8] = b"con:sto:";
type Key1 = T::AccountId;
type Key2 = Vec<u8>;
type Value = Vec<u8>;
}
impl<T: Trait> Module<T> {
/// Make a call to a specified account, optionally transferring some balance.
fn call(
origin: <T as system::Trait>::Origin,
dest: T::AccountId,
value: T::Balance,
gas_limit: T::Gas,
data: Vec<u8>,
) -> Result {
let origin = ensure_signed(origin)?;
// Pay for the gas upfront.
//
// NOTE: it is very important to avoid any state changes before
// paying for the gas.
let mut gas_meter = gas::buy_gas::<T>(&origin, gas_limit)?;
let mut ctx = ExecutionContext {
self_account: origin.clone(),
depth: 0,
overlay: OverlayAccountDb::<T>::new(&account_db::DirectAccountDb),
};
let result = ctx.call(origin.clone(), dest, value, &mut gas_meter, &data);
if let Ok(_) = result {
// Commit all changes that made it thus far into the persistant storage.
account_db::DirectAccountDb.commit(ctx.overlay.into_change_set());
}
// Refund cost of the unused gas.
//
// NOTE: this should go after the commit to the storage, since the storage changes
// can alter the balance of the caller.
gas::refund_unused_gas::<T>(&origin, gas_meter);
result.map(|_| ())
}
/// Create a new contract, optionally transfering some balance to the created account.
///
/// Creation is executed as follows:ExecutionContext
///
/// - the destination address is computed based on the sender and hash of the code.
/// - account is created at the computed address.
/// - the `ctor_code` is executed in the context of the newly created account. Buffer returned
/// after the execution is saved as the `code` of the account. That code will be invoked
/// upon any message received by this account.
fn create(
origin: <T as system::Trait>::Origin,
endowment: T::Balance,
gas_limit: T::Gas,
ctor_code: Vec<u8>,
data: Vec<u8>,
) -> Result {
let origin = ensure_signed(origin)?;
// Pay for the gas upfront.
//
// NOTE: it is very important to avoid any state changes before
// paying for the gas.
let mut gas_meter = gas::buy_gas::<T>(&origin, gas_limit)?;
let mut ctx = ExecutionContext {
self_account: origin.clone(),
depth: 0,
overlay: OverlayAccountDb::<T>::new(&account_db::DirectAccountDb),
};
let result = ctx.create(origin.clone(), endowment, &mut gas_meter, &ctor_code, &data);
if let Ok(_) = result {
// Commit all changes that made it thus far into the persistant storage.
account_db::DirectAccountDb.commit(ctx.overlay.into_change_set());
}
// Refund cost of the unused gas.
//
// NOTE: this should go after the commit to the storage, since the storage changes
// can alter the balance of the caller.
gas::refund_unused_gas::<T>(&origin, gas_meter);
result.map(|_| ())
}
}
impl<T: Trait> balances::OnFreeBalanceZero<T::AccountId> for Module<T> {
fn on_free_balance_zero(who: &T::AccountId) {
<CodeOf<T>>::remove(who);
<StorageOf<T>>::remove_prefix(who.clone());
}
}
/// Finalization hook for the smart-contract module.
impl<T: Trait> OnFinalise<T::BlockNumber> for Module<T> {
fn on_finalise(_n: T::BlockNumber) {
<GasSpent<T>>::kill();
}
}
+670
View File
@@ -0,0 +1,670 @@
// Copyright 2018 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
use double_map::StorageDoubleMap;
use runtime_io::with_externalities;
use runtime_primitives::testing::{Digest, H256, Header};
use runtime_primitives::traits::{BlakeTwo256};
use runtime_primitives::BuildStorage;
use runtime_support::StorageMap;
use substrate_primitives::Blake2Hasher;
use wabt;
use {
runtime_io, balances, system, CodeOf, ContractAddressFor,
GenesisConfig, Module, StorageOf, Trait,
};
impl_outer_origin! {
pub enum Origin for Test {}
}
#[derive(Clone, Eq, PartialEq)]
pub struct Test;
impl system::Trait for Test {
type Origin = Origin;
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type Hashing = BlakeTwo256;
type Digest = Digest;
type AccountId = u64;
type Header = Header;
type Event = ();
}
impl balances::Trait for Test {
type Balance = u64;
type AccountIndex = u64;
type OnFreeBalanceZero = Contract;
type EnsureAccountLiquid = ();
type Event = ();
}
impl Trait for Test {
type Gas = u64;
type DetermineContractAddress = DummyContractAddressFor;
}
type Balances = balances::Module<Test>;
type Contract = Module<Test>;
pub struct DummyContractAddressFor;
impl ContractAddressFor<u64> for DummyContractAddressFor {
fn contract_address_for(_code: &[u8], _data: &[u8], origin: &u64) -> u64 {
origin + 1
}
}
struct ExtBuilder {
existential_deposit: u64,
gas_price: u64,
block_gas_limit: u64,
}
impl Default for ExtBuilder {
fn default() -> Self {
Self {
existential_deposit: 0,
gas_price: 2,
block_gas_limit: 100_000_000,
}
}
}
impl ExtBuilder {
fn existential_deposit(mut self, existential_deposit: u64) -> Self {
self.existential_deposit = existential_deposit;
self
}
fn gas_price(mut self, gas_price: u64) -> Self {
self.gas_price = gas_price;
self
}
fn block_gas_limit(mut self, block_gas_limit: u64) -> Self {
self.block_gas_limit = block_gas_limit;
self
}
fn build(self) -> runtime_io::TestExternalities<Blake2Hasher> {
let mut t = system::GenesisConfig::<Test>::default()
.build_storage()
.unwrap();
t.extend(
balances::GenesisConfig::<Test> {
balances: vec![],
transaction_base_fee: 0,
transaction_byte_fee: 0,
existential_deposit: self.existential_deposit,
transfer_fee: 0,
creation_fee: 0,
reclaim_rebate: 0,
}.build_storage()
.unwrap(),
);
t.extend(
GenesisConfig::<Test> {
contract_fee: 21,
call_base_fee: 135,
create_base_fee: 175,
gas_price: self.gas_price,
max_depth: 100,
block_gas_limit: self.block_gas_limit,
}.build_storage()
.unwrap(),
);
t.into()
}
}
const CODE_TRANSFER: &str = r#"
(module
;; ext_call(
;; callee_ptr: u32,
;; callee_len: u32,
;; gas: u64,
;; value_ptr: u32,
;; value_len: u32,
;; input_data_ptr: u32,
;; input_data_len: u32
;; ) -> u32
(import "env" "ext_call" (func $ext_call (param i32 i32 i64 i32 i32 i32 i32) (result i32)))
(import "env" "memory" (memory 1 1))
(func (export "call")
(drop
(call $ext_call
(i32.const 4) ;; Pointer to "callee" address.
(i32.const 8) ;; Length of "callee" address.
(i64.const 0) ;; How much gas to devote for the execution. 0 = all.
(i32.const 12) ;; Pointer to the buffer with value to transfer
(i32.const 8) ;; Length of the buffer with value to transfer.
(i32.const 0) ;; Pointer to input data buffer address
(i32.const 0) ;; Length of input data buffer
)
)
)
;; Destination AccountId to transfer the funds.
;; Represented by u64 (8 bytes long) in little endian.
(data (i32.const 4) "\09\00\00\00\00\00\00\00")
;; Amount of value to transfer.
;; Represented by u64 (8 bytes long) in little endian.
(data (i32.const 12) "\06\00\00\00\00\00\00\00")
)
"#;
#[test]
fn contract_transfer() {
const CONTRACT_SHOULD_TRANSFER_VALUE: u64 = 6;
const CONTRACT_SHOULD_TRANSFER_TO: u64 = 9;
let code_transfer = wabt::wat2wasm(CODE_TRANSFER).unwrap();
with_externalities(&mut ExtBuilder::default().build(), || {
<CodeOf<Test>>::insert(1, code_transfer.to_vec());
Balances::set_free_balance(&0, 100_000_000);
Balances::increase_total_stake_by(100_000_000);
Balances::set_free_balance(&1, 11);
Balances::increase_total_stake_by(11);
assert_ok!(Contract::call(Origin::signed(0), 1, 3, 100_000, Vec::new()));
assert_eq!(
Balances::free_balance(&0),
// 3 - value sent with the transaction
// 2 * 10 - gas used by the contract (10) multiplied by gas price (2)
// 2 * 135 - base gas fee for call (by transaction)
// 2 * 135 - base gas fee for call (by the contract)
100_000_000 - 3 - (2 * 10) - (2 * 135) - (2 * 135),
);
assert_eq!(
Balances::free_balance(&1),
11 + 3 - CONTRACT_SHOULD_TRANSFER_VALUE,
);
assert_eq!(
Balances::free_balance(&CONTRACT_SHOULD_TRANSFER_TO),
CONTRACT_SHOULD_TRANSFER_VALUE,
);
});
}
#[test]
fn contract_transfer_oog() {
const CONTRACT_SHOULD_TRANSFER_TO: u64 = 9;
let code_transfer = wabt::wat2wasm(CODE_TRANSFER).unwrap();
with_externalities(&mut ExtBuilder::default().build(), || {
<CodeOf<Test>>::insert(1, code_transfer.to_vec());
Balances::set_free_balance(&0, 100_000_000);
Balances::increase_total_stake_by(100_000_000);
Balances::set_free_balance(&1, 11);
Balances::increase_total_stake_by(11);
assert_ok!(Contract::call(Origin::signed(0), 1, 3, 135 + 135 + 7, Vec::new()));
assert_eq!(
Balances::free_balance(&0),
// 3 - value sent with the transaction
// 2 * 7 - gas used by the contract (7) multiplied by gas price (2)
// 2 * 135 - base gas fee for call (by transaction)
// 2 * 135 - base gas fee for call (by contract)
100_000_000 - 3 - (2 * 7) - (2 * 135) - (2 * 135),
);
// Transaction level transfer should succeed.
assert_eq!(Balances::free_balance(&1), 14);
// But `ext_call` should not.
assert_eq!(Balances::free_balance(&CONTRACT_SHOULD_TRANSFER_TO), 0);
});
}
#[test]
fn contract_transfer_max_depth() {
const CONTRACT_SHOULD_TRANSFER_TO: u64 = 9;
let code_transfer = wabt::wat2wasm(CODE_TRANSFER).unwrap();
with_externalities(&mut ExtBuilder::default().build(), || {
<CodeOf<Test>>::insert(CONTRACT_SHOULD_TRANSFER_TO, code_transfer.to_vec());
Balances::set_free_balance(&0, 100_000_000);
Balances::increase_total_stake_by(100_000_000);
Balances::set_free_balance(&CONTRACT_SHOULD_TRANSFER_TO, 11);
Balances::increase_total_stake_by(11);
assert_ok!(Contract::call(Origin::signed(0), CONTRACT_SHOULD_TRANSFER_TO, 3, 100_000, Vec::new()));
assert_eq!(
Balances::free_balance(&0),
// 3 - value sent with the transaction
// 2 * 10 * 100 - gas used by the contract (10) multiplied by gas price (2)
// multiplied by max depth (100).
// 2 * 135 * 100 - base gas fee for call (by transaction) multiplied by max depth (100).
100_000_000 - 3 - (2 * 10 * 100) - (2 * 135 * 100),
);
assert_eq!(Balances::free_balance(&CONTRACT_SHOULD_TRANSFER_TO), 14);
});
}
/// Convert a byte slice to a string with hex values.
///
/// Each value is preceeded with a `\` character.
fn escaped_bytestring(bytes: &[u8]) -> String {
use std::fmt::Write;
let mut result = String::new();
for b in bytes {
write!(result, "\\{:02x}", b).unwrap();
}
result
}
/// Create a constructor for the specified code.
///
/// When constructor is executed, it will call `ext_return` with code that
/// specified in `child_bytecode`.
fn code_ctor(child_bytecode: &[u8]) -> String {
format!(
r#"
(module
;; ext_return(data_ptr: u32, data_len: u32) -> !
(import "env" "ext_return" (func $ext_return (param i32 i32)))
(import "env" "memory" (memory 1 1))
(func (export "call")
(call $ext_return
(i32.const 4)
(i32.const {code_len})
)
;; ext_return is diverging, i.e. doesn't return.
unreachable
)
(data (i32.const 4) "{escaped_bytecode}")
)
"#,
escaped_bytecode = escaped_bytestring(child_bytecode),
code_len = child_bytecode.len(),
)
}
/// Returns code that uses `ext_create` runtime call.
///
/// Takes bytecode of the contract that needs to be deployed.
fn code_create(constructor: &[u8]) -> String {
format!(
r#"
(module
;; ext_create(
;; code_ptr: u32,
;; code_len: u32,
;; gas: u64,
;; value_ptr: u32,
;; value_len: u32,
;; input_data_ptr: u32,
;; input_data_len: u32,
;; ) -> u32
(import "env" "ext_create" (func $ext_create (param i32 i32 i64 i32 i32 i32 i32) (result i32)))
(import "env" "memory" (memory 1 1))
(func (export "call")
(drop
(call $ext_create
(i32.const 12) ;; Pointer to `code`
(i32.const {code_len}) ;; Length of `code`
(i64.const 0) ;; How much gas to devote for the execution. 0 = all.
(i32.const 4) ;; Pointer to the buffer with value to transfer
(i32.const 8) ;; Length of the buffer with value to transfer
(i32.const 0) ;; Pointer to input data buffer address
(i32.const 0) ;; Length of input data buffer
)
)
)
;; Amount of value to transfer.
;; Represented by u64 (8 bytes long) in little endian.
(data (i32.const 4) "\03\00\00\00\00\00\00\00")
;; Embedded wasm code.
(data (i32.const 12) "{escaped_constructor}")
)
"#,
escaped_constructor = escaped_bytestring(constructor),
code_len = constructor.len(),
)
}
#[test]
fn contract_create() {
let code_transfer = wabt::wat2wasm(CODE_TRANSFER).unwrap();
let code_ctor_transfer = wabt::wat2wasm(&code_ctor(&code_transfer)).unwrap();
let code_create = wabt::wat2wasm(&code_create(&code_ctor_transfer)).unwrap();
with_externalities(&mut ExtBuilder::default().build(), || {
Balances::set_free_balance(&0, 100_000_000);
Balances::increase_total_stake_by(100_000_000);
Balances::set_free_balance(&1, 0);
Balances::set_free_balance(&9, 30);
Balances::increase_total_stake_by(30);
<CodeOf<Test>>::insert(1, code_create.to_vec());
// When invoked, the contract at address `1` must create a contract with 'transfer' code.
assert_ok!(Contract::call(Origin::signed(0), 1, 11, 100_000, Vec::new()));
let derived_address = <Test as Trait>::DetermineContractAddress::contract_address_for(
&code_ctor_transfer,
&[],
&1,
);
// 11 - value sent with the transaction
// 2 * 139 - gas spent by the deployer contract (139) multiplied by gas price (2)
// 2 * 135 - base gas fee for call (top level)
// 2 * 175 - base gas fee for create (by contract)
// ((21 / 2) * 2) - price per account creation
let expected_gas_after_create =
100_000_000 - 11 - (2 * 139) - (2 * 135) - (2 * 175) - ((21 / 2) * 2);
assert_eq!(Balances::free_balance(&0), expected_gas_after_create);
assert_eq!(Balances::free_balance(&1), 8);
assert_eq!(Balances::free_balance(&derived_address), 3);
// Initiate transfer to the newly created contract.
assert_ok!(Contract::call(Origin::signed(0), derived_address, 22, 100_000, Vec::new()));
assert_eq!(
Balances::free_balance(&0),
// 22 - value sent with the transaction
// (2 * 10) - gas used by the contract
// (2 * 135) - base gas fee for call (top level)
// (2 * 135) - base gas fee for call (by transfer contract)
expected_gas_after_create - 22 - (2 * 10) - (2 * 135) - (2 * 135),
);
assert_eq!(Balances::free_balance(&derived_address), 22 - 3);
assert_eq!(Balances::free_balance(&9), 36);
});
}
#[test]
fn top_level_create() {
let code_transfer = wabt::wat2wasm(CODE_TRANSFER).unwrap();
let code_ctor_transfer = wabt::wat2wasm(&code_ctor(&code_transfer)).unwrap();
with_externalities(&mut ExtBuilder::default().gas_price(3).build(), || {
let derived_address = <Test as Trait>::DetermineContractAddress::contract_address_for(
&code_ctor_transfer,
&[],
&0,
);
Balances::set_free_balance(&0, 100_000_000);
Balances::increase_total_stake_by(100_000_000);
Balances::set_free_balance(&derived_address, 30);
Balances::increase_total_stake_by(30);
assert_ok!(Contract::create(
Origin::signed(0),
11,
100_000,
code_ctor_transfer.clone(),
Vec::new(),
));
// 11 - value sent with the transaction
// (3 * 129) - gas spent by the ctor
// (3 * 175) - base gas fee for create (175) (top level) multipled by gas price (3)
// ((21 / 3) * 3) - price for contract creation
assert_eq!(
Balances::free_balance(&0),
100_000_000 - 11 - (3 * 129) - (3 * 175) - ((21 / 3) * 3)
);
assert_eq!(Balances::free_balance(&derived_address), 30 + 11);
assert_eq!(<CodeOf<Test>>::get(&derived_address), code_transfer);
});
}
const CODE_NOP: &'static str = r#"
(module
(func (export "call")
nop
)
)
"#;
#[test]
fn refunds_unused_gas() {
let code_nop = wabt::wat2wasm(CODE_NOP).unwrap();
with_externalities(&mut ExtBuilder::default().build(), || {
<CodeOf<Test>>::insert(1, code_nop.to_vec());
Balances::set_free_balance(&0, 100_000_000);
Balances::increase_total_stake_by(100_000_000);
assert_ok!(Contract::call(Origin::signed(0), 1, 0, 100_000, Vec::new()));
assert_eq!(Balances::free_balance(&0), 100_000_000 - 4 - (2 * 135));
});
}
#[test]
fn call_with_zero_value() {
with_externalities(&mut ExtBuilder::default().build(), || {
<CodeOf<Test>>::insert(1, vec![]);
Balances::set_free_balance(&0, 100_000_000);
Balances::increase_total_stake_by(100_000_000);
assert_ok!(Contract::call(Origin::signed(0), 1, 0, 100_000, Vec::new()));
assert_eq!(Balances::free_balance(&0), 100_000_000 - (2 * 135));
});
}
#[test]
fn create_with_zero_endowment() {
let code_nop = wabt::wat2wasm(CODE_NOP).unwrap();
with_externalities(&mut ExtBuilder::default().build(), || {
Balances::set_free_balance(&0, 100_000_000);
Balances::increase_total_stake_by(100_000_000);
assert_ok!(Contract::create(Origin::signed(0), 0, 100_000, code_nop, Vec::new()));
assert_eq!(
Balances::free_balance(&0),
// 4 - for the gas spent by the constructor
// 2 * 175 - base gas fee for create (175) multiplied by gas price (2) (top level)
100_000_000 - 4 - (2 * 175),
);
});
}
#[test]
fn account_removal_removes_storage() {
with_externalities(
&mut ExtBuilder::default().existential_deposit(100).build(),
|| {
// Setup two accounts with free balance above than exsistential threshold.
{
Balances::set_free_balance(&1, 110);
Balances::increase_total_stake_by(110);
<StorageOf<Test>>::insert(1, b"foo".to_vec(), b"1".to_vec());
<StorageOf<Test>>::insert(1, b"bar".to_vec(), b"2".to_vec());
Balances::set_free_balance(&2, 110);
Balances::increase_total_stake_by(110);
<StorageOf<Test>>::insert(2, b"hello".to_vec(), b"3".to_vec());
<StorageOf<Test>>::insert(2, b"world".to_vec(), b"4".to_vec());
}
// Transfer funds from account 1 of such amount that after this transfer
// the balance of account 1 is will be below than exsistential threshold.
//
// This should lead to the removal of all storage associated with this account.
assert_ok!(Balances::transfer(Origin::signed(1), 2.into(), 20));
// Verify that all entries from account 1 is removed, while
// entries from account 2 is in place.
{
assert_eq!(<StorageOf<Test>>::get(1, b"foo".to_vec()), None);
assert_eq!(<StorageOf<Test>>::get(1, b"bar".to_vec()), None);
assert_eq!(
<StorageOf<Test>>::get(2, b"hello".to_vec()),
Some(b"3".to_vec())
);
assert_eq!(
<StorageOf<Test>>::get(2, b"world".to_vec()),
Some(b"4".to_vec())
);
}
},
);
}
const CODE_UNREACHABLE: &'static str = r#"
(module
(func (export "call")
nop
unreachable
)
)
"#;
#[test]
fn top_level_call_refunds_even_if_fails() {
let code_unreachable = wabt::wat2wasm(CODE_UNREACHABLE).unwrap();
with_externalities(&mut ExtBuilder::default().gas_price(4).build(), || {
<CodeOf<Test>>::insert(1, code_unreachable.to_vec());
Balances::set_free_balance(&0, 100_000_000);
Balances::increase_total_stake_by(100_000_000);
assert_err!(
Contract::call(Origin::signed(0), 1, 0, 100_000, Vec::new()),
"vm execute returned error while call"
);
assert_eq!(Balances::free_balance(&0), 100_000_000 - (4 * 3) - (4 * 135));
});
}
const CODE_LOOP: &'static str = r#"
(module
(func (export "call")
(loop
(br 0)
)
)
)
"#;
#[test]
fn block_gas_limit() {
let code_loop = wabt::wat2wasm(CODE_LOOP).unwrap();
with_externalities(
&mut ExtBuilder::default().block_gas_limit(100_000).build(),
|| {
<CodeOf<Test>>::insert(1, code_loop.to_vec());
Balances::set_free_balance(&0, 100_000_000);
Balances::increase_total_stake_by(100_000_000);
// Spend 50_000 units of gas (OOG).
assert_err!(
Contract::call(Origin::signed(0), 1, 0, 50_000, Vec::new()),
"vm execute returned error while call"
);
// Ensure we can't spend more gas than available in block gas limit.
assert_err!(
Contract::call(Origin::signed(0), 1, 0, 50_001, Vec::new()),
"block gas limit is reached"
);
// However, we can spend another 50_000
assert_err!(
Contract::call(Origin::signed(0), 1, 0, 50_000, Vec::new()),
"vm execute returned error while call"
);
},
);
}
const CODE_INPUT_DATA: &'static str = r#"
(module
(import "env" "ext_input_size" (func $ext_input_size (result i32)))
(import "env" "ext_input_copy" (func $ext_input_copy (param i32 i32 i32)))
(import "env" "memory" (memory 1 1))
(func (export "call")
(block $fail
;; fail if ext_input_size != 4
(br_if $fail
(i32.ne
(i32.const 4)
(call $ext_input_size)
)
)
(call $ext_input_copy
(i32.const 0)
(i32.const 0)
(i32.const 4)
)
(br_if $fail
(i32.ne
(i32.load8_u (i32.const 0))
(i32.const 0)
)
)
(br_if $fail
(i32.ne
(i32.load8_u (i32.const 1))
(i32.const 1)
)
)
(br_if $fail
(i32.ne
(i32.load8_u (i32.const 2))
(i32.const 2)
)
)
(br_if $fail
(i32.ne
(i32.load8_u (i32.const 3))
(i32.const 3)
)
)
(return)
)
unreachable
)
)
"#;
#[test]
fn input_data() {
let code_input_data = wabt::wat2wasm(CODE_INPUT_DATA).unwrap();
with_externalities(
&mut ExtBuilder::default().build(),
|| {
<CodeOf<Test>>::insert(1, code_input_data.to_vec());
Balances::set_free_balance(&0, 100_000_000);
Balances::increase_total_stake_by(100_000_000);
assert_ok!(Contract::call(Origin::signed(0), 1, 0, 50_000, vec![0, 1, 2, 3]));
// all asserts are made within contract code itself.
},
);
}
@@ -0,0 +1,285 @@
// Copyright 2018 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Definition of macros that hides boilerplate of defining external environment
//! for a wasm module.
//!
//! Typically you should use `define_env` macro.
#[macro_export]
macro_rules! convert_args {
() => (vec![]);
( $( $t:ty ),* ) => ( vec![ $( { use $crate::vm::env_def::ConvertibleToWasm; <$t>::VALUE_TYPE }, )* ] );
}
#[macro_export]
macro_rules! gen_signature {
( ( $( $params: ty ),* ) ) => (
{
FunctionType::new(convert_args!($($params),*), None)
}
);
( ( $( $params: ty ),* ) -> $returns: ty ) => (
{
FunctionType::new(convert_args!($($params),*), Some({
use $crate::vm::env_def::ConvertibleToWasm; <$returns>::VALUE_TYPE
}))
}
);
}
/// Unmarshall arguments and then execute `body` expression and return its result.
macro_rules! unmarshall_then_body {
( $body:tt, $ctx:ident, $args_iter:ident, $( $names:ident : $params:ty ),* ) => ({
$(
let $names : <$params as $crate::vm::env_def::ConvertibleToWasm>::NativeType =
$args_iter.next()
.and_then(|v| <$params as $crate::vm::env_def::ConvertibleToWasm>
::from_typed_value(v.clone()))
.expect(
"precondition: all imports should be checked against the signatures of corresponding
functions defined by `define_env!` macro by the user of the macro;
signatures of these functions defined by `$params`;
calls always made with arguments types of which are defined by the corresponding imports;
thus types of arguments should be equal to type list in `$params` and
length of argument list and $params should be equal;
thus this can never be `None`;
qed;
"
);
)*
$body
})
}
/// Since we can't specify the type of closure directly at binding site:
///
/// ```rust,ignore
/// let f: FnOnce() -> Result<<u32 as ConvertibleToWasm>::NativeType, _> = || { /* ... */ };
/// ```
///
/// we use this function to constrain the type of the closure.
#[inline(always)]
pub fn constrain_closure<R, F>(f: F) -> F
where
F: FnOnce() -> Result<R, ::sandbox::HostError>,
{
f
}
#[macro_export]
macro_rules! unmarshall_then_body_then_marshall {
( $args_iter:ident, $ctx:ident, ( $( $names:ident : $params:ty ),* ) -> $returns:ty => $body:tt ) => ({
let body = $crate::vm::env_def::macros::constrain_closure::<
<$returns as $crate::vm::env_def::ConvertibleToWasm>::NativeType, _
>(|| {
unmarshall_then_body!($body, $ctx, $args_iter, $( $names : $params ),*)
});
let r = body()?;
return Ok($crate::sandbox::ReturnValue::Value({ use $crate::vm::env_def::ConvertibleToWasm; r.to_typed_value() }))
});
( $args_iter:ident, $ctx:ident, ( $( $names:ident : $params:ty ),* ) => $body:tt ) => ({
let body = $crate::vm::env_def::macros::constrain_closure::<(), _>(|| {
unmarshall_then_body!($body, $ctx, $args_iter, $( $names : $params ),*)
});
body()?;
return Ok($crate::sandbox::ReturnValue::Unit)
})
}
#[macro_export]
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::vm::Runtime<E>,
args: &[$crate::sandbox::TypedValue],
) -> Result<sandbox::ReturnValue, sandbox::HostError> {
#[allow(unused)]
let mut args = args.iter();
unmarshall_then_body_then_marshall!(
args,
$ctx,
( $( $names : $params ),* ) $( -> $returns )* => $body
)
}
};
}
/// Define a function set that can be imported by executing wasm code.
///
/// **NB**: Be advised that all functions defined by this macro
/// will panic if called with unexpected arguments.
///
/// It's up to the user of this macro to check signatures of wasm code to be executed
/// and reject the code if any imported function has a mismached signature.
macro_rules! define_env {
( $init_name:ident , < E: $ext_ty:tt > ,
$( $name:ident ( $ctx:ident $( , $names:ident : $params:ty )* )
$( -> $returns:ty )* => $body:tt , )*
) => {
pub(crate) fn $init_name<E: Ext>() -> HostFunctionSet<E> {
let mut env = HostFunctionSet::new();
$(
env.funcs.insert(
stringify!( $name ).into(),
HostFunction::new(
gen_signature!( ( $( $params ),* ) $( -> $returns )* ),
{
define_func!(
< E: $ext_ty > $name ( $ctx $(, $names : $params )* ) $( -> $returns )* => $body
);
$name::<E>
},
),
);
)*
env
}
};
}
#[cfg(test)]
mod tests {
use parity_wasm::elements::FunctionType;
use parity_wasm::elements::ValueType;
use runtime_primitives::traits::{As, Zero};
use sandbox::{self, ReturnValue, TypedValue};
use vm::env_def::{HostFunction, HostFunctionSet};
use vm::tests::MockExt;
use vm::{Ext, Runtime};
use Trait;
#[test]
fn macro_unmarshall_then_body_then_marshall_value_or_trap() {
fn test_value(
_ctx: &mut u32,
args: &[sandbox::TypedValue],
) -> Result<ReturnValue, sandbox::HostError> {
let mut args = args.iter();
unmarshall_then_body_then_marshall!(
args,
_ctx,
(a: u32, b: u32) -> u32 => {
if b == 0 {
Err(sandbox::HostError)
} else {
Ok(a / b)
}
}
)
}
let ctx = &mut 0;
assert_eq!(
test_value(ctx, &[TypedValue::I32(15), TypedValue::I32(3)]).unwrap(),
ReturnValue::Value(TypedValue::I32(5)),
);
assert!(test_value(ctx, &[TypedValue::I32(15), TypedValue::I32(0)]).is_err());
}
#[test]
fn macro_unmarshall_then_body_then_marshall_unit() {
fn test_unit(
ctx: &mut u32,
args: &[sandbox::TypedValue],
) -> Result<ReturnValue, sandbox::HostError> {
let mut args = args.iter();
unmarshall_then_body_then_marshall!(
args,
ctx,
(a: u32, b: u32) => {
*ctx = a + b;
Ok(())
}
)
}
let ctx = &mut 0;
let result = test_unit(ctx, &[TypedValue::I32(2), TypedValue::I32(3)]).unwrap();
assert_eq!(result, ReturnValue::Unit);
assert_eq!(*ctx, 5);
}
#[test]
fn macro_define_func() {
define_func!( <E: Ext> ext_gas (_ctx, amount: u32) => {
let amount = <<<E as Ext>::T as Trait>::Gas as As<u32>>::sa(amount);
if !amount.is_zero() {
Ok(())
} else {
Err(sandbox::HostError)
}
});
let _f: fn(&mut Runtime<MockExt>, &[sandbox::TypedValue])
-> Result<sandbox::ReturnValue, sandbox::HostError> = ext_gas::<MockExt>;
}
#[test]
fn macro_gen_signature() {
assert_eq!(
gen_signature!((i32)),
FunctionType::new(vec![ValueType::I32], None),
);
assert_eq!(
gen_signature!( (i32, u32) -> u32 ),
FunctionType::new(vec![ValueType::I32, ValueType::I32], Some(ValueType::I32)),
);
}
#[test]
fn macro_unmarshall_then_body() {
let args = vec![TypedValue::I32(5), TypedValue::I32(3)];
let mut args = args.iter();
let ctx: &mut u32 = &mut 0;
let r = unmarshall_then_body!(
{
*ctx = a + b;
a * b
},
ctx,
args,
a: u32,
b: u32
);
assert_eq!(*ctx, 8);
assert_eq!(r, 15);
}
#[test]
fn macro_define_env() {
define_env!(init_env, <E: Ext>,
ext_gas( _ctx, amount: u32 ) => {
let amount = <<<E as Ext>::T as Trait>::Gas as As<u32>>::sa(amount);
if !amount.is_zero() {
Ok(())
} else {
Err(sandbox::HostError)
}
},
);
let env = init_env::<MockExt>();
assert!(env.funcs.get("ext_gas").is_some());
}
}
@@ -0,0 +1,315 @@
// Copyright 2018 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
use super::{BalanceOf, CallReceipt, CreateReceipt, Ext, GasMeterResult, Runtime};
use codec::Decode;
use parity_wasm::elements::{FunctionType, ValueType};
use rstd::prelude::*;
use rstd::string::String;
use rstd::collections::btree_map::BTreeMap;
use runtime_primitives::traits::As;
use sandbox::{self, TypedValue};
use system;
use Trait;
#[macro_use]
mod macros;
pub trait ConvertibleToWasm: Sized {
const VALUE_TYPE: ValueType;
type NativeType;
fn to_typed_value(self) -> TypedValue;
fn from_typed_value(TypedValue) -> Option<Self>;
}
impl ConvertibleToWasm for i32 {
type NativeType = i32;
const VALUE_TYPE: ValueType = ValueType::I32;
fn to_typed_value(self) -> TypedValue {
TypedValue::I32(self)
}
fn from_typed_value(v: TypedValue) -> Option<Self> {
v.as_i32()
}
}
impl ConvertibleToWasm for u32 {
type NativeType = u32;
const VALUE_TYPE: ValueType = ValueType::I32;
fn to_typed_value(self) -> TypedValue {
TypedValue::I32(self as i32)
}
fn from_typed_value(v: TypedValue) -> Option<Self> {
match v {
TypedValue::I32(v) => Some(v as u32),
_ => None,
}
}
}
impl ConvertibleToWasm for u64 {
type NativeType = u64;
const VALUE_TYPE: ValueType = ValueType::I64;
fn to_typed_value(self) -> TypedValue {
TypedValue::I64(self as i64)
}
fn from_typed_value(v: TypedValue) -> Option<Self> {
match v {
TypedValue::I64(v) => Some(v as u64),
_ => None,
}
}
}
/// Represents a set of function that defined in this particular environment and
/// which can be imported and called by the module.
pub(crate) struct HostFunctionSet<E: Ext> {
/// Functions which defined in the environment.
pub funcs: BTreeMap<String, HostFunction<E>>,
}
impl<E: Ext> HostFunctionSet<E> {
pub fn new() -> Self {
HostFunctionSet {
funcs: BTreeMap::new(),
}
}
}
pub(crate) struct HostFunction<E: Ext> {
pub(crate) f: fn(&mut Runtime<E>, &[sandbox::TypedValue])
-> Result<sandbox::ReturnValue, sandbox::HostError>,
func_type: FunctionType,
}
impl<E: Ext> HostFunction<E> {
/// Create a new instance of a host function.
pub fn new(
func_type: FunctionType,
f: fn(&mut Runtime<E>, &[sandbox::TypedValue])
-> Result<sandbox::ReturnValue, sandbox::HostError>,
) -> Self {
HostFunction { func_type, f }
}
/// Returns a function pointer of this host function.
pub fn raw_fn_ptr(
&self,
) -> fn(&mut Runtime<E>, &[sandbox::TypedValue])
-> Result<sandbox::ReturnValue, sandbox::HostError> {
self.f
}
/// Check if the this function could be invoked with the given function signature.
pub fn func_type_matches(&self, func_type: &FunctionType) -> bool {
&self.func_type == func_type
}
}
// TODO: ext_balance, ext_address, ext_callvalue, etc.
// Define a function `fn init_env<E: Ext>() -> HostFunctionSet<E>` that returns
// a function set which can be imported by an executed contract.
define_env!(init_env, <E: Ext>,
// gas(amount: u32)
//
// Account for used gas. Traps if gas used is greater than gas limit.
//
// - amount: How much gas is used.
gas(ctx, amount: u32) => {
let amount = <<<E as Ext>::T as Trait>::Gas as As<u32>>::sa(amount);
match ctx.gas_meter.charge(amount) {
GasMeterResult::Proceed => Ok(()),
GasMeterResult::OutOfGas => Err(sandbox::HostError),
}
},
// ext_put_storage(location_ptr: u32, value_non_null: u32, value_ptr: u32);
//
// Change the value at the given location in storage or remove it.
//
// - location_ptr: pointer into the linear
// memory where the location of the requested value is placed.
// - value_non_null: if set to 0, then the entry
// at the given location will be removed.
// - value_ptr: pointer into the linear memory
// where the value to set is placed. If `value_non_null` is set to 0, then this parameter is ignored.
ext_set_storage(ctx, location_ptr: u32, value_non_null: u32, value_ptr: u32) => {
let mut location = [0; 32];
ctx.memory().get(location_ptr, &mut location)?;
let value = if value_non_null != 0 {
let mut value = [0; 32];
ctx.memory().get(value_ptr, &mut value)?;
Some(value.to_vec())
} else {
None
};
ctx.ext.set_storage(&location, value);
Ok(())
},
// ext_get_storage(location_ptr: u32, dest_ptr: u32);
//
// Retrieve the value at the given location from the strorage.
// If there is no entry at the given location then all-zero-value
// will be returned.
//
// - location_ptr: pointer into the linear
// memory where the location of the requested value is placed.
// - dest_ptr: pointer where contents of the specified storage location
// should be placed.
ext_get_storage(ctx, location_ptr: u32, dest_ptr: u32) => {
let mut location = [0; 32];
ctx.memory().get(location_ptr, &mut location)?;
if let Some(value) = ctx.ext.get_storage(&location) {
ctx.memory().set(dest_ptr, &value)?;
} else {
ctx.memory().set(dest_ptr, &[0u8; 32])?;
}
Ok(())
},
// ext_call(transfer_to_ptr: u32, transfer_to_len: u32, gas: u64, value_ptr: u32, value_len: u32, input_data_ptr: u32, input_data_len: u32)
ext_call(ctx, callee_ptr: u32, callee_len: u32, gas: u64, value_ptr: u32, value_len: u32, input_data_ptr: u32, input_data_len: u32) -> u32 => {
let mut callee = Vec::new();
callee.resize(callee_len as usize, 0);
ctx.memory().get(callee_ptr, &mut callee)?;
let callee =
<<E as Ext>::T as system::Trait>::AccountId::decode(&mut &callee[..])
.ok_or_else(|| sandbox::HostError)?;
let mut value_buf = Vec::new();
value_buf.resize(value_len as usize, 0);
ctx.memory().get(value_ptr, &mut value_buf)?;
let value = BalanceOf::<<E as Ext>::T>::decode(&mut &value_buf[..])
.ok_or_else(|| sandbox::HostError)?;
let mut input_data = Vec::new();
input_data.resize(input_data_len as usize, 0u8);
ctx.memory().get(input_data_ptr, &mut input_data)?;
let nested_gas_limit = if gas == 0 {
ctx.gas_meter.gas_left()
} else {
<<<E as Ext>::T as Trait>::Gas as As<u64>>::sa(gas)
};
let ext = &mut ctx.ext;
let call_outcome = ctx.gas_meter.with_nested(nested_gas_limit, |nested_meter| {
match nested_meter {
Some(nested_meter) => ext.call(&callee, value, nested_meter, &input_data),
// there is not enough gas to allocate for the nested call.
None => Err(()),
}
});
match call_outcome {
// TODO: Find a way how to pass return_data back to the this sandbox.
Ok(CallReceipt { .. }) => Ok(0),
Err(_) => Ok(1),
}
},
// ext_create(code_ptr: u32, code_len: u32, gas: u64, value_ptr: u32, value_len: u32, input_data_ptr: u32, input_data_len: u32) -> u32
ext_create(
ctx, code_ptr: u32,
code_len: u32,
gas: u64,
value_ptr: u32,
value_len: u32,
input_data_ptr: u32,
input_data_len: u32
) -> u32 => {
let mut value_buf = Vec::new();
value_buf.resize(value_len as usize, 0);
ctx.memory().get(value_ptr, &mut value_buf)?;
let value = BalanceOf::<<E as Ext>::T>::decode(&mut &value_buf[..])
.ok_or_else(|| sandbox::HostError)?;
let mut code = Vec::new();
code.resize(code_len as usize, 0u8);
ctx.memory().get(code_ptr, &mut code)?;
let mut input_data = Vec::new();
input_data.resize(input_data_len as usize, 0u8);
ctx.memory().get(input_data_ptr, &mut input_data)?;
let nested_gas_limit = if gas == 0 {
ctx.gas_meter.gas_left()
} else {
<<<E as Ext>::T as Trait>::Gas as As<u64>>::sa(gas)
};
let ext = &mut ctx.ext;
let create_outcome = ctx.gas_meter.with_nested(nested_gas_limit, |nested_meter| {
match nested_meter {
Some(nested_meter) => ext.create(&code, value, nested_meter, &input_data),
// there is not enough gas to allocate for the nested call.
None => Err(()),
}
});
match create_outcome {
// TODO: Copy an address of the created contract in the sandbox.
Ok(CreateReceipt { .. }) => Ok(0),
Err(_) => Ok(1),
}
},
// ext_return(data_ptr: u32, data_len: u32) -> !
ext_return(ctx, data_ptr: u32, data_len: u32) => {
let mut data_buf = Vec::new();
data_buf.resize(data_len as usize, 0);
ctx.memory().get(data_ptr, &mut data_buf)?;
ctx.store_return_data(data_buf)
.map_err(|_| sandbox::HostError)?;
// The trap mechanism is used to immediately terminate the execution.
// This trap should be handled appropriately before returning the result
// to the user of this crate.
Err(sandbox::HostError)
},
// ext_input_size() -> u32
//
// Returns size of an input buffer.
ext_input_size(ctx) -> u32 => {
Ok(ctx.input_data.len() as u32)
},
// ext_input_copy(dest_ptr: u32, offset: u32, len: u32)
//
// Copy data from an input buffer starting from `offset` with length `len` into the contract memory.
// The region at which the data should be put is specified by `dest_ptr`.
ext_input_copy(ctx, dest_ptr: u32, offset: u32, len: u32) => {
let offset = offset as usize;
if offset > ctx.input_data.len() {
// Offset can't be larger than input buffer length.
return Err(sandbox::HostError);
}
// This can't panic since `offset <= ctx.input_data.len()`.
let src = &ctx.input_data[offset..];
if src.len() != len as usize {
return Err(sandbox::HostError);
}
ctx.memory().set(dest_ptr, src)?;
Ok(())
},
);
+553
View File
@@ -0,0 +1,553 @@
// Copyright 2018 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! This module provides a means for executing contracts
//! represented in wasm.
use exec::{CallReceipt, CreateReceipt};
use gas::{GasMeter, GasMeterResult};
use rstd::prelude::*;
use runtime_primitives::traits::{As, CheckedMul};
use {sandbox, balances, system};
use Trait;
type BalanceOf<T> = <T as balances::Trait>::Balance;
type AccountIdOf<T> = <T as system::Trait>::AccountId;
mod prepare;
#[macro_use]
mod env_def;
use self::prepare::{prepare_contract, PreparedContract};
/// An interface that provides an access to the external environment in which the
/// smart-contract is executed.
///
/// This interface is specialised to an account of the executing code, so all
/// operations are implicitly performed on that account.
pub trait Ext {
type T: Trait;
/// Returns the storage entry of the executing account by the given key.
fn get_storage(&self, key: &[u8]) -> Option<Vec<u8>>;
/// Sets the storage entry by the given key to the specified value.
fn set_storage(&mut self, key: &[u8], value: Option<Vec<u8>>);
// TODO: Return the address of the created contract.
/// Create a new account for a contract.
///
/// The newly created account will be associated with the `code`. `value` specifies the amount of value
/// transfered from this to the newly created account.
fn create(
&mut self,
code: &[u8],
value: BalanceOf<Self::T>,
gas_meter: &mut GasMeter<Self::T>,
data: &[u8],
) -> Result<CreateReceipt<Self::T>, ()>;
/// Call (possibly transfering some amount of funds) into the specified account.
fn call(
&mut self,
to: &AccountIdOf<Self::T>,
value: BalanceOf<Self::T>,
gas_meter: &mut GasMeter<Self::T>,
data: &[u8],
) -> Result<CallReceipt, ()>;
}
/// Error that can occur while preparing or executing wasm smart-contract.
#[derive(Debug, PartialEq, Eq)]
pub enum Error {
/// Error happened while serializing the module.
Serialization,
/// Error happened while deserializing the module.
Deserialization,
/// Internal memory declaration has been found in the module.
InternalMemoryDeclared,
/// Gas instrumentation failed.
///
/// This most likely indicates the module isn't valid.
GasInstrumentation,
/// Stack instrumentation failed.
///
/// This most likely indicates the module isn't valid.
StackHeightInstrumentation,
/// Error happened during invocation of the contract's entrypoint.
///
/// Most likely because of trap.
Invoke,
/// Error happened during instantiation.
///
/// This might indicate that `start` function trapped, or module isn't
/// instantiable and/or unlinkable.
Instantiate,
/// Memory creation error.
///
/// This might happen when the memory import has invalid descriptor or
/// requested too much resources.
Memory,
}
/// Enumerates all possible *special* trap conditions.
///
/// In this runtime traps used not only for signaling about errors but also
/// to just terminate quickly in some cases.
enum SpecialTrap {
// TODO: Can we pass wrapped memory instance instead of copying?
/// Signals that trap was generated in response to call `ext_return` host function.
Return(Vec<u8>),
}
pub(crate) struct Runtime<'a, 'data, E: Ext + 'a> {
ext: &'a mut E,
input_data: &'data [u8],
config: &'a Config<E::T>,
memory: sandbox::Memory,
gas_meter: &'a mut GasMeter<E::T>,
special_trap: Option<SpecialTrap>,
}
impl<'a, 'data, E: Ext + 'a> Runtime<'a, 'data, E> {
fn memory(&self) -> &sandbox::Memory {
&self.memory
}
/// Save a data buffer as a result of the execution.
///
/// This function also charges gas for the returning.
///
/// Returns `Err` if there is not enough gas.
fn store_return_data(&mut self, data: Vec<u8>) -> Result<(), ()> {
let data_len = <<<E as Ext>::T as Trait>::Gas as As<u64>>::sa(data.len() as u64);
let price = (self.config.return_data_per_byte_cost)
.checked_mul(&data_len)
.ok_or_else(|| ())?;
match self.gas_meter.charge(price) {
GasMeterResult::Proceed => {
self.special_trap = Some(SpecialTrap::Return(data));
Ok(())
}
GasMeterResult::OutOfGas => Err(()),
}
}
}
fn to_execution_result<E: Ext>(
runtime: Runtime<E>,
run_err: Option<sandbox::Error>,
) -> Result<ExecutionResult, Error> {
// Check the exact type of the error. It could be plain trap or
// special runtime trap the we must recognize.
let return_data = match (run_err, runtime.special_trap) {
// No traps were generated. Proceed normally.
(None, None) => Vec::new(),
// Special case. The trap was the result of the execution `return` host function.
(Some(sandbox::Error::Execution), Some(SpecialTrap::Return(rd))) => rd,
// Any other kind of a trap should result in a failure.
(Some(_), _) => return Err(Error::Invoke),
// Any other case (such as special trap flag without actual trap) signifies
// a logic error.
_ => unreachable!(),
};
Ok(ExecutionResult { return_data })
}
/// The result of execution of a smart-contract.
#[derive(PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct ExecutionResult {
/// The result produced by the execution of the contract.
///
/// The contract can designate some buffer at the execution time via a special function.
/// If contract called this function with non-empty buffer it will be copied here.
///
/// Note that gas is already charged for returning the data.
pub return_data: Vec<u8>,
}
/// Execute the given code as a contract.
pub fn execute<'a, E: Ext>(
code: &[u8],
input_data: &[u8],
ext: &'a mut E,
gas_meter: &mut GasMeter<E::T>,
) -> Result<ExecutionResult, Error> {
let config = Config::default();
let env = env_def::init_env();
let PreparedContract {
instrumented_code,
memory,
} = prepare_contract(code, &config, &env)?;
let mut imports = sandbox::EnvironmentDefinitionBuilder::new();
for (func_name, ext_func) in &env.funcs {
imports.add_host_func("env", &func_name[..], ext_func.raw_fn_ptr());
}
imports.add_memory("env", "memory", memory.clone());
let mut runtime = Runtime {
ext,
input_data,
config: &config,
memory,
gas_meter,
special_trap: None,
};
let mut instance = sandbox::Instance::new(&instrumented_code, &imports, &mut runtime)
.map_err(|_| Error::Instantiate)?;
let run_result = instance.invoke(b"call", &[], &mut runtime);
to_execution_result(runtime, run_result.err())
}
// TODO: Extract it to the root of the crate
#[derive(Clone)]
struct Config<T: Trait> {
/// Gas cost of a growing memory by single page.
grow_mem_cost: T::Gas,
/// Gas cost of a regular operation.
regular_op_cost: T::Gas,
/// Gas cost per one byte returned.
return_data_per_byte_cost: T::Gas,
/// How tall the stack is allowed to grow?
///
/// See https://wiki.parity.io/WebAssembly-StackHeight to find out
/// how the stack frame cost is calculated.
max_stack_height: u32,
//// What is the maximal memory pages amount is allowed to have for
/// a contract.
max_memory_pages: u32,
}
impl<T: Trait> Default for Config<T> {
fn default() -> Config<T> {
Config {
grow_mem_cost: T::Gas::sa(1),
regular_op_cost: T::Gas::sa(1),
return_data_per_byte_cost: T::Gas::sa(1),
max_stack_height: 64 * 1024,
max_memory_pages: 16,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use gas::GasMeter;
use std::collections::HashMap;
use tests::Test;
use wabt;
#[derive(Debug, PartialEq, Eq)]
struct CreateEntry {
code: Vec<u8>,
endowment: u64,
data: Vec<u8>,
gas_left: u64,
}
#[derive(Debug, PartialEq, Eq)]
struct TransferEntry {
to: u64,
value: u64,
data: Vec<u8>,
gas_left: u64,
}
#[derive(Default)]
pub struct MockExt {
storage: HashMap<Vec<u8>, Vec<u8>>,
creates: Vec<CreateEntry>,
transfers: Vec<TransferEntry>,
next_account_id: u64,
}
impl Ext for MockExt {
type T = Test;
fn get_storage(&self, key: &[u8]) -> Option<Vec<u8>> {
self.storage.get(key).cloned()
}
fn set_storage(&mut self, key: &[u8], value: Option<Vec<u8>>) {
*self.storage.entry(key.to_vec()).or_insert(Vec::new()) = value.unwrap_or(Vec::new());
}
fn create(
&mut self,
code: &[u8],
endowment: u64,
gas_meter: &mut GasMeter<Test>,
data: &[u8],
) -> Result<CreateReceipt<Test>, ()> {
self.creates.push(CreateEntry {
code: code.to_vec(),
endowment,
data: data.to_vec(),
gas_left: gas_meter.gas_left(),
});
let address = self.next_account_id;
self.next_account_id += 1;
Ok(CreateReceipt { address })
}
fn call(
&mut self,
to: &u64,
value: u64,
gas_meter: &mut GasMeter<Test>,
data: &[u8],
) -> Result<CallReceipt, ()> {
self.transfers.push(TransferEntry {
to: *to,
value,
data: data.to_vec(),
gas_left: gas_meter.gas_left(),
});
// Assume for now that it was just a plain transfer.
// TODO: Add tests for different call outcomes.
Ok(CallReceipt {
return_data: Vec::new(),
})
}
}
const CODE_TRANSFER: &str = r#"
(module
;; ext_call(
;; callee_ptr: u32,
;; callee_len: u32,
;; gas: u64,
;; value_ptr: u32,
;; value_len: u32,
;; input_data_ptr: u32,
;; input_data_len: u32
;;) -> u32
(import "env" "ext_call" (func $ext_call (param i32 i32 i64 i32 i32 i32 i32) (result i32)))
(import "env" "memory" (memory 1 1))
(func (export "call")
(drop
(call $ext_call
(i32.const 4) ;; Pointer to "callee" address.
(i32.const 8) ;; Length of "callee" address.
(i64.const 0) ;; How much gas to devote for the execution. 0 = all.
(i32.const 12) ;; Pointer to the buffer with value to transfer
(i32.const 8) ;; Length of the buffer with value to transfer.
(i32.const 20) ;; Pointer to input data buffer address
(i32.const 4) ;; Length of input data buffer
)
)
)
;; Destination AccountId to transfer the funds.
;; Represented by u64 (8 bytes long) in little endian.
(data (i32.const 4) "\09\00\00\00\00\00\00\00")
;; Amount of value to transfer.
;; Represented by u64 (8 bytes long) in little endian.
(data (i32.const 12) "\06\00\00\00\00\00\00\00")
(data (i32.const 20) "\01\02\03\04")
)
"#;
#[test]
fn contract_transfer() {
let code_transfer = wabt::wat2wasm(CODE_TRANSFER).unwrap();
let mut mock_ext = MockExt::default();
execute(
&code_transfer,
&[],
&mut mock_ext,
&mut GasMeter::with_limit(50_000, 1),
).unwrap();
assert_eq!(
&mock_ext.transfers,
&[TransferEntry {
to: 9,
value: 6,
data: vec![
1, 2, 3, 4,
],
gas_left: 49990,
}]
);
}
const CODE_CREATE: &str = r#"
(module
;; ext_create(
;; code_ptr: u32,
;; code_len: u32,
;; gas: u64,
;; value_ptr: u32,
;; value_len: u32,
;; input_data_ptr: u32,
;; input_data_len: u32,
;; ) -> u32
(import "env" "ext_create" (func $ext_create (param i32 i32 i64 i32 i32 i32 i32) (result i32)))
(import "env" "memory" (memory 1 1))
(func (export "call")
(drop
(call $ext_create
(i32.const 12) ;; Pointer to `code`
(i32.const 8) ;; Length of `code`
(i64.const 0) ;; How much gas to devote for the execution. 0 = all.
(i32.const 4) ;; Pointer to the buffer with value to transfer
(i32.const 8) ;; Length of the buffer with value to transfer
(i32.const 20) ;; Pointer to input data buffer address
(i32.const 4) ;; Length of input data buffer
)
)
)
;; Amount of value to transfer.
;; Represented by u64 (8 bytes long) in little endian.
(data (i32.const 4) "\03\00\00\00\00\00\00\00")
;; Embedded wasm code.
(data (i32.const 12) "\00\61\73\6d\01\00\00\00")
;; Input data to pass to the contract being created.
(data (i32.const 20) "\01\02\03\04")
)
"#;
#[test]
fn contract_create() {
let code_create = wabt::wat2wasm(CODE_CREATE).unwrap();
let mut mock_ext = MockExt::default();
execute(
&code_create,
&[],
&mut mock_ext,
&mut GasMeter::with_limit(50_000, 1),
).unwrap();
assert_eq!(
&mock_ext.creates,
&[CreateEntry {
code: vec![0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00],
endowment: 3,
data: vec![
1, 2, 3, 4,
],
gas_left: 49990,
}]
);
}
const CODE_MEM: &str = r#"
(module
;; Internal memory is not allowed.
(memory 1 1)
(func (export "call")
nop
)
)
"#;
#[test]
fn contract_internal_mem() {
let code_mem = wabt::wat2wasm(CODE_MEM).unwrap();
let mut mock_ext = MockExt::default();
assert_matches!(
execute(
&code_mem,
&[],
&mut mock_ext,
&mut GasMeter::with_limit(100_000, 1)
),
Err(_)
);
}
const CODE_TRANSFER_LIMITED_GAS: &str = r#"
(module
;; ext_call(
;; callee_ptr: u32,
;; callee_len: u32,
;; gas: u64,
;; value_ptr: u32,
;; value_len: u32,
;; input_data_ptr: u32,
;; input_data_len: u32
;;) -> u32
(import "env" "ext_call" (func $ext_call (param i32 i32 i64 i32 i32 i32 i32) (result i32)))
(import "env" "memory" (memory 1 1))
(func (export "call")
(drop
(call $ext_call
(i32.const 4) ;; Pointer to "callee" address.
(i32.const 8) ;; Length of "callee" address.
(i64.const 228) ;; How much gas to devote for the execution.
(i32.const 12) ;; Pointer to the buffer with value to transfer
(i32.const 8) ;; Length of the buffer with value to transfer.
(i32.const 20) ;; Pointer to input data buffer address
(i32.const 4) ;; Length of input data buffer
)
)
)
;; Destination AccountId to transfer the funds.
;; Represented by u64 (8 bytes long) in little endian.
(data (i32.const 4) "\09\00\00\00\00\00\00\00")
;; Amount of value to transfer.
;; Represented by u64 (8 bytes long) in little endian.
(data (i32.const 12) "\06\00\00\00\00\00\00\00")
(data (i32.const 20) "\01\02\03\04")
)
"#;
#[test]
fn contract_call_limited_gas() {
let code_transfer = wabt::wat2wasm(CODE_TRANSFER_LIMITED_GAS).unwrap();
let mut mock_ext = MockExt::default();
execute(
&code_transfer,
&[],
&mut mock_ext,
&mut GasMeter::with_limit(50_000, 1),
).unwrap();
assert_eq!(
&mock_ext.transfers,
&[TransferEntry {
to: 9,
value: 6,
data: vec![
1, 2, 3, 4,
],
gas_left: 228,
}]
);
}
}
+286
View File
@@ -0,0 +1,286 @@
// Copyright 2018 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Module that takes care of loading, checking and preprocessing of a
//! wasm module before execution.
use super::env_def::HostFunctionSet;
use super::{Config, Error, Ext};
use rstd::prelude::*;
use parity_wasm::elements::{self, External, MemoryType, Type};
use pwasm_utils;
use pwasm_utils::rules;
use runtime_primitives::traits::As;
use sandbox;
use Trait;
struct ContractModule<'a, T: Trait + 'a> {
// An `Option` is used here for loaning (`take()`-ing) the module.
// Invariant: Can't be `None` (i.e. on enter and on exit from the function
// the value *must* be `Some`).
module: Option<elements::Module>,
config: &'a Config<T>,
}
impl<'a, T: Trait> ContractModule<'a, T> {
fn new(original_code: &[u8], config: &'a Config<T>) -> Result<ContractModule<'a, T>, Error> {
let module =
elements::deserialize_buffer(original_code).map_err(|_| Error::Deserialization)?;
Ok(ContractModule {
module: Some(module),
config,
})
}
/// Ensures that module doesn't declare internal memories.
///
/// In this runtime we only allow wasm module to import memory from the environment.
/// Memory section contains declarations of internal linear memories, so if we find one
/// we reject such a module.
fn ensure_no_internal_memory(&self) -> Result<(), Error> {
let module = self
.module
.as_ref()
.expect("On entry to the function `module` can't be None; qed");
if module
.memory_section()
.map_or(false, |ms| ms.entries().len() > 0)
{
return Err(Error::InternalMemoryDeclared);
}
Ok(())
}
fn inject_gas_metering(&mut self) -> Result<(), Error> {
let gas_rules = rules::Set::new(self.config.regular_op_cost.as_(), Default::default())
.with_grow_cost(self.config.grow_mem_cost.as_())
.with_forbidden_floats();
let module = self
.module
.take()
.expect("On entry to the function `module` can't be `None`; qed");
let contract_module = pwasm_utils::inject_gas_counter(module, &gas_rules)
.map_err(|_| Error::GasInstrumentation)?;
self.module = Some(contract_module);
Ok(())
}
fn inject_stack_height_metering(&mut self) -> Result<(), Error> {
let module = self
.module
.take()
.expect("On entry to the function `module` can't be `None`; qed");
let contract_module =
pwasm_utils::stack_height::inject_limiter(module, self.config.max_stack_height)
.map_err(|_| Error::StackHeightInstrumentation)?;
self.module = Some(contract_module);
Ok(())
}
/// Scan an import section if any.
///
/// This accomplishes two tasks:
///
/// - checks any imported function against defined host functions set, incl.
/// their signatures.
/// - if there is a memory import, returns it's descriptor
fn scan_imports<E: Ext>(&self, env: &HostFunctionSet<E>) -> Result<Option<&MemoryType>, Error> {
let module = self
.module
.as_ref()
.expect("On entry to the function `module` can't be `None`; qed");
let types = module.type_section().map(|ts| ts.types()).unwrap_or(&[]);
let import_entries = module
.import_section()
.map(|is| is.entries())
.unwrap_or(&[]);
let mut imported_mem_type = None;
for import in import_entries {
if import.module() != "env" {
// This import tries to import something from non-"env" module,
// but all imports are located in "env" at the moment.
return Err(Error::Instantiate);
}
let type_idx = match import.external() {
&External::Function(ref type_idx) => type_idx,
&External::Memory(ref memory_type) => {
imported_mem_type = Some(memory_type);
continue;
}
_ => continue,
};
let Type::Function(ref func_ty) = types
.get(*type_idx as usize)
.ok_or_else(|| Error::Instantiate)?;
let ext_func = env
.funcs
.get(import.field())
.ok_or_else(|| Error::Instantiate)?;
if !ext_func.func_type_matches(func_ty) {
return Err(Error::Instantiate);
}
}
Ok(imported_mem_type)
}
fn into_wasm_code(mut self) -> Result<Vec<u8>, Error> {
elements::serialize(
self.module
.take()
.expect("On entry to the function `module` can't be `None`; qed"),
).map_err(|_| Error::Serialization)
}
}
pub(super) struct PreparedContract {
pub instrumented_code: Vec<u8>,
pub memory: sandbox::Memory,
}
/// Loads the given module given in `original_code`, performs some checks on it and
/// does some preprocessing.
///
/// The checks are:
///
/// - module doesn't define an internal memory instance,
/// - imported memory (if any) doesn't reserve more memory than permitted by the `config`,
/// - all imported functions from the external environment matches defined by `env` module,
///
/// The preprocessing includes injecting code for gas metering and metering the height of stack.
pub(super) fn prepare_contract<E: Ext>(
original_code: &[u8],
config: &Config<E::T>,
env: &HostFunctionSet<E>,
) -> Result<PreparedContract, Error> {
let mut contract_module = ContractModule::new(original_code, config)?;
contract_module.ensure_no_internal_memory()?;
contract_module.inject_gas_metering()?;
contract_module.inject_stack_height_metering()?;
let memory = if let Some(memory_type) = contract_module.scan_imports(env)? {
// Inspect the module to extract the initial and maximum page count.
let limits = memory_type.limits();
match (limits.initial(), limits.maximum()) {
(initial, Some(maximum)) if initial > maximum => {
// Requested initial number of pages should not exceed the requested maximum.
return Err(Error::Memory);
}
(_, Some(maximum)) if maximum > config.max_memory_pages => {
// Maximum number of pages should not exceed the configured maximum.
return Err(Error::Memory);
}
(_, None) => {
// Maximum number of pages should be always declared.
// This isn't a hard requirement and can be treated as a maxiumum set
// to configured maximum.
return Err(Error::Memory);
}
(initial, maximum) => sandbox::Memory::new(initial, maximum),
}
} else {
// If none memory imported then just crate an empty placeholder.
// Any access to it will lead to out of bounds trap.
sandbox::Memory::new(0, Some(0))
};
let memory = memory.map_err(|_| Error::Memory)?;
Ok(PreparedContract {
instrumented_code: contract_module.into_wasm_code()?,
memory,
})
}
#[cfg(test)]
mod tests {
use super::*;
use std::fmt;
use tests::Test;
use vm::tests::MockExt;
use wabt;
impl fmt::Debug for PreparedContract {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "PreparedContract {{ .. }}")
}
}
fn parse_and_prepare_wat(wat: &str) -> Result<PreparedContract, Error> {
let wasm = wabt::Wat2Wasm::new().validate(false).convert(wat).unwrap();
let config = Config::<Test>::default();
let env = ::vm::env_def::init_env();
prepare_contract::<MockExt>(wasm.as_ref(), &config, &env)
}
#[test]
fn internal_memory_declaration() {
let r = parse_and_prepare_wat(r#"(module (memory 1 1))"#);
assert_matches!(r, Err(Error::InternalMemoryDeclared));
}
#[test]
fn memory() {
// This test assumes that maximum page number is configured to a certain number.
assert_eq!(Config::<Test>::default().max_memory_pages, 16);
let r = parse_and_prepare_wat(r#"(module (import "env" "memory" (memory 1 1)))"#);
assert_matches!(r, Ok(_));
// No memory import
let r = parse_and_prepare_wat(r#"(module)"#);
assert_matches!(r, Ok(_));
// initial exceed maximum
let r = parse_and_prepare_wat(r#"(module (import "env" "memory" (memory 16 1)))"#);
assert_matches!(r, Err(Error::Memory));
// no maximum
let r = parse_and_prepare_wat(r#"(module (import "env" "memory" (memory 1)))"#);
assert_matches!(r, Err(Error::Memory));
// requested maximum exceed configured maximum
let r = parse_and_prepare_wat(r#"(module (import "env" "memory" (memory 1 17)))"#);
assert_matches!(r, Err(Error::Memory));
}
#[test]
fn imports() {
// nothing can be imported from non-"env" module for now.
let r = parse_and_prepare_wat(r#"(module (import "another_module" "memory" (memory 1 1)))"#);
assert_matches!(r, Err(Error::Instantiate));
let r = parse_and_prepare_wat(r#"(module (import "env" "gas" (func (param i32))))"#);
assert_matches!(r, Ok(_));
// wrong signature
let r = parse_and_prepare_wat(r#"(module (import "env" "gas" (func (param i64))))"#);
assert_matches!(r, Err(Error::Instantiate));
// unknown function name
let r = parse_and_prepare_wat(r#"(module (import "env" "unknown_func" (func)))"#);
assert_matches!(r, Err(Error::Instantiate));
}
}