solana_accounts_db/
rent_collector.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
//! calculate and collect rent from Accounts
use solana_sdk::{
    account::{AccountSharedData, ReadableAccount, WritableAccount},
    clock::Epoch,
    epoch_schedule::EpochSchedule,
    genesis_config::GenesisConfig,
    incinerator,
    pubkey::Pubkey,
    rent::{Rent, RentDue},
};

#[derive(Serialize, Deserialize, Clone, PartialEq, Debug, AbiExample)]
pub struct RentCollector {
    pub epoch: Epoch,
    pub epoch_schedule: EpochSchedule,
    pub slots_per_year: f64,
    pub rent: Rent,
}

impl Default for RentCollector {
    fn default() -> Self {
        Self {
            epoch: Epoch::default(),
            epoch_schedule: EpochSchedule::default(),
            // derive default value using GenesisConfig::default()
            slots_per_year: GenesisConfig::default().slots_per_year(),
            rent: Rent::default(),
        }
    }
}

/// When rent is collected from an exempt account, rent_epoch is set to this
/// value. The idea is to have a fixed, consistent value for rent_epoch for all accounts that do not collect rent.
/// This enables us to get rid of the field completely.
pub const RENT_EXEMPT_RENT_EPOCH: Epoch = Epoch::MAX;

/// when rent is collected for this account, this is the action to apply to the account
#[derive(Debug)]
enum RentResult {
    /// this account will never have rent collected from it
    Exempt,
    /// maybe we collect rent later, but not now
    NoRentCollectionNow,
    /// collect rent
    CollectRent {
        new_rent_epoch: Epoch,
        rent_due: u64, // lamports, could be 0
    },
}

impl RentCollector {
    pub fn new(
        epoch: Epoch,
        epoch_schedule: EpochSchedule,
        slots_per_year: f64,
        rent: Rent,
    ) -> Self {
        Self {
            epoch,
            epoch_schedule,
            slots_per_year,
            rent,
        }
    }

    pub fn clone_with_epoch(&self, epoch: Epoch) -> Self {
        Self {
            epoch,
            ..self.clone()
        }
    }

    /// true if it is easy to determine this account should consider having rent collected from it
    pub fn should_collect_rent(&self, address: &Pubkey, account: &impl ReadableAccount) -> bool {
        !(account.executable() // executable accounts must be rent-exempt balance
            || *address == incinerator::id())
    }

    /// given an account that 'should_collect_rent'
    /// returns (amount rent due, is_exempt_from_rent)
    pub fn get_rent_due(&self, account: &impl ReadableAccount) -> RentDue {
        if self
            .rent
            .is_exempt(account.lamports(), account.data().len())
        {
            RentDue::Exempt
        } else {
            let account_rent_epoch = account.rent_epoch();
            let slots_elapsed: u64 = (account_rent_epoch..=self.epoch)
                .map(|epoch| self.epoch_schedule.get_slots_in_epoch(epoch + 1))
                .sum();

            // avoid infinite rent in rust 1.45
            let years_elapsed = if self.slots_per_year != 0.0 {
                slots_elapsed as f64 / self.slots_per_year
            } else {
                0.0
            };

            // we know this account is not exempt
            let due = self.rent.due_amount(account.data().len(), years_elapsed);
            RentDue::Paying(due)
        }
    }

    // Updates the account's lamports and status, and returns the amount of rent collected, if any.
    // This is NOT thread safe at some level. If we try to collect from the same account in
    // parallel, we may collect twice.
    #[must_use = "add to Bank::collected_rent"]
    pub fn collect_from_existing_account(
        &self,
        address: &Pubkey,
        account: &mut AccountSharedData,
        set_exempt_rent_epoch_max: bool,
    ) -> CollectedInfo {
        match self.calculate_rent_result(address, account) {
            RentResult::Exempt => {
                if set_exempt_rent_epoch_max {
                    account.set_rent_epoch(RENT_EXEMPT_RENT_EPOCH);
                }
                CollectedInfo::default()
            }
            RentResult::NoRentCollectionNow => CollectedInfo::default(),
            RentResult::CollectRent {
                new_rent_epoch,
                rent_due,
            } => match account.lamports().checked_sub(rent_due) {
                None | Some(0) => {
                    let account = std::mem::take(account);
                    CollectedInfo {
                        rent_amount: account.lamports(),
                        account_data_len_reclaimed: account.data().len() as u64,
                    }
                }
                Some(lamports) => {
                    account.set_lamports(lamports);
                    account.set_rent_epoch(new_rent_epoch);
                    CollectedInfo {
                        rent_amount: rent_due,
                        account_data_len_reclaimed: 0u64,
                    }
                }
            },
        }
    }

    /// determine what should happen to collect rent from this account
    #[must_use]
    fn calculate_rent_result(
        &self,
        address: &Pubkey,
        account: &impl ReadableAccount,
    ) -> RentResult {
        if account.rent_epoch() == RENT_EXEMPT_RENT_EPOCH || account.rent_epoch() > self.epoch {
            // potentially rent paying account (or known and already marked exempt)
            // Maybe collect rent later, leave account alone for now.
            return RentResult::NoRentCollectionNow;
        }
        if !self.should_collect_rent(address, account) {
            // easy to determine this account should not consider having rent collected from it
            return RentResult::Exempt;
        }
        match self.get_rent_due(account) {
            // account will not have rent collected ever
            RentDue::Exempt => RentResult::Exempt,
            // potentially rent paying account
            // Maybe collect rent later, leave account alone for now.
            RentDue::Paying(0) => RentResult::NoRentCollectionNow,
            // Rent is collected for next epoch.
            RentDue::Paying(rent_due) => RentResult::CollectRent {
                new_rent_epoch: self.epoch + 1,
                rent_due,
            },
        }
    }
}

/// Information computed during rent collection
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct CollectedInfo {
    /// Amount of rent collected from account
    pub rent_amount: u64,
    /// Size of data reclaimed from account (happens when account's lamports go to zero)
    pub account_data_len_reclaimed: u64,
}

impl std::ops::Add for CollectedInfo {
    type Output = Self;
    fn add(self, other: Self) -> Self {
        Self {
            rent_amount: self.rent_amount + other.rent_amount,
            account_data_len_reclaimed: self.account_data_len_reclaimed
                + other.account_data_len_reclaimed,
        }
    }
}

impl std::ops::AddAssign for CollectedInfo {
    fn add_assign(&mut self, other: Self) {
        *self = *self + other;
    }
}

#[cfg(test)]
mod tests {
    use {
        super::*,
        assert_matches::assert_matches,
        solana_sdk::{account::Account, sysvar},
    };

    fn default_rent_collector_clone_with_epoch(epoch: Epoch) -> RentCollector {
        RentCollector::default().clone_with_epoch(epoch)
    }

    impl RentCollector {
        #[must_use = "add to Bank::collected_rent"]
        fn collect_from_created_account(
            &self,
            address: &Pubkey,
            account: &mut AccountSharedData,
            set_exempt_rent_epoch_max: bool,
        ) -> CollectedInfo {
            // initialize rent_epoch as created at this epoch
            account.set_rent_epoch(self.epoch);
            self.collect_from_existing_account(address, account, set_exempt_rent_epoch_max)
        }
    }

    #[test]
    fn test_calculate_rent_result() {
        for set_exempt_rent_epoch_max in [false, true] {
            let mut rent_collector = RentCollector::default();

            let mut account = AccountSharedData::default();
            assert_matches!(
                rent_collector.calculate_rent_result(&Pubkey::default(), &account),
                RentResult::NoRentCollectionNow
            );
            {
                let mut account_clone = account.clone();
                assert_eq!(
                    rent_collector.collect_from_existing_account(
                        &Pubkey::default(),
                        &mut account_clone,
                        set_exempt_rent_epoch_max
                    ),
                    CollectedInfo::default()
                );
                assert_eq!(account_clone, account);
            }

            account.set_executable(true);
            assert_matches!(
                rent_collector.calculate_rent_result(&Pubkey::default(), &account),
                RentResult::Exempt
            );
            {
                let mut account_clone = account.clone();
                let mut account_expected = account.clone();
                if set_exempt_rent_epoch_max {
                    account_expected.set_rent_epoch(RENT_EXEMPT_RENT_EPOCH);
                }
                assert_eq!(
                    rent_collector.collect_from_existing_account(
                        &Pubkey::default(),
                        &mut account_clone,
                        set_exempt_rent_epoch_max
                    ),
                    CollectedInfo::default()
                );
                assert_eq!(account_clone, account_expected);
            }

            account.set_executable(false);
            assert_matches!(
                rent_collector.calculate_rent_result(&incinerator::id(), &account),
                RentResult::Exempt
            );
            {
                let mut account_clone = account.clone();
                let mut account_expected = account.clone();
                if set_exempt_rent_epoch_max {
                    account_expected.set_rent_epoch(RENT_EXEMPT_RENT_EPOCH);
                }
                assert_eq!(
                    rent_collector.collect_from_existing_account(
                        &incinerator::id(),
                        &mut account_clone,
                        set_exempt_rent_epoch_max
                    ),
                    CollectedInfo::default()
                );
                assert_eq!(account_clone, account_expected);
            }

            // try a few combinations of rent collector rent epoch and collecting rent
            for (rent_epoch, rent_due_expected) in [(2, 2), (3, 5)] {
                rent_collector.epoch = rent_epoch;
                account.set_lamports(10);
                account.set_rent_epoch(1);
                let new_rent_epoch_expected = rent_collector.epoch + 1;
                assert!(
                    matches!(
                        rent_collector.calculate_rent_result(&Pubkey::default(), &account),
                        RentResult::CollectRent{ new_rent_epoch, rent_due} if new_rent_epoch == new_rent_epoch_expected && rent_due == rent_due_expected,
                    ),
                    "{:?}",
                    rent_collector.calculate_rent_result(&Pubkey::default(), &account)
                );

                {
                    let mut account_clone = account.clone();
                    assert_eq!(
                        rent_collector.collect_from_existing_account(
                            &Pubkey::default(),
                            &mut account_clone,
                            set_exempt_rent_epoch_max
                        ),
                        CollectedInfo {
                            rent_amount: rent_due_expected,
                            account_data_len_reclaimed: 0
                        }
                    );
                    let mut account_expected = account.clone();
                    account_expected.set_lamports(account.lamports() - rent_due_expected);
                    account_expected.set_rent_epoch(new_rent_epoch_expected);
                    assert_eq!(account_clone, account_expected);
                }
            }

            // enough lamports to make us exempt
            account.set_lamports(1_000_000);
            let result = rent_collector.calculate_rent_result(&Pubkey::default(), &account);
            assert!(
                matches!(result, RentResult::Exempt),
                "{result:?}, set_exempt_rent_epoch_max: {set_exempt_rent_epoch_max}",
            );
            {
                let mut account_clone = account.clone();
                let mut account_expected = account.clone();
                if set_exempt_rent_epoch_max {
                    account_expected.set_rent_epoch(RENT_EXEMPT_RENT_EPOCH);
                }
                assert_eq!(
                    rent_collector.collect_from_existing_account(
                        &Pubkey::default(),
                        &mut account_clone,
                        set_exempt_rent_epoch_max
                    ),
                    CollectedInfo::default()
                );
                assert_eq!(account_clone, account_expected);
            }

            // enough lamports to make us exempt
            // but, our rent_epoch is set in the future, so we can't know if we are exempt yet or not.
            // We don't calculate rent amount vs data if the rent_epoch is already in the future.
            account.set_rent_epoch(1_000_000);
            assert_matches!(
                rent_collector.calculate_rent_result(&Pubkey::default(), &account),
                RentResult::NoRentCollectionNow
            );
            {
                let mut account_clone = account.clone();
                assert_eq!(
                    rent_collector.collect_from_existing_account(
                        &Pubkey::default(),
                        &mut account_clone,
                        set_exempt_rent_epoch_max
                    ),
                    CollectedInfo::default()
                );
                assert_eq!(account_clone, account);
            }
        }
    }

    #[test]
    fn test_collect_from_account_created_and_existing() {
        for set_exempt_rent_epoch_max in [false, true] {
            let old_lamports = 1000;
            let old_epoch = 1;
            let new_epoch = 2;

            let (mut created_account, mut existing_account) = {
                let account = AccountSharedData::from(Account {
                    lamports: old_lamports,
                    rent_epoch: old_epoch,
                    ..Account::default()
                });

                (account.clone(), account)
            };

            let rent_collector = default_rent_collector_clone_with_epoch(new_epoch);

            // collect rent on a newly-created account
            let collected = rent_collector.collect_from_created_account(
                &solana_sdk::pubkey::new_rand(),
                &mut created_account,
                set_exempt_rent_epoch_max,
            );
            assert!(created_account.lamports() < old_lamports);
            assert_eq!(
                created_account.lamports() + collected.rent_amount,
                old_lamports
            );
            assert_ne!(created_account.rent_epoch(), old_epoch);
            assert_eq!(collected.account_data_len_reclaimed, 0);

            // collect rent on a already-existing account
            let collected = rent_collector.collect_from_existing_account(
                &solana_sdk::pubkey::new_rand(),
                &mut existing_account,
                set_exempt_rent_epoch_max,
            );
            assert!(existing_account.lamports() < old_lamports);
            assert_eq!(
                existing_account.lamports() + collected.rent_amount,
                old_lamports
            );
            assert_ne!(existing_account.rent_epoch(), old_epoch);
            assert_eq!(collected.account_data_len_reclaimed, 0);

            // newly created account should be collected for less rent; thus more remaining balance
            assert!(created_account.lamports() > existing_account.lamports());
            assert_eq!(created_account.rent_epoch(), existing_account.rent_epoch());
        }
    }

    #[test]
    fn test_rent_exempt_temporal_escape() {
        for set_exempt_rent_epoch_max in [false, true] {
            for pass in 0..2 {
                let mut account = AccountSharedData::default();
                let epoch = 3;
                let huge_lamports = 123_456_789_012;
                let tiny_lamports = 789_012;
                let pubkey = solana_sdk::pubkey::new_rand();

                assert_eq!(account.rent_epoch(), 0);

                // create a tested rent collector
                let rent_collector = default_rent_collector_clone_with_epoch(epoch);

                if pass == 0 {
                    account.set_lamports(huge_lamports);
                    // first mark account as being collected while being rent-exempt
                    let collected = rent_collector.collect_from_existing_account(
                        &pubkey,
                        &mut account,
                        set_exempt_rent_epoch_max,
                    );
                    assert_eq!(account.lamports(), huge_lamports);
                    assert_eq!(collected, CollectedInfo::default());
                    continue;
                }

                // decrease the balance not to be rent-exempt
                // In a real validator, it is not legal to reduce an account's lamports such that the account becomes rent paying.
                // So, pass == 0 above tests the case of rent that is exempt. pass == 1 tests the case where we are rent paying.
                account.set_lamports(tiny_lamports);

                // ... and trigger another rent collection on the same epoch and check that rent is working
                let collected = rent_collector.collect_from_existing_account(
                    &pubkey,
                    &mut account,
                    set_exempt_rent_epoch_max,
                );
                assert_eq!(account.lamports(), tiny_lamports - collected.rent_amount);
                assert_ne!(collected, CollectedInfo::default());
            }
        }
    }

    #[test]
    fn test_rent_exempt_sysvar() {
        for set_exempt_rent_epoch_max in [false, true] {
            let tiny_lamports = 1;
            let mut account = AccountSharedData::default();
            account.set_owner(sysvar::id());
            account.set_lamports(tiny_lamports);

            let pubkey = solana_sdk::pubkey::new_rand();

            assert_eq!(account.rent_epoch(), 0);

            let epoch = 3;
            let rent_collector = default_rent_collector_clone_with_epoch(epoch);

            let collected = rent_collector.collect_from_existing_account(
                &pubkey,
                &mut account,
                set_exempt_rent_epoch_max,
            );
            assert_eq!(account.lamports(), 0);
            assert_eq!(collected.rent_amount, 1);
        }
    }

    /// Ensure that when an account is "rent collected" away, its data len is returned.
    #[test]
    fn test_collect_cleans_up_account() {
        for set_exempt_rent_epoch_max in [false, true] {
            solana_logger::setup();
            let account_lamports = 1; // must be *below* rent amount
            let account_data_len = 567;
            let account_rent_epoch = 11;
            let mut account = AccountSharedData::from(Account {
                lamports: account_lamports, // <-- must be below rent-exempt amount
                data: vec![u8::default(); account_data_len],
                rent_epoch: account_rent_epoch,
                ..Account::default()
            });
            let rent_collector = default_rent_collector_clone_with_epoch(account_rent_epoch + 1);

            let collected = rent_collector.collect_from_existing_account(
                &Pubkey::new_unique(),
                &mut account,
                set_exempt_rent_epoch_max,
            );

            assert_eq!(collected.rent_amount, account_lamports);
            assert_eq!(
                collected.account_data_len_reclaimed,
                account_data_len as u64
            );
            assert_eq!(account, AccountSharedData::default());
        }
    }
}