diff --git a/account-decoder/src/parse_sysvar.rs b/account-decoder/src/parse_sysvar.rs index e5896f9977..b52e0e0476 100644 --- a/account-decoder/src/parse_sysvar.rs +++ b/account-decoder/src/parse_sysvar.rs @@ -40,7 +40,7 @@ pub fn parse_sysvar(data: &[u8], pubkey: &Pubkey) -> Result::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::::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::::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::::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, } diff --git a/sdk/program/src/clock.rs b/sdk/program/src/clock.rs index 5cf5b27fc4..da3bb683a0 100644 --- a/sdk/program/src/clock.rs +++ b/sdk/program/src/clock.rs @@ -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| { + 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); + } +} diff --git a/sdk/program/src/epoch_schedule.rs b/sdk/program/src/epoch_schedule.rs index 26365f7e36..c81fa37c4e 100644 --- a/sdk/program/src/epoch_schedule.rs +++ b/sdk/program/src/epoch_schedule.rs @@ -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| { + 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); + } } diff --git a/sdk/program/src/fee_calculator.rs b/sdk/program/src/fee_calculator.rs index cd7f7a8ece..b190f050c4 100644 --- a/sdk/program/src/fee_calculator.rs +++ b/sdk/program/src/fee_calculator.rs @@ -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 diff --git a/sdk/program/src/lib.rs b/sdk/program/src/lib.rs index e87d65af71..ad515ebe5e 100644 --- a/sdk/program/src/lib.rs +++ b/sdk/program/src/lib.rs @@ -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(clone: F) -> T +where + F: Fn(&mut MaybeUninit), +{ + let mut value = MaybeUninit::::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; diff --git a/sdk/program/src/rent.rs b/sdk/program/src/rent.rs index 88f6aeb8d4..eb54adf04a 100644 --- a/sdk/program/src/rent.rs +++ b/sdk/program/src/rent.rs @@ -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| { + 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); + } } diff --git a/sdk/program/src/sysvar/fees.rs b/sdk/program/src/sysvar/fees.rs index 24a8823953..5675901f7a 100644 --- a/sdk/program/src/sysvar/fees.rs +++ b/sdk/program/src/sysvar/fees.rs @@ -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| { + 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); + } +}