1
1
use crate :: consensus_context:: ConsensusContext ;
2
2
use errors:: { BlockOperationError , BlockProcessingError , HeaderInvalid } ;
3
3
use rayon:: prelude:: * ;
4
- use safe_arith:: { ArithError , SafeArith } ;
4
+ use safe_arith:: { ArithError , SafeArith , SafeArithIter } ;
5
5
use signature_sets:: { block_proposal_signature_set, get_pubkey_from_state, randao_signature_set} ;
6
6
use std:: borrow:: Cow ;
7
7
use tree_hash:: TreeHash ;
@@ -512,9 +512,9 @@ pub fn get_expected_withdrawals<E: EthSpec>(
512
512
513
513
// [New in Electra:EIP7251]
514
514
// Consume pending partial withdrawals
515
- let partial_withdrawals_count =
515
+ let processed_partial_withdrawals_count =
516
516
if let Ok ( partial_withdrawals) = state. pending_partial_withdrawals ( ) {
517
- let mut partial_withdrawals_count = 0 ;
517
+ let mut processed_partial_withdrawals_count = 0 ;
518
518
for withdrawal in partial_withdrawals {
519
519
if withdrawal. withdrawable_epoch > epoch
520
520
|| withdrawals. len ( ) == spec. max_pending_partials_per_withdrawals_sweep as usize
@@ -547,9 +547,9 @@ pub fn get_expected_withdrawals<E: EthSpec>(
547
547
} ) ;
548
548
withdrawal_index. safe_add_assign ( 1 ) ?;
549
549
}
550
- partial_withdrawals_count . safe_add_assign ( 1 ) ?;
550
+ processed_partial_withdrawals_count . safe_add_assign ( 1 ) ?;
551
551
}
552
- Some ( partial_withdrawals_count )
552
+ Some ( processed_partial_withdrawals_count )
553
553
} else {
554
554
None
555
555
} ;
@@ -560,9 +560,19 @@ pub fn get_expected_withdrawals<E: EthSpec>(
560
560
) ;
561
561
for _ in 0 ..bound {
562
562
let validator = state. get_validator ( validator_index as usize ) ?;
563
- let balance = * state. balances ( ) . get ( validator_index as usize ) . ok_or (
564
- BeaconStateError :: BalancesOutOfBounds ( validator_index as usize ) ,
565
- ) ?;
563
+ let partially_withdrawn_balance = withdrawals
564
+ . iter ( )
565
+ . filter_map ( |withdrawal| {
566
+ ( withdrawal. validator_index == validator_index) . then_some ( withdrawal. amount )
567
+ } )
568
+ . safe_sum ( ) ?;
569
+ let balance = state
570
+ . balances ( )
571
+ . get ( validator_index as usize )
572
+ . ok_or ( BeaconStateError :: BalancesOutOfBounds (
573
+ validator_index as usize ,
574
+ ) ) ?
575
+ . safe_sub ( partially_withdrawn_balance) ?;
566
576
if validator. is_fully_withdrawable_at ( balance, epoch, spec, fork_name) {
567
577
withdrawals. push ( Withdrawal {
568
578
index : withdrawal_index,
@@ -594,7 +604,7 @@ pub fn get_expected_withdrawals<E: EthSpec>(
594
604
. safe_rem ( state. validators ( ) . len ( ) as u64 ) ?;
595
605
}
596
606
597
- Ok ( ( withdrawals. into ( ) , partial_withdrawals_count ) )
607
+ Ok ( ( withdrawals. into ( ) , processed_partial_withdrawals_count ) )
598
608
}
599
609
600
610
/// Apply withdrawals to the state.
0 commit comments