Skip to main content

dlp_api/instruction_builder/
commit_diff.rs

1use borsh::to_vec;
2use dlp::{
3    args::CommitDiffArgs,
4    discriminator::DlpDiscriminator,
5    pda::{
6        commit_record_pda_from_delegated_account,
7        commit_state_pda_from_delegated_account,
8        delegation_metadata_pda_from_delegated_account,
9        delegation_record_pda_from_delegated_account,
10        program_config_from_program_id,
11        validator_fees_vault_pda_from_validator,
12    },
13    total_size_budget, AccountSizeClass, DLP_PROGRAM_DATA_SIZE_CLASS,
14};
15use solana_program::{
16    instruction::{AccountMeta, Instruction},
17    pubkey::Pubkey,
18    system_program,
19};
20
21/// Builds a commit state instruction.
22/// See [dlp::processor::fast::process_commit_diff] for docs.
23pub fn commit_diff(
24    validator: Pubkey,
25    delegated_account: Pubkey,
26    delegated_account_owner: Pubkey,
27    commit_args: CommitDiffArgs,
28) -> Instruction {
29    let commit_args = to_vec(&commit_args).unwrap();
30    let delegation_record_pda =
31        delegation_record_pda_from_delegated_account(&delegated_account);
32    let commit_state_pda =
33        commit_state_pda_from_delegated_account(&delegated_account);
34    let commit_record_pda =
35        commit_record_pda_from_delegated_account(&delegated_account);
36    let validator_fees_vault_pda =
37        validator_fees_vault_pda_from_validator(&validator);
38    let delegation_metadata_pda =
39        delegation_metadata_pda_from_delegated_account(&delegated_account);
40    let program_config_pda =
41        program_config_from_program_id(&delegated_account_owner);
42    Instruction {
43        program_id: dlp::id(),
44        accounts: vec![
45            AccountMeta::new(validator, true),
46            AccountMeta::new_readonly(delegated_account, false),
47            AccountMeta::new(commit_state_pda, false),
48            AccountMeta::new(commit_record_pda, false),
49            AccountMeta::new_readonly(delegation_record_pda, false),
50            AccountMeta::new(delegation_metadata_pda, false),
51            AccountMeta::new_readonly(validator_fees_vault_pda, false),
52            AccountMeta::new_readonly(program_config_pda, false),
53            AccountMeta::new_readonly(system_program::id(), false),
54        ],
55        data: [DlpDiscriminator::CommitDiff.to_vec(), commit_args].concat(),
56    }
57}
58
59///
60/// Returns accounts-data-size budget for commit_diff instruction.
61///
62/// This value can be used with ComputeBudgetInstruction::SetLoadedAccountsDataSizeLimit
63///
64pub fn commit_diff_size_budget(delegated_account: AccountSizeClass) -> u32 {
65    total_size_budget(&[
66        DLP_PROGRAM_DATA_SIZE_CLASS,
67        AccountSizeClass::Tiny, // validator
68        delegated_account,      // delegated_account
69        delegated_account,      // commit_state_pda
70        AccountSizeClass::Tiny, // commit_record_pda
71        AccountSizeClass::Tiny, // delegation_record_pda
72        AccountSizeClass::Tiny, // delegation_metadata_pda
73        AccountSizeClass::Tiny, // validator_fees_vault_pda
74        AccountSizeClass::Tiny, // program_config_pda
75        AccountSizeClass::Tiny, // system_program
76    ])
77}