Refactor: move sysvar cache to new module
This commit is contained in:
		
				
					committed by
					
						
						Trent Nelson
					
				
			
			
				
	
			
			
			
						parent
						
							b27333e52d
						
					
				
				
					commit
					7171c95bdd
				
			@@ -19,6 +19,8 @@ log = "0.4.14"
 | 
			
		||||
num-derive = { version = "0.3" }
 | 
			
		||||
num-traits = { version = "0.2" }
 | 
			
		||||
serde = { version = "1.0.129", features = ["derive", "rc"] }
 | 
			
		||||
solana-frozen-abi = { path = "../frozen-abi", version = "=1.10.0" }
 | 
			
		||||
solana-frozen-abi-macro = { path = "../frozen-abi/macro", version = "=1.10.0" }
 | 
			
		||||
solana-measure = { path = "../measure", version = "=1.10.0" }
 | 
			
		||||
solana-sdk = { path = "../sdk", version = "=1.10.0" }
 | 
			
		||||
thiserror = "1.0"
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,7 @@ use {
 | 
			
		||||
        log_collector::LogCollector,
 | 
			
		||||
        native_loader::NativeLoader,
 | 
			
		||||
        pre_account::PreAccount,
 | 
			
		||||
        sysvar_cache::SysvarCache,
 | 
			
		||||
        timings::{ExecuteDetailsTimings, ExecuteTimings},
 | 
			
		||||
    },
 | 
			
		||||
    solana_measure::measure::Measure,
 | 
			
		||||
@@ -28,7 +29,7 @@ use {
 | 
			
		||||
        sysvar::Sysvar,
 | 
			
		||||
        transaction_context::{InstructionAccount, TransactionAccount, TransactionContext},
 | 
			
		||||
    },
 | 
			
		||||
    std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc, sync::Arc},
 | 
			
		||||
    std::{borrow::Cow, cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc, sync::Arc},
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
pub type ProcessInstructionWithContext =
 | 
			
		||||
@@ -185,7 +186,7 @@ pub struct InvokeContext<'a> {
 | 
			
		||||
    rent: Rent,
 | 
			
		||||
    pre_accounts: Vec<PreAccount>,
 | 
			
		||||
    builtin_programs: &'a [BuiltinProgram],
 | 
			
		||||
    pub sysvars: &'a [(Pubkey, Vec<u8>)],
 | 
			
		||||
    pub sysvar_cache: Cow<'a, SysvarCache>,
 | 
			
		||||
    log_collector: Option<Rc<RefCell<LogCollector>>>,
 | 
			
		||||
    compute_budget: ComputeBudget,
 | 
			
		||||
    current_compute_budget: ComputeBudget,
 | 
			
		||||
@@ -205,7 +206,7 @@ impl<'a> InvokeContext<'a> {
 | 
			
		||||
        transaction_context: &'a mut TransactionContext,
 | 
			
		||||
        rent: Rent,
 | 
			
		||||
        builtin_programs: &'a [BuiltinProgram],
 | 
			
		||||
        sysvars: &'a [(Pubkey, Vec<u8>)],
 | 
			
		||||
        sysvar_cache: Cow<'a, SysvarCache>,
 | 
			
		||||
        log_collector: Option<Rc<RefCell<LogCollector>>>,
 | 
			
		||||
        compute_budget: ComputeBudget,
 | 
			
		||||
        executors: Rc<RefCell<Executors>>,
 | 
			
		||||
@@ -221,7 +222,7 @@ impl<'a> InvokeContext<'a> {
 | 
			
		||||
            rent,
 | 
			
		||||
            pre_accounts: Vec::new(),
 | 
			
		||||
            builtin_programs,
 | 
			
		||||
            sysvars,
 | 
			
		||||
            sysvar_cache,
 | 
			
		||||
            log_collector,
 | 
			
		||||
            current_compute_budget: compute_budget,
 | 
			
		||||
            compute_budget,
 | 
			
		||||
@@ -244,7 +245,7 @@ impl<'a> InvokeContext<'a> {
 | 
			
		||||
            transaction_context,
 | 
			
		||||
            Rent::default(),
 | 
			
		||||
            builtin_programs,
 | 
			
		||||
            &[],
 | 
			
		||||
            Cow::Owned(SysvarCache::default()),
 | 
			
		||||
            Some(LogCollector::new_ref()),
 | 
			
		||||
            ComputeBudget::default(),
 | 
			
		||||
            Rc::new(RefCell::new(Executors::default())),
 | 
			
		||||
@@ -989,7 +990,7 @@ impl<'a> InvokeContext<'a> {
 | 
			
		||||
 | 
			
		||||
    /// Get the value of a sysvar by its id
 | 
			
		||||
    pub fn get_sysvar<T: Sysvar>(&self, id: &Pubkey) -> Result<T, InstructionError> {
 | 
			
		||||
        self.sysvars
 | 
			
		||||
        self.sysvar_cache
 | 
			
		||||
            .iter()
 | 
			
		||||
            .find_map(|(key, data)| {
 | 
			
		||||
                if id == key {
 | 
			
		||||
@@ -1081,7 +1082,7 @@ pub fn mock_process_instruction_with_sysvars(
 | 
			
		||||
    transaction_accounts: Vec<TransactionAccount>,
 | 
			
		||||
    instruction_accounts: Vec<AccountMeta>,
 | 
			
		||||
    expected_result: Result<(), InstructionError>,
 | 
			
		||||
    sysvars: &[(Pubkey, Vec<u8>)],
 | 
			
		||||
    sysvar_cache: &SysvarCache,
 | 
			
		||||
    process_instruction: ProcessInstructionWithContext,
 | 
			
		||||
) -> Vec<AccountSharedData> {
 | 
			
		||||
    program_indices.insert(0, transaction_accounts.len());
 | 
			
		||||
@@ -1096,7 +1097,7 @@ pub fn mock_process_instruction_with_sysvars(
 | 
			
		||||
        ComputeBudget::default().max_invoke_depth.saturating_add(1),
 | 
			
		||||
    );
 | 
			
		||||
    let mut invoke_context = InvokeContext::new_mock(&mut transaction_context, &[]);
 | 
			
		||||
    invoke_context.sysvars = sysvars;
 | 
			
		||||
    invoke_context.sysvar_cache = Cow::Borrowed(sysvar_cache);
 | 
			
		||||
    let result = invoke_context
 | 
			
		||||
        .push(
 | 
			
		||||
            &preparation.instruction_accounts,
 | 
			
		||||
@@ -1127,7 +1128,7 @@ pub fn mock_process_instruction(
 | 
			
		||||
        transaction_accounts,
 | 
			
		||||
        instruction_accounts,
 | 
			
		||||
        expected_result,
 | 
			
		||||
        &[],
 | 
			
		||||
        &SysvarCache::default(),
 | 
			
		||||
        process_instruction,
 | 
			
		||||
    )
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -8,4 +8,5 @@ pub mod native_loader;
 | 
			
		||||
pub mod neon_evm_program;
 | 
			
		||||
pub mod pre_account;
 | 
			
		||||
pub mod stable_log;
 | 
			
		||||
pub mod sysvar_cache;
 | 
			
		||||
pub mod timings;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										39
									
								
								program-runtime/src/sysvar_cache.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								program-runtime/src/sysvar_cache.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,39 @@
 | 
			
		||||
use {
 | 
			
		||||
    solana_sdk::{
 | 
			
		||||
        account::{AccountSharedData, ReadableAccount},
 | 
			
		||||
        pubkey::Pubkey,
 | 
			
		||||
    },
 | 
			
		||||
    std::ops::Deref,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#[cfg(RUSTC_WITH_SPECIALIZATION)]
 | 
			
		||||
impl ::solana_frozen_abi::abi_example::AbiExample for SysvarCache {
 | 
			
		||||
    fn example() -> Self {
 | 
			
		||||
        // SysvarCache is not Serialize so just rely on Default.
 | 
			
		||||
        SysvarCache::default()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Default, Clone, Debug)]
 | 
			
		||||
pub struct SysvarCache(Vec<(Pubkey, Vec<u8>)>);
 | 
			
		||||
 | 
			
		||||
impl Deref for SysvarCache {
 | 
			
		||||
    type Target = Vec<(Pubkey, Vec<u8>)>;
 | 
			
		||||
    fn deref(&self) -> &Self::Target {
 | 
			
		||||
        &self.0
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl SysvarCache {
 | 
			
		||||
    pub fn push_entry(&mut self, pubkey: Pubkey, data: Vec<u8>) {
 | 
			
		||||
        self.0.push((pubkey, data));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn update_entry(&mut self, pubkey: &Pubkey, new_account: &AccountSharedData) {
 | 
			
		||||
        if let Some(position) = self.iter().position(|(id, _data)| id == pubkey) {
 | 
			
		||||
            self.0[position].1 = new_account.data().to_vec();
 | 
			
		||||
        } else {
 | 
			
		||||
            self.0.push((*pubkey, new_account.data().to_vec()));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user