1#[cfg(test)]
21mod tests;
22
23use super::{Nested, Root, State};
24use crate::{
25 BalanceOf, Config, ExecConfig, ExecOrigin as Origin, HoldReason, Pallet,
26 StorageDeposit as Deposit, storage::ContractInfo,
27};
28use alloc::vec::Vec;
29use core::{marker::PhantomData, mem};
30use frame_support::{DebugNoBound, DefaultNoBound, traits::Get};
31use sp_runtime::{
32 DispatchError, FixedPointNumber, FixedU128,
33 traits::{Saturating, Zero},
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, DebugNoBound)]
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, DebugNoBound)]
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 },
159 Deposit::Refund(amount) => {
160 info.storage_byte_deposit = info.storage_byte_deposit.saturating_sub(*amount)
161 },
162 }
163 match &items_deposit {
164 Deposit::Charge(amount) => {
165 info.storage_item_deposit = info.storage_item_deposit.saturating_add(*amount)
166 },
167 Deposit::Refund(amount) => {
168 info.storage_item_deposit = info.storage_item_deposit.saturating_sub(*amount)
169 },
170 }
171
172 bytes_deposit.saturating_add(&items_deposit)
173 }
174}
175
176impl Diff {
177 fn saturating_add(&self, rhs: &Self) -> Self {
178 Self {
179 bytes_added: self.bytes_added.saturating_add(rhs.bytes_added),
180 bytes_removed: self.bytes_removed.saturating_add(rhs.bytes_removed),
181 items_added: self.items_added.saturating_add(rhs.items_added),
182 items_removed: self.items_removed.saturating_add(rhs.items_removed),
183 }
184 }
185}
186
187#[derive(DebugNoBound, Clone, PartialEq, Eq)]
189pub enum ContractState<T: Config> {
190 Alive { amount: DepositOf<T> },
191 Terminated,
192}
193
194#[derive(DebugNoBound, Clone)]
204struct Charge<T: Config> {
205 contract: T::AccountId,
206 state: ContractState<T>,
207}
208
209#[derive(DebugNoBound)]
211enum Contribution<T: Config> {
212 Alive(Diff),
214 Checked(DepositOf<T>),
217}
218
219impl<T: Config> Contribution<T> {
220 fn update_contract(&self, info: Option<&mut ContractInfo<T>>) -> DepositOf<T> {
222 match self {
223 Self::Alive(diff) => diff.update_contract::<T>(info),
224 Self::Checked(deposit) => deposit.clone(),
225 }
226 }
227}
228
229impl<T: Config> Default for Contribution<T> {
230 fn default() -> Self {
231 Self::Alive(Default::default())
232 }
233}
234
235impl<T, E, S> RawMeter<T, E, S>
237where
238 T: Config,
239 E: Ext<T>,
240 S: State,
241{
242 pub fn nested(&self, mut limit: Option<BalanceOf<T>>) -> RawMeter<T, E, Nested> {
248 if let (Some(new_limit), Some(old_limit)) = (limit, self.limit) {
249 limit = Some(new_limit.min(old_limit));
250 }
251
252 RawMeter { limit, ..Default::default() }
253 }
254
255 pub fn reset(&mut self) {
257 self.own_contribution = Default::default();
258 self.total_deposit = Default::default();
259 self.charges = Default::default();
260 self.max_charged = Default::default();
261 }
262
263 pub fn absorb(
279 &mut self,
280 absorbed: RawMeter<T, E, Nested>,
281 contract: &T::AccountId,
282 info: Option<&mut ContractInfo<T>>,
283 ) {
284 self.max_charged = self
292 .max_charged
293 .max(self.consumed().saturating_add(&absorbed.max_charged()).charge_or_zero());
294
295 let own_deposit = absorbed.own_contribution.update_contract(info);
296 self.total_deposit = self
297 .total_deposit
298 .saturating_add(&absorbed.total_deposit)
299 .saturating_add(&own_deposit);
300 self.charges.extend_from_slice(&absorbed.charges);
301
302 self.recalulculate_max_charged();
303
304 if !own_deposit.is_zero() {
305 self.charges.push(Charge {
306 contract: contract.clone(),
307 state: ContractState::Alive { amount: own_deposit },
308 });
309 }
310 }
311
312 pub fn absorb_only_max_charged(&mut self, absorbed: RawMeter<T, E, Nested>) {
320 self.max_charged = self
321 .max_charged
322 .max(self.consumed().saturating_add(&absorbed.max_charged()).charge_or_zero());
323 }
324
325 pub fn record_charge(&mut self, amount: &DepositOf<T>) {
330 self.total_deposit = self.total_deposit.saturating_add(amount);
331 self.recalulculate_max_charged();
332 }
333
334 pub fn consumed(&self) -> DepositOf<T> {
339 self.total_deposit.saturating_add(&self.own_contribution.update_contract(None))
340 }
341
342 pub fn max_charged(&self) -> DepositOf<T> {
344 Deposit::Charge(self.max_charged)
345 }
346
347 fn recalulculate_max_charged(&mut self) {
349 self.max_charged = self.max_charged.max(self.consumed().charge_or_zero());
350 }
351
352 #[cfg(test)]
356 pub fn available(&self) -> BalanceOf<T> {
357 self.consumed()
358 .available(&self.limit.unwrap_or(BalanceOf::<T>::max_value()))
359 .unwrap_or_default()
360 }
361}
362
363impl<T, E> RawMeter<T, E, Root>
365where
366 T: Config,
367 E: Ext<T>,
368{
369 pub fn new(limit: Option<BalanceOf<T>>) -> Self {
374 Self {
375 limit,
376 is_root: true,
377 own_contribution: Contribution::Checked(Default::default()),
378 ..Default::default()
379 }
380 }
381
382 pub fn execute_postponed_deposits(
386 &mut self,
387 origin: &Origin<T>,
388 exec_config: &ExecConfig<T>,
389 ) -> Result<DepositOf<T>, DispatchError> {
390 let origin = match origin {
392 Origin::Root => return Ok(Deposit::Charge(Zero::zero())),
393 Origin::Signed(o) => o,
394 };
395
396 self.charges.sort_by(|a, b| a.contract.cmp(&b.contract));
398 self.charges = {
399 let mut coalesced: Vec<Charge<T>> = Vec::with_capacity(self.charges.len());
400 for mut ch in mem::take(&mut self.charges) {
401 if let Some(last) = coalesced.last_mut() {
402 if last.contract == ch.contract {
403 match (&mut last.state, &mut ch.state) {
404 (
405 ContractState::Alive { amount: last_amount },
406 ContractState::Alive { amount: ch_amount },
407 ) => {
408 *last_amount = last_amount.saturating_add(&ch_amount);
409 },
410 (ContractState::Alive { amount }, ContractState::Terminated) |
411 (ContractState::Terminated, ContractState::Alive { amount }) => {
412 self.total_deposit = self.total_deposit.saturating_sub(&amount);
414 last.state = ContractState::Terminated;
415 },
416 (ContractState::Terminated, ContractState::Terminated) => {
417 debug_assert!(
418 false,
419 "We never emit two terminates for the same contract."
420 )
421 },
422 }
423 continue;
424 }
425 }
426 coalesced.push(ch);
427 }
428 coalesced
429 };
430
431 for charge in self.charges.iter() {
433 if let ContractState::Alive { amount: amount @ Deposit::Refund(_) } = &charge.state {
434 E::charge(origin, &charge.contract, amount, exec_config)?;
435 }
436 }
437 for charge in self.charges.iter() {
438 if let ContractState::Alive { amount: amount @ Deposit::Charge(_) } = &charge.state {
439 E::charge(origin, &charge.contract, amount, exec_config)?;
440 }
441 }
442
443 Ok(self.total_deposit.clone())
444 }
445
446 pub fn terminate(&mut self, contract: T::AccountId, refunded: BalanceOf<T>) {
451 self.total_deposit = self.total_deposit.saturating_add(&Deposit::Refund(refunded));
452 self.charges.push(Charge { contract, state: ContractState::Terminated });
453
454 }
457}
458
459impl<T: Config, E: Ext<T>> RawMeter<T, E, Nested> {
461 pub fn charge(&mut self, diff: &Diff) {
463 match &mut self.own_contribution {
464 Contribution::Alive(own) => {
465 *own = own.saturating_add(diff);
466 self.recalulculate_max_charged();
467 },
468 _ => panic!("Charge is never called after termination; qed"),
469 };
470 }
471
472 pub fn charge_deposit(&mut self, contract: T::AccountId, amount: DepositOf<T>) {
482 self.record_charge(&amount);
484 self.charges.push(Charge { contract, state: ContractState::Alive { amount } });
485 }
486
487 pub fn finalize_own_contributions(&mut self, info: Option<&mut ContractInfo<T>>) {
489 let deposit = self.own_contribution.update_contract(info);
490 self.own_contribution = Contribution::Checked(deposit);
491
492 }
495
496 pub fn apply_pending_changes_to_contract(&self, info: &mut ContractInfo<T>) {
504 if let Contribution::Alive(diff) = &self.own_contribution {
505 let _ = diff.update_contract::<T>(Some(info));
509 }
510 }
511}
512
513impl<T: Config> Ext<T> for ReservingExt {
514 fn charge(
515 origin: &T::AccountId,
516 contract: &T::AccountId,
517 amount: &DepositOf<T>,
518 exec_config: &ExecConfig<T>,
519 ) -> Result<(), DispatchError> {
520 match amount {
521 Deposit::Charge(amount) | Deposit::Refund(amount) if amount.is_zero() => (),
522 Deposit::Charge(amount) => {
523 <Pallet<T>>::charge_deposit(
524 HoldReason::StorageDepositReserve,
525 origin,
526 contract,
527 *amount,
528 exec_config,
529 )?;
530 },
531 Deposit::Refund(amount) => {
532 <Pallet<T>>::refund_deposit(
533 HoldReason::StorageDepositReserve,
534 contract,
535 exec_config.funds(origin),
536 *amount,
537 )?;
538 },
539 }
540 Ok(())
541 }
542}