Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the sysvar cache #17

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 6 additions & 35 deletions src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use solana_program_runtime::{
loaded_programs::{LoadProgramMetrics, LoadedProgram, LoadedProgramsForTxBatch},
log_collector::LogCollector,
message_processor::MessageProcessor,
sysvar_cache::SysvarCache,
timings::ExecuteTimings,
};
use solana_sdk::{
Expand All @@ -32,7 +31,6 @@ use solana_sdk::{
signers::Signers,
slot_history::Slot,
system_instruction, system_program,
sysvar::{Sysvar, SysvarId},
transaction::{MessageHash, SanitizedTransaction, TransactionError, VersionedTransaction},
transaction_context::{ExecutionRecord, IndexOfAccount, TransactionContext},
};
Expand All @@ -44,6 +42,7 @@ use crate::{
create_blockhash,
history::TransactionHistory,
spl::load_spl_programs,
sysvar::Sysvar,
types::{ExecutionResult, FailedTransactionMetadata, TransactionMetadata, TransactionResult},
utils::RentState,
PROGRAM_OWNERS,
Expand All @@ -66,7 +65,6 @@ pub struct LiteSVM {
//TODO compute budget
programs_cache: LoadedProgramsForTxBatch,
airdrop_kp: Keypair,
sysvar_cache: SysvarCache,
feature_set: Arc<FeatureSet>,
block_height: u64,
slot: Slot,
Expand All @@ -81,7 +79,6 @@ impl Default for LiteSVM {
accounts: Default::default(),
programs_cache: Default::default(),
airdrop_kp: Keypair::new(),
sysvar_cache: Default::default(),
feature_set: Default::default(),
block_height: 0,
slot: 0,
Expand Down Expand Up @@ -156,12 +153,8 @@ impl LiteSVM {
}

pub fn minimum_balance_for_rent_exemption(&self, data_len: usize) -> u64 {
1.max(
self.sysvar_cache
.get_rent()
.unwrap_or_default()
.minimum_balance(data_len),
)
let rent: Rent = self.get_sysvar();
1.max(rent.minimum_balance(data_len))
}

pub fn get_account(&self, pubkey: &Pubkey) -> Account {
Expand All @@ -184,28 +177,6 @@ impl LiteSVM {
self.slot
}

pub fn set_sysvar<T>(&mut self, sysvar: &T)
where
T: Sysvar + SysvarId,
{
let Ok(data) = bincode::serialize(sysvar) else {
return;
};

let account = AccountSharedData::new_data(1, &sysvar, &solana_sdk::sysvar::id()).unwrap();

if T::id() == Clock::id() {
if let Ok(clock) = bincode::deserialize(&data) {
self.sysvar_cache.set_clock(clock);
self.accounts.add_account(Clock::id(), account);
}
} else if T::id() == Rent::id() {
if let Ok(rent) = bincode::deserialize(&data) {
self.sysvar_cache.set_rent(rent);
self.accounts.add_account(Rent::id(), account);
}
}
}
pub fn get_transaction(&self, signature: &Signature) -> Option<&TransactionMetadata> {
self.history.get_transaction(signature)
}
Expand Down Expand Up @@ -419,15 +390,15 @@ impl LiteSVM {
tx.message(),
&program_indices,
context,
*self.sysvar_cache.get_rent().unwrap_or_default(),
self.get_sysvar(),
Some(self.log_collector.clone()),
&self.programs_cache,
&mut programs_modified_by_tx,
&mut programs_updated_only_for_global_cache,
self.feature_set.clone(),
compute_budget,
&mut ExecuteTimings::default(),
&self.sysvar_cache,
&self.sysvar_cache(),
*blockhash,
0,
u64::MAX,
Expand Down Expand Up @@ -456,7 +427,7 @@ impl LiteSVM {
let pubkey = context
.get_key_of_account_at_index(index as IndexOfAccount)
.map_err(|err| TransactionError::InstructionError(index as u8, err))?;
let rent = self.sysvar_cache.get_rent().unwrap_or_default();
let rent = self.get_sysvar();

if !account.data().is_empty() {
let post_rent_state = RentState::from_account(&account, &rent);
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ mod history;
mod spl;
mod utils;

pub mod sysvar;
pub use bank::LiteSVM;
pub use utils::*;
38 changes: 38 additions & 0 deletions src/sysvar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use solana_program_runtime::sysvar_cache::SysvarCache;
use solana_sdk::{
account::{Account, ReadableAccount},
sysvar::{Sysvar as SysvarTrait, SysvarId},
};

use crate::LiteSVM;

pub trait Sysvar {
fn set_sysvar<T: SysvarTrait + SysvarId>(&mut self, sysvar: &T);

fn get_sysvar<T: SysvarTrait + SysvarId>(&self) -> T;

fn sysvar_cache(&self) -> SysvarCache;
}

impl Sysvar for LiteSVM {
fn set_sysvar<T: SysvarTrait + SysvarId>(&mut self, sysvar: &T) {
let account = Account::new_data(1, &sysvar, &solana_sdk::sysvar::id()).unwrap();

self.set_account(T::id(), account);
}

fn get_sysvar<T: SysvarTrait + SysvarId>(&self) -> T {
let account = self.get_account(&T::id());

bincode::deserialize(account.data()).unwrap()
}

fn sysvar_cache(&self) -> SysvarCache {
let mut cache = SysvarCache::default();

cache.set_clock(self.get_sysvar());
cache.set_rent(self.get_sysvar());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the other sysvars? https://docs.solanalabs.com/runtime/sysvars

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add all, let met make a push on it


cache
}
}