rent collector improvments (#6888)

* avoid account copying + pre-empt rent

* adding support for base rent
This commit is contained in:
Parth
2019-11-14 10:56:49 +05:30
committed by GitHub
parent c96b8c8d68
commit 7b05b3dbb3
5 changed files with 56 additions and 19 deletions

View File

@ -26,6 +26,9 @@ 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;
/// default account storage over head for calculation of base rent
pub const ACCOUNT_STORAGE_OVERHEAD: u64 = 128;
impl Default for Rent {
fn default() -> Self {
Self {
@ -40,7 +43,8 @@ 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;
bytes * (self.exemption_threshold * self.lamports_per_byte_year as f64) as u64
(((ACCOUNT_STORAGE_OVERHEAD + bytes) * self.lamports_per_byte_year) as f64
* self.exemption_threshold) as u64
}
/// whether a given balance and data_len would be exempt
@ -54,7 +58,9 @@ impl Rent {
(0, true)
} else {
(
((self.lamports_per_byte_year * data_len as u64) as f64 * years_elapsed) as u64,
((self.lamports_per_byte_year * (data_len as u64 + ACCOUNT_STORAGE_OVERHEAD))
as f64
* years_elapsed) as u64,
false,
)
}
@ -74,20 +80,45 @@ mod tests {
#[test]
fn test_due() {
let rent = Rent::default();
let default_rent = Rent::default();
assert_eq!(
rent.due(0, 1, 1.0),
default_rent.due(0, 2, 1.2),
(
DEFAULT_LAMPORTS_PER_BYTE_YEAR,
(((2 + ACCOUNT_STORAGE_OVERHEAD) * DEFAULT_LAMPORTS_PER_BYTE_YEAR) as f64 * 1.2)
as u64,
DEFAULT_LAMPORTS_PER_BYTE_YEAR == 0
)
);
assert_eq!(
rent.due(
DEFAULT_LAMPORTS_PER_BYTE_YEAR * DEFAULT_EXEMPTION_THRESHOLD as u64,
1,
1.0
default_rent.due(
(((2 + ACCOUNT_STORAGE_OVERHEAD) * DEFAULT_LAMPORTS_PER_BYTE_YEAR) as f64
* DEFAULT_EXEMPTION_THRESHOLD) as u64,
2,
1.2
),
(0, true)
);
let mut custom_rent = Rent::default();
custom_rent.lamports_per_byte_year = 5;
custom_rent.exemption_threshold = 2.5;
assert_eq!(
custom_rent.due(0, 2, 1.2),
(
(((2 + ACCOUNT_STORAGE_OVERHEAD) * custom_rent.lamports_per_byte_year) as f64 * 1.2)
as u64,
false
)
);
assert_eq!(
custom_rent.due(
(((2 + ACCOUNT_STORAGE_OVERHEAD) * custom_rent.lamports_per_byte_year) as f64
* custom_rent.exemption_threshold) as u64,
2,
1.2
),
(0, true)
);