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

Integrate contracts into substrate-demo runtime #675

Merged
merged 6 commits into from
Sep 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions demo/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ substrate-primitives = { path = "../../substrate/primitives" }
substrate-keyring = { path = "../../substrate/keyring" }
substrate-runtime-balances = { path = "../../substrate/runtime/balances" }
substrate-runtime-consensus = { path = "../../substrate/runtime/consensus" }
substrate-runtime-contract = { path = "../../substrate/runtime/contract" }
substrate-runtime-council = { path = "../../substrate/runtime/council" }
substrate-runtime-democracy = { path = "../../substrate/runtime/democracy" }
substrate-runtime-executive = { path = "../../substrate/runtime/executive" }
Expand All @@ -41,6 +42,7 @@ std = [
"substrate-runtime-support/std",
"substrate-runtime-balances/std",
"substrate-runtime-consensus/std",
"substrate-runtime-contract/std",
"substrate-runtime-council/std",
"substrate-runtime-democracy/std",
"substrate-runtime-executive/std",
Expand Down
31 changes: 30 additions & 1 deletion demo/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ extern crate substrate_codec_derive;
extern crate substrate_runtime_std as rstd;
extern crate substrate_runtime_balances as balances;
extern crate substrate_runtime_consensus as consensus;
extern crate substrate_runtime_contract as contract;
extern crate substrate_runtime_council as council;
extern crate substrate_runtime_democracy as democracy;
extern crate substrate_runtime_executive as executive;
Expand Down Expand Up @@ -196,6 +197,33 @@ impl treasury::Trait for Runtime {
/// Treasury module for this concrete runtime.
pub type Treasury = treasury::Module<Runtime>;

/// Address calculated from the code (of the constructor), input data to the constructor
/// and account id which requested the account creation.
///
/// Formula: `blake2_256(blake2_256(code) + blake2_256(data) + origin)`
pub struct DetermineContractAddress;
impl contract::ContractAddressFor<AccountId> for DetermineContractAddress {
fn contract_address_for(code: &[u8], data: &[u8], origin: &AccountId) -> AccountId {
use runtime_primitives::traits::Hash;

let code_hash = BlakeTwo256::hash(code);
let data_hash = BlakeTwo256::hash(data);
let mut buf = [0u8, 32 + 32 + 32];
&mut buf[0..32].copy_from_slice(&code_hash);
&mut buf[32..64].copy_from_slice(&data_hash);
&mut buf[64..96].copy_from_slice(origin);
AccountId::from(BlakeTwo256::hash(&buf[..]))
}
}

impl contract::Trait for Runtime {
type Gas = u64;
type DetermineContractAddress = DetermineContractAddress;
}

/// Contract module for this concrete runtime.
pub type Contract = contract::Module<Runtime>;

impl_outer_event! {
pub enum Event for Runtime {
balances, session, staking, democracy, treasury, council_motions
Expand Down Expand Up @@ -226,6 +254,7 @@ impl_outer_dispatch! {
CouncilVoting,
CouncilMotions,
Treasury,
Contract,
}
}

Expand Down Expand Up @@ -269,7 +298,7 @@ pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<Address, Index, Call,
pub type CheckedExtrinsic = generic::CheckedExtrinsic<AccountId, Index, Call>;
/// Executive: handles dispatch to the various modules.
pub type Executive = executive::Executive<Runtime, Block, Balances, Balances,
(((((((), Treasury), Council), Democracy), Staking), Session), Timestamp)>;
((((((((), Treasury), Council), Democracy), Staking), Session), Timestamp), Contract)>;

impl_json_metadata!(
for Runtime with modules
Expand Down
31 changes: 31 additions & 0 deletions demo/runtime/wasm/Cargo.lock

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

2 changes: 2 additions & 0 deletions demo/runtime/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ substrate-runtime-io = { path = "../../../substrate/runtime-io", default-feature
substrate-runtime-support = { path = "../../../substrate/runtime-support", default-features = false }
substrate-runtime-balances = { path = "../../../substrate/runtime/balances", default-features = false }
substrate-runtime-consensus = { path = "../../../substrate/runtime/consensus", default-features = false }
substrate-runtime-contract = { path = "../../../substrate/runtime/contract", default-features = false }
substrate-runtime-council = { path = "../../../substrate/runtime/council", default-features = false }
substrate-runtime-democracy = { path = "../../../substrate/runtime/democracy", default-features = false }
substrate-runtime-executive = { path = "../../../substrate/runtime/executive", default-features = false }
Expand All @@ -40,6 +41,7 @@ std = [
"substrate-runtime-support/std",
"substrate-runtime-balances/std",
"substrate-runtime-consensus/std",
"substrate-runtime-contract/std",
"substrate-runtime-council/std",
"substrate-runtime-democracy/std",
"substrate-runtime-executive/std",
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions substrate/runtime-std/with_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub use std::ops;
pub use std::ptr;
pub use std::rc;
pub use std::slice;
pub use std::string;
pub use std::vec;
pub use std::result;

Expand Down
1 change: 1 addition & 0 deletions substrate/runtime-std/without_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extern crate pwasm_alloc;
pub use alloc::boxed;
pub use alloc::rc;
pub use alloc::vec;
pub use alloc::string;
pub use core::borrow;
pub use core::cell;
pub use core::clone;
Expand Down
2 changes: 1 addition & 1 deletion substrate/runtime/contract/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl<'a, T: Trait> ExecutionContext<'a, T> {
return Err("not enough gas to pay base create fee");
}

let dest = T::DetermineContractAddress::contract_address_for(ctor, &self.self_account);
let dest = T::DetermineContractAddress::contract_address_for(ctor, data, &self.self_account);
if <CodeOf<T>>::exists(&dest) {
// TODO: Is it enough?
return Err("contract already exists");
Expand Down
2 changes: 2 additions & 0 deletions substrate/runtime/contract/src/genesis_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

//! Build the contract module part of the genesis block storage.

#![cfg(feature = "std")]

use {Trait, ContractFee, CallBaseFee, CreateBaseFee, GasPrice, MaxDepth, BlockGasLimit};

use runtime_primitives;
Expand Down
7 changes: 5 additions & 2 deletions substrate/runtime/contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ extern crate substrate_codec as codec;
extern crate substrate_runtime_io as runtime_io;
extern crate substrate_runtime_sandbox as sandbox;

#[cfg_attr(feature = "std", macro_use)]
#[macro_use]
extern crate substrate_runtime_std as rstd;

extern crate substrate_runtime_balances as balances;
Expand All @@ -90,16 +90,19 @@ mod double_map;
mod exec;
mod vm;
mod gas;

mod genesis_config;

#[cfg(test)]
mod tests;

#[cfg(feature = "std")]
pub use genesis_config::GenesisConfig;
use exec::ExecutionContext;
use account_db::{AccountDb, OverlayAccountDb};
use double_map::StorageDoubleMap;

use rstd::prelude::*;
use codec::Codec;
use runtime_primitives::traits::{As, SimpleArithmetic, OnFinalise};
use runtime_support::dispatch::Result;
Expand All @@ -115,7 +118,7 @@ pub trait Trait: balances::Trait {
}

pub trait ContractAddressFor<AccountId: Sized> {
fn contract_address_for(code: &[u8], origin: &AccountId) -> AccountId;
fn contract_address_for(code: &[u8], data: &[u8], origin: &AccountId) -> AccountId;
}

decl_module! {
Expand Down
4 changes: 3 additions & 1 deletion substrate/runtime/contract/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type Contract = Module<Test>;

pub struct DummyContractAddressFor;
impl ContractAddressFor<u64> for DummyContractAddressFor {
fn contract_address_for(_code: &[u8], origin: &u64) -> u64 {
fn contract_address_for(_code: &[u8], _data: &[u8], origin: &u64) -> u64 {
origin + 1
}
}
Expand Down Expand Up @@ -357,6 +357,7 @@ fn contract_create() {

let derived_address = <Test as Trait>::DetermineContractAddress::contract_address_for(
&code_ctor_transfer,
&[],
&1,
);

Expand Down Expand Up @@ -395,6 +396,7 @@ fn top_level_create() {
with_externalities(&mut ExtBuilder::default().gas_price(3).build(), || {
let derived_address = <Test as Trait>::DetermineContractAddress::contract_address_for(
&code_ctor_transfer,
&[],
&0,
);

Expand Down
2 changes: 1 addition & 1 deletion substrate/runtime/contract/src/vm/env_def/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ macro_rules! define_env {

$(
env.funcs.insert(
stringify!( $name ).to_string(),
stringify!( $name ).into(),
HostFunction::new(
gen_signature!( ( $( $params ),* ) $( -> $returns )* ),
{
Expand Down
2 changes: 2 additions & 0 deletions substrate/runtime/contract/src/vm/env_def/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use super::{BalanceOf, CallReceipt, CreateReceipt, Ext, GasMeterResult, Runtime};
use codec::Decode;
use parity_wasm::elements::{FunctionType, ValueType};
use rstd::prelude::*;
use rstd::string::String;
use rstd::collections::btree_map::BTreeMap;
use runtime_primitives::traits::As;
use sandbox::{self, TypedValue};
Expand Down
1 change: 1 addition & 0 deletions substrate/runtime/contract/src/vm/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use super::env_def::HostFunctionSet;
use super::{Config, Error, Ext};
use rstd::prelude::*;
use parity_wasm::elements::{self, External, MemoryType, Type};
use pwasm_utils;
use pwasm_utils::rules;
Expand Down
Binary file not shown.
Binary file not shown.