Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.
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
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions program-runtime/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[package]
name = "solana-program-runtime"
version = "1.10.0"
description = "Solana program runtime"
authors = ["Solana Maintainers <maintainers@solana.foundation>"]
repository = "https://github.com/solana-labs/solana"
license = "Apache-2.0"
homepage = "https://solana.com/"
documentation = "https://docs.rs/solana-program-runtime"
edition = "2021"

[dependencies]
base64 = "0.13"
bincode = "1.3.3"
itertools = "0.10.1"
libc = "0.2.101"
libloading = "0.7.0"
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"

[dev-dependencies]
solana-logger = { path = "../logger", version = "=1.10.0" }

[lib]
crate-type = ["lib"]
name = "solana_program_runtime"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[build-dependencies]
rustc_version = "0.4"
29 changes: 20 additions & 9 deletions program-runtime/src/invoke_context.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
use {
crate::{
<<<<<<< HEAD
accounts_data_meter::AccountsDataMeter, ic_logger_msg, ic_msg,
instruction_recorder::InstructionRecorder, log_collector::LogCollector,
native_loader::NativeLoader, pre_account::PreAccount, timings::ExecuteDetailsTimings,
=======
accounts_data_meter::AccountsDataMeter,
ic_logger_msg, ic_msg,
instruction_recorder::InstructionRecorder,
log_collector::LogCollector,
native_loader::NativeLoader,
pre_account::PreAccount,
sysvar_cache::SysvarCache,
timings::{ExecuteDetailsTimings, ExecuteTimings},
>>>>>>> 7171c95bd (Refactor: move sysvar cache to new module)
},
solana_sdk::{
account::{AccountSharedData, ReadableAccount},
Expand All @@ -22,7 +33,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 =
Expand Down Expand Up @@ -180,7 +191,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,
Expand All @@ -200,7 +211,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>>,
Expand All @@ -217,7 +228,7 @@ impl<'a> InvokeContext<'a> {
rent,
pre_accounts: Vec::new(),
builtin_programs,
sysvars,
sysvar_cache,
log_collector,
current_compute_budget: compute_budget,
compute_budget,
Expand All @@ -240,7 +251,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())),
Expand Down Expand Up @@ -948,7 +959,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 {
Expand Down Expand Up @@ -1040,7 +1051,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());
Expand All @@ -1055,7 +1066,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,
Expand Down Expand Up @@ -1086,7 +1097,7 @@ pub fn mock_process_instruction(
transaction_accounts,
instruction_accounts,
expected_result,
&[],
&SysvarCache::default(),
process_instruction,
)
}
Expand Down
12 changes: 12 additions & 0 deletions program-runtime/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![cfg_attr(RUSTC_WITH_SPECIALIZATION, feature(min_specialization))]

pub mod accounts_data_meter;
pub mod instruction_recorder;
pub mod invoke_context;
pub mod log_collector;
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 changes: 39 additions & 0 deletions program-runtime/src/sysvar_cache.rs
Original file line number Diff line number Diff line change
@@ -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()));
}
}
}
30 changes: 30 additions & 0 deletions programs/bpf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 51 additions & 1 deletion programs/bpf_loader/src/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2766,6 +2766,10 @@ impl<'a> SyscallObject<BpfError> for SyscallLogData<'a> {
mod tests {
use {
super::*,
<<<<<<< HEAD
=======
solana_program_runtime::{invoke_context::InvokeContext, sysvar_cache::SysvarCache},
>>>>>>> 7171c95bd (Refactor: move sysvar cache to new module)
solana_rbpf::{
ebpf::HOST_ALIGN, memory_region::MemoryRegion, user_error::UserError, vm::Config,
},
Expand All @@ -2775,7 +2779,7 @@ mod tests {
hash::hashv,
process_instruction::{MockComputeMeter, MockInvokeContext, MockLogger},
},
std::str::FromStr,
std::{borrow::Cow, str::FromStr},
};

macro_rules! assert_access_violation {
Expand Down Expand Up @@ -3620,6 +3624,52 @@ mod tests {
#[test]
fn test_syscall_get_sysvar() {
let config = Config::default();
<<<<<<< HEAD
=======
let src_clock = Clock {
slot: 1,
epoch_start_timestamp: 2,
epoch: 3,
leader_schedule_epoch: 4,
unix_timestamp: 5,
};
let src_epochschedule = EpochSchedule {
slots_per_epoch: 1,
leader_schedule_slot_offset: 2,
warmup: false,
first_normal_epoch: 3,
first_normal_slot: 4,
};
let src_fees = Fees {
fee_calculator: FeeCalculator {
lamports_per_signature: 1,
},
};
let src_rent = Rent {
lamports_per_byte_year: 1,
exemption_threshold: 2.0,
burn_percent: 3,
};

let mut sysvar_cache = SysvarCache::default();
sysvar_cache.push_entry(sysvar::clock::id(), bincode::serialize(&src_clock).unwrap());
sysvar_cache.push_entry(
sysvar::epoch_schedule::id(),
bincode::serialize(&src_epochschedule).unwrap(),
);
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 mut transaction_context = TransactionContext::new(
vec![(program_id, AccountSharedData::new(0, 0, &bpf_loader::id()))],
1,
);
let mut invoke_context = InvokeContext::new_mock(&mut transaction_context, &[]);
invoke_context.sysvar_cache = Cow::Owned(sysvar_cache);
invoke_context.push(&[], &[0], &[]).unwrap();

>>>>>>> 7171c95bd (Refactor: move sysvar cache to new module)
// Test clock sysvar
{
let got_clock = Clock::default();
Expand Down
Loading