|
| 1 | +use solana_program::instruction::Instruction; |
| 2 | +use solana_program::system_program; |
| 3 | +use solana_program::{instruction::AccountMeta, pubkey::Pubkey}; |
| 4 | + |
| 5 | +use crate::args::{CommitBumps, CommitFinalizeArgs}; |
| 6 | +use crate::discriminator::DlpDiscriminator; |
| 7 | +use crate::pod_view::PodView; |
| 8 | +use crate::{ |
| 9 | + delegation_metadata_seeds_from_delegated_account, |
| 10 | + delegation_record_seeds_from_delegated_account, program_config_seeds_from_program_id, |
| 11 | + total_size_budget, validator_fees_vault_seeds_from_validator, AccountSizeClass, |
| 12 | + DLP_PROGRAM_DATA_SIZE_CLASS, |
| 13 | +}; |
| 14 | + |
| 15 | +/// Builds a commit state from buffer instruction. |
| 16 | +/// See [crate::processor::process_commit_diff_from_buffer] for docs. |
| 17 | +pub fn commit_finalize_from_buffer( |
| 18 | + validator: Pubkey, |
| 19 | + delegated_account: Pubkey, |
| 20 | + delegated_account_owner: Pubkey, |
| 21 | + data_buffer: Pubkey, |
| 22 | + commit_args: &mut CommitFinalizeArgs, |
| 23 | +) -> (Instruction, super::CommitPDAs) { |
| 24 | + let delegation_record = Pubkey::find_program_address( |
| 25 | + delegation_record_seeds_from_delegated_account!(delegated_account), |
| 26 | + &crate::id(), |
| 27 | + ); |
| 28 | + |
| 29 | + let validator_fees_vault = Pubkey::find_program_address( |
| 30 | + validator_fees_vault_seeds_from_validator!(validator), |
| 31 | + &crate::id(), |
| 32 | + ); |
| 33 | + |
| 34 | + let delegation_metadata = Pubkey::find_program_address( |
| 35 | + delegation_metadata_seeds_from_delegated_account!(delegated_account), |
| 36 | + &crate::id(), |
| 37 | + ); |
| 38 | + |
| 39 | + let program_config = Pubkey::find_program_address( |
| 40 | + program_config_seeds_from_program_id!(delegated_account_owner), |
| 41 | + &crate::id(), |
| 42 | + ); |
| 43 | + |
| 44 | + // save the bumps in the args |
| 45 | + commit_args.bumps = CommitBumps { |
| 46 | + delegation_record: delegation_record.1, |
| 47 | + delegation_metadata: delegation_metadata.1, |
| 48 | + validator_fees_vault: validator_fees_vault.1, |
| 49 | + program_config: program_config.1, |
| 50 | + }; |
| 51 | + |
| 52 | + ( |
| 53 | + Instruction { |
| 54 | + program_id: crate::id(), |
| 55 | + accounts: vec![ |
| 56 | + AccountMeta::new_readonly(validator, true), |
| 57 | + AccountMeta::new(delegated_account, false), |
| 58 | + AccountMeta::new_readonly(delegation_record.0, false), |
| 59 | + AccountMeta::new(delegation_metadata.0, false), |
| 60 | + AccountMeta::new_readonly(data_buffer, false), |
| 61 | + AccountMeta::new_readonly(validator_fees_vault.0, false), |
| 62 | + AccountMeta::new_readonly(program_config.0, false), |
| 63 | + AccountMeta::new_readonly(system_program::id(), false), |
| 64 | + ], |
| 65 | + data: [ |
| 66 | + DlpDiscriminator::CommitFinalizeFromBuffer.to_vec(), |
| 67 | + commit_args.to_bytes(), |
| 68 | + ] |
| 69 | + .concat(), |
| 70 | + }, |
| 71 | + super::CommitPDAs { |
| 72 | + delegation_record: delegation_record.0, |
| 73 | + delegation_metadata: delegation_metadata.0, |
| 74 | + validator_fees_vault: validator_fees_vault.0, |
| 75 | + program_config: program_config.0, |
| 76 | + }, |
| 77 | + ) |
| 78 | +} |
| 79 | + |
| 80 | +/// |
| 81 | +/// Returns accounts-data-size budget for commit_diff_from_buffer instruction. |
| 82 | +/// |
| 83 | +/// This value can be used with ComputeBudgetInstruction::SetLoadedAccountsDataSizeLimit |
| 84 | +/// |
| 85 | +pub fn commit_finalize_from_buffer_size_budget(delegated_account: AccountSizeClass) -> u32 { |
| 86 | + total_size_budget(&[ |
| 87 | + DLP_PROGRAM_DATA_SIZE_CLASS, |
| 88 | + AccountSizeClass::Tiny, // validator |
| 89 | + delegated_account, // delegated_account |
| 90 | + AccountSizeClass::Tiny, // delegation_record_pda |
| 91 | + AccountSizeClass::Tiny, // delegation_metadata_pda |
| 92 | + delegated_account, // data_buffer |
| 93 | + AccountSizeClass::Tiny, // validator_fees_vault_pda |
| 94 | + AccountSizeClass::Tiny, // program_config_pda |
| 95 | + AccountSizeClass::Tiny, // system_program |
| 96 | + ]) |
| 97 | +} |
0 commit comments