Skip to content

Commit 742e3ad

Browse files
LeanSerrarodrigo-o
andcommitted
feat: electra process_operations changes (#1426)
Co-authored-by: Rodrigo Oliveri <rodrigooliveri10@gmail.com>
1 parent a748265 commit 742e3ad

File tree

5 files changed

+466
-37
lines changed

5 files changed

+466
-37
lines changed

electra-gap.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,16 @@ The current status of the implementation in the [electra-support](https://github
7373
- [x] Modified `get_next_sync_committee_indices` ([Spec](docs/specs/electra/beacon-chain.md#modified-get_next_sync_committee_indices), [PR](https://github.com/lambdaclass/lambda_ethereum_consensus/pull/1417))
7474
- [x] New `get_balance_churn_limit` ([Spec](docs/specs/electra/beacon-chain.md#new-get_balance_churn_limit), [PR](https://github.com/lambdaclass/lambda_ethereum_consensus/pull/1420))
7575
- [x] New `get_activation_exit_churn_limit` ([Spec](docs/specs/electra/beacon-chain.md#new-get_activation_exit_churn_limit), [PR](https://github.com/lambdaclass/lambda_ethereum_consensus/pull/1420))
76-
- [ ] New `get_consolidation_churn_limit` ([Spec](docs/specs/electra/beacon-chain.md#new-get_consolidation_churn_limit))
77-
- [ ] New `get_pending_balance_to_withdraw` ([Spec](docs/specs/electra/beacon-chain.md#new-get_pending_balance_to_withdraw))
76+
- [x] New `get_consolidation_churn_limit` ([Spec](docs/specs/electra/beacon-chain.md#new-get_consolidation_churn_limit), [PR](https://github.com/lambdaclass/lambda_ethereum_consensus/pull/1426))
77+
- [x] New `get_pending_balance_to_withdraw` ([Spec](docs/specs/electra/beacon-chain.md#new-get_pending_balance_to_withdraw), [PR](https://github.com/lambdaclass/lambda_ethereum_consensus/pull/1426))
7878

7979
#### Beacon State Mutators (3/6 - 50%)
8080

8181
- [x] Modified `initiate_validator_exit` ([Spec](docs/specs/electra/beacon-chain.md#modified-initiate_validator_exit), [PR](https://github.com/lambdaclass/lambda_ethereum_consensus/pull/1420))
82-
- [ ] New `switch_to_compounding_validator` ([Spec](docs/specs/electra/beacon-chain.md#new-switch_to_compounding_validator))
83-
- [ ] New `queue_excess_active_balance` ([Spec](docs/specs/electra/beacon-chain.md#new-queue_excess_active_balance))
82+
- [x] New `switch_to_compounding_validator` ([Spec](docs/specs/electra/beacon-chain.md#new-switch_to_compounding_validator), [PR](https://github.com/lambdaclass/lambda_ethereum_consensus/pull/1426))
83+
- [x] New `queue_excess_active_balance` ([Spec](docs/specs/electra/beacon-chain.md#new-queue_excess_active_balance), [PR](https://github.com/lambdaclass/lambda_ethereum_consensus/pull/1426))
8484
- [x] New `compute_exit_epoch_and_update_churn` ([Spec](docs/specs/electra/beacon-chain.md#new-compute_exit_epoch_and_update_churn), [PR](https://github.com/lambdaclass/lambda_ethereum_consensus/pull/1420))
85-
- [ ] New `compute_consolidation_epoch_and_update_churn` ([Spec](docs/specs/electra/beacon-chain.md#new-compute_consolidation_epoch_and_update_churn))
85+
- [x] New `compute_consolidation_epoch_and_update_churn` ([Spec](docs/specs/electra/beacon-chain.md#new-compute_consolidation_epoch_and_update_churn), [PR](https://github.com/lambdaclass/lambda_ethereum_consensus/pull/1426))
8686
- [x] Modified `slash_validator` ([Spec](docs/specs/electra/beacon-chain.md#modified-slash_validator), [PR](https://github.com/lambdaclass/lambda_ethereum_consensus/pull/1420))
8787

8888
#### Miscellaneous (3/3 - 100%)
@@ -107,12 +107,12 @@ The current status of the implementation in the [electra-support](https://github
107107
- [ ] Modified `process_withdrawals` ([Spec](docs/specs/electra/beacon-chain.md#modified-process_withdrawals))
108108
- [ ] Modified `process_execution_payload` ([Spec](docs/specs/electra/beacon-chain.md#modified-process_execution_payload))
109109
- [x] Modified `process_operations` ([Spec](docs/specs/electra/beacon-chain.md#modified-process_operations), [PR](https://github.com/lambdaclass/lambda_ethereum_consensus/pull/1424))
110-
- [ ] Modified `process_attestation` ([Spec](docs/specs/electra/beacon-chain.md#modified-process_attestation))
110+
- [x] Modified `process_attestation` ([Spec](docs/specs/electra/beacon-chain.md#modified-process_attestation), [PR](https://github.com/lambdaclass/lambda_ethereum_consensus/pull/1426))
111111
- [x] Modified `process_deposit` ([Spec](docs/specs/electra/beacon-chain.md#modified-process_deposit), [PR](https://github.com/lambdaclass/lambda_ethereum_consensus/pull/1424))
112-
- [ ] Modified `process_voluntary_exit` ([Spec](docs/specs/electra/beacon-chain.md#modified-process_voluntary_exit))
113-
- [ ] New `process_withdrawal_request` ([Spec](docs/specs/electra/beacon-chain.md#new-process_withdrawal_request))
112+
- [x] Modified `process_voluntary_exit` ([Spec](docs/specs/electra/beacon-chain.md#modified-process_voluntary_exit), [PR](https://github.com/lambdaclass/lambda_ethereum_consensus/pull/1426))
113+
- [x] New `process_withdrawal_request` ([Spec](docs/specs/electra/beacon-chain.md#new-process_withdrawal_request), [PR](https://github.com/lambdaclass/lambda_ethereum_consensus/pull/1426))
114114
- [x] New `process_deposit_request` ([Spec](docs/specs/electra/beacon-chain.md#new-process_deposit_request), [PR](https://github.com/lambdaclass/lambda_ethereum_consensus/pull/1424))
115-
- [ ] New `process_consolidation_request` ([Spec](docs/specs/electra/beacon-chain.md#new-process_consolidation_request))
115+
- [x] New `process_consolidation_request` ([Spec](docs/specs/electra/beacon-chain.md#new-process_consolidation_request), [PR](https://github.com/lambdaclass/lambda_ethereum_consensus/pull/1426))
116116
- [x] New `is_valid_deposit_signature` ([Spec](docs/specs/electra/beacon-chain.md#new-is_valid_deposit_signature), [PR](https://github.com/lambdaclass/lambda_ethereum_consensus/pull/1424))
117117
- [x] Modified `add_validator_to_registry` ([Spec](docs/specs/electra/beacon-chain.md#modified-add_validator_to_registry), [PR](https://github.com/lambdaclass/lambda_ethereum_consensus/pull/1424))
118118
- [x] Modified `apply_deposit` ([Spec](docs/specs/electra/beacon-chain.md#modified-apply_deposit), [PR](https://github.com/lambdaclass/lambda_ethereum_consensus/pull/1424))

lib/constants.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,7 @@ defmodule Constants do
151151

152152
@spec consolidation_request_type() :: Types.bytes1()
153153
def consolidation_request_type(), do: <<2>>
154+
155+
@spec g2_point_at_infinity() :: Types.bls_signature()
156+
def g2_point_at_infinity(), do: <<0xC0, 0::8*95>>
154157
end

lib/lambda_ethereum_consensus/state_transition/accessors.ex

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,11 @@ defmodule LambdaEthereumConsensus.StateTransition.Accessors do
653653

654654
@spec get_committee_indices(Types.bitvector()) :: Enumerable.t(Types.commitee_index())
655655
def get_committee_indices(committee_bits) do
656-
bitlist = committee_bits |> :binary.bin_to_list() |> Enum.reverse()
656+
bitlist =
657+
for <<bit::1 <- committee_bits>> do
658+
bit
659+
end
660+
|> Enum.reverse()
657661

658662
for {bit, index} <- Enum.with_index(bitlist), bit == 1, do: index
659663
end
@@ -682,4 +686,19 @@ defmodule LambdaEthereumConsensus.StateTransition.Accessors do
682686
get_balance_churn_limit(state)
683687
)
684688
end
689+
690+
@spec get_pending_balance_to_withdraw(BeaconState.t(), Types.validator_index()) :: Types.gwei()
691+
def get_pending_balance_to_withdraw(state, validator_index) do
692+
for(
693+
withdrawal <- state.pending_partial_withdrawals,
694+
withdrawal.validator_index == validator_index,
695+
do: withdrawal.amount
696+
)
697+
|> Enum.sum()
698+
end
699+
700+
@spec get_consolidation_churn_limit(BeaconState.t()) :: Types.gwei()
701+
def get_consolidation_churn_limit(state) do
702+
get_balance_churn_limit(state) - get_activation_exit_churn_limit(state)
703+
end
685704
end

lib/lambda_ethereum_consensus/state_transition/mutators.ex

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,95 @@ defmodule LambdaEthereumConsensus.StateTransition.Mutators do
218218
earliest_exit_epoch: earliest_exit_epoch
219219
}
220220
end
221+
222+
@spec compute_consolidation_epoch_and_update_churn(Types.BeaconState.t(), Types.gwei()) ::
223+
Types.BeaconState.t()
224+
def compute_consolidation_epoch_and_update_churn(state, consolidation_balance) do
225+
current_epoch = Accessors.get_current_epoch(state)
226+
227+
earliest_consolidation_epoch =
228+
max(state.earliest_consolidation_epoch, Misc.compute_activation_exit_epoch(current_epoch))
229+
230+
per_epoch_consolidation_churn = Accessors.get_consolidation_churn_limit(state)
231+
232+
consolidation_balance_to_consume =
233+
if state.earliest_consolidation_epoch < earliest_consolidation_epoch do
234+
per_epoch_consolidation_churn
235+
else
236+
state.consolidation_balance_to_consume
237+
end
238+
239+
{earliest_consolidation_epoch, consolidation_balance_to_consume} =
240+
if consolidation_balance > consolidation_balance_to_consume do
241+
balance_to_process = consolidation_balance - consolidation_balance_to_consume
242+
additional_epochs = div(balance_to_process - 1, per_epoch_consolidation_churn) + 1
243+
244+
{
245+
earliest_consolidation_epoch + additional_epochs,
246+
consolidation_balance_to_consume + additional_epochs * per_epoch_consolidation_churn
247+
}
248+
else
249+
{earliest_consolidation_epoch, consolidation_balance_to_consume}
250+
end
251+
252+
%BeaconState{
253+
state
254+
| consolidation_balance_to_consume:
255+
consolidation_balance_to_consume - consolidation_balance,
256+
earliest_consolidation_epoch: earliest_consolidation_epoch
257+
}
258+
end
259+
260+
@spec switch_to_compounding_validator(BeaconState.t(), Types.validator_index()) ::
261+
BeaconState.t()
262+
def switch_to_compounding_validator(state, index) do
263+
validator = Aja.Enum.at(state.validators, index)
264+
<<_first_byte::binary-size(1), rest::binary>> = validator.withdrawal_credentials
265+
266+
withdrawal_credentials =
267+
Constants.compounding_withdrawal_prefix() <> rest
268+
269+
updated_validator = %Validator{
270+
validator
271+
| withdrawal_credentials: withdrawal_credentials
272+
}
273+
274+
state = %BeaconState{
275+
state
276+
| validators: Aja.Vector.replace_at(state.validators, index, updated_validator)
277+
}
278+
279+
queue_excess_active_balance(state, index)
280+
end
281+
282+
@spec queue_excess_active_balance(BeaconState.t(), Types.validator_index()) ::
283+
BeaconState.t()
284+
def queue_excess_active_balance(state, index) do
285+
min_activation_balance = ChainSpec.get("MIN_ACTIVATION_BALANCE")
286+
balance = Aja.Vector.at(state.balances, index)
287+
288+
if balance > min_activation_balance do
289+
excess_balance = balance - min_activation_balance
290+
validator = Aja.Vector.at(state.validators, index)
291+
292+
updated_balances = Aja.Vector.replace_at(state.balances, index, min_activation_balance)
293+
# Use bls.G2_POINT_AT_INFINITY as a signature field placeholder
294+
# and GENESIS_SLOT to distinguish from a pending deposit request
295+
pending_deposit = %PendingDeposit{
296+
pubkey: validator.pubkey,
297+
withdrawal_credentials: validator.withdrawal_credentials,
298+
amount: excess_balance,
299+
signature: Constants.g2_point_at_infinity(),
300+
slot: Constants.genesis_slot()
301+
}
302+
303+
%BeaconState{
304+
state
305+
| balances: updated_balances,
306+
pending_deposits: state.pending_deposits ++ [pending_deposit]
307+
}
308+
else
309+
state
310+
end
311+
end
221312
end

0 commit comments

Comments
 (0)