plumb some rent (#5610)

* plumb some rent

* nits

* fixups

* fixups

* fixups
This commit is contained in:
Rob Walker
2019-08-23 14:04:53 -07:00
committed by GitHub
parent 9b8d59d2e9
commit 0ffe7a9c8f
19 changed files with 385 additions and 143 deletions

View File

@ -1,4 +1,5 @@
use crate::pubkey::Pubkey;
use crate::Epoch;
use std::{cmp, fmt};
/// An Account with data that is stored on chain
@ -13,6 +14,8 @@ pub struct Account {
pub owner: Pubkey,
/// this account's data contains a loaded program (and is now read-only)
pub executable: bool,
/// the epoch at which this account will next owe rent
pub rent_epoch: Epoch,
}
impl fmt::Debug for Account {
@ -25,11 +28,12 @@ impl fmt::Debug for Account {
};
write!(
f,
"Account {{ lamports: {} data.len: {} owner: {} executable: {}{} }}",
"Account {{ lamports: {} data.len: {} owner: {} executable: {} rent_epoch: {}{} }}",
self.lamports,
self.data.len(),
self.owner,
self.executable,
self.rent_epoch,
data_str,
)
}
@ -42,7 +46,7 @@ impl Account {
lamports,
data: vec![0u8; space],
owner: *owner,
executable: false,
..Account::default()
}
}
@ -56,7 +60,7 @@ impl Account {
lamports,
data,
owner: *owner,
executable: false,
..Account::default()
})
}

View File

@ -29,7 +29,7 @@ pub struct FeeCalculator {
pub const DEFAULT_TARGET_LAMPORTS_PER_SIGNATURE: u64 = 42;
pub const DEFAULT_TARGET_SIGNATURES_PER_SLOT: usize =
710_000 * DEFAULT_TICKS_PER_SLOT as usize / DEFAULT_TICKS_PER_SECOND as usize;
pub const DEFAULT_BURN_PERCENT: u8 = 127;
pub const DEFAULT_BURN_PERCENT: u8 = ((50usize * std::u8::MAX as usize) / 100usize) as u8;
impl Default for FeeCalculator {
fn default() -> Self {

View File

@ -1,6 +1,6 @@
//! configuration for network inflation
#[derive(Serialize, Deserialize, PartialEq, Clone, Debug)]
#[derive(Serialize, Deserialize, PartialEq, Clone, Debug, Copy)]
pub struct Inflation {
/// Initial inflation percentage, from time=0
pub initial: f64,

View File

@ -28,3 +28,6 @@ pub mod transport;
#[macro_use]
extern crate serde_derive;
pub type Epoch = u64;
pub type Slot = u64;

View File

@ -14,5 +14,6 @@ pub fn create_loadable_account(name: &str) -> Account {
owner: id(),
data: name.as_bytes().to_vec(),
executable: true,
rent_epoch: 0,
}
}

View File

@ -1,12 +1,15 @@
//! configuration for network rent
#[derive(Serialize, Deserialize, PartialEq, Clone, Debug)]
#[derive(Serialize, Deserialize, PartialEq, Clone, Copy, Debug)]
pub struct Rent {
/// Rental rate
pub lamports_per_byte_year: u64,
/// exemption threshold, in years
pub exemption_threshold: f64,
// What portion of collected rent are to be destroyed, percentage-wise
pub burn_percent: u8,
}
/// default rental rate in lamports/byte-year, based on:
@ -14,16 +17,20 @@ pub struct Rent {
/// $1 per Sol
/// $0.01 per megabyte day
/// $3.65 per megabyte year
pub const DEFAULT_LAMPORTS_PER_BYTE_YEAR: u64 = 17_179_869_184 / 100 * 365 / (1024 * 1024);
pub const DEFAULT_LAMPORTS_PER_BYTE_YEAR: u64 = 0; //17_179_869_184 / 100 * 365 / (1024 * 1024);
/// default amount of time (in years) the balance has to include rent for
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 {
fn default() -> Self {
Self {
lamports_per_byte_year: DEFAULT_LAMPORTS_PER_BYTE_YEAR,
exemption_threshold: DEFAULT_EXEMPTION_THRESHOLD,
burn_percent: DEFAULT_BURN_PERCENT,
}
}
}
@ -41,12 +48,14 @@ impl Rent {
}
/// rent due on account's data_len with balance
pub fn due(&self, balance: u64, data_len: usize, years_elapsed: f64) -> u64 {
pub fn due(&self, balance: u64, data_len: usize, years_elapsed: f64) -> (u64, bool) {
if self.is_exempt(balance, data_len) {
0
(0, true)
} else {
let bytes = data_len as u64;
((self.lamports_per_byte_year * bytes) as f64 * years_elapsed) as u64
(
((self.lamports_per_byte_year * data_len as u64) as f64 * years_elapsed) as u64,
false,
)
}
}
}
@ -59,14 +68,63 @@ mod tests {
fn test_due() {
let rent = Rent::default();
assert_eq!(rent.due(0, 1, 1.0), DEFAULT_LAMPORTS_PER_BYTE_YEAR);
assert_eq!(
rent.due(0, 1, 1.0),
(
DEFAULT_LAMPORTS_PER_BYTE_YEAR,
DEFAULT_LAMPORTS_PER_BYTE_YEAR == 0
)
);
assert_eq!(
rent.due(
DEFAULT_LAMPORTS_PER_BYTE_YEAR * DEFAULT_EXEMPTION_THRESHOLD as u64,
1,
1.0
),
0
(0, true)
);
}
// uncomment me and make my eprintlns macros
// #[test]
// fn test_rent_model() {
// use crate::timing::*;
//
// const SECONDS_PER_YEAR: f64 = (365.25 * 24.0 * 60.0 * 60.0);
// const SLOTS_PER_YEAR: f64 =
// SECONDS_PER_YEAR / (DEFAULT_TICKS_PER_SLOT as f64 / DEFAULT_TICKS_PER_SECOND as f64);
//
// 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.due(): {}",
// (1.0 / SLOTS_PER_YEAR) * DEFAULT_LAMPORTS_PER_BYTE_YEAR as f64,
// 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.due(): {}",
// (1.0 / SLOTS_PER_YEAR)
// * (DEFAULT_LAMPORTS_PER_BYTE_YEAR * DEFAULT_SLOTS_PER_EPOCH) as f64,
// rent.due(
// 0,
// 1,
// (1.0 / SLOTS_PER_YEAR) * DEFAULT_SLOTS_PER_EPOCH as f64
// )
// .0,
// );
// // have a look at what a large-ish sysvar would cost, were it a real account...
// eprintln(
// "stake_history: {}kB == {} lamports per epoch",
// crate::sysvar::stake_history::StakeHistory::size_of() / 1024,
// rent.due(
// 0,
// crate::sysvar::stake_history::StakeHistory::size_of(),
// (1.0 / SLOTS_PER_YEAR) * DEFAULT_SLOTS_PER_EPOCH as f64
// )
// .0,
// );
// }
}