This repository was archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Add AccountsDataMeter to InvokeContext #21813
Merged
brooksprumo
merged 8 commits into
solana-labs:master
from
brooksprumo:accounts-data-len-3
Dec 28, 2021
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f75e26d
Add AccountsDataMeter to InvokeContext
brooksprumo b98418c
Tweak the API of the AccountsDataMeter to hold max/cur
brooksprumo cd82a70
Add feature gate for capping accounts data len
brooksprumo 1326aab
pr: map invalid error
brooksprumo 1c4231a
pr: fix match
brooksprumo c543da6
pr: move AccountsDataMeter to own file, and remove Rc<RefCell<>>
brooksprumo 60dc5fd
fixup: remove dup const
brooksprumo f672328
add more tests
brooksprumo 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
This file contains hidden or 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,170 @@ | ||
| //! The accounts data space has a maximum size it is permitted to grow to. This module contains | ||
| //! the constants and types for tracking and metering the accounts data space during program | ||
| //! runtime. | ||
| use solana_sdk::instruction::InstructionError; | ||
|
|
||
| /// The maximum allowed size, in bytes, of the accounts data | ||
| /// 128 GB was chosen because it is the RAM amount listed under Hardware Recommendations on | ||
| /// [Validator Requirements](https://docs.solana.com/running-validator/validator-reqs), and | ||
| /// validators often put the ledger on a RAM disk (i.e. tmpfs). | ||
| pub const MAX_ACCOUNTS_DATA_LEN: u64 = 128_000_000_000; | ||
|
|
||
| /// Meter and track the amount of available accounts data space | ||
| #[derive(Debug, Default, Clone, Copy, Eq, PartialEq)] | ||
| pub struct AccountsDataMeter { | ||
| /// The maximum amount of accounts data space that can be used (in bytes) | ||
| maximum: u64, | ||
|
|
||
| /// The current amount of accounts data space used (in bytes) | ||
| current: u64, | ||
| } | ||
|
|
||
| impl AccountsDataMeter { | ||
| /// Make a new AccountsDataMeter | ||
| pub fn new(current_accounts_data_len: u64) -> Self { | ||
| let accounts_data_meter = Self { | ||
| maximum: MAX_ACCOUNTS_DATA_LEN, | ||
| current: current_accounts_data_len, | ||
| }; | ||
| debug_assert!(accounts_data_meter.current <= accounts_data_meter.maximum); | ||
| accounts_data_meter | ||
| } | ||
|
|
||
| /// Return the maximum amount of accounts data space that can be used (in bytes) | ||
| pub fn maximum(&self) -> u64 { | ||
| self.maximum | ||
| } | ||
|
|
||
| /// Return the current amount of accounts data space used (in bytes) | ||
| pub fn current(&self) -> u64 { | ||
| self.current | ||
| } | ||
|
|
||
| /// Get the remaining amount of accounts data space (in bytes) | ||
| pub fn remaining(&self) -> u64 { | ||
| self.maximum.saturating_sub(self.current) | ||
| } | ||
|
|
||
| /// Consume accounts data space, in bytes. If `amount` is positive, we are *increasing* the | ||
| /// amount of accounts data space used. If `amount` is negative, we are *decreasing* the | ||
| /// amount of accounts data space used. | ||
| pub fn consume(&mut self, amount: i64) -> Result<(), InstructionError> { | ||
| if amount == 0 { | ||
| // nothing to do here; lets us skip doing unnecessary work in the 'else' case | ||
| return Ok(()); | ||
| } | ||
|
|
||
| if amount.is_positive() { | ||
| let amount = amount as u64; | ||
| if amount > self.remaining() { | ||
| return Err(InstructionError::AccountsDataBudgetExceeded); | ||
| } | ||
| self.current = self.current.saturating_add(amount); | ||
| } else { | ||
| let amount = amount.abs() as u64; | ||
| self.current = self.current.saturating_sub(amount); | ||
| } | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_new() { | ||
| let current = 1234; | ||
| let accounts_data_meter = AccountsDataMeter::new(current); | ||
| assert_eq!(accounts_data_meter.maximum, MAX_ACCOUNTS_DATA_LEN); | ||
| assert_eq!(accounts_data_meter.current, current); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_new_can_use_max_len() { | ||
| let _ = AccountsDataMeter::new(MAX_ACCOUNTS_DATA_LEN); | ||
| } | ||
|
|
||
| #[test] | ||
| #[should_panic] | ||
| fn test_new_panics_if_current_len_too_big() { | ||
| let _ = AccountsDataMeter::new(MAX_ACCOUNTS_DATA_LEN + 1); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_remaining() { | ||
| let current_accounts_data_len = 0; | ||
| let accounts_data_meter = AccountsDataMeter::new(current_accounts_data_len); | ||
| assert_eq!(accounts_data_meter.remaining(), MAX_ACCOUNTS_DATA_LEN); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_remaining_saturates() { | ||
| let current_accounts_data_len = 0; | ||
| let mut accounts_data_meter = AccountsDataMeter::new(current_accounts_data_len); | ||
| // To test that remaining() saturates, need to break the invariant that current <= maximum | ||
| accounts_data_meter.current = MAX_ACCOUNTS_DATA_LEN + 1; | ||
| assert_eq!(accounts_data_meter.remaining(), 0); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_consume() { | ||
| let current_accounts_data_len = 0; | ||
| let mut accounts_data_meter = AccountsDataMeter::new(current_accounts_data_len); | ||
|
|
||
| // Test: simple, positive numbers | ||
| let result = accounts_data_meter.consume(0); | ||
| assert!(result.is_ok()); | ||
| let result = accounts_data_meter.consume(1); | ||
| assert!(result.is_ok()); | ||
| let result = accounts_data_meter.consume(4); | ||
| assert!(result.is_ok()); | ||
| let result = accounts_data_meter.consume(9); | ||
| assert!(result.is_ok()); | ||
|
|
||
| // Test: can consume the remaining amount | ||
| let remaining = accounts_data_meter.remaining() as i64; | ||
| let result = accounts_data_meter.consume(remaining); | ||
| assert!(result.is_ok()); | ||
| assert_eq!(accounts_data_meter.remaining(), 0); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_consume_deallocate() { | ||
| let current_accounts_data_len = 10_000; | ||
| let mut accounts_data_meter = AccountsDataMeter::new(current_accounts_data_len); | ||
| let remaining_before = accounts_data_meter.remaining(); | ||
|
|
||
| let amount = (current_accounts_data_len / 2) as i64; | ||
| let amount = -amount; | ||
| let result = accounts_data_meter.consume(amount); | ||
| assert!(result.is_ok()); | ||
| let remaining_after = accounts_data_meter.remaining(); | ||
| assert_eq!(remaining_after, remaining_before + amount.abs() as u64); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_consume_too_much() { | ||
| let current_accounts_data_len = 0; | ||
| let mut accounts_data_meter = AccountsDataMeter::new(current_accounts_data_len); | ||
|
|
||
| // Test: consuming more than what's available (1) returns an error, (2) does not consume | ||
| let remaining = accounts_data_meter.remaining(); | ||
| let result = accounts_data_meter.consume(remaining as i64 + 1); | ||
| assert!(result.is_err()); | ||
| assert_eq!(accounts_data_meter.remaining(), remaining); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_consume_zero() { | ||
| // Pre-condition: set up the accounts data meter such that there is no remaining space | ||
| let current_accounts_data_len = 1234; | ||
| let mut accounts_data_meter = AccountsDataMeter::new(current_accounts_data_len); | ||
| accounts_data_meter.maximum = current_accounts_data_len; | ||
| assert_eq!(accounts_data_meter.remaining(), 0); | ||
|
|
||
| // Test: can always consume zero, even if there is no remaining space | ||
| let result = accounts_data_meter.consume(0); | ||
| assert!(result.is_ok()); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
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.
Edge case (with potential panic) which is not covered?
https://doc.rust-lang.org/std/primitive.i64.html#method.abs
Uh oh!
There was an error while loading. Please reload this page.
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.
Fair point! I figure since we'll never be able to deallocate more than the maximum that can be allocated,
amountshould never be less than-MAX_ACCOUNTS_DATA_LEN, which means.abs()here will be safe. Is that sufficient? Would you like a comment and/or adebug_assert!()?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.
No, makes sense. There should be an implicit
debug_assert!()in.abs()already.