From c2a64a18f0deb318c81ffc4dfa9fa96ab2370cdf Mon Sep 17 00:00:00 2001 From: dapplion <35266934+dapplion@users.noreply.github.com> Date: Fri, 27 Oct 2023 17:07:50 +0300 Subject: [PATCH] pass lint --- pysetup/spec_builders/deneb.py | 13 +++- specs/deneb/p2p-interface.md | 2 +- specs/deneb/validator.md | 8 +-- .../unittests/validator/test_validator.py | 69 ++++++++++++------- 4 files changed, 63 insertions(+), 29 deletions(-) diff --git a/pysetup/spec_builders/deneb.py b/pysetup/spec_builders/deneb.py index c32bee830..612ae6cf6 100644 --- a/pysetup/spec_builders/deneb.py +++ b/pysetup/spec_builders/deneb.py @@ -23,7 +23,17 @@ T = TypeVar('T') # For generic function return ''' def retrieve_blobs_and_proofs(beacon_block_root: Root) -> Tuple[Sequence[Blob], Sequence[KZGProof]]: # pylint: disable=unused-argument - return [], []''' + return [], [] + + +def compute_commitment_inclusion_proof( + body: BeaconBlockBody, + blob_kzg_commitments: Sequence[KZGCommitment], + index: int +) -> List[Bytes32, KZG_COMMITMENT_INCLUSION_PROOF_DEPTH]: + # pylint: disable=unused-argument + return [] +''' @classmethod def execution_engine_cls(cls) -> str: @@ -68,4 +78,5 @@ EXECUTION_ENGINE = NoopExecutionEngine()""" 'BYTES_PER_FIELD_ELEMENT': spec_object.constant_vars['BYTES_PER_FIELD_ELEMENT'].value, 'FIELD_ELEMENTS_PER_BLOB': spec_object.preset_vars['FIELD_ELEMENTS_PER_BLOB'].value, 'MAX_BLOBS_PER_BLOCK': spec_object.preset_vars['MAX_BLOBS_PER_BLOCK'].value, + 'MAX_BLOB_COMMITMENTS_PER_BLOCK': spec_object.preset_vars['MAX_BLOB_COMMITMENTS_PER_BLOCK'].value, } diff --git a/specs/deneb/p2p-interface.md b/specs/deneb/p2p-interface.md index 7846c9a2a..c7044dc1e 100644 --- a/specs/deneb/p2p-interface.md +++ b/specs/deneb/p2p-interface.md @@ -52,7 +52,7 @@ The specification of these changes continues in the same format as the network s | `MIN_EPOCHS_FOR_BLOB_SIDECARS_REQUESTS` | `2**12` (= 4096 epochs, ~18 days) | The minimum epoch range over which a node must serve blob sidecars | | `BLOB_SIDECAR_SUBNET_COUNT` | `6` | The number of blob sidecar subnets used in the gossipsub protocol. | | `BLOB_KZG_COMMITMENTS_GINDEX` | `4 ** 2 + 11` (= 27) | `blob_kzg_commitments` field gindex on `BeaconBlockBody` container | -| `KZG_COMMITMENT_INCLUSION_PROOF_DEPTH` | `floorlog2(BLOB_KZG_COMMITMENTS_GINDEX) + ceillog2(MAX_BLOB_COMMITMENTS_PER_BLOCK) + 1` | Merkle proof for `blob_kzg_commitments` list item | +| `KZG_COMMITMENT_INCLUSION_PROOF_DEPTH` | `floorlog2(BLOB_KZG_COMMITMENTS_GINDEX) + 1 + ceillog2(MAX_BLOB_COMMITMENTS_PER_BLOCK) # noqa: E501` | Merkle proof for `blob_kzg_commitments` list item | ### Containers diff --git a/specs/deneb/validator.md b/specs/deneb/validator.md index 49e113600..13b816b6f 100644 --- a/specs/deneb/validator.md +++ b/specs/deneb/validator.md @@ -148,6 +148,7 @@ Each `sidecar` is obtained from: def get_blob_sidecars(signed_block: SignedBeaconBlock, blobs: Sequence[Blob], blob_kzg_proofs: Sequence[KZGProof]) -> Sequence[BlobSidecar]: + block = signed_block.message block_header = BeaconBlockHeader( slot=block.slot, proposer_index=block.proposer_index, @@ -160,18 +161,17 @@ def get_blob_sidecars(signed_block: SignedBeaconBlock, BlobSidecar( index=index, blob=blob, - kzg_commitment=signed_block.message.body.blob_kzg_commitments[index], + kzg_commitment=block.body.blob_kzg_commitments[index], kzg_proof=blob_kzg_proofs[index], commitment_inclusion_proof=compute_commitment_inclusion_proof( - signed_block.message.body, - signed_block.message.body.blob_kzg_commitments[index], + block.body, + block.body.blob_kzg_commitments[index], index, ), signed_block_header=signed_block_header, ) for index, blob in enumerate(blobs) ] - ``` The `subnet_id` for the `signed_sidecar` is calculated with: diff --git a/tests/core/pyspec/eth2spec/test/deneb/unittests/validator/test_validator.py b/tests/core/pyspec/eth2spec/test/deneb/unittests/validator/test_validator.py index 876824107..ef031693d 100644 --- a/tests/core/pyspec/eth2spec/test/deneb/unittests/validator/test_validator.py +++ b/tests/core/pyspec/eth2spec/test/deneb/unittests/validator/test_validator.py @@ -12,16 +12,48 @@ from eth2spec.test.helpers.sharding import ( from eth2spec.test.helpers.block import ( build_empty_block_for_next_slot ) -from eth2spec.test.helpers.keys import ( - pubkey_to_privkey -) +from tests.core.pyspec.eth2spec.utils.ssz.ssz_impl import hash_tree_root + + +def get_blob_sidecars(spec, signed_block, blobs, blob_kzg_proofs): + block = signed_block.message + block_header = spec.BeaconBlockHeader( + slot=block.slot, + proposer_index=block.proposer_index, + parent_root=block.parent_root, + state_root=block.state_root, + body_root=hash_tree_root(block.body), + ) + signed_block_header = spec.SignedBeaconBlockHeader(message=block_header, signature=signed_block.signature) + return [ + spec.BlobSidecar( + index=index, + blob=blob, + kzg_commitment=signed_block.message.body.blob_kzg_commitments[index], + kzg_proof=blob_kzg_proofs[index], + commitment_inclusion_proof=compute_commitment_inclusion_proof( + spec, + signed_block.message.body, + signed_block.message.body.blob_kzg_commitments[index], + index, + ), + signed_block_header=signed_block_header, + ) + for index, blob in enumerate(blobs) + ] + + +def compute_commitment_inclusion_proof(spec, body, kzg_commitment, index): + gindex = (spec.BeaconBlockBody / 'blob_kzg_commitments' / index).gindex() + raise Exception('todo, does remerkleable expose an API to compute proofs?') + return gindex @with_deneb_and_later @spec_state_test -def test_blob_sidecar_signature(spec, state): +def test_blob_sidecar_inclusion_proof(spec, state): """ - Test `get_blob_sidecar_signature` + Test `verify_blob_sidecar_inclusion_proof` """ blob_count = 4 block = build_empty_block_for_next_slot(spec, state) @@ -30,22 +62,16 @@ def test_blob_sidecar_signature(spec, state): block.body.execution_payload.transactions = [opaque_tx] block.body.execution_payload.block_hash = compute_el_block_hash(spec, block.body.execution_payload) - blob_sidecars = spec.get_blob_sidecars(block, blobs, proofs) - proposer = state.validators[blob_sidecars[1].proposer_index] - privkey = pubkey_to_privkey[proposer.pubkey] - sidecar_signature = spec.get_blob_sidecar_signature(state, - blob_sidecars[1], - privkey) + blob_sidecars = spec.get_blob_sidecars(spec, block, blobs, proofs) - signed_blob_sidecar = spec.SignedBlobSidecar(message=blob_sidecars[1], signature=sidecar_signature) - - assert spec.verify_blob_sidecar_signature(state, signed_blob_sidecar) + for blob_sidecar in blob_sidecars: + assert spec.verify_blob_sidecar_inclusion_proof(blob_sidecar) @with_deneb_and_later @spec_state_test @always_bls -def test_blob_sidecar_signature_incorrect(spec, state): +def test_blob_sidecar_inclusion_proof_incorrect(spec, state): """ Test `get_blob_sidecar_signature` """ @@ -56,12 +82,9 @@ def test_blob_sidecar_signature_incorrect(spec, state): block.body.execution_payload.transactions = [opaque_tx] block.body.execution_payload.block_hash = compute_el_block_hash(spec, block.body.execution_payload) - blob_sidecars = spec.get_blob_sidecars(block, blobs, proofs) + blob_sidecars = spec.get_blob_sidecars(spec, block, blobs, proofs) - sidecar_signature = spec.get_blob_sidecar_signature(state, - blob_sidecars[1], - 123) - - signed_blob_sidecar = spec.SignedBlobSidecar(message=blob_sidecars[1], signature=sidecar_signature) - - assert not spec.verify_blob_sidecar_signature(state, signed_blob_sidecar) + for blob_sidecar in blob_sidecars: + block = blob_sidecar.signed_block_header.message + block = block.body_root = hash_tree_root(block.body_root) # mutate body root to break proof + assert not spec.verify_blob_sidecar_inclusion_proof(blob_sidecar)