Rent tangential stuff (#5910)

* rename rent.rs to rent_calculator.rs

* add rent sysvar

* integrate rent_calculator with bank

* rent_calculator integration with genesis

* add test for rent sysvar
This commit is contained in:
Parth
2019-09-17 17:12:55 +05:30
committed by GitHub
parent b31d334ef4
commit d63518a835
11 changed files with 157 additions and 30 deletions

View File

@ -7,7 +7,7 @@ use crate::hash::{hash, Hash};
use crate::inflation::Inflation;
use crate::poh_config::PohConfig;
use crate::pubkey::Pubkey;
use crate::rent::Rent;
use crate::rent_calculator::RentCalculator;
use crate::signature::{Keypair, KeypairUtil};
use crate::system_program::{self, solana_system_program};
use bincode::{deserialize, serialize};
@ -28,8 +28,8 @@ pub struct GenesisBlock {
pub slots_per_segment: u64,
pub poh_config: PohConfig,
pub fee_calculator: FeeCalculator,
pub rent_calculator: RentCalculator,
pub inflation: Inflation,
pub rent: Rent,
}
// useful for basic tests
@ -61,7 +61,7 @@ impl Default for GenesisBlock {
poh_config: PohConfig::default(),
inflation: Inflation::default(),
fee_calculator: FeeCalculator::default(),
rent: Rent::default(),
rent_calculator: RentCalculator::default(),
}
}
}
@ -147,6 +147,10 @@ impl Builder {
self.genesis_block.inflation = inflation;
self
}
pub fn rent_calculator(mut self, rent_calculator: RentCalculator) -> Self {
self.genesis_block.rent_calculator = rent_calculator;
self
}
}
impl GenesisBlock {

View File

@ -13,7 +13,7 @@ pub mod native_loader;
pub mod packet;
pub mod poh_config;
pub mod pubkey;
pub mod rent;
pub mod rent_calculator;
pub mod rpc_port;
pub mod short_vec;
pub mod system_instruction;

View File

@ -1,7 +1,7 @@
//! configuration for network rent
#[derive(Serialize, Deserialize, PartialEq, Clone, Copy, Debug)]
pub struct Rent {
pub struct RentCalculator {
/// Rental rate
pub lamports_per_byte_year: u64,
@ -25,7 +25,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 Rent {
impl Default for RentCalculator {
fn default() -> Self {
Self {
lamports_per_byte_year: DEFAULT_LAMPORTS_PER_BYTE_YEAR,
@ -35,7 +35,7 @@ impl Default for Rent {
}
}
impl Rent {
impl RentCalculator {
/// 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 +66,17 @@ mod tests {
#[test]
fn test_due() {
let rent = Rent::default();
let rent_calculator = RentCalculator::default();
assert_eq!(
rent.due(0, 1, 1.0),
rent_calculator.due(0, 1, 1.0),
(
DEFAULT_LAMPORTS_PER_BYTE_YEAR,
DEFAULT_LAMPORTS_PER_BYTE_YEAR == 0
)
);
assert_eq!(
rent.due(
rent_calculator.due(
DEFAULT_LAMPORTS_PER_BYTE_YEAR * DEFAULT_EXEMPTION_THRESHOLD as u64,
1,
1.0
@ -94,21 +94,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 = Rent::default();
// let rent_calculator = RentCalculator::default();
//
// eprintln();
// // lamports charged per byte per slot at $1/MByear, rent per slot is zero
// eprintln(
// "{} lamports per byte-slot, rent.due(): {}",
// "{} lamports per byte-slot, rent_calculator.due(): {}",
// (1.0 / SLOTS_PER_YEAR) * DEFAULT_LAMPORTS_PER_BYTE_YEAR as f64,
// rent.due(0, 1, 1.0 / SLOTS_PER_YEAR).0,
// rent_calculator.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.due(): {}",
// "{} lamports per byte-epoch, rent_calculator.due(): {}",
// (1.0 / SLOTS_PER_YEAR)
// * (DEFAULT_LAMPORTS_PER_BYTE_YEAR * DEFAULT_SLOTS_PER_EPOCH) as f64,
// rent.due(
// rent_calculator.due(
// 0,
// 1,
// (1.0 / SLOTS_PER_YEAR) * DEFAULT_SLOTS_PER_EPOCH as f64
@ -119,7 +119,7 @@ mod tests {
// eprintln(
// "stake_history: {}kB == {} lamports per epoch",
// crate::sysvar::stake_history::StakeHistory::size_of() / 1024,
// rent.due(
// rent_calculator.due(
// 0,
// crate::sysvar::stake_history::StakeHistory::size_of(),
// (1.0 / SLOTS_PER_YEAR) * DEFAULT_SLOTS_PER_EPOCH as f64

View File

@ -4,6 +4,7 @@ use crate::pubkey::Pubkey;
pub mod clock;
pub mod fees;
pub mod rent;
pub mod rewards;
pub mod slot_hashes;
pub mod stake_history;

63
sdk/src/sysvar/rent.rs Normal file
View File

@ -0,0 +1,63 @@
//! This account contains the current cluster rent
//!
use crate::account::Account;
use crate::account_info::AccountInfo;
use crate::rent_calculator::RentCalculator;
use crate::sysvar;
use bincode::serialized_size;
/// rent account pubkey
const ID: [u8; 32] = [
6, 167, 213, 23, 25, 44, 92, 81, 33, 140, 201, 76, 61, 74, 241, 127, 88, 218, 238, 8, 155, 161,
253, 68, 227, 219, 217, 138, 0, 0, 0, 0,
];
crate::solana_name_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()
}
pub fn to_account(&self, account: &mut Account) -> Option<()> {
account.serialize_data(self).ok()
}
pub fn from_account_info(account: &AccountInfo) -> Option<Self> {
account.deserialize_data().ok()
}
pub fn to_account_info(&self, account: &mut AccountInfo) -> Option<()> {
account.serialize_data(self).ok()
}
pub fn size_of() -> usize {
serialized_size(&Rent::default()).unwrap() as usize
}
}
pub fn create_account(lamports: u64, rent_calculator: &RentCalculator) -> Account {
Account::new_data(
lamports,
&Rent {
rent_calculator: *rent_calculator,
},
&sysvar::id(),
)
.unwrap()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_fees_create_account() {
let lamports = 42;
let account = create_account(lamports, &RentCalculator::default());
let rent = Rent::from_account(&account).unwrap();
assert_eq!(rent.rent_calculator, RentCalculator::default());
}
}