Make BeaconState.balances a tree list!

This commit is contained in:
Michael Sproul
2021-12-01 14:14:47 +11:00
parent 4b808d3c72
commit fca92c37ad
11 changed files with 76 additions and 31 deletions

View File

@@ -16,7 +16,7 @@ pub use initiate_validator_exit::initiate_validator_exit;
pub use slash_validator::slash_validator;
use safe_arith::SafeArith;
use types::{BeaconState, BeaconStateError, EthSpec};
use types::{BeaconState, BeaconStateError, EthSpec, GetBalanceMut};
/// Increase the balance of a validator, erroring upon overflow, as per the spec.
pub fn increase_balance<E: EthSpec>(
@@ -24,7 +24,10 @@ pub fn increase_balance<E: EthSpec>(
index: usize,
delta: u64,
) -> Result<(), BeaconStateError> {
state.get_balance_mut(index)?.safe_add_assign(delta)?;
state
.balances_mut()
.get_balance_mut(index)?
.safe_add_assign(delta)?;
Ok(())
}
@@ -34,7 +37,8 @@ pub fn decrease_balance<E: EthSpec>(
index: usize,
delta: u64,
) -> Result<(), BeaconStateError> {
let balance = state.get_balance_mut(index)?;
let mut balances = state.balances_mut();
let balance = balances.get_balance_mut(index)?;
*balance = balance.saturating_sub(delta);
Ok(())
}