refactor clone

This commit is contained in:
Jack May
2022-04-13 22:46:50 -07:00
parent 468d9b3933
commit ad6df47c24
10 changed files with 172 additions and 21 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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);
}
}