Skip to content

Commit b29a1d3

Browse files
committed
Isolate switch to compounding flow
1 parent 1bf8ca5 commit b29a1d3

File tree

3 files changed

+425
-57
lines changed

3 files changed

+425
-57
lines changed

specs/electra/beacon-chain.md

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
- [Modified `get_next_sync_committee_indices`](#modified-get_next_sync_committee_indices)
6262
- [Beacon state mutators](#beacon-state-mutators)
6363
- [Modified `initiate_validator_exit`](#modified-initiate_validator_exit)
64+
- [New `switch_to_compounding_validator`](#new-switch_to_compounding_validator)
6465
- [New `queue_excess_active_balance`](#new-queue_excess_active_balance)
6566
- [New `queue_entire_balance_and_reset_validator`](#new-queue_entire_balance_and_reset_validator)
6667
- [New `compute_exit_epoch_and_update_churn`](#new-compute_exit_epoch_and_update_churn)
@@ -96,6 +97,7 @@
9697
- [Deposit requests](#deposit-requests)
9798
- [New `process_deposit_request`](#new-process_deposit_request)
9899
- [Execution layer consolidation requests](#execution-layer-consolidation-requests)
100+
- [New `is_valid_switch_to_compounding_request`](#new-is_valid_switch_to_compounding_request)
99101
- [New `process_consolidation_request`](#new-process_consolidation_request)
100102
- [Testing](#testing)
101103

@@ -677,6 +679,15 @@ def initiate_validator_exit(state: BeaconState, index: ValidatorIndex) -> None:
677679
validator.withdrawable_epoch = Epoch(validator.exit_epoch + MIN_VALIDATOR_WITHDRAWABILITY_DELAY)
678680
```
679681

682+
#### New `switch_to_compounding_validator`
683+
684+
```python
685+
def switch_to_compounding_validator(state: BeaconState, index: ValidatorIndex) -> None:
686+
validator = state.validators[index]
687+
validator.withdrawal_credentials = COMPOUNDING_WITHDRAWAL_PREFIX + validator.withdrawal_credentials[1:]
688+
queue_excess_active_balance(state, index)
689+
```
690+
680691
#### New `queue_excess_active_balance`
681692

682693
```python
@@ -1383,13 +1394,62 @@ def process_deposit_request(state: BeaconState, deposit_request: DepositRequest)
13831394

13841395
##### Execution layer consolidation requests
13851396

1397+
###### New `is_valid_switch_to_compounding_request`
1398+
1399+
```python
1400+
def is_valid_switch_to_compounding_request(
1401+
state: BeaconState,
1402+
consolidation_request: ConsolidationRequest
1403+
) -> bool:
1404+
# Switch to compounding requires source and target be equal
1405+
if consolidation_request.source_pubkey != consolidation_request.target_pubkey:
1406+
return False
1407+
1408+
# Verify pubkey exists
1409+
source_pubkey = consolidation_request.source_pubkey
1410+
validator_pubkeys = [v.pubkey for v in state.validators]
1411+
if source_pubkey not in validator_pubkeys:
1412+
return False
1413+
1414+
source_validator = state.validators[ValidatorIndex(validator_pubkeys.index(source_pubkey))]
1415+
1416+
# Verify request has been authorized
1417+
if source_validator.withdrawal_credentials[12:] != consolidation_request.source_address:
1418+
return False
1419+
1420+
# Verify source withdrawal credentials
1421+
if not has_eth1_withdrawal_credential(source_validator):
1422+
return False
1423+
1424+
# Verify the source is active
1425+
current_epoch = get_current_epoch(state)
1426+
if not is_active_validator(source_validator, current_epoch):
1427+
return False
1428+
1429+
# Verify exit for source have not been initiated
1430+
if source_validator.exit_epoch != FAR_FUTURE_EPOCH:
1431+
return False
1432+
1433+
return True
1434+
```
1435+
13861436
###### New `process_consolidation_request`
13871437

13881438
```python
13891439
def process_consolidation_request(
13901440
state: BeaconState,
13911441
consolidation_request: ConsolidationRequest
13921442
) -> None:
1443+
if is_valid_switch_to_compounding_request(state, consolidation_request):
1444+
validator_pubkeys = [v.pubkey for v in state.validators]
1445+
request_source_pubkey = consolidation_request.source_pubkey
1446+
source_index = ValidatorIndex(validator_pubkeys.index(request_source_pubkey))
1447+
switch_to_compounding_validator(state, source_index)
1448+
return
1449+
1450+
# Verify that source != target, so a consolidation cannot be used as an exit.
1451+
if consolidation_request.source_pubkey == consolidation_request.target_pubkey:
1452+
return
13931453
# If the pending consolidations queue is full, consolidation requests are ignored
13941454
if len(state.pending_consolidations) == PENDING_CONSOLIDATIONS_LIMIT:
13951455
return
@@ -1434,28 +1494,21 @@ def process_consolidation_request(
14341494
if target_validator.exit_epoch != FAR_FUTURE_EPOCH:
14351495
return
14361496

1437-
# Churn any target excess active balance of target and raise its max
1438-
if has_eth1_withdrawal_credential(target_validator):
1439-
state.validators[target_index].withdrawal_credentials = (
1440-
COMPOUNDING_WITHDRAWAL_PREFIX + target_validator.withdrawal_credentials[1:]
1441-
)
1442-
queue_excess_active_balance(state, target_index)
1443-
1444-
# Verify that source != target, so a consolidation cannot be used as an exit.
1445-
if source_index == target_index:
1446-
return
1447-
14481497
# Initiate source validator exit and append pending consolidation
1449-
state.validators[source_index].exit_epoch = compute_consolidation_epoch_and_update_churn(
1498+
source_validator.exit_epoch = compute_consolidation_epoch_and_update_churn(
14501499
state, source_validator.effective_balance
14511500
)
1452-
state.validators[source_index].withdrawable_epoch = Epoch(
1453-
state.validators[source_index].exit_epoch + MIN_VALIDATOR_WITHDRAWABILITY_DELAY
1501+
source_validator.withdrawable_epoch = Epoch(
1502+
source_validator.exit_epoch + MIN_VALIDATOR_WITHDRAWABILITY_DELAY
14541503
)
14551504
state.pending_consolidations.append(PendingConsolidation(
14561505
source_index=source_index,
14571506
target_index=target_index
14581507
))
1508+
1509+
# Churn any target excess active balance of target and raise its max
1510+
if has_eth1_withdrawal_credential(target_validator):
1511+
switch_to_compounding_validator(state, target_index)
14591512
```
14601513

14611514
## Testing

0 commit comments

Comments
 (0)