Skip to main content

drive/util/batch/drive_op_batch/
system.rs

1use crate::drive::Drive;
2use crate::error::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    /// We want to add credits to the system.
27    AddToSystemCredits {
28        /// The amount of credits we are seeking to add
29        amount: Credits,
30    },
31    /// We want to remove credits from the system.
32    RemoveFromSystemCredits {
33        /// The amount of credits we are seeking to remove
34        amount: Credits,
35    },
36    /// Adding a used asset lock, if it is only partially used the asset_lock_value
37    /// will have a non 0 remaining_credit_value
38    AddUsedAssetLock {
39        /// The asset lock outpoint that should be added
40        asset_lock_outpoint: Bytes36,
41        /// The asset lock value, both initial and remaining
42        asset_lock_value: AssetLockValue,
43    },
44}
45
46impl DriveLowLevelOperationConverter for SystemOperationType {
47    fn into_low_level_drive_operations(
48        self,
49        drive: &Drive,
50        estimated_costs_only_with_layer_info: &mut Option<
51            HashMap<KeyInfoPath, EstimatedLayerInformation>,
52        >,
53        _block_info: &BlockInfo,
54        transaction: TransactionArg,
55        platform_version: &PlatformVersion,
56    ) -> Result<Vec<LowLevelDriveOperation>, Error> {
57        match self {
58            SystemOperationType::AddToSystemCredits { amount } => drive
59                .add_to_system_credits_operations(
60                    amount,
61                    estimated_costs_only_with_layer_info,
62                    transaction,
63                    platform_version,
64                ),
65            SystemOperationType::RemoveFromSystemCredits { amount } => drive
66                .remove_from_system_credits_operations(
67                    amount,
68                    estimated_costs_only_with_layer_info,
69                    transaction,
70                    platform_version,
71                ),
72            SystemOperationType::AddUsedAssetLock {
73                asset_lock_outpoint,
74                asset_lock_value,
75            } => drive.add_asset_lock_outpoint_operations(
76                &asset_lock_outpoint,
77                asset_lock_value,
78                estimated_costs_only_with_layer_info,
79                platform_version,
80            ),
81        }
82    }
83}