From 4aaf911142fd86e15d1add004cc14bc92cfecf73 Mon Sep 17 00:00:00 2001 From: Parth Desai Date: Thu, 14 Nov 2019 09:53:36 +0530 Subject: [PATCH] fix bug + more precise test --- sdk/src/rent.rs | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/sdk/src/rent.rs b/sdk/src/rent.rs index 502e0c24acb6b9..b0914d52049f93 100644 --- a/sdk/src/rent.rs +++ b/sdk/src/rent.rs @@ -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 } @@ -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)