mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-20 09:15:42 +00:00
Contracts module rejig (#1358)
* Move prepare under code. * Schedule update * CodeHash * create takes code_hash * pass mem def and use code in vm::execute * Actually save and load code * Use T::Hash as CodeHash * Explicit entrypoint name * Return code_hash and deposit an Event * Charge for deployed code with gas. * ImportSatisfyCheck and FunctionImplProvider * Progress. * Use new infrastructure for checking imports * Rename entrypoint to entrypoint_name * Use strings instead of a Error enum * Clean * WIP * Fix macro_define_env test. * Fix vm code tests. * Remove tests for now. * Fix borked merge * Fix build for wasm * fmt * Scaffolding for abstracting vm. * Hook up execution to exec layer. * Fix vm tests. * Use schedule directly in WasmLoader * Implement test language. * Add input_data test. * Max depth test * ext_caller * Simplify test. * Add TODO * Some tests and todos. * top_level * Clean. * Restore a couple of integration tests. * Add a few comments. * Add ext_address runtime call. * Deduplicate caller/self_account * Add not_exists test. * Change bool to TransferCause. * Add address tests. * Remove output_buf from parameter. * return from start fn. * Smart gas meter * Tracing * Fix prepare tests. * Code moving * Add ExecFeeToken * Use tokens everywhere. * Make it compile in no_std. * Lift all test requirements to TestAuxiliaries * A minor clean * First create tests * Remove unneeded TODO * Docs. * Code shuffling * Rename create → instantiate * Add test address. * Code shuffling * Add base_fee tests. * rejig the code * Add some comments * on_finalise comment * Move event deposit further * Update Cargo.lock * Use crates.io version of pwasm-utils * Format todo comments * Fix formatting * Comments * EmptyOutputBuf and OutputBuf split. * Restore code_hash * Fix node-executor. * Fix typo * Fix fmt * Update srml/contract/src/account_db.rs Co-Authored-By: pepyakin <s.pepyakin@gmail.com> * Update srml/contract/src/lib.rs Co-Authored-By: pepyakin <s.pepyakin@gmail.com> * Line wraps * Wrapping macros * Add _ prefix * Grumbles * Doc updates. * Update srml/contract/src/wasm/mod.rs Co-Authored-By: pepyakin <s.pepyakin@gmail.com> * Update srml/contract/src/lib.rs Co-Authored-By: pepyakin <s.pepyakin@gmail.com> * Add comment * Use saturation to signal overflow * Add prepare_test! macro * Require deploy function. * Add entry point tests * Add comment. * Rename code → code_cache to better describe * Get rid of weird match! * Recompile binaries * Add comments * refuse_instantiate_with_value_below_existential_deposit * Little fix * Make test more complete * Clean * Add integration test for instantiation * Rebuild runtime. * Add some tests. * Attach an issue to a TODO * Attach another issue * Apply suggestions from code review Co-Authored-By: pepyakin <s.pepyakin@gmail.com> * Update srml/contract/src/exec.rs Co-Authored-By: pepyakin <s.pepyakin@gmail.com> * Update srml/contract/src/exec.rs Co-Authored-By: pepyakin <s.pepyakin@gmail.com> * Recompile node_runtime
This commit is contained in:
Generated
+5
-4
@@ -2163,7 +2163,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pwasm-utils"
|
name = "pwasm-utils"
|
||||||
version = "0.3.1"
|
version = "0.6.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
"byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
@@ -2797,10 +2797,11 @@ name = "srml-contract"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"assert_matches 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"assert_matches 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"hex-literal 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"parity-codec 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"parity-codec 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"parity-codec-derive 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"parity-codec-derive 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"parity-wasm 0.31.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
"parity-wasm 0.31.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"pwasm-utils 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"pwasm-utils 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"serde 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)",
|
"serde 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"sr-io 0.1.0",
|
"sr-io 0.1.0",
|
||||||
"sr-primitives 0.1.0",
|
"sr-primitives 0.1.0",
|
||||||
@@ -4186,7 +4187,7 @@ name = "twox-hash"
|
|||||||
version = "1.1.2"
|
version = "1.1.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"rand 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)",
|
"rand 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -4700,7 +4701,7 @@ dependencies = [
|
|||||||
"checksum proc-macro2 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "77997c53ae6edd6d187fec07ec41b207063b5ee6f33680e9fa86d405cdd313d4"
|
"checksum proc-macro2 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "77997c53ae6edd6d187fec07ec41b207063b5ee6f33680e9fa86d405cdd313d4"
|
||||||
"checksum proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)" = "77619697826f31a02ae974457af0b29b723e5619e113e9397b8b82c6bd253f09"
|
"checksum proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)" = "77619697826f31a02ae974457af0b29b723e5619e113e9397b8b82c6bd253f09"
|
||||||
"checksum protobuf 2.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "eae0479da11de87d371fa47eb033059715ffa8f714748da20aa364d56ec5bee2"
|
"checksum protobuf 2.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "eae0479da11de87d371fa47eb033059715ffa8f714748da20aa364d56ec5bee2"
|
||||||
"checksum pwasm-utils 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "efd695333cfae6e9dbe2703a6d040e252b57a6fc3b9a65c712615ac042b2e0c5"
|
"checksum pwasm-utils 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "efb0dcbddbb600f47a7098d33762a00552c671992171637f5bb310b37fe1f0e4"
|
||||||
"checksum quick-error 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "5fb6ccf8db7bbcb9c2eae558db5ab4f3da1c2a87e4e597ed394726bc8ea6ca1d"
|
"checksum quick-error 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "5fb6ccf8db7bbcb9c2eae558db5ab4f3da1c2a87e4e597ed394726bc8ea6ca1d"
|
||||||
"checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0"
|
"checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0"
|
||||||
"checksum quote 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9949cfe66888ffe1d53e6ec9d9f3b70714083854be20fd5e271b232a017401e8"
|
"checksum quote 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9949cfe66888ffe1d53e6ec9d9f3b70714083854be20fd5e271b232a017401e8"
|
||||||
|
|||||||
BIN
Binary file not shown.
@@ -61,7 +61,7 @@ mod tests {
|
|||||||
use primitives::{twox_128, Blake2Hasher, ChangesTrieConfiguration,
|
use primitives::{twox_128, Blake2Hasher, ChangesTrieConfiguration,
|
||||||
ed25519::{Public, Pair}};
|
ed25519::{Public, Pair}};
|
||||||
use node_primitives::{Hash, BlockNumber, AccountId};
|
use node_primitives::{Hash, BlockNumber, AccountId};
|
||||||
use runtime_primitives::traits::{Header as HeaderT, Digest as DigestT};
|
use runtime_primitives::traits::{Header as HeaderT, Digest as DigestT, Hash as HashT};
|
||||||
use runtime_primitives::{generic, generic::Era, ApplyOutcome, ApplyError, ApplyResult, Perbill};
|
use runtime_primitives::{generic, generic::Era, ApplyOutcome, ApplyError, ApplyResult, Perbill};
|
||||||
use {balances, indices, staking, session, system, consensus, timestamp, treasury, contract};
|
use {balances, indices, staking, session, system, consensus, timestamp, treasury, contract};
|
||||||
use contract::ContractAddressFor;
|
use contract::ContractAddressFor;
|
||||||
@@ -315,9 +315,9 @@ mod tests {
|
|||||||
1,
|
1,
|
||||||
GENESIS_HASH.into(),
|
GENESIS_HASH.into(),
|
||||||
if support_changes_trie {
|
if support_changes_trie {
|
||||||
hex!("e2dc1ed62a3878e084c49baef3eed4bc462caaa1ee9db0763eaa8d39d1affc7e").into()
|
hex!("cc63808897a07869d3b9103df5ad92f9be2f865ece506df5de0a87b2a95131d5").into()
|
||||||
} else {
|
} else {
|
||||||
hex!("7dad66de05ab6391ece9db092288cb39ba723f7f5d0b69b507b753fc92c98b8e").into()
|
hex!("fe5275f4d9f8130c8e80d0132f0a718ae0eeea2872c841843192720ad5c3f05a").into()
|
||||||
},
|
},
|
||||||
if support_changes_trie {
|
if support_changes_trie {
|
||||||
vec![changes_trie_log(
|
vec![changes_trie_log(
|
||||||
@@ -343,7 +343,7 @@ mod tests {
|
|||||||
construct_block(
|
construct_block(
|
||||||
2,
|
2,
|
||||||
block1(false).1,
|
block1(false).1,
|
||||||
hex!("f085a4077d071d3334f7553bcc3f10d97fd612e25c6df5c8f94151bbe557e24a").into(),
|
hex!("45b6655508fb524467b5c24184a7509b9ae07db4f95e16052ed425af182f39a8").into(),
|
||||||
vec![ // session changes here, so we add a grandpa change signal log.
|
vec![ // session changes here, so we add a grandpa change signal log.
|
||||||
Log::from(::grandpa::RawLog::AuthoritiesChangeSignal(0, vec![
|
Log::from(::grandpa::RawLog::AuthoritiesChangeSignal(0, vec![
|
||||||
(Keyring::One.to_raw_public().into(), 1),
|
(Keyring::One.to_raw_public().into(), 1),
|
||||||
@@ -372,7 +372,7 @@ mod tests {
|
|||||||
construct_block(
|
construct_block(
|
||||||
1,
|
1,
|
||||||
GENESIS_HASH.into(),
|
GENESIS_HASH.into(),
|
||||||
hex!("1d47422645f5fa3d79cff9ab5cd69ee62919c909860f063bdaf18eda834baa05").into(),
|
hex!("ec00658cc2826d3499dde2954e399f0a0b2596eec1b0da9b76bc72394161dc99").into(),
|
||||||
vec![],
|
vec![],
|
||||||
vec![
|
vec![
|
||||||
CheckedExtrinsic {
|
CheckedExtrinsic {
|
||||||
@@ -546,6 +546,8 @@ mod tests {
|
|||||||
(import "env" "ext_input_size" (func $ext_input_size (result i32)))
|
(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" "ext_input_copy" (func $ext_input_copy (param i32 i32 i32)))
|
||||||
(import "env" "memory" (memory 1 1))
|
(import "env" "memory" (memory 1 1))
|
||||||
|
(func (export "deploy")
|
||||||
|
)
|
||||||
(func (export "call")
|
(func (export "call")
|
||||||
(block $fail
|
(block $fail
|
||||||
;; fail if ext_input_size != 4
|
;; fail if ext_input_size != 4
|
||||||
@@ -613,55 +615,15 @@ mod tests {
|
|||||||
)
|
)
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
/// Convert a byte slice to a string with hex values.
|
|
||||||
/// 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(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn deploying_wasm_contract_should_work() {
|
fn deploying_wasm_contract_should_work() {
|
||||||
let mut t = new_test_ext(COMPACT_CODE, false);
|
let mut t = new_test_ext(COMPACT_CODE, false);
|
||||||
|
|
||||||
let code_transfer = wabt::wat2wasm(CODE_TRANSFER).unwrap();
|
let transfer_code = wabt::wat2wasm(CODE_TRANSFER).unwrap();
|
||||||
let code_ctor_transfer = wabt::wat2wasm(&code_ctor(&code_transfer)).unwrap();
|
let transfer_ch = <Runtime as system::Trait>::Hashing::hash(&transfer_code);
|
||||||
|
|
||||||
let addr = <Runtime as contract::Trait>::DetermineContractAddress::contract_address_for(
|
let addr = <Runtime as contract::Trait>::DetermineContractAddress::contract_address_for(
|
||||||
&code_ctor_transfer,
|
&transfer_ch,
|
||||||
&[],
|
&[],
|
||||||
&charlie(),
|
&charlie(),
|
||||||
);
|
);
|
||||||
@@ -669,7 +631,7 @@ mod tests {
|
|||||||
let b = construct_block(
|
let b = construct_block(
|
||||||
1,
|
1,
|
||||||
GENESIS_HASH.into(),
|
GENESIS_HASH.into(),
|
||||||
hex!("c442a475a16805d20c326f1134fe5a9656511ed7e1a7f0c7dae2dfc8612e418b").into(),
|
hex!("6a4da4ed61c4d9eba0477aa67024d573693df781176dfe7fe903d1088b38b266").into(),
|
||||||
vec![],
|
vec![],
|
||||||
vec![
|
vec![
|
||||||
CheckedExtrinsic {
|
CheckedExtrinsic {
|
||||||
@@ -679,11 +641,17 @@ mod tests {
|
|||||||
CheckedExtrinsic {
|
CheckedExtrinsic {
|
||||||
signed: Some((charlie(), 0)),
|
signed: Some((charlie(), 0)),
|
||||||
function: Call::Contract(
|
function: Call::Contract(
|
||||||
contract::Call::create::<Runtime>(10.into(), 10_000.into(), code_ctor_transfer, Vec::new())
|
contract::Call::put_code::<Runtime>(10_000.into(), transfer_code)
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
CheckedExtrinsic {
|
CheckedExtrinsic {
|
||||||
signed: Some((charlie(), 1)),
|
signed: Some((charlie(), 1)),
|
||||||
|
function: Call::Contract(
|
||||||
|
contract::Call::create::<Runtime>(10.into(), 10_000.into(), transfer_ch, Vec::new())
|
||||||
|
),
|
||||||
|
},
|
||||||
|
CheckedExtrinsic {
|
||||||
|
signed: Some((charlie(), 2)),
|
||||||
function: Call::Contract(
|
function: Call::Contract(
|
||||||
contract::Call::call::<Runtime>(indices::address::Address::Id(addr), 10.into(), 10_000.into(), vec![0x00, 0x01, 0x02, 0x03])
|
contract::Call::call::<Runtime>(indices::address::Address::Id(addr), 10.into(), 10_000.into(), vec![0x00, 0x01, 0x02, 0x03])
|
||||||
),
|
),
|
||||||
@@ -695,7 +663,7 @@ mod tests {
|
|||||||
|
|
||||||
runtime_io::with_externalities(&mut t, || {
|
runtime_io::with_externalities(&mut t, || {
|
||||||
// Verify that the contract constructor worked well and code of TRANSFER contract is actually deployed.
|
// Verify that the contract constructor worked well and code of TRANSFER contract is actually deployed.
|
||||||
assert_eq!(&contract::CodeOf::<Runtime>::get(addr), &code_transfer);
|
assert_eq!(&contract::CodeHashOf::<Runtime>::get(addr).unwrap(), &transfer_ch);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Generated
+3
-3
@@ -703,7 +703,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pwasm-utils"
|
name = "pwasm-utils"
|
||||||
version = "0.3.1"
|
version = "0.6.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
"byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
@@ -1093,7 +1093,7 @@ dependencies = [
|
|||||||
"parity-codec 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"parity-codec 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"parity-codec-derive 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"parity-codec-derive 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"parity-wasm 0.31.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
"parity-wasm 0.31.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"pwasm-utils 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"pwasm-utils 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"serde 1.0.81 (registry+https://github.com/rust-lang/crates.io-index)",
|
"serde 1.0.81 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"sr-io 0.1.0",
|
"sr-io 0.1.0",
|
||||||
"sr-primitives 0.1.0",
|
"sr-primitives 0.1.0",
|
||||||
@@ -1998,7 +1998,7 @@ dependencies = [
|
|||||||
"checksum proc-macro-hack 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2c725b36c99df7af7bf9324e9c999b9e37d92c8f8caf106d82e1d7953218d2d8"
|
"checksum proc-macro-hack 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2c725b36c99df7af7bf9324e9c999b9e37d92c8f8caf106d82e1d7953218d2d8"
|
||||||
"checksum proc-macro-hack-impl 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2b753ad9ed99dd8efeaa7d2fb8453c8f6bc3e54b97966d35f1bc77ca6865254a"
|
"checksum proc-macro-hack-impl 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2b753ad9ed99dd8efeaa7d2fb8453c8f6bc3e54b97966d35f1bc77ca6865254a"
|
||||||
"checksum proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)" = "77619697826f31a02ae974457af0b29b723e5619e113e9397b8b82c6bd253f09"
|
"checksum proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)" = "77619697826f31a02ae974457af0b29b723e5619e113e9397b8b82c6bd253f09"
|
||||||
"checksum pwasm-utils 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "efd695333cfae6e9dbe2703a6d040e252b57a6fc3b9a65c712615ac042b2e0c5"
|
"checksum pwasm-utils 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e9135bed7b452e20dbb395a2d519abaf0c46d60e7ecc02daeeab447d29bada1"
|
||||||
"checksum quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "53fa22a1994bd0f9372d7a816207d8a2677ad0325b073f5c5332760f0fb62b5c"
|
"checksum quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "53fa22a1994bd0f9372d7a816207d8a2677ad0325b073f5c5332760f0fb62b5c"
|
||||||
"checksum rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8356f47b32624fef5b3301c1be97e5944ecdd595409cc5da11d05f211db6cfbd"
|
"checksum rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8356f47b32624fef5b3301c1be97e5944ecdd595409cc5da11d05f211db6cfbd"
|
||||||
"checksum rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e464cd887e869cddcae8792a4ee31d23c7edd516700695608f5b98c67ee0131c"
|
"checksum rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e464cd887e869cddcae8792a4ee31d23c7edd516700695608f5b98c67ee0131c"
|
||||||
|
|||||||
BIN
Binary file not shown.
@@ -5,7 +5,7 @@ authors = ["Parity Technologies <admin@parity.io>"]
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
serde = { version = "1.0", default-features = false }
|
serde = { version = "1.0", default-features = false }
|
||||||
pwasm-utils = { version = "0.3", default-features = false }
|
pwasm-utils = { version = "0.6.1", default-features = false }
|
||||||
parity-codec = { version = "2.2", default-features = false }
|
parity-codec = { version = "2.2", default-features = false }
|
||||||
parity-codec-derive = { version = "2.1", default-features = false }
|
parity-codec-derive = { version = "2.1", default-features = false }
|
||||||
parity-wasm = { version = "0.31", default-features = false }
|
parity-wasm = { version = "0.31", default-features = false }
|
||||||
@@ -21,6 +21,7 @@ srml-balances = { path = "../balances", default-features = false }
|
|||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
wabt = "~0.7.4"
|
wabt = "~0.7.4"
|
||||||
assert_matches = "1.1"
|
assert_matches = "1.1"
|
||||||
|
hex-literal = "0.1.0"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["std"]
|
default = ["std"]
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
//! Auxilliaries to help with managing partial changes to accounts state.
|
//! Auxilliaries to help with managing partial changes to accounts state.
|
||||||
|
|
||||||
use super::{CodeOf, StorageOf, Trait};
|
use super::{CodeHash, CodeHashOf, StorageOf, Trait};
|
||||||
use rstd::cell::RefCell;
|
use rstd::cell::RefCell;
|
||||||
use rstd::collections::btree_map::{BTreeMap, Entry};
|
use rstd::collections::btree_map::{BTreeMap, Entry};
|
||||||
use rstd::prelude::*;
|
use rstd::prelude::*;
|
||||||
@@ -25,7 +25,8 @@ use {balances, system};
|
|||||||
|
|
||||||
pub struct ChangeEntry<T: Trait> {
|
pub struct ChangeEntry<T: Trait> {
|
||||||
balance: Option<T::Balance>,
|
balance: Option<T::Balance>,
|
||||||
code: Option<Vec<u8>>,
|
/// In the case the outer option is None, the code_hash remains untouched, while providing `Some(None)` signifies a removing of the code in question
|
||||||
|
code: Option<Option<CodeHash<T>>>,
|
||||||
storage: BTreeMap<Vec<u8>, Option<Vec<u8>>>,
|
storage: BTreeMap<Vec<u8>, Option<Vec<u8>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,7 +45,7 @@ pub type ChangeSet<T> = BTreeMap<<T as system::Trait>::AccountId, ChangeEntry<T>
|
|||||||
|
|
||||||
pub trait AccountDb<T: Trait> {
|
pub trait AccountDb<T: Trait> {
|
||||||
fn get_storage(&self, account: &T::AccountId, location: &[u8]) -> Option<Vec<u8>>;
|
fn get_storage(&self, account: &T::AccountId, location: &[u8]) -> Option<Vec<u8>>;
|
||||||
fn get_code(&self, account: &T::AccountId) -> Vec<u8>;
|
fn get_code(&self, account: &T::AccountId) -> Option<CodeHash<T>>;
|
||||||
fn get_balance(&self, account: &T::AccountId) -> T::Balance;
|
fn get_balance(&self, account: &T::AccountId) -> T::Balance;
|
||||||
|
|
||||||
fn commit(&mut self, change_set: ChangeSet<T>);
|
fn commit(&mut self, change_set: ChangeSet<T>);
|
||||||
@@ -55,8 +56,8 @@ impl<T: Trait> AccountDb<T> for DirectAccountDb {
|
|||||||
fn get_storage(&self, account: &T::AccountId, location: &[u8]) -> Option<Vec<u8>> {
|
fn get_storage(&self, account: &T::AccountId, location: &[u8]) -> Option<Vec<u8>> {
|
||||||
<StorageOf<T>>::get(account.clone(), location.to_vec())
|
<StorageOf<T>>::get(account.clone(), location.to_vec())
|
||||||
}
|
}
|
||||||
fn get_code(&self, account: &T::AccountId) -> Vec<u8> {
|
fn get_code(&self, account: &T::AccountId) -> Option<CodeHash<T>> {
|
||||||
<CodeOf<T>>::get(account)
|
<CodeHashOf<T>>::get(account)
|
||||||
}
|
}
|
||||||
fn get_balance(&self, account: &T::AccountId) -> T::Balance {
|
fn get_balance(&self, account: &T::AccountId) -> T::Balance {
|
||||||
balances::Module::<T>::free_balance(account)
|
balances::Module::<T>::free_balance(account)
|
||||||
@@ -68,13 +69,17 @@ impl<T: Trait> AccountDb<T> for DirectAccountDb {
|
|||||||
balances::Module::<T>::set_free_balance_creating(&address, balance)
|
balances::Module::<T>::set_free_balance_creating(&address, balance)
|
||||||
{
|
{
|
||||||
// Account killed. This will ultimately lead to calling `OnFreeBalanceZero` callback
|
// Account killed. This will ultimately lead to calling `OnFreeBalanceZero` callback
|
||||||
// which will make removal of CodeOf and StorageOf for this account.
|
// which will make removal of CodeHashOf and StorageOf for this account.
|
||||||
// In order to avoid writing over the deleted properties we `continue` here.
|
// In order to avoid writing over the deleted properties we `continue` here.
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(code) = changed.code {
|
if let Some(code) = changed.code {
|
||||||
<CodeOf<T>>::insert(&address, &code);
|
if let Some(code) = code {
|
||||||
|
<CodeHashOf<T>>::insert(&address, code);
|
||||||
|
} else {
|
||||||
|
<CodeHashOf<T>>::remove(&address);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for (k, v) in changed.storage.into_iter() {
|
for (k, v) in changed.storage.into_iter() {
|
||||||
if let Some(value) = v {
|
if let Some(value) = v {
|
||||||
@@ -116,7 +121,7 @@ impl<'a, T: Trait> OverlayAccountDb<'a, T> {
|
|||||||
.storage
|
.storage
|
||||||
.insert(location, value);
|
.insert(location, value);
|
||||||
}
|
}
|
||||||
pub fn set_code(&mut self, account: &T::AccountId, code: Vec<u8>) {
|
pub fn set_code(&mut self, account: &T::AccountId, code: Option<CodeHash<T>>) {
|
||||||
self.local
|
self.local
|
||||||
.borrow_mut()
|
.borrow_mut()
|
||||||
.entry(account.clone())
|
.entry(account.clone())
|
||||||
@@ -141,7 +146,7 @@ impl<'a, T: Trait> AccountDb<T> for OverlayAccountDb<'a, T> {
|
|||||||
.cloned()
|
.cloned()
|
||||||
.unwrap_or_else(|| self.underlying.get_storage(account, location))
|
.unwrap_or_else(|| self.underlying.get_storage(account, location))
|
||||||
}
|
}
|
||||||
fn get_code(&self, account: &T::AccountId) -> Vec<u8> {
|
fn get_code(&self, account: &T::AccountId) -> Option<CodeHash<T>> {
|
||||||
self.local
|
self.local
|
||||||
.borrow()
|
.borrow()
|
||||||
.get(account)
|
.get(account)
|
||||||
|
|||||||
+1105
-126
File diff suppressed because it is too large
Load Diff
@@ -14,10 +14,13 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use {Trait, Module, GasSpent};
|
use balances;
|
||||||
use runtime_primitives::traits::{As, CheckedMul, CheckedSub, Zero};
|
use runtime_primitives::traits::{As, CheckedMul, CheckedSub, Zero};
|
||||||
use runtime_support::StorageValue;
|
use runtime_support::StorageValue;
|
||||||
use balances;
|
use {GasSpent, Module, Trait};
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
use std::{any::Any, fmt::Debug};
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
@@ -35,11 +38,54 @@ impl GasMeterResult {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(not(test))]
|
||||||
|
pub trait TestAuxiliaries {}
|
||||||
|
#[cfg(not(test))]
|
||||||
|
impl<T> TestAuxiliaries for T {}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub trait TestAuxiliaries: Any + Debug + PartialEq + Eq {}
|
||||||
|
#[cfg(test)]
|
||||||
|
impl<T: Any + Debug + PartialEq + Eq> TestAuxiliaries for T {}
|
||||||
|
|
||||||
|
/// This trait represents a token that can be used for charging `GasMeter`.
|
||||||
|
/// There is no other way of charging it.
|
||||||
|
///
|
||||||
|
/// Implementing type is expected to be super lightweight hence `Copy` (`Clone` is added
|
||||||
|
/// for consistency). If inlined there should be no observable difference compared
|
||||||
|
/// to a hand-written code.
|
||||||
|
pub trait Token<T: Trait>: Copy + Clone + TestAuxiliaries {
|
||||||
|
/// Metadata type, which the token can require for calculating the amount
|
||||||
|
/// of gas to charge. Can be a some configuration type or
|
||||||
|
/// just the `()`.
|
||||||
|
type Metadata;
|
||||||
|
|
||||||
|
/// Calculate amount of gas that should be taken by this token.
|
||||||
|
///
|
||||||
|
/// This function should be really lightweight and must not fail. It is not
|
||||||
|
/// expected that implementors will query the storage or do any kinds of heavy operations.
|
||||||
|
///
|
||||||
|
/// That said, implementors of this function still can run into overflows
|
||||||
|
/// while calculating the amount. In this case it is ok to use saturating operations
|
||||||
|
/// since on overflow they will return `max_value` which should consume all gas.
|
||||||
|
fn calculate_amount(&self, metadata: &Self::Metadata) -> T::Gas;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A wrapper around a type-erased trait object of what used to be a `Token`.
|
||||||
|
#[cfg(test)]
|
||||||
|
pub struct ErasedToken {
|
||||||
|
pub description: String,
|
||||||
|
pub token: Box<dyn Any>,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct GasMeter<T: Trait> {
|
pub struct GasMeter<T: Trait> {
|
||||||
limit: T::Gas,
|
limit: T::Gas,
|
||||||
/// Amount of gas left from initial gas limit. Can reach zero.
|
/// Amount of gas left from initial gas limit. Can reach zero.
|
||||||
gas_left: T::Gas,
|
gas_left: T::Gas,
|
||||||
gas_price: T::Balance,
|
gas_price: T::Balance,
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
tokens: Vec<ErasedToken>,
|
||||||
}
|
}
|
||||||
impl<T: Trait> GasMeter<T> {
|
impl<T: Trait> GasMeter<T> {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -48,17 +94,37 @@ impl<T: Trait> GasMeter<T> {
|
|||||||
limit: gas_limit,
|
limit: gas_limit,
|
||||||
gas_left: gas_limit,
|
gas_left: gas_limit,
|
||||||
gas_price,
|
gas_price,
|
||||||
|
#[cfg(test)]
|
||||||
|
tokens: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Account for used gas.
|
/// Account for used gas.
|
||||||
///
|
///
|
||||||
|
/// Amount is calculated by the given `token`.
|
||||||
|
///
|
||||||
/// Returns `OutOfGas` if there is not enough gas or addition of the specified
|
/// Returns `OutOfGas` if there is not enough gas or addition of the specified
|
||||||
/// amount of gas has lead to overflow. On success returns `Proceed`.
|
/// 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
|
/// NOTE that amount is always consumed, i.e. if there is not enough gas
|
||||||
/// then the counter will be set to zero.
|
/// then the counter will be set to zero.
|
||||||
pub fn charge(&mut self, amount: T::Gas) -> GasMeterResult {
|
#[inline]
|
||||||
|
pub fn charge<Tok: Token<T>>(
|
||||||
|
&mut self,
|
||||||
|
metadata: &Tok::Metadata,
|
||||||
|
token: Tok,
|
||||||
|
) -> GasMeterResult {
|
||||||
|
#[cfg(test)]
|
||||||
|
{
|
||||||
|
// Unconditionally add the token to the storage.
|
||||||
|
let erased_tok = ErasedToken {
|
||||||
|
description: format!("{:?}", token),
|
||||||
|
token: Box::new(token),
|
||||||
|
};
|
||||||
|
self.tokens.push(erased_tok);
|
||||||
|
}
|
||||||
|
|
||||||
|
let amount = token.calculate_amount(metadata);
|
||||||
let new_value = match self.gas_left.checked_sub(&amount) {
|
let new_value = match self.gas_left.checked_sub(&amount) {
|
||||||
None => None,
|
None => None,
|
||||||
Some(val) if val.is_zero() => None,
|
Some(val) if val.is_zero() => None,
|
||||||
@@ -74,18 +140,6 @@ impl<T: Trait> GasMeter<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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
|
/// Allocate some amount of gas and perform some work with
|
||||||
/// a newly created nested gas meter.
|
/// a newly created nested gas meter.
|
||||||
///
|
///
|
||||||
@@ -108,6 +162,8 @@ impl<T: Trait> GasMeter<T> {
|
|||||||
limit: amount,
|
limit: amount,
|
||||||
gas_left: amount,
|
gas_left: amount,
|
||||||
gas_price: self.gas_price,
|
gas_price: self.gas_price,
|
||||||
|
#[cfg(test)]
|
||||||
|
tokens: Vec::new(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let r = f(Some(&mut nested));
|
let r = f(Some(&mut nested));
|
||||||
@@ -118,6 +174,10 @@ impl<T: Trait> GasMeter<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn gas_price(&self) -> T::Balance {
|
||||||
|
self.gas_price
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns how much gas left from the initial budget.
|
/// Returns how much gas left from the initial budget.
|
||||||
pub fn gas_left(&self) -> T::Gas {
|
pub fn gas_left(&self) -> T::Gas {
|
||||||
self.gas_left
|
self.gas_left
|
||||||
@@ -127,6 +187,11 @@ impl<T: Trait> GasMeter<T> {
|
|||||||
fn spent(&self) -> T::Gas {
|
fn spent(&self) -> T::Gas {
|
||||||
self.limit - self.gas_left
|
self.limit - self.gas_left
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub fn tokens(&self) -> &[ErasedToken] {
|
||||||
|
&self.tokens
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Buy the given amount of gas.
|
/// Buy the given amount of gas.
|
||||||
@@ -162,6 +227,8 @@ pub fn buy_gas<T: Trait>(
|
|||||||
limit: gas_limit,
|
limit: gas_limit,
|
||||||
gas_left: gas_limit,
|
gas_left: gas_limit,
|
||||||
gas_price,
|
gas_price,
|
||||||
|
#[cfg(test)]
|
||||||
|
tokens: Vec::new(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -179,3 +246,101 @@ pub fn refund_unused_gas<T: Trait>(transactor: &T::AccountId, gas_meter: GasMete
|
|||||||
<balances::Module<T>>::set_free_balance(transactor, b + refund);
|
<balances::Module<T>>::set_free_balance(transactor, b + refund);
|
||||||
<balances::Module<T>>::increase_total_stake_by(refund);
|
<balances::Module<T>>::increase_total_stake_by(refund);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A simple utility macro that helps to match against a
|
||||||
|
/// list of tokens.
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! match_tokens {
|
||||||
|
($tokens_iter:ident,) => {
|
||||||
|
};
|
||||||
|
($tokens_iter:ident, $x:expr, $($rest:tt)*) => {
|
||||||
|
{
|
||||||
|
let next = ($tokens_iter).next().unwrap();
|
||||||
|
let pattern = $x;
|
||||||
|
|
||||||
|
// Note that we don't specify the type name directly in this macro,
|
||||||
|
// we only have some expression $x of some type. At the same time, we
|
||||||
|
// have an iterator of Box<dyn Any> and to downcast we need to specify
|
||||||
|
// the type which we want downcast to.
|
||||||
|
//
|
||||||
|
// So what we do is we assign `_pattern_typed_next_ref` to the a variable which has
|
||||||
|
// the required type.
|
||||||
|
//
|
||||||
|
// Then we make `_pattern_typed_next_ref = token.downcast_ref()`. This makes
|
||||||
|
// rustc infer the type `T` (in `downcast_ref<T: Any>`) to be the same as in $x.
|
||||||
|
|
||||||
|
let mut _pattern_typed_next_ref = &pattern;
|
||||||
|
_pattern_typed_next_ref = match next.token.downcast_ref() {
|
||||||
|
Some(p) => {
|
||||||
|
assert_eq!(p, &pattern);
|
||||||
|
p
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
panic!("expected type {} got {}", stringify!($x), next.description);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
match_tokens!($tokens_iter, $($rest)*);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::{GasMeter, Token};
|
||||||
|
use tests::Test;
|
||||||
|
|
||||||
|
/// A trivial token that charges 1 unit of gas.
|
||||||
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||||
|
struct UnitToken;
|
||||||
|
impl Token<Test> for UnitToken {
|
||||||
|
type Metadata = ();
|
||||||
|
fn calculate_amount(&self, _metadata: &()) -> u64 { 1 }
|
||||||
|
}
|
||||||
|
|
||||||
|
struct DoubleTokenMetadata {
|
||||||
|
multiplier: u64,
|
||||||
|
}
|
||||||
|
/// A simple token that charges for the given amount multipled to
|
||||||
|
/// a multiplier taken from a given metadata.
|
||||||
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||||
|
struct DoubleToken(u64);
|
||||||
|
|
||||||
|
impl Token<Test> for DoubleToken {
|
||||||
|
type Metadata = DoubleTokenMetadata;
|
||||||
|
fn calculate_amount(&self, metadata: &DoubleTokenMetadata) -> u64 {
|
||||||
|
// Probably you want to use saturating mul in producation code.
|
||||||
|
self.0 * metadata.multiplier
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_works() {
|
||||||
|
let gas_meter = GasMeter::<Test>::with_limit(50000, 10);
|
||||||
|
assert_eq!(gas_meter.gas_left(), 50000);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn simple() {
|
||||||
|
let mut gas_meter = GasMeter::<Test>::with_limit(50000, 10);
|
||||||
|
|
||||||
|
let result = gas_meter.charge(&DoubleTokenMetadata { multiplier: 3 }, DoubleToken(10));
|
||||||
|
assert!(!result.is_out_of_gas());
|
||||||
|
|
||||||
|
assert_eq!(gas_meter.gas_left(), 49_970);
|
||||||
|
assert_eq!(gas_meter.spent(), 30);
|
||||||
|
assert_eq!(gas_meter.gas_price(), 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tracing() {
|
||||||
|
let mut gas_meter = GasMeter::<Test>::with_limit(50000, 10);
|
||||||
|
assert!(!gas_meter.charge(&(), UnitToken).is_out_of_gas());
|
||||||
|
assert!(!gas_meter
|
||||||
|
.charge(&DoubleTokenMetadata { multiplier: 3 }, DoubleToken(10))
|
||||||
|
.is_out_of_gas());
|
||||||
|
|
||||||
|
let mut tokens = gas_meter.tokens()[0..2].iter();
|
||||||
|
match_tokens!(tokens, UnitToken, DoubleToken(10),);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -39,7 +39,7 @@
|
|||||||
//! This module requires performing some finalization steps at the end of the block. If not performed
|
//! This module requires performing some finalization steps at the end of the block. If not performed
|
||||||
//! the module will have incorrect behavior.
|
//! the module will have incorrect behavior.
|
||||||
//!
|
//!
|
||||||
//! Call [`Module::execute`] at the end of the block. The order in relation to
|
//! Thus [`Module::on_finalise`] must be called at the end of the block. The order in relation to
|
||||||
//! the other module doesn't matter.
|
//! the other module doesn't matter.
|
||||||
//!
|
//!
|
||||||
//! ## Account killing
|
//! ## Account killing
|
||||||
@@ -48,7 +48,7 @@
|
|||||||
//! exsistential deposit) then it reaps the account. That will lead to deletion of the associated
|
//! exsistential deposit) then it reaps the account. That will lead to deletion of the associated
|
||||||
//! code and storage of the account.
|
//! code and storage of the account.
|
||||||
//!
|
//!
|
||||||
//! [`Module::execute`]: struct.Module.html#impl-OnFinalise
|
//! [`Module::on_finalise`]: struct.Module.html#impl-OnFinalise
|
||||||
|
|
||||||
#![cfg_attr(not(feature = "std"), no_std)]
|
#![cfg_attr(not(feature = "std"), no_std)]
|
||||||
|
|
||||||
@@ -83,39 +83,47 @@ extern crate assert_matches;
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
extern crate wabt;
|
extern crate wabt;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
#[macro_use]
|
||||||
|
extern crate hex_literal;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
mod gas;
|
||||||
|
|
||||||
mod account_db;
|
mod account_db;
|
||||||
mod exec;
|
mod exec;
|
||||||
mod vm;
|
mod wasm;
|
||||||
mod gas;
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
use exec::ExecutionContext;
|
use exec::ExecutionContext;
|
||||||
use account_db::{AccountDb, OverlayAccountDb};
|
use account_db::AccountDb;
|
||||||
|
|
||||||
use rstd::prelude::*;
|
use rstd::prelude::*;
|
||||||
use rstd::marker::PhantomData;
|
use rstd::marker::PhantomData;
|
||||||
use codec::{Codec, HasCompact};
|
use codec::{Codec, HasCompact};
|
||||||
use runtime_primitives::traits::{Hash, As, SimpleArithmetic, StaticLookup};
|
use runtime_primitives::traits::{Hash, As, SimpleArithmetic,Bounded, StaticLookup};
|
||||||
use runtime_support::dispatch::Result;
|
use runtime_support::dispatch::Result;
|
||||||
use runtime_support::{Parameter, StorageMap, StorageValue, StorageDoubleMap};
|
use runtime_support::{Parameter, StorageMap, StorageValue, StorageDoubleMap};
|
||||||
use system::ensure_signed;
|
use system::ensure_signed;
|
||||||
use runtime_io::{blake2_256, twox_128};
|
use runtime_io::{blake2_256, twox_128};
|
||||||
|
|
||||||
|
pub type CodeHash<T> = <T as system::Trait>::Hash;
|
||||||
|
|
||||||
pub trait Trait: balances::Trait {
|
pub trait Trait: balances::Trait {
|
||||||
/// Function type to get the contract address given the creator.
|
/// Function type to get the contract address given the creator.
|
||||||
type DetermineContractAddress: ContractAddressFor<Self::AccountId>;
|
type DetermineContractAddress: ContractAddressFor<CodeHash<Self>, Self::AccountId>;
|
||||||
|
|
||||||
// As<u32> is needed for wasm-utils
|
// As<u32> is needed for wasm-utils
|
||||||
type Gas: Parameter + Default + Codec + SimpleArithmetic + Copy + As<Self::Balance> + As<u64> + As<u32>;
|
type Gas: Parameter + Default + Codec + SimpleArithmetic + Bounded + Copy + As<Self::Balance> + As<u64> + As<u32>;
|
||||||
|
|
||||||
/// The overarching event type.
|
/// The overarching event type.
|
||||||
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
|
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait ContractAddressFor<AccountId: Sized> {
|
pub trait ContractAddressFor<CodeHash, AccountId: Sized> {
|
||||||
fn contract_address_for(code: &[u8], data: &[u8], origin: &AccountId) -> AccountId;
|
fn contract_address_for(code_hash: &CodeHash, data: &[u8], origin: &AccountId) -> AccountId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Simple contract address determintator.
|
/// Simple contract address determintator.
|
||||||
@@ -126,12 +134,11 @@ pub trait ContractAddressFor<AccountId: Sized> {
|
|||||||
/// Formula: `blake2_256(blake2_256(code) + blake2_256(data) + origin)`
|
/// Formula: `blake2_256(blake2_256(code) + blake2_256(data) + origin)`
|
||||||
pub struct SimpleAddressDeterminator<T: Trait>(PhantomData<T>);
|
pub struct SimpleAddressDeterminator<T: Trait>(PhantomData<T>);
|
||||||
|
|
||||||
impl<T: Trait> ContractAddressFor<T::AccountId> for SimpleAddressDeterminator<T>
|
impl<T: Trait> ContractAddressFor<CodeHash<T>, T::AccountId> for SimpleAddressDeterminator<T>
|
||||||
where
|
where
|
||||||
T::AccountId: From<T::Hash> + AsRef<[u8]>
|
T::AccountId: From<T::Hash> + AsRef<[u8]>
|
||||||
{
|
{
|
||||||
fn contract_address_for(code: &[u8], data: &[u8], origin: &T::AccountId) -> T::AccountId {
|
fn contract_address_for(code_hash: &CodeHash<T>, data: &[u8], origin: &T::AccountId) -> T::AccountId {
|
||||||
let code_hash = T::Hashing::hash(code);
|
|
||||||
let data_hash = T::Hashing::hash(data);
|
let data_hash = T::Hashing::hash(data);
|
||||||
|
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
@@ -147,8 +154,43 @@ decl_module! {
|
|||||||
/// Contracts module.
|
/// Contracts module.
|
||||||
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
|
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
|
||||||
fn deposit_event<T>() = default;
|
fn deposit_event<T>() = default;
|
||||||
// TODO: Change AccountId to staking::Address
|
|
||||||
/// Make a call to a specified account, optionally transferring some balance.
|
/// Updates the schedule for metering contracts.
|
||||||
|
///
|
||||||
|
/// The schedule must have a greater version than the stored schedule.
|
||||||
|
fn update_schedule(schedule: Schedule<T::Gas>) -> Result {
|
||||||
|
if <Module<T>>::current_schedule().version >= schedule.version {
|
||||||
|
return Err("new schedule must have a greater version than current");
|
||||||
|
}
|
||||||
|
|
||||||
|
Self::deposit_event(RawEvent::ScheduleUpdated(schedule.version));
|
||||||
|
<CurrentSchedule<T>>::put(schedule);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Stores code in the storage. You can instantiate contracts only with stored code.
|
||||||
|
fn put_code(
|
||||||
|
origin,
|
||||||
|
gas_limit: <T::Gas as HasCompact>::Type,
|
||||||
|
code: Vec<u8>
|
||||||
|
) -> Result {
|
||||||
|
let origin = ensure_signed(origin)?;
|
||||||
|
let gas_limit = gas_limit.into();
|
||||||
|
let schedule = <Module<T>>::current_schedule();
|
||||||
|
|
||||||
|
let mut gas_meter = gas::buy_gas::<T>(&origin, gas_limit)?;
|
||||||
|
|
||||||
|
let result = wasm::save_code::<T>(code, &mut gas_meter, &schedule);
|
||||||
|
if let Ok(code_hash) = result {
|
||||||
|
Self::deposit_event(RawEvent::CodeStored(code_hash));
|
||||||
|
}
|
||||||
|
|
||||||
|
gas::refund_unused_gas::<T>(&origin, gas_meter);
|
||||||
|
|
||||||
|
result.map(|_| ())
|
||||||
|
}
|
||||||
|
|
||||||
/// Make a call to a specified account, optionally transferring some balance.
|
/// Make a call to a specified account, optionally transferring some balance.
|
||||||
fn call(
|
fn call(
|
||||||
origin,
|
origin,
|
||||||
@@ -169,16 +211,11 @@ decl_module! {
|
|||||||
let mut gas_meter = gas::buy_gas::<T>(&origin, gas_limit)?;
|
let mut gas_meter = gas::buy_gas::<T>(&origin, gas_limit)?;
|
||||||
|
|
||||||
let cfg = Config::preload();
|
let cfg = Config::preload();
|
||||||
let mut ctx = ExecutionContext {
|
let vm = ::wasm::WasmVm::new(&cfg.schedule);
|
||||||
self_account: origin.clone(),
|
let loader = ::wasm::WasmLoader::new(&cfg.schedule);
|
||||||
depth: 0,
|
let mut ctx = ExecutionContext::top_level(origin.clone(), &cfg, &vm, &loader);
|
||||||
overlay: OverlayAccountDb::<T>::new(&account_db::DirectAccountDb),
|
|
||||||
events: Vec::new(),
|
|
||||||
config: &cfg,
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut output_data = Vec::new();
|
let result = ctx.call(dest, value, &mut gas_meter, &data, exec::EmptyOutputBuf::new());
|
||||||
let result = ctx.call(origin.clone(), dest, value, &mut gas_meter, &data, &mut output_data);
|
|
||||||
|
|
||||||
if let Ok(_) = result {
|
if let Ok(_) = result {
|
||||||
// Commit all changes that made it thus far into the persistant storage.
|
// Commit all changes that made it thus far into the persistant storage.
|
||||||
@@ -210,7 +247,7 @@ decl_module! {
|
|||||||
origin,
|
origin,
|
||||||
endowment: <T::Balance as HasCompact>::Type,
|
endowment: <T::Balance as HasCompact>::Type,
|
||||||
gas_limit: <T::Gas as HasCompact>::Type,
|
gas_limit: <T::Gas as HasCompact>::Type,
|
||||||
ctor_code: Vec<u8>,
|
code_hash: CodeHash<T>,
|
||||||
data: Vec<u8>
|
data: Vec<u8>
|
||||||
) -> Result {
|
) -> Result {
|
||||||
let origin = ensure_signed(origin)?;
|
let origin = ensure_signed(origin)?;
|
||||||
@@ -224,23 +261,17 @@ decl_module! {
|
|||||||
let mut gas_meter = gas::buy_gas::<T>(&origin, gas_limit)?;
|
let mut gas_meter = gas::buy_gas::<T>(&origin, gas_limit)?;
|
||||||
|
|
||||||
let cfg = Config::preload();
|
let cfg = Config::preload();
|
||||||
let mut ctx = ExecutionContext {
|
let vm = ::wasm::WasmVm::new(&cfg.schedule);
|
||||||
self_account: origin.clone(),
|
let loader = ::wasm::WasmLoader::new(&cfg.schedule);
|
||||||
depth: 0,
|
let mut ctx = ExecutionContext::top_level(origin.clone(), &cfg, &vm, &loader);
|
||||||
overlay: OverlayAccountDb::<T>::new(&account_db::DirectAccountDb),
|
let result = ctx.instantiate(endowment, &mut gas_meter, &code_hash, &data);
|
||||||
events: Vec::new(),
|
|
||||||
config: &cfg,
|
|
||||||
};
|
|
||||||
let result = ctx.create(origin.clone(), endowment, &mut gas_meter, &ctor_code, &data);
|
|
||||||
|
|
||||||
if let Ok(ref r) = result {
|
if let Ok(_) = result {
|
||||||
// Commit all changes that made it thus far into the persistant storage.
|
// Commit all changes that made it thus far into the persistant storage.
|
||||||
account_db::DirectAccountDb.commit(ctx.overlay.into_change_set());
|
account_db::DirectAccountDb.commit(ctx.overlay.into_change_set());
|
||||||
|
|
||||||
// Then deposit all events produced.
|
// Then deposit all events produced.
|
||||||
ctx.events.into_iter().for_each(Self::deposit_event);
|
ctx.events.into_iter().for_each(Self::deposit_event);
|
||||||
|
|
||||||
Self::deposit_event(RawEvent::Created(origin.clone(), r.address.clone()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Refund cost of the unused gas.
|
// Refund cost of the unused gas.
|
||||||
@@ -262,13 +293,20 @@ decl_event! {
|
|||||||
pub enum Event<T>
|
pub enum Event<T>
|
||||||
where
|
where
|
||||||
<T as balances::Trait>::Balance,
|
<T as balances::Trait>::Balance,
|
||||||
<T as system::Trait>::AccountId
|
<T as system::Trait>::AccountId,
|
||||||
|
<T as system::Trait>::Hash
|
||||||
{
|
{
|
||||||
/// Transfer happened `from` -> `to` with given `value` as part of a `message-call` or `create`.
|
/// Transfer happened `from` -> `to` with given `value` as part of a `message-call` or `create`.
|
||||||
Transfer(AccountId, AccountId, Balance),
|
Transfer(AccountId, AccountId, Balance),
|
||||||
|
|
||||||
/// Contract deployed by address at the specified address.
|
/// Contract deployed by address at the specified address.
|
||||||
Created(AccountId, AccountId),
|
Instantiated(AccountId, AccountId),
|
||||||
|
|
||||||
|
/// Code with the specified hash has been stored.
|
||||||
|
CodeStored(Hash),
|
||||||
|
|
||||||
|
/// Triggered when the current schedule is updated.
|
||||||
|
ScheduleUpdated(u32),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -290,8 +328,12 @@ decl_storage! {
|
|||||||
GasSpent get(gas_spent): T::Gas;
|
GasSpent get(gas_spent): T::Gas;
|
||||||
/// Current cost schedule for contracts.
|
/// Current cost schedule for contracts.
|
||||||
CurrentSchedule get(current_schedule) config(): Schedule<T::Gas> = Schedule::default();
|
CurrentSchedule get(current_schedule) config(): Schedule<T::Gas> = Schedule::default();
|
||||||
/// The code associated with an account.
|
/// The code associated with a given account.
|
||||||
pub CodeOf: map T::AccountId => Vec<u8>; // TODO Vec<u8> values should be optimised to not do a length prefix.
|
pub CodeHashOf: map T::AccountId => Option<CodeHash<T>>;
|
||||||
|
/// A mapping from an original code hash to the original code, untouched by instrumentation.
|
||||||
|
pub PristineCode: map CodeHash<T> => Option<Vec<u8>>;
|
||||||
|
/// A mapping between an original code hash and instrumented wasm code, ready for the execution.
|
||||||
|
pub CodeStorage: map CodeHash<T> => Option<wasm::PrefabWasmModule>;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -322,7 +364,7 @@ impl<T: Trait> StorageDoubleMap for StorageOf<T> {
|
|||||||
|
|
||||||
impl<T: Trait> balances::OnFreeBalanceZero<T::AccountId> for Module<T> {
|
impl<T: Trait> balances::OnFreeBalanceZero<T::AccountId> for Module<T> {
|
||||||
fn on_free_balance_zero(who: &T::AccountId) {
|
fn on_free_balance_zero(who: &T::AccountId) {
|
||||||
<CodeOf<T>>::remove(who);
|
<CodeHashOf<T>>::remove(who);
|
||||||
<StorageOf<T>>::remove_prefix(who.clone());
|
<StorageOf<T>>::remove_prefix(who.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -335,11 +377,11 @@ pub struct Config<T: Trait> {
|
|||||||
pub schedule: Schedule<T::Gas>,
|
pub schedule: Schedule<T::Gas>,
|
||||||
pub existential_deposit: T::Balance,
|
pub existential_deposit: T::Balance,
|
||||||
pub max_depth: u32,
|
pub max_depth: u32,
|
||||||
pub contract_account_create_fee: T::Balance,
|
pub contract_account_instantiate_fee: T::Balance,
|
||||||
pub account_create_fee: T::Balance,
|
pub account_create_fee: T::Balance,
|
||||||
pub transfer_fee: T::Balance,
|
pub transfer_fee: T::Balance,
|
||||||
pub call_base_fee: T::Gas,
|
pub call_base_fee: T::Gas,
|
||||||
pub create_base_fee: T::Gas,
|
pub instantiate_base_fee: T::Gas,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Trait> Config<T> {
|
impl<T: Trait> Config<T> {
|
||||||
@@ -348,19 +390,25 @@ impl<T: Trait> Config<T> {
|
|||||||
schedule: <Module<T>>::current_schedule(),
|
schedule: <Module<T>>::current_schedule(),
|
||||||
existential_deposit: <balances::Module<T>>::existential_deposit(),
|
existential_deposit: <balances::Module<T>>::existential_deposit(),
|
||||||
max_depth: <Module<T>>::max_depth(),
|
max_depth: <Module<T>>::max_depth(),
|
||||||
contract_account_create_fee: <Module<T>>::contract_fee(),
|
contract_account_instantiate_fee: <Module<T>>::contract_fee(),
|
||||||
account_create_fee: <balances::Module<T>>::creation_fee(),
|
account_create_fee: <balances::Module<T>>::creation_fee(),
|
||||||
transfer_fee: <balances::Module<T>>::transfer_fee(),
|
transfer_fee: <balances::Module<T>>::transfer_fee(),
|
||||||
call_base_fee: <Module<T>>::call_base_fee(),
|
call_base_fee: <Module<T>>::call_base_fee(),
|
||||||
create_base_fee: <Module<T>>::create_base_fee(),
|
instantiate_base_fee: <Module<T>>::create_base_fee(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Definition of the cost schedule and other parameterizations for wasm vm.
|
/// Definition of the cost schedule and other parameterizations for wasm vm.
|
||||||
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
|
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
|
||||||
#[derive(Clone, Encode, Decode)]
|
#[derive(Clone, Encode, Decode, PartialEq, Eq)]
|
||||||
pub struct Schedule<Gas> {
|
pub struct Schedule<Gas> {
|
||||||
|
/// Version of the schedule.
|
||||||
|
pub version: u32,
|
||||||
|
|
||||||
|
/// Cost of putting a byte of code into the storage.
|
||||||
|
pub put_code_per_byte_cost: Gas,
|
||||||
|
|
||||||
/// Gas cost of a growing memory by single page.
|
/// Gas cost of a growing memory by single page.
|
||||||
pub grow_mem_cost: Gas,
|
pub grow_mem_cost: Gas,
|
||||||
|
|
||||||
@@ -371,10 +419,10 @@ pub struct Schedule<Gas> {
|
|||||||
pub return_data_per_byte_cost: Gas,
|
pub return_data_per_byte_cost: Gas,
|
||||||
|
|
||||||
/// Gas cost per one byte read from the sandbox memory.
|
/// Gas cost per one byte read from the sandbox memory.
|
||||||
sandbox_data_read_cost: Gas,
|
pub sandbox_data_read_cost: Gas,
|
||||||
|
|
||||||
/// Gas cost per one byte written to the sandbox memory.
|
/// Gas cost per one byte written to the sandbox memory.
|
||||||
sandbox_data_write_cost: Gas,
|
pub sandbox_data_write_cost: Gas,
|
||||||
|
|
||||||
/// How tall the stack is allowed to grow?
|
/// How tall the stack is allowed to grow?
|
||||||
///
|
///
|
||||||
@@ -382,7 +430,7 @@ pub struct Schedule<Gas> {
|
|||||||
/// how the stack frame cost is calculated.
|
/// how the stack frame cost is calculated.
|
||||||
pub max_stack_height: u32,
|
pub max_stack_height: u32,
|
||||||
|
|
||||||
//// What is the maximal memory pages amount is allowed to have for
|
/// What is the maximal memory pages amount is allowed to have for
|
||||||
/// a contract.
|
/// a contract.
|
||||||
pub max_memory_pages: u32,
|
pub max_memory_pages: u32,
|
||||||
}
|
}
|
||||||
@@ -390,6 +438,8 @@ pub struct Schedule<Gas> {
|
|||||||
impl<Gas: As<u64>> Default for Schedule<Gas> {
|
impl<Gas: As<u64>> Default for Schedule<Gas> {
|
||||||
fn default() -> Schedule<Gas> {
|
fn default() -> Schedule<Gas> {
|
||||||
Schedule {
|
Schedule {
|
||||||
|
version: 0,
|
||||||
|
put_code_per_byte_cost: Gas::sa(1),
|
||||||
grow_mem_cost: Gas::sa(1),
|
grow_mem_cost: Gas::sa(1),
|
||||||
regular_op_cost: Gas::sa(1),
|
regular_op_cost: Gas::sa(1),
|
||||||
return_data_per_byte_cost: Gas::sa(1),
|
return_data_per_byte_cost: Gas::sa(1),
|
||||||
|
|||||||
@@ -14,6 +14,11 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
// TODO: #1417 Add more integration tests
|
||||||
|
// also remove the #![allow(unused)] below.
|
||||||
|
|
||||||
|
#![allow(unused)]
|
||||||
|
|
||||||
use runtime_io::with_externalities;
|
use runtime_io::with_externalities;
|
||||||
use runtime_primitives::testing::{Digest, DigestItem, H256, Header};
|
use runtime_primitives::testing::{Digest, DigestItem, H256, Header};
|
||||||
use runtime_primitives::traits::{BlakeTwo256, IdentityLookup};
|
use runtime_primitives::traits::{BlakeTwo256, IdentityLookup};
|
||||||
@@ -23,8 +28,8 @@ use substrate_primitives::{Blake2Hasher};
|
|||||||
use system::{Phase, EventRecord};
|
use system::{Phase, EventRecord};
|
||||||
use wabt;
|
use wabt;
|
||||||
use {
|
use {
|
||||||
runtime_io, balances, system, CodeOf, ContractAddressFor,
|
balances, runtime_io, system, ContractAddressFor, GenesisConfig, Module, RawEvent, StorageOf,
|
||||||
GenesisConfig, Module, StorageOf, Trait, RawEvent,
|
Trait,
|
||||||
};
|
};
|
||||||
|
|
||||||
impl_outer_origin! {
|
impl_outer_origin! {
|
||||||
@@ -32,6 +37,9 @@ impl_outer_origin! {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mod contract {
|
mod contract {
|
||||||
|
// Re-export contents of the root. This basically
|
||||||
|
// needs to give a name for the current crate.
|
||||||
|
// This hack is required for `impl_outer_event!`.
|
||||||
pub use super::super::*;
|
pub use super::super::*;
|
||||||
}
|
}
|
||||||
impl_outer_event! {
|
impl_outer_event! {
|
||||||
@@ -73,13 +81,17 @@ type Contract = Module<Test>;
|
|||||||
type System = system::Module<Test>;
|
type System = system::Module<Test>;
|
||||||
|
|
||||||
pub struct DummyContractAddressFor;
|
pub struct DummyContractAddressFor;
|
||||||
impl ContractAddressFor<u64> for DummyContractAddressFor {
|
impl ContractAddressFor<H256, u64> for DummyContractAddressFor {
|
||||||
fn contract_address_for(_code: &[u8], _data: &[u8], origin: &u64) -> u64 {
|
fn contract_address_for(_code_hash: &H256, _data: &[u8], origin: &u64) -> u64 {
|
||||||
origin + 1
|
*origin + 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ExtBuilder {
|
const ALICE: u64 = 1;
|
||||||
|
const BOB: u64 = 2;
|
||||||
|
const CHARLIE: u64 = 3;
|
||||||
|
|
||||||
|
pub struct ExtBuilder {
|
||||||
existential_deposit: u64,
|
existential_deposit: u64,
|
||||||
gas_price: u64,
|
gas_price: u64,
|
||||||
block_gas_limit: u64,
|
block_gas_limit: u64,
|
||||||
@@ -98,30 +110,31 @@ impl Default for ExtBuilder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl ExtBuilder {
|
impl ExtBuilder {
|
||||||
fn existential_deposit(mut self, existential_deposit: u64) -> Self {
|
pub fn existential_deposit(mut self, existential_deposit: u64) -> Self {
|
||||||
self.existential_deposit = existential_deposit;
|
self.existential_deposit = existential_deposit;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
fn gas_price(mut self, gas_price: u64) -> Self {
|
pub fn gas_price(mut self, gas_price: u64) -> Self {
|
||||||
self.gas_price = gas_price;
|
self.gas_price = gas_price;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
fn block_gas_limit(mut self, block_gas_limit: u64) -> Self {
|
pub fn block_gas_limit(mut self, block_gas_limit: u64) -> Self {
|
||||||
self.block_gas_limit = block_gas_limit;
|
self.block_gas_limit = block_gas_limit;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
fn transfer_fee(mut self, transfer_fee: u64) -> Self {
|
pub fn transfer_fee(mut self, transfer_fee: u64) -> Self {
|
||||||
self.transfer_fee = transfer_fee;
|
self.transfer_fee = transfer_fee;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
fn creation_fee(mut self, creation_fee: u64) -> Self {
|
pub fn creation_fee(mut self, creation_fee: u64) -> Self {
|
||||||
self.creation_fee = creation_fee;
|
self.creation_fee = creation_fee;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
fn build(self) -> runtime_io::TestExternalities<Blake2Hasher> {
|
pub fn build(self) -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||||
let mut t = system::GenesisConfig::<Test>::default()
|
let mut t = system::GenesisConfig::<Test>::default()
|
||||||
.build_storage()
|
.build_storage()
|
||||||
.unwrap().0;
|
.unwrap()
|
||||||
|
.0;
|
||||||
t.extend(
|
t.extend(
|
||||||
balances::GenesisConfig::<Test> {
|
balances::GenesisConfig::<Test> {
|
||||||
balances: vec![],
|
balances: vec![],
|
||||||
@@ -130,8 +143,10 @@ impl ExtBuilder {
|
|||||||
existential_deposit: self.existential_deposit,
|
existential_deposit: self.existential_deposit,
|
||||||
transfer_fee: self.transfer_fee,
|
transfer_fee: self.transfer_fee,
|
||||||
creation_fee: self.creation_fee,
|
creation_fee: self.creation_fee,
|
||||||
}.build_storage()
|
}
|
||||||
.unwrap().0,
|
.build_storage()
|
||||||
|
.unwrap()
|
||||||
|
.0,
|
||||||
);
|
);
|
||||||
t.extend(
|
t.extend(
|
||||||
GenesisConfig::<Test> {
|
GenesisConfig::<Test> {
|
||||||
@@ -142,544 +157,33 @@ impl ExtBuilder {
|
|||||||
max_depth: 100,
|
max_depth: 100,
|
||||||
block_gas_limit: self.block_gas_limit,
|
block_gas_limit: self.block_gas_limit,
|
||||||
current_schedule: Default::default(),
|
current_schedule: Default::default(),
|
||||||
}.build_storage()
|
}
|
||||||
.unwrap().0,
|
.build_storage()
|
||||||
|
.unwrap()
|
||||||
|
.0,
|
||||||
);
|
);
|
||||||
runtime_io::TestExternalities::new(t)
|
runtime_io::TestExternalities::new(t)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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.into(), 100_000.into(), Vec::new()));
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
Balances::free_balance(&0),
|
|
||||||
// 3 - value sent with the transaction
|
|
||||||
// 2 * 26 - gas used by the contract (26) 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 * 26) - (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,
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_eq!(System::events(), vec![
|
|
||||||
EventRecord {
|
|
||||||
phase: Phase::ApplyExtrinsic(0),
|
|
||||||
event: MetaEvent::balances(
|
|
||||||
balances::RawEvent::NewAccount(
|
|
||||||
CONTRACT_SHOULD_TRANSFER_TO,
|
|
||||||
6
|
|
||||||
)
|
|
||||||
),
|
|
||||||
},
|
|
||||||
EventRecord {
|
|
||||||
phase: Phase::ApplyExtrinsic(0),
|
|
||||||
event: MetaEvent::contract(RawEvent::Transfer(0, 1, 3)),
|
|
||||||
},
|
|
||||||
EventRecord {
|
|
||||||
phase: Phase::ApplyExtrinsic(0),
|
|
||||||
event: MetaEvent::contract(RawEvent::Transfer(1, CONTRACT_SHOULD_TRANSFER_TO, 6)),
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn contract_transfer_to_death() {
|
|
||||||
const CONTRACT_SHOULD_TRANSFER_VALUE: u64 = 6;
|
|
||||||
|
|
||||||
let code_transfer = wabt::wat2wasm(CODE_TRANSFER).unwrap();
|
|
||||||
|
|
||||||
with_externalities(&mut ExtBuilder::default().existential_deposit(5).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, 6);
|
|
||||||
Balances::increase_total_stake_by(6);
|
|
||||||
<StorageOf<Test>>::insert(1, b"foo".to_vec(), b"1".to_vec());
|
|
||||||
|
|
||||||
assert_ok!(Contract::call(Origin::signed(0), 1, 0.into(), 100_000.into(), Vec::new()));
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
Balances::free_balance(&0),
|
|
||||||
// 2 * 26 - gas used by the contract (26) 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 - (2 * 26) - (2 * 135) - (2 * 135),
|
|
||||||
);
|
|
||||||
|
|
||||||
assert!(!<CodeOf<Test>>::exists(1));
|
|
||||||
assert!(!<StorageOf<Test>>::exists(1, b"foo".to_vec()));
|
|
||||||
assert_eq!(Balances::free_balance(&1), 0);
|
|
||||||
|
|
||||||
assert_eq!(Balances::free_balance(&9), CONTRACT_SHOULD_TRANSFER_VALUE);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn contract_transfer_takes_creation_fee() {
|
|
||||||
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().creation_fee(105).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.into(), 100_000.into(), Vec::new()));
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
Balances::free_balance(&0),
|
|
||||||
// 3 - value sent with the transaction
|
|
||||||
// 2 * 26 - gas used by the contract (26) multiplied by gas price (2)
|
|
||||||
// 2 * 135 - base gas fee for call (by transaction)
|
|
||||||
// 2 * 135 - base gas fee for call (by the contract)
|
|
||||||
// 104 - (rounded) fee per creation (by the contract)
|
|
||||||
100_000_000 - 3 - (2 * 26) - (2 * 135) - (2 * 135) - 104,
|
|
||||||
);
|
|
||||||
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_takes_transfer_fee() {
|
|
||||||
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().creation_fee(105).transfer_fee(45).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);
|
|
||||||
|
|
||||||
// Create destination account here so we can check that transfer fee
|
|
||||||
// is charged (and creation fee is not).
|
|
||||||
Balances::set_free_balance(&CONTRACT_SHOULD_TRANSFER_TO, 25);
|
|
||||||
|
|
||||||
assert_ok!(Contract::call(Origin::signed(0), 1, 3.into(), 100_000.into(), Vec::new()));
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
Balances::free_balance(&0),
|
|
||||||
// 3 - value sent with the transaction
|
|
||||||
// 2 * 26 - gas used by the contract (26) multiplied by gas price (2)
|
|
||||||
// 2 * 135 - base gas fee for call (by transaction)
|
|
||||||
// 44 - (rounded from 45) fee per transfer (by transaction)
|
|
||||||
// 2 * 135 - base gas fee for call (by the contract)
|
|
||||||
// 44 - (rounded from 45) fee per transfer (by the contract)
|
|
||||||
100_000_000 - 3 - (2 * 26) - (2 * 135) - 44 - (2 * 135) - 44,
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
Balances::free_balance(&1),
|
|
||||||
11 + 3 - CONTRACT_SHOULD_TRANSFER_VALUE,
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
Balances::free_balance(&CONTRACT_SHOULD_TRANSFER_TO),
|
|
||||||
25 + 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.into(), (135 + 135 + 7).into(), 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);
|
|
||||||
|
|
||||||
assert_eq!(System::events(), vec![
|
|
||||||
EventRecord {
|
|
||||||
phase: Phase::ApplyExtrinsic(0),
|
|
||||||
event: MetaEvent::contract(RawEvent::Transfer(0, 1, 3)),
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
#[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.into(), 100_000.into(), Vec::new()));
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
Balances::free_balance(&0),
|
|
||||||
// 3 - value sent with the transaction
|
|
||||||
// 2 * 26 * 100 - gas used by the contract (26) 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 * 26 * 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.into(), 100_000.into(), Vec::new()));
|
|
||||||
|
|
||||||
let derived_address = <Test as Trait>::DetermineContractAddress::contract_address_for(
|
|
||||||
&code_ctor_transfer,
|
|
||||||
&[],
|
|
||||||
&1,
|
|
||||||
);
|
|
||||||
|
|
||||||
// 11 - value sent with the transaction
|
|
||||||
// 2 * 362 - gas spent by the deployer contract (362) 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 * 362) - (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);
|
|
||||||
|
|
||||||
assert_eq!(System::events(), vec![
|
|
||||||
EventRecord {
|
|
||||||
phase: Phase::ApplyExtrinsic(0),
|
|
||||||
event: MetaEvent::balances(
|
|
||||||
balances::RawEvent::NewAccount(
|
|
||||||
derived_address,
|
|
||||||
3
|
|
||||||
)
|
|
||||||
),
|
|
||||||
},
|
|
||||||
EventRecord {
|
|
||||||
phase: Phase::ApplyExtrinsic(0),
|
|
||||||
event: MetaEvent::contract(RawEvent::Transfer(0, 1, 11)),
|
|
||||||
},
|
|
||||||
EventRecord {
|
|
||||||
phase: Phase::ApplyExtrinsic(0),
|
|
||||||
event: MetaEvent::contract(RawEvent::Transfer(1, 2, 3)),
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
|
|
||||||
// Initiate transfer to the newly created contract.
|
|
||||||
assert_ok!(Contract::call(Origin::signed(0), derived_address, 22.into(), 100_000.into(), Vec::new()));
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
Balances::free_balance(&0),
|
|
||||||
// 22 - value sent with the transaction
|
|
||||||
// (2 * 26) - 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 * 26) - (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.into(),
|
|
||||||
100_000.into(),
|
|
||||||
code_ctor_transfer.clone(),
|
|
||||||
Vec::new(),
|
|
||||||
));
|
|
||||||
|
|
||||||
// 11 - value sent with the transaction
|
|
||||||
// (3 * 129) - gas spent by the init_code.
|
|
||||||
// (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);
|
|
||||||
|
|
||||||
assert_eq!(System::events(), vec![
|
|
||||||
EventRecord {
|
|
||||||
phase: Phase::ApplyExtrinsic(0),
|
|
||||||
event: MetaEvent::contract(RawEvent::Transfer(0, derived_address, 11)),
|
|
||||||
},
|
|
||||||
EventRecord {
|
|
||||||
phase: Phase::ApplyExtrinsic(0),
|
|
||||||
event: MetaEvent::contract(RawEvent::Created(0, 1)),
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const CODE_NOP: &'static str = r#"
|
|
||||||
(module
|
|
||||||
(func (export "call")
|
|
||||||
nop
|
|
||||||
)
|
|
||||||
)
|
|
||||||
"#;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn refunds_unused_gas() {
|
fn refunds_unused_gas() {
|
||||||
let code_nop = wabt::wat2wasm(CODE_NOP).unwrap();
|
|
||||||
|
|
||||||
with_externalities(&mut ExtBuilder::default().build(), || {
|
with_externalities(&mut ExtBuilder::default().build(), || {
|
||||||
<CodeOf<Test>>::insert(1, code_nop.to_vec());
|
|
||||||
|
|
||||||
Balances::set_free_balance(&0, 100_000_000);
|
Balances::set_free_balance(&0, 100_000_000);
|
||||||
Balances::increase_total_stake_by(100_000_000);
|
Balances::increase_total_stake_by(100_000_000);
|
||||||
|
|
||||||
assert_ok!(Contract::call(Origin::signed(0), 1, 0.into(), 100_000.into(), Vec::new()));
|
assert_ok!(Contract::call(
|
||||||
|
Origin::signed(0),
|
||||||
assert_eq!(Balances::free_balance(&0), 100_000_000 - 4 - (2 * 135));
|
1,
|
||||||
});
|
0.into(),
|
||||||
}
|
100_000.into(),
|
||||||
|
Vec::new()
|
||||||
#[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.into(), 100_000.into(), Vec::new()));
|
|
||||||
|
|
||||||
assert_eq!(Balances::free_balance(&0), 100_000_000 - (2 * 135));
|
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.into(), 100_000.into(), 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]
|
#[test]
|
||||||
fn account_removal_removes_storage() {
|
fn account_removal_removes_storage() {
|
||||||
with_externalities(
|
with_externalities(
|
||||||
@@ -723,232 +227,74 @@ fn account_removal_removes_storage() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const CODE_UNREACHABLE: &'static str = r#"
|
const CODE_RETURN_FROM_START_FN: &str = r#"
|
||||||
(module
|
(module
|
||||||
(func (export "call")
|
(import "env" "ext_return" (func $ext_return (param i32 i32)))
|
||||||
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.into(), 100_000.into(), Vec::new()),
|
|
||||||
"vm execute returned error while call"
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_eq!(Balances::free_balance(&0), 100_000_000 - (4 * 3) - (4 * 135));
|
|
||||||
|
|
||||||
assert_eq!(System::events(), vec![]);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
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.into(), 50_000.into(), 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.into(), 50_001.into(), Vec::new()),
|
|
||||||
"block gas limit is reached"
|
|
||||||
);
|
|
||||||
|
|
||||||
// However, we can spend another 50_000
|
|
||||||
assert_err!(
|
|
||||||
Contract::call(Origin::signed(0), 1, 0.into(), 50_000.into(), 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))
|
(import "env" "memory" (memory 1 1))
|
||||||
|
|
||||||
(func (export "call")
|
(start $start)
|
||||||
(block $fail
|
(func $start
|
||||||
;; fail if ext_input_size != 4
|
(call $ext_return
|
||||||
(br_if $fail
|
(i32.const 8)
|
||||||
(i32.ne
|
(i32.const 4)
|
||||||
(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
|
(unreachable)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
(func (export "call")
|
||||||
|
(unreachable)
|
||||||
|
)
|
||||||
|
(func (export "deploy"))
|
||||||
|
|
||||||
|
(data (i32.const 8) "\01\02\03\04")
|
||||||
)
|
)
|
||||||
"#;
|
"#;
|
||||||
|
const HASH_RETURN_FROM_START_FN: [u8; 32] = hex!("e6411d12daa2a19e4e9c7d8306c31c7d53a352cb8ed84385c8a1d48fc232e708");
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn input_data() {
|
fn instantiate_and_call() {
|
||||||
let code_input_data = wabt::wat2wasm(CODE_INPUT_DATA).unwrap();
|
let wasm = wabt::wat2wasm(CODE_RETURN_FROM_START_FN).unwrap();
|
||||||
|
|
||||||
with_externalities(
|
with_externalities(
|
||||||
&mut ExtBuilder::default().build(),
|
&mut ExtBuilder::default().existential_deposit(100).build(),
|
||||||
|| {
|
|| {
|
||||||
<CodeOf<Test>>::insert(1, code_input_data.to_vec());
|
Balances::set_free_balance(&ALICE, 1_000_000);
|
||||||
|
Balances::increase_total_stake_by(1_000_000);
|
||||||
|
|
||||||
Balances::set_free_balance(&0, 100_000_000);
|
assert_ok!(Contract::put_code(
|
||||||
Balances::increase_total_stake_by(100_000_000);
|
Origin::signed(ALICE),
|
||||||
|
100_000.into(),
|
||||||
|
wasm,
|
||||||
|
));
|
||||||
|
|
||||||
assert_ok!(Contract::call(Origin::signed(0), 1, 0.into(), 50_000.into(), vec![0, 1, 2, 3]));
|
assert_ok!(Contract::create(
|
||||||
|
Origin::signed(ALICE),
|
||||||
|
100.into(),
|
||||||
|
100_000.into(),
|
||||||
|
HASH_RETURN_FROM_START_FN.into(),
|
||||||
|
vec![],
|
||||||
|
));
|
||||||
|
|
||||||
// all asserts are made within contract code itself.
|
assert_eq!(System::events(), vec![
|
||||||
|
EventRecord {
|
||||||
|
phase: Phase::ApplyExtrinsic(0),
|
||||||
|
event: MetaEvent::contract(RawEvent::CodeStored(HASH_RETURN_FROM_START_FN.into())),
|
||||||
|
},
|
||||||
|
EventRecord {
|
||||||
|
phase: Phase::ApplyExtrinsic(0),
|
||||||
|
event: MetaEvent::balances(
|
||||||
|
balances::RawEvent::NewAccount(BOB, 100)
|
||||||
|
)
|
||||||
|
},
|
||||||
|
EventRecord {
|
||||||
|
phase: Phase::ApplyExtrinsic(0),
|
||||||
|
event: MetaEvent::contract(RawEvent::Transfer(ALICE, BOB, 100))
|
||||||
|
},
|
||||||
|
EventRecord {
|
||||||
|
phase: Phase::ApplyExtrinsic(0),
|
||||||
|
event: MetaEvent::contract(RawEvent::Instantiated(ALICE, BOB))
|
||||||
|
}
|
||||||
|
]);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Stores the caller into the storage under the [0x00; 32] key in the contract's storage.
|
|
||||||
const CODE_CALLER_LOGGER: &'static str = r#"
|
|
||||||
(module
|
|
||||||
(import "env" "ext_caller" (func $ext_caller))
|
|
||||||
(import "env" "ext_scratch_size" (func $ext_scratch_size (result i32)))
|
|
||||||
(import "env" "ext_scratch_copy" (func $ext_scratch_copy (param i32 i32 i32)))
|
|
||||||
(import "env" "ext_set_storage" (func $ext_set_storage (param i32 i32 i32 i32)))
|
|
||||||
(import "env" "memory" (memory 1 1))
|
|
||||||
|
|
||||||
;; Memory layout
|
|
||||||
;; [0..32]: the storage key (passed as the key for ext_set_storage)
|
|
||||||
;; [32..40]: contents of the scratch buffer (which is expected to be 8 bytes long)
|
|
||||||
|
|
||||||
(func (export "call")
|
|
||||||
;; Fill the scratch buffer with the caller.
|
|
||||||
(call $ext_caller)
|
|
||||||
|
|
||||||
;; Copy contents of the scratch buffer into the contract's memory.
|
|
||||||
(call $ext_scratch_copy
|
|
||||||
(i32.const 32) ;; Store scratch's buffer contents at this address.
|
|
||||||
(i32.const 0) ;; Offset from the start of the scratch buffer.
|
|
||||||
(i32.const 8) ;; Count of bytes to copy.
|
|
||||||
)
|
|
||||||
|
|
||||||
(call $ext_set_storage
|
|
||||||
(i32.const 0) ;; The storage key to save the value at. 32 bytes long.
|
|
||||||
(i32.const 1) ;; value_not_null=1, i.e. we are not removing the value
|
|
||||||
(i32.const 32) ;; the pointer to the value to store
|
|
||||||
(i32.const 8) ;; the length of the value
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
"#;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn caller_top_level() {
|
|
||||||
let code_caller_logger = wabt::wat2wasm(CODE_CALLER_LOGGER).unwrap();
|
|
||||||
with_externalities(
|
|
||||||
&mut ExtBuilder::default().build(),
|
|
||||||
|| {
|
|
||||||
<CodeOf<Test>>::insert(1, code_caller_logger.to_vec());
|
|
||||||
|
|
||||||
Balances::set_free_balance(&2, 100_000_000);
|
|
||||||
Balances::increase_total_stake_by(100_000_000);
|
|
||||||
|
|
||||||
assert_ok!(Contract::call(Origin::signed(2), 1, 0.into(), 50_000.into(), vec![]));
|
|
||||||
|
|
||||||
// Load the zero-th slot of the storage of the caller logger contract.
|
|
||||||
// We verify here that the caller logger contract has witnessed the call coming from
|
|
||||||
// the account with address 0x02 (see the origin above) - the origin of the tx.
|
|
||||||
assert_eq!(
|
|
||||||
<StorageOf<Test>>::get(1, vec![0; 32]),
|
|
||||||
Some(vec![0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn caller_contract() {
|
|
||||||
const CONTRACT_SHOULD_TRANSFER_TO: u64 = 9;
|
|
||||||
|
|
||||||
let code_caller_logger = wabt::wat2wasm(CODE_CALLER_LOGGER).unwrap();
|
|
||||||
let code_transfer = wabt::wat2wasm(CODE_TRANSFER).unwrap();
|
|
||||||
|
|
||||||
with_externalities(&mut ExtBuilder::default().build(), || {
|
|
||||||
<CodeOf<Test>>::insert(1, code_transfer.to_vec());
|
|
||||||
<CodeOf<Test>>::insert(CONTRACT_SHOULD_TRANSFER_TO, code_caller_logger);
|
|
||||||
|
|
||||||
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.into(), 100_000.into(), Vec::new()));
|
|
||||||
|
|
||||||
// Load the zero-th slot of the storage of the caller logger contract.
|
|
||||||
// We verify here that the caller logger contract has witnessed the call coming from
|
|
||||||
// the caller contract - 0x01.
|
|
||||||
assert_eq!(
|
|
||||||
<StorageOf<Test>>::get(CONTRACT_SHOULD_TRANSFER_TO, vec![0; 32]),
|
|
||||||
Some(vec![0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,285 +0,0 @@
|
|||||||
// 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::{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, Schedule};
|
|
||||||
|
|
||||||
struct ContractModule<'a, Gas: '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>,
|
|
||||||
schedule: &'a Schedule<Gas>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, Gas: 'a + As<u32> + Clone> ContractModule<'a, Gas> {
|
|
||||||
fn new(original_code: &[u8], schedule: &'a Schedule<Gas>) -> Result<ContractModule<'a, Gas>, Error> {
|
|
||||||
let module =
|
|
||||||
elements::deserialize_buffer(original_code).map_err(|_| Error::Deserialization)?;
|
|
||||||
Ok(ContractModule {
|
|
||||||
module: Some(module),
|
|
||||||
schedule,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 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.schedule.regular_op_cost.clone().as_(), Default::default())
|
|
||||||
.with_grow_cost(self.schedule.grow_mem_cost.clone().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.schedule.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().as_bytes())
|
|
||||||
.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 `schedule`,
|
|
||||||
/// - 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],
|
|
||||||
schedule: &Schedule<<E::T as Trait>::Gas>,
|
|
||||||
env: &HostFunctionSet<E>,
|
|
||||||
) -> Result<PreparedContract, Error> {
|
|
||||||
let mut contract_module = ContractModule::new(original_code, schedule)?;
|
|
||||||
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 > schedule.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 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 schedule = Schedule::<u64>::default();
|
|
||||||
let env = ::vm::runtime::init_env();
|
|
||||||
prepare_contract::<MockExt>(wasm.as_ref(), &schedule, &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!(Schedule::<u64>::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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
// 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/>.
|
||||||
|
|
||||||
|
//! A module that implements instrumented code cache.
|
||||||
|
//!
|
||||||
|
//! - In order to run contract code we need to instrument it with gas metering.
|
||||||
|
//! To do that we need to provide the schedule which will supply exact gas costs values.
|
||||||
|
//! We cache this code in the storage saving the schedule version.
|
||||||
|
//! - Before running contract code we check if the cached code has the schedule version that is equal to the current saved schedule.
|
||||||
|
//! If it is equal then run the code, if it isn't reinstrument with the current schedule.
|
||||||
|
//! - When we update the schedule we want it to have strictly greater version than the current saved one:
|
||||||
|
//! 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 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.
|
||||||
|
///
|
||||||
|
/// Specifies the code length in bytes.
|
||||||
|
#[cfg_attr(test, derive(Debug, PartialEq, Eq))]
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
|
pub struct PutCodeToken(u64);
|
||||||
|
|
||||||
|
impl<T: Trait> Token<T> for PutCodeToken {
|
||||||
|
type Metadata = Schedule<T::Gas>;
|
||||||
|
|
||||||
|
fn calculate_amount(&self, metadata: &Schedule<T::Gas>) -> T::Gas {
|
||||||
|
let code_len_in_gas = <T::Gas as As<u64>>::sa(self.0);
|
||||||
|
metadata
|
||||||
|
.put_code_per_byte_cost
|
||||||
|
.checked_mul(&code_len_in_gas)
|
||||||
|
.unwrap_or_else(|| Bounded::max_value())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Put code in the storage. The hash of code is used as a key and is returned
|
||||||
|
/// as a result of this function.
|
||||||
|
///
|
||||||
|
/// This function instruments the given code and caches it in the storage.
|
||||||
|
pub fn save<T: Trait>(
|
||||||
|
original_code: Vec<u8>,
|
||||||
|
gas_meter: &mut GasMeter<T>,
|
||||||
|
schedule: &Schedule<T::Gas>,
|
||||||
|
) -> Result<CodeHash<T>, &'static str> {
|
||||||
|
// The first time instrumentation is on the user. However, consequent reinstrumentation
|
||||||
|
// due to the schedule changes is on governance system.
|
||||||
|
if gas_meter
|
||||||
|
.charge(schedule, PutCodeToken(original_code.len() as u64))
|
||||||
|
.is_out_of_gas()
|
||||||
|
{
|
||||||
|
return Err("there is not enough gas for storing the code");
|
||||||
|
}
|
||||||
|
|
||||||
|
let prefab_module = prepare::prepare_contract::<T, Env>(&original_code, schedule)?;
|
||||||
|
let code_hash = T::Hashing::hash(&original_code);
|
||||||
|
|
||||||
|
// TODO: #1416 validate the code. If the code is not valid, then don't store it.
|
||||||
|
|
||||||
|
<CodeStorage<T>>::insert(code_hash, prefab_module);
|
||||||
|
<PristineCode<T>>::insert(code_hash, original_code);
|
||||||
|
|
||||||
|
Ok(code_hash)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Load code with the given code hash.
|
||||||
|
///
|
||||||
|
/// If the module was instrumented with a lower version of schedule than
|
||||||
|
/// the current one given as an argument, then this function will perform
|
||||||
|
/// re-instrumentation and update the cache in the storage.
|
||||||
|
pub fn load<T: Trait>(
|
||||||
|
code_hash: &CodeHash<T>,
|
||||||
|
schedule: &Schedule<T::Gas>,
|
||||||
|
) -> Result<PrefabWasmModule, &'static str> {
|
||||||
|
let mut prefab_module =
|
||||||
|
<CodeStorage<T>>::get(code_hash).ok_or_else(|| "code is not found")?;
|
||||||
|
|
||||||
|
if prefab_module.schedule_version < schedule.version {
|
||||||
|
// The current schedule version is greater than the version of the one cached
|
||||||
|
// in the storage.
|
||||||
|
//
|
||||||
|
// We need to re-instrument the code with the latest schedule here.
|
||||||
|
let original_code =
|
||||||
|
<PristineCode<T>>::get(code_hash).ok_or_else(|| "pristine code is not found")?;
|
||||||
|
prefab_module = prepare::prepare_contract::<T, Env>(&original_code, schedule)?;
|
||||||
|
<CodeStorage<T>>::insert(code_hash, prefab_module.clone());
|
||||||
|
}
|
||||||
|
Ok(prefab_module)
|
||||||
|
}
|
||||||
+71
-32
@@ -17,12 +17,12 @@
|
|||||||
//! Definition of macros that hides boilerplate of defining external environment
|
//! Definition of macros that hides boilerplate of defining external environment
|
||||||
//! for a wasm module.
|
//! for a wasm module.
|
||||||
//!
|
//!
|
||||||
//! Typically you should use `define_env` macro.
|
//! Most likely you should use `define_env` macro.
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! convert_args {
|
macro_rules! convert_args {
|
||||||
() => (vec![]);
|
() => (vec![]);
|
||||||
( $( $t:ty ),* ) => ( vec![ $( { use $crate::vm::env_def::ConvertibleToWasm; <$t>::VALUE_TYPE }, )* ] );
|
( $( $t:ty ),* ) => ( vec![ $( { use $crate::wasm::env_def::ConvertibleToWasm; <$t>::VALUE_TYPE }, )* ] );
|
||||||
}
|
}
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
@@ -36,19 +36,39 @@ macro_rules! gen_signature {
|
|||||||
( ( $( $params: ty ),* ) -> $returns: ty ) => (
|
( ( $( $params: ty ),* ) -> $returns: ty ) => (
|
||||||
{
|
{
|
||||||
$crate::parity_wasm::elements::FunctionType::new(convert_args!($($params),*), Some({
|
$crate::parity_wasm::elements::FunctionType::new(convert_args!($($params),*), Some({
|
||||||
use $crate::vm::env_def::ConvertibleToWasm; <$returns>::VALUE_TYPE
|
use $crate::wasm::env_def::ConvertibleToWasm; <$returns>::VALUE_TYPE
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! gen_signature_dispatch {
|
||||||
|
(
|
||||||
|
$needle_name:ident,
|
||||||
|
$needle_sig:ident ;
|
||||||
|
$name:ident
|
||||||
|
( $ctx:ident $( , $names:ident : $params:ty )* ) $( -> $returns:ty )* , $($rest:tt)* ) => {
|
||||||
|
if stringify!($name).as_bytes() == $needle_name {
|
||||||
|
let signature = gen_signature!( ( $( $params ),* ) $( -> $returns )* );
|
||||||
|
if $needle_sig == &signature {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
gen_signature_dispatch!($needle_name, $needle_sig ; $($rest)*);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
( $needle_name:ident, $needle_sig:ident ; ) => {
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/// Unmarshall arguments and then execute `body` expression and return its result.
|
/// Unmarshall arguments and then execute `body` expression and return its result.
|
||||||
macro_rules! unmarshall_then_body {
|
macro_rules! unmarshall_then_body {
|
||||||
( $body:tt, $ctx:ident, $args_iter:ident, $( $names:ident : $params:ty ),* ) => ({
|
( $body:tt, $ctx:ident, $args_iter:ident, $( $names:ident : $params:ty ),* ) => ({
|
||||||
$(
|
$(
|
||||||
let $names : <$params as $crate::vm::env_def::ConvertibleToWasm>::NativeType =
|
let $names : <$params as $crate::wasm::env_def::ConvertibleToWasm>::NativeType =
|
||||||
$args_iter.next()
|
$args_iter.next()
|
||||||
.and_then(|v| <$params as $crate::vm::env_def::ConvertibleToWasm>
|
.and_then(|v| <$params as $crate::wasm::env_def::ConvertibleToWasm>
|
||||||
::from_typed_value(v.clone()))
|
::from_typed_value(v.clone()))
|
||||||
.expect(
|
.expect(
|
||||||
"precondition: all imports should be checked against the signatures of corresponding
|
"precondition: all imports should be checked against the signatures of corresponding
|
||||||
@@ -84,16 +104,16 @@ where
|
|||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! unmarshall_then_body_then_marshall {
|
macro_rules! unmarshall_then_body_then_marshall {
|
||||||
( $args_iter:ident, $ctx:ident, ( $( $names:ident : $params:ty ),* ) -> $returns:ty => $body:tt ) => ({
|
( $args_iter:ident, $ctx:ident, ( $( $names:ident : $params:ty ),* ) -> $returns:ty => $body:tt ) => ({
|
||||||
let body = $crate::vm::env_def::macros::constrain_closure::<
|
let body = $crate::wasm::env_def::macros::constrain_closure::<
|
||||||
<$returns as $crate::vm::env_def::ConvertibleToWasm>::NativeType, _
|
<$returns as $crate::wasm::env_def::ConvertibleToWasm>::NativeType, _
|
||||||
>(|| {
|
>(|| {
|
||||||
unmarshall_then_body!($body, $ctx, $args_iter, $( $names : $params ),*)
|
unmarshall_then_body!($body, $ctx, $args_iter, $( $names : $params ),*)
|
||||||
});
|
});
|
||||||
let r = body()?;
|
let r = body()?;
|
||||||
return Ok($crate::sandbox::ReturnValue::Value({ use $crate::vm::env_def::ConvertibleToWasm; r.to_typed_value() }))
|
return Ok($crate::sandbox::ReturnValue::Value({ use $crate::wasm::env_def::ConvertibleToWasm; r.to_typed_value() }))
|
||||||
});
|
});
|
||||||
( $args_iter:ident, $ctx:ident, ( $( $names:ident : $params:ty ),* ) => $body:tt ) => ({
|
( $args_iter:ident, $ctx:ident, ( $( $names:ident : $params:ty ),* ) => $body:tt ) => ({
|
||||||
let body = $crate::vm::env_def::macros::constrain_closure::<(), _>(|| {
|
let body = $crate::wasm::env_def::macros::constrain_closure::<(), _>(|| {
|
||||||
unmarshall_then_body!($body, $ctx, $args_iter, $( $names : $params ),*)
|
unmarshall_then_body!($body, $ctx, $args_iter, $( $names : $params ),*)
|
||||||
});
|
});
|
||||||
body()?;
|
body()?;
|
||||||
@@ -105,7 +125,7 @@ macro_rules! unmarshall_then_body_then_marshall {
|
|||||||
macro_rules! define_func {
|
macro_rules! define_func {
|
||||||
( < E: $ext_ty:tt > $name:ident ( $ctx: ident $(, $names:ident : $params:ty)*) $(-> $returns:ty)* => $body:tt ) => {
|
( < E: $ext_ty:tt > $name:ident ( $ctx: ident $(, $names:ident : $params:ty)*) $(-> $returns:ty)* => $body:tt ) => {
|
||||||
fn $name< E: $ext_ty >(
|
fn $name< E: $ext_ty >(
|
||||||
$ctx: &mut $crate::vm::Runtime<E>,
|
$ctx: &mut $crate::wasm::Runtime<E>,
|
||||||
args: &[$crate::sandbox::TypedValue],
|
args: &[$crate::sandbox::TypedValue],
|
||||||
) -> Result<sandbox::ReturnValue, sandbox::HostError> {
|
) -> Result<sandbox::ReturnValue, sandbox::HostError> {
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
@@ -120,6 +140,27 @@ macro_rules! define_func {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! register_func {
|
||||||
|
( $reg_cb:ident, < E: $ext_ty:tt > ; ) => {};
|
||||||
|
|
||||||
|
( $reg_cb:ident, < E: $ext_ty:tt > ;
|
||||||
|
$name:ident ( $ctx:ident $( , $names:ident : $params:ty )* )
|
||||||
|
$( -> $returns:ty )* => $body:tt $($rest:tt)*
|
||||||
|
) => {
|
||||||
|
$reg_cb(
|
||||||
|
stringify!($name).as_bytes(),
|
||||||
|
{
|
||||||
|
define_func!(
|
||||||
|
< E: $ext_ty > $name ( $ctx $(, $names : $params )* ) $( -> $returns )* => $body
|
||||||
|
);
|
||||||
|
$name::<E>
|
||||||
|
}
|
||||||
|
);
|
||||||
|
register_func!( $reg_cb, < E: $ext_ty > ; $($rest)* );
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/// Define a function set that can be imported by executing wasm code.
|
/// Define a function set that can be imported by executing wasm code.
|
||||||
///
|
///
|
||||||
/// **NB**: Be advised that all functions defined by this macro
|
/// **NB**: Be advised that all functions defined by this macro
|
||||||
@@ -132,25 +173,20 @@ macro_rules! define_env {
|
|||||||
$( $name:ident ( $ctx:ident $( , $names:ident : $params:ty )* )
|
$( $name:ident ( $ctx:ident $( , $names:ident : $params:ty )* )
|
||||||
$( -> $returns:ty )* => $body:tt , )*
|
$( -> $returns:ty )* => $body:tt , )*
|
||||||
) => {
|
) => {
|
||||||
pub(crate) fn $init_name<E: Ext>() -> $crate::vm::env_def::HostFunctionSet<E> {
|
pub struct $init_name;
|
||||||
let mut env = $crate::vm::env_def::HostFunctionSet::new();
|
|
||||||
|
|
||||||
$(
|
impl $crate::wasm::env_def::ImportSatisfyCheck for $init_name {
|
||||||
env.funcs.insert(
|
fn can_satisfy(name: &[u8], func_type: &$crate::parity_wasm::elements::FunctionType) -> bool {
|
||||||
stringify!( $name ).into(),
|
gen_signature_dispatch!( name, func_type ; $( $name ( $ctx $(, $names : $params )* ) $( -> $returns )* , )* );
|
||||||
$crate::vm::env_def::HostFunction::new(
|
|
||||||
gen_signature!( ( $( $params ),* ) $( -> $returns )* ),
|
|
||||||
{
|
|
||||||
define_func!(
|
|
||||||
< E: $ext_ty > $name ( $ctx $(, $names : $params )* ) $( -> $returns )* => $body
|
|
||||||
);
|
|
||||||
$name::<E>
|
|
||||||
},
|
|
||||||
),
|
|
||||||
);
|
|
||||||
)*
|
|
||||||
|
|
||||||
env
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<E: Ext> $crate::wasm::env_def::FunctionImplProvider<E> for $init_name {
|
||||||
|
fn impls<F: FnMut(&[u8], $crate::wasm::env_def::HostFunc<E>)>(f: &mut F) {
|
||||||
|
register_func!(f, < E: $ext_ty > ; $( $name ( $ctx $( , $names : $params )* ) $( -> $returns)* => $body )* );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -161,8 +197,9 @@ mod tests {
|
|||||||
use parity_wasm::elements::ValueType;
|
use parity_wasm::elements::ValueType;
|
||||||
use runtime_primitives::traits::{As, Zero};
|
use runtime_primitives::traits::{As, Zero};
|
||||||
use sandbox::{self, ReturnValue, TypedValue};
|
use sandbox::{self, ReturnValue, TypedValue};
|
||||||
use vm::tests::MockExt;
|
use wasm::tests::MockExt;
|
||||||
use vm::{Ext, Runtime};
|
use wasm::Runtime;
|
||||||
|
use exec::Ext;
|
||||||
use Trait;
|
use Trait;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -267,7 +304,9 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn macro_define_env() {
|
fn macro_define_env() {
|
||||||
define_env!(init_env, <E: Ext>,
|
use wasm::env_def::ImportSatisfyCheck;
|
||||||
|
|
||||||
|
define_env!(Env, <E: Ext>,
|
||||||
ext_gas( _ctx, amount: u32 ) => {
|
ext_gas( _ctx, amount: u32 ) => {
|
||||||
let amount = <<E::T as Trait>::Gas as As<u32>>::sa(amount);
|
let amount = <<E::T as Trait>::Gas as As<u32>>::sa(amount);
|
||||||
if !amount.is_zero() {
|
if !amount.is_zero() {
|
||||||
@@ -278,7 +317,7 @@ mod tests {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
let env = init_env::<MockExt>();
|
assert!(Env::can_satisfy(b"ext_gas", &FunctionType::new(vec![ValueType::I32], None)));
|
||||||
assert!(env.funcs.get(&b"ext_gas"[..]).is_some());
|
assert!(!Env::can_satisfy(b"not_exists", &FunctionType::new(vec![], None)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+17
-42
@@ -14,10 +14,9 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use super::{Ext, Runtime};
|
use super::Runtime;
|
||||||
|
use exec::Ext;
|
||||||
use parity_wasm::elements::{FunctionType, ValueType};
|
use parity_wasm::elements::{FunctionType, ValueType};
|
||||||
use rstd::prelude::*;
|
|
||||||
use rstd::collections::btree_map::BTreeMap;
|
|
||||||
use sandbox::{self, TypedValue};
|
use sandbox::{self, TypedValue};
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
@@ -66,45 +65,21 @@ impl ConvertibleToWasm for u64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Represents a set of function that defined in this particular environment and
|
pub(crate) type HostFunc<E> =
|
||||||
/// which can be imported and called by the module.
|
fn(
|
||||||
pub(crate) struct HostFunctionSet<E: Ext> {
|
&mut Runtime<E>,
|
||||||
/// Functions which defined in the environment.
|
&[sandbox::TypedValue]
|
||||||
pub funcs: BTreeMap<Vec<u8>, HostFunction<E>>,
|
) -> Result<sandbox::ReturnValue, sandbox::HostError>;
|
||||||
}
|
|
||||||
impl<E: Ext> HostFunctionSet<E> {
|
pub(crate) trait FunctionImplProvider<E: Ext> {
|
||||||
pub fn new() -> Self {
|
fn impls<F: FnMut(&[u8], HostFunc<E>)>(f: &mut F);
|
||||||
HostFunctionSet {
|
|
||||||
funcs: BTreeMap::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct HostFunction<E: Ext> {
|
/// This trait can be used to check whether the host environment can satisfy
|
||||||
pub(crate) f: fn(&mut Runtime<E>, &[sandbox::TypedValue])
|
/// a requested function import.
|
||||||
-> Result<sandbox::ReturnValue, sandbox::HostError>,
|
pub trait ImportSatisfyCheck {
|
||||||
func_type: FunctionType,
|
/// Returns `true` if the host environment contains a function with
|
||||||
}
|
/// the specified name and its type matches to the given type, or `false`
|
||||||
impl<E: Ext> HostFunction<E> {
|
/// otherwise.
|
||||||
/// Create a new instance of a host function.
|
fn can_satisfy(name: &[u8], func_type: &FunctionType) -> bool;
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -17,154 +17,172 @@
|
|||||||
//! This module provides a means for executing contracts
|
//! This module provides a means for executing contracts
|
||||||
//! represented in wasm.
|
//! represented in wasm.
|
||||||
|
|
||||||
use exec::CreateReceipt;
|
use codec::Compact;
|
||||||
|
use exec::{Ext, EmptyOutputBuf, VmExecResult};
|
||||||
use gas::GasMeter;
|
use gas::GasMeter;
|
||||||
use rstd::prelude::*;
|
use rstd::prelude::*;
|
||||||
use {Trait, Schedule};
|
use sandbox;
|
||||||
use {balances, sandbox, system};
|
use wasm::env_def::FunctionImplProvider;
|
||||||
|
use {CodeHash, Schedule, Trait};
|
||||||
|
|
||||||
type BalanceOf<T> = <T as balances::Trait>::Balance;
|
|
||||||
type AccountIdOf<T> = <T as system::Trait>::AccountId;
|
|
||||||
|
|
||||||
mod prepare;
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod env_def;
|
mod env_def;
|
||||||
|
mod code_cache;
|
||||||
|
mod prepare;
|
||||||
mod runtime;
|
mod runtime;
|
||||||
|
|
||||||
use self::prepare::{prepare_contract, PreparedContract};
|
|
||||||
use self::runtime::{to_execution_result, Runtime};
|
use self::runtime::{to_execution_result, Runtime};
|
||||||
|
use self::code_cache::load as load_code;
|
||||||
|
|
||||||
/// An interface that provides an access to the external environment in which the
|
pub use self::code_cache::save as save_code;
|
||||||
/// 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.
|
/// A prepared wasm module ready for execution.
|
||||||
fn get_storage(&self, key: &[u8]) -> Option<Vec<u8>>;
|
#[derive(Clone, Encode, Decode)]
|
||||||
|
pub struct PrefabWasmModule {
|
||||||
/// Sets the storage entry by the given key to the specified value.
|
/// Version of the schedule with which the code was instrumented.
|
||||||
fn set_storage(&mut self, key: &[u8], value: Option<Vec<u8>>);
|
#[codec(compact)]
|
||||||
|
schedule_version: u32,
|
||||||
/// Create a new account for a contract.
|
#[codec(compact)]
|
||||||
|
initial: u32,
|
||||||
|
#[codec(compact)]
|
||||||
|
maximum: u32,
|
||||||
|
/// This field is reserved for future evolution of format.
|
||||||
///
|
///
|
||||||
/// The newly created account will be associated with the `code`. `value` specifies the amount of value
|
/// Basically, for now this field will be serialized as `None`. In the future
|
||||||
/// transfered from this to the newly created account.
|
/// we would be able to extend this structure with.
|
||||||
fn create(
|
_reserved: Option<()>,
|
||||||
&mut self,
|
/// Code instrumented with the latest schedule.
|
||||||
code: &[u8],
|
code: Vec<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],
|
|
||||||
output_data: &mut Vec<u8>,
|
|
||||||
) -> Result<(), ()>;
|
|
||||||
|
|
||||||
/// Returns a reference to the account id of the caller.
|
|
||||||
fn caller(&self) -> &AccountIdOf<Self::T>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Error that can occur while preparing or executing wasm smart-contract.
|
/// Wasm executable loaded by `WasmLoader` and executed by `WasmVm`.
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
pub struct WasmExecutable {
|
||||||
pub enum Error {
|
entrypoint_name: &'static [u8],
|
||||||
/// Error happened while serializing the module.
|
prefab_module: PrefabWasmModule,
|
||||||
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,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Execute the given code as a contract.
|
/// Loader which fetches `WasmExecutable` from the code cache.
|
||||||
pub fn execute<'a, E: Ext>(
|
pub struct WasmLoader<'a, T: Trait> {
|
||||||
code: &[u8],
|
schedule: &'a Schedule<T::Gas>,
|
||||||
input_data: &[u8],
|
}
|
||||||
output_data: &mut Vec<u8>,
|
|
||||||
ext: &'a mut E,
|
|
||||||
schedule: &Schedule<<E::T as Trait>::Gas>,
|
|
||||||
gas_meter: &mut GasMeter<E::T>,
|
|
||||||
) -> Result<(), Error> {
|
|
||||||
let env = runtime::init_env();
|
|
||||||
|
|
||||||
let PreparedContract {
|
impl<'a, T: Trait> WasmLoader<'a, T> {
|
||||||
instrumented_code,
|
pub fn new(schedule: &'a Schedule<T::Gas>) -> Self {
|
||||||
memory,
|
WasmLoader { schedule }
|
||||||
} = prepare_contract(code, &schedule, &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::new(ext, input_data, output_data, &schedule, memory, gas_meter);
|
impl<'a, T: Trait> ::exec::Loader<T> for WasmLoader<'a, T> {
|
||||||
|
type Executable = WasmExecutable;
|
||||||
|
|
||||||
// Instantiate the instance from the instrumented module code.
|
fn load_init(&self, code_hash: &CodeHash<T>) -> Result<WasmExecutable, &'static str> {
|
||||||
match sandbox::Instance::new(&instrumented_code, &imports, &mut runtime) {
|
let prefab_module = load_code::<T>(code_hash, self.schedule)?;
|
||||||
// No errors or traps were generated on instantiation! That
|
Ok(WasmExecutable {
|
||||||
// means we can now invoke the contract entrypoint.
|
entrypoint_name: b"deploy",
|
||||||
Ok(mut instance) => {
|
prefab_module,
|
||||||
let err = instance.invoke(b"call", &[], &mut runtime).err();
|
})
|
||||||
to_execution_result(runtime, err)
|
}
|
||||||
|
fn load_main(&self, code_hash: &CodeHash<T>) -> Result<WasmExecutable, &'static str> {
|
||||||
|
let prefab_module = load_code::<T>(code_hash, self.schedule)?;
|
||||||
|
Ok(WasmExecutable {
|
||||||
|
entrypoint_name: b"call",
|
||||||
|
prefab_module,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Implementation of `Vm` that takes `WasmExecutable` and executes it.
|
||||||
|
pub struct WasmVm<'a, T: Trait> {
|
||||||
|
schedule: &'a Schedule<T::Gas>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T: Trait> WasmVm<'a, T> {
|
||||||
|
pub fn new(schedule: &'a Schedule<T::Gas>) -> Self {
|
||||||
|
WasmVm { schedule }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T: Trait> ::exec::Vm<T> for WasmVm<'a, T> {
|
||||||
|
type Executable = WasmExecutable;
|
||||||
|
|
||||||
|
fn execute<E: Ext<T = T>>(
|
||||||
|
&self,
|
||||||
|
exec: &WasmExecutable,
|
||||||
|
ext: &mut E,
|
||||||
|
input_data: &[u8],
|
||||||
|
empty_output_buf: EmptyOutputBuf,
|
||||||
|
gas_meter: &mut GasMeter<E::T>,
|
||||||
|
) -> VmExecResult {
|
||||||
|
let memory =
|
||||||
|
sandbox::Memory::new(exec.prefab_module.initial, Some(exec.prefab_module.maximum))
|
||||||
|
.unwrap_or_else(|_| {
|
||||||
|
// unlike `.expect`, explicit panic preserves the source location.
|
||||||
|
// Needed as we can't use `RUST_BACKTRACE` in here.
|
||||||
|
panic!(
|
||||||
|
"exec.prefab_module.initial can't be greater than exec.prefab_module.maximum;
|
||||||
|
thus Memory::new must not fail;
|
||||||
|
qed"
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut imports = sandbox::EnvironmentDefinitionBuilder::new();
|
||||||
|
imports.add_memory("env", "memory", memory.clone());
|
||||||
|
runtime::Env::impls(&mut |name, func_ptr| {
|
||||||
|
imports.add_host_func("env", name, func_ptr);
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut runtime = Runtime::new(
|
||||||
|
ext,
|
||||||
|
input_data,
|
||||||
|
empty_output_buf,
|
||||||
|
&self.schedule,
|
||||||
|
memory,
|
||||||
|
gas_meter,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Instantiate the instance from the instrumented module code.
|
||||||
|
match sandbox::Instance::new(&exec.prefab_module.code, &imports, &mut runtime) {
|
||||||
|
// No errors or traps were generated on instantiation! That
|
||||||
|
// means we can now invoke the contract entrypoint.
|
||||||
|
Ok(mut instance) => {
|
||||||
|
let err = instance
|
||||||
|
.invoke(exec.entrypoint_name, &[], &mut runtime)
|
||||||
|
.err();
|
||||||
|
to_execution_result(runtime, err)
|
||||||
|
}
|
||||||
|
// `start` function trapped. Treat it in the same manner as an execution error.
|
||||||
|
Err(err @ sandbox::Error::Execution) => to_execution_result(runtime, Some(err)),
|
||||||
|
Err(_err @ sandbox::Error::Module) => {
|
||||||
|
// `Error::Module` is returned only if instantiation or linking failed (i.e.
|
||||||
|
// wasm bianry tried to import a function that is not provided by the host).
|
||||||
|
// This shouldn't happen because validation proccess ought to reject such binaries.
|
||||||
|
//
|
||||||
|
// Because panics are really undesirable in the runtime code, we treat this as
|
||||||
|
// a trap for now. Eventually, we might want to revisit this.
|
||||||
|
return VmExecResult::Trap("validation error");
|
||||||
|
}
|
||||||
|
// Other instantiation errors.
|
||||||
|
// Return without executing anything.
|
||||||
|
Err(_) => return VmExecResult::Trap("during start function"),
|
||||||
}
|
}
|
||||||
// `start` function trapped. Treat it in the same manner as an execution error.
|
|
||||||
Err(err @ sandbox::Error::Execution) => to_execution_result(runtime, Some(err)),
|
|
||||||
// Other instantiation errors.
|
|
||||||
// Return without executing anything.
|
|
||||||
Err(_) => return Err(Error::Instantiate),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use gas::GasMeter;
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use substrate_primitives::H256;
|
||||||
|
use exec::{CallReceipt, Ext, InstantiateReceipt, EmptyOutputBuf};
|
||||||
|
use gas::GasMeter;
|
||||||
use tests::Test;
|
use tests::Test;
|
||||||
use wabt;
|
use wabt;
|
||||||
|
use wasm::prepare::prepare_contract;
|
||||||
|
use CodeHash;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
struct CreateEntry {
|
struct CreateEntry {
|
||||||
code: Vec<u8>,
|
code_hash: H256,
|
||||||
endowment: u64,
|
endowment: u64,
|
||||||
data: Vec<u8>,
|
data: Vec<u8>,
|
||||||
gas_left: u64,
|
gas_left: u64,
|
||||||
@@ -192,15 +210,15 @@ mod tests {
|
|||||||
fn set_storage(&mut self, key: &[u8], value: Option<Vec<u8>>) {
|
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());
|
*self.storage.entry(key.to_vec()).or_insert(Vec::new()) = value.unwrap_or(Vec::new());
|
||||||
}
|
}
|
||||||
fn create(
|
fn instantiate(
|
||||||
&mut self,
|
&mut self,
|
||||||
code: &[u8],
|
code_hash: &CodeHash<Test>,
|
||||||
endowment: u64,
|
endowment: u64,
|
||||||
gas_meter: &mut GasMeter<Test>,
|
gas_meter: &mut GasMeter<Test>,
|
||||||
data: &[u8],
|
data: &[u8],
|
||||||
) -> Result<CreateReceipt<Test>, ()> {
|
) -> Result<InstantiateReceipt<u64>, &'static str> {
|
||||||
self.creates.push(CreateEntry {
|
self.creates.push(CreateEntry {
|
||||||
code: code.to_vec(),
|
code_hash: code_hash.clone(),
|
||||||
endowment,
|
endowment,
|
||||||
data: data.to_vec(),
|
data: data.to_vec(),
|
||||||
gas_left: gas_meter.gas_left(),
|
gas_left: gas_meter.gas_left(),
|
||||||
@@ -208,7 +226,7 @@ mod tests {
|
|||||||
let address = self.next_account_id;
|
let address = self.next_account_id;
|
||||||
self.next_account_id += 1;
|
self.next_account_id += 1;
|
||||||
|
|
||||||
Ok(CreateReceipt { address })
|
Ok(InstantiateReceipt { address })
|
||||||
}
|
}
|
||||||
fn call(
|
fn call(
|
||||||
&mut self,
|
&mut self,
|
||||||
@@ -216,8 +234,8 @@ mod tests {
|
|||||||
value: u64,
|
value: u64,
|
||||||
gas_meter: &mut GasMeter<Test>,
|
gas_meter: &mut GasMeter<Test>,
|
||||||
data: &[u8],
|
data: &[u8],
|
||||||
_output_data: &mut Vec<u8>,
|
_output_data: EmptyOutputBuf,
|
||||||
) -> Result<(), ()> {
|
) -> Result<CallReceipt, &'static str> {
|
||||||
self.transfers.push(TransferEntry {
|
self.transfers.push(TransferEntry {
|
||||||
to: *to,
|
to: *to,
|
||||||
value,
|
value,
|
||||||
@@ -226,11 +244,46 @@ mod tests {
|
|||||||
});
|
});
|
||||||
// Assume for now that it was just a plain transfer.
|
// Assume for now that it was just a plain transfer.
|
||||||
// TODO: Add tests for different call outcomes.
|
// TODO: Add tests for different call outcomes.
|
||||||
Ok(())
|
Ok(CallReceipt {
|
||||||
|
output_data: Vec::new(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
fn caller(&self) -> &u64 {
|
fn caller(&self) -> &u64 {
|
||||||
&42
|
&42
|
||||||
}
|
}
|
||||||
|
fn address(&self) -> &u64 {
|
||||||
|
&69
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn execute<E: Ext>(
|
||||||
|
wat: &str,
|
||||||
|
input_data: &[u8],
|
||||||
|
output_data: &mut Vec<u8>,
|
||||||
|
ext: &mut E,
|
||||||
|
gas_meter: &mut GasMeter<E::T>,
|
||||||
|
) -> Result<(), &'static str> {
|
||||||
|
use exec::Vm;
|
||||||
|
|
||||||
|
let wasm = wabt::wat2wasm(wat).unwrap();
|
||||||
|
let schedule = ::Schedule::<u64>::default();
|
||||||
|
let prefab_module =
|
||||||
|
prepare_contract::<Test, super::runtime::Env>(&wasm, &schedule).unwrap();
|
||||||
|
|
||||||
|
let exec = WasmExecutable {
|
||||||
|
// Use a "call" convention.
|
||||||
|
entrypoint_name: b"call",
|
||||||
|
prefab_module,
|
||||||
|
};
|
||||||
|
|
||||||
|
let cfg = Default::default();
|
||||||
|
let vm = WasmVm::new(&cfg);
|
||||||
|
|
||||||
|
*output_data = vm
|
||||||
|
.execute(&exec, ext, input_data, EmptyOutputBuf::new(), gas_meter)
|
||||||
|
.into_result()?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
const CODE_TRANSFER: &str = r#"
|
const CODE_TRANSFER: &str = r#"
|
||||||
@@ -259,6 +312,8 @@ mod tests {
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
(func (export "deploy"))
|
||||||
|
|
||||||
;; Destination AccountId to transfer the funds.
|
;; Destination AccountId to transfer the funds.
|
||||||
;; Represented by u64 (8 bytes long) in little endian.
|
;; Represented by u64 (8 bytes long) in little endian.
|
||||||
(data (i32.const 4) "\09\00\00\00\00\00\00\00")
|
(data (i32.const 4) "\09\00\00\00\00\00\00\00")
|
||||||
@@ -272,26 +327,22 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn contract_transfer() {
|
fn contract_transfer() {
|
||||||
let code_transfer = wabt::wat2wasm(CODE_TRANSFER).unwrap();
|
|
||||||
|
|
||||||
let mut mock_ext = MockExt::default();
|
let mut mock_ext = MockExt::default();
|
||||||
execute(
|
execute(
|
||||||
&code_transfer,
|
CODE_TRANSFER,
|
||||||
&[],
|
&[],
|
||||||
&mut Vec::new(),
|
&mut Vec::new(),
|
||||||
&mut mock_ext,
|
&mut mock_ext,
|
||||||
&Schedule::<u64>::default(),
|
|
||||||
&mut GasMeter::with_limit(50_000, 1),
|
&mut GasMeter::with_limit(50_000, 1),
|
||||||
).unwrap();
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
&mock_ext.transfers,
|
&mock_ext.transfers,
|
||||||
&[TransferEntry {
|
&[TransferEntry {
|
||||||
to: 9,
|
to: 9,
|
||||||
value: 6,
|
value: 6,
|
||||||
data: vec![
|
data: vec![1, 2, 3, 4],
|
||||||
1, 2, 3, 4,
|
|
||||||
],
|
|
||||||
gas_left: 49970,
|
gas_left: 49970,
|
||||||
}]
|
}]
|
||||||
);
|
);
|
||||||
@@ -313,83 +364,51 @@ mod tests {
|
|||||||
(func (export "call")
|
(func (export "call")
|
||||||
(drop
|
(drop
|
||||||
(call $ext_create
|
(call $ext_create
|
||||||
(i32.const 12) ;; Pointer to `code`
|
(i32.const 16) ;; Pointer to `code_hash`
|
||||||
(i32.const 8) ;; Length of `code`
|
(i32.const 32) ;; Length of `code_hash`
|
||||||
(i64.const 0) ;; How much gas to devote for the execution. 0 = all.
|
(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 4) ;; Pointer to the buffer with value to transfer
|
||||||
(i32.const 8) ;; Length of 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 12) ;; Pointer to input data buffer address
|
||||||
(i32.const 4) ;; Length of input data buffer
|
(i32.const 4) ;; Length of input data buffer
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
(func (export "deploy"))
|
||||||
|
|
||||||
;; Amount of value to transfer.
|
;; Amount of value to transfer.
|
||||||
;; Represented by u64 (8 bytes long) in little endian.
|
;; Represented by u64 (8 bytes long) in little endian.
|
||||||
(data (i32.const 4) "\03\00\00\00\00\00\00\00")
|
(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.
|
;; Input data to pass to the contract being created.
|
||||||
(data (i32.const 20) "\01\02\03\04")
|
(data (i32.const 12) "\01\02\03\04")
|
||||||
|
;; Hash of code.
|
||||||
|
(data (i32.const 16) "\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11")
|
||||||
)
|
)
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn contract_create() {
|
fn contract_create() {
|
||||||
let code_create = wabt::wat2wasm(CODE_CREATE).unwrap();
|
|
||||||
|
|
||||||
let mut mock_ext = MockExt::default();
|
let mut mock_ext = MockExt::default();
|
||||||
execute(
|
execute(
|
||||||
&code_create,
|
CODE_CREATE,
|
||||||
&[],
|
&[],
|
||||||
&mut Vec::new(),
|
&mut Vec::new(),
|
||||||
&mut mock_ext,
|
&mut mock_ext,
|
||||||
&Schedule::default(),
|
|
||||||
&mut GasMeter::with_limit(50_000, 1),
|
&mut GasMeter::with_limit(50_000, 1),
|
||||||
).unwrap();
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
&mock_ext.creates,
|
&mock_ext.creates,
|
||||||
&[CreateEntry {
|
&[CreateEntry {
|
||||||
code: vec![0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00],
|
code_hash: [0x11; 32].into(),
|
||||||
endowment: 3,
|
endowment: 3,
|
||||||
data: vec![
|
data: vec![1, 2, 3, 4],
|
||||||
1, 2, 3, 4,
|
gas_left: 49946,
|
||||||
],
|
|
||||||
gas_left: 49970,
|
|
||||||
}]
|
}]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
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 Vec::new(),
|
|
||||||
&mut mock_ext,
|
|
||||||
&Schedule::default(),
|
|
||||||
&mut GasMeter::with_limit(100_000, 1)
|
|
||||||
),
|
|
||||||
Err(_)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const CODE_TRANSFER_LIMITED_GAS: &str = r#"
|
const CODE_TRANSFER_LIMITED_GAS: &str = r#"
|
||||||
(module
|
(module
|
||||||
;; ext_call(
|
;; ext_call(
|
||||||
@@ -416,6 +435,8 @@ mod tests {
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
(func (export "deploy"))
|
||||||
|
|
||||||
;; Destination AccountId to transfer the funds.
|
;; Destination AccountId to transfer the funds.
|
||||||
;; Represented by u64 (8 bytes long) in little endian.
|
;; Represented by u64 (8 bytes long) in little endian.
|
||||||
(data (i32.const 4) "\09\00\00\00\00\00\00\00")
|
(data (i32.const 4) "\09\00\00\00\00\00\00\00")
|
||||||
@@ -429,26 +450,22 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn contract_call_limited_gas() {
|
fn contract_call_limited_gas() {
|
||||||
let code_transfer = wabt::wat2wasm(CODE_TRANSFER_LIMITED_GAS).unwrap();
|
|
||||||
|
|
||||||
let mut mock_ext = MockExt::default();
|
let mut mock_ext = MockExt::default();
|
||||||
execute(
|
execute(
|
||||||
&code_transfer,
|
&CODE_TRANSFER_LIMITED_GAS,
|
||||||
&[],
|
&[],
|
||||||
&mut Vec::new(),
|
&mut Vec::new(),
|
||||||
&mut mock_ext,
|
&mut mock_ext,
|
||||||
&Schedule::default(),
|
|
||||||
&mut GasMeter::with_limit(50_000, 1),
|
&mut GasMeter::with_limit(50_000, 1),
|
||||||
).unwrap();
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
&mock_ext.transfers,
|
&mock_ext.transfers,
|
||||||
&[TransferEntry {
|
&[TransferEntry {
|
||||||
to: 9,
|
to: 9,
|
||||||
value: 6,
|
value: 6,
|
||||||
data: vec![
|
data: vec![1, 2, 3, 4],
|
||||||
1, 2, 3, 4,
|
|
||||||
],
|
|
||||||
gas_left: 228,
|
gas_left: 228,
|
||||||
}]
|
}]
|
||||||
);
|
);
|
||||||
@@ -513,38 +530,35 @@ mod tests {
|
|||||||
(unreachable)
|
(unreachable)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
(func (export "deploy"))
|
||||||
|
|
||||||
(data (i32.const 4) "\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11")
|
(data (i32.const 4) "\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11")
|
||||||
)
|
)
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn get_storage_puts_data_into_scratch_buf() {
|
fn get_storage_puts_data_into_scratch_buf() {
|
||||||
let code_get_storage = wabt::wat2wasm(CODE_GET_STORAGE).unwrap();
|
|
||||||
|
|
||||||
let mut mock_ext = MockExt::default();
|
let mut mock_ext = MockExt::default();
|
||||||
mock_ext.storage.insert([0x11; 32].to_vec(), [0x22; 32].to_vec());
|
mock_ext
|
||||||
|
.storage
|
||||||
|
.insert([0x11; 32].to_vec(), [0x22; 32].to_vec());
|
||||||
|
|
||||||
let mut return_buf = Vec::new();
|
let mut return_buf = Vec::new();
|
||||||
execute(
|
execute(
|
||||||
&code_get_storage,
|
CODE_GET_STORAGE,
|
||||||
&[],
|
&[],
|
||||||
&mut return_buf,
|
&mut return_buf,
|
||||||
&mut mock_ext,
|
&mut mock_ext,
|
||||||
&Schedule::default(),
|
|
||||||
&mut GasMeter::with_limit(50_000, 1),
|
&mut GasMeter::with_limit(50_000, 1),
|
||||||
).unwrap();
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(return_buf, [0x22; 32].to_vec());
|
||||||
return_buf,
|
|
||||||
[0x22; 32].to_vec(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// calls `ext_caller`, loads the address from the scratch buffer and
|
/// calls `ext_caller`, loads the address from the scratch buffer and
|
||||||
/// compares it with the constant 42.
|
/// compares it with the constant 42.
|
||||||
const CODE_CALLER: &'static str =
|
const CODE_CALLER: &'static str = r#"
|
||||||
r#"
|
|
||||||
(module
|
(module
|
||||||
(import "env" "ext_caller" (func $ext_caller))
|
(import "env" "ext_caller" (func $ext_caller))
|
||||||
(import "env" "ext_scratch_size" (func $ext_scratch_size (result i32)))
|
(import "env" "ext_scratch_size" (func $ext_scratch_size (result i32)))
|
||||||
@@ -589,21 +603,125 @@ r#"
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
(func (export "deploy"))
|
||||||
)
|
)
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn caller() {
|
fn caller() {
|
||||||
let code_caller = wabt::wat2wasm(CODE_CALLER).unwrap();
|
|
||||||
|
|
||||||
let mut mock_ext = MockExt::default();
|
let mut mock_ext = MockExt::default();
|
||||||
execute(
|
execute(
|
||||||
&code_caller,
|
CODE_CALLER,
|
||||||
&[],
|
&[],
|
||||||
&mut Vec::new(),
|
&mut Vec::new(),
|
||||||
&mut mock_ext,
|
&mut mock_ext,
|
||||||
&Schedule::<u64>::default(),
|
|
||||||
&mut GasMeter::with_limit(50_000, 1),
|
&mut GasMeter::with_limit(50_000, 1),
|
||||||
).unwrap();
|
)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// calls `ext_address`, loads the address from the scratch buffer and
|
||||||
|
/// compares it with the constant 69.
|
||||||
|
const CODE_ADDRESS: &'static str = r#"
|
||||||
|
(module
|
||||||
|
(import "env" "ext_address" (func $ext_address))
|
||||||
|
(import "env" "ext_scratch_size" (func $ext_scratch_size (result i32)))
|
||||||
|
(import "env" "ext_scratch_copy" (func $ext_scratch_copy (param i32 i32 i32)))
|
||||||
|
(import "env" "memory" (memory 1 1))
|
||||||
|
|
||||||
|
(func $assert (param i32)
|
||||||
|
(block $ok
|
||||||
|
(br_if $ok
|
||||||
|
(get_local 0)
|
||||||
|
)
|
||||||
|
(unreachable)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
(func (export "call")
|
||||||
|
;; fill the scratch buffer with the self address.
|
||||||
|
(call $ext_address)
|
||||||
|
|
||||||
|
;; assert $ext_scratch_size == 8
|
||||||
|
(call $assert
|
||||||
|
(i32.eq
|
||||||
|
(call $ext_scratch_size)
|
||||||
|
(i32.const 8)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
;; copy contents of the scratch buffer into the contract's memory.
|
||||||
|
(call $ext_scratch_copy
|
||||||
|
(i32.const 8) ;; Pointer in memory to the place where to copy.
|
||||||
|
(i32.const 0) ;; Offset from the start of the scratch buffer.
|
||||||
|
(i32.const 8) ;; Count of bytes to copy.
|
||||||
|
)
|
||||||
|
|
||||||
|
;; assert that contents of the buffer is equal to the i64 value of 69.
|
||||||
|
(call $assert
|
||||||
|
(i64.eq
|
||||||
|
(i64.load
|
||||||
|
(i32.const 8)
|
||||||
|
)
|
||||||
|
(i64.const 69)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
(func (export "deploy"))
|
||||||
|
)
|
||||||
|
"#;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn address() {
|
||||||
|
let mut mock_ext = MockExt::default();
|
||||||
|
execute(
|
||||||
|
CODE_ADDRESS,
|
||||||
|
&[],
|
||||||
|
&mut Vec::new(),
|
||||||
|
&mut mock_ext,
|
||||||
|
&mut GasMeter::with_limit(50_000, 1),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
const CODE_RETURN_FROM_START_FN: &str = r#"
|
||||||
|
(module
|
||||||
|
(import "env" "ext_return" (func $ext_return (param i32 i32)))
|
||||||
|
(import "env" "memory" (memory 1 1))
|
||||||
|
|
||||||
|
(start $start)
|
||||||
|
(func $start
|
||||||
|
(call $ext_return
|
||||||
|
(i32.const 8)
|
||||||
|
(i32.const 4)
|
||||||
|
)
|
||||||
|
(unreachable)
|
||||||
|
)
|
||||||
|
|
||||||
|
(func (export "call")
|
||||||
|
(unreachable)
|
||||||
|
)
|
||||||
|
(func (export "deploy"))
|
||||||
|
|
||||||
|
(data (i32.const 8) "\01\02\03\04")
|
||||||
|
)
|
||||||
|
"#;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn return_from_start_fn() {
|
||||||
|
let mut mock_ext = MockExt::default();
|
||||||
|
let mut output_data = Vec::new();
|
||||||
|
execute(
|
||||||
|
CODE_RETURN_FROM_START_FN,
|
||||||
|
&[],
|
||||||
|
&mut output_data,
|
||||||
|
&mut mock_ext,
|
||||||
|
&mut GasMeter::with_limit(50_000, 1),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(output_data, vec![1, 2, 3, 4]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,546 @@
|
|||||||
|
// 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 takes care of loading, checking and preprocessing of a
|
||||||
|
//! wasm module before execution. It also extracts some essential information
|
||||||
|
//! from a module.
|
||||||
|
|
||||||
|
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.
|
||||||
|
// Invariant: Can't be `None` (i.e. on enter and on exit from the function
|
||||||
|
// the value *must* be `Some`).
|
||||||
|
module: Option<elements::Module>,
|
||||||
|
schedule: &'a Schedule<Gas>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, Gas: 'a + As<u32> + Clone> ContractModule<'a, Gas> {
|
||||||
|
fn new(
|
||||||
|
original_code: &[u8],
|
||||||
|
schedule: &'a Schedule<Gas>,
|
||||||
|
) -> Result<ContractModule<'a, Gas>, &'static str> {
|
||||||
|
let module =
|
||||||
|
elements::deserialize_buffer(original_code).map_err(|_| "can't decode wasm code")?;
|
||||||
|
Ok(ContractModule {
|
||||||
|
module: Some(module),
|
||||||
|
schedule,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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<(), &'static str> {
|
||||||
|
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("module declares internal memory");
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inject_gas_metering(&mut self) -> Result<(), &'static str> {
|
||||||
|
let gas_rules =
|
||||||
|
rules::Set::new(
|
||||||
|
self.schedule.regular_op_cost.clone().as_(),
|
||||||
|
Default::default(),
|
||||||
|
)
|
||||||
|
.with_grow_cost(self.schedule.grow_mem_cost.clone().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(|_| "gas instrumentation failed")?;
|
||||||
|
|
||||||
|
self.module = Some(contract_module);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inject_stack_height_metering(&mut self) -> Result<(), &'static str> {
|
||||||
|
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.schedule.max_stack_height)
|
||||||
|
.map_err(|_| "stack height instrumentation failed")?;
|
||||||
|
|
||||||
|
self.module = Some(contract_module);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Check that the module has required exported functions. For now
|
||||||
|
/// these are just entrypoints:
|
||||||
|
///
|
||||||
|
/// - 'call'
|
||||||
|
/// - 'deploy'
|
||||||
|
fn scan_exports(&self) -> Result<(), &'static str> {
|
||||||
|
let mut deploy_found = false;
|
||||||
|
let mut call_found = false;
|
||||||
|
|
||||||
|
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 export_entries = module
|
||||||
|
.export_section()
|
||||||
|
.map(|is| is.entries())
|
||||||
|
.unwrap_or(&[]);
|
||||||
|
let func_entries = module
|
||||||
|
.function_section()
|
||||||
|
.map(|fs| fs.entries())
|
||||||
|
.unwrap_or(&[]);
|
||||||
|
|
||||||
|
// Function index space consists of imported function following by
|
||||||
|
// declared functions. Calculate the total number of imported functions so
|
||||||
|
// we can use it to convert indexes from function space to declared function space.
|
||||||
|
let fn_space_offset = module
|
||||||
|
.import_section()
|
||||||
|
.map(|is| is.entries())
|
||||||
|
.unwrap_or(&[])
|
||||||
|
.iter()
|
||||||
|
.filter(|entry| {
|
||||||
|
match *entry.external() {
|
||||||
|
External::Function(_) => true,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.count();
|
||||||
|
|
||||||
|
for export in export_entries {
|
||||||
|
match export.field() {
|
||||||
|
"call" => call_found = true,
|
||||||
|
"deploy" => deploy_found = true,
|
||||||
|
_ => continue,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then check the export kind. "call" and "deploy" are
|
||||||
|
// functions.
|
||||||
|
let fn_idx = match export.internal() {
|
||||||
|
Internal::Function(ref fn_idx) => *fn_idx,
|
||||||
|
_ => return Err("expected a function"),
|
||||||
|
};
|
||||||
|
|
||||||
|
// convert index from function index space to declared index space.
|
||||||
|
let fn_idx = match fn_idx.checked_sub(fn_space_offset as u32) {
|
||||||
|
Some(fn_idx) => fn_idx,
|
||||||
|
None => {
|
||||||
|
// Underflow here means fn_idx points to imported function which we don't allow!
|
||||||
|
return Err("entry point points to an imported function");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Then check the signature.
|
||||||
|
// Both "call" and "deploy" has a () -> () function type.
|
||||||
|
let func_ty_idx = func_entries.get(fn_idx as usize)
|
||||||
|
.ok_or_else(|| "export refers to non-existent function")?
|
||||||
|
.type_ref();
|
||||||
|
let Type::Function(ref func_ty) = types
|
||||||
|
.get(func_ty_idx as usize)
|
||||||
|
.ok_or_else(|| "function has a non-existent type")?;
|
||||||
|
if !(func_ty.params().is_empty() && func_ty.return_type().is_none()) {
|
||||||
|
return Err("entry point has wrong signature");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !deploy_found {
|
||||||
|
return Err("deploy function isn't exported");
|
||||||
|
}
|
||||||
|
if !call_found {
|
||||||
|
return Err("call function isn't exported");
|
||||||
|
}
|
||||||
|
|
||||||
|
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<C: ImportSatisfyCheck>(&self) -> Result<Option<&MemoryType>, &'static str> {
|
||||||
|
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("module has imports from a non-'env' namespace");
|
||||||
|
}
|
||||||
|
|
||||||
|
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(|| "validation: import entry points to a non-existent type")?;
|
||||||
|
|
||||||
|
if !C::can_satisfy(import.field().as_bytes(), func_ty) {
|
||||||
|
return Err("module imports a non-existent function");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(imported_mem_type)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_wasm_code(mut self) -> Result<Vec<u8>, &'static str> {
|
||||||
|
elements::serialize(
|
||||||
|
self.module
|
||||||
|
.take()
|
||||||
|
.expect("On entry to the function `module` can't be `None`; qed"),
|
||||||
|
)
|
||||||
|
.map_err(|_| "error serializing instrumented module")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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 `schedule`,
|
||||||
|
/// - 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 fn prepare_contract<T: Trait, C: ImportSatisfyCheck>(
|
||||||
|
original_code: &[u8],
|
||||||
|
schedule: &Schedule<T::Gas>,
|
||||||
|
) -> Result<PrefabWasmModule, &'static str> {
|
||||||
|
let mut contract_module = ContractModule::new(original_code, schedule)?;
|
||||||
|
contract_module.scan_exports()?;
|
||||||
|
contract_module.ensure_no_internal_memory()?;
|
||||||
|
contract_module.inject_gas_metering()?;
|
||||||
|
contract_module.inject_stack_height_metering()?;
|
||||||
|
|
||||||
|
struct MemoryDefinition {
|
||||||
|
initial: u32,
|
||||||
|
maximum: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
let memory_def = if let Some(memory_type) = contract_module.scan_imports::<C>()? {
|
||||||
|
// 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 => {
|
||||||
|
return Err(
|
||||||
|
"Requested initial number of pages should not exceed the requested maximum",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
(_, Some(maximum)) if maximum > schedule.max_memory_pages => {
|
||||||
|
return Err("Maximum number of pages should not exceed the configured maximum.");
|
||||||
|
}
|
||||||
|
(initial, Some(maximum)) => MemoryDefinition { initial, maximum },
|
||||||
|
(_, 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("Maximum number of pages should be always declared.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// If none memory imported then just crate an empty placeholder.
|
||||||
|
// Any access to it will lead to out of bounds trap.
|
||||||
|
MemoryDefinition {
|
||||||
|
initial: 0,
|
||||||
|
maximum: 0,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(PrefabWasmModule {
|
||||||
|
schedule_version: schedule.version,
|
||||||
|
initial: memory_def.initial,
|
||||||
|
maximum: memory_def.maximum,
|
||||||
|
_reserved: None,
|
||||||
|
code: contract_module.into_wasm_code()?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use std::fmt;
|
||||||
|
use tests::Test;
|
||||||
|
use exec::Ext;
|
||||||
|
use wabt;
|
||||||
|
|
||||||
|
impl fmt::Debug for PrefabWasmModule {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "PreparedContract {{ .. }}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Define test environment for tests. We need ImportSatisfyCheck
|
||||||
|
// implementation from it. So actual implementations doesn't matter.
|
||||||
|
define_env!(TestEnv, <E: Ext>,
|
||||||
|
panic(_ctx) => { unreachable!(); },
|
||||||
|
gas(_ctx, _amount: u32) => { unreachable!(); },
|
||||||
|
);
|
||||||
|
|
||||||
|
macro_rules! prepare_test {
|
||||||
|
($name:ident, $wat:expr, $($expected:tt)*) => {
|
||||||
|
#[test]
|
||||||
|
fn $name() {
|
||||||
|
let wasm = wabt::Wat2Wasm::new().validate(false).convert($wat).unwrap();
|
||||||
|
let schedule = Schedule::<u64>::default();
|
||||||
|
let r = prepare_contract::<Test, TestEnv>(wasm.as_ref(), &schedule);
|
||||||
|
assert_matches!(r, $($expected)*);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
mod memories {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
// Tests below assumes that maximum page number is configured to a certain number.
|
||||||
|
#[test]
|
||||||
|
fn assume_memory_size() {
|
||||||
|
assert_eq!(Schedule::<u64>::default().max_memory_pages, 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
prepare_test!(memory_with_one_page,
|
||||||
|
r#"
|
||||||
|
(module
|
||||||
|
(import "env" "memory" (memory 1 1))
|
||||||
|
|
||||||
|
(func (export "call"))
|
||||||
|
(func (export "deploy"))
|
||||||
|
)
|
||||||
|
"#,
|
||||||
|
Ok(_)
|
||||||
|
);
|
||||||
|
|
||||||
|
prepare_test!(internal_memory_declaration,
|
||||||
|
r#"
|
||||||
|
(module
|
||||||
|
(memory 1 1)
|
||||||
|
|
||||||
|
(func (export "call"))
|
||||||
|
(func (export "deploy"))
|
||||||
|
)
|
||||||
|
"#,
|
||||||
|
Err("module declares internal memory")
|
||||||
|
);
|
||||||
|
|
||||||
|
prepare_test!(no_memory_import,
|
||||||
|
r#"
|
||||||
|
(module
|
||||||
|
;; no memory imported
|
||||||
|
|
||||||
|
(func (export "call"))
|
||||||
|
(func (export "deploy"))
|
||||||
|
)"#,
|
||||||
|
Ok(_)
|
||||||
|
);
|
||||||
|
|
||||||
|
prepare_test!(initial_exceeds_maximum,
|
||||||
|
r#"
|
||||||
|
(module
|
||||||
|
(import "env" "memory" (memory 16 1))
|
||||||
|
|
||||||
|
(func (export "call"))
|
||||||
|
(func (export "deploy"))
|
||||||
|
)
|
||||||
|
"#,
|
||||||
|
Err("Requested initial number of pages should not exceed the requested maximum")
|
||||||
|
);
|
||||||
|
|
||||||
|
prepare_test!(no_maximum,
|
||||||
|
r#"
|
||||||
|
(module
|
||||||
|
(import "env" "memory" (memory 1))
|
||||||
|
|
||||||
|
(func (export "call"))
|
||||||
|
(func (export "deploy"))
|
||||||
|
)
|
||||||
|
"#,
|
||||||
|
Err("Maximum number of pages should be always declared.")
|
||||||
|
);
|
||||||
|
|
||||||
|
prepare_test!(requested_maximum_exceeds_configured_maximum,
|
||||||
|
r#"
|
||||||
|
(module
|
||||||
|
(import "env" "memory" (memory 1 17))
|
||||||
|
|
||||||
|
(func (export "call"))
|
||||||
|
(func (export "deploy"))
|
||||||
|
)
|
||||||
|
"#,
|
||||||
|
Err("Maximum number of pages should not exceed the configured maximum.")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
mod imports {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
prepare_test!(can_import_legit_function,
|
||||||
|
r#"
|
||||||
|
(module
|
||||||
|
(import "env" "gas" (func (param i32)))
|
||||||
|
|
||||||
|
(func (export "call"))
|
||||||
|
(func (export "deploy"))
|
||||||
|
)
|
||||||
|
"#,
|
||||||
|
Ok(_)
|
||||||
|
);
|
||||||
|
|
||||||
|
// nothing can be imported from non-"env" module for now.
|
||||||
|
prepare_test!(non_env_import,
|
||||||
|
r#"
|
||||||
|
(module
|
||||||
|
(import "another_module" "memory" (memory 1 1))
|
||||||
|
|
||||||
|
(func (export "call"))
|
||||||
|
(func (export "deploy"))
|
||||||
|
)
|
||||||
|
"#,
|
||||||
|
Err("module has imports from a non-'env' namespace")
|
||||||
|
);
|
||||||
|
|
||||||
|
// wrong signature
|
||||||
|
prepare_test!(wrong_signature,
|
||||||
|
r#"
|
||||||
|
(module
|
||||||
|
(import "env" "gas" (func (param i64)))
|
||||||
|
|
||||||
|
(func (export "call"))
|
||||||
|
(func (export "deploy"))
|
||||||
|
)
|
||||||
|
"#,
|
||||||
|
Err("module imports a non-existent function")
|
||||||
|
);
|
||||||
|
|
||||||
|
prepare_test!(unknown_func_name,
|
||||||
|
r#"
|
||||||
|
(module
|
||||||
|
(import "env" "unknown_func" (func))
|
||||||
|
|
||||||
|
(func (export "call"))
|
||||||
|
(func (export "deploy"))
|
||||||
|
)
|
||||||
|
"#,
|
||||||
|
Err("module imports a non-existent function")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
mod entrypoints {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
prepare_test!(it_works,
|
||||||
|
r#"
|
||||||
|
(module
|
||||||
|
(func (export "call"))
|
||||||
|
(func (export "deploy"))
|
||||||
|
)
|
||||||
|
"#,
|
||||||
|
Ok(_)
|
||||||
|
);
|
||||||
|
|
||||||
|
prepare_test!(omit_deploy,
|
||||||
|
r#"
|
||||||
|
(module
|
||||||
|
(func (export "call"))
|
||||||
|
)
|
||||||
|
"#,
|
||||||
|
Err("deploy function isn't exported")
|
||||||
|
);
|
||||||
|
|
||||||
|
prepare_test!(omit_call,
|
||||||
|
r#"
|
||||||
|
(module
|
||||||
|
(func (export "deploy"))
|
||||||
|
)
|
||||||
|
"#,
|
||||||
|
Err("call function isn't exported")
|
||||||
|
);
|
||||||
|
|
||||||
|
// Try to use imported function as an entry point.
|
||||||
|
prepare_test!(try_sneak_export_as_entrypoint,
|
||||||
|
r#"
|
||||||
|
(module
|
||||||
|
(import "env" "panic" (func))
|
||||||
|
|
||||||
|
(func (export "deploy"))
|
||||||
|
|
||||||
|
(export "call" (func 0))
|
||||||
|
)
|
||||||
|
"#,
|
||||||
|
Err("entry point points to an imported function")
|
||||||
|
);
|
||||||
|
|
||||||
|
// Try to use imported function as an entry point.
|
||||||
|
prepare_test!(try_sneak_export_as_global,
|
||||||
|
r#"
|
||||||
|
(module
|
||||||
|
(func (export "deploy"))
|
||||||
|
(global (export "call") i32 (i32.const 0))
|
||||||
|
)
|
||||||
|
"#,
|
||||||
|
Err("expected a function")
|
||||||
|
);
|
||||||
|
|
||||||
|
prepare_test!(wrong_signature,
|
||||||
|
r#"
|
||||||
|
(module
|
||||||
|
(func (export "deploy"))
|
||||||
|
(func (export "call") (param i32))
|
||||||
|
)
|
||||||
|
"#,
|
||||||
|
Err("entry point has wrong signature")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
+132
-55
@@ -16,16 +16,16 @@
|
|||||||
|
|
||||||
//! Environment definition of the wasm smart-contract runtime.
|
//! Environment definition of the wasm smart-contract runtime.
|
||||||
|
|
||||||
use super::{BalanceOf, Schedule, CreateReceipt, Error, Ext};
|
use super::{Schedule};
|
||||||
|
use exec::{Ext, BalanceOf, VmExecResult, OutputBuf, EmptyOutputBuf, CallReceipt, InstantiateReceipt};
|
||||||
use rstd::prelude::*;
|
use rstd::prelude::*;
|
||||||
|
use rstd::mem;
|
||||||
use codec::{Decode, Encode};
|
use codec::{Decode, Encode};
|
||||||
use gas::{GasMeter, GasMeterResult};
|
use gas::{GasMeter, Token, GasMeterResult};
|
||||||
use runtime_primitives::traits::{As, CheckedMul};
|
use runtime_primitives::traits::{As, CheckedMul, Bounded};
|
||||||
use sandbox;
|
use sandbox;
|
||||||
use system;
|
use system;
|
||||||
use Trait;
|
use {Trait, CodeHash};
|
||||||
|
|
||||||
type GasOf<E> = <<E as Ext>::T as Trait>::Gas;
|
|
||||||
|
|
||||||
/// Enumerates all possible *special* trap conditions.
|
/// Enumerates all possible *special* trap conditions.
|
||||||
///
|
///
|
||||||
@@ -33,13 +33,16 @@ type GasOf<E> = <<E as Ext>::T as Trait>::Gas;
|
|||||||
/// to just terminate quickly in some cases.
|
/// to just terminate quickly in some cases.
|
||||||
enum SpecialTrap {
|
enum SpecialTrap {
|
||||||
/// Signals that trap was generated in response to call `ext_return` host function.
|
/// Signals that trap was generated in response to call `ext_return` host function.
|
||||||
Return,
|
Return(OutputBuf),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Can only be used for one call.
|
||||||
pub(crate) struct Runtime<'a, 'data, E: Ext + 'a> {
|
pub(crate) struct Runtime<'a, 'data, E: Ext + 'a> {
|
||||||
ext: &'a mut E,
|
ext: &'a mut E,
|
||||||
input_data: &'data [u8],
|
input_data: &'data [u8],
|
||||||
output_data: &'data mut Vec<u8>,
|
// A VM can return a result only once and only by value. So
|
||||||
|
// we wrap output buffer to make it possible to take the buffer out.
|
||||||
|
empty_output_buf: Option<EmptyOutputBuf>,
|
||||||
scratch_buf: Vec<u8>,
|
scratch_buf: Vec<u8>,
|
||||||
schedule: &'a Schedule<<E::T as Trait>::Gas>,
|
schedule: &'a Schedule<<E::T as Trait>::Gas>,
|
||||||
memory: sandbox::Memory,
|
memory: sandbox::Memory,
|
||||||
@@ -50,7 +53,7 @@ impl<'a, 'data, E: Ext + 'a> Runtime<'a, 'data, E> {
|
|||||||
pub(crate) fn new(
|
pub(crate) fn new(
|
||||||
ext: &'a mut E,
|
ext: &'a mut E,
|
||||||
input_data: &'data [u8],
|
input_data: &'data [u8],
|
||||||
output_data: &'data mut Vec<u8>,
|
empty_output_buf: EmptyOutputBuf,
|
||||||
schedule: &'a Schedule<<E::T as Trait>::Gas>,
|
schedule: &'a Schedule<<E::T as Trait>::Gas>,
|
||||||
memory: sandbox::Memory,
|
memory: sandbox::Memory,
|
||||||
gas_meter: &'a mut GasMeter<E::T>,
|
gas_meter: &'a mut GasMeter<E::T>,
|
||||||
@@ -58,7 +61,7 @@ impl<'a, 'data, E: Ext + 'a> Runtime<'a, 'data, E> {
|
|||||||
Runtime {
|
Runtime {
|
||||||
ext,
|
ext,
|
||||||
input_data,
|
input_data,
|
||||||
output_data,
|
empty_output_buf: Some(empty_output_buf),
|
||||||
scratch_buf: Vec::new(),
|
scratch_buf: Vec::new(),
|
||||||
schedule,
|
schedule,
|
||||||
memory,
|
memory,
|
||||||
@@ -75,30 +78,68 @@ impl<'a, 'data, E: Ext + 'a> Runtime<'a, 'data, E> {
|
|||||||
pub(crate) fn to_execution_result<E: Ext>(
|
pub(crate) fn to_execution_result<E: Ext>(
|
||||||
runtime: Runtime<E>,
|
runtime: Runtime<E>,
|
||||||
sandbox_err: Option<sandbox::Error>,
|
sandbox_err: Option<sandbox::Error>,
|
||||||
) -> Result<(), Error> {
|
) -> VmExecResult {
|
||||||
// Check the exact type of the error. It could be plain trap or
|
// Check the exact type of the error. It could be plain trap or
|
||||||
// special runtime trap the we must recognize.
|
// special runtime trap the we must recognize.
|
||||||
match (sandbox_err, runtime.special_trap) {
|
match (sandbox_err, runtime.special_trap) {
|
||||||
// No traps were generated. Proceed normally.
|
// No traps were generated. Proceed normally.
|
||||||
(None, None) => Ok(()),
|
(None, None) => VmExecResult::Ok,
|
||||||
// Special case. The trap was the result of the execution `return` host function.
|
// Special case. The trap was the result of the execution `return` host function.
|
||||||
(Some(sandbox::Error::Execution), Some(SpecialTrap::Return)) => Ok(()),
|
(Some(sandbox::Error::Execution), Some(SpecialTrap::Return(buf))) => VmExecResult::Returned(buf),
|
||||||
// Any other kind of a trap should result in a failure.
|
// Any other kind of a trap should result in a failure.
|
||||||
(Some(_), _) => Err(Error::Invoke),
|
(Some(_), _) => VmExecResult::Trap("during execution"),
|
||||||
// Any other case (such as special trap flag without actual trap) signifies
|
// Any other case (such as special trap flag without actual trap) signifies
|
||||||
// a logic error.
|
// a logic error.
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Charge the specified amount of gas.
|
#[cfg_attr(test, derive(Debug, PartialEq, Eq))]
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
|
pub enum RuntimeToken {
|
||||||
|
/// Explicit call to the `gas` function. Charge the gas meter
|
||||||
|
/// with the value provided.
|
||||||
|
Explicit(u32),
|
||||||
|
/// The given number of bytes is read from the sandbox memory.
|
||||||
|
ReadMemory(u32),
|
||||||
|
/// The given number of bytes is written to the sandbox memory.
|
||||||
|
WriteMemory(u32),
|
||||||
|
/// The given number of bytes is read from the sandbox memory and
|
||||||
|
/// is returned as the return data buffer of the call.
|
||||||
|
ReturnData(u32),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Trait> Token<T> for RuntimeToken {
|
||||||
|
type Metadata = Schedule<T::Gas>;
|
||||||
|
|
||||||
|
fn calculate_amount(&self, metadata: &Schedule<T::Gas>) -> T::Gas {
|
||||||
|
use self::RuntimeToken::*;
|
||||||
|
let value = match *self {
|
||||||
|
Explicit(amount) => Some(<T::Gas as As<u32>>::sa(amount)),
|
||||||
|
ReadMemory(byte_count) => metadata
|
||||||
|
.sandbox_data_read_cost
|
||||||
|
.checked_mul(&<T::Gas as As<u32>>::sa(byte_count)),
|
||||||
|
WriteMemory(byte_count) => metadata
|
||||||
|
.sandbox_data_write_cost
|
||||||
|
.checked_mul(&<T::Gas as As<u32>>::sa(byte_count)),
|
||||||
|
ReturnData(byte_count) => metadata
|
||||||
|
.return_data_per_byte_cost
|
||||||
|
.checked_mul(&<T::Gas as As<u32>>::sa(byte_count)),
|
||||||
|
};
|
||||||
|
|
||||||
|
value.unwrap_or_else(|| Bounded::max_value())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Charge the gas meter with the specified token.
|
||||||
///
|
///
|
||||||
/// Returns `Err` if there is not enough gas.
|
/// Returns `Err(HostError)` if there is not enough gas.
|
||||||
fn charge_gas<T: Trait>(
|
fn charge_gas<T: Trait, Tok: Token<T>>(
|
||||||
gas_meter: &mut GasMeter<T>,
|
gas_meter: &mut GasMeter<T>,
|
||||||
amount: T::Gas,
|
metadata: &Tok::Metadata,
|
||||||
|
token: Tok,
|
||||||
) -> Result<(), sandbox::HostError> {
|
) -> Result<(), sandbox::HostError> {
|
||||||
match gas_meter.charge(amount) {
|
match gas_meter.charge(metadata, token) {
|
||||||
GasMeterResult::Proceed => Ok(()),
|
GasMeterResult::Proceed => Ok(()),
|
||||||
GasMeterResult::OutOfGas => Err(sandbox::HostError),
|
GasMeterResult::OutOfGas => Err(sandbox::HostError),
|
||||||
}
|
}
|
||||||
@@ -117,10 +158,7 @@ fn read_sandbox_memory<E: Ext>(
|
|||||||
ptr: u32,
|
ptr: u32,
|
||||||
len: u32,
|
len: u32,
|
||||||
) -> Result<Vec<u8>, sandbox::HostError> {
|
) -> Result<Vec<u8>, sandbox::HostError> {
|
||||||
let price = (ctx.schedule.sandbox_data_read_cost)
|
charge_gas(ctx.gas_meter, ctx.schedule, RuntimeToken::ReadMemory(len))?;
|
||||||
.checked_mul(&<GasOf<E> as As<u32>>::sa(len))
|
|
||||||
.ok_or(sandbox::HostError)?;
|
|
||||||
charge_gas(ctx.gas_meter, price)?;
|
|
||||||
|
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
buf.resize(len as usize, 0);
|
buf.resize(len as usize, 0);
|
||||||
@@ -139,16 +177,13 @@ fn read_sandbox_memory<E: Ext>(
|
|||||||
/// - out of gas
|
/// - out of gas
|
||||||
/// - designated area is not within the bounds of the sandbox memory.
|
/// - designated area is not within the bounds of the sandbox memory.
|
||||||
fn write_sandbox_memory<T: Trait>(
|
fn write_sandbox_memory<T: Trait>(
|
||||||
per_byte_cost: T::Gas,
|
schedule: &Schedule<T::Gas>,
|
||||||
gas_meter: &mut GasMeter<T>,
|
gas_meter: &mut GasMeter<T>,
|
||||||
memory: &sandbox::Memory,
|
memory: &sandbox::Memory,
|
||||||
ptr: u32,
|
ptr: u32,
|
||||||
buf: &[u8],
|
buf: &[u8],
|
||||||
) -> Result<(), sandbox::HostError> {
|
) -> Result<(), sandbox::HostError> {
|
||||||
let price = per_byte_cost
|
charge_gas(gas_meter, schedule, RuntimeToken::WriteMemory(buf.len() as u32))?;
|
||||||
.checked_mul(&<T::Gas as As<u32>>::sa(buf.len() as u32))
|
|
||||||
.ok_or(sandbox::HostError)?;
|
|
||||||
charge_gas(gas_meter, price)?;
|
|
||||||
|
|
||||||
memory.set(ptr, buf)?;
|
memory.set(ptr, buf)?;
|
||||||
|
|
||||||
@@ -163,15 +198,13 @@ fn write_sandbox_memory<T: Trait>(
|
|||||||
|
|
||||||
// Define a function `fn init_env<E: Ext>() -> HostFunctionSet<E>` that returns
|
// Define a function `fn init_env<E: Ext>() -> HostFunctionSet<E>` that returns
|
||||||
// a function set which can be imported by an executed contract.
|
// a function set which can be imported by an executed contract.
|
||||||
define_env!(init_env, <E: Ext>,
|
define_env!(Env, <E: Ext>,
|
||||||
|
|
||||||
// Account for used gas. Traps if gas used is greater than gas limit.
|
// Account for used gas. Traps if gas used is greater than gas limit.
|
||||||
//
|
//
|
||||||
// - amount: How much gas is used.
|
// - amount: How much gas is used.
|
||||||
gas(ctx, amount: u32) => {
|
gas(ctx, amount: u32) => {
|
||||||
let amount = <<E::T as Trait>::Gas as As<u32>>::sa(amount);
|
charge_gas(&mut ctx.gas_meter, ctx.schedule, RuntimeToken::Explicit(amount))?;
|
||||||
charge_gas(&mut ctx.gas_meter, amount)?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -251,8 +284,10 @@ define_env!(init_env, <E: Ext>,
|
|||||||
};
|
};
|
||||||
let input_data = read_sandbox_memory(ctx, input_data_ptr, input_data_len)?;
|
let input_data = read_sandbox_memory(ctx, input_data_ptr, input_data_len)?;
|
||||||
|
|
||||||
// Clear the scratch buffer in any case.
|
// Grab the scratch buffer and put in its' place an empty one.
|
||||||
ctx.scratch_buf.clear();
|
// We will use it for creating `EmptyOutputBuf` container for the call.
|
||||||
|
let scratch_buf = mem::replace(&mut ctx.scratch_buf, Vec::new());
|
||||||
|
let empty_output_buf = EmptyOutputBuf::from_spare_vec(scratch_buf);
|
||||||
|
|
||||||
let nested_gas_limit = if gas == 0 {
|
let nested_gas_limit = if gas == 0 {
|
||||||
ctx.gas_meter.gas_left()
|
ctx.gas_meter.gas_left()
|
||||||
@@ -260,22 +295,33 @@ define_env!(init_env, <E: Ext>,
|
|||||||
<<E::T as Trait>::Gas as As<u64>>::sa(gas)
|
<<E::T as Trait>::Gas as As<u64>>::sa(gas)
|
||||||
};
|
};
|
||||||
let ext = &mut ctx.ext;
|
let ext = &mut ctx.ext;
|
||||||
let scratch_buf = &mut ctx.scratch_buf;
|
|
||||||
let call_outcome = ctx.gas_meter.with_nested(nested_gas_limit, |nested_meter| {
|
let call_outcome = ctx.gas_meter.with_nested(nested_gas_limit, |nested_meter| {
|
||||||
match nested_meter {
|
match nested_meter {
|
||||||
Some(nested_meter) => ext.call(&callee, value, nested_meter, &input_data, scratch_buf),
|
Some(nested_meter) => {
|
||||||
|
ext.call(
|
||||||
|
&callee,
|
||||||
|
value,
|
||||||
|
nested_meter,
|
||||||
|
&input_data,
|
||||||
|
empty_output_buf
|
||||||
|
)
|
||||||
|
.map_err(|_| ())
|
||||||
|
}
|
||||||
// there is not enough gas to allocate for the nested call.
|
// there is not enough gas to allocate for the nested call.
|
||||||
None => Err(()),
|
None => Err(()),
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
match call_outcome {
|
match call_outcome {
|
||||||
Ok(()) => Ok(0),
|
Ok(CallReceipt { output_data }) => {
|
||||||
|
ctx.scratch_buf = output_data;
|
||||||
|
Ok(0)
|
||||||
|
},
|
||||||
Err(_) => Ok(1),
|
Err(_) => Ok(1),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
// Create a contract with code returned by the specified initializer code.
|
// Instantiate a contract with code returned by the specified initializer code.
|
||||||
//
|
//
|
||||||
// This function creates an account and executes initializer code. After the execution,
|
// This function creates an account and executes initializer code. After the execution,
|
||||||
// the returned buffer is saved as the code of the created account.
|
// the returned buffer is saved as the code of the created account.
|
||||||
@@ -302,7 +348,10 @@ define_env!(init_env, <E: Ext>,
|
|||||||
input_data_ptr: u32,
|
input_data_ptr: u32,
|
||||||
input_data_len: u32
|
input_data_len: u32
|
||||||
) -> u32 => {
|
) -> u32 => {
|
||||||
let init_code = read_sandbox_memory(ctx, init_code_ptr, init_code_len)?;
|
let code_hash = {
|
||||||
|
let code_hash_buf = read_sandbox_memory(ctx, init_code_ptr, init_code_len)?;
|
||||||
|
<CodeHash<<E as Ext>::T>>::decode(&mut &code_hash_buf[..]).ok_or_else(|| sandbox::HostError)?
|
||||||
|
};
|
||||||
let value = {
|
let value = {
|
||||||
let value_buf = read_sandbox_memory(ctx, value_ptr, value_len)?;
|
let value_buf = read_sandbox_memory(ctx, value_ptr, value_len)?;
|
||||||
BalanceOf::<<E as Ext>::T>::decode(&mut &value_buf[..])
|
BalanceOf::<<E as Ext>::T>::decode(&mut &value_buf[..])
|
||||||
@@ -319,15 +368,23 @@ define_env!(init_env, <E: Ext>,
|
|||||||
<<E::T as Trait>::Gas as As<u64>>::sa(gas)
|
<<E::T as Trait>::Gas as As<u64>>::sa(gas)
|
||||||
};
|
};
|
||||||
let ext = &mut ctx.ext;
|
let ext = &mut ctx.ext;
|
||||||
let create_outcome = ctx.gas_meter.with_nested(nested_gas_limit, |nested_meter| {
|
let instantiate_outcome = ctx.gas_meter.with_nested(nested_gas_limit, |nested_meter| {
|
||||||
match nested_meter {
|
match nested_meter {
|
||||||
Some(nested_meter) => ext.create(&init_code, value, nested_meter, &input_data),
|
Some(nested_meter) => {
|
||||||
|
ext.instantiate(
|
||||||
|
&code_hash,
|
||||||
|
value,
|
||||||
|
nested_meter,
|
||||||
|
&input_data
|
||||||
|
)
|
||||||
|
.map_err(|_| ())
|
||||||
|
}
|
||||||
// there is not enough gas to allocate for the nested call.
|
// there is not enough gas to allocate for the nested call.
|
||||||
None => Err(()),
|
None => Err(()),
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
match create_outcome {
|
match instantiate_outcome {
|
||||||
Ok(CreateReceipt { address }) => {
|
Ok(InstantiateReceipt { address }) => {
|
||||||
// Write the address to the scratch buffer.
|
// Write the address to the scratch buffer.
|
||||||
address.encode_to(&mut ctx.scratch_buf);
|
address.encode_to(&mut ctx.scratch_buf);
|
||||||
Ok(0)
|
Ok(0)
|
||||||
@@ -339,20 +396,34 @@ define_env!(init_env, <E: Ext>,
|
|||||||
// Save a data buffer as a result of the execution, terminate the execution and return a
|
// Save a data buffer as a result of the execution, terminate the execution and return a
|
||||||
// successful result to the caller.
|
// successful result to the caller.
|
||||||
ext_return(ctx, data_ptr: u32, data_len: u32) => {
|
ext_return(ctx, data_ptr: u32, data_len: u32) => {
|
||||||
let data_len_in_gas = <<E::T as Trait>::Gas as As<u64>>::sa(data_len as u64);
|
match ctx
|
||||||
let price = (ctx.schedule.return_data_per_byte_cost)
|
.gas_meter
|
||||||
.checked_mul(&data_len_in_gas)
|
.charge(
|
||||||
.ok_or(sandbox::HostError)?;
|
ctx.schedule,
|
||||||
|
RuntimeToken::ReturnData(data_len)
|
||||||
match ctx.gas_meter.charge(price) {
|
)
|
||||||
|
{
|
||||||
GasMeterResult::Proceed => (),
|
GasMeterResult::Proceed => (),
|
||||||
GasMeterResult::OutOfGas => return Err(sandbox::HostError),
|
GasMeterResult::OutOfGas => return Err(sandbox::HostError),
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.output_data.resize(data_len as usize, 0);
|
let empty_output_buf = ctx
|
||||||
ctx.memory.get(data_ptr, &mut ctx.output_data)?;
|
.empty_output_buf
|
||||||
|
.take()
|
||||||
ctx.special_trap = Some(SpecialTrap::Return);
|
.expect(
|
||||||
|
"`empty_output_buf` is taken only here;
|
||||||
|
`ext_return` traps;
|
||||||
|
`Runtime` can only be used only for one execution;
|
||||||
|
qed"
|
||||||
|
);
|
||||||
|
let output_buf = empty_output_buf.fill(
|
||||||
|
data_len as usize,
|
||||||
|
|slice_mut| {
|
||||||
|
// Read the memory at the specified pointer to the provided slice.
|
||||||
|
ctx.memory.get(data_ptr, slice_mut)
|
||||||
|
}
|
||||||
|
)?;
|
||||||
|
ctx.special_trap = Some(SpecialTrap::Return(output_buf));
|
||||||
|
|
||||||
// The trap mechanism is used to immediately terminate the execution.
|
// The trap mechanism is used to immediately terminate the execution.
|
||||||
// This trap should be handled appropriately before returning the result
|
// This trap should be handled appropriately before returning the result
|
||||||
@@ -370,6 +441,12 @@ define_env!(init_env, <E: Ext>,
|
|||||||
Ok(())
|
Ok(())
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Stores the address of the current contract into the scratch buffer.
|
||||||
|
ext_address(ctx) => {
|
||||||
|
ctx.scratch_buf = ctx.ext.address().encode();
|
||||||
|
Ok(())
|
||||||
|
},
|
||||||
|
|
||||||
// Returns the size of the input buffer.
|
// Returns the size of the input buffer.
|
||||||
ext_input_size(ctx) -> u32 => {
|
ext_input_size(ctx) -> u32 => {
|
||||||
Ok(ctx.input_data.len() as u32)
|
Ok(ctx.input_data.len() as u32)
|
||||||
@@ -392,7 +469,7 @@ define_env!(init_env, <E: Ext>,
|
|||||||
|
|
||||||
// Finally, perform the write.
|
// Finally, perform the write.
|
||||||
write_sandbox_memory(
|
write_sandbox_memory(
|
||||||
ctx.schedule.sandbox_data_write_cost,
|
ctx.schedule,
|
||||||
ctx.gas_meter,
|
ctx.gas_meter,
|
||||||
&ctx.memory,
|
&ctx.memory,
|
||||||
dest_ptr,
|
dest_ptr,
|
||||||
@@ -424,7 +501,7 @@ define_env!(init_env, <E: Ext>,
|
|||||||
|
|
||||||
// Finally, perform the write.
|
// Finally, perform the write.
|
||||||
write_sandbox_memory(
|
write_sandbox_memory(
|
||||||
ctx.schedule.sandbox_data_write_cost,
|
ctx.schedule,
|
||||||
ctx.gas_meter,
|
ctx.gas_meter,
|
||||||
&ctx.memory,
|
&ctx.memory,
|
||||||
dest_ptr,
|
dest_ptr,
|
||||||
Reference in New Issue
Block a user