contracts: is_contract(address) and caller_is_origin() are added to API (#10789)

* is_contract() and caller_is_origin() added to Ext API

* is_contract() exposed in wasm runtime.rs

* + test for is_contract()

* + seal_is_contract benchmark

* caller_is_origin() exposed to wasm/runtime.rs and covered by a test

* + seal_caller_is_origin benchmark

* Update frame/contracts/src/exec.rs

Co-authored-by: Alexander Theißen <alex.theissen@me.com>

* Update frame/contracts/src/exec.rs

Co-authored-by: Alexander Theißen <alex.theissen@me.com>

* Update frame/contracts/src/exec.rs

Co-authored-by: Alexander Theißen <alex.theissen@me.com>

* Update frame/contracts/src/wasm/runtime.rs

Co-authored-by: Alexander Theißen <alex.theissen@me.com>

* Update frame/contracts/src/wasm/runtime.rs

Co-authored-by: Alexander Theißen <alex.theissen@me.com>

* Update frame/contracts/src/wasm/runtime.rs

Co-authored-by: Alexander Theißen <alex.theissen@me.com>

* Update frame/contracts/src/exec.rs

Co-authored-by: Alexander Theißen <alex.theissen@me.com>

* identation fix for benchmark macroses; test cosmetic improvement

* benchmark fix

* + is_contract() wasm test

* + caller_is_origin() wasm test

* Apply suggestions from code review

Co-authored-by: Alexander Theißen <alex.theissen@me.com>

* is_contract() to borrow param instead of taking ownership

* phrasing improved

Co-authored-by: Hernando Castano <HCastano@users.noreply.github.com>

* fixed wasm tests according to @athei feedback

* dead code warnings suppressed by unstable-interface attributes

* cargo run --quiet --profile=production  --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_contracts --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/contracts/src/weights.rs --template=./.maintain/frame-weight-template.hbs

Co-authored-by: Alexander Theißen <alex.theissen@me.com>
Co-authored-by: Hernando Castano <HCastano@users.noreply.github.com>
Co-authored-by: Parity Bot <admin@parity.io>
This commit is contained in:
Alexander Gryaznov
2022-02-04 20:13:00 +01:00
committed by GitHub
parent 89fa61125e
commit d5c8593566
6 changed files with 965 additions and 652 deletions
@@ -138,6 +138,12 @@ pub enum RuntimeCosts {
MeteringBlock(u32),
/// Weight of calling `seal_caller`.
Caller,
/// Weight of calling `seal_is_contract`.
#[cfg(feature = "unstable-interface")]
IsContract,
/// Weight of calling `seal_caller_is_origin`.
#[cfg(feature = "unstable-interface")]
CallerIsOrigin,
/// Weight of calling `seal_address`.
Address,
/// Weight of calling `seal_gas_left`.
@@ -225,6 +231,10 @@ impl RuntimeCosts {
let weight = match *self {
MeteringBlock(amount) => s.gas.saturating_add(amount.into()),
Caller => s.caller,
#[cfg(feature = "unstable-interface")]
IsContract => s.is_contract,
#[cfg(feature = "unstable-interface")]
CallerIsOrigin => s.caller_is_origin,
Address => s.address,
GasLeft => s.gas_left,
Balance => s.balance,
@@ -1254,6 +1264,37 @@ define_env!(Env, <E: Ext>,
)?)
},
// Checks whether a specified address belongs to a contract.
//
// # Parameters
//
// - account_ptr: a pointer to the address of the beneficiary account
// Should be decodable as an `T::AccountId`. Traps otherwise.
//
// Returned value is a u32-encoded boolean: (0 = false, 1 = true).
[__unstable__] seal_is_contract(ctx, account_ptr: u32) -> u32 => {
ctx.charge_gas(RuntimeCosts::IsContract)?;
let address: <<E as Ext>::T as frame_system::Config>::AccountId =
ctx.read_sandbox_memory_as(account_ptr)?;
Ok(ctx.ext.is_contract(&address) as u32)
},
// Checks whether the caller of the current contract is the origin of the whole call stack.
//
// Prefer this over `seal_is_contract` when checking whether your contract is being called by a contract
// or a plain account. The reason is that it performs better since it does not need to
// do any storage lookups.
//
// A return value of`true` indicates that this contract is being called by a plain account
// and `false` indicates that the caller is another contract.
//
// Returned value is a u32-encoded boolean: (0 = false, 1 = true).
[__unstable__] seal_caller_is_origin(ctx) -> u32 => {
ctx.charge_gas(RuntimeCosts::CallerIsOrigin)?;
Ok(ctx.ext.caller_is_origin() as u32)
},
// Stores the address of the current contract into the supplied buffer.
//
// The value is stored to linear memory at the address pointed to by `out_ptr`.