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

a few more bellatrix tests #2962

Merged
merged 6 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 3 additions & 3 deletions specs/capella/beacon-chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,13 @@ class BeaconState(Container):
#### `withdraw`

```python
def withdraw_balance(state: BeaconState, index: ValidatorIndex, amount: Gwei) -> None:
def withdraw_balance(state: BeaconState, validator_index: ValidatorIndex, amount: Gwei) -> None:
# Decrease the validator's balance
decrease_balance(state, index, amount)
decrease_balance(state, validator_index, amount)
# Create a corresponding withdrawal receipt
withdrawal = Withdrawal(
index=state.next_withdrawal_index,
address=ExecutionAddress(state.validators[index].withdrawal_credentials[12:]),
address=ExecutionAddress(state.validators[validator_index].withdrawal_credentials[12:]),
amount=amount,
)
state.next_withdrawal_index = WithdrawalIndex(state.next_withdrawal_index + 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from random import Random

from eth2spec.debug.random_value import get_random_bytes_list
from eth2spec.test.helpers.execution_payload import (
build_empty_execution_payload,
build_randomized_execution_payload,
get_execution_payload_header,
build_state_with_incomplete_transition,
build_state_with_complete_transition,
Expand Down Expand Up @@ -307,33 +307,6 @@ def test_zero_length_transaction_regular_payload(spec, state):
yield from run_zero_length_transaction_test(spec, state)


def build_randomized_execution_payload(spec, state, rng):
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))
execution_payload.receipts_root = spec.Bytes32(get_random_bytes_list(rng, 32))
execution_payload.logs_bloom = spec.ByteVector[spec.BYTES_PER_LOGS_BLOOM](
get_random_bytes_list(rng, spec.BYTES_PER_LOGS_BLOOM)
)
execution_payload.block_number = rng.randint(0, 10e10)
execution_payload.gas_limit = rng.randint(0, 10e10)
execution_payload.gas_used = rng.randint(0, 10e10)
extra_data_length = rng.randint(0, spec.MAX_EXTRA_DATA_BYTES)
execution_payload.extra_data = spec.ByteList[spec.MAX_EXTRA_DATA_BYTES](
get_random_bytes_list(rng, extra_data_length)
)
execution_payload.base_fee_per_gas = rng.randint(0, 2**256 - 1)
execution_payload.block_hash = spec.Hash32(get_random_bytes_list(rng, 32))

num_transactions = rng.randint(0, 100)
execution_payload.transactions = [
spec.Transaction(get_random_bytes_list(rng, rng.randint(0, 1000)))
for _ in range(num_transactions)
]

return execution_payload


def run_randomized_non_validated_execution_fields_test(spec, state, execution_valid=True, rng=Random(5555)):
next_slot(spec, state)
execution_payload = build_randomized_execution_payload(spec, state, rng)
Expand Down
23 changes: 21 additions & 2 deletions tests/core/pyspec/eth2spec/test/bellatrix/sanity/test_blocks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from random import Random
from eth2spec.test.helpers.state import (
state_transition_and_sign_block
state_transition_and_sign_block,
next_slot,
)
from eth2spec.test.helpers.block import (
build_empty_block_for_next_slot
)
from eth2spec.test.helpers.execution_payload import (
build_randomized_execution_payload
)
from eth2spec.test.context import (
with_bellatrix_and_later, spec_state_test
)
Expand All @@ -22,7 +27,21 @@ def test_empty_block_transition_no_tx(spec, state):
yield 'blocks', [signed_block]
yield 'post', state

# TODO: tests with EVM, mock or replacement?

@with_bellatrix_and_later
@spec_state_test
def test_empty_block_transition_randomized_payload(spec, state):
yield 'pre', state

block = build_empty_block_for_next_slot(spec, state)
next_slot_state = state.copy()
next_slot(spec, next_slot_state)
block.body.execution_payload = build_randomized_execution_payload(spec, next_slot_state, rng=Random(34433))

signed_block = state_transition_and_sign_block(spec, state, block)

yield 'blocks', [signed_block]
yield 'post', state


@with_bellatrix_and_later
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from eth2spec.test.helpers.random import (
randomize_state,
)
from eth2spec.test.context import (
ForkMeta,
with_fork_metas,
Expand Down Expand Up @@ -33,3 +36,31 @@ def test_sample_transition(state, fork_epoch, spec, post_spec, pre_tag, post_tag

yield "blocks", blocks
yield "post", state


@with_fork_metas([
ForkMeta(pre_fork_name=pre, post_fork_name=post, fork_epoch=8) for pre, post in AFTER_BELLATRIX_PRE_POST_FORKS
])
djrtwo marked this conversation as resolved.
Show resolved Hide resolved
def test_transition_randomized_state(state, fork_epoch, spec, post_spec, pre_tag, post_tag):
randomize_state(spec, state)
# `transition_until_fork` does not play nicely with slashed validator proposals
# bypassing for now
for validator in state.validators:
validator.slashed = False
transition_until_fork(spec, state, fork_epoch)

# check pre state
assert spec.get_current_epoch(state) < fork_epoch

yield "pre", state

# irregular state transition to handle fork:
blocks = []
state, block = do_fork(state, spec, post_spec, fork_epoch)
blocks.append(post_tag(block))

# continue regular state transition with new spec into next epoch
transition_to_next_epoch_and_append_blocks(post_spec, state, post_tag, blocks, only_last_block=True)
djrtwo marked this conversation as resolved.
Show resolved Hide resolved

yield "blocks", blocks
yield "post", state
28 changes: 28 additions & 0 deletions tests/core/pyspec/eth2spec/test/helpers/execution_payload.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from eth2spec.debug.random_value import get_random_bytes_list
from eth2spec.test.context import is_post_capella


Expand Down Expand Up @@ -38,6 +39,33 @@ def build_empty_execution_payload(spec, state, randao_mix=None):
return payload


def build_randomized_execution_payload(spec, state, rng):
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))
execution_payload.receipts_root = spec.Bytes32(get_random_bytes_list(rng, 32))
execution_payload.logs_bloom = spec.ByteVector[spec.BYTES_PER_LOGS_BLOOM](
get_random_bytes_list(rng, spec.BYTES_PER_LOGS_BLOOM)
)
execution_payload.block_number = rng.randint(0, 10e10)
execution_payload.gas_limit = rng.randint(0, 10e10)
execution_payload.gas_used = rng.randint(0, 10e10)
extra_data_length = rng.randint(0, spec.MAX_EXTRA_DATA_BYTES)
execution_payload.extra_data = spec.ByteList[spec.MAX_EXTRA_DATA_BYTES](
get_random_bytes_list(rng, extra_data_length)
)
execution_payload.base_fee_per_gas = rng.randint(0, 2**256 - 1)
execution_payload.block_hash = spec.Hash32(get_random_bytes_list(rng, 32))

num_transactions = rng.randint(0, 100)
execution_payload.transactions = [
spec.Transaction(get_random_bytes_list(rng, rng.randint(0, 1000)))
for _ in range(num_transactions)
]

return execution_payload


def get_execution_payload_header(spec, execution_payload):
payload_header = spec.ExecutionPayloadHeader(
parent_hash=execution_payload.parent_hash,
Expand Down