Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ext_dispatch_call #124

Merged
merged 72 commits into from Jul 30, 2019
Merged

Implement ext_dispatch_call #124

merged 72 commits into from Jul 30, 2019

Conversation

ascjones
Copy link
Collaborator

@ascjones ascjones commented Jun 18, 2019

Resolves #42.

Here is an example of dispatching a Balances::transfer from a contract:

pub(external) fn balance_transfer(&mut self, dest: u32, value: Balance) {
    let transfer_call = calls::Balances::<DefaultSrmlTypes>::transfer(dest, value);
    env.dispatch_call(transfer_call);
}

The pre-baked Call types are "copies" of the srml module Call enums e.g. Balances. Importing the actual types from substrate is possible but it results in a very large wasm blob size. To ensure compatibility with the equivalent substrate types, tests are required for each Call which encode the local type and decode as the substrate type.

Note: This PR only includes a single predefined Call type for Balances. I think we should consider which additional calls/modules we should add in a follow up PR.

The final piece is the definition of the top level Call enum to compose the modules to match the substrate runtime. This can be user defined for custom runtimes by defining the Call type in EnvTypes e.g.

#[derive(Encode, Decode)]
#[cfg_attr(feature = "test-env", derive(Debug, Clone, PartialEq, Eq))]
pub enum Call {
    #[codec(index = "5")]
    Balances(super::calls::Balances<DefaultSrmlTypes>),
}

Because of how parity-scale-codec encodes enums, chain authors can define only the srml module calls they want to support, so long as the index matches up with the the relevant Call variant in the target chain's substrate runtime declaration.

@ascjones ascjones added the E-in-progress A task that is already being worked on. label Jun 18, 2019
lang/src/gen/build.rs Outdated Show resolved Hide resolved
@ascjones ascjones added B-enhancement New feature or request and removed E-in-progress A task that is already being worked on. labels Jul 12, 2019
@ascjones ascjones marked this pull request as ready for review July 12, 2019 16:59
cli/template/Cargo.toml Outdated Show resolved Hide resolved
core/src/env/api.rs Outdated Show resolved Hide resolved
core/src/env/srml/types.rs Show resolved Hide resolved
core/src/env/srml/types.rs Outdated Show resolved Hide resolved
impl Decode for Call {
fn decode<I: Input>(_value: &mut I) -> Option<Self> {
impl parity_codec::Decode for Call {
fn decode<I: parity_codec::Input>(_value: &mut I) -> Option<Self> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Then better put it behind test-env feature instead of std feature and also provide a rational (comment) why it is there.

core/src/env/traits.rs Outdated Show resolved Hide resolved
core/src/env/traits.rs Outdated Show resolved Hide resolved
name = "calls"
crate-type = ["cdylib"]

[features]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Still needs adjustment

@@ -0,0 +1 @@
nightly-2019-05-21
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please delete rust-toolchain file.

model/src/exec_env.rs Show resolved Hide resolved
core/src/env/srml/types.rs Show resolved Hide resolved
core/src/env/srml/types.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@Robbepop Robbepop left a comment

Choose a reason for hiding this comment

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

LGTM 🌠

@Robbepop Robbepop merged commit 49f221b into master Jul 30, 2019
Robbepop pushed a commit that referenced this pull request Jul 31, 2019
* [core] Add AccountId to EnvTypes

* [core] Add calls mod, plus WIP tests

* [core] Balance transfer call roundtrip works

* [core] Add ext_dispatch_call

* [core] Implement ext_dispatch_call

* [examples] Add example lang contract for ext_dispatch_call

* [examples] WIP: implement example lang contract for ext_dispatch_call

* [examples] complete basic implementation of dispatching Balances Call

* srml-contracts -> srml-contract

* [core] remove unsafe from dispatch_call api fn

* [core] Add docs to dispatch_call

* [core] Add missing trait bound to account index

* [core] Some todos

* [core] Rename dispatch_call to dispatch_raw_call on Env trait

* [lang] Add AccountIndex type alias

* [model] add strongly type dispatch call to EnvHandler

* [*] specify latest parity-codec version, same as substrate

* [core] fix call roundtrip tests

* [lang] fix tests

* [core] fix wasm build

* [core] fix std build

* [core] move call type serialization tests

* [core] test Call serialization roundtrip

* [core] use node_runtime for Call serialization roundtrip

* [core] remove unused dependencies

* [core] remove unused substrate dependencies

* [core] change default Balance to u128, matching substrate

* [core] introduce Address type for balance calls

* [core] fix Balances transfer serialization test

* [core] add Address serialization tests

* [ci] install wasm-gc for building substrate dev deps

* [ci] move wasm-gc installation after wasm target added

* [examples] get the calls example compiling

* [CI] add temporary check for debugging ci build

* [CI] restore wasm-gc to install section and remove temp version check

* [CI] temporarily depend on substrate branch to test build fix

* [CI] remove temp substrate branch, build issue fixed

* [core] use std feature instead of test-env for EnvTypes

* [core] remove Call types, moved to ink-types-node-runtime

* [core] remove AccountIndex from EnvTypes

* [core] remove AccountIndex from lang codegen

* [core] fix unused for std

* [CI] remove wasm-gc from travis build

* [CI] remove AccountIndex type alias from codegen

* parity-codec version 4.1.1 -> 4.1

* [core] ext_dispatch_call docs

* [core] describe in comment what will happen if decoding fails

* [model] implement suggestion of Into<Call>

* rustfmt

* rustfmt again

* rustfmt again again

* [examples] deleted calls example - moved to ink_types_node_runtime

* [core] use into in api::dispatch_call

* [core] make Call empty enum and use test-env feature for EnvTypes

* [core] remove unused Call enum

* [core] add comments explaining rationale for test-env

* [core] missing period

* [core] add comments

* [core] add space in comment

* [core] add comment to Call decode impl

* [core] make dispatched_calls a DoubleEndedIterator

* [core] add missing doc comment

* [core] add comment explaining Decode requirement

* [core] doc comment and DoubleEndedIterator
@ascjones ascjones deleted the aj-dispatch-call branch November 25, 2019 11:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
B-enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement infrastructure for smart contract to call back into runtime
3 participants