|
| 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::CommitFinalizeFromBufferArgs; |
| 8 | +use crate::processor::fast::{ |
| 9 | + commit_finalize_internal::{process_commit_finalize_internal, CommitFinalizeInternalArgs}, |
| 10 | + NewState, |
| 11 | +}; |
| 12 | +use crate::{require_n_accounts, DiffSet}; |
| 13 | + |
| 14 | +/// Commit a new state of a delegated PDA |
| 15 | +/// |
| 16 | +/// Accounts: |
| 17 | +/// |
| 18 | +/// 0: `[signer]` the validator requesting the commit |
| 19 | +/// 1: `[]` the delegated account |
| 20 | +/// 2: `[writable]` the PDA storing the new state |
| 21 | +/// 3: `[writable]` the PDA storing the commit record |
| 22 | +/// 4: `[]` the delegation record |
| 23 | +/// 5: `[writable]` the delegation metadata |
| 24 | +/// 6: `[]` the validator fees vault |
| 25 | +/// 7: `[]` the program config account |
| 26 | +/// |
| 27 | +/// Requirements: |
| 28 | +/// |
| 29 | +/// - delegation record is initialized |
| 30 | +/// - delegation metadata is initialized |
| 31 | +/// - validator fees vault is initialized |
| 32 | +/// - program config is initialized |
| 33 | +/// - commit state is uninitialized |
| 34 | +/// - commit record is uninitialized |
| 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_finalize_from_buffer( |
| 44 | + _program_id: &Pubkey, |
| 45 | + accounts: &[AccountInfo], |
| 46 | + data: &[u8], |
| 47 | +) -> ProgramResult { |
| 48 | + let [ |
| 49 | + validator, // force multi-line |
| 50 | + delegated_account, |
| 51 | + delegation_record_account, |
| 52 | + delegation_metadata_account, |
| 53 | + data_account, // full bytes or diff |
| 54 | + validator_fees_vault, |
| 55 | + program_config_account, |
| 56 | + _system_program, |
| 57 | + ] = require_n_accounts!(accounts, 8); |
| 58 | + |
| 59 | + let args = CommitFinalizeFromBufferArgs::try_from_slice(data) |
| 60 | + .map_err(|_| ProgramError::BorshIoError)?; |
| 61 | + |
| 62 | + let data = data_account.try_borrow_data()?; |
| 63 | + let commit_args = CommitFinalizeInternalArgs { |
| 64 | + new_state: match args.data_is_diff { |
| 65 | + 0 => NewState::FullBytes(&data), |
| 66 | + 1 => { |
| 67 | + let diffset = DiffSet::try_new(data.as_ref())?; |
| 68 | + if diffset.segments_count() == 0 { |
| 69 | + log!("WARN: noop; empty diff sent"); |
| 70 | + } |
| 71 | + NewState::Diff(diffset) |
| 72 | + } |
| 73 | + _ => return Err(ProgramError::InvalidInstructionData), |
| 74 | + }, |
| 75 | + commit_record_nonce: args.nonce, |
| 76 | + allow_undelegation: args.allow_undelegation == 1, |
| 77 | + validator, |
| 78 | + delegated_account, |
| 79 | + delegation_record_account, |
| 80 | + delegation_metadata_account, |
| 81 | + validator_fees_vault, |
| 82 | + program_config_account, |
| 83 | + }; |
| 84 | + |
| 85 | + process_commit_finalize_internal(commit_args) |
| 86 | +} |
0 commit comments