|
| 1 | +use borsh::BorshDeserialize; |
| 2 | +use pinocchio::{ |
| 3 | + account_info::AccountInfo, program_error::ProgramError, pubkey::Pubkey, ProgramResult, |
| 4 | +}; |
| 5 | +use pinocchio_log::log; |
| 6 | + |
| 7 | +use crate::args::CommitDiffArgs; |
| 8 | +use crate::error::DlpError; |
| 9 | +use crate::processor::fast::{process_commit_state_internal, CommitStateInternalArgs}; |
| 10 | +use crate::{apply_diff_in_place, DiffSet}; |
| 11 | + |
| 12 | +/// Commit diff to a delegated PDA |
| 13 | +/// |
| 14 | +/// Accounts: |
| 15 | +/// |
| 16 | +/// 0: `[signer]` the validator requesting the commit |
| 17 | +/// 1: `[]` the delegated account |
| 18 | +/// 2: `[writable]` the PDA storing the new state |
| 19 | +/// 3: `[writable]` the PDA storing the commit record |
| 20 | +/// 4: `[]` the delegation record |
| 21 | +/// 5: `[writable]` the delegation metadata |
| 22 | +/// 6: `[]` the validator fees vault |
| 23 | +/// 7: `[]` the program config account |
| 24 | +/// |
| 25 | +/// Requirements: |
| 26 | +/// |
| 27 | +/// - The following accounts must be initialized: |
| 28 | +/// - delegation record |
| 29 | +/// - delegation metadata |
| 30 | +/// - validator fees vault |
| 31 | +/// - program config |
| 32 | +/// - The following accounts must be uninitialized: |
| 33 | +/// - commit state |
| 34 | +/// - commit record |
| 35 | +/// - delegated account holds at least the lamports indicated in the delegation record |
| 36 | +/// - account was not committed at a later slot |
| 37 | +/// |
| 38 | +/// Steps: |
| 39 | +/// 1. Check that the pda is delegated |
| 40 | +/// 2. Init a new PDA to store the new state |
| 41 | +/// 3. Copy the new state to the new PDA |
| 42 | +/// 4. Init a new PDA to store the record of the new state commitment |
| 43 | +pub fn process_commit_diff( |
| 44 | + _program_id: &Pubkey, |
| 45 | + accounts: &[AccountInfo], |
| 46 | + data: &[u8], |
| 47 | +) -> ProgramResult { |
| 48 | + let [validator, delegated_account, commit_state_account, commit_record_account, delegation_record_account, delegation_metadata_account, validator_fees_vault, program_config_account, _system_program] = |
| 49 | + accounts |
| 50 | + else { |
| 51 | + return Err(ProgramError::NotEnoughAccountKeys); |
| 52 | + }; |
| 53 | + |
| 54 | + let args = CommitDiffArgs::try_from_slice(data).map_err(|_| ProgramError::BorshIoError)?; |
| 55 | + |
| 56 | + if DiffSet::new(args.diff.as_slice()).num_offset_pairs() == 0 { |
| 57 | + log!("WARN: noop; empty diff sent"); |
| 58 | + } |
| 59 | + |
| 60 | + let commit_record_lamports = args.lamports; |
| 61 | + let commit_record_nonce = args.nonce; |
| 62 | + let allow_undelegation = args.allow_undelegation; |
| 63 | + |
| 64 | + let mut original = unsafe { delegated_account.borrow_data_unchecked() }.to_vec(); |
| 65 | + |
| 66 | + apply_diff_in_place(&mut original, &args.diff) |
| 67 | + .map_err(|_size_changed| DlpError::AccountSizeChangedError)?; |
| 68 | + |
| 69 | + let commit_args = CommitStateInternalArgs { |
| 70 | + commit_state_bytes: &original, |
| 71 | + commit_record_lamports, |
| 72 | + commit_record_nonce, |
| 73 | + allow_undelegation, |
| 74 | + validator, |
| 75 | + delegated_account, |
| 76 | + commit_state_account, |
| 77 | + commit_record_account, |
| 78 | + delegation_record_account, |
| 79 | + delegation_metadata_account, |
| 80 | + validator_fees_vault, |
| 81 | + program_config_account, |
| 82 | + }; |
| 83 | + |
| 84 | + process_commit_state_internal(commit_args) |
| 85 | +} |
0 commit comments