forked from solana-labs/solana
-
Notifications
You must be signed in to change notification settings - Fork 261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SVM: add new solana-svm-rent-collector
crate
#2688
Merged
buffalojoec
merged 1 commit into
anza-xyz:master
from
buffalojoec:svm-rent-collector-crate
Aug 23, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "solana-svm-rent-collector" | ||
description = "Solana SVM Rent Collector" | ||
documentation = "https://docs.rs/solana-svm-rent-collector" | ||
version = { workspace = true } | ||
authors = { workspace = true } | ||
repository = { workspace = true } | ||
homepage = { workspace = true } | ||
license = { workspace = true } | ||
edition = { workspace = true } | ||
|
||
[dependencies] | ||
solana-sdk = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//! Solana SVM Rent Collector. | ||
//! | ||
//! Rent management for SVM. | ||
|
||
pub mod rent_state; | ||
pub mod svm_rent_collector; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//! Account rent state. | ||
|
||
/// Rent state of a Solana account. | ||
#[derive(Debug, PartialEq, Eq)] | ||
pub 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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
//! Plugin trait for rent collection within the Solana SVM. | ||
|
||
use { | ||
crate::rent_state::RentState, | ||
solana_sdk::{ | ||
account::{AccountSharedData, ReadableAccount}, | ||
clock::Epoch, | ||
pubkey::Pubkey, | ||
rent::{Rent, RentDue}, | ||
rent_collector::CollectedInfo, | ||
transaction::{Result, TransactionError}, | ||
transaction_context::{IndexOfAccount, TransactionContext}, | ||
}, | ||
}; | ||
|
||
mod rent_collector; | ||
|
||
/// Rent collector trait. Represents an entity that can evaluate the rent state | ||
/// of an account, determine rent due, and collect rent. | ||
/// | ||
/// Implementors are responsible for evaluating rent due and collecting rent | ||
/// from accounts, if required. Methods for evaluating account rent state have | ||
/// default implementations, which can be overridden for customized rent | ||
/// management. | ||
pub trait SVMRentCollector { | ||
/// Check rent state transition for an account in a transaction. | ||
/// | ||
/// This method has a default implementation that calls into | ||
/// `check_rent_state_with_account`. | ||
fn check_rent_state( | ||
&self, | ||
pre_rent_state: Option<&RentState>, | ||
post_rent_state: Option<&RentState>, | ||
transaction_context: &TransactionContext, | ||
index: IndexOfAccount, | ||
) -> 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"; | ||
self.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(), | ||
index, | ||
)?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Check rent state transition for an account directly. | ||
/// | ||
/// This method has a default implementation that checks whether the | ||
/// transition is allowed and returns an error if it is not. It also | ||
/// verifies that the account is not the incinerator. | ||
fn check_rent_state_with_account( | ||
&self, | ||
pre_rent_state: &RentState, | ||
post_rent_state: &RentState, | ||
address: &Pubkey, | ||
_account_state: &AccountSharedData, | ||
account_index: IndexOfAccount, | ||
) -> Result<()> { | ||
if !solana_sdk::incinerator::check_id(address) | ||
&& !self.transition_allowed(pre_rent_state, post_rent_state) | ||
{ | ||
let account_index = account_index as u8; | ||
Err(TransactionError::InsufficientFundsForRent { account_index }) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
|
||
/// Collect rent from an account. | ||
fn collect_rent(&self, address: &Pubkey, account: &mut AccountSharedData) -> CollectedInfo; | ||
|
||
/// Determine the rent state of an account. | ||
/// | ||
/// This method has a default implementation that treats accounts with zero | ||
/// lamports as uninitialized and uses the implemented `get_rent` to | ||
/// determine whether an account is rent-exempt. | ||
fn get_account_rent_state(&self, account: &AccountSharedData) -> RentState { | ||
if account.lamports() == 0 { | ||
RentState::Uninitialized | ||
} else if self | ||
.get_rent() | ||
.is_exempt(account.lamports(), account.data().len()) | ||
{ | ||
RentState::RentExempt | ||
} else { | ||
RentState::RentPaying { | ||
data_size: account.data().len(), | ||
lamports: account.lamports(), | ||
} | ||
} | ||
} | ||
Comment on lines
+82
to
+101
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've taken the methods from |
||
|
||
/// Get the rent collector's rent instance. | ||
fn get_rent(&self) -> &Rent; | ||
|
||
/// Get the rent due for an account. | ||
fn get_rent_due(&self, lamports: u64, data_len: usize, account_rent_epoch: Epoch) -> RentDue; | ||
|
||
/// Check whether a transition from the pre_rent_state to the | ||
/// post_rent_state is valid. | ||
/// | ||
/// This method has a default implementation that allows transitions from | ||
/// any state to `RentState::Uninitialized` or `RentState::RentExempt`. | ||
/// Pre-state `RentState::RentPaying` can only transition to | ||
/// `RentState::RentPaying` if the data size remains the same and the | ||
/// account is not credited. | ||
fn transition_allowed(&self, pre_rent_state: &RentState, post_rent_state: &RentState) -> bool { | ||
match post_rent_state { | ||
RentState::Uninitialized | RentState::RentExempt => true, | ||
RentState::RentPaying { | ||
data_size: post_data_size, | ||
lamports: post_lamports, | ||
} => { | ||
match pre_rent_state { | ||
RentState::Uninitialized | RentState::RentExempt => false, | ||
RentState::RentPaying { | ||
data_size: pre_data_size, | ||
lamports: pre_lamports, | ||
} => { | ||
// Cannot remain RentPaying if resized or credited. | ||
post_data_size == pre_data_size && post_lamports <= pre_lamports | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here I've used the same implementation as the original, but I've omitted the
debug!
(to avoid a dependency onlog
) and the metrics submission. The former omission is why_account_state
is unused here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned in the PR description, Bank's wrapper around
RentCollector
would then override this method to injectdebug!
and metrics submission.