contracts: Add RPC that allows instantiating of a contract (#8451)

* contracts: Add RPC that allows instantiating of a contract

* Encode `debug_message` as bytes because usage of `String` is forbidden

* Remove erroneous derive attribute

* Fix rpc tests for new `debug_message` encoding

* Fix typo

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

Co-authored-by: Andrew Jones <ascjones@gmail.com>
This commit is contained in:
Alexander Theißen
2021-04-13 13:26:52 +02:00
committed by GitHub
parent 24311eee3e
commit f854194139
16 changed files with 471 additions and 191 deletions
+180 -85
View File
@@ -27,14 +27,13 @@ use serde::{Deserialize, Serialize};
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_core::{Bytes, H256};
use sp_rpc::number;
use sp_rpc::number::NumberOrHex;
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, Header as HeaderT},
DispatchError,
};
use std::convert::{TryFrom, TryInto};
use pallet_contracts_primitives::ContractExecResult;
use pallet_contracts_primitives::{Code, ContractExecResult, ContractInstantiateResult};
pub use pallet_contracts_rpc_runtime_api::ContractsApi as ContractsRuntimeApi;
@@ -42,6 +41,8 @@ const RUNTIME_ERROR: i64 = 1;
const CONTRACT_DOESNT_EXIST: i64 = 2;
const CONTRACT_IS_A_TOMBSTONE: i64 = 3;
pub type Weight = u64;
/// A rough estimate of how much gas a decent hardware consumes per second,
/// using native execution.
/// This value is used to set the upper bound for maximal contract calls to
@@ -50,7 +51,11 @@ const CONTRACT_IS_A_TOMBSTONE: i64 = 3;
/// As 1 gas is equal to 1 weight we base this on the conducted benchmarks which
/// determined runtime weights:
/// https://github.com/paritytech/substrate/pull/5446
const GAS_PER_SECOND: u64 = 1_000_000_000_000;
const GAS_PER_SECOND: Weight = 1_000_000_000_000;
/// The maximum amount of weight that the call and instantiate rpcs are allowed to consume.
/// This puts a ceiling on the weight limit that is supplied to the rpc as an argument.
const GAS_LIMIT: Weight = 5 * GAS_PER_SECOND;
/// A private newtype for converting `ContractAccessError` into an RPC error.
struct ContractAccessError(pallet_contracts_primitives::ContractAccessError);
@@ -79,59 +84,27 @@ impl From<ContractAccessError> for Error {
pub struct CallRequest<AccountId> {
origin: AccountId,
dest: AccountId,
value: number::NumberOrHex,
gas_limit: number::NumberOrHex,
value: NumberOrHex,
gas_limit: NumberOrHex,
input_data: Bytes,
}
/// A struct that encodes RPC parameters required to instantiate a new smart-contract.
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[serde(rename_all = "camelCase")]
struct RpcContractExecSuccess {
/// The return flags. See `pallet_contracts_primitives::ReturnFlags`.
flags: u32,
/// Data as returned by the contract.
#[serde(deny_unknown_fields)]
pub struct InstantiateRequest<AccountId, Hash> {
origin: AccountId,
endowment: NumberOrHex,
gas_limit: NumberOrHex,
code: Code<Hash>,
data: Bytes,
}
/// An RPC serializable result of contract execution
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[serde(rename_all = "camelCase")]
pub struct RpcContractExecResult {
/// How much gas was consumed by the call. In case of an error this is the amount
/// that was used up until the error occurred.
gas_consumed: u64,
/// Additional dynamic human readable error information for debugging. An empty string
/// indicates that no additional information is available.
debug_message: String,
/// Indicates whether the contract execution was successful or not.
result: std::result::Result<RpcContractExecSuccess, DispatchError>,
}
impl From<ContractExecResult> for RpcContractExecResult {
fn from(r: ContractExecResult) -> Self {
match r.exec_result {
Ok(val) => RpcContractExecResult {
gas_consumed: r.gas_consumed,
debug_message: String::new(),
result: Ok(RpcContractExecSuccess {
flags: val.flags.bits(),
data: val.data.into(),
}),
},
Err(err) => RpcContractExecResult {
gas_consumed: r.gas_consumed,
debug_message: String::new(),
result: Err(err.error),
},
}
}
salt: Bytes,
}
/// Contracts RPC methods.
#[rpc]
pub trait ContractsApi<BlockHash, BlockNumber, AccountId, Balance> {
pub trait ContractsApi<BlockHash, BlockNumber, AccountId, Balance, Hash> {
/// Executes a call to a contract.
///
/// This call is performed locally without submitting any transactions. Thus executing this
@@ -143,7 +116,20 @@ pub trait ContractsApi<BlockHash, BlockNumber, AccountId, Balance> {
&self,
call_request: CallRequest<AccountId>,
at: Option<BlockHash>,
) -> Result<RpcContractExecResult>;
) -> Result<ContractExecResult>;
/// Instantiate a new contract.
///
/// This call is performed locally without submitting any transactions. Thus the contract
/// is not actually created.
///
/// This method is useful for UIs to dry-run contract instantiations.
#[rpc(name = "contracts_instantiate")]
fn instantiate(
&self,
instantiate_request: InstantiateRequest<AccountId, Hash>,
at: Option<BlockHash>,
) -> Result<ContractInstantiateResult<AccountId, BlockNumber>>;
/// Returns the value under a specified storage `key` in a contract given by `address` param,
/// or `None` if it is not set.
@@ -184,12 +170,13 @@ impl<C, B> Contracts<C, B> {
}
}
}
impl<C, Block, AccountId, Balance>
impl<C, Block, AccountId, Balance, Hash>
ContractsApi<
<Block as BlockT>::Hash,
<<Block as BlockT>::Header as HeaderT>::Number,
AccountId,
Balance,
Hash,
> for Contracts<C, Block>
where
Block: BlockT,
@@ -199,15 +186,17 @@ where
AccountId,
Balance,
<<Block as BlockT>::Header as HeaderT>::Number,
Hash,
>,
AccountId: Codec,
Balance: Codec + TryFrom<number::NumberOrHex>,
Balance: Codec + TryFrom<NumberOrHex>,
Hash: Codec,
{
fn call(
&self,
call_request: CallRequest<AccountId>,
at: Option<<Block as BlockT>::Hash>,
) -> Result<RpcContractExecResult> {
) -> Result<ContractExecResult> {
let api = self.client.runtime_api();
let at = BlockId::hash(at.unwrap_or_else(||
// If the block hash is not supplied assume the best block.
@@ -221,37 +210,45 @@ where
input_data,
} = call_request;
// Make sure that value fits into the balance type.
let value: Balance = value.try_into().map_err(|_| Error {
code: ErrorCode::InvalidParams,
message: format!("{:?} doesn't fit into the balance type", value),
data: None,
})?;
// Make sure that gas_limit fits into 64 bits.
let gas_limit: u64 = gas_limit.try_into().map_err(|_| Error {
code: ErrorCode::InvalidParams,
message: format!("{:?} doesn't fit in 64 bit unsigned value", gas_limit),
data: None,
})?;
let max_gas_limit = 5 * GAS_PER_SECOND;
if gas_limit > max_gas_limit {
return Err(Error {
code: ErrorCode::InvalidParams,
message: format!(
"Requested gas limit is greater than maximum allowed: {} > {}",
gas_limit, max_gas_limit
),
data: None,
});
}
let value: Balance = decode_hex(value, "balance")?;
let gas_limit: Weight = decode_hex(gas_limit, "weight")?;
limit_gas(gas_limit)?;
let exec_result = api
.call(&at, origin, dest, value, gas_limit, input_data.to_vec())
.map_err(runtime_error_into_rpc_err)?;
Ok(exec_result.into())
Ok(exec_result)
}
fn instantiate(
&self,
instantiate_request: InstantiateRequest<AccountId, Hash>,
at: Option<<Block as BlockT>::Hash>,
) -> Result<ContractInstantiateResult<AccountId, <<Block as BlockT>::Header as HeaderT>::Number>> {
let api = self.client.runtime_api();
let at = BlockId::hash(at.unwrap_or_else(||
// If the block hash is not supplied assume the best block.
self.client.info().best_hash));
let InstantiateRequest {
origin,
endowment,
gas_limit,
code,
data,
salt,
} = instantiate_request;
let endowment: Balance = decode_hex(endowment, "balance")?;
let gas_limit: Weight = decode_hex(gas_limit, "weight")?;
limit_gas(gas_limit)?;
let exec_result = api
.instantiate(&at, origin, endowment, gas_limit, code, data.to_vec(), salt.to_vec())
.map_err(runtime_error_into_rpc_err)?;
Ok(exec_result)
}
fn get_storage(
@@ -300,16 +297,43 @@ where
fn runtime_error_into_rpc_err(err: impl std::fmt::Debug) -> Error {
Error {
code: ErrorCode::ServerError(RUNTIME_ERROR),
message: "Runtime trapped".into(),
message: "Runtime error".into(),
data: Some(format!("{:?}", err).into()),
}
}
fn decode_hex<H: std::fmt::Debug + Copy, T: TryFrom<H>>(from: H, name: &str) -> Result<T> {
from.try_into().map_err(|_| Error {
code: ErrorCode::InvalidParams,
message: format!("{:?} does not fit into the {} type", from, name),
data: None,
})
}
fn limit_gas(gas_limit: Weight) -> Result<()> {
if gas_limit > GAS_LIMIT {
Err(Error {
code: ErrorCode::InvalidParams,
message: format!(
"Requested gas limit is greater than maximum allowed: {} > {}",
gas_limit, GAS_LIMIT
),
data: None,
})
} else {
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use sp_core::U256;
fn trim(json: &str) -> String {
json.chars().filter(|c| !c.is_whitespace()).collect()
}
#[test]
fn call_request_should_serialize_deserialize_properly() {
type Req = CallRequest<String>;
@@ -327,13 +351,84 @@ mod tests {
}
#[test]
fn result_should_serialize_deserialize_properly() {
fn test(expected: &str) {
let res: RpcContractExecResult = serde_json::from_str(expected).unwrap();
let actual = serde_json::to_string(&res).unwrap();
assert_eq!(actual, expected);
fn instantiate_request_should_serialize_deserialize_properly() {
type Req = InstantiateRequest<String, String>;
let req: Req = serde_json::from_str(r#"
{
"origin": "5CiPPseXPECbkjWCa6MnjNokrgYjMqmKndv2rSnekmSK2DjL",
"endowment": "0x88",
"gasLimit": 42,
"code": { "existing": "0x1122" },
"data": "0x4299",
"salt": "0x9988"
}
test(r#"{"gasConsumed":5000,"debugMessage":"helpOk","result":{"Ok":{"flags":5,"data":"0x1234"}}}"#);
test(r#"{"gasConsumed":3400,"debugMessage":"helpErr","result":{"Err":"BadOrigin"}}"#);
"#).unwrap();
assert_eq!(req.origin, "5CiPPseXPECbkjWCa6MnjNokrgYjMqmKndv2rSnekmSK2DjL");
assert_eq!(req.endowment.into_u256(), 0x88.into());
assert_eq!(req.gas_limit.into_u256(), 42.into());
assert_eq!(&*req.data, [0x42, 0x99].as_ref());
assert_eq!(&*req.salt, [0x99, 0x88].as_ref());
let code = match req.code {
Code::Existing(hash) => hash,
_ => panic!("json encoded an existing hash"),
};
assert_eq!(&code, "0x1122");
}
#[test]
fn call_result_should_serialize_deserialize_properly() {
fn test(expected: &str) {
let res: ContractExecResult = serde_json::from_str(expected).unwrap();
let actual = serde_json::to_string(&res).unwrap();
assert_eq!(actual, trim(expected).as_str());
}
test(r#"{
"gasConsumed": 5000,
"debugMessage": "0x68656c704f6b",
"result": {
"Ok": {
"flags": 5,
"data": "0x1234"
}
}
}"#);
test(r#"{
"gasConsumed": 3400,
"debugMessage": "0x68656c70457272",
"result": {
"Err": "BadOrigin"
}
}"#);
}
#[test]
fn instantiate_result_should_serialize_deserialize_properly() {
fn test(expected: &str) {
let res: ContractInstantiateResult<String, u64> = serde_json::from_str(expected).unwrap();
let actual = serde_json::to_string(&res).unwrap();
assert_eq!(actual, trim(expected).as_str());
}
test(r#"{
"gasConsumed": 5000,
"debugMessage": "0x68656c704f6b",
"result": {
"Ok": {
"result": {
"flags": 5,
"data": "0x1234"
},
"accountId": "5CiPP",
"rentProjection": null
}
}
}"#);
test(r#"{
"gasConsumed": 3400,
"debugMessage": "0x68656c70457272",
"result": {
"Err": "BadOrigin"
}
}"#);
}
}