From 708bd81e46f3a4ec3c7e9f4984f727da757cfd88 Mon Sep 17 00:00:00 2001 From: xermicus Date: Mon, 23 Sep 2024 11:35:43 +0200 Subject: [PATCH 1/7] add chain id to config and expose via syscall Signed-off-by: xermicus --- .../revive/fixtures/contracts/chain_id.rs | 37 ++++++++++ substrate/frame/revive/src/lib.rs | 25 ++++--- substrate/frame/revive/src/tests.rs | 19 ++++- substrate/frame/revive/src/wasm/runtime.rs | 70 ++++++++++++------- substrate/frame/revive/uapi/src/host.rs | 3 + .../frame/revive/uapi/src/host/riscv32.rs | 3 +- 6 files changed, 122 insertions(+), 35 deletions(-) create mode 100644 substrate/frame/revive/fixtures/contracts/chain_id.rs diff --git a/substrate/frame/revive/fixtures/contracts/chain_id.rs b/substrate/frame/revive/fixtures/contracts/chain_id.rs new file mode 100644 index 000000000000..ce7a0cc671ce --- /dev/null +++ b/substrate/frame/revive/fixtures/contracts/chain_id.rs @@ -0,0 +1,37 @@ +// This file is part of Substrate. + +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#![no_std] +#![no_main] + +extern crate common; + +use uapi::{HostFn, HostFnImpl as api, ReturnFlags}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() { + call() +} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + let mut buf = [0; 32]; + api::chain_id(&mut buf); + api::return_value(ReturnFlags::empty(), &buf); +} diff --git a/substrate/frame/revive/src/lib.rs b/substrate/frame/revive/src/lib.rs index bf9803690560..955c68586801 100644 --- a/substrate/frame/revive/src/lib.rs +++ b/substrate/frame/revive/src/lib.rs @@ -293,6 +293,13 @@ pub mod pallet { /// This value is usually higher than [`Self::RuntimeMemory`] to account for the fact /// that validators have to hold all storage items in PvF memory. type PVFMemory: Get; + + /// The [EIP-155](https://eips.ethereum.org/EIPS/eip-155) chain ID. + /// + /// This is a unique identifier assigned to each blockchain network, + /// preventing replay attacks. + #[pallet::constant] + type ChainId: Get; } /// Container for different types that implement [`DefaultConfig`]` of this pallet. @@ -303,7 +310,7 @@ pub mod pallet { traits::{ConstBool, ConstU32}, }; use frame_system::EnsureSigned; - use sp_core::parameter_types; + use sp_core::{parameter_types, ConstU64}; type AccountId = sp_runtime::AccountId32; type Balance = u64; @@ -365,6 +372,7 @@ pub mod pallet { type Xcm = (); type RuntimeMemory = ConstU32<{ 128 * 1024 * 1024 }>; type PVFMemory = ConstU32<{ 512 * 1024 * 1024 }>; + type ChainId = ConstU64<{ 0 }>; } } @@ -659,8 +667,8 @@ pub mod pallet { // We can use storage to store items using the available block ref_time with the // `set_storage` host function. - let max_storage_size: u32 = ((max_block_ref_time / - (>::weight(&RuntimeCosts::SetStorage { + let max_storage_size: u32 = ((max_block_ref_time + / (>::weight(&RuntimeCosts::SetStorage { new_bytes: max_payload_size, old_bytes: 0, }) @@ -682,8 +690,8 @@ pub mod pallet { // We can use storage to store events using the available block ref_time with the // `deposit_event` host function. The overhead of stored events, which is around 100B, // is not taken into account to simplify calculations, as it does not change much. - let max_events_size: u32 = ((max_block_ref_time / - (>::weight(&RuntimeCosts::DepositEvent { + let max_events_size: u32 = ((max_block_ref_time + / (>::weight(&RuntimeCosts::DepositEvent { num_topic: 0, len: max_payload_size, }) @@ -919,7 +927,7 @@ pub mod pallet { let contract = if let Some(contract) = contract { contract } else { - return Err(>::ContractNotFound.into()) + return Err(>::ContractNotFound.into()); }; >>::increment_refcount(code_hash)?; >>::decrement_refcount(contract.code_hash); @@ -1041,8 +1049,9 @@ where storage_deposit_limit.saturating_reduce(upload_deposit); (executable, upload_deposit) }, - Code::Existing(code_hash) => - (WasmBlob::from_storage(code_hash, &mut gas_meter)?, Default::default()), + Code::Existing(code_hash) => { + (WasmBlob::from_storage(code_hash, &mut gas_meter)?, Default::default()) + }, }; let instantiate_origin = Origin::from_account_id(instantiate_account.clone()); let mut storage_meter = diff --git a/substrate/frame/revive/src/tests.rs b/substrate/frame/revive/src/tests.rs index 167e8da201b8..2f4448109ea4 100644 --- a/substrate/frame/revive/src/tests.rs +++ b/substrate/frame/revive/src/tests.rs @@ -149,8 +149,8 @@ pub mod test_utils { let code_info_len = CodeInfo::::max_encoded_len() as u64; // Calculate deposit to be reserved. // We add 2 storage items: one for code, other for code_info - DepositPerByte::get().saturating_mul(code_len as u64 + code_info_len) + - DepositPerItem::get().saturating_mul(2) + DepositPerByte::get().saturating_mul(code_len as u64 + code_info_len) + + DepositPerItem::get().saturating_mul(2) } pub fn ensure_stored(code_hash: sp_core::H256) -> usize { // Assert that code_info is stored @@ -412,6 +412,7 @@ parameter_types! { pub static DepositPerByte: BalanceOf = 1; pub const DepositPerItem: BalanceOf = 2; pub static CodeHashLockupDepositPercent: Perbill = Perbill::from_percent(0); + pub static ChainId: u64 = 384; } impl Convert> for Test { @@ -496,6 +497,7 @@ impl Config for Test { type InstantiateOrigin = EnsureAccount; type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent; type Debug = TestDebug; + type ChainId = ChainId; } pub struct ExtBuilder { @@ -4310,4 +4312,17 @@ mod run_tests { assert_ok!(builder::call(addr).build()); }); } + + #[test] + fn chain_id_works() { + let (code, _) = compile_module("chain_id").unwrap(); + + ExtBuilder::default().existential_deposit(100).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + let chain_id = U256::from(::ChainId::get()); + let received = builder::bare_instantiate(Code::Upload(code)).build_and_unwrap_result(); + assert_eq!(received.result.data, chain_id.encode()); + }); + } } diff --git a/substrate/frame/revive/src/wasm/runtime.rs b/substrate/frame/revive/src/wasm/runtime.rs index 34cce533c14a..6d7754183641 100644 --- a/substrate/frame/revive/src/wasm/runtime.rs +++ b/substrate/frame/revive/src/wasm/runtime.rs @@ -468,22 +468,28 @@ impl Token for RuntimeCosts { Terminate(locked_dependencies) => T::WeightInfo::seal_terminate(locked_dependencies), DepositEvent { num_topic, len } => T::WeightInfo::seal_deposit_event(num_topic, len), DebugMessage(len) => T::WeightInfo::seal_debug_message(len), - SetStorage { new_bytes, old_bytes } => - cost_storage!(write, seal_set_storage, new_bytes, old_bytes), + SetStorage { new_bytes, old_bytes } => { + cost_storage!(write, seal_set_storage, new_bytes, old_bytes) + }, ClearStorage(len) => cost_storage!(write, seal_clear_storage, len), ContainsStorage(len) => cost_storage!(read, seal_contains_storage, len), GetStorage(len) => cost_storage!(read, seal_get_storage, len), TakeStorage(len) => cost_storage!(write, seal_take_storage, len), - SetTransientStorage { new_bytes, old_bytes } => - cost_storage!(write_transient, seal_set_transient_storage, new_bytes, old_bytes), - ClearTransientStorage(len) => - cost_storage!(write_transient, seal_clear_transient_storage, len), - ContainsTransientStorage(len) => - cost_storage!(read_transient, seal_contains_transient_storage, len), - GetTransientStorage(len) => - cost_storage!(read_transient, seal_get_transient_storage, len), - TakeTransientStorage(len) => - cost_storage!(write_transient, seal_take_transient_storage, len), + SetTransientStorage { new_bytes, old_bytes } => { + cost_storage!(write_transient, seal_set_transient_storage, new_bytes, old_bytes) + }, + ClearTransientStorage(len) => { + cost_storage!(write_transient, seal_clear_transient_storage, len) + }, + ContainsTransientStorage(len) => { + cost_storage!(read_transient, seal_contains_transient_storage, len) + }, + GetTransientStorage(len) => { + cost_storage!(read_transient, seal_get_transient_storage, len) + }, + TakeTransientStorage(len) => { + cost_storage!(write_transient, seal_take_transient_storage, len) + }, Transfer => T::WeightInfo::seal_transfer(), CallBase => T::WeightInfo::seal_call(0, 0), DelegateCallBase => T::WeightInfo::seal_delegate_call(), @@ -563,15 +569,16 @@ impl<'a, E: Ext, M: PolkaVmInstance> Runtime<'a, E, M> { log::error!(target: LOG_TARGET, "polkavm execution error: {error}"); Some(Err(Error::::ExecutionFailed.into())) }, - Ok(Finished) => - Some(Ok(ExecReturnValue { flags: ReturnFlags::empty(), data: Vec::new() })), + Ok(Finished) => { + Some(Ok(ExecReturnValue { flags: ReturnFlags::empty(), data: Vec::new() })) + }, Ok(Trap) => Some(Err(Error::::ContractTrapped.into())), Ok(Segfault(_)) => Some(Err(Error::::ExecutionFailed.into())), Ok(NotEnoughGas) => Some(Err(Error::::OutOfGas.into())), Ok(Step) => None, Ok(Ecalli(idx)) => { let Some(syscall_symbol) = module.imports().get(idx) else { - return Some(Err(>::InvalidSyscall.into())) + return Some(Err(>::InvalidSyscall.into())); }; match self.handle_ecall(instance, syscall_symbol.as_bytes(), api_version) { Ok(None) => None, @@ -579,11 +586,12 @@ impl<'a, E: Ext, M: PolkaVmInstance> Runtime<'a, E, M> { instance.write_output(return_value); None }, - Err(TrapReason::Return(ReturnData { flags, data })) => + Err(TrapReason::Return(ReturnData { flags, data })) => { match ReturnFlags::from_bits(flags) { None => Some(Err(Error::::InvalidCallFlags.into())), Some(flags) => Some(Ok(ExecReturnValue { flags, data })), - }, + } + }, Err(TrapReason::Termination) => Some(Ok(Default::default())), Err(TrapReason::SupervisorError(error)) => Some(Err(error.into())), } @@ -679,7 +687,7 @@ impl<'a, E: Ext, M: ?Sized + Memory> Runtime<'a, E, M> { create_token: impl FnOnce(u32) -> Option, ) -> Result<(), DispatchError> { if allow_skip && out_ptr == SENTINEL { - return Ok(()) + return Ok(()); } let len = memory.read_u32(out_len_ptr)?; @@ -703,7 +711,7 @@ impl<'a, E: Ext, M: ?Sized + Memory> Runtime<'a, E, M> { create_token: impl FnOnce(u32) -> Option, ) -> Result<(), DispatchError> { if allow_skip && out_ptr == SENTINEL { - return Ok(()) + return Ok(()); } let buf_len = buf.len() as u32; @@ -820,7 +828,7 @@ impl<'a, E: Ext, M: ?Sized + Memory> Runtime<'a, E, M> { let max_size = self.ext.max_value_size(); let charged = self.charge_gas(costs(value_len, self.ext.max_value_size()))?; if value_len > max_size { - return Err(Error::::ValueTooLarge.into()) + return Err(Error::::ValueTooLarge.into()); } let key = self.decode_key(memory, key_ptr, key_len)?; let value = Some(memory.read(value_ptr, value_len)?); @@ -1022,7 +1030,7 @@ impl<'a, E: Ext, M: ?Sized + Memory> Runtime<'a, E, M> { }, CallType::DelegateCall { code_hash_ptr } => { if flags.intersects(CallFlags::ALLOW_REENTRY | CallFlags::READ_ONLY) { - return Err(Error::::InvalidCallFlags.into()) + return Err(Error::::InvalidCallFlags.into()); } let code_hash = memory.read_h256(code_hash_ptr)?; @@ -1037,7 +1045,7 @@ impl<'a, E: Ext, M: ?Sized + Memory> Runtime<'a, E, M> { return Err(TrapReason::Return(ReturnData { flags: return_value.flags.bits(), data: return_value.data, - })) + })); } } @@ -1536,6 +1544,19 @@ pub mod env { )?) } + /// Returns the chain ID. + /// See [`pallet_revive_uapi::HostFn::chain_id`]. + #[api_version(0)] + fn chain_id(&mut self, memory: &mut M, out_ptr: u32) -> Result<(), TrapReason> { + Ok(self.write_fixed_sandbox_output( + memory, + out_ptr, + &as_bytes(U256::from(::ChainId::get())), + false, + already_charged, + )?) + } + /// Stores the value transferred along with this call/instantiate into the supplied buffer. /// See [`pallet_revive_uapi::HostFn::value_transferred`]. #[api_version(0)] @@ -1719,8 +1740,9 @@ pub mod env { Environment::new(self, memory, id, input_ptr, input_len, output_ptr, output_len_ptr); let ret = match chain_extension.call(env)? { RetVal::Converging(val) => Ok(val), - RetVal::Diverging { flags, data } => - Err(TrapReason::Return(ReturnData { flags: flags.bits(), data })), + RetVal::Diverging { flags, data } => { + Err(TrapReason::Return(ReturnData { flags: flags.bits(), data })) + }, }; self.chain_extension = Some(chain_extension); ret diff --git a/substrate/frame/revive/uapi/src/host.rs b/substrate/frame/revive/uapi/src/host.rs index 538de7ea251d..57a03332670f 100644 --- a/substrate/frame/revive/uapi/src/host.rs +++ b/substrate/frame/revive/uapi/src/host.rs @@ -71,6 +71,9 @@ pub trait HostFn: private::Sealed { /// - `output`: A reference to the output data buffer to write the balance. fn balance_of(addr: &[u8; 20], output: &mut [u8; 32]); + /// Returns the [EIP-155](https://eips.ethereum.org/EIPS/eip-155) chain ID. + fn chain_id(output: &mut [u8; 32]); + /// Stores the current block number of the current contract into the supplied buffer. /// /// # Parameters diff --git a/substrate/frame/revive/uapi/src/host/riscv32.rs b/substrate/frame/revive/uapi/src/host/riscv32.rs index 0bb0ede4543b..a60c338e8bd9 100644 --- a/substrate/frame/revive/uapi/src/host/riscv32.rs +++ b/substrate/frame/revive/uapi/src/host/riscv32.rs @@ -83,6 +83,7 @@ mod sys { pub fn weight_left(out_ptr: *mut u8, out_len_ptr: *mut u32); pub fn balance(out_ptr: *mut u8); pub fn balance_of(addr_ptr: *const u8, out_ptr: *mut u8); + pub fn chain_id(out_ptr: *mut u8); pub fn value_transferred(out_ptr: *mut u8); pub fn now(out_ptr: *mut u8); pub fn minimum_balance(out_ptr: *mut u8); @@ -447,7 +448,7 @@ impl HostFn for HostFnImpl { } impl_wrapper_for! { - [u8; 32] => block_number, balance, value_transferred, now, minimum_balance; + [u8; 32] => block_number, balance, value_transferred, now, minimum_balance, chain_id; [u8; 20] => address, caller; } From d1f70d2b7c5ea338ca084d4352fcf176b75aef83 Mon Sep 17 00:00:00 2001 From: command-bot <> Date: Mon, 23 Sep 2024 09:41:14 +0000 Subject: [PATCH 2/7] ".git/.scripts/commands/fmt/fmt.sh" --- substrate/frame/revive/src/lib.rs | 13 ++++++------- substrate/frame/revive/src/tests.rs | 4 ++-- substrate/frame/revive/src/wasm/runtime.rs | 15 ++++++--------- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/substrate/frame/revive/src/lib.rs b/substrate/frame/revive/src/lib.rs index 955c68586801..08f3b23dd7a0 100644 --- a/substrate/frame/revive/src/lib.rs +++ b/substrate/frame/revive/src/lib.rs @@ -667,8 +667,8 @@ pub mod pallet { // We can use storage to store items using the available block ref_time with the // `set_storage` host function. - let max_storage_size: u32 = ((max_block_ref_time - / (>::weight(&RuntimeCosts::SetStorage { + let max_storage_size: u32 = ((max_block_ref_time / + (>::weight(&RuntimeCosts::SetStorage { new_bytes: max_payload_size, old_bytes: 0, }) @@ -690,8 +690,8 @@ pub mod pallet { // We can use storage to store events using the available block ref_time with the // `deposit_event` host function. The overhead of stored events, which is around 100B, // is not taken into account to simplify calculations, as it does not change much. - let max_events_size: u32 = ((max_block_ref_time - / (>::weight(&RuntimeCosts::DepositEvent { + let max_events_size: u32 = ((max_block_ref_time / + (>::weight(&RuntimeCosts::DepositEvent { num_topic: 0, len: max_payload_size, }) @@ -1049,9 +1049,8 @@ where storage_deposit_limit.saturating_reduce(upload_deposit); (executable, upload_deposit) }, - Code::Existing(code_hash) => { - (WasmBlob::from_storage(code_hash, &mut gas_meter)?, Default::default()) - }, + Code::Existing(code_hash) => + (WasmBlob::from_storage(code_hash, &mut gas_meter)?, Default::default()), }; let instantiate_origin = Origin::from_account_id(instantiate_account.clone()); let mut storage_meter = diff --git a/substrate/frame/revive/src/tests.rs b/substrate/frame/revive/src/tests.rs index 2f4448109ea4..d06cdcfd4654 100644 --- a/substrate/frame/revive/src/tests.rs +++ b/substrate/frame/revive/src/tests.rs @@ -149,8 +149,8 @@ pub mod test_utils { let code_info_len = CodeInfo::::max_encoded_len() as u64; // Calculate deposit to be reserved. // We add 2 storage items: one for code, other for code_info - DepositPerByte::get().saturating_mul(code_len as u64 + code_info_len) - + DepositPerItem::get().saturating_mul(2) + DepositPerByte::get().saturating_mul(code_len as u64 + code_info_len) + + DepositPerItem::get().saturating_mul(2) } pub fn ensure_stored(code_hash: sp_core::H256) -> usize { // Assert that code_info is stored diff --git a/substrate/frame/revive/src/wasm/runtime.rs b/substrate/frame/revive/src/wasm/runtime.rs index 6d7754183641..fb0d67cf250e 100644 --- a/substrate/frame/revive/src/wasm/runtime.rs +++ b/substrate/frame/revive/src/wasm/runtime.rs @@ -569,9 +569,8 @@ impl<'a, E: Ext, M: PolkaVmInstance> Runtime<'a, E, M> { log::error!(target: LOG_TARGET, "polkavm execution error: {error}"); Some(Err(Error::::ExecutionFailed.into())) }, - Ok(Finished) => { - Some(Ok(ExecReturnValue { flags: ReturnFlags::empty(), data: Vec::new() })) - }, + Ok(Finished) => + Some(Ok(ExecReturnValue { flags: ReturnFlags::empty(), data: Vec::new() })), Ok(Trap) => Some(Err(Error::::ContractTrapped.into())), Ok(Segfault(_)) => Some(Err(Error::::ExecutionFailed.into())), Ok(NotEnoughGas) => Some(Err(Error::::OutOfGas.into())), @@ -586,12 +585,11 @@ impl<'a, E: Ext, M: PolkaVmInstance> Runtime<'a, E, M> { instance.write_output(return_value); None }, - Err(TrapReason::Return(ReturnData { flags, data })) => { + Err(TrapReason::Return(ReturnData { flags, data })) => match ReturnFlags::from_bits(flags) { None => Some(Err(Error::::InvalidCallFlags.into())), Some(flags) => Some(Ok(ExecReturnValue { flags, data })), - } - }, + }, Err(TrapReason::Termination) => Some(Ok(Default::default())), Err(TrapReason::SupervisorError(error)) => Some(Err(error.into())), } @@ -1740,9 +1738,8 @@ pub mod env { Environment::new(self, memory, id, input_ptr, input_len, output_ptr, output_len_ptr); let ret = match chain_extension.call(env)? { RetVal::Converging(val) => Ok(val), - RetVal::Diverging { flags, data } => { - Err(TrapReason::Return(ReturnData { flags: flags.bits(), data })) - }, + RetVal::Diverging { flags, data } => + Err(TrapReason::Return(ReturnData { flags: flags.bits(), data })), }; self.chain_extension = Some(chain_extension); ret From e016926b3dbbb5ef8b864769f47e9b5a9fb55706 Mon Sep 17 00:00:00 2001 From: xermicus Date: Mon, 23 Sep 2024 11:43:00 +0200 Subject: [PATCH 3/7] prdoc Signed-off-by: xermicus --- prdoc/pr_5807.prdoc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 prdoc/pr_5807.prdoc diff --git a/prdoc/pr_5807.prdoc b/prdoc/pr_5807.prdoc new file mode 100644 index 000000000000..3447ea64e439 --- /dev/null +++ b/prdoc/pr_5807.prdoc @@ -0,0 +1,16 @@ +title: "[pallet-revive] last call return data API" + +doc: + - audience: Runtime Dev + description: | + This PR adds the EVM chain ID to Config as well as a corresponding runtime API so contracts can query it. + + Related issue: https://github.com/paritytech/revive/issues/44 + +crates: + - name: pallet-revive + bump: major + - name: pallet-revive-fixtures + bump: patch + - name: pallet-revive-uapi + bump: minor From 83b6426cd1e0ee0530e06a20fbb64332531dc690 Mon Sep 17 00:00:00 2001 From: xermicus Date: Mon, 23 Sep 2024 11:45:18 +0200 Subject: [PATCH 4/7] impor Signed-off-by: xermicus --- substrate/frame/revive/src/lib.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/substrate/frame/revive/src/lib.rs b/substrate/frame/revive/src/lib.rs index 08f3b23dd7a0..8ddaff896c25 100644 --- a/substrate/frame/revive/src/lib.rs +++ b/substrate/frame/revive/src/lib.rs @@ -60,7 +60,7 @@ use frame_support::{ ensure, traits::{ fungible::{Inspect, Mutate, MutateHold}, - ConstU32, Contains, EnsureOrigin, Get, Time, + ConstU32, ConstU64, Contains, EnsureOrigin, Get, Time, }, weights::{Weight, WeightMeter}, BoundedVec, RuntimeDebugNoBound, @@ -310,7 +310,7 @@ pub mod pallet { traits::{ConstBool, ConstU32}, }; use frame_system::EnsureSigned; - use sp_core::{parameter_types, ConstU64}; + use sp_core::parameter_types; type AccountId = sp_runtime::AccountId32; type Balance = u64; @@ -667,8 +667,8 @@ pub mod pallet { // We can use storage to store items using the available block ref_time with the // `set_storage` host function. - let max_storage_size: u32 = ((max_block_ref_time / - (>::weight(&RuntimeCosts::SetStorage { + let max_storage_size: u32 = ((max_block_ref_time + / (>::weight(&RuntimeCosts::SetStorage { new_bytes: max_payload_size, old_bytes: 0, }) @@ -690,8 +690,8 @@ pub mod pallet { // We can use storage to store events using the available block ref_time with the // `deposit_event` host function. The overhead of stored events, which is around 100B, // is not taken into account to simplify calculations, as it does not change much. - let max_events_size: u32 = ((max_block_ref_time / - (>::weight(&RuntimeCosts::DepositEvent { + let max_events_size: u32 = ((max_block_ref_time + / (>::weight(&RuntimeCosts::DepositEvent { num_topic: 0, len: max_payload_size, }) @@ -1049,8 +1049,9 @@ where storage_deposit_limit.saturating_reduce(upload_deposit); (executable, upload_deposit) }, - Code::Existing(code_hash) => - (WasmBlob::from_storage(code_hash, &mut gas_meter)?, Default::default()), + Code::Existing(code_hash) => { + (WasmBlob::from_storage(code_hash, &mut gas_meter)?, Default::default()) + }, }; let instantiate_origin = Origin::from_account_id(instantiate_account.clone()); let mut storage_meter = From 8289a48cbe27733b77731aedfd6765aab39191ae Mon Sep 17 00:00:00 2001 From: xermicus Date: Mon, 23 Sep 2024 12:37:36 +0200 Subject: [PATCH 5/7] gas token Signed-off-by: xermicus --- substrate/frame/revive/src/wasm/runtime.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/frame/revive/src/wasm/runtime.rs b/substrate/frame/revive/src/wasm/runtime.rs index fb0d67cf250e..ebc407adacd2 100644 --- a/substrate/frame/revive/src/wasm/runtime.rs +++ b/substrate/frame/revive/src/wasm/runtime.rs @@ -1551,7 +1551,7 @@ pub mod env { out_ptr, &as_bytes(U256::from(::ChainId::get())), false, - already_charged, + |_| Some(RuntimeCosts::CopyToContract(32)), )?) } From 32b4b0a521e305b93bf55fd8d19ff03aaeb7acbf Mon Sep 17 00:00:00 2001 From: xermicus Date: Mon, 23 Sep 2024 13:28:53 +0200 Subject: [PATCH 6/7] configure chainid in the ktichensink runtime Signed-off-by: xermicus --- substrate/bin/node/runtime/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index bfcaf5e9af8f..784d74973c32 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -49,8 +49,8 @@ use frame_support::{ imbalance::ResolveAssetTo, nonfungibles_v2::Inspect, pay::PayAssetFromAccount, GetSalary, PayFromAccount, }, - AsEnsureOriginWithArg, ConstBool, ConstU128, ConstU16, ConstU32, Contains, Currency, - EitherOfDiverse, EnsureOriginWithArg, EqualPrivilegeOnly, Imbalance, InsideBoth, + AsEnsureOriginWithArg, ConstBool, ConstU128, ConstU16, ConstU32, ConstU64, Contains, + Currency, EitherOfDiverse, EnsureOriginWithArg, EqualPrivilegeOnly, Imbalance, InsideBoth, InstanceFilter, KeyOwnerProofSystem, LinearStoragePrice, LockIdentifier, Nothing, OnUnbalanced, VariantCountOf, WithdrawReasons, }, @@ -1419,6 +1419,7 @@ impl pallet_revive::Config for Runtime { type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent; type Debug = (); type Xcm = (); + type ChainId = ConstU64<420_420_420>; } impl pallet_sudo::Config for Runtime { From ae1b232951955d0780b37de455cdd6e90d2f1ad1 Mon Sep 17 00:00:00 2001 From: command-bot <> Date: Mon, 23 Sep 2024 13:52:52 +0000 Subject: [PATCH 7/7] ".git/.scripts/commands/fmt/fmt.sh" --- substrate/frame/revive/src/lib.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/substrate/frame/revive/src/lib.rs b/substrate/frame/revive/src/lib.rs index 8ddaff896c25..114d51c89695 100644 --- a/substrate/frame/revive/src/lib.rs +++ b/substrate/frame/revive/src/lib.rs @@ -667,8 +667,8 @@ pub mod pallet { // We can use storage to store items using the available block ref_time with the // `set_storage` host function. - let max_storage_size: u32 = ((max_block_ref_time - / (>::weight(&RuntimeCosts::SetStorage { + let max_storage_size: u32 = ((max_block_ref_time / + (>::weight(&RuntimeCosts::SetStorage { new_bytes: max_payload_size, old_bytes: 0, }) @@ -690,8 +690,8 @@ pub mod pallet { // We can use storage to store events using the available block ref_time with the // `deposit_event` host function. The overhead of stored events, which is around 100B, // is not taken into account to simplify calculations, as it does not change much. - let max_events_size: u32 = ((max_block_ref_time - / (>::weight(&RuntimeCosts::DepositEvent { + let max_events_size: u32 = ((max_block_ref_time / + (>::weight(&RuntimeCosts::DepositEvent { num_topic: 0, len: max_payload_size, }) @@ -1049,9 +1049,8 @@ where storage_deposit_limit.saturating_reduce(upload_deposit); (executable, upload_deposit) }, - Code::Existing(code_hash) => { - (WasmBlob::from_storage(code_hash, &mut gas_meter)?, Default::default()) - }, + Code::Existing(code_hash) => + (WasmBlob::from_storage(code_hash, &mut gas_meter)?, Default::default()), }; let instantiate_origin = Origin::from_account_id(instantiate_account.clone()); let mut storage_meter =