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
use {
    log::*,
    solana_sdk::{
        account::{AccountSharedData, ReadableAccount},
        pubkey::Pubkey,
        rent::Rent,
        transaction::{Result, TransactionError},
        transaction_context::TransactionContext,
    },
};

#[derive(Debug, PartialEq, Eq)]
pub(crate) enum RentState {
    /// account.lamports == 0
    Uninitialized,
    /// 0 < account.lamports < rent-exempt-minimum
    RentPaying {
        lamports: u64,    // account.lamports()
        data_size: usize, // account.data().len()
    },
    /// account.lamports >= rent-exempt-minimum
    RentExempt,
}

impl RentState {
    pub(crate) fn from_account(account: &AccountSharedData, rent: &Rent) -> Self {
        if account.lamports() == 0 {
            Self::Uninitialized
        } else if rent.is_exempt(account.lamports(), account.data().len()) {
            Self::RentExempt
        } else {
            Self::RentPaying {
                data_size: account.data().len(),
                lamports: account.lamports(),
            }
        }
    }

    pub(crate) fn transition_allowed_from(
        &self,
        pre_rent_state: &RentState,
        prevent_crediting_accounts_that_end_rent_paying: bool,
    ) -> bool {
        match self {
            Self::Uninitialized | Self::RentExempt => true,
            Self::RentPaying {
                data_size: post_data_size,
                lamports: post_lamports,
            } => {
                match pre_rent_state {
                    Self::Uninitialized | Self::RentExempt => false,
                    Self::RentPaying {
                        data_size: pre_data_size,
                        lamports: pre_lamports,
                    } => {
                        // Cannot remain RentPaying if resized
                        if post_data_size != pre_data_size {
                            false
                        } else if prevent_crediting_accounts_that_end_rent_paying {
                            // Cannot remain RentPaying if credited
                            post_lamports <= pre_lamports
                        } else {
                            true
                        }
                    }
                }
            }
        }
    }
}

pub(crate) fn submit_rent_state_metrics(pre_rent_state: &RentState, post_rent_state: &RentState) {
    match (pre_rent_state, post_rent_state) {
        (&RentState::Uninitialized, &RentState::RentPaying { .. }) => {
            inc_new_counter_info!("rent_paying_err-new_account", 1);
        }
        (&RentState::RentPaying { .. }, &RentState::RentPaying { .. }) => {
            inc_new_counter_info!("rent_paying_ok-legacy", 1);
        }
        (_, &RentState::RentPaying { .. }) => {
            inc_new_counter_info!("rent_paying_err-other", 1);
        }
        _ => {}
    }
}

pub(crate) fn check_rent_state(
    pre_rent_state: Option<&RentState>,
    post_rent_state: Option<&RentState>,
    transaction_context: &TransactionContext,
    index: usize,
    include_account_index_in_err: bool,
    prevent_crediting_accounts_that_end_rent_paying: bool,
) -> Result<()> {
    if let Some((pre_rent_state, post_rent_state)) = pre_rent_state.zip(post_rent_state) {
        let expect_msg = "account must exist at TransactionContext index if rent-states are Some";
        check_rent_state_with_account(
            pre_rent_state,
            post_rent_state,
            transaction_context
                .get_key_of_account_at_index(index)
                .expect(expect_msg),
            &transaction_context
                .get_account_at_index(index)
                .expect(expect_msg)
                .borrow(),
            include_account_index_in_err.then(|| index),
            prevent_crediting_accounts_that_end_rent_paying,
        )?;
    }
    Ok(())
}

pub(crate) fn check_rent_state_with_account(
    pre_rent_state: &RentState,
    post_rent_state: &RentState,
    address: &Pubkey,
    account_state: &AccountSharedData,
    account_index: Option<usize>,
    prevent_crediting_accounts_that_end_rent_paying: bool,
) -> Result<()> {
    submit_rent_state_metrics(pre_rent_state, post_rent_state);
    if !solana_sdk::incinerator::check_id(address)
        && !post_rent_state.transition_allowed_from(
            pre_rent_state,
            prevent_crediting_accounts_that_end_rent_paying,
        )
    {
        debug!(
            "Account {} not rent exempt, state {:?}",
            address, account_state,
        );
        if let Some(account_index) = account_index {
            let account_index = account_index as u8;
            Err(TransactionError::InsufficientFundsForRent { account_index })
        } else {
            Err(TransactionError::InvalidRentPayingAccount)
        }
    } else {
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use {super::*, solana_sdk::pubkey::Pubkey};

    #[test]
    fn test_from_account() {
        let program_id = Pubkey::new_unique();
        let uninitialized_account = AccountSharedData::new(0, 0, &Pubkey::default());

        let account_data_size = 100;

        let rent = Rent::free();
        let rent_exempt_account = AccountSharedData::new(1, account_data_size, &program_id); // if rent is free, all accounts with non-zero lamports and non-empty data are rent-exempt

        assert_eq!(
            RentState::from_account(&uninitialized_account, &rent),
            RentState::Uninitialized
        );
        assert_eq!(
            RentState::from_account(&rent_exempt_account, &rent),
            RentState::RentExempt
        );

        let rent = Rent::default();
        let rent_minimum_balance = rent.minimum_balance(account_data_size);
        let rent_paying_account = AccountSharedData::new(
            rent_minimum_balance.saturating_sub(1),
            account_data_size,
            &program_id,
        );
        let rent_exempt_account = AccountSharedData::new(
            rent.minimum_balance(account_data_size),
            account_data_size,
            &program_id,
        );

        assert_eq!(
            RentState::from_account(&uninitialized_account, &rent),
            RentState::Uninitialized
        );
        assert_eq!(
            RentState::from_account(&rent_paying_account, &rent),
            RentState::RentPaying {
                data_size: account_data_size,
                lamports: rent_paying_account.lamports(),
            }
        );
        assert_eq!(
            RentState::from_account(&rent_exempt_account, &rent),
            RentState::RentExempt
        );
    }

    #[test]
    fn test_transition_allowed_from() {
        check_transition_allowed_from(
            /*prevent_crediting_accounts_that_end_rent_paying:*/ false,
        );
        check_transition_allowed_from(
            /*prevent_crediting_accounts_that_end_rent_paying:*/ true,
        );
    }

    fn check_transition_allowed_from(prevent_crediting_accounts_that_end_rent_paying: bool) {
        let post_rent_state = RentState::Uninitialized;
        assert!(post_rent_state.transition_allowed_from(
            &RentState::Uninitialized,
            prevent_crediting_accounts_that_end_rent_paying
        ));
        assert!(post_rent_state.transition_allowed_from(
            &RentState::RentExempt,
            prevent_crediting_accounts_that_end_rent_paying
        ));
        assert!(post_rent_state.transition_allowed_from(
            &RentState::RentPaying {
                data_size: 0,
                lamports: 1,
            },
            prevent_crediting_accounts_that_end_rent_paying
        ));

        let post_rent_state = RentState::RentExempt;
        assert!(post_rent_state.transition_allowed_from(
            &RentState::Uninitialized,
            prevent_crediting_accounts_that_end_rent_paying
        ));
        assert!(post_rent_state.transition_allowed_from(
            &RentState::RentExempt,
            prevent_crediting_accounts_that_end_rent_paying
        ));
        assert!(post_rent_state.transition_allowed_from(
            &RentState::RentPaying {
                data_size: 0,
                lamports: 1,
            },
            prevent_crediting_accounts_that_end_rent_paying
        ));
        let post_rent_state = RentState::RentPaying {
            data_size: 2,
            lamports: 5,
        };
        assert!(!post_rent_state.transition_allowed_from(
            &RentState::Uninitialized,
            prevent_crediting_accounts_that_end_rent_paying
        ));
        assert!(!post_rent_state.transition_allowed_from(
            &RentState::RentExempt,
            prevent_crediting_accounts_that_end_rent_paying
        ));
        assert!(!post_rent_state.transition_allowed_from(
            &RentState::RentPaying {
                data_size: 3,
                lamports: 5
            },
            prevent_crediting_accounts_that_end_rent_paying
        ));
        assert!(!post_rent_state.transition_allowed_from(
            &RentState::RentPaying {
                data_size: 1,
                lamports: 5
            },
            prevent_crediting_accounts_that_end_rent_paying
        ));
        // Transition is always allowed if there is no account data resize or
        // change in account's lamports.
        assert!(post_rent_state.transition_allowed_from(
            &RentState::RentPaying {
                data_size: 2,
                lamports: 5
            },
            prevent_crediting_accounts_that_end_rent_paying
        ));
        // Transition is always allowed if there is no account data resize and
        // account's lamports is reduced.
        assert!(post_rent_state.transition_allowed_from(
            &RentState::RentPaying {
                data_size: 2,
                lamports: 7
            },
            prevent_crediting_accounts_that_end_rent_paying
        ));
        // Once the feature is activated, transition is not allowed if the
        // account is credited with more lamports and remains rent-paying.
        assert_eq!(
            post_rent_state.transition_allowed_from(
                &RentState::RentPaying {
                    data_size: 2,
                    lamports: 3
                },
                prevent_crediting_accounts_that_end_rent_paying
            ),
            !prevent_crediting_accounts_that_end_rent_paying
        );
    }
}