Skip to content

Commit

Permalink
fix bug + more precise test
Browse files Browse the repository at this point in the history
  • Loading branch information
ParthDesai committed Nov 14, 2019
1 parent fc06e64 commit 4aaf911
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions sdk/src/rent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ 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;
(((ACCOUNT_STORAGE_OVERHEAD * bytes) * self.lamports_per_byte_year) as f64
(((ACCOUNT_STORAGE_OVERHEAD + bytes) * self.lamports_per_byte_year) as f64
* self.exemption_threshold) as u64
}

Expand Down Expand Up @@ -80,20 +80,42 @@ 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.0),
(
(1 + ACCOUNT_STORAGE_OVERHEAD) * DEFAULT_LAMPORTS_PER_BYTE_YEAR,
(2 + ACCOUNT_STORAGE_OVERHEAD) * DEFAULT_LAMPORTS_PER_BYTE_YEAR,
DEFAULT_LAMPORTS_PER_BYTE_YEAR == 0
)
);
assert_eq!(
rent.due(
((1 + ACCOUNT_STORAGE_OVERHEAD) * DEFAULT_LAMPORTS_PER_BYTE_YEAR)
* DEFAULT_EXEMPTION_THRESHOLD as u64,
1,
default_rent.due(
(((2 + ACCOUNT_STORAGE_OVERHEAD) * DEFAULT_LAMPORTS_PER_BYTE_YEAR) as f64
* DEFAULT_EXEMPTION_THRESHOLD) as u64,
2,
1.0
),
(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.0),
(
(2 + ACCOUNT_STORAGE_OVERHEAD) * custom_rent.lamports_per_byte_year,
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.0
),
(0, true)
Expand Down

0 comments on commit 4aaf911

Please sign in to comment.