mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-09 21:21:11 +00:00
contracts: Use WeakBoundedVec for instrumented code (#12186)
* Use WeakBoundedVec for instrumented code * Remove `RelaxedMaxCodeLen` from kitchensink
This commit is contained in:
committed by
GitHub
parent
f7ac2cd20f
commit
0d2adf1fa1
@@ -1167,7 +1167,6 @@ impl pallet_contracts::Config for Runtime {
|
||||
type AddressGenerator = pallet_contracts::DefaultAddressGenerator;
|
||||
type ContractAccessWeight = pallet_contracts::DefaultContractAccessWeight<RuntimeBlockWeights>;
|
||||
type MaxCodeLen = ConstU32<{ 128 * 1024 }>;
|
||||
type RelaxedMaxCodeLen = ConstU32<{ 256 * 1024 }>;
|
||||
type MaxStorageKeyLen = ConstU32<128>;
|
||||
}
|
||||
|
||||
|
||||
@@ -111,7 +111,7 @@ use frame_support::{
|
||||
ensure,
|
||||
traits::{ConstU32, Contains, Currency, Get, Randomness, ReservableCurrency, Time},
|
||||
weights::Weight,
|
||||
BoundedVec,
|
||||
BoundedVec, WeakBoundedVec,
|
||||
};
|
||||
use frame_system::{limits::BlockWeights, Pallet as System};
|
||||
use pallet_contracts_primitives::{
|
||||
@@ -135,7 +135,7 @@ type TrieId = BoundedVec<u8, ConstU32<128>>;
|
||||
type BalanceOf<T> =
|
||||
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
|
||||
type CodeVec<T> = BoundedVec<u8, <T as Config>::MaxCodeLen>;
|
||||
type RelaxedCodeVec<T> = BoundedVec<u8, <T as Config>::RelaxedMaxCodeLen>;
|
||||
type RelaxedCodeVec<T> = WeakBoundedVec<u8, <T as Config>::MaxCodeLen>;
|
||||
type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;
|
||||
|
||||
/// Used as a sentinel value when reading and writing contract memory.
|
||||
@@ -366,15 +366,6 @@ pub mod pallet {
|
||||
/// a wasm binary below this maximum size.
|
||||
type MaxCodeLen: Get<u32>;
|
||||
|
||||
/// The maximum length of a contract code after reinstrumentation.
|
||||
///
|
||||
/// When uploading a new contract the size defined by [`Self::MaxCodeLen`] is used for both
|
||||
/// the pristine **and** the instrumented version. When a existing contract needs to be
|
||||
/// reinstrumented after a runtime upgrade we apply this bound. The reason is that if the
|
||||
/// new instrumentation increases the size beyond the limit it would make that contract
|
||||
/// inaccessible until rectified by another runtime upgrade.
|
||||
type RelaxedMaxCodeLen: Get<u32>;
|
||||
|
||||
/// The maximum allowable length in bytes for storage keys.
|
||||
type MaxStorageKeyLen: Get<u32>;
|
||||
}
|
||||
|
||||
@@ -395,7 +395,6 @@ impl Config for Test {
|
||||
type AddressGenerator = DefaultAddressGenerator;
|
||||
type ContractAccessWeight = DefaultContractAccessWeight<BlockWeights>;
|
||||
type MaxCodeLen = ConstU32<{ 128 * 1024 }>;
|
||||
type RelaxedMaxCodeLen = ConstU32<{ 256 * 1024 }>;
|
||||
type MaxStorageKeyLen = ConstU32<128>;
|
||||
}
|
||||
|
||||
|
||||
@@ -39,6 +39,7 @@ use frame_support::{
|
||||
dispatch::{DispatchError, DispatchResult},
|
||||
ensure,
|
||||
traits::{Get, ReservableCurrency},
|
||||
WeakBoundedVec,
|
||||
};
|
||||
use sp_core::crypto::UncheckedFrom;
|
||||
use sp_runtime::traits::BadOrigin;
|
||||
@@ -195,10 +196,15 @@ pub fn reinstrument<T: Config>(
|
||||
let original_code =
|
||||
<PristineCode<T>>::get(&prefab_module.code_hash).ok_or(Error::<T>::CodeNotFound)?;
|
||||
let original_code_len = original_code.len();
|
||||
prefab_module.code = prepare::reinstrument_contract::<T>(&original_code, schedule)
|
||||
.map_err(|_| <Error<T>>::CodeRejected)?
|
||||
.try_into()
|
||||
.map_err(|_| <Error<T>>::CodeTooLarge)?;
|
||||
// We need to allow contracts growing too big after re-instrumentation. Otherwise
|
||||
// the contract can become inaccessible. The user has no influence over this size
|
||||
// as the contract is already deployed and every change in size would be the result
|
||||
// of changes in the instrumentation algorithm controlled by the chain authors.
|
||||
prefab_module.code = WeakBoundedVec::force_from(
|
||||
prepare::reinstrument_contract::<T>(&original_code, schedule)
|
||||
.map_err(|_| <Error<T>>::CodeRejected)?,
|
||||
Some("Contract exceeds limit after re-instrumentation."),
|
||||
);
|
||||
prefab_module.instruction_weights_version = schedule.instruction_weights.version;
|
||||
<CodeStorage<T>>::insert(&prefab_module.code_hash, &*prefab_module);
|
||||
Ok(original_code_len as u32)
|
||||
|
||||
@@ -35,11 +35,7 @@ use crate::{
|
||||
Schedule,
|
||||
};
|
||||
use codec::{Decode, Encode, MaxEncodedLen};
|
||||
use frame_support::{
|
||||
dispatch::{DispatchError, DispatchResult},
|
||||
ensure,
|
||||
traits::Get,
|
||||
};
|
||||
use frame_support::dispatch::{DispatchError, DispatchResult};
|
||||
use sp_core::crypto::UncheckedFrom;
|
||||
use sp_sandbox::{SandboxEnvironmentBuilder, SandboxInstance, SandboxMemory};
|
||||
use sp_std::prelude::*;
|
||||
@@ -134,12 +130,6 @@ where
|
||||
schedule,
|
||||
owner,
|
||||
)?;
|
||||
// When instrumenting a new code we apply a stricter limit than enforced by the
|
||||
// `RelaxedCodeVec` in order to leave some headroom for reinstrumentation.
|
||||
ensure!(
|
||||
module.code.len() as u32 <= T::MaxCodeLen::get(),
|
||||
(<Error<T>>::CodeTooLarge.into(), ""),
|
||||
);
|
||||
Ok(module)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user