Skip to content

Commit

Permalink
Merge pull request #2581 from mkalinin/set-random-to-prev-randao-mix
Browse files Browse the repository at this point in the history
Set the value of the random field to the previous randao_mix
  • Loading branch information
djrtwo authored Sep 20, 2021
2 parents e5463d7 + 26c78b5 commit 5811046
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 23 deletions.
8 changes: 4 additions & 4 deletions specs/merge/beacon-chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,17 @@ The above function is accessed through the `EXECUTION_ENGINE` module which insta

### Block processing

*Note*: The call to the `process_execution_payload` must happen before the call to the `process_randao` as the former depends on the `randao_mix` computed with the reveal of the previous block.

```python
def process_block(state: BeaconState, block: BeaconBlock) -> None:
process_block_header(state, block)
if is_execution_enabled(state, block.body):
process_execution_payload(state, block.body.execution_payload, EXECUTION_ENGINE) # [New in Merge]
process_randao(state, block.body)
process_eth1_data(state, block.body)
process_operations(state, block.body)
process_sync_aggregate(state, block.body.sync_aggregate)
if is_execution_enabled(state, block.body):
process_execution_payload(state, block.body.execution_payload, EXECUTION_ENGINE) # [New in Merge]
```

### Execution payload processing
Expand Down Expand Up @@ -287,8 +289,6 @@ def is_valid_gas_limit(payload: ExecutionPayload, parent: ExecutionPayloadHeader

#### `process_execution_payload`

*Note:* This function depends on `process_randao` function call as it retrieves the most recent randao mix from the `state`. Implementations that are considering parallel processing of execution payload with respect to beacon chain state transition function should work around this dependency.

```python
def process_execution_payload(state: BeaconState, payload: ExecutionPayload, execution_engine: ExecutionEngine) -> None:
# Verify consistency of the parent hash, block number, base fee per gas and gas limit
Expand Down
13 changes: 3 additions & 10 deletions specs/merge/validator.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,16 @@ def get_pow_block_at_total_difficulty(total_difficulty: uint256, pow_chain: Sequ
return None


def compute_randao_mix(state: BeaconState, randao_reveal: BLSSignature) -> Bytes32:
epoch = get_current_epoch(state)
return xor(get_randao_mix(state, epoch), hash(randao_reveal))


def produce_execution_payload(state: BeaconState,
parent_hash: Hash32,
randao_reveal: BLSSignature,
execution_engine: ExecutionEngine) -> ExecutionPayload:
timestamp = compute_timestamp_at_slot(state, state.slot)
randao_mix = compute_randao_mix(state, randao_reveal)
randao_mix = get_randao_mix(state, get_current_epoch(state))
return execution_engine.assemble_block(parent_hash, timestamp, randao_mix)


def get_execution_payload(state: BeaconState,
transition_store: TransitionStore,
randao_reveal: BLSSignature,
execution_engine: ExecutionEngine,
pow_chain: Sequence[PowBlock]) -> ExecutionPayload:
if not is_merge_complete(state):
Expand All @@ -101,9 +94,9 @@ def get_execution_payload(state: BeaconState,
return ExecutionPayload()
else:
# Signify merge via producing on top of the last PoW block
return produce_execution_payload(state, terminal_pow_block.block_hash, randao_reveal, execution_engine)
return produce_execution_payload(state, terminal_pow_block.block_hash, execution_engine)

# Post-merge, normal payload
parent_hash = state.latest_execution_payload_header.block_hash
return produce_execution_payload(state, parent_hash, randao_reveal, execution_engine)
return produce_execution_payload(state, parent_hash, execution_engine)
```
4 changes: 2 additions & 2 deletions specs/sharding/beacon-chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -552,12 +552,12 @@ def compute_committee_index_from_shard(state: BeaconState, slot: Slot, shard: Sh
```python
def process_block(state: BeaconState, block: BeaconBlock) -> None:
process_block_header(state, block)
# is_execution_enabled is omitted, execution is enabled by default.
process_execution_payload(state, block.body.execution_payload, EXECUTION_ENGINE)
process_randao(state, block.body)
process_eth1_data(state, block.body)
process_operations(state, block.body) # [Modified in Sharding]
process_sync_aggregate(state, block.body.sync_aggregate)
# is_execution_enabled is omitted, execution is enabled by default.
process_execution_payload(state, block.body.execution_payload, EXECUTION_ENGINE)
```

#### Operations
Expand Down
3 changes: 1 addition & 2 deletions tests/core/pyspec/eth2spec/test/helpers/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ def build_empty_block(spec, state, slot=None):
empty_block.body.sync_aggregate.sync_committee_signature = spec.G2_POINT_AT_INFINITY

if is_post_merge(spec):
randao_mix = spec.compute_randao_mix(state, empty_block.body.randao_reveal)
empty_block.body.execution_payload = build_empty_execution_payload(spec, state, randao_mix)
empty_block.body.execution_payload = build_empty_execution_payload(spec, state)

return empty_block

Expand Down
9 changes: 4 additions & 5 deletions tests/core/pyspec/eth2spec/test/phase0/sanity/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,16 @@ def process_and_sign_block_without_header_validations(spec, state, block):
state_root=spec.Bytes32(),
body_root=block.body.hash_tree_root(),
)
if is_post_merge(spec):
if spec.is_execution_enabled(state, block.body):
spec.process_execution_payload(state, block.body.execution_payload, spec.EXECUTION_ENGINE)

# Perform rest of process_block transitions
spec.process_randao(state, block.body)
spec.process_eth1_data(state, block.body)
spec.process_operations(state, block.body)
if is_post_altair(spec):
spec.process_sync_aggregate(state, block.body.sync_aggregate)
if is_post_merge(spec):
if spec.is_execution_enabled(state, block.body):
spec.process_execution_payload(state, block.body.execution_payload, spec.EXECUTION_ENGINE)

# Insert post-state rot
block.state_root = state.hash_tree_root()
Expand Down Expand Up @@ -196,8 +196,7 @@ def test_parent_from_same_slot(spec, state):
child_block.parent_root = state.latest_block_header.hash_tree_root()

if is_post_merge(spec):
randao_mix = spec.compute_randao_mix(state, child_block.body.randao_reveal)
child_block.body.execution_payload = build_empty_execution_payload(spec, state, randao_mix)
child_block.body.execution_payload = build_empty_execution_payload(spec, state)

# Show that normal path through transition fails
failed_state = state.copy()
Expand Down

0 comments on commit 5811046

Please sign in to comment.