Skip to content

Commit 8cf2fd5

Browse files
committed
add EIP-7251 initialize_beacon_state_from_eth1 function
1 parent 61168e6 commit 8cf2fd5

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

specs/_features/eip7251/beacon-chain.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
- [New `process_consolidation`](#new-process_consolidation)
7878
- [Voluntary exits](#voluntary-exits)
7979
- [Updated `process_voluntary_exit`](#updated-process_voluntary_exit)
80+
- [Testing](#testing)
8081

8182
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
8283
<!-- /TOC -->
@@ -1063,3 +1064,61 @@ def process_voluntary_exit(state: BeaconState, signed_voluntary_exit: SignedVolu
10631064
# Initiate exit
10641065
initiate_validator_exit(state, voluntary_exit.validator_index)
10651066
```
1067+
1068+
## Testing
1069+
1070+
*Note*: The function `initialize_beacon_state_from_eth1` is modified for pure EIP-7251 testing only.
1071+
1072+
```python
1073+
def initialize_beacon_state_from_eth1(eth1_block_hash: Hash32,
1074+
eth1_timestamp: uint64,
1075+
deposits: Sequence[Deposit],
1076+
execution_payload_header: ExecutionPayloadHeader=ExecutionPayloadHeader()
1077+
) -> BeaconState:
1078+
fork = Fork(
1079+
previous_version=EIP7251_FORK_VERSION, # [Modified in EIP-7251] for testing only
1080+
current_version=EIP7251_FORK_VERSION, # [Modified in EIP-7251]
1081+
epoch=GENESIS_EPOCH,
1082+
)
1083+
state = BeaconState(
1084+
genesis_time=eth1_timestamp + GENESIS_DELAY,
1085+
fork=fork,
1086+
eth1_data=Eth1Data(block_hash=eth1_block_hash, deposit_count=uint64(len(deposits))),
1087+
latest_block_header=BeaconBlockHeader(body_root=hash_tree_root(BeaconBlockBody())),
1088+
randao_mixes=[eth1_block_hash] * EPOCHS_PER_HISTORICAL_VECTOR, # Seed RANDAO with Eth1 entropy
1089+
)
1090+
1091+
# Process deposits
1092+
leaves = list(map(lambda deposit: deposit.data, deposits))
1093+
for index, deposit in enumerate(deposits):
1094+
deposit_data_list = List[DepositData, 2**DEPOSIT_CONTRACT_TREE_DEPTH](*leaves[:index + 1])
1095+
state.eth1_data.deposit_root = hash_tree_root(deposit_data_list)
1096+
process_deposit(state, deposit)
1097+
1098+
# Process deposit balance updates
1099+
for deposit in state.pending_balance_deposits:
1100+
increase_balance(state, deposit.index, deposit.amount)
1101+
state.pending_balance_deposits = []
1102+
1103+
# Process activations
1104+
for index, validator in enumerate(state.validators):
1105+
balance = state.balances[index]
1106+
validator.effective_balance = min(balance - balance % EFFECTIVE_BALANCE_INCREMENT, MAX_EFFECTIVE_BALANCE)
1107+
if validator.effective_balance == MAX_EFFECTIVE_BALANCE:
1108+
validator.activation_eligibility_epoch = GENESIS_EPOCH
1109+
validator.activation_epoch = GENESIS_EPOCH
1110+
1111+
# Set genesis validators root for domain separation and chain versioning
1112+
state.genesis_validators_root = hash_tree_root(state.validators)
1113+
1114+
# Fill in sync committees
1115+
# Note: A duplicate committee is assigned for the current and next committee at genesis
1116+
state.current_sync_committee = get_next_sync_committee(state)
1117+
state.next_sync_committee = get_next_sync_committee(state)
1118+
1119+
# Initialize the execution payload header
1120+
# If empty, will initialize a chain that has not yet gone through the Merge transition
1121+
state.latest_execution_payload_header = execution_payload_header
1122+
1123+
return state
1124+
```

0 commit comments

Comments
 (0)