kill rent calculator (#6625)

This commit is contained in:
Rob Walker
2019-10-30 16:25:12 -07:00
committed by GitHub
parent d2d78a073f
commit fa12a5f70b
14 changed files with 74 additions and 86 deletions

View File

@ -9,7 +9,7 @@ use crate::{
inflation::Inflation,
poh_config::PohConfig,
pubkey::Pubkey,
rent_calculator::RentCalculator,
rent::Rent,
signature::{Keypair, KeypairUtil},
system_program::{self, solana_system_program},
};
@ -36,7 +36,7 @@ pub struct GenesisBlock {
pub slots_per_segment: u64,
pub poh_config: PohConfig,
pub fee_calculator: FeeCalculator,
pub rent_calculator: RentCalculator,
pub rent: Rent,
pub inflation: Inflation,
pub epoch_schedule: EpochSchedule,
pub operating_mode: OperatingMode,
@ -68,7 +68,7 @@ impl Default for GenesisBlock {
poh_config: PohConfig::default(),
inflation: Inflation::default(),
fee_calculator: FeeCalculator::default(),
rent_calculator: RentCalculator::default(),
rent: Rent::default(),
epoch_schedule: EpochSchedule::default(),
operating_mode: OperatingMode::Development,
}

View File

@ -15,7 +15,7 @@ pub mod native_loader;
pub mod native_token;
pub mod poh_config;
pub mod pubkey;
pub mod rent_calculator;
pub mod rent;
pub mod rpc_port;
pub mod short_vec;
pub mod slot_hashes;

View File

@ -1,7 +1,8 @@
//! configuration for network rent
#[repr(C)]
#[derive(Serialize, Deserialize, PartialEq, Clone, Copy, Debug)]
pub struct RentCalculator {
pub struct Rent {
/// Rental rate
pub lamports_per_byte_year: u64,
@ -25,7 +26,7 @@ pub const DEFAULT_EXEMPTION_THRESHOLD: f64 = 2.0;
/// default amount of rent to burn, as a fraction of std::u8::MAX
pub const DEFAULT_BURN_PERCENT: u8 = ((50usize * std::u8::MAX as usize) / 100usize) as u8;
impl Default for RentCalculator {
impl Default for Rent {
fn default() -> Self {
Self {
lamports_per_byte_year: DEFAULT_LAMPORTS_PER_BYTE_YEAR,
@ -35,7 +36,7 @@ impl Default for RentCalculator {
}
}
impl RentCalculator {
impl Rent {
/// minimum balance due for a given size Account::data.len()
pub fn minimum_balance(&self, data_len: usize) -> u64 {
let bytes = data_len as u64;
@ -66,17 +67,17 @@ mod tests {
#[test]
fn test_due() {
let rent_calculator = RentCalculator::default();
let rent = Rent::default();
assert_eq!(
rent_calculator.due(0, 1, 1.0),
rent.due(0, 1, 1.0),
(
DEFAULT_LAMPORTS_PER_BYTE_YEAR,
DEFAULT_LAMPORTS_PER_BYTE_YEAR == 0
)
);
assert_eq!(
rent_calculator.due(
rent.due(
DEFAULT_LAMPORTS_PER_BYTE_YEAR * DEFAULT_EXEMPTION_THRESHOLD as u64,
1,
1.0
@ -94,21 +95,21 @@ mod tests {
// const SLOTS_PER_YEAR: f64 =
// SECONDS_PER_YEAR / (DEFAULT_TICKS_PER_SLOT as f64 / DEFAULT_TICKS_PER_SECOND as f64);
//
// let rent_calculator = RentCalculator::default();
// let rent = Rent::default();
//
// eprintln();
// // lamports charged per byte per slot at $1/MByear, rent per slot is zero
// eprintln(
// "{} lamports per byte-slot, rent_calculator.due(): {}",
// "{} lamports per byte-slot, rent.due(): {}",
// (1.0 / SLOTS_PER_YEAR) * DEFAULT_LAMPORTS_PER_BYTE_YEAR as f64,
// rent_calculator.due(0, 1, 1.0 / SLOTS_PER_YEAR).0,
// rent.due(0, 1, 1.0 / SLOTS_PER_YEAR).0,
// );
// // lamports charged per byte per _epoch_ starts to have some significant digits
// eprintln(
// "{} lamports per byte-epoch, rent_calculator.due(): {}",
// "{} lamports per byte-epoch, rent.due(): {}",
// (1.0 / SLOTS_PER_YEAR)
// * (DEFAULT_LAMPORTS_PER_BYTE_YEAR * DEFAULT_SLOTS_PER_EPOCH) as f64,
// rent_calculator.due(
// rent.due(
// 0,
// 1,
// (1.0 / SLOTS_PER_YEAR) * DEFAULT_SLOTS_PER_EPOCH as f64
@ -119,7 +120,7 @@ mod tests {
// eprintln(
// "stake_history: {}kB == {} lamports per epoch",
// crate::sysvar::stake_history::StakeHistory::size_of() / 1024,
// rent_calculator.due(
// rent.due(
// 0,
// crate::sysvar::stake_history::StakeHistory::size_of(),
// (1.0 / SLOTS_PER_YEAR) * DEFAULT_SLOTS_PER_EPOCH as f64

View File

@ -1,10 +1,11 @@
//! This account contains the current cluster rent
//!
pub use crate::rent::Rent;
use crate::{
account::{Account, KeyedAccount},
account_info::AccountInfo,
instruction::InstructionError,
rent_calculator::RentCalculator,
sysvar,
};
use bincode::serialized_size;
@ -17,12 +18,6 @@ const ID: [u8; 32] = [
crate::solana_sysvar_id!(ID, "SysvarRent111111111111111111111111111111111");
#[repr(C)]
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct Rent {
pub rent_calculator: RentCalculator,
}
impl Rent {
pub fn from_account(account: &Account) -> Option<Self> {
account.deserialize_data().ok()
@ -41,15 +36,8 @@ impl Rent {
}
}
pub fn create_account(lamports: u64, rent_calculator: &RentCalculator) -> Account {
Account::new_data(
lamports,
&Rent {
rent_calculator: *rent_calculator,
},
&sysvar::id(),
)
.unwrap()
pub fn create_account(lamports: u64, rent: &Rent) -> Account {
Account::new_data(lamports, rent, &sysvar::id()).unwrap()
}
pub fn from_keyed_account(account: &KeyedAccount) -> Result<Rent, InstructionError> {
@ -64,7 +52,6 @@ pub fn verify_rent_exemption(
rent_sysvar_account: &KeyedAccount,
) -> Result<(), InstructionError> {
if !from_keyed_account(rent_sysvar_account)?
.rent_calculator
.is_exempt(account.account.lamports, account.account.data.len())
{
Err(InstructionError::InsufficientFunds)
@ -80,8 +67,8 @@ mod tests {
#[test]
fn test_rent_create_account() {
let lamports = 42;
let account = create_account(lamports, &RentCalculator::default());
let account = create_account(lamports, &Rent::default());
let rent = Rent::from_account(&account).unwrap();
assert_eq!(rent.rent_calculator, RentCalculator::default());
assert_eq!(rent, Rent::default());
}
}