From 75b29456199865a2cb89ccdd528332b173ab7bde Mon Sep 17 00:00:00 2001 From: Parth Desai Date: Tue, 12 Nov 2019 19:01:20 +0530 Subject: [PATCH 01/19] avoid account copying + pre-empt rent --- runtime/src/accounts.rs | 5 ++++- runtime/src/rent_collector.rs | 14 ++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/runtime/src/accounts.rs b/runtime/src/accounts.rs index 401722e6af1168..205434e7de40b2 100644 --- a/runtime/src/accounts.rs +++ b/runtime/src/accounts.rs @@ -114,7 +114,10 @@ impl Accounts { .filter(|key| !message.program_ids().contains(key)) { let (account, rent) = AccountsDB::load(storage, ancestors, accounts_index, key) - .and_then(|(account, _)| rent_collector.update(account)) + .and_then(|(mut account, _)| { + let rent_due = rent_collector.update(&mut account); + Some((account, rent_due)) + }) .unwrap_or_default(); accounts.push(account); diff --git a/runtime/src/rent_collector.rs b/runtime/src/rent_collector.rs index 373b6fd5ca21ed..86ccd1ba111af9 100644 --- a/runtime/src/rent_collector.rs +++ b/runtime/src/rent_collector.rs @@ -33,9 +33,9 @@ impl RentCollector { // updates this account's lamports and status and returns // the account rent collected, if any // - pub fn update(&self, mut account: Account) -> Option<(Account, u64)> { - if account.data.is_empty() || account.rent_epoch > self.epoch { - Some((account, 0)) + pub fn update(&self, account: &mut Account) -> u64 { + if account.rent_epoch > self.epoch { + 0 } else { let slots_elapsed: u64 = (account.rent_epoch..=self.epoch) .map(|epoch| self.epoch_schedule.get_slots_in_epoch(epoch + 1)) @@ -51,13 +51,15 @@ impl RentCollector { if account.lamports > rent_due { account.rent_epoch = self.epoch + 1; account.lamports -= rent_due; - Some((account, rent_due)) + rent_due } else { - None + let rent_charged = account.lamports; + *account = Account::default(); + rent_charged } } else { // maybe collect rent later, leave account alone - Some((account, 0)) + 0 } } } From fe83e2c0ffa8623bf4fa86e1a326f8b8d1a9586c Mon Sep 17 00:00:00 2001 From: Parth Desai Date: Tue, 12 Nov 2019 19:14:25 +0530 Subject: [PATCH 02/19] put empty data condition back for time being --- runtime/src/rent_collector.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/src/rent_collector.rs b/runtime/src/rent_collector.rs index 86ccd1ba111af9..800193d3214f95 100644 --- a/runtime/src/rent_collector.rs +++ b/runtime/src/rent_collector.rs @@ -34,7 +34,7 @@ impl RentCollector { // the account rent collected, if any // pub fn update(&self, account: &mut Account) -> u64 { - if account.rent_epoch > self.epoch { + if account.data.is_empty() || account.rent_epoch > self.epoch { 0 } else { let slots_elapsed: u64 = (account.rent_epoch..=self.epoch) From 533bb3befd279c36fedefd0a5c066a5dbfef6bc1 Mon Sep 17 00:00:00 2001 From: Parth Desai Date: Tue, 12 Nov 2019 19:56:31 +0530 Subject: [PATCH 03/19] adding support for base rent --- runtime/src/rent_collector.rs | 2 +- sdk/src/rent.rs | 38 ++++++++++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/runtime/src/rent_collector.rs b/runtime/src/rent_collector.rs index 800193d3214f95..86ccd1ba111af9 100644 --- a/runtime/src/rent_collector.rs +++ b/runtime/src/rent_collector.rs @@ -34,7 +34,7 @@ impl RentCollector { // the account rent collected, if any // pub fn update(&self, account: &mut Account) -> u64 { - if account.data.is_empty() || account.rent_epoch > self.epoch { + if account.rent_epoch > self.epoch { 0 } else { let slots_elapsed: u64 = (account.rent_epoch..=self.epoch) diff --git a/sdk/src/rent.rs b/sdk/src/rent.rs index d150d4714f6e89..9bdb32952607ba 100644 --- a/sdk/src/rent.rs +++ b/sdk/src/rent.rs @@ -11,6 +11,8 @@ pub struct Rent { // What portion of collected rent are to be destroyed, percentage-wise pub burn_percent: u8, + + pub base_rent_per_year: u64, } /// default rental rate in lamports/byte-year, based on: @@ -26,12 +28,16 @@ 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 base rent +pub const DEFAULT_BASE_RENT_PER_YEAR: u64 = 0; + 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, + base_rent_per_year: DEFAULT_BASE_RENT_PER_YEAR, } } } @@ -40,7 +46,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 + ((self.base_rent_per_year + 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 +61,8 @@ impl Rent { (0, true) } else { ( - ((self.lamports_per_byte_year * data_len as u64) as f64 * years_elapsed) as u64, + ((self.base_rent_per_year + (self.lamports_per_byte_year * data_len as u64)) as f64 + * years_elapsed) as u64, false, ) } @@ -67,23 +75,43 @@ mod tests { #[test] fn test_due() { - let rent = Rent::default(); + let rent_with_base_zero = Rent::default(); assert_eq!( - rent.due(0, 1, 1.0), + rent_with_base_zero.due(0, 1, 1.0), ( DEFAULT_LAMPORTS_PER_BYTE_YEAR, DEFAULT_LAMPORTS_PER_BYTE_YEAR == 0 ) ); assert_eq!( - rent.due( + rent_with_base_zero.due( DEFAULT_LAMPORTS_PER_BYTE_YEAR * DEFAULT_EXEMPTION_THRESHOLD as u64, 1, 1.0 ), (0, true) ); + + let mut rent_with_nonzero_base = Rent::default(); + rent_with_nonzero_base.base_rent_per_year = 1; + + assert_eq!( + rent_with_nonzero_base.due(0, 1, 1.0), + ( + DEFAULT_LAMPORTS_PER_BYTE_YEAR + rent_with_nonzero_base.base_rent_per_year, + false + ) + ); + assert_eq!( + rent_with_nonzero_base.due( + (DEFAULT_LAMPORTS_PER_BYTE_YEAR + rent_with_nonzero_base.base_rent_per_year) + * DEFAULT_EXEMPTION_THRESHOLD as u64, + 1, + 1.0 + ), + (0, true) + ); } // uncomment me and make my eprintlns macros From cad0ca5be7481605f7f048273b7af8d854fd933b Mon Sep 17 00:00:00 2001 From: Parth Desai Date: Tue, 12 Nov 2019 20:31:54 +0530 Subject: [PATCH 04/19] take value from examples --- genesis/src/main.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/genesis/src/main.rs b/genesis/src/main.rs index 9656903fe2c5ad..a2f7c017df6b1d 100644 --- a/genesis/src/main.rs +++ b/genesis/src/main.rs @@ -97,12 +97,14 @@ fn main() -> Result<(), Box> { default_lamports_per_byte_year, default_rent_exemption_threshold, default_rent_burn_percentage, + default_base_rent_per_year, ) = { let rent = Rent::default(); ( &rent.lamports_per_byte_year.to_string(), &rent.exemption_threshold.to_string(), &rent.burn_percent.to_string(), + &rent.base_rent_per_year.to_string(), ) }; let default_target_tick_duration = @@ -213,7 +215,7 @@ fn main() -> Result<(), Box> { .default_value(default_lamports_per_byte_year) .help( "The cost in lamports that the cluster will charge per byte per year \ - for accounts with data.", + for accounts.", ), ) .arg( @@ -235,6 +237,16 @@ fn main() -> Result<(), Box> { .default_value(default_rent_burn_percentage) .help("amount of rent to burn, as a fraction of std::u8::MAX."), ) + .arg( + Arg::with_name("base_rent_per_year") + .long("base-rent-per-year") + .value_name("LAMPORTS") + .takes_value(true) + .default_value(default_base_rent_per_year) + .help( + "The cost in lamports that the cluster will charge as base rent for accounts", + ), + ) .arg( Arg::with_name("target_signatures_per_slot") .long("target-signatures-per-slot") @@ -356,6 +368,7 @@ fn main() -> Result<(), Box> { lamports_per_byte_year: value_t_or_exit!(matches, "lamports_per_byte_year", u64), exemption_threshold: value_t_or_exit!(matches, "rent_exemption_threshold", f64), burn_percent: value_t_or_exit!(matches, "rent_burn_percentage", u8), + base_rent_per_year: value_t_or_exit!(matches, "base_rent_per_year", u64), }; let mut poh_config = PohConfig::default(); From a34a47f9013c38e2b2932068eaa903b5bb3ee1b9 Mon Sep 17 00:00:00 2001 From: Parth Desai Date: Tue, 12 Nov 2019 20:41:35 +0530 Subject: [PATCH 05/19] fixing test --- runtime/src/bank.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/runtime/src/bank.rs b/runtime/src/bank.rs index c784d9d1402b12..5be81a1c8b5277 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -1673,6 +1673,7 @@ mod tests { lamports_per_byte_year: 5, exemption_threshold: 1.2, burn_percent: 5, + base_rent_per_year: 0, }; let bank = Bank::new(&genesis_config); From 60e4b70fdf8edb40224426a688e5eac0e2f70db7 Mon Sep 17 00:00:00 2001 From: Parth Desai Date: Tue, 12 Nov 2019 20:53:04 +0530 Subject: [PATCH 06/19] add parenthesis for clarity --- sdk/src/rent.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/src/rent.rs b/sdk/src/rent.rs index 9bdb32952607ba..1d796e8a4096b5 100644 --- a/sdk/src/rent.rs +++ b/sdk/src/rent.rs @@ -46,7 +46,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; - ((self.base_rent_per_year + bytes * self.lamports_per_byte_year) as f64 + ((self.base_rent_per_year + (bytes * self.lamports_per_byte_year)) as f64 * self.exemption_threshold) as u64 } From b02f8d6864acc1f8a1add7c8fad8c5373cb25c33 Mon Sep 17 00:00:00 2001 From: Parth Desai Date: Tue, 12 Nov 2019 22:53:55 +0530 Subject: [PATCH 07/19] fix tests --- core/src/rpc.rs | 4 ++-- sdk/src/rent.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/rpc.rs b/core/src/rpc.rs index edcb2f0501e417..d34181c0ef871f 100644 --- a/core/src/rpc.rs +++ b/core/src/rpc.rs @@ -1372,7 +1372,7 @@ pub mod tests { "lamports": 20, "data": [], "executable": false, - "rent_epoch": 0 + "rent_epoch": 1 }}]], "id":1}} "#, @@ -1383,7 +1383,7 @@ pub mod tests { serde_json::from_str(&expected).expect("expected response deserialization"); let result: Response = serde_json::from_str(&res.expect("actual response")) .expect("actual response deserialization"); - assert_eq!(expected, result); + assert_eq!(result, expected); } #[test] diff --git a/sdk/src/rent.rs b/sdk/src/rent.rs index 1d796e8a4096b5..bf90018d37bd38 100644 --- a/sdk/src/rent.rs +++ b/sdk/src/rent.rs @@ -29,7 +29,7 @@ pub const DEFAULT_EXEMPTION_THRESHOLD: f64 = 2.0; pub const DEFAULT_BURN_PERCENT: u8 = ((50usize * std::u8::MAX as usize) / 100usize) as u8; /// default base rent -pub const DEFAULT_BASE_RENT_PER_YEAR: u64 = 0; +pub const DEFAULT_BASE_RENT_PER_YEAR: u64 = 112 * DEFAULT_LAMPORTS_PER_BYTE_YEAR; impl Default for Rent { fn default() -> Self { From 22d8d6f2793d9ce2c7b91c81e633c86a7cf08c17 Mon Sep 17 00:00:00 2001 From: Parth Desai Date: Wed, 13 Nov 2019 00:31:12 +0530 Subject: [PATCH 08/19] adding comments --- sdk/src/rent.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/sdk/src/rent.rs b/sdk/src/rent.rs index bf90018d37bd38..7bb2f4f0b3e42a 100644 --- a/sdk/src/rent.rs +++ b/sdk/src/rent.rs @@ -28,8 +28,11 @@ 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 base rent -pub const DEFAULT_BASE_RENT_PER_YEAR: u64 = 112 * DEFAULT_LAMPORTS_PER_BYTE_YEAR; +/// default account size (determined by size_of::()) +pub const DEFAULT_ACCOUNT_SIZE: u64 = 112; + +/// default base rent to be payable per account apart from rent charged on data bytes per year +pub const DEFAULT_BASE_RENT_PER_YEAR: u64 = DEFAULT_ACCOUNT_SIZE * DEFAULT_LAMPORTS_PER_BYTE_YEAR; impl Default for Rent { fn default() -> Self { From 6bb27b6426300bd5a388792fb260f80705057a59 Mon Sep 17 00:00:00 2001 From: Parth Desai Date: Wed, 13 Nov 2019 01:04:04 +0530 Subject: [PATCH 09/19] improved design --- core/src/rpc.rs | 4 ++-- genesis/src/main.rs | 15 +-------------- runtime/src/bank.rs | 1 - sdk/src/rent.rs | 22 +++++++++------------- 4 files changed, 12 insertions(+), 30 deletions(-) diff --git a/core/src/rpc.rs b/core/src/rpc.rs index d34181c0ef871f..edcb2f0501e417 100644 --- a/core/src/rpc.rs +++ b/core/src/rpc.rs @@ -1372,7 +1372,7 @@ pub mod tests { "lamports": 20, "data": [], "executable": false, - "rent_epoch": 1 + "rent_epoch": 0 }}]], "id":1}} "#, @@ -1383,7 +1383,7 @@ pub mod tests { serde_json::from_str(&expected).expect("expected response deserialization"); let result: Response = serde_json::from_str(&res.expect("actual response")) .expect("actual response deserialization"); - assert_eq!(result, expected); + assert_eq!(expected, result); } #[test] diff --git a/genesis/src/main.rs b/genesis/src/main.rs index a2f7c017df6b1d..9656903fe2c5ad 100644 --- a/genesis/src/main.rs +++ b/genesis/src/main.rs @@ -97,14 +97,12 @@ fn main() -> Result<(), Box> { default_lamports_per_byte_year, default_rent_exemption_threshold, default_rent_burn_percentage, - default_base_rent_per_year, ) = { let rent = Rent::default(); ( &rent.lamports_per_byte_year.to_string(), &rent.exemption_threshold.to_string(), &rent.burn_percent.to_string(), - &rent.base_rent_per_year.to_string(), ) }; let default_target_tick_duration = @@ -215,7 +213,7 @@ fn main() -> Result<(), Box> { .default_value(default_lamports_per_byte_year) .help( "The cost in lamports that the cluster will charge per byte per year \ - for accounts.", + for accounts with data.", ), ) .arg( @@ -237,16 +235,6 @@ fn main() -> Result<(), Box> { .default_value(default_rent_burn_percentage) .help("amount of rent to burn, as a fraction of std::u8::MAX."), ) - .arg( - Arg::with_name("base_rent_per_year") - .long("base-rent-per-year") - .value_name("LAMPORTS") - .takes_value(true) - .default_value(default_base_rent_per_year) - .help( - "The cost in lamports that the cluster will charge as base rent for accounts", - ), - ) .arg( Arg::with_name("target_signatures_per_slot") .long("target-signatures-per-slot") @@ -368,7 +356,6 @@ fn main() -> Result<(), Box> { lamports_per_byte_year: value_t_or_exit!(matches, "lamports_per_byte_year", u64), exemption_threshold: value_t_or_exit!(matches, "rent_exemption_threshold", f64), burn_percent: value_t_or_exit!(matches, "rent_burn_percentage", u8), - base_rent_per_year: value_t_or_exit!(matches, "base_rent_per_year", u64), }; let mut poh_config = PohConfig::default(); diff --git a/runtime/src/bank.rs b/runtime/src/bank.rs index 5be81a1c8b5277..c784d9d1402b12 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -1673,7 +1673,6 @@ mod tests { lamports_per_byte_year: 5, exemption_threshold: 1.2, burn_percent: 5, - base_rent_per_year: 0, }; let bank = Bank::new(&genesis_config); diff --git a/sdk/src/rent.rs b/sdk/src/rent.rs index 7bb2f4f0b3e42a..36de3067fb35f0 100644 --- a/sdk/src/rent.rs +++ b/sdk/src/rent.rs @@ -11,8 +11,6 @@ pub struct Rent { // What portion of collected rent are to be destroyed, percentage-wise pub burn_percent: u8, - - pub base_rent_per_year: u64, } /// default rental rate in lamports/byte-year, based on: @@ -28,11 +26,8 @@ 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 size (determined by size_of::()) -pub const DEFAULT_ACCOUNT_SIZE: u64 = 112; - -/// default base rent to be payable per account apart from rent charged on data bytes per year -pub const DEFAULT_BASE_RENT_PER_YEAR: u64 = DEFAULT_ACCOUNT_SIZE * DEFAULT_LAMPORTS_PER_BYTE_YEAR; +/// default account storage over head for calculation of base rent +pub const ACCOUNT_STORAGE_OVERHEAD: u64 = 32; impl Default for Rent { fn default() -> Self { @@ -40,7 +35,6 @@ impl Default for Rent { lamports_per_byte_year: DEFAULT_LAMPORTS_PER_BYTE_YEAR, exemption_threshold: DEFAULT_EXEMPTION_THRESHOLD, burn_percent: DEFAULT_BURN_PERCENT, - base_rent_per_year: DEFAULT_BASE_RENT_PER_YEAR, } } } @@ -49,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; - ((self.base_rent_per_year + (bytes * self.lamports_per_byte_year)) as f64 + let base_rent_per_year = ACCOUNT_STORAGE_OVERHEAD * self.lamports_per_byte_year; + ((base_rent_per_year + (bytes * self.lamports_per_byte_year)) as f64 * self.exemption_threshold) as u64 } @@ -64,7 +59,8 @@ impl Rent { (0, true) } else { ( - ((self.base_rent_per_year + (self.lamports_per_byte_year * data_len as u64)) as f64 + (((ACCOUNT_STORAGE_OVERHEAD * self.lamports_per_byte_year) + + (self.lamports_per_byte_year * data_len as u64)) as f64 * years_elapsed) as u64, false, ) @@ -97,18 +93,18 @@ mod tests { ); let mut rent_with_nonzero_base = Rent::default(); - rent_with_nonzero_base.base_rent_per_year = 1; + rent_with_nonzero_base.lamports_per_byte_year = 1; assert_eq!( rent_with_nonzero_base.due(0, 1, 1.0), ( - DEFAULT_LAMPORTS_PER_BYTE_YEAR + rent_with_nonzero_base.base_rent_per_year, + (1 + ACCOUNT_STORAGE_OVERHEAD) * rent_with_nonzero_base.lamports_per_byte_year, false ) ); assert_eq!( rent_with_nonzero_base.due( - (DEFAULT_LAMPORTS_PER_BYTE_YEAR + rent_with_nonzero_base.base_rent_per_year) + ((1 + ACCOUNT_STORAGE_OVERHEAD) * rent_with_nonzero_base.lamports_per_byte_year) * DEFAULT_EXEMPTION_THRESHOLD as u64, 1, 1.0 From 2d42da538683b62854d9204aae4b0775f7f9cfe0 Mon Sep 17 00:00:00 2001 From: Parth Desai Date: Wed, 13 Nov 2019 01:16:21 +0530 Subject: [PATCH 10/19] change overhead to 128 and style change --- sdk/src/rent.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sdk/src/rent.rs b/sdk/src/rent.rs index 36de3067fb35f0..96f7f2bfa5e500 100644 --- a/sdk/src/rent.rs +++ b/sdk/src/rent.rs @@ -27,7 +27,7 @@ pub const DEFAULT_EXEMPTION_THRESHOLD: f64 = 2.0; 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 = 32; +pub const ACCOUNT_STORAGE_OVERHEAD: u64 = 128; impl Default for Rent { fn default() -> Self { @@ -43,8 +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; - let base_rent_per_year = ACCOUNT_STORAGE_OVERHEAD * self.lamports_per_byte_year; - ((base_rent_per_year + (bytes * self.lamports_per_byte_year)) as f64 + (((ACCOUNT_STORAGE_OVERHEAD * self.lamports_per_byte_year) + + (bytes * self.lamports_per_byte_year)) as f64 * self.exemption_threshold) as u64 } From 96d4fb010a0ea56c65671bc1c28db0dc9ee3f4a8 Mon Sep 17 00:00:00 2001 From: Parth Desai Date: Wed, 13 Nov 2019 01:27:48 +0530 Subject: [PATCH 11/19] address review comments --- sdk/src/rent.rs | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/sdk/src/rent.rs b/sdk/src/rent.rs index 96f7f2bfa5e500..3567b5bc12fbca 100644 --- a/sdk/src/rent.rs +++ b/sdk/src/rent.rs @@ -43,8 +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 * self.lamports_per_byte_year) - + (bytes * self.lamports_per_byte_year)) as f64 + (((ACCOUNT_STORAGE_OVERHEAD * bytes) * self.lamports_per_byte_year) as f64 * self.exemption_threshold) as u64 } @@ -59,8 +58,8 @@ impl Rent { (0, true) } else { ( - (((ACCOUNT_STORAGE_OVERHEAD * self.lamports_per_byte_year) - + (self.lamports_per_byte_year * data_len as u64)) as f64 + ((self.lamports_per_byte_year * (data_len as u64 + ACCOUNT_STORAGE_OVERHEAD)) + as f64 * years_elapsed) as u64, false, ) @@ -74,17 +73,17 @@ mod tests { #[test] fn test_due() { - let rent_with_base_zero = Rent::default(); + let zero_rent = Rent::default(); assert_eq!( - rent_with_base_zero.due(0, 1, 1.0), + zero_rent.due(0, 1, 1.0), ( DEFAULT_LAMPORTS_PER_BYTE_YEAR, DEFAULT_LAMPORTS_PER_BYTE_YEAR == 0 ) ); assert_eq!( - rent_with_base_zero.due( + zero_rent.due( DEFAULT_LAMPORTS_PER_BYTE_YEAR * DEFAULT_EXEMPTION_THRESHOLD as u64, 1, 1.0 @@ -92,19 +91,19 @@ mod tests { (0, true) ); - let mut rent_with_nonzero_base = Rent::default(); - rent_with_nonzero_base.lamports_per_byte_year = 1; + let mut nonzero_rent = Rent::default(); + nonzero_rent.lamports_per_byte_year = 1; assert_eq!( - rent_with_nonzero_base.due(0, 1, 1.0), + nonzero_rent.due(0, 1, 1.0), ( - (1 + ACCOUNT_STORAGE_OVERHEAD) * rent_with_nonzero_base.lamports_per_byte_year, + (1 + ACCOUNT_STORAGE_OVERHEAD) * nonzero_rent.lamports_per_byte_year, false ) ); assert_eq!( - rent_with_nonzero_base.due( - ((1 + ACCOUNT_STORAGE_OVERHEAD) * rent_with_nonzero_base.lamports_per_byte_year) + nonzero_rent.due( + ((1 + ACCOUNT_STORAGE_OVERHEAD) * nonzero_rent.lamports_per_byte_year) * DEFAULT_EXEMPTION_THRESHOLD as u64, 1, 1.0 From 0fa20e08b21b71796dde54b2de414329f5c96acb Mon Sep 17 00:00:00 2001 From: Parth Desai Date: Wed, 13 Nov 2019 01:29:20 +0530 Subject: [PATCH 12/19] addressing review comments --- sdk/src/rent.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sdk/src/rent.rs b/sdk/src/rent.rs index 3567b5bc12fbca..994daaa11b5a8f 100644 --- a/sdk/src/rent.rs +++ b/sdk/src/rent.rs @@ -73,7 +73,8 @@ mod tests { #[test] fn test_due() { - let zero_rent = Rent::default(); + let mut zero_rent = Rent::default(); + zero_rent.lamports_per_byte_year = 0; assert_eq!( zero_rent.due(0, 1, 1.0), From cf2f580a754834e42d80f04f8ba3f376ad42d93b Mon Sep 17 00:00:00 2001 From: Parth Desai Date: Wed, 13 Nov 2019 01:40:45 +0530 Subject: [PATCH 13/19] more clear test --- sdk/src/rent.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/sdk/src/rent.rs b/sdk/src/rent.rs index 994daaa11b5a8f..b00f4a52fa4736 100644 --- a/sdk/src/rent.rs +++ b/sdk/src/rent.rs @@ -78,18 +78,15 @@ mod tests { assert_eq!( zero_rent.due(0, 1, 1.0), - ( - DEFAULT_LAMPORTS_PER_BYTE_YEAR, - DEFAULT_LAMPORTS_PER_BYTE_YEAR == 0 - ) + (zero_rent.lamports_per_byte_year, true) ); assert_eq!( zero_rent.due( - DEFAULT_LAMPORTS_PER_BYTE_YEAR * DEFAULT_EXEMPTION_THRESHOLD as u64, + zero_rent.lamports_per_byte_year * DEFAULT_EXEMPTION_THRESHOLD as u64, 1, 1.0 ), - (0, true) + (zero_rent.lamports_per_byte_year, true) ); let mut nonzero_rent = Rent::default(); From 5933bb7349099764188dd9823468d607491f05ce Mon Sep 17 00:00:00 2001 From: Rob Walker Date: Tue, 12 Nov 2019 12:25:19 -0800 Subject: [PATCH 14/19] works without 2 tests --- sdk/src/rent.rs | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/sdk/src/rent.rs b/sdk/src/rent.rs index b00f4a52fa4736..217326b6677660 100644 --- a/sdk/src/rent.rs +++ b/sdk/src/rent.rs @@ -73,40 +73,27 @@ mod tests { #[test] fn test_due() { - let mut zero_rent = Rent::default(); - zero_rent.lamports_per_byte_year = 0; + let rent = Rent::default(); assert_eq!( - zero_rent.due(0, 1, 1.0), - (zero_rent.lamports_per_byte_year, true) - ); - assert_eq!( - zero_rent.due( - zero_rent.lamports_per_byte_year * DEFAULT_EXEMPTION_THRESHOLD as u64, - 1, - 1.0 - ), - (zero_rent.lamports_per_byte_year, true) - ); - - let mut nonzero_rent = Rent::default(); - nonzero_rent.lamports_per_byte_year = 1; - - assert_eq!( - nonzero_rent.due(0, 1, 1.0), + rent.due(0, 1, 1.0), ( - (1 + ACCOUNT_STORAGE_OVERHEAD) * nonzero_rent.lamports_per_byte_year, - false + (1 + ACCOUNT_STORAGE_OVERHEAD) * DEFAULT_LAMPORTS_PER_BYTE_YEAR, + DEFAULT_LAMPORTS_PER_BYTE_YEAR == 0 ) ); assert_eq!( - nonzero_rent.due( - ((1 + ACCOUNT_STORAGE_OVERHEAD) * nonzero_rent.lamports_per_byte_year) + rent.due( + ((1 + ACCOUNT_STORAGE_OVERHEAD) * DEFAULT_LAMPORTS_PER_BYTE_YEAR) * DEFAULT_EXEMPTION_THRESHOLD as u64, 1, 1.0 ), - (0, true) + ( + ((1 + ACCOUNT_STORAGE_OVERHEAD) * DEFAULT_LAMPORTS_PER_BYTE_YEAR) + * DEFAULT_EXEMPTION_THRESHOLD as u64, + DEFAULT_LAMPORTS_PER_BYTE_YEAR == 0 + ) ); } From 11aca62f35aff1c7089771220afe89d77744dc55 Mon Sep 17 00:00:00 2001 From: Rob Walker Date: Tue, 12 Nov 2019 12:29:58 -0800 Subject: [PATCH 15/19] fixup --- sdk/src/rent.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/sdk/src/rent.rs b/sdk/src/rent.rs index 217326b6677660..e2e6ee44dcd14f 100644 --- a/sdk/src/rent.rs +++ b/sdk/src/rent.rs @@ -89,11 +89,7 @@ mod tests { 1, 1.0 ), - ( - ((1 + ACCOUNT_STORAGE_OVERHEAD) * DEFAULT_LAMPORTS_PER_BYTE_YEAR) - * DEFAULT_EXEMPTION_THRESHOLD as u64, - DEFAULT_LAMPORTS_PER_BYTE_YEAR == 0 - ) + (0, true) ); } From 859a22f3818af77d16d8d8ed942b39fe1ba2acd9 Mon Sep 17 00:00:00 2001 From: Parth Desai Date: Wed, 13 Nov 2019 10:45:33 +0530 Subject: [PATCH 16/19] fix couple of tests --- core/src/rpc.rs | 2 +- core/src/validator.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/rpc.rs b/core/src/rpc.rs index edcb2f0501e417..edb5523d37cd33 100644 --- a/core/src/rpc.rs +++ b/core/src/rpc.rs @@ -1372,7 +1372,7 @@ pub mod tests { "lamports": 20, "data": [], "executable": false, - "rent_epoch": 0 + "rent_epoch": 1 }}]], "id":1}} "#, diff --git a/core/src/validator.rs b/core/src/validator.rs index 7254088785fc57..cf478f25bc78bf 100644 --- a/core/src/validator.rs +++ b/core/src/validator.rs @@ -511,7 +511,7 @@ pub fn new_validator_for_tests() -> (Validator, ContactInfo, Keypair, PathBuf) { mut genesis_config, mint_keypair, voting_keypair, - } = create_genesis_config_with_leader(10_000, &contact_info.id, 42); + } = create_genesis_config_with_leader(10_00000, &contact_info.id, 42); genesis_config .native_instruction_processors .push(solana_budget_program!()); From 8ea2b336bc00b027c2bd58a4782ffd02e058a776 Mon Sep 17 00:00:00 2001 From: Parth Desai Date: Wed, 13 Nov 2019 13:01:05 +0530 Subject: [PATCH 17/19] fix tests --- core/src/validator.rs | 2 +- core/tests/client.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/validator.rs b/core/src/validator.rs index cf478f25bc78bf..b0aac054770c22 100644 --- a/core/src/validator.rs +++ b/core/src/validator.rs @@ -511,7 +511,7 @@ pub fn new_validator_for_tests() -> (Validator, ContactInfo, Keypair, PathBuf) { mut genesis_config, mint_keypair, voting_keypair, - } = create_genesis_config_with_leader(10_00000, &contact_info.id, 42); + } = create_genesis_config_with_leader(1_000_000, &contact_info.id, 42); genesis_config .native_instruction_processors .push(solana_budget_program!()); diff --git a/core/tests/client.rs b/core/tests/client.rs index a3f81f36df6474..7d68537a6322a0 100644 --- a/core/tests/client.rs +++ b/core/tests/client.rs @@ -23,7 +23,7 @@ fn test_rpc_client() { assert_eq!(client.get_balance(&bob_pubkey).unwrap(), 0); - assert_eq!(client.get_balance(&alice.pubkey()).unwrap(), 10000); + assert_eq!(client.get_balance(&alice.pubkey()).unwrap(), 1_000_000); let (blockhash, _fee_calculator) = client.get_recent_blockhash().unwrap(); @@ -50,7 +50,7 @@ fn test_rpc_client() { assert!(confirmed_tx); assert_eq!(client.get_balance(&bob_pubkey).unwrap(), 20); - assert_eq!(client.get_balance(&alice.pubkey()).unwrap(), 9980); + assert_eq!(client.get_balance(&alice.pubkey()).unwrap(), 999980); server.close().unwrap(); remove_dir_all(ledger_path).unwrap(); From e9ca59ab572433c05e7a027070a33903f0a5ce9e Mon Sep 17 00:00:00 2001 From: Parth Desai Date: Thu, 14 Nov 2019 09:53:36 +0530 Subject: [PATCH 18/19] fix bug + more precise test --- sdk/src/rent.rs | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/sdk/src/rent.rs b/sdk/src/rent.rs index 502e0c24acb6b9..bfa3a101fa52eb 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,21 +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), ( - (1 + ACCOUNT_STORAGE_OVERHEAD) * 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( - ((1 + ACCOUNT_STORAGE_OVERHEAD) * 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) ); From c2ec04d1a73de66fbd68a60038d1cb665f40c88e Mon Sep 17 00:00:00 2001 From: Parth Desai Date: Thu, 14 Nov 2019 10:22:44 +0530 Subject: [PATCH 19/19] fix test --- core/src/rpc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/rpc.rs b/core/src/rpc.rs index e69bc788ca055b..ebe5b082102998 100644 --- a/core/src/rpc.rs +++ b/core/src/rpc.rs @@ -1394,7 +1394,7 @@ pub mod tests { "lamports": 20, "data": [], "executable": false, - "rent_epoch": 1 + "rent_epoch": 0 }}]], "id":1}} "#,