From d07f07e6b2cd953b4fb21ac69c4a97c19c30c28e Mon Sep 17 00:00:00 2001 From: Tyera Eulberg Date: Wed, 3 Jul 2024 13:29:11 -0600 Subject: [PATCH] Duplicate code to limit pub and dependency complexity --- programs/system/src/system_processor.rs | 52 ++++++++++++++++++------- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/programs/system/src/system_processor.rs b/programs/system/src/system_processor.rs index d455fb84ba5c12..1174702222114f 100644 --- a/programs/system/src/system_processor.rs +++ b/programs/system/src/system_processor.rs @@ -542,7 +542,10 @@ declare_process_instruction!(Entrypoint, DEFAULT_COMPUTE_UNITS, |invoke_context| mod tests { #[allow(deprecated)] use solana_sdk::{ - account::{self, Account, AccountSharedData, ReadableAccount}, + account::{ + self, create_account_shared_data_with_fields, to_account, Account, AccountSharedData, + ReadableAccount, DUMMY_INHERITABLE_ACCOUNT_FIELDS, + }, fee_calculator::FeeCalculator, hash::{hash, Hash}, instruction::{AccountMeta, Instruction, InstructionError}, @@ -552,8 +555,12 @@ mod tests { Data as NonceData, DurableNonce, State as NonceState, Versions as NonceVersions, }, }, - nonce_account, recent_blockhashes_account, system_instruction, system_program, - sysvar::{self, recent_blockhashes::IterItem, rent::Rent}, + nonce_account, system_instruction, system_program, + sysvar::{ + self, + recent_blockhashes::{IntoIterSorted, IterItem, RecentBlockhashes, MAX_ENTRIES}, + rent::Rent, + }, }; use { super::*, @@ -562,6 +569,7 @@ mod tests { solana_program_runtime::{ invoke_context::mock_process_instruction, with_mock_invoke_context, }, + std::collections::BinaryHeap, }; impl From for Address { @@ -595,11 +603,30 @@ mod tests { fn create_default_account() -> AccountSharedData { AccountSharedData::new(0, 0, &Pubkey::new_unique()) } + #[allow(deprecated)] + fn create_recent_blockhashes_account_for_test<'a, I>( + recent_blockhash_iter: I, + ) -> AccountSharedData + where + I: IntoIterator>, + { + let mut account = create_account_shared_data_with_fields::( + &RecentBlockhashes::default(), + DUMMY_INHERITABLE_ACCOUNT_FIELDS, + ); + let sorted = BinaryHeap::from_iter(recent_blockhash_iter); + let sorted_iter = IntoIterSorted::new(sorted); + let recent_blockhash_iter = sorted_iter.take(MAX_ENTRIES); + let recent_blockhashes: RecentBlockhashes = recent_blockhash_iter.collect(); + to_account(&recent_blockhashes, &mut account); + account + } fn create_default_recent_blockhashes_account() -> AccountSharedData { #[allow(deprecated)] - recent_blockhashes_account::create_account_with_data_for_test( - vec![IterItem(0u64, &Hash::default(), 0); sysvar::recent_blockhashes::MAX_ENTRIES], - ) + create_recent_blockhashes_account_for_test(vec![ + IterItem(0u64, &Hash::default(), 0); + sysvar::recent_blockhashes::MAX_ENTRIES + ]) } fn create_default_rent_account() -> AccountSharedData { account::create_account_shared_data_for_test(&Rent::free()) @@ -1552,9 +1579,10 @@ mod tests { let blockhash = hash(&serialize(&0).unwrap()); #[allow(deprecated)] let new_recent_blockhashes_account = - solana_sdk::recent_blockhashes_account::create_account_with_data_for_test( - vec![IterItem(0u64, &blockhash, 0); sysvar::recent_blockhashes::MAX_ENTRIES], - ); + create_recent_blockhashes_account_for_test(vec![ + IterItem(0u64, &blockhash, 0); + sysvar::recent_blockhashes::MAX_ENTRIES + ]); mock_process_instruction( &system_program::id(), Vec::new(), @@ -1837,8 +1865,7 @@ mod tests { #[allow(deprecated)] let blockhash_id = sysvar::recent_blockhashes::id(); #[allow(deprecated)] - let new_recent_blockhashes_account = - solana_sdk::recent_blockhashes_account::create_account_with_data_for_test(vec![]); + let new_recent_blockhashes_account = create_recent_blockhashes_account_for_test(vec![]); process_instruction( &serialize(&SystemInstruction::InitializeNonceAccount(nonce_address)).unwrap(), vec![ @@ -1900,8 +1927,7 @@ mod tests { Ok(()), ); #[allow(deprecated)] - let new_recent_blockhashes_account = - solana_sdk::recent_blockhashes_account::create_account_with_data_for_test(vec![]); + let new_recent_blockhashes_account = create_recent_blockhashes_account_for_test(vec![]); mock_process_instruction( &system_program::id(), Vec::new(),