Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Contracts module rejig #1358

Merged
merged 116 commits into from
Jan 17, 2019
Merged
Show file tree
Hide file tree
Changes from 82 commits
Commits
Show all changes
116 commits
Select commit Hold shift + click to select a range
429a030
Move prepare under code.
pepyakin Nov 20, 2018
169c5e8
Schedule update
pepyakin Nov 21, 2018
eb64e5e
CodeHash
pepyakin Nov 21, 2018
f79aa14
create takes code_hash
pepyakin Nov 21, 2018
32bbbbd
pass mem def and use code in vm::execute
pepyakin Nov 21, 2018
7f5df27
Actually save and load code
pepyakin Nov 21, 2018
ee53c8c
Use T::Hash as CodeHash
pepyakin Nov 21, 2018
045b1d8
Explicit entrypoint name
pepyakin Nov 21, 2018
1c846b7
Return code_hash and deposit an Event
pepyakin Nov 23, 2018
29fd7b2
Charge for deployed code with gas.
pepyakin Nov 28, 2018
40163e4
ImportSatisfyCheck and FunctionImplProvider
pepyakin Nov 29, 2018
f7b28b3
Progress.
pepyakin Nov 29, 2018
0a08178
Use new infrastructure for checking imports
pepyakin Nov 29, 2018
487c630
Rename entrypoint to entrypoint_name
pepyakin Dec 1, 2018
62e7b5d
Use strings instead of a Error enum
pepyakin Dec 3, 2018
09d9a2b
Clean
pepyakin Dec 3, 2018
e520a2d
WIP
pepyakin Dec 7, 2018
c1e0018
Fix macro_define_env test.
pepyakin Dec 14, 2018
9738d81
Fix vm code tests.
pepyakin Dec 20, 2018
56f0c4f
Remove tests for now.
pepyakin Dec 20, 2018
2d8dd9c
Fix borked merge
pepyakin Dec 20, 2018
601359d
Fix build for wasm
pepyakin Dec 20, 2018
4271c48
fmt
pepyakin Dec 20, 2018
56280a0
Scaffolding for abstracting vm.
pepyakin Dec 20, 2018
7053d29
Hook up execution to exec layer.
pepyakin Dec 20, 2018
e629768
Fix vm tests.
pepyakin Dec 20, 2018
cef469f
Use schedule directly in WasmLoader
pepyakin Dec 20, 2018
e1969ae
Implement test language.
pepyakin Dec 20, 2018
bbb50cd
Add input_data test.
pepyakin Dec 20, 2018
53ca6be
Max depth test
pepyakin Dec 20, 2018
3324b8e
ext_caller
pepyakin Dec 20, 2018
c851fd6
Simplify test.
pepyakin Dec 21, 2018
2c025f7
Add TODO
pepyakin Dec 21, 2018
d43d5e3
Some tests and todos.
pepyakin Dec 21, 2018
09ca3b5
top_level
pepyakin Dec 21, 2018
4a12044
Clean.
pepyakin Dec 22, 2018
700cf43
Restore a couple of integration tests.
pepyakin Dec 22, 2018
8f58d0e
Add a few comments.
pepyakin Dec 22, 2018
34a26b0
Add ext_address runtime call.
pepyakin Dec 22, 2018
6519b69
Deduplicate caller/self_account
pepyakin Dec 22, 2018
f40a2c7
Add not_exists test.
pepyakin Dec 22, 2018
f2727a3
Change bool to TransferCause.
pepyakin Dec 22, 2018
ddf9658
Add address tests.
pepyakin Dec 24, 2018
b6ded65
Remove output_buf from parameter.
pepyakin Dec 24, 2018
5b62988
return from start fn.
pepyakin Dec 26, 2018
b5fd8dc
Smart gas meter
pepyakin Dec 26, 2018
8b1f113
Tracing
pepyakin Dec 26, 2018
e2559c6
Fix prepare tests.
pepyakin Dec 27, 2018
a07066a
Code moving
pepyakin Jan 2, 2019
de81162
Add ExecFeeToken
pepyakin Jan 2, 2019
97eb2a9
Use tokens everywhere.
pepyakin Jan 2, 2019
16351ca
Make it compile in no_std.
pepyakin Jan 2, 2019
d9d2769
Lift all test requirements to TestAuxiliaries
pepyakin Jan 2, 2019
81be0fd
A minor clean
pepyakin Jan 2, 2019
7451166
First create tests
pepyakin Jan 2, 2019
bf094c7
Remove unneeded TODO
pepyakin Jan 3, 2019
5239bf8
Docs.
pepyakin Jan 3, 2019
46ac8fa
Code shuffling
pepyakin Jan 3, 2019
e17b369
Rename create → instantiate
pepyakin Jan 3, 2019
6d5e0aa
Add test address.
pepyakin Jan 3, 2019
f48f565
Merge remote-tracking branch 'origin/master' into ser-putcode-model
pepyakin Jan 3, 2019
5365d8b
Code shuffling
pepyakin Jan 3, 2019
224fadf
Add base_fee tests.
pepyakin Jan 3, 2019
01390c7
rejig the code
pepyakin Jan 3, 2019
3f6b174
Add some comments
pepyakin Jan 3, 2019
83e4ee1
on_finalise comment
pepyakin Jan 3, 2019
452977d
Move event deposit further
pepyakin Jan 4, 2019
0af6aa8
Update Cargo.lock
pepyakin Jan 7, 2019
a5a5d20
Use crates.io version of pwasm-utils
pepyakin Jan 7, 2019
af9427b
Format todo comments
pepyakin Jan 7, 2019
89932cd
Fix formatting
pepyakin Jan 7, 2019
9265e73
Comments
pepyakin Jan 7, 2019
56e9969
EmptyOutputBuf and OutputBuf split.
pepyakin Jan 7, 2019
755fccd
Restore code_hash
pepyakin Jan 7, 2019
d3693df
Fix node-executor.
pepyakin Jan 7, 2019
f807766
Fix typo
pepyakin Jan 8, 2019
e58fa2c
Fix fmt
pepyakin Jan 8, 2019
088ec93
Merge remote-tracking branch 'origin/master'
pepyakin Jan 9, 2019
2cbf622
Update srml/contract/src/account_db.rs
gnunicorn Jan 9, 2019
785bbb9
Update srml/contract/src/lib.rs
gnunicorn Jan 9, 2019
4e2e443
Line wraps
pepyakin Jan 9, 2019
a2fada6
Wrapping macros
pepyakin Jan 9, 2019
d8f433f
Add _ prefix
pepyakin Jan 9, 2019
bf2c8ae
Grumbles
pepyakin Jan 9, 2019
7c8d487
Doc updates.
pepyakin Jan 9, 2019
25cce89
Update srml/contract/src/wasm/mod.rs
gnunicorn Jan 10, 2019
a9fc6ef
Update srml/contract/src/lib.rs
gnunicorn Jan 10, 2019
c166e74
Merge branch 'ser-putcode-model' of github.com:paritytech/substrate i…
pepyakin Jan 10, 2019
cad39fe
Add comment
pepyakin Jan 10, 2019
bbbac91
Use saturation to signal overflow
pepyakin Jan 10, 2019
09e4a99
Add prepare_test! macro
pepyakin Jan 10, 2019
0087a63
Require deploy function.
pepyakin Jan 10, 2019
260665d
Add entry point tests
pepyakin Jan 10, 2019
59a2a76
Add comment.
pepyakin Jan 10, 2019
b666bd6
Rename code → code_cache to better describe
pepyakin Jan 10, 2019
9b13600
Get rid of weird match!
pepyakin Jan 10, 2019
c351d9e
Recompile binaries
pepyakin Jan 10, 2019
49e4d04
Add comments
pepyakin Jan 10, 2019
1ebe45f
refuse_instantiate_with_value_below_existential_deposit
pepyakin Jan 10, 2019
7d76184
Little fix
pepyakin Jan 10, 2019
7b40828
Make test more complete
pepyakin Jan 10, 2019
1aa7dc7
Clean
pepyakin Jan 10, 2019
66e2cc6
Add integration test for instantiation
pepyakin Jan 10, 2019
fd085c7
Merge remote-tracking branch 'origin/master' into ser-putcode-model
pepyakin Jan 10, 2019
9974158
Rebuild runtime.
pepyakin Jan 11, 2019
e19d643
Merge remote-tracking branch 'origin/master' into ser-putcode-model
pepyakin Jan 14, 2019
27f9d5f
Add some tests.
pepyakin Jan 14, 2019
1401dec
Attach an issue to a TODO
pepyakin Jan 14, 2019
449f5ad
Attach another issue
pepyakin Jan 14, 2019
34a1698
Apply suggestions from code review
gnunicorn Jan 15, 2019
8108d87
Update srml/contract/src/exec.rs
gnunicorn Jan 15, 2019
930cbf2
Update srml/contract/src/exec.rs
gnunicorn Jan 15, 2019
40cf826
Recompile node_runtime
pepyakin Jan 15, 2019
8cc6f33
Merge remote-tracking branch 'origin/master' into ser-putcode-model
pepyakin Jan 15, 2019
7c9c8a6
Merge and fix merge conflicts
pepyakin Jan 16, 2019
41f769a
Merge and fix conflicts
pepyakin Jan 17, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
70 changes: 19 additions & 51 deletions node/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ mod tests {
use primitives::{twox_128, Blake2Hasher, ChangesTrieConfiguration,
ed25519::{Public, Pair}};
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 {balances, staking, session, system, consensus, timestamp, treasury, contract};
use contract::ContractAddressFor;
Expand Down Expand Up @@ -312,9 +312,9 @@ mod tests {
1,
GENESIS_HASH.into(),
if support_changes_trie {
hex!("22e7fc466d555b9dce285425081d89751b2063243684979df3840b3ac7e8ecdc").into()
hex!("46b1e3ab29eb2f0aeea69dc324b7e83ec37ccd8dd974ddc11403b89a982cbaae").into()
} else {
hex!("7395363e53e682984f817fb1d5a862c5ce8b817375c06270d7a39be7097ad953").into()
hex!("9d03b99028b6ab0c4f08641c3ba8010406e918a2d18f0be506b3d64ff18f1b66").into()
},
if support_changes_trie {
vec![changes_trie_log(
Expand All @@ -340,7 +340,7 @@ mod tests {
construct_block(
2,
block1(false).1,
hex!("66b9625c9c824de867815215528fe43014d50af7fb95c8da120910c220a46f6b").into(),
hex!("f633ab9f0266e520801e681463f6085c1d87d408d3384efb932f8bcbe27b91e3").into(),
vec![ // session changes here, so we add a grandpa change signal log.
Log::from(::grandpa::RawLog::AuthoritiesChangeSignal(0, vec![
(Keyring::One.to_raw_public().into(), 1),
Expand Down Expand Up @@ -369,7 +369,7 @@ mod tests {
construct_block(
1,
GENESIS_HASH.into(),
hex!("66dfdf3a0ef93ec49ec36c0a65fe328d085a865c2382397b2cd6468e391f2f51").into(),
hex!("ca013fb95c220233695f73cf734580e3aa9bafacc2dd7ed7d2ec8d2121868250").into(),
vec![],
vec![
CheckedExtrinsic {
Expand Down Expand Up @@ -536,6 +536,8 @@ mod tests {
(import "env" "ext_input_size" (func $ext_input_size (result i32)))
(import "env" "ext_input_copy" (func $ext_input_copy (param i32 i32 i32)))
(import "env" "memory" (memory 1 1))
(func (export "deploy")
)
(func (export "call")
(block $fail
;; fail if ext_input_size != 4
Expand Down Expand Up @@ -603,63 +605,23 @@ 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]
fn deploying_wasm_contract_should_work() {
let mut t = new_test_ext(COMPACT_CODE, false);

let code_transfer = wabt::wat2wasm(CODE_TRANSFER).unwrap();
let code_ctor_transfer = wabt::wat2wasm(&code_ctor(&code_transfer)).unwrap();
let transfer_code = wabt::wat2wasm(CODE_TRANSFER).unwrap();
let transfer_ch = <Runtime as system::Trait>::Hashing::hash(&transfer_code);

let addr = <Runtime as contract::Trait>::DetermineContractAddress::contract_address_for(
&code_ctor_transfer,
&transfer_ch,
&[],
&charlie(),
);

let b = construct_block(
1,
GENESIS_HASH.into(),
hex!("8197608e90fff1f7d92b35381169242d081779b1718c910689f2589a8ac09b44").into(),
hex!("87866feb911e43cebc31c4ea726868d71b579b9530637e0f14315512df444a66").into(),
vec![],
vec![
CheckedExtrinsic {
Expand All @@ -669,11 +631,17 @@ mod tests {
CheckedExtrinsic {
signed: Some((charlie(), 0)),
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 {
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(
contract::Call::call::<Runtime>(addr, 10.into(), 10_000.into(), vec![0x00, 0x01, 0x02, 0x03])
),
Expand All @@ -685,7 +653,7 @@ mod tests {

runtime_io::with_externalities(&mut t, || {
// 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);
});
}

Expand Down
6 changes: 3 additions & 3 deletions node/runtime/wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
2 changes: 1 addition & 1 deletion srml/contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["Parity Technologies <admin@parity.io>"]

[dependencies]
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.1", default-features = false }
parity-codec-derive = { version = "2.1", default-features = false }
parity-wasm = { version = "0.31", default-features = false }
Expand Down
23 changes: 14 additions & 9 deletions srml/contract/src/account_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! Auxilliaries to help with managing partial changes to accounts state.

use super::{CodeOf, StorageOf, Trait};
use super::{CodeHash, CodeHashOf, StorageOf, Trait};
use double_map::StorageDoubleMap;
use rstd::cell::RefCell;
use rstd::collections::btree_map::{BTreeMap, Entry};
Expand All @@ -26,7 +26,8 @@ use {balances, system};

pub struct ChangeEntry<T: Trait> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is rust not complaining that his pub structs doesn't have appropriate docs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the reason is although it's public it is not exported, thus even if we leave doc here it will not be externally visible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another possibility is that we don't have #![deny(missing_docs)] in this crate : )

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>>>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not introduce an enum that expresses this directly in code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this code was written in this style because there is some usage of Option combinators.

storage: BTreeMap<Vec<u8>, Option<Vec<u8>>>,
}

Expand All @@ -45,7 +46,7 @@ pub type ChangeSet<T> = BTreeMap<<T as system::Trait>::AccountId, ChangeEntry<T>

pub trait AccountDb<T: Trait> {
fn get_storage(&self, account: &T::AccountId, location: &[u8]) -> Option<Vec<u8>>;
fn get_code(&self, account: &T::AccountId) -> Vec<u8>;
fn get_code(&self, account: &T::AccountId) -> Option<CodeHash<T>>;
fn get_balance(&self, account: &T::AccountId) -> T::Balance;

fn commit(&mut self, change_set: ChangeSet<T>);
Expand All @@ -56,8 +57,8 @@ impl<T: Trait> AccountDb<T> for DirectAccountDb {
fn get_storage(&self, account: &T::AccountId, location: &[u8]) -> Option<Vec<u8>> {
<StorageOf<T>>::get(account.clone(), location.to_vec())
}
fn get_code(&self, account: &T::AccountId) -> Vec<u8> {
<CodeOf<T>>::get(account)
fn get_code(&self, account: &T::AccountId) -> Option<CodeHash<T>> {
<CodeHashOf<T>>::get(account)
}
fn get_balance(&self, account: &T::AccountId) -> T::Balance {
balances::Module::<T>::free_balance(account)
Expand All @@ -69,13 +70,17 @@ impl<T: Trait> AccountDb<T> for DirectAccountDb {
balances::Module::<T>::set_free_balance_creating(&address, balance)
{
// Account killed. This will ultimately lead to calling `OnFreeBalanceZero` callback
// which will make removal of CodeOf and StorageOf for this account.
// which will make removal of CodeHashOf and StorageOf for this account.
// In order to avoid writing over the deleted properties we `continue` here.
continue;
}
}
if let Some(code) = changed.code {
<CodeOf<T>>::insert(&address, &code);
if let Some(code) = code {
<CodeHashOf<T>>::insert(&address, code);
} else {
<CodeHashOf<T>>::remove(&address);
}
}
for (k, v) in changed.storage.into_iter() {
if let Some(value) = v {
Expand Down Expand Up @@ -117,7 +122,7 @@ impl<'a, T: Trait> OverlayAccountDb<'a, T> {
.storage
.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
.borrow_mut()
.entry(account.clone())
Expand All @@ -142,7 +147,7 @@ impl<'a, T: Trait> AccountDb<T> for OverlayAccountDb<'a, T> {
.cloned()
.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
.borrow()
.get(account)
Expand Down
Loading