Skip to main content

drive/util/batch/drive_op_batch/
system.rs

1use crate::drive::Drive;
2use crate::error::{drive::DriveError, Error};
3use crate::fees::op::LowLevelDriveOperation;
4use crate::util::batch::drive_op_batch::DriveLowLevelOperationConverter;
5use dpp::block::block_info::BlockInfo;
6use dpp::fee::Credits;
7use dpp::platform_value::Bytes36;
8
9use dpp::asset_lock::reduced_asset_lock_value::AssetLockValue;
10
11use dpp::version::PlatformVersion;
12use grovedb::batch::KeyInfoPath;
13use grovedb::{EstimatedLayerInformation, TransactionArg};
14use std::collections::HashMap;
15
16/// Operations on the System
17///
18/// `AddToSystemCredits` and `RemoveFromSystemCredits` compute the new total from the one
19/// committed before their batch, and GroveDB keeps only the last write of a key, so a batch
20/// must carry at most one of them: a second would replace the first, and the total would no
21/// longer match the credits in the trees. Every batch does today (at most one per state
22/// transition, one per block for the epoch payout), and `DriveOperation::merge_balance_writes`
23/// does not merge them. A batch that needs two must sum them into one.
24#[derive(Clone, Debug)]
25pub enum SystemOperationType {
26    /// Returns allocated credits to the applying block's processing pool after ordinary
27    /// writes and debt repayment, without creating new system credits.
28    AddToCurrentEpochProcessingPool {
29        /// The amount returned to the pool.
30        amount: Credits,
31    },
32    /// We want to add credits to the system.
33    AddToSystemCredits {
34        /// The amount of credits we are seeking to add
35        amount: Credits,
36    },
37    /// We want to remove credits from the system.
38    RemoveFromSystemCredits {
39        /// The amount of credits we are seeking to remove
40        amount: Credits,
41    },
42    /// Adding a used asset lock, if it is only partially used the asset_lock_value
43    /// will have a non 0 remaining_credit_value
44    AddUsedAssetLock {
45        /// The asset lock outpoint that should be added
46        asset_lock_outpoint: Bytes36,
47        /// The asset lock value, both initial and remaining
48        asset_lock_value: AssetLockValue,
49    },
50}
51
52impl DriveLowLevelOperationConverter for SystemOperationType {
53    fn into_low_level_drive_operations(
54        self,
55        drive: &Drive,
56        estimated_costs_only_with_layer_info: &mut Option<
57            HashMap<KeyInfoPath, EstimatedLayerInformation>,
58        >,
59        _block_info: &BlockInfo,
60        transaction: TransactionArg,
61        platform_version: &PlatformVersion,
62    ) -> Result<Vec<LowLevelDriveOperation>, Error> {
63        match self {
64            SystemOperationType::AddToCurrentEpochProcessingPool { .. } => {
65                Err(Error::Drive(DriveError::NotSupported(
66                    "processing pool credits require apply_drive_operations generation 1",
67                )))
68            }
69            SystemOperationType::AddToSystemCredits { amount } => drive
70                .add_to_system_credits_operations(
71                    amount,
72                    estimated_costs_only_with_layer_info,
73                    transaction,
74                    platform_version,
75                ),
76            SystemOperationType::RemoveFromSystemCredits { amount } => drive
77                .remove_from_system_credits_operations(
78                    amount,
79                    estimated_costs_only_with_layer_info,
80                    transaction,
81                    platform_version,
82                ),
83            SystemOperationType::AddUsedAssetLock {
84                asset_lock_outpoint,
85                asset_lock_value,
86            } => drive.add_asset_lock_outpoint_operations(
87                &asset_lock_outpoint,
88                asset_lock_value,
89                estimated_costs_only_with_layer_info,
90                platform_version,
91            ),
92        }
93    }
94}