Skip to main content

drive/util/batch/drive_op_batch/
shielded.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::version::PlatformVersion;
7use grovedb::batch::KeyInfoPath;
8use grovedb::{EstimatedLayerInformation, TransactionArg};
9use std::collections::HashMap;
10
11/// Operations on the Shielded Pool
12#[derive(Clone, Debug)]
13pub enum ShieldedPoolOperationType {
14    /// Insert a note into the CommitmentTree (appends cmx to frontier + stores
15    /// cmx||rho||cv_net||encrypted_note as item)
16    InsertNote {
17        /// The 32-byte nullifier (rho) of the spent note in this action, stored alongside
18        /// the ciphertext so light clients can derive Rho for trial decryption
19        nullifier: [u8; 32],
20        /// The 32-byte note commitment (cmx)
21        cmx: [u8; 32],
22        /// The 32-byte value commitment (cv_net), stored unencrypted so a wallet can
23        /// recover the value of an outgoing note via OVK decryption
24        cv_net: [u8; 32],
25        /// The encrypted note payload (216 bytes)
26        encrypted_note: Vec<u8>,
27    },
28    /// Insert nullifiers into the permanent tree (double-spend prevention).
29    InsertNullifiers {
30        /// The 32-byte nullifiers to insert
31        nullifiers: Vec<[u8; 32]>,
32    },
33    /// Update the shielded pool total balance
34    UpdateTotalBalance {
35        /// The new total balance value
36        new_total_balance: u64,
37    },
38}
39
40impl DriveLowLevelOperationConverter for ShieldedPoolOperationType {
41    fn into_low_level_drive_operations(
42        self,
43        drive: &Drive,
44        estimated_costs_only_with_layer_info: &mut Option<
45            HashMap<KeyInfoPath, EstimatedLayerInformation>,
46        >,
47        _block_info: &BlockInfo,
48        _transaction: TransactionArg,
49        platform_version: &PlatformVersion,
50    ) -> Result<Vec<LowLevelDriveOperation>, Error> {
51        if let Some(ref mut estimated_costs) = estimated_costs_only_with_layer_info {
52            Drive::add_estimation_costs_for_shielded_pool_operations(estimated_costs);
53        }
54
55        match self {
56            ShieldedPoolOperationType::InsertNote {
57                nullifier,
58                cmx,
59                cv_net,
60                encrypted_note,
61            } => Drive::insert_note_op(nullifier, cmx, cv_net, encrypted_note, platform_version),
62            ShieldedPoolOperationType::InsertNullifiers { nullifiers } => {
63                drive.insert_nullifiers(&nullifiers, platform_version)
64            }
65            ShieldedPoolOperationType::UpdateTotalBalance { new_total_balance } => {
66                Drive::update_total_balance_op(new_total_balance, platform_version)
67            }
68        }
69    }
70}