@@ -82,18 +82,20 @@ pub fn processWithdrawals(
8282 }
8383}
8484
85- // Consumer should deinit WithdrawalsResult with .deinit() after use
85+ /// Called by the block proposer to find a list of withdrawals to include in the block.
86+ ///
87+ /// This list is assumed to be bounded by `preset.MAX_WITHDRAWALS_PER_PAYLOAD`.
88+ ///
89+ /// Caller should deinit `withdrawal_balances` with .deinit() after use.
8690pub fn getExpectedWithdrawals (
8791 comptime fork : ForkSeq ,
88- allocator : Allocator ,
8992 epoch_cache : * const EpochCache ,
9093 state : * BeaconState (fork ),
9194 withdrawals_result : * WithdrawalsResult ,
9295 withdrawal_balances : * std .AutoHashMap (ValidatorIndex , usize ),
9396) ! void {
94- if (comptime fork .lt (.capella )) {
95- return error .InvalidForkSequence ;
96- }
97+ std .debug .assert (withdrawals_result .withdrawals .capacity == preset .MAX_WITHDRAWALS_PER_PAYLOAD );
98+ if (comptime fork .lt (.capella )) return error .InvalidForkSequence ;
9799
98100 const epoch = epoch_cache .epoch ;
99101 var withdrawal_index = try state .nextWithdrawalIndex ();
@@ -134,7 +136,7 @@ pub fn getExpectedWithdrawals(
134136 const withdrawable_balance = if (balance_over_min_activation_balance < withdrawal .amount ) balance_over_min_activation_balance else withdrawal .amount ;
135137 var execution_address : ExecutionAddress = undefined ;
136138 @memcpy (& execution_address , validator .withdrawal_credentials [12.. ]);
137- try withdrawals_result .withdrawals .append ( allocator , .{
139+ withdrawals_result .withdrawals .appendAssumeCapacity ( .{
138140 .index = withdrawal_index ,
139141 .validator_index = withdrawal .validator_index ,
140142 .address = execution_address ,
@@ -179,7 +181,7 @@ pub fn getExpectedWithdrawals(
179181 if (withdrawable_epoch <= epoch ) {
180182 var execution_address : ExecutionAddress = undefined ;
181183 @memcpy (& execution_address , withdrawal_credentials [12.. ]);
182- try withdrawals_result .withdrawals .append ( allocator , .{
184+ withdrawals_result .withdrawals .appendAssumeCapacity ( .{
183185 .index = withdrawal_index ,
184186 .validator_index = validator_index ,
185187 .address = execution_address ,
@@ -195,7 +197,7 @@ pub fn getExpectedWithdrawals(
195197 const partial_amount = balance - effective_balance ;
196198 var execution_address : ExecutionAddress = undefined ;
197199 @memcpy (& execution_address , withdrawal_credentials [12.. ]);
198- try withdrawals_result .withdrawals .append ( allocator , .{
200+ withdrawals_result .withdrawals .appendAssumeCapacity ( .{
199201 .index = withdrawal_index ,
200202 .validator_index = validator_index ,
201203 .address = execution_address ,
@@ -227,13 +229,10 @@ test "process withdrawals - sanity" {
227229 var test_state = try TestCachedBeaconState .init (allocator , & pool , 256 );
228230 defer test_state .deinit ();
229231
232+ var withdrawals_buf : [preset .MAX_WITHDRAWALS_PER_PAYLOAD ]types.capella.Withdrawal.Type = undefined ;
230233 var withdrawals_result = WithdrawalsResult {
231- .withdrawals = try Withdrawals .initCapacity (
232- allocator ,
233- preset .MAX_WITHDRAWALS_PER_PAYLOAD ,
234- ),
234+ .withdrawals = Withdrawals .initBuffer (& withdrawals_buf ),
235235 };
236- defer withdrawals_result .withdrawals .deinit (allocator );
237236 var withdrawal_balances = std .AutoHashMap (ValidatorIndex , usize ).init (allocator );
238237 defer withdrawal_balances .deinit ();
239238
@@ -242,7 +241,6 @@ test "process withdrawals - sanity" {
242241
243242 try getExpectedWithdrawals (
244243 .electra ,
245- allocator ,
246244 test_state .cached_state .epoch_cache ,
247245 test_state .cached_state .state .castToFork (.electra ),
248246 & withdrawals_result ,
0 commit comments