Merge pull request #2581 from mkalinin/set-random-to-prev-randao-mix

Set the value of the random field to the previous randao_mix
This commit is contained in:
Danny Ryan 2021-09-20 08:17:55 -06:00 committed by GitHub
commit 5811046f1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 14 additions and 23 deletions

View File

@ -249,15 +249,17 @@ The above function is accessed through the `EXECUTION_ENGINE` module which insta
### Block processing ### 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 ```python
def process_block(state: BeaconState, block: BeaconBlock) -> None: def process_block(state: BeaconState, block: BeaconBlock) -> None:
process_block_header(state, block) 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_randao(state, block.body)
process_eth1_data(state, block.body) process_eth1_data(state, block.body)
process_operations(state, block.body) process_operations(state, block.body)
process_sync_aggregate(state, block.body.sync_aggregate) 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 ### Execution payload processing
@ -287,8 +289,6 @@ def is_valid_gas_limit(payload: ExecutionPayload, parent: ExecutionPayloadHeader
#### `process_execution_payload` #### `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 ```python
def process_execution_payload(state: BeaconState, payload: ExecutionPayload, execution_engine: ExecutionEngine) -> None: 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 # Verify consistency of the parent hash, block number, base fee per gas and gas limit

View File

@ -75,23 +75,16 @@ def get_pow_block_at_total_difficulty(total_difficulty: uint256, pow_chain: Sequ
return None 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, def produce_execution_payload(state: BeaconState,
parent_hash: Hash32, parent_hash: Hash32,
randao_reveal: BLSSignature,
execution_engine: ExecutionEngine) -> ExecutionPayload: execution_engine: ExecutionEngine) -> ExecutionPayload:
timestamp = compute_timestamp_at_slot(state, state.slot) 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) return execution_engine.assemble_block(parent_hash, timestamp, randao_mix)
def get_execution_payload(state: BeaconState, def get_execution_payload(state: BeaconState,
transition_store: TransitionStore, transition_store: TransitionStore,
randao_reveal: BLSSignature,
execution_engine: ExecutionEngine, execution_engine: ExecutionEngine,
pow_chain: Sequence[PowBlock]) -> ExecutionPayload: pow_chain: Sequence[PowBlock]) -> ExecutionPayload:
if not is_merge_complete(state): if not is_merge_complete(state):
@ -101,9 +94,9 @@ def get_execution_payload(state: BeaconState,
return ExecutionPayload() return ExecutionPayload()
else: else:
# Signify merge via producing on top of the last PoW block # 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 # Post-merge, normal payload
parent_hash = state.latest_execution_payload_header.block_hash 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)
``` ```

View File

@ -552,12 +552,12 @@ def compute_committee_index_from_shard(state: BeaconState, slot: Slot, shard: Sh
```python ```python
def process_block(state: BeaconState, block: BeaconBlock) -> None: def process_block(state: BeaconState, block: BeaconBlock) -> None:
process_block_header(state, block) 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_randao(state, block.body)
process_eth1_data(state, block.body) process_eth1_data(state, block.body)
process_operations(state, block.body) # [Modified in Sharding] process_operations(state, block.body) # [Modified in Sharding]
process_sync_aggregate(state, block.body.sync_aggregate) 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 #### Operations

View File

@ -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 empty_block.body.sync_aggregate.sync_committee_signature = spec.G2_POINT_AT_INFINITY
if is_post_merge(spec): 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)
empty_block.body.execution_payload = build_empty_execution_payload(spec, state, randao_mix)
return empty_block return empty_block

View File

@ -143,6 +143,9 @@ def process_and_sign_block_without_header_validations(spec, state, block):
state_root=spec.Bytes32(), state_root=spec.Bytes32(),
body_root=block.body.hash_tree_root(), 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 # Perform rest of process_block transitions
spec.process_randao(state, block.body) spec.process_randao(state, block.body)
@ -150,9 +153,6 @@ def process_and_sign_block_without_header_validations(spec, state, block):
spec.process_operations(state, block.body) spec.process_operations(state, block.body)
if is_post_altair(spec): if is_post_altair(spec):
spec.process_sync_aggregate(state, block.body.sync_aggregate) 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 # Insert post-state rot
block.state_root = state.hash_tree_root() block.state_root = state.hash_tree_root()
@ -196,8 +196,7 @@ def test_parent_from_same_slot(spec, state):
child_block.parent_root = state.latest_block_header.hash_tree_root() child_block.parent_root = state.latest_block_header.hash_tree_root()
if is_post_merge(spec): 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)
child_block.body.execution_payload = build_empty_execution_payload(spec, state, randao_mix)
# Show that normal path through transition fails # Show that normal path through transition fails
failed_state = state.copy() failed_state = state.copy()