Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add random execution_payload_header to Bellatrix state in tests #4047

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions tests/core/pyspec/eth2spec/test/helpers/execution_payload.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from eth_hash.auto import keccak
from hashlib import sha256
from trie import HexaryTrie
from random import Random
from rlp import encode
from rlp.sedes import big_endian_int, Binary, List

Expand Down Expand Up @@ -263,6 +264,41 @@ def compute_el_block_hash_for_block(spec, block):
spec, block.body.execution_payload, block.parent_root, requests_hash)


def build_randomized_execution_payload_header(spec, state, rng=Random(10000)):
execution_payload = build_randomized_execution_payload(spec, state)
extra_data_length = rng.randint(0, spec.MAX_EXTRA_DATA_BYTES)

execution_payload_header = spec.ExecutionPayloadHeader(
parent_hash=spec.Hash32(spec.hash(bytearray(rng.getrandbits(8) for _ in range(32)))),
fee_recipient=spec.ExecutionAddress(get_random_bytes_list(rng, 20)),
state_root=spec.Bytes32(get_random_bytes_list(rng, 32)),
receipts_root=spec.Bytes32(get_random_bytes_list(rng, 32)),
logs_bloom=spec.ByteVector[spec.BYTES_PER_LOGS_BLOOM](
get_random_bytes_list(rng, spec.BYTES_PER_LOGS_BLOOM)
),
prev_randao=spec.Bytes32(get_random_bytes_list(rng, 32)),
block_number=rng.randint(0, int(10e10)),
gas_limit=rng.randint(0, int(10e10)),
gas_used=rng.randint(0, int(10e10)),
timestamp=spec.compute_timestamp_at_slot(state, state.slot),
extra_data=spec.ByteList[spec.MAX_EXTRA_DATA_BYTES](
get_random_bytes_list(rng, extra_data_length)
),
base_fee_per_gas=rng.randint(0, int(10e10)),
block_hash=compute_el_block_hash(spec, execution_payload, state),
transactions_root=spec.hash_tree_root(execution_payload.transactions),
)

if is_post_capella(spec):
execution_payload_header.withdrawals_root = spec.hash_tree_root(execution_payload.withdrawals)

if is_post_deneb(spec):
execution_payload_header.blob_gas_used = rng.randint(0, int(10e10))
execution_payload_header.excess_blob_gas = rng.randint(0, int(10e10))

return execution_payload_header


def build_empty_post_eip7732_execution_payload_header(spec, state):
if not is_post_eip7732(spec):
return
Expand Down Expand Up @@ -330,6 +366,7 @@ def build_empty_execution_payload(spec, state, randao_mix=None):


def build_randomized_execution_payload(spec, state, rng):
from eth2spec.test.helpers.random import exit_random_validators
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jtraglia I have a circular import error when I put the import at the beginning. Is there a way to avoid it here?

execution_payload = build_empty_execution_payload(spec, state)
execution_payload.fee_recipient = spec.ExecutionAddress(get_random_bytes_list(rng, 20))
execution_payload.state_root = spec.Bytes32(get_random_bytes_list(rng, 32))
Expand All @@ -352,6 +389,22 @@ def build_randomized_execution_payload(spec, state, rng):
for _ in range(num_transactions)
]

if is_post_capella(spec):
current_epoch = spec.get_current_epoch(state)
exit_random_validators(
spec,
state,
rng,
exit_epoch=current_epoch,
withdrawable_epoch=current_epoch,
from_epoch=current_epoch
)
execution_payload.withdrawals = get_expected_withdrawals(spec, state)

if is_post_deneb(spec):
execution_payload.blob_gas_used = rng.randint(0, int(10e10))
execution_payload.excess_blob_gas = rng.randint(0, int(10e10))

execution_payload.block_hash = compute_el_block_hash(spec, execution_payload, state)

return execution_payload
Expand Down
5 changes: 5 additions & 0 deletions tests/core/pyspec/eth2spec/test/helpers/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from eth2spec.test.helpers.deposits import mock_deposit
from eth2spec.test.helpers.forks import is_post_altair
from eth2spec.test.helpers.state import next_epoch
from eth2spec.test.helpers.execution_payload import build_randomized_execution_payload_header


def set_some_activations(spec, state, rng, activation_epoch=None):
Expand Down Expand Up @@ -162,6 +163,10 @@ def randomize_attestation_participation(spec, state, rng=Random(8020)):
randomize_epoch_participation(spec, state, spec.get_current_epoch(state), rng)


def randomize_execution_payload_header(spec, state, rng=Random(8020)):
state.latest_execution_payload_header = build_randomized_execution_payload_header(spec, state, rng)


def randomize_state(spec, state, rng=Random(8020), exit_fraction=0.5, slash_fraction=0.5):
set_some_new_deposits(spec, state, rng)
exit_random_validators(spec, state, rng, fraction=exit_fraction)
Expand Down
14 changes: 14 additions & 0 deletions tests/core/pyspec/eth2spec/test/helpers/withdrawals.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ def get_expected_withdrawals(spec, state):
return spec.get_expected_withdrawals(state)


def get_random_withdrawal(spec, state, rng):
if is_post_electra(spec):
amount = rng.randint(0, spec.MAX_EFFECTIVE_BALANCE_ELECTRA)
else:
amount = rng.randint(0, spec.MAX_EFFECTIVE_BALANCE)

return spec.Withdrawal(
index=rng.randrange(0, spec.MAX_WITHDRAWALS_PER_PAYLOAD),
validator_index=rng.randrange(0, len(state.validators)),
address=rng.getrandbits(160).to_bytes(20, 'big'),
amount=amount,
)


def set_validator_fully_withdrawable(spec, state, index, withdrawable_epoch=None):
if withdrawable_epoch is None:
withdrawable_epoch = spec.get_current_epoch(state)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from eth2spec.test.helpers.random import (
randomize_state as randomize_state_helper,
patch_state_to_non_leaking,
randomize_execution_payload_header,
)
from eth2spec.test.helpers.blob import (
get_sample_blob_tx,
Expand Down Expand Up @@ -76,7 +77,7 @@ def randomize_state_bellatrix(spec, state, stats, exit_fraction=0.1, slash_fract
stats,
exit_fraction=exit_fraction,
slash_fraction=slash_fraction)
# TODO: randomize execution payload, merge status, etc.
randomize_execution_payload_header(spec, state)
return scenario_state


Expand All @@ -86,7 +87,6 @@ def randomize_state_capella(spec, state, stats, exit_fraction=0.1, slash_fractio
stats,
exit_fraction=exit_fraction,
slash_fraction=slash_fraction)
# TODO: randomize withdrawals
return scenario_state


Expand All @@ -96,7 +96,6 @@ def randomize_state_deneb(spec, state, stats, exit_fraction=0.1, slash_fraction=
stats,
exit_fraction=exit_fraction,
slash_fraction=slash_fraction)
# TODO: randomize execution payload
return scenario_state


Expand Down