Skip to content

Commit

Permalink
Add random_execution_payload_header to bellatrix state
Browse files Browse the repository at this point in the history
  • Loading branch information
KatyaRyazantseva committed Dec 13, 2024
1 parent 83a8042 commit f3267dd
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 3 deletions.
78 changes: 77 additions & 1 deletion tests/core/pyspec/eth2spec/test/helpers/execution_payload.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
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

from eth2spec.test.helpers.keys import privkeys
from eth2spec.utils.ssz.ssz_impl import hash_tree_root
from eth2spec.debug.random_value import get_random_bytes_list
from eth2spec.test.helpers.withdrawals import get_expected_withdrawals
from eth2spec.test.helpers.withdrawals import (
get_expected_withdrawals,
get_random_withdrawal,
)
from eth2spec.test.helpers.forks import (
is_post_capella,
is_post_deneb,
Expand Down Expand Up @@ -263,6 +267,69 @@ def compute_el_block_hash_for_block(spec, block):
spec, block.body.execution_payload, block.parent_root, requests_hash)


def build_empty_execution_payload_header(spec, state):
latest = state.latest_execution_payload_header
timestamp = spec.compute_timestamp_at_slot(state, state.slot)
execution_payload = build_empty_execution_payload(spec, state)

execution_payload_header = spec.ExecutionPayloadHeader(
parent_hash = latest.block_hash,
fee_recipient = spec.ExecutionAddress(),
state_root = latest.state_root,
receipts_root = spec.Bytes32(),
logs_bloom = spec.ByteVector[spec.BYTES_PER_LOGS_BLOOM](),
prev_randao = spec.Bytes32(),
block_number = 0,
gas_limit = 0,
gas_used = 0,
timestamp = timestamp,
extra_data = spec.ByteList[spec.MAX_EXTRA_DATA_BYTES](),
base_fee_per_gas = 0,
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.Root()

if is_post_deneb(spec):
execution_payload_header.blob_gas_used = 0
execution_payload_header.excess_blob_gas = 0

return execution_payload_header


def build_randomized_execution_payload_header(spec, state, rng=Random(10000)):
execution_payload_header = build_empty_execution_payload_header(spec, state)
execution_payload = build_randomized_execution_payload(spec, state)

execution_payload_header.fee_recipient = spec.ExecutionAddress(get_random_bytes_list(rng, 20)),
execution_payload_header.state_root = spec.Bytes32(get_random_bytes_list(rng, 32)),
execution_payload_header.receipts_root = spec.Bytes32(get_random_bytes_list(rng, 32)),
execution_payload_header.logs_bloom = spec.ByteVector[spec.BYTES_PER_LOGS_BLOOM](
get_random_bytes_list(rng, spec.BYTES_PER_LOGS_BLOOM)
),
execution_payload_header.prev_randao = spec.Bytes32(get_random_bytes_list(rng, 32)),
execution_payload_header.block_number = rng.randint(0, int(10e10)),
execution_payload_header.gas_limit = rng.randint(0, int(10e10)),
execution_payload_header.gas_used = rng.randint(0, int(10e10)),
extra_data_length = rng.randint(0, spec.MAX_EXTRA_DATA_BYTES)
execution_payload_header.extra_data = spec.ByteList[spec.MAX_EXTRA_DATA_BYTES](
get_random_bytes_list(rng, extra_data_length)
),
execution_payload_header.base_fee_per_gas = rng.randint(0, int(10e10)),
execution_payload_header.block_hash = compute_el_block_hash(spec, execution_payload, state),
execution_payload_header.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 @@ -351,6 +418,15 @@ def build_randomized_execution_payload(spec, state, rng):
get_random_tx(rng)
for _ in range(num_transactions)
]
if is_post_capella(spec):
num_withdrawals = rng.randint(0, spec.MAX_WITHDRAWALS_PER_PAYLOAD)
execution_payload.withdrawals = [
get_random_withdrawal(spec, state, rng)
for _ in range(num_withdrawals)
]
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)

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:
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,7 +24,8 @@
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 Down

0 comments on commit f3267dd

Please sign in to comment.