contracts: add seal_code_hash and seal_own_code_hash to API (#10933)

* `seal_origin` + tests added

* `seal_origin` benchmark added

* `seal_code_hash` + tests added

* `seal_code_hash` benchmark added

* `seal_own_code_hash` + tests added

* `seal_own_code_hash` benchmark added

* fmt lil fix

* akward accident bug fix

* Apply suggestions from code review

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

* Apply suggestions from code review

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

* benchmark fix

* `WasmModule::getter()` to take `module_name` arg

* test enhanced

* fixes based on review feedback

* Apply suggestions from code review

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

* Hash left as const to return a ref to it from mock

* HASH test val to local const in mock

* Apply suggestions from code review

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

* fixes to benchmarks according to review feedback

* 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

* removed `seal_origin` from API

Co-authored-by: Alexander Theißen <alex.theissen@me.com>
Co-authored-by: Parity Bot <admin@parity.io>
This commit is contained in:
Alexander Gryaznov
2022-03-29 17:14:49 +02:00
committed by GitHub
parent 83d4be6151
commit e4caf7388a
7 changed files with 995 additions and 658 deletions
@@ -144,6 +144,12 @@ pub enum RuntimeCosts {
Caller,
/// Weight of calling `seal_is_contract`.
IsContract,
/// Weight of calling `seal_code_hash`.
#[cfg(feature = "unstable-interface")]
CodeHash,
/// Weight of calling `seal_own_code_hash`.
#[cfg(feature = "unstable-interface")]
OwnCodeHash,
/// Weight of calling `seal_caller_is_origin`.
CallerIsOrigin,
/// Weight of calling `seal_address`.
@@ -234,6 +240,10 @@ impl RuntimeCosts {
CopyToContract(len) => s.input_per_byte.saturating_mul(len.into()),
Caller => s.caller,
IsContract => s.is_contract,
#[cfg(feature = "unstable-interface")]
CodeHash => s.code_hash,
#[cfg(feature = "unstable-interface")]
OwnCodeHash => s.own_code_hash,
CallerIsOrigin => s.caller_is_origin,
Address => s.address,
GasLeft => s.gas_left,
@@ -1371,6 +1381,44 @@ define_env!(Env, <E: Ext>,
Ok(ctx.ext.is_contract(&address) as u32)
},
// Retrieve the code hash for a specified contract address.
//
// # Parameters
//
// - `account_ptr`: a pointer to the address in question.
// Should be decodable as an `T::AccountId`. Traps otherwise.
// - `out_ptr`: pointer to the linear memory where the returning value is written to.
// - `out_len_ptr`: in-out pointer into linear memory where the buffer length
// is read from and the value length is written to.
//
// # Errors
//
// `ReturnCode::KeyNotFound`
[__unstable__] seal_code_hash(ctx, account_ptr: u32, out_ptr: u32, out_len_ptr: u32) -> ReturnCode => {
ctx.charge_gas(RuntimeCosts::CodeHash)?;
let address: <<E as Ext>::T as frame_system::Config>::AccountId =
ctx.read_sandbox_memory_as(account_ptr)?;
if let Some(value) = ctx.ext.code_hash(&address) {
ctx.write_sandbox_output(out_ptr, out_len_ptr, &value.encode(), false, already_charged)?;
Ok(ReturnCode::Success)
} else {
Ok(ReturnCode::KeyNotFound)
}
},
// Retrieve the code hash of the currently executing contract.
//
// # Parameters
//
// - `out_ptr`: pointer to the linear memory where the returning value is written to.
// - `out_len_ptr`: in-out pointer into linear memory where the buffer length
// is read from and the value length is written to.
[__unstable__] seal_own_code_hash(ctx, out_ptr: u32, out_len_ptr: u32) => {
ctx.charge_gas(RuntimeCosts::OwnCodeHash)?;
let code_hash_encoded = &ctx.ext.own_code_hash().encode();
Ok(ctx.write_sandbox_output(out_ptr, out_len_ptr, code_hash_encoded, false, already_charged)?)
},
// 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