Add tests. Add validation in the p2p beacon block gossiping

This commit is contained in:
Hsiao-Wei Wang 2023-05-24 16:11:32 +08:00
parent a05b689b4c
commit f0a4281afd
No known key found for this signature in database
GPG Key ID: AE3D6B174F971DE4
6 changed files with 40 additions and 4 deletions

View File

@ -4,5 +4,7 @@
# ---------------------------------------------------------------
# `uint64(4096)`
FIELD_ELEMENTS_PER_BLOB: 4096
# `uint64(2**12)` (= 4096)
MAX_BLOB_COMMITMENTS_PER_BLOCK: 4096
# `uint64(2**2)` (= 4)
MAX_BLOBS_PER_BLOCK: 4

View File

@ -4,5 +4,7 @@
# ---------------------------------------------------------------
# [customized]
FIELD_ELEMENTS_PER_BLOB: 4
# [customized]
MAX_BLOB_COMMITMENTS_PER_BLOCK: 16
# `uint64(2**2)` (= 4)
MAX_BLOBS_PER_BLOCK: 4

View File

@ -244,6 +244,8 @@ def process_execution_payload(state: BeaconState, body: BeaconBlockBody, executi
assert payload.prev_randao == get_randao_mix(state, get_current_epoch(state))
# Verify timestamp
assert payload.timestamp == compute_timestamp_at_slot(state, state.slot)
# Verify commitments are under limit
assert len(body.blob_kzg_commitments) <= MAX_BLOBS_PER_BLOCK
# Verify the execution payload is valid
versioned_hashes = [kzg_commitment_to_versioned_hash(commitment) for commitment in body.blob_kzg_commitments]
assert execution_engine.verify_and_notify_new_payload(

View File

@ -117,6 +117,11 @@ Deneb introduces new global topics for blob sidecars.
The *type* of the payload of this topic changes to the (modified) `SignedBeaconBlock` found in deneb.
New validation:
- _[REJECT]_ The length of KZG commitments is less than or equal to the limitation defined in Consensus Layer --
i.e. validate that `len(body.signed_beacon_block.message.blob_kzg_commitments) <= MAX_BLOBS_PER_BLOCK`
###### `blob_sidecar_{subnet_id}`
This topic is used to propagate signed blob sidecars, where each blob index maps to some `subnet_id`.

View File

@ -190,3 +190,16 @@ def test_invalid_correct_input__execution_invalid(spec, state):
yield from run_execution_payload_processing(spec, state, execution_payload, blob_kzg_commitments,
valid=False, execution_valid=False)
@with_deneb_and_later
@spec_state_test
def test_invalid_exceed_max_blobs_per_block(spec, state):
execution_payload = build_empty_execution_payload(spec, state)
opaque_tx, _, blob_kzg_commitments, _ = get_sample_opaque_tx(spec, blob_count=spec.MAX_BLOBS_PER_BLOCK + 1)
execution_payload.transactions = [opaque_tx]
execution_payload.block_hash = compute_el_block_hash(spec, execution_payload)
yield from run_execution_payload_processing(spec, state, execution_payload, blob_kzg_commitments, valid=False)

View File

@ -16,7 +16,7 @@ from eth2spec.test.helpers.sharding import (
)
def run_block_with_blobs(spec, state, blob_count, excess_data_gas=1):
def run_block_with_blobs(spec, state, blob_count, excess_data_gas=1, valid=True):
yield 'pre', state
block = build_empty_block_for_next_slot(spec, state)
@ -25,10 +25,16 @@ def run_block_with_blobs(spec, state, blob_count, excess_data_gas=1):
block.body.execution_payload.transactions = [opaque_tx]
block.body.execution_payload.excess_data_gas = excess_data_gas
block.body.execution_payload.block_hash = compute_el_block_hash(spec, block.body.execution_payload)
signed_block = state_transition_and_sign_block(spec, state, block)
print(len(block.body.blob_kzg_commitments))
if valid:
signed_block = state_transition_and_sign_block(spec, state, block)
else:
signed_block = state_transition_and_sign_block(spec, state, block, expect_fail=True)
yield 'blocks', [signed_block]
yield 'post', state
yield 'post', state if valid else None
@with_deneb_and_later
@ -45,5 +51,11 @@ def test_one_blob(spec, state):
@with_deneb_and_later
@spec_state_test
def test_max_blobs(spec, state):
def test_max_blobs_per_block(spec, state):
yield from run_block_with_blobs(spec, state, blob_count=spec.MAX_BLOBS_PER_BLOCK)
@with_deneb_and_later
@spec_state_test
def test_invalid_exceed_max_blobs_per_block(spec, state):
yield from run_block_with_blobs(spec, state, blob_count=spec.MAX_BLOBS_PER_BLOCK + 1, valid=False)