seal: Rework contracts API (#6573)

* Transition getter functions to not use scratch buffer

* Remove scratch buffer from ext_get_storage

* Remove scratch buffer from ext_call

* Remove scratch buffer from ext_instantiate

* Add ext_input and remove scratch buffer

* Rework error handling (changes RPC exposed data)

* ext_return passes a flags field instead of a return code
	* Flags is only for seal and not for the caller
	* flags: u32 replaced status_code: u8 in RPC exposed type
* API functions use a unified error type (ReturnCode)
* ext_transfer now traps on error to be consistent with call and instantiate

* Remove the no longer used `Dispatched` event

* Updated inline documentation

* Prevent skipping of copying the output for getter API

* Return gas_consumed from the RPC contracts call interface

* Updated COMPLEXTITY.md

* Rename ext_gas_price to ext_weight_to_fee

* Align comments with spaces

* Removed no longer used `ExecError`

* Remove possible panic in `from_typed_value`

* Use a struct as associated data for SpecialTrap::Return

* Fix nits in COMPLEXITY.md

* Renamed SpecialTrap to TrapReason

* Fix test

* Finish renaming special_trap -> trap_reason

* Remove no longer used get_runtime_storage

* fixup! Remove no longer used get_runtime_storage

* Removed tabs for comment aligment
This commit is contained in:
Alexander Theißen
2020-07-09 15:07:02 +02:00
committed by GitHub
parent a4427f3635
commit 25de5b5c78
26 changed files with 1116 additions and 1256 deletions
+17 -12
View File
@@ -93,7 +93,7 @@ use crate::exec::ExecutionContext;
use crate::wasm::{WasmLoader, WasmVm};
pub use crate::gas::{Gas, GasMeter};
pub use crate::exec::{ExecResult, ExecReturnValue, ExecError, StatusCode};
pub use crate::exec::{ExecResult, ExecReturnValue};
#[cfg(feature = "std")]
use serde::{Serialize, Deserialize};
@@ -410,7 +410,11 @@ decl_error! {
/// Tombstones don't match.
InvalidTombstone,
/// An origin TrieId written in the current block.
InvalidContractOrigin
InvalidContractOrigin,
/// The executed contract exhausted its gas limit.
OutOfGas,
/// The output buffer supplied to a contract API call was too small.
OutputBufferTooSmall,
}
}
@@ -515,7 +519,7 @@ decl_module! {
let result = Self::execute_wasm(origin, &mut gas_meter, |ctx, gas_meter| {
ctx.call(dest, value, gas_meter, data)
});
gas_meter.into_dispatch_result(result.map_err(|e| e.reason))
gas_meter.into_dispatch_result(result)
}
/// Instantiates a new contract from the `codehash` generated by `put_code`, optionally transferring some balance.
@@ -543,7 +547,7 @@ decl_module! {
ctx.instantiate(endowment, gas_meter, &code_hash, data)
.map(|(_address, output)| output)
});
gas_meter.into_dispatch_result(result.map_err(|e| e.reason))
gas_meter.into_dispatch_result(result)
}
/// Allows block producers to claim a small reward for evicting a contract. If a block producer
@@ -587,17 +591,22 @@ impl<T: Trait> Module<T> {
///
/// This function is similar to `Self::call`, but doesn't perform any address lookups and better
/// suitable for calling directly from Rust.
///
/// It returns the exection result and the amount of used weight.
pub fn bare_call(
origin: T::AccountId,
dest: T::AccountId,
value: BalanceOf<T>,
gas_limit: Gas,
input_data: Vec<u8>,
) -> ExecResult {
) -> (ExecResult, Gas) {
let mut gas_meter = GasMeter::new(gas_limit);
Self::execute_wasm(origin, &mut gas_meter, |ctx, gas_meter| {
ctx.call(dest, value, gas_meter, input_data)
})
(
Self::execute_wasm(origin, &mut gas_meter, |ctx, gas_meter| {
ctx.call(dest, value, gas_meter, input_data)
}),
gas_meter.gas_spent(),
)
}
/// Query storage of a specified contract under a specified key.
@@ -673,10 +682,6 @@ decl_event! {
/// Triggered when the current schedule is updated.
ScheduleUpdated(u32),
/// A call was dispatched from the given account. The bool signals whether it was
/// successful execution or not.
Dispatched(AccountId, bool),
/// An event deposited upon execution of a contract from the account.
ContractExecution(AccountId, Vec<u8>),
}