|
| 1 | +use std::ptr::read_unaligned; |
| 2 | + |
| 3 | +use dlp::{ |
| 4 | + pda::validator_fees_vault_pda_from_validator, |
| 5 | + pod_view::PodView, |
| 6 | + v2::{ |
| 7 | + CommitFinalizeArgs, DelegationStateHeader, ValidatedDelegationBindings, |
| 8 | + }, |
| 9 | +}; |
| 10 | +use solana_program::{ |
| 11 | + hash::Hash, native_token::LAMPORTS_PER_SOL, rent::Rent, system_program, |
| 12 | +}; |
| 13 | +use solana_program_test::{ |
| 14 | + BanksClient, BanksTransactionResultWithMetadata, ProgramTest, |
| 15 | +}; |
| 16 | +use solana_sdk::{ |
| 17 | + account::Account, |
| 18 | + compute_budget::ComputeBudgetInstruction, |
| 19 | + signature::{Keypair, Signer}, |
| 20 | + transaction::Transaction, |
| 21 | +}; |
| 22 | + |
| 23 | +use crate::fixtures::{ |
| 24 | + DEFAULT_COMMIT_FREQUENCY_MS, DEFAULT_DELEGATION_SLOT, DELEGATED_PDA_ID, |
| 25 | + DELEGATED_PDA_OWNER_ID, TEST_AUTHORITY, |
| 26 | +}; |
| 27 | + |
| 28 | +mod fixtures; |
| 29 | + |
| 30 | +#[tokio::test] |
| 31 | +async fn test_v2_commit_finalize_inline_resize_perf() { |
| 32 | + // Setup |
| 33 | + const KB: usize = 1024; |
| 34 | + const ACCOUNT_SIZE: usize = 4 * KB; |
| 35 | + |
| 36 | + let (banks, _, authority, blockhash) = |
| 37 | + setup_program_test_env(vec![0; ACCOUNT_SIZE]).await; |
| 38 | + let new_state: Vec<u8> = vec![1; ACCOUNT_SIZE + 0 * KB]; |
| 39 | + |
| 40 | + let new_account_balance = 1_000_000; |
| 41 | + |
| 42 | + let ix = dlp::instruction_builder::v2_commit_finalize_inline_resize( |
| 43 | + authority.pubkey(), |
| 44 | + DELEGATED_PDA_ID, |
| 45 | + &mut CommitFinalizeArgs { |
| 46 | + commit_id: 1, |
| 47 | + allow_undelegation: true.into(), |
| 48 | + data_is_diff: false.into(), |
| 49 | + lamports: new_account_balance, |
| 50 | + reserved_padding: Default::default(), |
| 51 | + }, |
| 52 | + &new_state, |
| 53 | + ); |
| 54 | + let tx = Transaction::new_signed_with_payer( |
| 55 | + &[ |
| 56 | + ComputeBudgetInstruction::set_compute_unit_limit( |
| 57 | + dlp::instruction_builder::MAX_CU_COMMIT_FINALIZE + 150, /* 150 for ComputeBudgetInstruction itself */ |
| 58 | + ), |
| 59 | + ix, |
| 60 | + ], |
| 61 | + Some(&authority.pubkey()), |
| 62 | + &[&authority], |
| 63 | + blockhash, |
| 64 | + ); |
| 65 | + |
| 66 | + // execute CommitFinalize and validate CU performmace |
| 67 | + { |
| 68 | + let BanksTransactionResultWithMetadata { |
| 69 | + result: _, |
| 70 | + metadata, |
| 71 | + } = banks.process_transaction_with_metadata(tx).await.unwrap(); |
| 72 | + |
| 73 | + let metadata = metadata.unwrap(); |
| 74 | + |
| 75 | + assertables::assert_lt!(metadata.compute_units_consumed, 1150); |
| 76 | + |
| 77 | + assert_eq!( |
| 78 | + metadata |
| 79 | + .log_messages |
| 80 | + .iter() |
| 81 | + .filter(|log| !log |
| 82 | + .contains("ComputeBudget111111111111111111111111111111")) |
| 83 | + .count(), |
| 84 | + 3, |
| 85 | + "CommitFinalize must not log anything in OK scenario" |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + let delegated_account = |
| 90 | + banks.get_account(DELEGATED_PDA_ID).await.unwrap().unwrap(); |
| 91 | + let state_size = unsafe { |
| 92 | + read_unaligned(delegated_account.data.as_ptr() as *const u32) |
| 93 | + } as usize; |
| 94 | + assert_eq!( |
| 95 | + delegated_account.data[4 + state_size..].len(), |
| 96 | + new_state.len() |
| 97 | + ); |
| 98 | + assert_eq!(delegated_account.data[4 + state_size..], new_state); |
| 99 | + |
| 100 | + // let delegation_metadata_account = banks |
| 101 | + // .get_account(pdas.delegation_metadata) |
| 102 | + // .await |
| 103 | + // .unwrap() |
| 104 | + // .unwrap(); |
| 105 | + |
| 106 | + // let delegation_metadata = |
| 107 | + // DelegationMetadata::try_from_bytes_with_discriminator( |
| 108 | + // &delegation_metadata_account.data, |
| 109 | + // ) |
| 110 | + // .unwrap(); |
| 111 | + |
| 112 | + // assert_eq!(delegation_metadata.is_undelegatable, true); |
| 113 | +} |
| 114 | + |
| 115 | +async fn setup_program_test_env( |
| 116 | + pda_data: Vec<u8>, |
| 117 | +) -> (BanksClient, Keypair, Keypair, Hash) { |
| 118 | + let mut program_test = ProgramTest::new("dlp", dlp::ID, None); |
| 119 | + program_test.prefer_bpf(true); |
| 120 | + |
| 121 | + let validator_keypair = Keypair::from_bytes(&TEST_AUTHORITY).unwrap(); |
| 122 | + |
| 123 | + program_test.add_account( |
| 124 | + validator_keypair.pubkey(), |
| 125 | + Account { |
| 126 | + lamports: 10 * LAMPORTS_PER_SOL, |
| 127 | + data: vec![], |
| 128 | + owner: system_program::id(), |
| 129 | + executable: false, |
| 130 | + rent_epoch: 0, |
| 131 | + }, |
| 132 | + ); |
| 133 | + |
| 134 | + let validator_fees_vault = |
| 135 | + validator_fees_vault_pda_from_validator(&validator_keypair.pubkey()); |
| 136 | + let state_header = DelegationStateHeader { |
| 137 | + discriminator: DelegationStateHeader::DISCRIMINATOR, |
| 138 | + original_owner: DELEGATED_PDA_OWNER_ID.to_bytes().into(), |
| 139 | + delegation_slot: DEFAULT_DELEGATION_SLOT, |
| 140 | + original_lamports: Rent::default().minimum_balance(500), |
| 141 | + commit_frequency_ms: DEFAULT_COMMIT_FREQUENCY_MS, |
| 142 | + bindings: ValidatedDelegationBindings { |
| 143 | + delegated_account: DELEGATED_PDA_ID.to_bytes().into(), |
| 144 | + validator_as_authority: validator_keypair |
| 145 | + .pubkey() |
| 146 | + .to_bytes() |
| 147 | + .into(), |
| 148 | + validator_fees_vault: validator_fees_vault.to_bytes().into(), |
| 149 | + }, |
| 150 | + last_commit_id: 0, |
| 151 | + rent_payer: validator_keypair.pubkey().to_bytes().into(), |
| 152 | + is_undelegatable: false.into(), |
| 153 | + reserved_padding0: Default::default(), |
| 154 | + }; |
| 155 | + |
| 156 | + // Setup a delegated PDA containing the delegation_state at the end |
| 157 | + program_test.add_account( |
| 158 | + DELEGATED_PDA_ID, |
| 159 | + Account { |
| 160 | + lamports: LAMPORTS_PER_SOL, |
| 161 | + data: { |
| 162 | + let state_buffer = vec![]; |
| 163 | + let state = [state_header.to_bytes(), state_buffer].concat(); |
| 164 | + let state_size = (state.len() as u32).to_le_bytes().to_vec(); |
| 165 | + |
| 166 | + [ |
| 167 | + state_size, // size of the delegation state |
| 168 | + state, // delegation state (appended by Delegate ix) |
| 169 | + pda_data, // delegated pda |
| 170 | + ] |
| 171 | + .concat() |
| 172 | + }, |
| 173 | + owner: dlp::id(), |
| 174 | + executable: false, |
| 175 | + rent_epoch: 0, |
| 176 | + }, |
| 177 | + ); |
| 178 | + |
| 179 | + // Setup the validator fees vault |
| 180 | + program_test.add_account( |
| 181 | + validator_fees_vault, |
| 182 | + Account { |
| 183 | + lamports: LAMPORTS_PER_SOL, |
| 184 | + data: vec![], |
| 185 | + owner: dlp::id(), |
| 186 | + executable: false, |
| 187 | + rent_epoch: 0, |
| 188 | + }, |
| 189 | + ); |
| 190 | + |
| 191 | + let (banks, payer, blockhash) = program_test.start().await; |
| 192 | + (banks, payer, validator_keypair, blockhash) |
| 193 | +} |
0 commit comments