@@ -26,6 +26,12 @@ pub const RENT_ID: Pubkey = [
26
26
/// - `$3.65` per megabyte year
27
27
pub const DEFAULT_LAMPORTS_PER_BYTE_YEAR : u64 = 1_000_000_000 / 100 * 365 / ( 1024 * 1024 ) ;
28
28
29
+ /// SIMD-0194
30
+ ///
31
+ /// This equates to the integer value of 3480. To account for 2 years of rent
32
+ /// exemption, we multiply this value by 2 to make it 6960.
33
+ pub const DEFAULT_LAMPORTS_PER_BYTE : u64 = 6960 ;
34
+
29
35
/// Default amount of time (in years) the balance has to include rent for the
30
36
/// account to be rent exempt.
31
37
pub const DEFAULT_EXEMPTION_THRESHOLD : f64 = 2.0 ;
@@ -39,6 +45,13 @@ const DEFAULT_EXEMPTION_THRESHOLD_AS_U64: u64 = 2;
39
45
/// This is used to check whether the `f64` value can be safely cast to a `u64`.
40
46
const F64_EXEMPTION_THRESHOLD_AS_U64 : u64 = 4611686018427387904 ;
41
47
48
+ /// The `u64` representation of the deprecated exemption threshold.
49
+ ///
50
+ /// This value is equivalent to `1f64`. It is only used to check whether
51
+ /// the exemption threshold is the deprecated value to avoid performing
52
+ /// floating-point operations on-chain.
53
+ const F64_SIMD0194_EXEMPTION_THRESHOLD_AS_U64 : u64 = 4607182418800017408 ;
54
+
42
55
/// Default percentage of collected rent that is burned.
43
56
///
44
57
/// Valid values are in the range [0, 100]. The remaining percentage is
@@ -56,9 +69,18 @@ pub const ACCOUNT_STORAGE_OVERHEAD: u64 = 128;
56
69
#[ derive( Clone , Copy , Debug ) ]
57
70
pub struct Rent {
58
71
/// Rental rate in lamports per byte-year
72
+ #[ deprecated(
73
+ since = "0.9.2" ,
74
+ note = "SIMD-0194 will rename this field to `lamports_per_byte`"
75
+ ) ]
59
76
pub lamports_per_byte_year : u64 ,
60
77
61
- /// Exemption threshold in years
78
+ /// Exemption threshold in years.
79
+ ///
80
+ /// SIMD-0194 will deprecate this value. The current implementation checks
81
+ /// the value against known defaults to avoid performing a floating-point
82
+ /// operation on-chain.
83
+ #[ deprecated( since = "0.9.2" , note = "SIMD-0194 will deprecate this value" ) ]
62
84
pub exemption_threshold : f64 ,
63
85
64
86
/// Burn percentage
@@ -150,6 +172,7 @@ impl Rent {
150
172
151
173
/// Rent due for account that is known to be not exempt.
152
174
#[ inline]
175
+ #[ allow( deprecated) ]
153
176
pub fn due_amount ( & self , data_len : usize , years_elapsed : f64 ) -> u64 {
154
177
let actual_data_len = data_len as u64 + ACCOUNT_STORAGE_OVERHEAD ;
155
178
let lamports_per_year = self . lamports_per_byte_year * actual_data_len;
@@ -169,15 +192,23 @@ impl Rent {
169
192
///
170
193
/// The minimum balance in lamports for rent exemption.
171
194
#[ inline]
195
+ #[ allow( deprecated) ]
172
196
pub fn minimum_balance ( & self , data_len : usize ) -> u64 {
173
197
let bytes = data_len as u64 ;
174
-
175
- if self . is_default_rent_threshold ( ) {
176
- ( ( ACCOUNT_STORAGE_OVERHEAD + bytes) * self . lamports_per_byte_year )
177
- * DEFAULT_EXEMPTION_THRESHOLD_AS_U64
178
- } else {
179
- ( ( ( ACCOUNT_STORAGE_OVERHEAD + bytes) * self . lamports_per_byte_year ) as f64
180
- * self . exemption_threshold ) as u64
198
+ let exemption_threshold_as_u64 = u64:: from_le_bytes ( self . exemption_threshold . to_le_bytes ( ) ) ;
199
+
200
+ match exemption_threshold_as_u64 {
201
+ F64_SIMD0194_EXEMPTION_THRESHOLD_AS_U64 => {
202
+ ( ACCOUNT_STORAGE_OVERHEAD + bytes) * self . lamports_per_byte_year
203
+ }
204
+ F64_EXEMPTION_THRESHOLD_AS_U64 => {
205
+ ( ( ACCOUNT_STORAGE_OVERHEAD + bytes) * self . lamports_per_byte_year )
206
+ * DEFAULT_EXEMPTION_THRESHOLD_AS_U64
207
+ }
208
+ _ => {
209
+ ( ( ( ACCOUNT_STORAGE_OVERHEAD + bytes) * self . lamports_per_byte_year ) as f64
210
+ * self . exemption_threshold ) as u64
211
+ }
181
212
}
182
213
}
183
214
@@ -195,15 +226,6 @@ impl Rent {
195
226
pub fn is_exempt ( & self , lamports : u64 , data_len : usize ) -> bool {
196
227
lamports >= self . minimum_balance ( data_len)
197
228
}
198
-
199
- /// Determines if the `exemption_threshold` is the default value.
200
- ///
201
- /// This is used to check whether the `f64` value can be safely cast to a `u64`
202
- /// to avoid floating-point operations.
203
- #[ inline]
204
- fn is_default_rent_threshold ( & self ) -> bool {
205
- u64:: from_le_bytes ( self . exemption_threshold . to_le_bytes ( ) ) == F64_EXEMPTION_THRESHOLD_AS_U64
206
- }
207
229
}
208
230
209
231
impl Sysvar for Rent {
@@ -238,10 +260,11 @@ impl RentDue {
238
260
}
239
261
240
262
#[ cfg( test) ]
263
+ #[ allow( deprecated) ]
241
264
mod tests {
242
265
use crate :: sysvars:: rent:: {
243
266
ACCOUNT_STORAGE_OVERHEAD , DEFAULT_BURN_PERCENT , DEFAULT_EXEMPTION_THRESHOLD ,
244
- DEFAULT_LAMPORTS_PER_BYTE_YEAR ,
267
+ DEFAULT_LAMPORTS_PER_BYTE , DEFAULT_LAMPORTS_PER_BYTE_YEAR ,
245
268
} ;
246
269
247
270
#[ test]
@@ -271,4 +294,30 @@ mod tests {
271
294
assert ! ( calculated > 0 ) ;
272
295
assert_eq ! ( balance, calculated) ;
273
296
}
297
+
298
+ #[ test]
299
+ pub fn test_minimum_balance_simd0194 ( ) {
300
+ let mut rent = super :: Rent {
301
+ lamports_per_byte_year : DEFAULT_LAMPORTS_PER_BYTE ,
302
+ exemption_threshold : 1.0 , // SIMD-0194 default
303
+ burn_percent : DEFAULT_BURN_PERCENT ,
304
+ } ;
305
+
306
+ // Using the default exemption threshold.
307
+
308
+ let balance = rent. minimum_balance ( 100 ) ;
309
+ let calculated = ( ACCOUNT_STORAGE_OVERHEAD + 100 ) * rent. lamports_per_byte_year ;
310
+
311
+ assert ! ( calculated > 0 ) ;
312
+ assert_eq ! ( balance, calculated) ;
313
+
314
+ // Using a different lamports per byte value.
315
+ rent. lamports_per_byte_year = DEFAULT_LAMPORTS_PER_BYTE * 2 ;
316
+
317
+ let balance = rent. minimum_balance ( 100 ) ;
318
+ let calculated = ( ACCOUNT_STORAGE_OVERHEAD + 100 ) * rent. lamports_per_byte_year ;
319
+
320
+ assert ! ( calculated > 0 ) ;
321
+ assert_eq ! ( balance, calculated) ;
322
+ }
274
323
}
0 commit comments