Merge pull request from GHSA-wxrh-ff9f-fc6w
This commit is contained in:
parent
5625959f7e
commit
21e066ef26
|
@ -40,7 +40,7 @@ pub fn parse_sysvar(data: &[u8], pubkey: &Pubkey) -> Result<SysvarAccountType, P
|
|||
.iter()
|
||||
.map(|entry| UiRecentBlockhashesEntry {
|
||||
blockhash: entry.blockhash.to_string(),
|
||||
fee_calculator: entry.fee_calculator.clone().into(),
|
||||
fee_calculator: entry.fee_calculator.into(),
|
||||
})
|
||||
.collect();
|
||||
SysvarAccountType::RecentBlockhashes(recent_blockhashes)
|
||||
|
|
|
@ -361,7 +361,7 @@ mod tests {
|
|||
},
|
||||
value: json!(RpcFees {
|
||||
blockhash: rpc_blockhash.to_string(),
|
||||
fee_calculator: rpc_fee_calc.clone(),
|
||||
fee_calculator: rpc_fee_calc,
|
||||
last_valid_slot: 42,
|
||||
last_valid_block_height: 42,
|
||||
}),
|
||||
|
@ -372,7 +372,7 @@ mod tests {
|
|||
api_version: None
|
||||
},
|
||||
value: json!(RpcFeeCalculator {
|
||||
fee_calculator: rpc_fee_calc.clone()
|
||||
fee_calculator: rpc_fee_calc
|
||||
}),
|
||||
});
|
||||
let mut mocks = HashMap::new();
|
||||
|
@ -382,7 +382,7 @@ mod tests {
|
|||
BlockhashQuery::default()
|
||||
.get_blockhash_and_fee_calculator(&rpc_client, CommitmentConfig::default())
|
||||
.unwrap(),
|
||||
(rpc_blockhash, rpc_fee_calc.clone()),
|
||||
(rpc_blockhash, rpc_fee_calc),
|
||||
);
|
||||
let mut mocks = HashMap::new();
|
||||
mocks.insert(RpcRequest::GetFees, get_recent_blockhash_response.clone());
|
||||
|
@ -416,7 +416,7 @@ mod tests {
|
|||
let data = nonce::state::Data {
|
||||
authority: Pubkey::new(&[3u8; 32]),
|
||||
blockhash: nonce_blockhash,
|
||||
fee_calculator: nonce_fee_calc.clone(),
|
||||
fee_calculator: nonce_fee_calc,
|
||||
};
|
||||
let nonce_account = Account::new_data_with_space(
|
||||
42,
|
||||
|
@ -448,7 +448,7 @@ mod tests {
|
|||
BlockhashQuery::All(Source::NonceAccount(nonce_pubkey))
|
||||
.get_blockhash_and_fee_calculator(&rpc_client, CommitmentConfig::default())
|
||||
.unwrap(),
|
||||
(nonce_blockhash, nonce_fee_calc.clone()),
|
||||
(nonce_blockhash, nonce_fee_calc),
|
||||
);
|
||||
let mut mocks = HashMap::new();
|
||||
mocks.insert(RpcRequest::GetAccountInfo, get_account_response.clone());
|
||||
|
|
|
@ -4610,34 +4610,55 @@ mod tests {
|
|||
);
|
||||
}
|
||||
|
||||
fn create_filled_type<T: Default>(zero_init: bool) -> T {
|
||||
let mut val = T::default();
|
||||
let p = &mut val as *mut _ as *mut u8;
|
||||
for i in 0..(size_of::<T>() as isize) {
|
||||
unsafe {
|
||||
*p.offset(i) = if zero_init { 0 } else { i as u8 };
|
||||
}
|
||||
}
|
||||
val
|
||||
}
|
||||
|
||||
fn are_bytes_equal<T>(first: &T, second: &T) -> bool {
|
||||
let p_first = first as *const _ as *const u8;
|
||||
let p_second = second as *const _ as *const u8;
|
||||
for i in 0..(size_of::<T>() as isize) {
|
||||
unsafe {
|
||||
if *p_first.offset(i) != *p_second.offset(i) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[allow(deprecated)]
|
||||
fn test_syscall_get_sysvar() {
|
||||
let config = Config::default();
|
||||
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 src_clock = create_filled_type::<Clock>(false);
|
||||
src_clock.slot = 1;
|
||||
src_clock.epoch_start_timestamp = 2;
|
||||
src_clock.epoch = 3;
|
||||
src_clock.leader_schedule_epoch = 4;
|
||||
src_clock.unix_timestamp = 5;
|
||||
let mut src_epochschedule = create_filled_type::<EpochSchedule>(false);
|
||||
src_epochschedule.slots_per_epoch = 1;
|
||||
src_epochschedule.leader_schedule_slot_offset = 2;
|
||||
src_epochschedule.warmup = false;
|
||||
src_epochschedule.first_normal_epoch = 3;
|
||||
src_epochschedule.first_normal_slot = 4;
|
||||
let mut src_fees = create_filled_type::<Fees>(false);
|
||||
src_fees.fee_calculator = FeeCalculator {
|
||||
lamports_per_signature: 1,
|
||||
};
|
||||
let mut src_rent = create_filled_type::<Rent>(false);
|
||||
src_rent.lamports_per_byte_year = 1;
|
||||
src_rent.exemption_threshold = 2.0;
|
||||
src_rent.burn_percent = 3;
|
||||
|
||||
let mut sysvar_cache = SysvarCache::default();
|
||||
sysvar_cache.set_clock(src_clock.clone());
|
||||
|
@ -4680,6 +4701,14 @@ mod tests {
|
|||
syscall.call(got_clock_va, 0, 0, 0, 0, &memory_mapping, &mut result);
|
||||
result.unwrap();
|
||||
assert_eq!(got_clock, src_clock);
|
||||
|
||||
let mut clean_clock = create_filled_type::<Clock>(true);
|
||||
clean_clock.slot = src_clock.slot;
|
||||
clean_clock.epoch_start_timestamp = src_clock.epoch_start_timestamp;
|
||||
clean_clock.epoch = src_clock.epoch;
|
||||
clean_clock.leader_schedule_epoch = src_clock.leader_schedule_epoch;
|
||||
clean_clock.unix_timestamp = src_clock.unix_timestamp;
|
||||
assert!(are_bytes_equal(&got_clock, &clean_clock));
|
||||
}
|
||||
|
||||
// Test epoch_schedule sysvar
|
||||
|
@ -4717,6 +4746,15 @@ mod tests {
|
|||
);
|
||||
result.unwrap();
|
||||
assert_eq!(got_epochschedule, src_epochschedule);
|
||||
|
||||
let mut clean_epochschedule = create_filled_type::<EpochSchedule>(true);
|
||||
clean_epochschedule.slots_per_epoch = src_epochschedule.slots_per_epoch;
|
||||
clean_epochschedule.leader_schedule_slot_offset =
|
||||
src_epochschedule.leader_schedule_slot_offset;
|
||||
clean_epochschedule.warmup = src_epochschedule.warmup;
|
||||
clean_epochschedule.first_normal_epoch = src_epochschedule.first_normal_epoch;
|
||||
clean_epochschedule.first_normal_slot = src_epochschedule.first_normal_slot;
|
||||
assert!(are_bytes_equal(&got_epochschedule, &clean_epochschedule));
|
||||
}
|
||||
|
||||
// Test fees sysvar
|
||||
|
@ -4746,11 +4784,15 @@ mod tests {
|
|||
syscall.call(got_fees_va, 0, 0, 0, 0, &memory_mapping, &mut result);
|
||||
result.unwrap();
|
||||
assert_eq!(got_fees, src_fees);
|
||||
|
||||
let mut clean_fees = create_filled_type::<Fees>(true);
|
||||
clean_fees.fee_calculator = src_fees.fee_calculator;
|
||||
assert!(are_bytes_equal(&got_fees, &clean_fees));
|
||||
}
|
||||
|
||||
// Test rent sysvar
|
||||
{
|
||||
let got_rent = Rent::default();
|
||||
let got_rent = create_filled_type::<Rent>(true);
|
||||
let got_rent_va = 0x100000000;
|
||||
|
||||
let memory_mapping = MemoryMapping::new::<UserError>(
|
||||
|
@ -4775,6 +4817,12 @@ mod tests {
|
|||
syscall.call(got_rent_va, 0, 0, 0, 0, &memory_mapping, &mut result);
|
||||
result.unwrap();
|
||||
assert_eq!(got_rent, src_rent);
|
||||
|
||||
let mut clean_rent = create_filled_type::<Rent>(true);
|
||||
clean_rent.lamports_per_byte_year = src_rent.lamports_per_byte_year;
|
||||
clean_rent.exemption_threshold = src_rent.exemption_threshold;
|
||||
clean_rent.burn_percent = src_rent.burn_percent;
|
||||
assert!(are_bytes_equal(&got_rent, &clean_rent));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2266,7 +2266,7 @@ impl Bank {
|
|||
block_height: self.block_height,
|
||||
collector_id: self.collector_id,
|
||||
collector_fees: self.collector_fees.load(Relaxed),
|
||||
fee_calculator: self.fee_calculator.clone(),
|
||||
fee_calculator: self.fee_calculator,
|
||||
fee_rate_governor: self.fee_rate_governor.clone(),
|
||||
collected_rent: self.collected_rent.load(Relaxed),
|
||||
rent_collector: self.rent_collector.clone(),
|
||||
|
@ -12751,7 +12751,7 @@ pub(crate) mod tests {
|
|||
StateMut::<nonce::state::Versions>::state(&acc).map(|v| v.convert_to_current());
|
||||
match state {
|
||||
Ok(nonce::State::Initialized(ref data)) => {
|
||||
Some((data.blockhash, data.fee_calculator.clone()))
|
||||
Some((data.blockhash, data.fee_calculator))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
|
@ -12788,7 +12788,7 @@ pub(crate) mod tests {
|
|||
StateMut::<nonce::state::Versions>::state(&acc).map(|v| v.convert_to_current());
|
||||
match state {
|
||||
Ok(nonce::State::Initialized(ref data)) => {
|
||||
Some((data.blockhash, data.fee_calculator.clone()))
|
||||
Some((data.blockhash, data.fee_calculator))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
|
@ -12821,7 +12821,7 @@ pub(crate) mod tests {
|
|||
StateMut::<nonce::state::Versions>::state(&acc).map(|v| v.convert_to_current());
|
||||
match state {
|
||||
Ok(nonce::State::Initialized(ref data)) => {
|
||||
Some((data.blockhash, data.fee_calculator.clone()))
|
||||
Some((data.blockhash, data.fee_calculator))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
|
@ -12858,7 +12858,7 @@ pub(crate) mod tests {
|
|||
StateMut::<nonce::state::Versions>::state(&acc).map(|v| v.convert_to_current());
|
||||
match state {
|
||||
Ok(nonce::State::Initialized(ref data)) => {
|
||||
Some((data.blockhash, data.fee_calculator.clone()))
|
||||
Some((data.blockhash, data.fee_calculator))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
//! Information about the network's clock, ticks, slots, etc.
|
||||
|
||||
use {
|
||||
crate::{clone_zeroed, copy_field},
|
||||
std::mem::MaybeUninit,
|
||||
};
|
||||
|
||||
// The default tick rate that the cluster attempts to achieve. Note that the actual tick
|
||||
// rate at any given time should be expected to drift
|
||||
pub const DEFAULT_TICKS_PER_SECOND: u64 = 160;
|
||||
|
@ -105,7 +110,7 @@ pub type UnixTimestamp = i64;
|
|||
/// as the network progresses).
|
||||
///
|
||||
#[repr(C)]
|
||||
#[derive(Serialize, Clone, Deserialize, Debug, Default, PartialEq)]
|
||||
#[derive(Serialize, Deserialize, Debug, Default, PartialEq)]
|
||||
pub struct Clock {
|
||||
/// the current network/bank Slot
|
||||
pub slot: Slot,
|
||||
|
@ -121,3 +126,36 @@ pub struct Clock {
|
|||
/// timestamp_correction and timestamp_bounding features
|
||||
pub unix_timestamp: UnixTimestamp,
|
||||
}
|
||||
|
||||
impl Clone for Clock {
|
||||
fn clone(&self) -> Self {
|
||||
clone_zeroed(|cloned: &mut MaybeUninit<Self>| {
|
||||
let ptr = cloned.as_mut_ptr();
|
||||
unsafe {
|
||||
copy_field!(ptr, self, slot);
|
||||
copy_field!(ptr, self, epoch_start_timestamp);
|
||||
copy_field!(ptr, self, epoch);
|
||||
copy_field!(ptr, self, leader_schedule_epoch);
|
||||
copy_field!(ptr, self, unix_timestamp);
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_clone() {
|
||||
let clock = Clock {
|
||||
slot: 1,
|
||||
epoch_start_timestamp: 2,
|
||||
epoch: 3,
|
||||
leader_schedule_epoch: 4,
|
||||
unix_timestamp: 5,
|
||||
};
|
||||
let cloned_clock = clock.clone();
|
||||
assert_eq!(cloned_clock, clock);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,10 @@
|
|||
|
||||
/// 1 Epoch = 400 * 8192 ms ~= 55 minutes
|
||||
pub use crate::clock::{Epoch, Slot, DEFAULT_SLOTS_PER_EPOCH};
|
||||
use {
|
||||
crate::{clone_zeroed, copy_field},
|
||||
std::mem::MaybeUninit,
|
||||
};
|
||||
|
||||
/// The number of slots before an epoch starts to calculate the leader schedule.
|
||||
/// Default is an entire epoch, i.e. leader schedule for epoch X is calculated at
|
||||
|
@ -17,7 +21,7 @@ pub const MAX_LEADER_SCHEDULE_EPOCH_OFFSET: u64 = 3;
|
|||
pub const MINIMUM_SLOTS_PER_EPOCH: u64 = 32;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Deserialize, Serialize, AbiExample)]
|
||||
#[derive(Debug, Copy, PartialEq, Deserialize, Serialize, AbiExample)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct EpochSchedule {
|
||||
/// The maximum number of slots in each epoch.
|
||||
|
@ -47,6 +51,21 @@ impl Default for EpochSchedule {
|
|||
}
|
||||
}
|
||||
|
||||
impl Clone for EpochSchedule {
|
||||
fn clone(&self) -> Self {
|
||||
clone_zeroed(|cloned: &mut MaybeUninit<Self>| {
|
||||
let ptr = cloned.as_mut_ptr();
|
||||
unsafe {
|
||||
copy_field!(ptr, self, slots_per_epoch);
|
||||
copy_field!(ptr, self, leader_schedule_slot_offset);
|
||||
copy_field!(ptr, self, warmup);
|
||||
copy_field!(ptr, self, first_normal_epoch);
|
||||
copy_field!(ptr, self, first_normal_slot);
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl EpochSchedule {
|
||||
pub fn new(slots_per_epoch: u64) -> Self {
|
||||
Self::custom(slots_per_epoch, slots_per_epoch, true)
|
||||
|
@ -229,4 +248,18 @@ mod tests {
|
|||
assert!(last_slots_in_epoch == slots_per_epoch);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_clone() {
|
||||
let epoch_schedule = EpochSchedule {
|
||||
slots_per_epoch: 1,
|
||||
leader_schedule_slot_offset: 2,
|
||||
warmup: true,
|
||||
first_normal_epoch: 4,
|
||||
first_normal_slot: 5,
|
||||
};
|
||||
#[allow(clippy::clone_on_copy)]
|
||||
let cloned_epoch_schedule = epoch_schedule.clone();
|
||||
assert_eq!(cloned_epoch_schedule, epoch_schedule);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ use {
|
|||
log::*,
|
||||
};
|
||||
|
||||
#[derive(Serialize, Deserialize, Default, PartialEq, Eq, Clone, Debug, AbiExample)]
|
||||
#[derive(Serialize, Deserialize, Default, PartialEq, Eq, Clone, Copy, Debug, AbiExample)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct FeeCalculator {
|
||||
/// The current cost of a signature.
|
||||
|
|
|
@ -798,6 +798,25 @@ macro_rules! unchecked_div_by_const {
|
|||
}};
|
||||
}
|
||||
|
||||
use std::{mem::MaybeUninit, ptr::write_bytes};
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! copy_field {
|
||||
($ptr:expr, $self:ident, $field:ident) => {
|
||||
std::ptr::addr_of_mut!((*$ptr).$field).write($self.$field)
|
||||
};
|
||||
}
|
||||
|
||||
pub fn clone_zeroed<T, F>(clone: F) -> T
|
||||
where
|
||||
F: Fn(&mut MaybeUninit<T>),
|
||||
{
|
||||
let mut value = MaybeUninit::<T>::uninit();
|
||||
unsafe { write_bytes(&mut value, 0, 1) }
|
||||
clone(&mut value);
|
||||
unsafe { value.assume_init() }
|
||||
}
|
||||
|
||||
// This module is purposefully listed after all other exports: because of an
|
||||
// interaction within rustdoc between the reexports inside this module of
|
||||
// `solana_program`'s top-level modules, and `solana_sdk`'s glob re-export of
|
||||
|
|
|
@ -3,10 +3,15 @@
|
|||
//! [rent]: https://docs.solana.com/implemented-proposals/rent
|
||||
|
||||
#![allow(clippy::integer_arithmetic)]
|
||||
use crate::clock::DEFAULT_SLOTS_PER_EPOCH;
|
||||
//! configuration for network rent
|
||||
|
||||
use {
|
||||
crate::{clock::DEFAULT_SLOTS_PER_EPOCH, clone_zeroed, copy_field},
|
||||
std::mem::MaybeUninit,
|
||||
};
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Serialize, Deserialize, PartialEq, Clone, Copy, Debug, AbiExample)]
|
||||
#[derive(Serialize, Deserialize, PartialEq, Copy, Debug, AbiExample)]
|
||||
pub struct Rent {
|
||||
/// Rental rate
|
||||
pub lamports_per_byte_year: u64,
|
||||
|
@ -44,6 +49,19 @@ impl Default for Rent {
|
|||
}
|
||||
}
|
||||
|
||||
impl Clone for Rent {
|
||||
fn clone(&self) -> Self {
|
||||
clone_zeroed(|cloned: &mut MaybeUninit<Self>| {
|
||||
let ptr = cloned.as_mut_ptr();
|
||||
unsafe {
|
||||
copy_field!(ptr, self, lamports_per_byte_year);
|
||||
copy_field!(ptr, self, exemption_threshold);
|
||||
copy_field!(ptr, self, burn_percent);
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Rent {
|
||||
/// calculate how much rent to burn from the collected rent
|
||||
pub fn calculate_burn(&self, rent_collected: u64) -> (u64, u64) {
|
||||
|
@ -191,4 +209,16 @@ mod tests {
|
|||
assert!(RentDue::Exempt.is_exempt());
|
||||
assert!(!RentDue::Paying(0).is_exempt());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_clone() {
|
||||
let rent = Rent {
|
||||
lamports_per_byte_year: 1,
|
||||
exemption_threshold: 2.2,
|
||||
burn_percent: 3,
|
||||
};
|
||||
#[allow(clippy::clone_on_copy)]
|
||||
let cloned_rent = rent.clone();
|
||||
assert_eq!(cloned_rent, rent);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,8 +2,12 @@
|
|||
//!
|
||||
#![allow(deprecated)]
|
||||
|
||||
use crate::{
|
||||
fee_calculator::FeeCalculator, impl_sysvar_get, program_error::ProgramError, sysvar::Sysvar,
|
||||
use {
|
||||
crate::{
|
||||
clone_zeroed, copy_field, fee_calculator::FeeCalculator, impl_sysvar_get,
|
||||
program_error::ProgramError, sysvar::Sysvar,
|
||||
},
|
||||
std::mem::MaybeUninit,
|
||||
};
|
||||
|
||||
crate::declare_deprecated_sysvar_id!("SysvarFees111111111111111111111111111111111", Fees);
|
||||
|
@ -13,15 +17,27 @@ crate::declare_deprecated_sysvar_id!("SysvarFees11111111111111111111111111111111
|
|||
note = "Please do not use, will no longer be available in the future"
|
||||
)]
|
||||
#[repr(C)]
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
|
||||
#[derive(Serialize, Deserialize, Debug, Default, PartialEq)]
|
||||
pub struct Fees {
|
||||
pub fee_calculator: FeeCalculator,
|
||||
}
|
||||
|
||||
impl Clone for Fees {
|
||||
fn clone(&self) -> Self {
|
||||
clone_zeroed(|cloned: &mut MaybeUninit<Self>| {
|
||||
let ptr = cloned.as_mut_ptr();
|
||||
unsafe {
|
||||
copy_field!(ptr, self, fee_calculator);
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Fees {
|
||||
pub fn new(fee_calculator: &FeeCalculator) -> Self {
|
||||
#[allow(deprecated)]
|
||||
Self {
|
||||
fee_calculator: fee_calculator.clone(),
|
||||
fee_calculator: *fee_calculator,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -29,3 +45,19 @@ impl Fees {
|
|||
impl Sysvar for Fees {
|
||||
impl_sysvar_get!(sol_get_fees_sysvar);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_clone() {
|
||||
let fees = Fees {
|
||||
fee_calculator: FeeCalculator {
|
||||
lamports_per_signature: 1,
|
||||
},
|
||||
};
|
||||
let cloned_fees = fees.clone();
|
||||
assert_eq!(cloned_fees, fees);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue