diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index 4f9a55e87d..b89743546f 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -2178,7 +2178,7 @@ dependencies = [ "sr-io 2.0.0", "sr-primitives 2.0.0", "srml-balances 2.0.0", - "srml-contract 2.0.0", + "srml-contracts 2.0.0", "srml-grandpa 2.0.0", "srml-indices 2.0.0", "srml-session 2.0.0", @@ -2238,7 +2238,7 @@ dependencies = [ "sr-version 2.0.0", "srml-aura 2.0.0", "srml-balances 2.0.0", - "srml-contract 2.0.0", + "srml-contracts 2.0.0", "srml-council 2.0.0", "srml-democracy 2.0.0", "srml-executive 2.0.0", @@ -3499,7 +3499,7 @@ dependencies = [ ] [[package]] -name = "srml-contract" +name = "srml-contracts" version = "2.0.0" dependencies = [ "assert_matches 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/substrate/Cargo.toml b/substrate/Cargo.toml index 2f4139c7af..f45353204f 100644 --- a/substrate/Cargo.toml +++ b/substrate/Cargo.toml @@ -64,7 +64,7 @@ members = [ "srml/assets", "srml/aura", "srml/balances", - "srml/contract", + "srml/contracts", "srml/council", "srml/democracy", "srml/example", diff --git a/substrate/README.adoc b/substrate/README.adoc index 2a76525914..826370ee6f 100644 --- a/substrate/README.adoc +++ b/substrate/README.adoc @@ -388,7 +388,7 @@ substrate-trie sr-api, sr-io, sr-primitives, sr-sandbox, sr-std, sr-version * Substrate Runtime Module Library (SRML) [source, shell] -srml-assets, srml-balances, srml-consensus, srml-contract, srml-council, srml-democracy, srml-example, +srml-assets, srml-balances, srml-consensus, srml-contracts, srml-council, srml-democracy, srml-example, srml-executive, srml-metadata, srml-session, srml-staking, srml-support, srml-system, srml-timestamp, srml-treasury * Node diff --git a/substrate/node/cli/src/chain_spec.rs b/substrate/node/cli/src/chain_spec.rs index 7f3c250d7c..672c75b1a9 100644 --- a/substrate/node/cli/src/chain_spec.rs +++ b/substrate/node/cli/src/chain_spec.rs @@ -20,7 +20,7 @@ use primitives::{ed25519, sr25519, Pair, crypto::UncheckedInto}; use node_primitives::{AccountId, AuraId}; use node_runtime::{CouncilSeatsConfig, AuraConfig, DemocracyConfig, SystemConfig, SessionConfig, StakingConfig, StakerStatus, TimestampConfig, BalancesConfig, TreasuryConfig, - SudoConfig, ContractConfig, GrandpaConfig, IndicesConfig, Permill, Perbill, SessionKeys}; + SudoConfig, ContractsConfig, GrandpaConfig, IndicesConfig, Permill, Perbill, SessionKeys}; pub use node_runtime::GenesisConfig; use substrate_service; use hex_literal::hex; @@ -162,7 +162,7 @@ fn staging_testnet_config_genesis() -> GenesisConfig { spend_period: 1 * DAYS, burn: Permill::from_percent(50), }), - contract: Some(ContractConfig { + contracts: Some(ContractsConfig { signed_claim_handicap: 2, rent_byte_price: 4, rent_deposit_offset: 1000, @@ -268,7 +268,7 @@ pub fn testnet_genesis( const ENDOWMENT: u128 = 1 << 20; let council_desired_seats = (endowed_accounts.len() / 2 - initial_authorities.len()) as u32; - let mut contract_config = ContractConfig { + let mut contracts_config = ContractsConfig { signed_claim_handicap: 2, rent_byte_price: 4, rent_deposit_offset: 1000, @@ -288,7 +288,7 @@ pub fn testnet_genesis( current_schedule: Default::default(), }; // this should only be enabled on development chains - contract_config.current_schedule.enable_println = enable_println; + contracts_config.current_schedule.enable_println = enable_println; GenesisConfig { system: Some(SystemConfig { @@ -349,7 +349,7 @@ pub fn testnet_genesis( spend_period: 12 * 60 * 24, burn: Permill::from_percent(50), }), - contract: Some(contract_config), + contracts: Some(contracts_config), sudo: Some(SudoConfig { key: root_key, }), diff --git a/substrate/node/executor/Cargo.toml b/substrate/node/executor/Cargo.toml index 4d5c69b281..a5e5958f6a 100644 --- a/substrate/node/executor/Cargo.toml +++ b/substrate/node/executor/Cargo.toml @@ -27,7 +27,7 @@ staking = { package = "srml-staking", path = "../../srml/staking" } system = { package = "srml-system", path = "../../srml/system" } timestamp = { package = "srml-timestamp", path = "../../srml/timestamp" } treasury = { package = "srml-treasury", path = "../../srml/treasury" } -contract = { package = "srml-contract", path = "../../srml/contract" } +contracts = { package = "srml-contracts", path = "../../srml/contracts" } grandpa = { package = "srml-grandpa", path = "../../srml/grandpa" } indices = { package = "srml-indices", path = "../../srml/indices" } wabt = "~0.7.4" diff --git a/substrate/node/executor/src/lib.rs b/substrate/node/executor/src/lib.rs index 4f0c38d884..1622e92c6d 100644 --- a/substrate/node/executor/src/lib.rs +++ b/substrate/node/executor/src/lib.rs @@ -23,7 +23,17 @@ pub use substrate_executor::NativeExecutor; use substrate_executor::native_executor_instance; -native_executor_instance!(pub Executor, node_runtime::api::dispatch, node_runtime::native_version, include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/node_runtime.compact.wasm")); + +// Declare an instance of the native executor named `Executor`. Include the wasm binary as the +// equivalent wasm code. +native_executor_instance!( + pub Executor, + node_runtime::api::dispatch, + node_runtime::native_version, + include_bytes!( + "../../runtime/wasm/target/wasm32-unknown-unknown/release/node_runtime.compact.wasm" + ) +); #[cfg(test)] mod tests { @@ -39,8 +49,8 @@ mod tests { use node_primitives::{Hash, BlockNumber, AccountId}; use runtime_primitives::traits::{Header as HeaderT, Hash as HashT}; use runtime_primitives::{generic::Era, ApplyOutcome, ApplyError, ApplyResult, Perbill}; - use {balances, indices, system, staking, timestamp, treasury, contract}; - use contract::ContractAddressFor; + use {balances, indices, system, staking, timestamp, treasury, contracts}; + use contracts::ContractAddressFor; use system::{EventRecord, Phase}; use node_runtime::{Header, Block, UncheckedExtrinsic, CheckedExtrinsic, Call, Runtime, Balances, BuildStorage, GenesisConfig, BalancesConfig, SessionConfig, StakingConfig, System, @@ -48,8 +58,23 @@ mod tests { use wabt; use primitives::map; - const BLOATY_CODE: &[u8] = include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/node_runtime.wasm"); - const COMPACT_CODE: &[u8] = include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/node_runtime.compact.wasm"); + /// The wasm runtime code. + /// + /// `compact` since it is after post-processing with wasm-gc which performs tree-shaking thus + /// making the binary slimmer. There is a convention to use compact version of the runtime + /// as canonical. This is why `native_executor_instance` also uses the compact version of the + /// runtime. + const COMPACT_CODE: &[u8] = + include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/node_runtime.compact.wasm"); + + /// The wasm runtime binary which hasn't undergone the compacting process. + /// + /// The idea here is to pass it as the current runtime code to the executor so the executor will + /// have to execute provided wasm code instead of the native equivalent. This trick is used to + /// test code paths that differ between native and wasm versions. + const BLOATY_CODE: &[u8] = + include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/node_runtime.wasm"); + const GENESIS_HASH: [u8; 32] = [69u8; 32]; type TestExternalities = CoreTestExternalities; @@ -363,7 +388,7 @@ mod tests { council_seats: Some(Default::default()), timestamp: Some(Default::default()), treasury: Some(Default::default()), - contract: Some(Default::default()), + contracts: Some(Default::default()), sudo: Some(Default::default()), grandpa: Some(GrandpaConfig { _genesis_phantom_data: Default::default(), @@ -751,10 +776,17 @@ mod tests { ) ;; Destination AccountId to transfer the funds. ;; Represented by H256 (32 bytes long) in little endian. - (data (i32.const 4) "\09\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00") + (data (i32.const 4) + "\09\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00" + "\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00" + "\00\00\00\00" + ) ;; Amount of value to transfer. ;; Represented by u128 (16 bytes long) in little endian. - (data (i32.const 36) "\06\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00") + (data (i32.const 36) + "\06\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00" + "\00\00" + ) ) "#; @@ -764,7 +796,7 @@ mod tests { let transfer_code = wabt::wat2wasm(CODE_TRANSFER).unwrap(); let transfer_ch = ::Hashing::hash(&transfer_code); - let addr = ::DetermineContractAddress::contract_address_for( + let addr = ::DetermineContractAddress::contract_address_for( &transfer_ch, &[], &charlie(), @@ -781,20 +813,25 @@ mod tests { }, CheckedExtrinsic { signed: Some((charlie(), 0)), - function: Call::Contract( - contract::Call::put_code::(10_000, transfer_code) + function: Call::Contracts( + contracts::Call::put_code::(10_000, transfer_code) ), }, CheckedExtrinsic { signed: Some((charlie(), 1)), - function: Call::Contract( - contract::Call::create::(10, 10_000, transfer_ch, Vec::new()) + function: Call::Contracts( + contracts::Call::create::(10, 10_000, transfer_ch, Vec::new()) ), }, CheckedExtrinsic { signed: Some((charlie(), 2)), - function: Call::Contract( - contract::Call::call::(indices::address::Address::Id(addr.clone()), 10, 10_000, vec![0x00, 0x01, 0x02, 0x03]) + function: Call::Contracts( + contracts::Call::call::( + indices::address::Address::Id(addr.clone()), + 10, + 10_000, + vec![0x00, 0x01, 0x02, 0x03] + ) ), }, ] @@ -807,7 +844,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::ContractInfoOf::::get(addr) + &contracts::ContractInfoOf::::get(addr) .and_then(|c| c.get_alive()) .unwrap() .code_hash, @@ -861,8 +898,7 @@ mod tests { #[test] fn panic_execution_gives_error() { - let foreign_code = include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/node_runtime.wasm"); - let mut t = TestExternalities::::new_with_code(foreign_code, map![ + let mut t = TestExternalities::::new_with_code(BLOATY_CODE, map![ blake2_256(&>::key_for(alice())).to_vec() => { vec![69u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] }, @@ -889,8 +925,7 @@ mod tests { #[test] fn successful_execution_gives_ok() { - let foreign_code = include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/node_runtime.compact.wasm"); - let mut t = TestExternalities::::new_with_code(foreign_code, map![ + let mut t = TestExternalities::::new_with_code(COMPACT_CODE, map![ blake2_256(&>::key_for(alice())).to_vec() => { vec![111u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] }, diff --git a/substrate/node/runtime/Cargo.toml b/substrate/node/runtime/Cargo.toml index a87ce511cd..f1981a5852 100644 --- a/substrate/node/runtime/Cargo.toml +++ b/substrate/node/runtime/Cargo.toml @@ -17,7 +17,7 @@ version = { package = "sr-version", path = "../../core/sr-version", default-feat support = { package = "srml-support", path = "../../srml/support", default-features = false } aura = { package = "srml-aura", path = "../../srml/aura", default-features = false } balances = { package = "srml-balances", path = "../../srml/balances", default-features = false } -contract = { package = "srml-contract", path = "../../srml/contract", default-features = false } +contracts = { package = "srml-contracts", path = "../../srml/contracts", default-features = false } council = { package = "srml-council", path = "../../srml/council", default-features = false } democracy = { package = "srml-democracy", path = "../../srml/democracy", default-features = false } executive = { package = "srml-executive", path = "../../srml/executive", default-features = false } @@ -39,7 +39,7 @@ substrate-keyring = { path = "../../core/keyring", optional = true } [features] default = ["std"] core = [ - "contract/core", + "contracts/core", ] std = [ "parity-codec/std", @@ -49,7 +49,7 @@ std = [ "support/std", "aura/std", "balances/std", - "contract/std", + "contracts/std", "council/std", "democracy/std", "executive/std", diff --git a/substrate/node/runtime/src/lib.rs b/substrate/node/runtime/src/lib.rs index 67a5b450a9..ec529696a8 100644 --- a/substrate/node/runtime/src/lib.rs +++ b/substrate/node/runtime/src/lib.rs @@ -58,8 +58,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("node"), impl_name: create_runtime_str!("substrate-node"), authoring_version: 10, - spec_version: 95, - impl_version: 95, + spec_version: 96, + impl_version: 96, apis: RUNTIME_API_VERSIONS, }; @@ -112,7 +112,7 @@ impl indices::Trait for Runtime { impl balances::Trait for Runtime { type Balance = Balance; - type OnFreeBalanceZero = ((Staking, Contract), Session); + type OnFreeBalanceZero = ((Staking, Contracts), Session); type OnNewAccount = Indices; type Event = Event; type TransactionPayment = (); @@ -217,14 +217,14 @@ impl treasury::Trait for Runtime { type ProposalRejection = (); } -impl contract::Trait for Runtime { +impl contracts::Trait for Runtime { type Currency = Balances; type Call = Call; type Event = Event; type Gas = u64; - type DetermineContractAddress = contract::SimpleAddressDeterminator; - type ComputeDispatchFee = contract::DefaultDispatchFeeComputor; - type TrieIdGenerator = contract::TrieIdFromParentCounter; + type DetermineContractAddress = contracts::SimpleAddressDeterminator; + type ComputeDispatchFee = contracts::DefaultDispatchFeeComputor; + type TrieIdGenerator = contracts::TrieIdFromParentCounter; type GasPayment = (); } @@ -261,7 +261,7 @@ construct_runtime!( FinalityTracker: finality_tracker::{Module, Call, Inherent}, Grandpa: grandpa::{Module, Call, Storage, Config, Event}, Treasury: treasury, - Contract: contract, + Contracts: contracts, Sudo: sudo, } ); diff --git a/substrate/node/runtime/wasm/Cargo.lock b/substrate/node/runtime/wasm/Cargo.lock index dcb7e8f26b..89c1bf9303 100644 --- a/substrate/node/runtime/wasm/Cargo.lock +++ b/substrate/node/runtime/wasm/Cargo.lock @@ -1484,7 +1484,7 @@ dependencies = [ "sr-version 2.0.0", "srml-aura 2.0.0", "srml-balances 2.0.0", - "srml-contract 2.0.0", + "srml-contracts 2.0.0", "srml-council 2.0.0", "srml-democracy 2.0.0", "srml-executive 2.0.0", @@ -2443,7 +2443,7 @@ dependencies = [ ] [[package]] -name = "srml-contract" +name = "srml-contracts" version = "2.0.0" dependencies = [ "parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/substrate/srml/contract/COMPLEXITY.md b/substrate/srml/contracts/COMPLEXITY.md similarity index 100% rename from substrate/srml/contract/COMPLEXITY.md rename to substrate/srml/contracts/COMPLEXITY.md diff --git a/substrate/srml/contract/Cargo.toml b/substrate/srml/contracts/Cargo.toml similarity index 98% rename from substrate/srml/contract/Cargo.toml rename to substrate/srml/contracts/Cargo.toml index 8d3507db35..08c44bf63a 100644 --- a/substrate/srml/contract/Cargo.toml +++ b/substrate/srml/contracts/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "srml-contract" +name = "srml-contracts" version = "2.0.0" authors = ["Parity Technologies "] edition = "2018" diff --git a/substrate/srml/contract/src/account_db.rs b/substrate/srml/contracts/src/account_db.rs similarity index 97% rename from substrate/srml/contract/src/account_db.rs rename to substrate/srml/contracts/src/account_db.rs index 2421eab00a..80d5fbe3bb 100644 --- a/substrate/srml/contract/src/account_db.rs +++ b/substrate/srml/contracts/src/account_db.rs @@ -75,7 +75,12 @@ pub trait AccountDb { pub struct DirectAccountDb; impl AccountDb for DirectAccountDb { - fn get_storage(&self, _account: &T::AccountId, trie_id: Option<&TrieId>, location: &StorageKey) -> Option> { + fn get_storage( + &self, + _account: &T::AccountId, + trie_id: Option<&TrieId>, + location: &StorageKey + ) -> Option> { trie_id.and_then(|id| child::get_raw(id, &blake2_256(location))) } fn get_code_hash(&self, account: &T::AccountId) -> Option> { @@ -240,7 +245,12 @@ impl<'a, T: Trait> OverlayAccountDb<'a, T> { } impl<'a, T: Trait> AccountDb for OverlayAccountDb<'a, T> { - fn get_storage(&self, account: &T::AccountId, trie_id: Option<&TrieId>, location: &StorageKey) -> Option> { + fn get_storage( + &self, + account: &T::AccountId, + trie_id: Option<&TrieId>, + location: &StorageKey + ) -> Option> { self.local .borrow() .get(account) diff --git a/substrate/srml/contract/src/exec.rs b/substrate/srml/contracts/src/exec.rs similarity index 100% rename from substrate/srml/contract/src/exec.rs rename to substrate/srml/contracts/src/exec.rs diff --git a/substrate/srml/contract/src/gas.rs b/substrate/srml/contracts/src/gas.rs similarity index 100% rename from substrate/srml/contract/src/gas.rs rename to substrate/srml/contracts/src/gas.rs diff --git a/substrate/srml/contract/src/lib.rs b/substrate/srml/contracts/src/lib.rs similarity index 100% rename from substrate/srml/contract/src/lib.rs rename to substrate/srml/contracts/src/lib.rs diff --git a/substrate/srml/contract/src/rent.rs b/substrate/srml/contracts/src/rent.rs similarity index 100% rename from substrate/srml/contract/src/rent.rs rename to substrate/srml/contracts/src/rent.rs diff --git a/substrate/srml/contract/src/tests.rs b/substrate/srml/contracts/src/tests.rs similarity index 100% rename from substrate/srml/contract/src/tests.rs rename to substrate/srml/contracts/src/tests.rs diff --git a/substrate/srml/contract/src/wasm/code_cache.rs b/substrate/srml/contracts/src/wasm/code_cache.rs similarity index 98% rename from substrate/srml/contract/src/wasm/code_cache.rs rename to substrate/srml/contracts/src/wasm/code_cache.rs index da92272c38..30c02ef942 100644 --- a/substrate/srml/contract/src/wasm/code_cache.rs +++ b/substrate/srml/contracts/src/wasm/code_cache.rs @@ -19,7 +19,8 @@ //! - 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. +//! - 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. diff --git a/substrate/srml/contract/src/wasm/env_def/macros.rs b/substrate/srml/contracts/src/wasm/env_def/macros.rs similarity index 100% rename from substrate/srml/contract/src/wasm/env_def/macros.rs rename to substrate/srml/contracts/src/wasm/env_def/macros.rs diff --git a/substrate/srml/contract/src/wasm/env_def/mod.rs b/substrate/srml/contracts/src/wasm/env_def/mod.rs similarity index 100% rename from substrate/srml/contract/src/wasm/env_def/mod.rs rename to substrate/srml/contracts/src/wasm/env_def/mod.rs diff --git a/substrate/srml/contract/src/wasm/mod.rs b/substrate/srml/contracts/src/wasm/mod.rs similarity index 99% rename from substrate/srml/contract/src/wasm/mod.rs rename to substrate/srml/contracts/src/wasm/mod.rs index d86678d383..7dcd16d5da 100644 --- a/substrate/srml/contract/src/wasm/mod.rs +++ b/substrate/srml/contracts/src/wasm/mod.rs @@ -419,7 +419,10 @@ mod tests { ;; Input data to pass to the contract being created. (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") + (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" + ) ) "#; @@ -569,7 +572,10 @@ mod tests { (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" + ) ) "#; diff --git a/substrate/srml/contract/src/wasm/prepare.rs b/substrate/srml/contracts/src/wasm/prepare.rs similarity index 100% rename from substrate/srml/contract/src/wasm/prepare.rs rename to substrate/srml/contracts/src/wasm/prepare.rs diff --git a/substrate/srml/contract/src/wasm/runtime.rs b/substrate/srml/contracts/src/wasm/runtime.rs similarity index 100% rename from substrate/srml/contract/src/wasm/runtime.rs rename to substrate/srml/contracts/src/wasm/runtime.rs