|
| 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::{CommitDiffArgsWithoutDiff, SIZE_COMMIT_DIFF_ARGS_WITHOUT_DIFF}; |
| 8 | +use crate::processor::fast::{process_commit_state_internal, CommitStateInternalArgs}; |
| 9 | +use crate::{apply_diff_copy, DiffSet}; |
| 10 | + |
| 11 | +/// Commit diff to a delegated PDA |
| 12 | +/// |
| 13 | +/// Accounts: |
| 14 | +/// |
| 15 | +/// 0: `[signer]` the validator requesting the commit |
| 16 | +/// 1: `[]` the delegated account |
| 17 | +/// 2: `[writable]` the PDA storing the new state |
| 18 | +/// 3: `[writable]` the PDA storing the commit record |
| 19 | +/// 4: `[]` the delegation record |
| 20 | +/// 5: `[writable]` the delegation metadata |
| 21 | +/// 6: `[]` the validator fees vault |
| 22 | +/// 7: `[]` the program config account |
| 23 | +/// 8: `[]` the system program |
| 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 | + if data.len() < SIZE_COMMIT_DIFF_ARGS_WITHOUT_DIFF { |
| 55 | + return Err(ProgramError::InvalidInstructionData); |
| 56 | + } |
| 57 | + |
| 58 | + let (diff, data) = data.split_at(data.len() - SIZE_COMMIT_DIFF_ARGS_WITHOUT_DIFF); |
| 59 | + |
| 60 | + let args = |
| 61 | + CommitDiffArgsWithoutDiff::try_from_slice(data).map_err(|_| ProgramError::BorshIoError)?; |
| 62 | + |
| 63 | + let diffset = DiffSet::try_new_from_borsh_vec(diff)?; |
| 64 | + |
| 65 | + if diffset.segments_count() == 0 { |
| 66 | + log!("WARN: noop; empty diff sent"); |
| 67 | + } |
| 68 | + |
| 69 | + let commit_record_lamports = args.lamports; |
| 70 | + let commit_record_nonce = args.nonce; |
| 71 | + let allow_undelegation = args.allow_undelegation; |
| 72 | + |
| 73 | + // TODO (snawaz): the following approach to apply diff works, but it's not efficient. |
| 74 | + // It is also problematic for larger account as it allocates memory on the heap. |
| 75 | + // It will be fixed in a separate PR. |
| 76 | + let original = unsafe { delegated_account.borrow_data_unchecked() }; |
| 77 | + let changed = apply_diff_copy(original, &diffset)?; |
| 78 | + |
| 79 | + let commit_args = CommitStateInternalArgs { |
| 80 | + commit_state_bytes: &changed, |
| 81 | + commit_record_lamports, |
| 82 | + commit_record_nonce, |
| 83 | + allow_undelegation, |
| 84 | + validator, |
| 85 | + delegated_account, |
| 86 | + commit_state_account, |
| 87 | + commit_record_account, |
| 88 | + delegation_record_account, |
| 89 | + delegation_metadata_account, |
| 90 | + validator_fees_vault, |
| 91 | + program_config_account, |
| 92 | + }; |
| 93 | + |
| 94 | + process_commit_state_internal(commit_args) |
| 95 | +} |
0 commit comments