Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 6aaa031

Browse files
atheiascjones
andauthored
Make Schedule fields public to allow for customization (#8924)
* Make `Schedule` fields public for customization * Fix doc typo Co-authored-by: Andrew Jones <[email protected]> Co-authored-by: Andrew Jones <[email protected]>
1 parent ac277db commit 6aaa031

File tree

2 files changed

+49
-27
lines changed

2 files changed

+49
-27
lines changed

frame/contracts/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ pub mod weights;
9696
#[cfg(test)]
9797
mod tests;
9898

99-
pub use crate::{pallet::*, schedule::Schedule, exec::Frame};
99+
pub use crate::{
100+
pallet::*,
101+
schedule::{Schedule, Limits, InstructionWeights, HostFnWeights},
102+
exec::Frame,
103+
};
100104
use crate::{
101105
gas::GasMeter,
102106
exec::{Stack as ExecStack, Executable},

frame/contracts/src/schedule.rs

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,39 +40,48 @@ pub const INSTR_BENCHMARK_BATCH_SIZE: u32 = 1_000;
4040

4141
/// Definition of the cost schedule and other parameterizations for the wasm vm.
4242
///
43-
/// Its fields are private to the crate in order to allow addition of new contract
44-
/// callable functions without bumping to a new major version. The supplied [`Config::Schedule`]
45-
/// should rely on public functions of this type.
43+
/// Its [`Default`] implementation is the designated way to initialize this type. It uses
44+
/// the benchmarked information supplied by [`Config::WeightInfo`]. All of its fields are
45+
/// public and can therefore be modified. For example in order to change some of the limits
46+
/// and set a custom instruction weight version the following code could be used:
47+
/// ```rust
48+
/// use pallet_contracts::{Schedule, Limits, InstructionWeights, Config};
49+
///
50+
/// fn create_schedule<T: Config>() -> Schedule<T> {
51+
/// Schedule {
52+
/// limits: Limits {
53+
/// globals: 3,
54+
/// parameters: 3,
55+
/// memory_pages: 16,
56+
/// table_size: 3,
57+
/// br_table_size: 3,
58+
/// .. Default::default()
59+
/// },
60+
/// instruction_weights: InstructionWeights {
61+
/// version: 5,
62+
/// .. Default::default()
63+
/// },
64+
/// .. Default::default()
65+
/// }
66+
/// }
67+
/// ```
68+
///
69+
/// # Note
70+
///
71+
/// Please make sure to bump the [`InstructionWeights::version`] whenever substantial
72+
/// changes are made to its values.
4673
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
4774
#[cfg_attr(feature = "std", serde(bound(serialize = "", deserialize = "")))]
4875
#[derive(Clone, Encode, Decode, PartialEq, Eq, ScheduleDebug, DefaultNoBound)]
4976
pub struct Schedule<T: Config> {
5077
/// Describes the upper limits on various metrics.
51-
pub(crate) limits: Limits,
78+
pub limits: Limits,
5279

5380
/// The weights for individual wasm instructions.
54-
pub(crate) instruction_weights: InstructionWeights<T>,
81+
pub instruction_weights: InstructionWeights<T>,
5582

5683
/// The weights for each imported function a contract is allowed to call.
57-
pub(crate) host_fn_weights: HostFnWeights<T>,
58-
}
59-
60-
impl<T: Config> Schedule<T> {
61-
/// Set the version of the instruction weights.
62-
///
63-
/// # Note
64-
///
65-
/// Should be incremented whenever any instruction weight is changed. The
66-
/// reason is that changes to instruction weights require a re-instrumentation
67-
/// in order to apply the changes to an already deployed code. The re-instrumentation
68-
/// is triggered by comparing the version of the current schedule with the the code was
69-
/// instrumented with. Changes usually happen when pallet_contracts is re-benchmarked.
70-
///
71-
/// Changes to other parts of the schedule should not increment the version in
72-
/// order to avoid unnecessary re-instrumentations.
73-
pub fn set_instruction_weights_version(&mut self, version: u32) {
74-
self.instruction_weights.version = version;
75-
}
84+
pub host_fn_weights: HostFnWeights<T>,
7685
}
7786

7887
/// Describes the upper limits on various metrics.
@@ -169,8 +178,17 @@ impl Limits {
169178
pub struct InstructionWeights<T: Config> {
170179
/// Version of the instruction weights.
171180
///
172-
/// See [`Schedule::set_instruction_weights_version`].
173-
pub(crate) version: u32,
181+
/// # Note
182+
///
183+
/// Should be incremented whenever any instruction weight is changed. The
184+
/// reason is that changes to instruction weights require a re-instrumentation
185+
/// in order to apply the changes to an already deployed code. The re-instrumentation
186+
/// is triggered by comparing the version of the current schedule with the version the code was
187+
/// instrumented with. Changes usually happen when pallet_contracts is re-benchmarked.
188+
///
189+
/// Changes to other parts of the schedule should not increment the version in
190+
/// order to avoid unnecessary re-instrumentations.
191+
pub version: u32,
174192
pub i64const: u32,
175193
pub i64load: u32,
176194
pub i64store: u32,

0 commit comments

Comments
 (0)