1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use frame_support::parameter_types;
use frame_support::weights::constants::{
BlockExecutionWeight, ExtrinsicBaseWeight, WEIGHT_REF_TIME_PER_SECOND,
};
use frame_support::weights::{Weight, WeightMeter};
use frame_system::limits;
use sp_arithmetic::Perbill;
use sp_std::marker::PhantomData;
use crate::primitives::Balance;
use frame_support::dispatch::{
DispatchClass, DispatchErrorWithPostInfo, DispatchResultWithPostInfo, Pays,
};
use sp_runtime::DispatchError;
pub mod constants {
use crate::{balance, Balance};
use frame_support::weights::Weight;
pub const EXTRINSIC_FIXED_WEIGHT: Weight = Weight::from_parts(100_000_000, 0);
pub const SMALL_FEE: Balance = balance!(0.0007);
pub const BIG_FEE: Balance = balance!(0.007);
}
pub struct PresetWeightInfo<T>(PhantomData<T>);
const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
const MAXIMUM_BLOCK_WEIGHT: Weight =
Weight::from_parts(2u64 * WEIGHT_REF_TIME_PER_SECOND, u64::MAX);
pub const ON_INITIALIZE_RATIO: Perbill = Perbill::from_perthousand(20);
parameter_types! {
pub BlockWeights: limits::BlockWeights = limits::BlockWeights::builder()
.base_block(BlockExecutionWeight::get())
.for_class(DispatchClass::all(), |weights| {
weights.base_extrinsic = ExtrinsicBaseWeight::get();
})
.for_class(DispatchClass::Normal, |weights| {
weights.max_total = Some(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT);
})
.for_class(DispatchClass::Operational, |weights| {
weights.max_total = Some(MAXIMUM_BLOCK_WEIGHT);
weights.reserved = Some(
MAXIMUM_BLOCK_WEIGHT - NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT,
);
})
.avg_block_initialization(ON_INITIALIZE_RATIO)
.build_or_panic();
pub BlockLength: limits::BlockLength =
limits::BlockLength::max_with_normal_ratio(7 * 1024 * 1024, NORMAL_DISPATCH_RATIO);
pub const TransactionByteFee: Balance = 0;
}
#[inline(always)]
pub fn pays_no_with_maybe_weight<E: Into<DispatchError>>(
result: Result<Option<Weight>, E>,
) -> DispatchResultWithPostInfo {
result
.map_err(|e| DispatchErrorWithPostInfo {
post_info: Pays::No.into(),
error: e.into(),
})
.map(|weight| (weight, Pays::No).into())
}
#[inline(always)]
pub fn pays_no<T, E: Into<DispatchError>>(result: Result<T, E>) -> DispatchResultWithPostInfo {
pays_no_with_maybe_weight(result.map(|_| None))
}
#[inline(always)]
pub fn err_pays_no(err: impl Into<DispatchError>) -> DispatchErrorWithPostInfo {
DispatchErrorWithPostInfo {
post_info: Pays::No.into(),
error: err.into(),
}
}
pub fn check_accrue_n(meter: &mut WeightMeter, w: Weight, max_n: u64) -> u64 {
let n = {
let weight_left = meter.remaining();
let n_ref_time = weight_left
.ref_time()
.checked_div(w.ref_time())
.unwrap_or(u64::MAX);
let n_proof_size = weight_left
.proof_size()
.checked_div(w.proof_size())
.unwrap_or(u64::MAX);
let max_possible_n = n_ref_time.min(n_proof_size);
max_possible_n.min(max_n)
};
let to_consume = w.saturating_mul(n);
meter.defensive_saturating_accrue(to_consume);
n
}
#[cfg(test)]
mod tests {
use super::check_accrue_n;
#[test]
fn test_check_accrue_n_works() {
let mut weight_counter = frame_support::weights::WeightMeter::from_limit(100.into());
assert_eq!(
check_accrue_n(&mut weight_counter, 10.into(), 10),
10,
"Should accrue within limits"
);
assert_eq!(weight_counter.remaining(), 0.into());
let mut weight_counter = frame_support::weights::WeightMeter::from_limit(100.into());
assert_eq!(
check_accrue_n(&mut weight_counter, 11.into(), 10),
9,
"Should partially accrue"
);
assert_eq!(weight_counter.remaining(), 1.into()); let mut weight_counter = frame_support::weights::WeightMeter::from_limit(100.into());
assert_eq!(
check_accrue_n(&mut weight_counter, 101.into(), 1),
0,
"Should restrict even a single consumption exceeding limits"
);
assert_eq!(weight_counter.remaining(), 100.into());
assert_eq!(
check_accrue_n(&mut weight_counter, 1.into(), 0),
0,
"Should work with 0 maximum consumptions"
);
assert_eq!(weight_counter.remaining(), 100.into());
assert_eq!(
check_accrue_n(&mut weight_counter, 0.into(), u64::MAX),
u64::MAX,
"0 weight should be allowed to be consumed infinitely (max_int is reasonably high for weight)"
);
assert_eq!(weight_counter.remaining(), 100.into());
}
}