Skip to content

Commit f77d5b1

Browse files
authored
Merge pull request #1168 from interlay/fix/contracts-runtime-api
fix: contracts runtime-api and code limit
2 parents d95530d + 78132f0 commit f77d5b1

5 files changed

Lines changed: 141 additions & 2 deletions

File tree

parachain/runtime/kintsugi/src/contracts.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,14 @@ parameter_types! {
3838
pub const DeletionWeightLimit: Weight = Weight::from_parts(100000000, 0);
3939
pub const DepositPerByte: Balance = 1;
4040
pub const DepositPerItem: Balance = 1;
41-
pub const MaxCodeLen: u32 = 123 * 1024;
41+
// can't increase much beyond 400k unless we decrease max call stack height
42+
pub const MaxCodeLen: u32 = 400_000;
4243
pub const MaxStorageKeyLen: u32 = 128;
4344
pub const UnsafeUnstableInterface: bool = false;
4445
pub const MaxDebugBufferLen: u32 = 2 * 1024 * 1024;
4546
pub const DefaultDepositLimit: Balance = 1_000_000_000_000;
47+
// address 943dd009e661df00c8a21661ce6b89d4
48+
pub storage EnableContracts: bool = false;
4649
}
4750

4851
pub struct NativeCurrencyWithEd;
@@ -265,7 +268,7 @@ impl pallet_contracts::Config for Runtime {
265268
type WeightInfo = ();
266269
type ChainExtension = BtcRelayExtension;
267270
type Schedule = DefaultSchedule;
268-
type CallStack = [pallet_contracts::Frame<Self>; 5];
271+
type CallStack = [pallet_contracts::Frame<Self>; 1];
269272
type DepositPerByte = DepositPerByte;
270273
type DepositPerItem = DepositPerItem;
271274
type DefaultDepositLimit = DefaultDepositLimit;

parachain/runtime/kintsugi/src/lib.rs

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use frame_system::{
2828
use loans::{OnSlashHook, PostDeposit, PostTransfer, PreDeposit, PreTransfer};
2929
use orml_asset_registry::SequentialId;
3030
use orml_traits::{currency::MutationHooks, parameter_type_with_key};
31+
use pallet_contracts_primitives::ContractResult;
3132
use pallet_transaction_payment::{Multiplier, TargetedFeeAdjustment};
3233
use sp_api::impl_runtime_apis;
3334
use sp_core::{OpaqueMetadata, H256};
@@ -2156,6 +2157,133 @@ impl_runtime_apis! {
21562157
)
21572158
}
21582159
}
2160+
2161+
impl pallet_contracts::ContractsApi<Block, AccountId, Balance, BlockNumber, Hash> for Runtime {
2162+
fn call(
2163+
origin: AccountId,
2164+
dest: AccountId,
2165+
value: Balance,
2166+
gas_limit: Option<Weight>,
2167+
storage_deposit_limit: Option<Balance>,
2168+
input_data: Vec<u8>,
2169+
) -> pallet_contracts_primitives::ContractExecResult<Balance> {
2170+
if !contracts::EnableContracts::get() {
2171+
return ContractResult {
2172+
gas_consumed: Default::default(),
2173+
gas_required: Default::default(),
2174+
storage_deposit: Default::default(),
2175+
debug_message: Default::default(),
2176+
result: Err(sp_runtime::DispatchError::Other("pallet_contracts is disabled")),
2177+
};
2178+
}
2179+
2180+
let gas_limit = gas_limit.unwrap_or(RuntimeBlockWeights::get().max_block);
2181+
Contracts::bare_call(
2182+
origin,
2183+
dest,
2184+
value,
2185+
gas_limit,
2186+
storage_deposit_limit,
2187+
input_data,
2188+
true,
2189+
pallet_contracts::Determinism::Enforced,
2190+
)
2191+
}
2192+
2193+
fn instantiate(
2194+
origin: AccountId,
2195+
value: Balance,
2196+
gas_limit: Option<Weight>,
2197+
storage_deposit_limit: Option<Balance>,
2198+
code: pallet_contracts_primitives::Code<Hash>,
2199+
data: Vec<u8>,
2200+
salt: Vec<u8>,
2201+
) -> pallet_contracts_primitives::ContractInstantiateResult<AccountId, Balance> {
2202+
if !contracts::EnableContracts::get() {
2203+
return ContractResult {
2204+
gas_consumed: Default::default(),
2205+
gas_required: Default::default(),
2206+
storage_deposit: Default::default(),
2207+
debug_message: Default::default(),
2208+
result: Err(sp_runtime::DispatchError::Other("pallet_contracts is disabled")),
2209+
};
2210+
}
2211+
2212+
let gas_limit = gas_limit.unwrap_or(RuntimeBlockWeights::get().max_block);
2213+
Contracts::bare_instantiate(
2214+
origin,
2215+
value,
2216+
gas_limit,
2217+
storage_deposit_limit,
2218+
code,
2219+
data,
2220+
salt,
2221+
true,
2222+
)
2223+
}
2224+
2225+
fn upload_code(
2226+
origin: AccountId,
2227+
code: Vec<u8>,
2228+
storage_deposit_limit: Option<Balance>,
2229+
determinism: pallet_contracts::Determinism,
2230+
) -> pallet_contracts_primitives::CodeUploadResult<Hash, Balance>
2231+
{
2232+
if !contracts::EnableContracts::get() {
2233+
return Err(sp_runtime::DispatchError::Other("pallet_contracts is disabled"));
2234+
}
2235+
Contracts::bare_upload_code(origin, code, storage_deposit_limit, determinism)
2236+
}
2237+
2238+
fn get_storage(
2239+
address: AccountId,
2240+
key: Vec<u8>,
2241+
) -> pallet_contracts_primitives::GetStorageResult {
2242+
Contracts::get_storage(address, key)
2243+
}
2244+
}
2245+
2246+
// todo: enable this once we add contracts benchmarking
2247+
// #[cfg(feature = "runtime-benchmarks")]
2248+
// impl frame_benchmarking::Benchmark<Block> for Runtime {
2249+
// fn benchmark_metadata(extra: bool) -> (
2250+
// Vec<frame_benchmarking::BenchmarkList>,
2251+
// Vec<frame_support::traits::StorageInfo>,
2252+
// ) {
2253+
// use frame_benchmarking::{baseline, Benchmarking, BenchmarkList};
2254+
// use frame_support::traits::StorageInfoTrait;
2255+
// use frame_system_benchmarking::Pallet as SystemBench;
2256+
// use baseline::Pallet as BaselineBench;
2257+
//
2258+
// let mut list = Vec::<BenchmarkList>::new();
2259+
// list_benchmarks!(list, extra);
2260+
//
2261+
// let storage_info = AllPalletsWithSystem::storage_info();
2262+
//
2263+
// (list, storage_info)
2264+
// }
2265+
//
2266+
// fn dispatch_benchmark(
2267+
// config: frame_benchmarking::BenchmarkConfig
2268+
// ) -> Result<Vec<frame_benchmarking::BenchmarkBatch>, sp_runtime::RuntimeString> {
2269+
// use frame_benchmarking::{baseline, Benchmarking, BenchmarkBatch, TrackedStorageKey};
2270+
// use frame_system_benchmarking::Pallet as SystemBench;
2271+
// use baseline::Pallet as BaselineBench;
2272+
//
2273+
// impl frame_system_benchmarking::Config for Runtime {}
2274+
// impl baseline::Config for Runtime {}
2275+
//
2276+
// use frame_support::traits::WhitelistedStorageKeys;
2277+
// let whitelist: Vec<TrackedStorageKey> = AllPalletsWithSystem::whitelisted_storage_keys();
2278+
//
2279+
// let mut batches = Vec::<BenchmarkBatch>::new();
2280+
// let params = (&config, &whitelist);
2281+
// add_benchmarks!(params, batches);
2282+
//
2283+
// if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) }
2284+
// Ok(batches)
2285+
// }
2286+
// }
21592287
}
21602288

21612289
struct CheckInherents;

parachain/runtime/runtime-tests/src/parachain/contracts.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ mod relay {
3030
fn test_basic_contract() {
3131
// not sure this case would ever be used, best we have a test for it anyway..
3232
ExtBuilder::build().execute_with(|| {
33+
let key = kintsugi_runtime_parachain::contracts::EnableContracts::key();
34+
let hex = hex::encode(key);
35+
println!("key = {hex}");
3336
// note: current working directory is diffent when you run this test, vs when you debug it.
3437
// However, the `PWD` env variable is (surprisingly) set to the workspace root in both cases.
3538
// So, we use a path relative to PWD

parachain/src/chain_spec/kintsugi.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ pub struct KintsugiDevGenesisExt {
1717
pub(crate) enable_instant_seal: bool,
1818
/// The flag to enable EVM contract creation.
1919
pub(crate) enable_create: bool,
20+
/// The flag to enable wasm contracts.
21+
pub(crate) enable_contracts: bool,
2022
}
2123

2224
impl sp_runtime::BuildStorage for KintsugiDevGenesisExt {
2325
fn assimilate_storage(&self, storage: &mut Storage) -> Result<(), String> {
2426
sp_state_machine::BasicExternalities::execute_with_storage(storage, || {
2527
kintsugi_runtime::EnableManualSeal::set(&self.enable_instant_seal);
2628
kintsugi_runtime::evm::EnableCreate::set(&self.enable_create);
29+
kintsugi_runtime::contracts::EnableContracts::set(&self.enable_contracts);
2730
});
2831
self.genesis_config.assimilate_storage(storage)
2932
}
@@ -73,6 +76,7 @@ pub fn kintsugi_dev_config(enable_instant_seal: bool) -> KintsugiDevChainSpec {
7376
),
7477
enable_instant_seal,
7578
enable_create: true,
79+
enable_contracts: true,
7680
},
7781
Vec::new(),
7882
None,

parachain/src/chain_spec/testnet_kintsugi.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ pub fn development_config(id: ParaId, enable_instant_seal: bool) -> KintsugiDevC
105105
),
106106
enable_instant_seal,
107107
enable_create: true,
108+
enable_contracts: true,
108109
},
109110
Vec::new(),
110111
None,

0 commit comments

Comments
 (0)