refactor clone
This commit is contained in:
parent
468d9b3933
commit
ad6df47c24
@ -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)
|
||||
|
@ -358,7 +358,7 @@ mod tests {
|
||||
context: RpcResponseContext { slot: 1 },
|
||||
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,
|
||||
}),
|
||||
@ -366,7 +366,7 @@ mod tests {
|
||||
let get_fee_calculator_for_blockhash_response = json!(Response {
|
||||
context: RpcResponseContext { slot: 1 },
|
||||
value: json!(RpcFeeCalculator {
|
||||
fee_calculator: rpc_fee_calc.clone()
|
||||
fee_calculator: rpc_fee_calc
|
||||
}),
|
||||
});
|
||||
let mut mocks = HashMap::new();
|
||||
@ -376,7 +376,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());
|
||||
@ -410,7 +410,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,
|
||||
@ -439,7 +439,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());
|
||||
|
@ -3980,7 +3980,7 @@ mod tests {
|
||||
|
||||
let src_fees = Fees {
|
||||
fee_calculator: FeeCalculator {
|
||||
lamports_per_signature: 1,
|
||||
lamports_per_signature: 1,
|
||||
},
|
||||
};
|
||||
|
||||
|
@ -2055,7 +2055,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(),
|
||||
@ -12253,7 +12253,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,
|
||||
}
|
||||
@ -12290,7 +12290,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,
|
||||
}
|
||||
@ -12323,7 +12323,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,
|
||||
}
|
||||
@ -12360,7 +12360,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 @@
|
||||
//! Provides information about the network's clock which is made up of 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);
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,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 This amount may increase/decrease over time based on
|
||||
|
@ -226,6 +226,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() }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::unchecked_div_by_const;
|
||||
|
@ -1,9 +1,13 @@
|
||||
#![allow(clippy::integer_arithmetic)]
|
||||
//! configuration for network rent
|
||||
use crate::clock::DEFAULT_SLOTS_PER_EPOCH;
|
||||
|
||||
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,
|
||||
@ -41,6 +45,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) {
|
||||
@ -183,4 +200,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…
x
Reference in New Issue
Block a user