Return the accounts data len delta after processing messages (#22986)

This commit is contained in:
Brooks Prumo
2022-02-08 19:24:47 -06:00
committed by GitHub
parent ba2d83f580
commit 869cfc9a1c
4 changed files with 67 additions and 51 deletions

View File

@ -15,18 +15,23 @@ pub struct AccountsDataMeter {
/// The maximum amount of accounts data space that can be used (in bytes) /// The maximum amount of accounts data space that can be used (in bytes)
maximum: u64, maximum: u64,
/// The current amount of accounts data space used (in bytes) /// The initial amount of accounts data space used (in bytes)
current: u64, initial: u64,
/// The amount of accounts data space that has changed since `initial` (in bytes)
delta: i64,
} }
impl AccountsDataMeter { impl AccountsDataMeter {
/// Make a new AccountsDataMeter /// Make a new AccountsDataMeter
pub fn new(current_accounts_data_len: u64) -> Self { #[must_use]
pub fn new(initial_accounts_data_len: u64) -> Self {
let accounts_data_meter = Self { let accounts_data_meter = Self {
maximum: MAX_ACCOUNTS_DATA_LEN, maximum: MAX_ACCOUNTS_DATA_LEN,
current: current_accounts_data_len, initial: initial_accounts_data_len,
delta: 0,
}; };
debug_assert!(accounts_data_meter.current <= accounts_data_meter.maximum); debug_assert!(accounts_data_meter.initial <= accounts_data_meter.maximum);
accounts_data_meter accounts_data_meter
} }
@ -35,36 +40,47 @@ impl AccountsDataMeter {
self.maximum self.maximum
} }
/// Return the initial amount of accounts data space used (in bytes)
pub fn initial(&self) -> u64 {
self.initial
}
/// Return the amount of accounts data space that has changed (in bytes)
pub fn delta(&self) -> i64 {
self.delta
}
/// Return the current amount of accounts data space used (in bytes) /// Return the current amount of accounts data space used (in bytes)
pub fn current(&self) -> u64 { pub fn current(&self) -> u64 {
self.current /// NOTE: Mixed integer ops currently not stable, so copying the impl.
/// * https://github.com/rust-lang/rust/issues/87840
/// * https://github.com/a1phyr/rust/blob/47edde1086412b36e9efd6098b191ec15a2a760a/library/core/src/num/uint_macros.rs#L1039-L1048
const fn saturating_add_signed(lhs: u64, rhs: i64) -> u64 {
let (res, overflow) = lhs.overflowing_add(rhs as u64);
if overflow == (rhs < 0) {
res
} else if overflow {
u64::MAX
} else {
u64::MIN
}
}
saturating_add_signed(self.initial, self.delta)
} }
/// Get the remaining amount of accounts data space (in bytes) /// Get the remaining amount of accounts data space (in bytes)
pub fn remaining(&self) -> u64 { pub fn remaining(&self) -> u64 {
self.maximum.saturating_sub(self.current) self.maximum.saturating_sub(self.current())
} }
/// Consume accounts data space, in bytes. If `amount` is positive, we are *increasing* the /// 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. If `amount` is negative, we are *decreasing* the
/// amount of accounts data space used. /// amount of accounts data space used.
pub fn consume(&mut self, amount: i64) -> Result<(), InstructionError> { pub fn consume(&mut self, amount: i64) -> Result<(), InstructionError> {
if amount == 0 { if amount > self.remaining() as i64 {
// nothing to do here; lets us skip doing unnecessary work in the 'else' case return Err(InstructionError::AccountsDataBudgetExceeded);
return Ok(());
} }
self.delta = self.delta.saturating_add(amount);
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(()) Ok(())
} }
} }
@ -74,8 +90,8 @@ impl AccountsDataMeter {
pub fn set_maximum(&mut self, maximum: u64) { pub fn set_maximum(&mut self, maximum: u64) {
self.maximum = maximum; self.maximum = maximum;
} }
pub fn set_current(&mut self, current: u64) { pub fn set_initial(&mut self, initial: u64) {
self.current = current; self.initial = initial;
} }
} }
@ -85,10 +101,10 @@ mod tests {
#[test] #[test]
fn test_new() { fn test_new() {
let current = 1234; let initial = 1234;
let accounts_data_meter = AccountsDataMeter::new(current); let accounts_data_meter = AccountsDataMeter::new(initial);
assert_eq!(accounts_data_meter.maximum, MAX_ACCOUNTS_DATA_LEN); assert_eq!(accounts_data_meter.maximum, MAX_ACCOUNTS_DATA_LEN);
assert_eq!(accounts_data_meter.current, current); assert_eq!(accounts_data_meter.initial, initial);
} }
#[test] #[test]
@ -98,30 +114,30 @@ mod tests {
#[test] #[test]
#[should_panic] #[should_panic]
fn test_new_panics_if_current_len_too_big() { fn test_new_panics_if_initial_len_too_big() {
let _ = AccountsDataMeter::new(MAX_ACCOUNTS_DATA_LEN + 1); let _ = AccountsDataMeter::new(MAX_ACCOUNTS_DATA_LEN + 1);
} }
#[test] #[test]
fn test_remaining() { fn test_remaining() {
let current_accounts_data_len = 0; let initial_accounts_data_len = 0;
let accounts_data_meter = AccountsDataMeter::new(current_accounts_data_len); let accounts_data_meter = AccountsDataMeter::new(initial_accounts_data_len);
assert_eq!(accounts_data_meter.remaining(), MAX_ACCOUNTS_DATA_LEN); assert_eq!(accounts_data_meter.remaining(), MAX_ACCOUNTS_DATA_LEN);
} }
#[test] #[test]
fn test_remaining_saturates() { fn test_remaining_saturates() {
let current_accounts_data_len = 0; let initial_accounts_data_len = 0;
let mut accounts_data_meter = AccountsDataMeter::new(current_accounts_data_len); let mut accounts_data_meter = AccountsDataMeter::new(initial_accounts_data_len);
// To test that remaining() saturates, need to break the invariant that current <= maximum // To test that remaining() saturates, need to break the invariant that initial <= maximum
accounts_data_meter.current = MAX_ACCOUNTS_DATA_LEN + 1; accounts_data_meter.initial = MAX_ACCOUNTS_DATA_LEN + 1;
assert_eq!(accounts_data_meter.remaining(), 0); assert_eq!(accounts_data_meter.remaining(), 0);
} }
#[test] #[test]
fn test_consume() { fn test_consume() {
let current_accounts_data_len = 0; let initial_accounts_data_len = 0;
let mut accounts_data_meter = AccountsDataMeter::new(current_accounts_data_len); let mut accounts_data_meter = AccountsDataMeter::new(initial_accounts_data_len);
// Test: simple, positive numbers // Test: simple, positive numbers
let result = accounts_data_meter.consume(0); let result = accounts_data_meter.consume(0);
@ -142,11 +158,11 @@ mod tests {
#[test] #[test]
fn test_consume_deallocate() { fn test_consume_deallocate() {
let current_accounts_data_len = 10_000; let initial_accounts_data_len = 10_000;
let mut accounts_data_meter = AccountsDataMeter::new(current_accounts_data_len); let mut accounts_data_meter = AccountsDataMeter::new(initial_accounts_data_len);
let remaining_before = accounts_data_meter.remaining(); let remaining_before = accounts_data_meter.remaining();
let amount = (current_accounts_data_len / 2) as i64; let amount = (initial_accounts_data_len / 2) as i64;
let amount = -amount; let amount = -amount;
let result = accounts_data_meter.consume(amount); let result = accounts_data_meter.consume(amount);
assert!(result.is_ok()); assert!(result.is_ok());
@ -156,8 +172,8 @@ mod tests {
#[test] #[test]
fn test_consume_too_much() { fn test_consume_too_much() {
let current_accounts_data_len = 0; let initial_accounts_data_len = 0;
let mut accounts_data_meter = AccountsDataMeter::new(current_accounts_data_len); let mut accounts_data_meter = AccountsDataMeter::new(initial_accounts_data_len);
// Test: consuming more than what's available (1) returns an error, (2) does not consume // Test: consuming more than what's available (1) returns an error, (2) does not consume
let remaining = accounts_data_meter.remaining(); let remaining = accounts_data_meter.remaining();
@ -169,9 +185,9 @@ mod tests {
#[test] #[test]
fn test_consume_zero() { fn test_consume_zero() {
// Pre-condition: set up the accounts data meter such that there is no remaining space // Pre-condition: set up the accounts data meter such that there is no remaining space
let current_accounts_data_len = 1234; let initial_accounts_data_len = 1234;
let mut accounts_data_meter = AccountsDataMeter::new(current_accounts_data_len); let mut accounts_data_meter = AccountsDataMeter::new(initial_accounts_data_len);
accounts_data_meter.maximum = current_accounts_data_len; accounts_data_meter.maximum = initial_accounts_data_len;
assert_eq!(accounts_data_meter.remaining(), 0); assert_eq!(accounts_data_meter.remaining(), 0);
// Test: can always consume zero, even if there is no remaining space // Test: can always consume zero, even if there is no remaining space

View File

@ -212,7 +212,7 @@ impl<'a> InvokeContext<'a> {
feature_set: Arc<FeatureSet>, feature_set: Arc<FeatureSet>,
blockhash: Hash, blockhash: Hash,
lamports_per_signature: u64, lamports_per_signature: u64,
current_accounts_data_len: u64, initial_accounts_data_len: u64,
) -> Self { ) -> Self {
Self { Self {
transaction_context, transaction_context,
@ -225,7 +225,7 @@ impl<'a> InvokeContext<'a> {
current_compute_budget: compute_budget, current_compute_budget: compute_budget,
compute_budget, compute_budget,
compute_meter: ComputeMeter::new_ref(compute_budget.max_units), compute_meter: ComputeMeter::new_ref(compute_budget.max_units),
accounts_data_meter: AccountsDataMeter::new(current_accounts_data_len), accounts_data_meter: AccountsDataMeter::new(initial_accounts_data_len),
executors, executors,
feature_set, feature_set,
timings: ExecuteDetailsTimings::default(), timings: ExecuteDetailsTimings::default(),
@ -1657,7 +1657,7 @@ mod tests {
invoke_context invoke_context
.accounts_data_meter .accounts_data_meter
.set_current(user_account_data_len as u64); .set_initial(user_account_data_len as u64);
invoke_context invoke_context
.accounts_data_meter .accounts_data_meter
.set_maximum(user_account_data_len as u64 * 3); .set_maximum(user_account_data_len as u64 * 3);

View File

@ -3878,7 +3878,7 @@ impl Bank {
.map(|_| info) .map(|_| info)
}) })
.map(|info| { .map(|info| {
self.store_accounts_data_len(info.accounts_data_len); self.update_accounts_data_len(info.accounts_data_len_delta);
}) })
.map_err(|err| { .map_err(|err| {
match err { match err {

View File

@ -38,8 +38,8 @@ impl ::solana_frozen_abi::abi_example::AbiExample for MessageProcessor {
/// Resultant information gathered from calling process_message() /// Resultant information gathered from calling process_message()
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct ProcessedMessageInfo { pub struct ProcessedMessageInfo {
/// The new accounts data len /// The change in accounts data len
pub accounts_data_len: u64, pub accounts_data_len_delta: i64,
} }
impl MessageProcessor { impl MessageProcessor {
@ -149,7 +149,7 @@ impl MessageProcessor {
.map_err(|err| TransactionError::InstructionError(instruction_index as u8, err))?; .map_err(|err| TransactionError::InstructionError(instruction_index as u8, err))?;
} }
Ok(ProcessedMessageInfo { Ok(ProcessedMessageInfo {
accounts_data_len: invoke_context.get_accounts_data_meter().current(), accounts_data_len_delta: invoke_context.get_accounts_data_meter().delta(),
}) })
} }
} }