contracts: Refactor the runtime API in order to simplify node integration (#7409)

* contracts: Make use of existing type aliases for runtime API types

* contracts: Refactor the contracts call runtime API

* review: Fix comment typo

Co-authored-by: Andrew Jones <ascjones@gmail.com>

* Update frame/contracts/common/src/lib.rs

Co-authored-by: Nikolay Volf <nikvolf@gmail.com>

* Update frame/contracts/common/src/lib.rs

Co-authored-by: Nikolay Volf <nikvolf@gmail.com>

* Update frame/contracts/common/src/lib.rs

Co-authored-by: Nikolay Volf <nikvolf@gmail.com>

* Update frame/contracts/common/src/lib.rs

Co-authored-by: Nikolay Volf <nikvolf@gmail.com>

* Update frame/contracts/common/src/lib.rs

Co-authored-by: Nikolay Volf <nikvolf@gmail.com>

* Update lib.rs

* review: Group crate imports

Co-authored-by: Andrew Jones <ascjones@gmail.com>
Co-authored-by: Addie Wagenknecht <addie@nortd.com>
Co-authored-by: Nikolay Volf <nikvolf@gmail.com>
This commit is contained in:
Alexander Theißen
2020-10-29 15:57:56 +01:00
committed by GitHub
parent bd450c24ff
commit a5ec7e5c4e
14 changed files with 143 additions and 161 deletions
@@ -23,31 +23,9 @@
#![cfg_attr(not(feature = "std"), no_std)]
use codec::{Codec, Decode, Encode};
use pallet_contracts_primitives::{GetStorageResult, RentProjectionResult};
use sp_runtime::RuntimeDebug;
use codec::Codec;
use sp_std::vec::Vec;
/// A result of execution of a contract.
#[derive(Eq, PartialEq, Encode, Decode, RuntimeDebug)]
pub enum ContractExecResult {
/// The contract returned successfully.
///
/// There is a status code and, optionally, some data returned by the contract.
Success {
/// Flags that the contract passed along on returning to alter its exit behaviour.
/// Described in `pallet_contracts::exec::ReturnFlags`.
flags: u32,
/// Output data returned by the contract.
///
/// Can be empty.
data: Vec<u8>,
/// How much gas was consumed by the call.
gas_consumed: u64,
},
/// The contract execution either trapped or returned an error.
Error,
}
use pallet_contracts_primitives::{ContractExecResult, GetStorageResult, RentProjectionResult};
sp_api::decl_runtime_apis! {
/// The API to interact with contracts without using executive.
+11 -17
View File
@@ -33,11 +33,9 @@ use sp_runtime::{
traits::{Block as BlockT, Header as HeaderT},
};
use std::convert::TryInto;
use pallet_contracts_primitives::ContractExecResult;
pub use self::gen_client::Client as ContractsClient;
pub use pallet_contracts_rpc_runtime_api::{
self as runtime_api, ContractExecResult, ContractsApi as ContractsRuntimeApi,
};
pub use pallet_contracts_rpc_runtime_api::ContractsApi as ContractsRuntimeApi;
const RUNTIME_ERROR: i64 = 1;
const CONTRACT_DOESNT_EXIST: i64 = 2;
@@ -105,17 +103,13 @@ pub enum RpcContractExecResult {
impl From<ContractExecResult> for RpcContractExecResult {
fn from(r: ContractExecResult) -> Self {
match r {
ContractExecResult::Success {
flags,
data,
gas_consumed
} => RpcContractExecResult::Success {
flags,
data: data.into(),
gas_consumed,
match r.exec_result {
Ok(val) => RpcContractExecResult::Success {
flags: val.flags.bits(),
data: val.data.into(),
gas_consumed: r.gas_consumed,
},
ContractExecResult::Error => RpcContractExecResult::Error(()),
_ => RpcContractExecResult::Error(()),
}
}
}
@@ -233,7 +227,7 @@ where
let exec_result = api
.call(&at, origin, dest, value, gas_limit, input_data.to_vec())
.map_err(|e| runtime_error_into_rpc_err(e))?;
.map_err(runtime_error_into_rpc_err)?;
Ok(exec_result.into())
}
@@ -251,7 +245,7 @@ where
let result = api
.get_storage(&at, address, key.into())
.map_err(|e| runtime_error_into_rpc_err(e))?
.map_err(runtime_error_into_rpc_err)?
.map_err(ContractAccessError)?
.map(Bytes);
@@ -270,7 +264,7 @@ where
let result = api
.rent_projection(&at, address)
.map_err(|e| runtime_error_into_rpc_err(e))?
.map_err(runtime_error_into_rpc_err)?
.map_err(ContractAccessError)?;
Ok(match result {