1#[cfg(test)]
21mod tests;
22
23use super::{Nested, Root, State};
24use crate::{
25 storage::ContractInfo, BalanceOf, Config, ExecConfig, ExecOrigin as Origin, HoldReason, Pallet,
26 StorageDeposit as Deposit,
27};
28use alloc::vec::Vec;
29use core::{marker::PhantomData, mem};
30use frame_support::{traits::Get, DefaultNoBound, RuntimeDebugNoBound};
31use sp_runtime::{
32 traits::{Saturating, Zero},
33 DispatchError, FixedPointNumber, FixedU128,
34};
35
36#[cfg(test)]
37use num_traits::Bounded;
38
39pub type DepositOf<T> = Deposit<BalanceOf<T>>;
41
42pub type Meter<T> = RawMeter<T, ReservingExt, Root>;
44
45pub type GenericMeter<T, S> = RawMeter<T, ReservingExt, S>;
49
50pub trait Ext<T: Config> {
54 fn charge(
61 origin: &T::AccountId,
62 contract: &T::AccountId,
63 amount: &DepositOf<T>,
64 exec_config: &ExecConfig<T>,
65 ) -> Result<(), DispatchError>;
66}
67
68pub enum ReservingExt {}
72
73#[derive(DefaultNoBound, RuntimeDebugNoBound)]
75pub struct RawMeter<T: Config, E, S: State> {
76 pub(crate) limit: Option<BalanceOf<T>>,
78 total_deposit: DepositOf<T>,
80 own_contribution: Contribution<T>,
83 charges: Vec<Charge<T>>,
88 max_charged: BalanceOf<T>,
91 pub(crate) is_root: bool,
95 _phantom: PhantomData<(E, S)>,
97}
98
99#[derive(Default, RuntimeDebugNoBound)]
101pub struct Diff {
102 pub bytes_added: u32,
104 pub bytes_removed: u32,
106 pub items_added: u32,
108 pub items_removed: u32,
110}
111
112impl Diff {
113 pub fn update_contract<T: Config>(&self, info: Option<&mut ContractInfo<T>>) -> DepositOf<T> {
122 let per_byte = T::DepositPerByte::get();
123 let per_item = T::DepositPerChildTrieItem::get();
124 let bytes_added = self.bytes_added.saturating_sub(self.bytes_removed);
125 let items_added = self.items_added.saturating_sub(self.items_removed);
126 let mut bytes_deposit = Deposit::Charge(per_byte.saturating_mul((bytes_added).into()));
127 let mut items_deposit = Deposit::Charge(per_item.saturating_mul((items_added).into()));
128
129 let info = if let Some(info) = info {
131 info
132 } else {
133 return bytes_deposit.saturating_add(&items_deposit)
134 };
135
136 let bytes_removed = self.bytes_removed.saturating_sub(self.bytes_added);
138 let items_removed = self.items_removed.saturating_sub(self.items_added);
139 let ratio = FixedU128::checked_from_rational(bytes_removed, info.storage_bytes)
140 .unwrap_or_default()
141 .min(FixedU128::from_u32(1));
142 bytes_deposit = bytes_deposit
143 .saturating_add(&Deposit::Refund(ratio.saturating_mul_int(info.storage_byte_deposit)));
144 let ratio = FixedU128::checked_from_rational(items_removed, info.storage_items)
145 .unwrap_or_default()
146 .min(FixedU128::from_u32(1));
147 items_deposit = items_deposit
148 .saturating_add(&Deposit::Refund(ratio.saturating_mul_int(info.storage_item_deposit)));
149
150 info.storage_bytes =
152 info.storage_bytes.saturating_add(bytes_added).saturating_sub(bytes_removed);
153 info.storage_items =
154 info.storage_items.saturating_add(items_added).saturating_sub(items_removed);
155 match &bytes_deposit {
156 Deposit::Charge(amount) =>
157 info.storage_byte_deposit = info.storage_byte_deposit.saturating_add(*amount),
158 Deposit::Refund(amount) =>
159 info.storage_byte_deposit = info.storage_byte_deposit.saturating_sub(*amount),
160 }
161 match &items_deposit {
162 Deposit::Charge(amount) =>
163 info.storage_item_deposit = info.storage_item_deposit.saturating_add(*amount),
164 Deposit::Refund(amount) =>
165 info.storage_item_deposit = info.storage_item_deposit.saturating_sub(*amount),
166 }
167
168 bytes_deposit.saturating_add(&items_deposit)
169 }
170}
171
172impl Diff {
173 fn saturating_add(&self, rhs: &Self) -> Self {
174 Self {
175 bytes_added: self.bytes_added.saturating_add(rhs.bytes_added),
176 bytes_removed: self.bytes_removed.saturating_add(rhs.bytes_removed),
177 items_added: self.items_added.saturating_add(rhs.items_added),
178 items_removed: self.items_removed.saturating_add(rhs.items_removed),
179 }
180 }
181}
182
183#[derive(RuntimeDebugNoBound, Clone, PartialEq, Eq)]
185pub enum ContractState<T: Config> {
186 Alive { amount: DepositOf<T> },
187 Terminated,
188}
189
190#[derive(RuntimeDebugNoBound, Clone)]
200struct Charge<T: Config> {
201 contract: T::AccountId,
202 state: ContractState<T>,
203}
204
205#[derive(RuntimeDebugNoBound)]
207enum Contribution<T: Config> {
208 Alive(Diff),
210 Checked(DepositOf<T>),
213}
214
215impl<T: Config> Contribution<T> {
216 fn update_contract(&self, info: Option<&mut ContractInfo<T>>) -> DepositOf<T> {
218 match self {
219 Self::Alive(diff) => diff.update_contract::<T>(info),
220 Self::Checked(deposit) => deposit.clone(),
221 }
222 }
223}
224
225impl<T: Config> Default for Contribution<T> {
226 fn default() -> Self {
227 Self::Alive(Default::default())
228 }
229}
230
231impl<T, E, S> RawMeter<T, E, S>
233where
234 T: Config,
235 E: Ext<T>,
236 S: State,
237{
238 pub fn nested(&self, mut limit: Option<BalanceOf<T>>) -> RawMeter<T, E, Nested> {
244 if let (Some(new_limit), Some(old_limit)) = (limit, self.limit) {
245 limit = Some(new_limit.min(old_limit));
246 }
247
248 RawMeter { limit, ..Default::default() }
249 }
250
251 pub fn reset(&mut self) {
253 self.own_contribution = Default::default();
254 self.total_deposit = Default::default();
255 self.charges = Default::default();
256 self.max_charged = Default::default();
257 }
258
259 pub fn absorb(
275 &mut self,
276 absorbed: RawMeter<T, E, Nested>,
277 contract: &T::AccountId,
278 info: Option<&mut ContractInfo<T>>,
279 ) {
280 self.max_charged = self
288 .max_charged
289 .max(self.consumed().saturating_add(&absorbed.max_charged()).charge_or_zero());
290
291 let own_deposit = absorbed.own_contribution.update_contract(info);
292 self.total_deposit = self
293 .total_deposit
294 .saturating_add(&absorbed.total_deposit)
295 .saturating_add(&own_deposit);
296 self.charges.extend_from_slice(&absorbed.charges);
297
298 self.recalulculate_max_charged();
299
300 if !own_deposit.is_zero() {
301 self.charges.push(Charge {
302 contract: contract.clone(),
303 state: ContractState::Alive { amount: own_deposit },
304 });
305 }
306 }
307
308 pub fn absorb_only_max_charged(&mut self, absorbed: RawMeter<T, E, Nested>) {
316 self.max_charged = self
317 .max_charged
318 .max(self.consumed().saturating_add(&absorbed.max_charged()).charge_or_zero());
319 }
320
321 pub fn record_charge(&mut self, amount: &DepositOf<T>) {
326 self.total_deposit = self.total_deposit.saturating_add(amount);
327 self.recalulculate_max_charged();
328 }
329
330 pub fn consumed(&self) -> DepositOf<T> {
335 self.total_deposit.saturating_add(&self.own_contribution.update_contract(None))
336 }
337
338 pub fn max_charged(&self) -> DepositOf<T> {
340 Deposit::Charge(self.max_charged)
341 }
342
343 fn recalulculate_max_charged(&mut self) {
345 self.max_charged = self.max_charged.max(self.consumed().charge_or_zero());
346 }
347
348 #[cfg(test)]
352 pub fn available(&self) -> BalanceOf<T> {
353 self.consumed()
354 .available(&self.limit.unwrap_or(BalanceOf::<T>::max_value()))
355 .unwrap_or_default()
356 }
357}
358
359impl<T, E> RawMeter<T, E, Root>
361where
362 T: Config,
363 E: Ext<T>,
364{
365 pub fn new(limit: Option<BalanceOf<T>>) -> Self {
370 Self {
371 limit,
372 is_root: true,
373 own_contribution: Contribution::Checked(Default::default()),
374 ..Default::default()
375 }
376 }
377
378 pub fn execute_postponed_deposits(
382 &mut self,
383 origin: &Origin<T>,
384 exec_config: &ExecConfig<T>,
385 ) -> Result<DepositOf<T>, DispatchError> {
386 let origin = match origin {
388 Origin::Root => return Ok(Deposit::Charge(Zero::zero())),
389 Origin::Signed(o) => o,
390 };
391
392 self.charges.sort_by(|a, b| a.contract.cmp(&b.contract));
394 self.charges = {
395 let mut coalesced: Vec<Charge<T>> = Vec::with_capacity(self.charges.len());
396 for mut ch in mem::take(&mut self.charges) {
397 if let Some(last) = coalesced.last_mut() {
398 if last.contract == ch.contract {
399 match (&mut last.state, &mut ch.state) {
400 (
401 ContractState::Alive { amount: last_amount },
402 ContractState::Alive { amount: ch_amount },
403 ) => {
404 *last_amount = last_amount.saturating_add(&ch_amount);
405 },
406 (ContractState::Alive { amount }, ContractState::Terminated) |
407 (ContractState::Terminated, ContractState::Alive { amount }) => {
408 self.total_deposit = self.total_deposit.saturating_sub(&amount);
410 last.state = ContractState::Terminated;
411 },
412 (ContractState::Terminated, ContractState::Terminated) =>
413 debug_assert!(
414 false,
415 "We never emit two terminates for the same contract."
416 ),
417 }
418 continue;
419 }
420 }
421 coalesced.push(ch);
422 }
423 coalesced
424 };
425
426 for charge in self.charges.iter() {
428 if let ContractState::Alive { amount: amount @ Deposit::Refund(_) } = &charge.state {
429 E::charge(origin, &charge.contract, amount, exec_config)?;
430 }
431 }
432 for charge in self.charges.iter() {
433 if let ContractState::Alive { amount: amount @ Deposit::Charge(_) } = &charge.state {
434 E::charge(origin, &charge.contract, amount, exec_config)?;
435 }
436 }
437
438 Ok(self.total_deposit.clone())
439 }
440
441 pub fn terminate(&mut self, contract: T::AccountId, refunded: BalanceOf<T>) {
446 self.total_deposit = self.total_deposit.saturating_add(&Deposit::Refund(refunded));
447 self.charges.push(Charge { contract, state: ContractState::Terminated });
448
449 }
452}
453
454impl<T: Config, E: Ext<T>> RawMeter<T, E, Nested> {
456 pub fn charge(&mut self, diff: &Diff) {
458 match &mut self.own_contribution {
459 Contribution::Alive(own) => {
460 *own = own.saturating_add(diff);
461 self.recalulculate_max_charged();
462 },
463 _ => panic!("Charge is never called after termination; qed"),
464 };
465 }
466
467 pub fn charge_deposit(&mut self, contract: T::AccountId, amount: DepositOf<T>) {
477 self.record_charge(&amount);
479 self.charges.push(Charge { contract, state: ContractState::Alive { amount } });
480 }
481
482 pub fn finalize_own_contributions(&mut self, info: Option<&mut ContractInfo<T>>) {
484 let deposit = self.own_contribution.update_contract(info);
485 self.own_contribution = Contribution::Checked(deposit);
486
487 }
490}
491
492impl<T: Config> Ext<T> for ReservingExt {
493 fn charge(
494 origin: &T::AccountId,
495 contract: &T::AccountId,
496 amount: &DepositOf<T>,
497 exec_config: &ExecConfig<T>,
498 ) -> Result<(), DispatchError> {
499 match amount {
500 Deposit::Charge(amount) | Deposit::Refund(amount) if amount.is_zero() => (),
501 Deposit::Charge(amount) => {
502 <Pallet<T>>::charge_deposit(
503 Some(HoldReason::StorageDepositReserve),
504 origin,
505 contract,
506 *amount,
507 exec_config,
508 )?;
509 },
510 Deposit::Refund(amount) => {
511 <Pallet<T>>::refund_deposit(
512 HoldReason::StorageDepositReserve,
513 contract,
514 origin,
515 *amount,
516 Some(exec_config),
517 )?;
518 },
519 }
520 Ok(())
521 }
522}