Refactor: move sysvar cache to new module
This commit is contained in:
parent
b27333e52d
commit
7171c95bdd
|
@ -5514,6 +5514,8 @@ dependencies = [
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"rustc_version 0.4.0",
|
"rustc_version 0.4.0",
|
||||||
"serde",
|
"serde",
|
||||||
|
"solana-frozen-abi 1.10.0",
|
||||||
|
"solana-frozen-abi-macro 1.10.0",
|
||||||
"solana-logger 1.10.0",
|
"solana-logger 1.10.0",
|
||||||
"solana-measure",
|
"solana-measure",
|
||||||
"solana-sdk",
|
"solana-sdk",
|
||||||
|
|
|
@ -19,6 +19,8 @@ log = "0.4.14"
|
||||||
num-derive = { version = "0.3" }
|
num-derive = { version = "0.3" }
|
||||||
num-traits = { version = "0.2" }
|
num-traits = { version = "0.2" }
|
||||||
serde = { version = "1.0.129", features = ["derive", "rc"] }
|
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-measure = { path = "../measure", version = "=1.10.0" }
|
||||||
solana-sdk = { path = "../sdk", version = "=1.10.0" }
|
solana-sdk = { path = "../sdk", version = "=1.10.0" }
|
||||||
thiserror = "1.0"
|
thiserror = "1.0"
|
||||||
|
|
|
@ -6,6 +6,7 @@ use {
|
||||||
log_collector::LogCollector,
|
log_collector::LogCollector,
|
||||||
native_loader::NativeLoader,
|
native_loader::NativeLoader,
|
||||||
pre_account::PreAccount,
|
pre_account::PreAccount,
|
||||||
|
sysvar_cache::SysvarCache,
|
||||||
timings::{ExecuteDetailsTimings, ExecuteTimings},
|
timings::{ExecuteDetailsTimings, ExecuteTimings},
|
||||||
},
|
},
|
||||||
solana_measure::measure::Measure,
|
solana_measure::measure::Measure,
|
||||||
|
@ -28,7 +29,7 @@ use {
|
||||||
sysvar::Sysvar,
|
sysvar::Sysvar,
|
||||||
transaction_context::{InstructionAccount, TransactionAccount, TransactionContext},
|
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 =
|
pub type ProcessInstructionWithContext =
|
||||||
|
@ -185,7 +186,7 @@ pub struct InvokeContext<'a> {
|
||||||
rent: Rent,
|
rent: Rent,
|
||||||
pre_accounts: Vec<PreAccount>,
|
pre_accounts: Vec<PreAccount>,
|
||||||
builtin_programs: &'a [BuiltinProgram],
|
builtin_programs: &'a [BuiltinProgram],
|
||||||
pub sysvars: &'a [(Pubkey, Vec<u8>)],
|
pub sysvar_cache: Cow<'a, SysvarCache>,
|
||||||
log_collector: Option<Rc<RefCell<LogCollector>>>,
|
log_collector: Option<Rc<RefCell<LogCollector>>>,
|
||||||
compute_budget: ComputeBudget,
|
compute_budget: ComputeBudget,
|
||||||
current_compute_budget: ComputeBudget,
|
current_compute_budget: ComputeBudget,
|
||||||
|
@ -205,7 +206,7 @@ impl<'a> InvokeContext<'a> {
|
||||||
transaction_context: &'a mut TransactionContext,
|
transaction_context: &'a mut TransactionContext,
|
||||||
rent: Rent,
|
rent: Rent,
|
||||||
builtin_programs: &'a [BuiltinProgram],
|
builtin_programs: &'a [BuiltinProgram],
|
||||||
sysvars: &'a [(Pubkey, Vec<u8>)],
|
sysvar_cache: Cow<'a, SysvarCache>,
|
||||||
log_collector: Option<Rc<RefCell<LogCollector>>>,
|
log_collector: Option<Rc<RefCell<LogCollector>>>,
|
||||||
compute_budget: ComputeBudget,
|
compute_budget: ComputeBudget,
|
||||||
executors: Rc<RefCell<Executors>>,
|
executors: Rc<RefCell<Executors>>,
|
||||||
|
@ -221,7 +222,7 @@ impl<'a> InvokeContext<'a> {
|
||||||
rent,
|
rent,
|
||||||
pre_accounts: Vec::new(),
|
pre_accounts: Vec::new(),
|
||||||
builtin_programs,
|
builtin_programs,
|
||||||
sysvars,
|
sysvar_cache,
|
||||||
log_collector,
|
log_collector,
|
||||||
current_compute_budget: compute_budget,
|
current_compute_budget: compute_budget,
|
||||||
compute_budget,
|
compute_budget,
|
||||||
|
@ -244,7 +245,7 @@ impl<'a> InvokeContext<'a> {
|
||||||
transaction_context,
|
transaction_context,
|
||||||
Rent::default(),
|
Rent::default(),
|
||||||
builtin_programs,
|
builtin_programs,
|
||||||
&[],
|
Cow::Owned(SysvarCache::default()),
|
||||||
Some(LogCollector::new_ref()),
|
Some(LogCollector::new_ref()),
|
||||||
ComputeBudget::default(),
|
ComputeBudget::default(),
|
||||||
Rc::new(RefCell::new(Executors::default())),
|
Rc::new(RefCell::new(Executors::default())),
|
||||||
|
@ -989,7 +990,7 @@ impl<'a> InvokeContext<'a> {
|
||||||
|
|
||||||
/// Get the value of a sysvar by its id
|
/// Get the value of a sysvar by its id
|
||||||
pub fn get_sysvar<T: Sysvar>(&self, id: &Pubkey) -> Result<T, InstructionError> {
|
pub fn get_sysvar<T: Sysvar>(&self, id: &Pubkey) -> Result<T, InstructionError> {
|
||||||
self.sysvars
|
self.sysvar_cache
|
||||||
.iter()
|
.iter()
|
||||||
.find_map(|(key, data)| {
|
.find_map(|(key, data)| {
|
||||||
if id == key {
|
if id == key {
|
||||||
|
@ -1081,7 +1082,7 @@ pub fn mock_process_instruction_with_sysvars(
|
||||||
transaction_accounts: Vec<TransactionAccount>,
|
transaction_accounts: Vec<TransactionAccount>,
|
||||||
instruction_accounts: Vec<AccountMeta>,
|
instruction_accounts: Vec<AccountMeta>,
|
||||||
expected_result: Result<(), InstructionError>,
|
expected_result: Result<(), InstructionError>,
|
||||||
sysvars: &[(Pubkey, Vec<u8>)],
|
sysvar_cache: &SysvarCache,
|
||||||
process_instruction: ProcessInstructionWithContext,
|
process_instruction: ProcessInstructionWithContext,
|
||||||
) -> Vec<AccountSharedData> {
|
) -> Vec<AccountSharedData> {
|
||||||
program_indices.insert(0, transaction_accounts.len());
|
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),
|
ComputeBudget::default().max_invoke_depth.saturating_add(1),
|
||||||
);
|
);
|
||||||
let mut invoke_context = InvokeContext::new_mock(&mut transaction_context, &[]);
|
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
|
let result = invoke_context
|
||||||
.push(
|
.push(
|
||||||
&preparation.instruction_accounts,
|
&preparation.instruction_accounts,
|
||||||
|
@ -1127,7 +1128,7 @@ pub fn mock_process_instruction(
|
||||||
transaction_accounts,
|
transaction_accounts,
|
||||||
instruction_accounts,
|
instruction_accounts,
|
||||||
expected_result,
|
expected_result,
|
||||||
&[],
|
&SysvarCache::default(),
|
||||||
process_instruction,
|
process_instruction,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,4 +8,5 @@ pub mod native_loader;
|
||||||
pub mod neon_evm_program;
|
pub mod neon_evm_program;
|
||||||
pub mod pre_account;
|
pub mod pre_account;
|
||||||
pub mod stable_log;
|
pub mod stable_log;
|
||||||
|
pub mod sysvar_cache;
|
||||||
pub mod timings;
|
pub mod timings;
|
||||||
|
|
|
@ -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()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -3405,6 +3405,8 @@ dependencies = [
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"rustc_version 0.4.0",
|
"rustc_version 0.4.0",
|
||||||
"serde",
|
"serde",
|
||||||
|
"solana-frozen-abi 1.10.0",
|
||||||
|
"solana-frozen-abi-macro 1.10.0",
|
||||||
"solana-measure",
|
"solana-measure",
|
||||||
"solana-sdk",
|
"solana-sdk",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
|
|
|
@ -2921,7 +2921,7 @@ impl<'a, 'b> SyscallObject<BpfError> for SyscallLogData<'a, 'b> {
|
||||||
mod tests {
|
mod tests {
|
||||||
use {
|
use {
|
||||||
super::*,
|
super::*,
|
||||||
solana_program_runtime::invoke_context::InvokeContext,
|
solana_program_runtime::{invoke_context::InvokeContext, sysvar_cache::SysvarCache},
|
||||||
solana_rbpf::{
|
solana_rbpf::{
|
||||||
ebpf::HOST_ALIGN, memory_region::MemoryRegion, user_error::UserError, vm::Config,
|
ebpf::HOST_ALIGN, memory_region::MemoryRegion, user_error::UserError, vm::Config,
|
||||||
},
|
},
|
||||||
|
@ -2929,7 +2929,7 @@ mod tests {
|
||||||
account::AccountSharedData, bpf_loader, fee_calculator::FeeCalculator, hash::hashv,
|
account::AccountSharedData, bpf_loader, fee_calculator::FeeCalculator, hash::hashv,
|
||||||
transaction_context::TransactionContext,
|
transaction_context::TransactionContext,
|
||||||
},
|
},
|
||||||
std::str::FromStr,
|
std::{borrow::Cow, str::FromStr},
|
||||||
};
|
};
|
||||||
|
|
||||||
macro_rules! assert_access_violation {
|
macro_rules! assert_access_violation {
|
||||||
|
@ -3792,8 +3792,6 @@ mod tests {
|
||||||
leader_schedule_epoch: 4,
|
leader_schedule_epoch: 4,
|
||||||
unix_timestamp: 5,
|
unix_timestamp: 5,
|
||||||
};
|
};
|
||||||
let mut data_clock = vec![];
|
|
||||||
bincode::serialize_into(&mut data_clock, &src_clock).unwrap();
|
|
||||||
let src_epochschedule = EpochSchedule {
|
let src_epochschedule = EpochSchedule {
|
||||||
slots_per_epoch: 1,
|
slots_per_epoch: 1,
|
||||||
leader_schedule_slot_offset: 2,
|
leader_schedule_slot_offset: 2,
|
||||||
|
@ -3801,35 +3799,33 @@ mod tests {
|
||||||
first_normal_epoch: 3,
|
first_normal_epoch: 3,
|
||||||
first_normal_slot: 4,
|
first_normal_slot: 4,
|
||||||
};
|
};
|
||||||
let mut data_epochschedule = vec![];
|
|
||||||
bincode::serialize_into(&mut data_epochschedule, &src_epochschedule).unwrap();
|
|
||||||
let src_fees = Fees {
|
let src_fees = Fees {
|
||||||
fee_calculator: FeeCalculator {
|
fee_calculator: FeeCalculator {
|
||||||
lamports_per_signature: 1,
|
lamports_per_signature: 1,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
let mut data_fees = vec![];
|
|
||||||
bincode::serialize_into(&mut data_fees, &src_fees).unwrap();
|
|
||||||
let src_rent = Rent {
|
let src_rent = Rent {
|
||||||
lamports_per_byte_year: 1,
|
lamports_per_byte_year: 1,
|
||||||
exemption_threshold: 2.0,
|
exemption_threshold: 2.0,
|
||||||
burn_percent: 3,
|
burn_percent: 3,
|
||||||
};
|
};
|
||||||
let mut data_rent = vec![];
|
|
||||||
bincode::serialize_into(&mut data_rent, &src_rent).unwrap();
|
let mut sysvar_cache = SysvarCache::default();
|
||||||
let sysvars = [
|
sysvar_cache.push_entry(sysvar::clock::id(), bincode::serialize(&src_clock).unwrap());
|
||||||
(sysvar::clock::id(), data_clock),
|
sysvar_cache.push_entry(
|
||||||
(sysvar::epoch_schedule::id(), data_epochschedule),
|
sysvar::epoch_schedule::id(),
|
||||||
(sysvar::fees::id(), data_fees),
|
bincode::serialize(&src_epochschedule).unwrap(),
|
||||||
(sysvar::rent::id(), data_rent),
|
);
|
||||||
];
|
sysvar_cache.push_entry(sysvar::fees::id(), bincode::serialize(&src_fees).unwrap());
|
||||||
|
sysvar_cache.push_entry(sysvar::rent::id(), bincode::serialize(&src_rent).unwrap());
|
||||||
|
|
||||||
let program_id = Pubkey::new_unique();
|
let program_id = Pubkey::new_unique();
|
||||||
let mut transaction_context = TransactionContext::new(
|
let mut transaction_context = TransactionContext::new(
|
||||||
vec![(program_id, AccountSharedData::new(0, 0, &bpf_loader::id()))],
|
vec![(program_id, AccountSharedData::new(0, 0, &bpf_loader::id()))],
|
||||||
1,
|
1,
|
||||||
);
|
);
|
||||||
let mut invoke_context = InvokeContext::new_mock(&mut transaction_context, &[]);
|
let mut invoke_context = InvokeContext::new_mock(&mut transaction_context, &[]);
|
||||||
invoke_context.sysvars = &sysvars;
|
invoke_context.sysvar_cache = Cow::Owned(sysvar_cache);
|
||||||
invoke_context.push(&[], &[0], &[]).unwrap();
|
invoke_context.push(&[], &[0], &[]).unwrap();
|
||||||
|
|
||||||
// Test clock sysvar
|
// Test clock sysvar
|
||||||
|
|
|
@ -341,8 +341,9 @@ mod tests {
|
||||||
super::*,
|
super::*,
|
||||||
crate::stake_state::{Meta, StakeState},
|
crate::stake_state::{Meta, StakeState},
|
||||||
bincode::serialize,
|
bincode::serialize,
|
||||||
solana_program_runtime::invoke_context::{
|
solana_program_runtime::{
|
||||||
mock_process_instruction, mock_process_instruction_with_sysvars,
|
invoke_context::{mock_process_instruction, mock_process_instruction_with_sysvars},
|
||||||
|
sysvar_cache::SysvarCache,
|
||||||
},
|
},
|
||||||
solana_sdk::{
|
solana_sdk::{
|
||||||
account::{self, AccountSharedData},
|
account::{self, AccountSharedData},
|
||||||
|
@ -354,7 +355,7 @@ mod tests {
|
||||||
instruction::{self, LockupArgs},
|
instruction::{self, LockupArgs},
|
||||||
state::{Authorized, Lockup, StakeAuthorize},
|
state::{Authorized, Lockup, StakeAuthorize},
|
||||||
},
|
},
|
||||||
sysvar::{stake_history::StakeHistory, Sysvar},
|
sysvar::stake_history::StakeHistory,
|
||||||
},
|
},
|
||||||
std::str::FromStr,
|
std::str::FromStr,
|
||||||
};
|
};
|
||||||
|
@ -436,8 +437,9 @@ mod tests {
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
let mut data = Vec::with_capacity(sysvar::clock::Clock::size_of());
|
let mut sysvar_cache = SysvarCache::default();
|
||||||
bincode::serialize_into(&mut data, &sysvar::clock::Clock::default()).unwrap();
|
let clock = Clock::default();
|
||||||
|
sysvar_cache.push_entry(sysvar::clock::id(), bincode::serialize(&clock).unwrap());
|
||||||
mock_process_instruction_with_sysvars(
|
mock_process_instruction_with_sysvars(
|
||||||
&id(),
|
&id(),
|
||||||
Vec::new(),
|
Vec::new(),
|
||||||
|
@ -445,7 +447,7 @@ mod tests {
|
||||||
transaction_accounts,
|
transaction_accounts,
|
||||||
instruction.accounts.clone(),
|
instruction.accounts.clone(),
|
||||||
expected_result,
|
expected_result,
|
||||||
&[(sysvar::clock::id(), data)],
|
&sysvar_cache,
|
||||||
super::process_instruction,
|
super::process_instruction,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -1186,8 +1188,9 @@ mod tests {
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let mut data = Vec::with_capacity(sysvar::clock::Clock::size_of());
|
let mut sysvar_cache = SysvarCache::default();
|
||||||
bincode::serialize_into(&mut data, &sysvar::clock::Clock::default()).unwrap();
|
let clock = Clock::default();
|
||||||
|
sysvar_cache.push_entry(sysvar::clock::id(), bincode::serialize(&clock).unwrap());
|
||||||
mock_process_instruction_with_sysvars(
|
mock_process_instruction_with_sysvars(
|
||||||
&id(),
|
&id(),
|
||||||
Vec::new(),
|
Vec::new(),
|
||||||
|
@ -1215,7 +1218,7 @@ mod tests {
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
Ok(()),
|
Ok(()),
|
||||||
&[(sysvar::clock::id(), data)],
|
&sysvar_cache,
|
||||||
super::process_instruction,
|
super::process_instruction,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -518,8 +518,9 @@ mod tests {
|
||||||
use {
|
use {
|
||||||
super::*,
|
super::*,
|
||||||
bincode::serialize,
|
bincode::serialize,
|
||||||
solana_program_runtime::invoke_context::{
|
solana_program_runtime::{
|
||||||
mock_process_instruction, mock_process_instruction_with_sysvars,
|
invoke_context::{mock_process_instruction, mock_process_instruction_with_sysvars},
|
||||||
|
sysvar_cache::SysvarCache,
|
||||||
},
|
},
|
||||||
solana_sdk::{
|
solana_sdk::{
|
||||||
account::{self, Account, AccountSharedData},
|
account::{self, Account, AccountSharedData},
|
||||||
|
@ -579,12 +580,13 @@ mod tests {
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
let mut sysvar_cache = SysvarCache::default();
|
||||||
let rent = Rent::default();
|
let rent = Rent::default();
|
||||||
let rent_sysvar = (sysvar::rent::id(), bincode::serialize(&rent).unwrap());
|
sysvar_cache.push_entry(sysvar::rent::id(), bincode::serialize(&rent).unwrap());
|
||||||
let clock = Clock::default();
|
let clock = Clock::default();
|
||||||
let clock_sysvar = (sysvar::clock::id(), bincode::serialize(&clock).unwrap());
|
sysvar_cache.push_entry(sysvar::clock::id(), bincode::serialize(&clock).unwrap());
|
||||||
let slot_hashes = SlotHashes::default();
|
let slot_hashes = SlotHashes::default();
|
||||||
let slot_hashes_sysvar = (
|
sysvar_cache.push_entry(
|
||||||
sysvar::slot_hashes::id(),
|
sysvar::slot_hashes::id(),
|
||||||
bincode::serialize(&slot_hashes).unwrap(),
|
bincode::serialize(&slot_hashes).unwrap(),
|
||||||
);
|
);
|
||||||
|
@ -595,7 +597,7 @@ mod tests {
|
||||||
transaction_accounts,
|
transaction_accounts,
|
||||||
instruction.accounts.clone(),
|
instruction.accounts.clone(),
|
||||||
expected_result,
|
expected_result,
|
||||||
&[rent_sysvar, clock_sysvar, slot_hashes_sysvar],
|
&sysvar_cache,
|
||||||
super::process_instruction,
|
super::process_instruction,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,6 +79,7 @@ use {
|
||||||
BuiltinProgram, Executor, Executors, ProcessInstructionWithContext, TransactionExecutor,
|
BuiltinProgram, Executor, Executors, ProcessInstructionWithContext, TransactionExecutor,
|
||||||
},
|
},
|
||||||
log_collector::LogCollector,
|
log_collector::LogCollector,
|
||||||
|
sysvar_cache::SysvarCache,
|
||||||
timings::ExecuteTimings,
|
timings::ExecuteTimings,
|
||||||
},
|
},
|
||||||
solana_sdk::{
|
solana_sdk::{
|
||||||
|
@ -160,6 +161,7 @@ use {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
mod sysvar_cache;
|
||||||
mod transaction_account_state_info;
|
mod transaction_account_state_info;
|
||||||
|
|
||||||
pub const SECONDS_PER_YEAR: f64 = 365.25 * 24.0 * 60.0 * 60.0;
|
pub const SECONDS_PER_YEAR: f64 = 365.25 * 24.0 * 60.0 * 60.0;
|
||||||
|
@ -1186,7 +1188,7 @@ pub struct Bank {
|
||||||
|
|
||||||
pub cost_tracker: RwLock<CostTracker>,
|
pub cost_tracker: RwLock<CostTracker>,
|
||||||
|
|
||||||
sysvar_cache: RwLock<Vec<(Pubkey, Vec<u8>)>>,
|
sysvar_cache: RwLock<SysvarCache>,
|
||||||
|
|
||||||
/// Current size of the accounts data. Used when processing messages to enforce a limit on its
|
/// Current size of the accounts data. Used when processing messages to enforce a limit on its
|
||||||
/// maximum size.
|
/// maximum size.
|
||||||
|
@ -1331,7 +1333,7 @@ impl Bank {
|
||||||
freeze_started: AtomicBool::default(),
|
freeze_started: AtomicBool::default(),
|
||||||
vote_only_bank: false,
|
vote_only_bank: false,
|
||||||
cost_tracker: RwLock::<CostTracker>::default(),
|
cost_tracker: RwLock::<CostTracker>::default(),
|
||||||
sysvar_cache: RwLock::new(Vec::new()),
|
sysvar_cache: RwLock::<SysvarCache>::default(),
|
||||||
accounts_data_len: AtomicU64::default(),
|
accounts_data_len: AtomicU64::default(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1584,7 +1586,7 @@ impl Bank {
|
||||||
)),
|
)),
|
||||||
freeze_started: AtomicBool::new(false),
|
freeze_started: AtomicBool::new(false),
|
||||||
cost_tracker: RwLock::new(CostTracker::default()),
|
cost_tracker: RwLock::new(CostTracker::default()),
|
||||||
sysvar_cache: RwLock::new(Vec::new()),
|
sysvar_cache: RwLock::new(SysvarCache::default()),
|
||||||
accounts_data_len: AtomicU64::new(parent.load_accounts_data_len()),
|
accounts_data_len: AtomicU64::new(parent.load_accounts_data_len()),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1772,7 +1774,7 @@ impl Bank {
|
||||||
freeze_started: AtomicBool::new(fields.hash != Hash::default()),
|
freeze_started: AtomicBool::new(fields.hash != Hash::default()),
|
||||||
vote_only_bank: false,
|
vote_only_bank: false,
|
||||||
cost_tracker: RwLock::new(CostTracker::default()),
|
cost_tracker: RwLock::new(CostTracker::default()),
|
||||||
sysvar_cache: RwLock::new(Vec::new()),
|
sysvar_cache: RwLock::new(SysvarCache::default()),
|
||||||
accounts_data_len: AtomicU64::new(accounts_data_len),
|
accounts_data_len: AtomicU64::new(accounts_data_len),
|
||||||
};
|
};
|
||||||
bank.finish_init(
|
bank.finish_init(
|
||||||
|
@ -1955,11 +1957,7 @@ impl Bank {
|
||||||
|
|
||||||
// Update the entry in the cache
|
// Update the entry in the cache
|
||||||
let mut sysvar_cache = self.sysvar_cache.write().unwrap();
|
let mut sysvar_cache = self.sysvar_cache.write().unwrap();
|
||||||
if let Some(position) = sysvar_cache.iter().position(|(id, _data)| id == pubkey) {
|
sysvar_cache.update_entry(pubkey, &new_account);
|
||||||
sysvar_cache[position].1 = new_account.data().to_vec();
|
|
||||||
} else {
|
|
||||||
sysvar_cache.push((*pubkey, new_account.data().to_vec()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn inherit_specially_retained_account_fields(
|
fn inherit_specially_retained_account_fields(
|
||||||
|
@ -2109,17 +2107,6 @@ impl Bank {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fill_sysvar_cache(&mut self) {
|
|
||||||
let mut sysvar_cache = self.sysvar_cache.write().unwrap();
|
|
||||||
for id in sysvar::ALL_IDS.iter() {
|
|
||||||
if !sysvar_cache.iter().any(|(key, _data)| key == id) {
|
|
||||||
if let Some(account) = self.get_account_with_fixed_root(id) {
|
|
||||||
sysvar_cache.push((*id, account.data().to_vec()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_slot_history(&self) -> SlotHistory {
|
pub fn get_slot_history(&self) -> SlotHistory {
|
||||||
from_account(&self.get_account(&sysvar::slot_history::id()).unwrap()).unwrap()
|
from_account(&self.get_account(&sysvar::slot_history::id()).unwrap()).unwrap()
|
||||||
}
|
}
|
||||||
|
@ -3629,21 +3616,6 @@ impl Bank {
|
||||||
Arc::make_mut(&mut cache).remove(pubkey);
|
Arc::make_mut(&mut cache).remove(pubkey);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the value of a cached sysvar by its id
|
|
||||||
pub fn get_cached_sysvar<T: Sysvar>(&self, id: &Pubkey) -> Option<T> {
|
|
||||||
self.sysvar_cache
|
|
||||||
.read()
|
|
||||||
.unwrap()
|
|
||||||
.iter()
|
|
||||||
.find_map(|(key, data)| {
|
|
||||||
if id == key {
|
|
||||||
bincode::deserialize(data).ok()
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn load_lookup_table_addresses(
|
pub fn load_lookup_table_addresses(
|
||||||
&self,
|
&self,
|
||||||
address_table_lookups: &[MessageAddressTableLookup],
|
address_table_lookups: &[MessageAddressTableLookup],
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
use {
|
||||||
|
super::Bank,
|
||||||
|
solana_sdk::{
|
||||||
|
account::ReadableAccount,
|
||||||
|
pubkey::Pubkey,
|
||||||
|
sysvar::{self, Sysvar},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
impl Bank {
|
||||||
|
pub(crate) fn fill_sysvar_cache(&mut self) {
|
||||||
|
let mut sysvar_cache = self.sysvar_cache.write().unwrap();
|
||||||
|
for id in sysvar::ALL_IDS.iter() {
|
||||||
|
if !sysvar_cache.iter().any(|(key, _data)| key == id) {
|
||||||
|
if let Some(account) = self.get_account_with_fixed_root(id) {
|
||||||
|
sysvar_cache.push_entry(*id, account.data().to_vec());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the value of a cached sysvar by its id
|
||||||
|
pub fn get_cached_sysvar<T: Sysvar>(&self, id: &Pubkey) -> Option<T> {
|
||||||
|
let sysvar_cache = self.sysvar_cache.read().unwrap();
|
||||||
|
sysvar_cache.iter().find_map(|(key, data)| {
|
||||||
|
if id == key {
|
||||||
|
bincode::deserialize(data).ok()
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,6 +5,7 @@ use {
|
||||||
instruction_recorder::InstructionRecorder,
|
instruction_recorder::InstructionRecorder,
|
||||||
invoke_context::{BuiltinProgram, Executors, InvokeContext},
|
invoke_context::{BuiltinProgram, Executors, InvokeContext},
|
||||||
log_collector::LogCollector,
|
log_collector::LogCollector,
|
||||||
|
sysvar_cache::SysvarCache,
|
||||||
timings::ExecuteTimings,
|
timings::ExecuteTimings,
|
||||||
},
|
},
|
||||||
solana_sdk::{
|
solana_sdk::{
|
||||||
|
@ -14,14 +15,13 @@ use {
|
||||||
hash::Hash,
|
hash::Hash,
|
||||||
message::SanitizedMessage,
|
message::SanitizedMessage,
|
||||||
precompiles::is_precompile,
|
precompiles::is_precompile,
|
||||||
pubkey::Pubkey,
|
|
||||||
rent::Rent,
|
rent::Rent,
|
||||||
saturating_add_assign,
|
saturating_add_assign,
|
||||||
sysvar::instructions,
|
sysvar::instructions,
|
||||||
transaction::TransactionError,
|
transaction::TransactionError,
|
||||||
transaction_context::{InstructionAccount, TransactionContext},
|
transaction_context::{InstructionAccount, TransactionContext},
|
||||||
},
|
},
|
||||||
std::{cell::RefCell, rc::Rc, sync::Arc},
|
std::{borrow::Cow, cell::RefCell, rc::Rc, sync::Arc},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
|
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
|
||||||
|
@ -62,7 +62,7 @@ impl MessageProcessor {
|
||||||
feature_set: Arc<FeatureSet>,
|
feature_set: Arc<FeatureSet>,
|
||||||
compute_budget: ComputeBudget,
|
compute_budget: ComputeBudget,
|
||||||
timings: &mut ExecuteTimings,
|
timings: &mut ExecuteTimings,
|
||||||
sysvars: &[(Pubkey, Vec<u8>)],
|
sysvar_cache: &SysvarCache,
|
||||||
blockhash: Hash,
|
blockhash: Hash,
|
||||||
lamports_per_signature: u64,
|
lamports_per_signature: u64,
|
||||||
current_accounts_data_len: u64,
|
current_accounts_data_len: u64,
|
||||||
|
@ -71,7 +71,7 @@ impl MessageProcessor {
|
||||||
transaction_context,
|
transaction_context,
|
||||||
rent,
|
rent,
|
||||||
builtin_programs,
|
builtin_programs,
|
||||||
sysvars,
|
Cow::Borrowed(sysvar_cache),
|
||||||
log_collector,
|
log_collector,
|
||||||
compute_budget,
|
compute_budget,
|
||||||
executors,
|
executors,
|
||||||
|
@ -166,6 +166,7 @@ mod tests {
|
||||||
instruction::{AccountMeta, Instruction, InstructionError},
|
instruction::{AccountMeta, Instruction, InstructionError},
|
||||||
message::Message,
|
message::Message,
|
||||||
native_loader::{self, create_loadable_account_for_test},
|
native_loader::{self, create_loadable_account_for_test},
|
||||||
|
pubkey::Pubkey,
|
||||||
secp256k1_instruction::new_secp256k1_instruction,
|
secp256k1_instruction::new_secp256k1_instruction,
|
||||||
secp256k1_program,
|
secp256k1_program,
|
||||||
},
|
},
|
||||||
|
@ -257,6 +258,7 @@ mod tests {
|
||||||
)],
|
)],
|
||||||
Some(transaction_context.get_key_of_account_at_index(0)),
|
Some(transaction_context.get_key_of_account_at_index(0)),
|
||||||
));
|
));
|
||||||
|
let sysvar_cache = SysvarCache::default();
|
||||||
let result = MessageProcessor::process_message(
|
let result = MessageProcessor::process_message(
|
||||||
builtin_programs,
|
builtin_programs,
|
||||||
&message,
|
&message,
|
||||||
|
@ -269,7 +271,7 @@ mod tests {
|
||||||
Arc::new(FeatureSet::all_enabled()),
|
Arc::new(FeatureSet::all_enabled()),
|
||||||
ComputeBudget::new(),
|
ComputeBudget::new(),
|
||||||
&mut ExecuteTimings::default(),
|
&mut ExecuteTimings::default(),
|
||||||
&[],
|
&sysvar_cache,
|
||||||
Hash::default(),
|
Hash::default(),
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
|
@ -310,7 +312,7 @@ mod tests {
|
||||||
Arc::new(FeatureSet::all_enabled()),
|
Arc::new(FeatureSet::all_enabled()),
|
||||||
ComputeBudget::new(),
|
ComputeBudget::new(),
|
||||||
&mut ExecuteTimings::default(),
|
&mut ExecuteTimings::default(),
|
||||||
&[],
|
&sysvar_cache,
|
||||||
Hash::default(),
|
Hash::default(),
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
|
@ -343,7 +345,7 @@ mod tests {
|
||||||
Arc::new(FeatureSet::all_enabled()),
|
Arc::new(FeatureSet::all_enabled()),
|
||||||
ComputeBudget::new(),
|
ComputeBudget::new(),
|
||||||
&mut ExecuteTimings::default(),
|
&mut ExecuteTimings::default(),
|
||||||
&[],
|
&sysvar_cache,
|
||||||
Hash::default(),
|
Hash::default(),
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
|
@ -457,6 +459,7 @@ mod tests {
|
||||||
)],
|
)],
|
||||||
Some(transaction_context.get_key_of_account_at_index(0)),
|
Some(transaction_context.get_key_of_account_at_index(0)),
|
||||||
));
|
));
|
||||||
|
let sysvar_cache = SysvarCache::default();
|
||||||
let result = MessageProcessor::process_message(
|
let result = MessageProcessor::process_message(
|
||||||
builtin_programs,
|
builtin_programs,
|
||||||
&message,
|
&message,
|
||||||
|
@ -469,7 +472,7 @@ mod tests {
|
||||||
Arc::new(FeatureSet::all_enabled()),
|
Arc::new(FeatureSet::all_enabled()),
|
||||||
ComputeBudget::new(),
|
ComputeBudget::new(),
|
||||||
&mut ExecuteTimings::default(),
|
&mut ExecuteTimings::default(),
|
||||||
&[],
|
&sysvar_cache,
|
||||||
Hash::default(),
|
Hash::default(),
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
|
@ -503,7 +506,7 @@ mod tests {
|
||||||
Arc::new(FeatureSet::all_enabled()),
|
Arc::new(FeatureSet::all_enabled()),
|
||||||
ComputeBudget::new(),
|
ComputeBudget::new(),
|
||||||
&mut ExecuteTimings::default(),
|
&mut ExecuteTimings::default(),
|
||||||
&[],
|
&sysvar_cache,
|
||||||
Hash::default(),
|
Hash::default(),
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
|
@ -534,7 +537,7 @@ mod tests {
|
||||||
Arc::new(FeatureSet::all_enabled()),
|
Arc::new(FeatureSet::all_enabled()),
|
||||||
ComputeBudget::new(),
|
ComputeBudget::new(),
|
||||||
&mut ExecuteTimings::default(),
|
&mut ExecuteTimings::default(),
|
||||||
&[],
|
&sysvar_cache,
|
||||||
Hash::default(),
|
Hash::default(),
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
|
@ -595,6 +598,7 @@ mod tests {
|
||||||
],
|
],
|
||||||
None,
|
None,
|
||||||
));
|
));
|
||||||
|
let sysvar_cache = SysvarCache::default();
|
||||||
let result = MessageProcessor::process_message(
|
let result = MessageProcessor::process_message(
|
||||||
builtin_programs,
|
builtin_programs,
|
||||||
&message,
|
&message,
|
||||||
|
@ -607,7 +611,7 @@ mod tests {
|
||||||
Arc::new(FeatureSet::all_enabled()),
|
Arc::new(FeatureSet::all_enabled()),
|
||||||
ComputeBudget::new(),
|
ComputeBudget::new(),
|
||||||
&mut ExecuteTimings::default(),
|
&mut ExecuteTimings::default(),
|
||||||
&[],
|
&sysvar_cache,
|
||||||
Hash::default(),
|
Hash::default(),
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
|
|
Loading…
Reference in New Issue