@@ -46,6 +46,10 @@ pub const DEFAULT_LAMPORTS_PER_BYTE: u64 = 6_960;
4646// 1 as a 64-bit little-endian float, f64::to_le_bytes() is not stable in a
4747// const context for older rust versions
4848const DEFAULT_EXEMPTION_THRESHOLD : [ u8 ; 8 ] = [ 0 , 0 , 0 , 0 , 0 , 0 , 240 , 63 ] ;
49+ // 2 as a 64-bit little-endian float, f64::to_le_bytes() is not stable in a
50+ // const context for older rust versions. Remove this const and the match arm
51+ // using it in `minimum_balance()` once SIMD-0194 is released.
52+ const PREVIOUS_EXEMPTION_THRESHOLD : [ u8 ; 8 ] = [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 64 ] ;
4953const DEFAULT_BURN_PERCENT : u8 = 50 ;
5054
5155/// Account storage overhead for calculation of base rent.
@@ -68,11 +72,17 @@ impl Rent {
6872 /// Minimum balance due for rent-exemption of a given account data size.
6973 pub fn minimum_balance ( & self , data_len : usize ) -> u64 {
7074 let bytes = data_len as u64 ;
71- if self . unused_exemption_threshold == DEFAULT_EXEMPTION_THRESHOLD {
72- ( ACCOUNT_STORAGE_OVERHEAD + bytes) * self . lamports_per_byte
73- } else {
74- ( ( ( ACCOUNT_STORAGE_OVERHEAD + bytes) * self . lamports_per_byte ) as f64
75- * f64:: from_le_bytes ( self . unused_exemption_threshold ) ) as u64
75+ match self . unused_exemption_threshold {
76+ DEFAULT_EXEMPTION_THRESHOLD => {
77+ ( ACCOUNT_STORAGE_OVERHEAD + bytes) * self . lamports_per_byte
78+ }
79+ PREVIOUS_EXEMPTION_THRESHOLD => {
80+ 2 * ( ACCOUNT_STORAGE_OVERHEAD + bytes) * self . lamports_per_byte
81+ }
82+ _ => {
83+ ( ( ( ACCOUNT_STORAGE_OVERHEAD + bytes) * self . lamports_per_byte ) as f64
84+ * f64:: from_le_bytes ( self . unused_exemption_threshold ) ) as u64
85+ }
7686 }
7787 }
7888
@@ -117,8 +127,9 @@ mod tests {
117127 }
118128
119129 #[ test]
120- fn test_default_exemption_threshold ( ) {
130+ fn test_exemption_threshold ( ) {
121131 assert_eq ! ( 1f64 . to_le_bytes( ) , DEFAULT_EXEMPTION_THRESHOLD ) ;
132+ assert_eq ! ( 2f64 . to_le_bytes( ) , PREVIOUS_EXEMPTION_THRESHOLD ) ;
122133 }
123134
124135 proptest ! {
@@ -130,7 +141,13 @@ mod tests {
130141 unused_exemption_threshold: 2.0f64 . to_le_bytes( ) ,
131142 ..Default :: default ( )
132143 } ;
133- assert_eq!( default_rent. minimum_balance( bytes) , previous_rent. minimum_balance( bytes) ) ;
144+ let default_calc = default_rent. minimum_balance( bytes) ;
145+ assert_eq!( default_calc, previous_rent. minimum_balance( bytes) ) ;
146+
147+ // check that the calculation gives the same result using floats
148+ let float_calc = ( ( ( ACCOUNT_STORAGE_OVERHEAD + bytes as u64 ) * previous_rent. lamports_per_byte) as f64
149+ * f64 :: from_le_bytes( previous_rent. unused_exemption_threshold) ) as u64 ;
150+ assert_eq!( default_calc, float_calc) ;
134151 }
135152 }
136153}
0 commit comments