From 530efa88aa262f4b90c8680a7ee709ff98183725 Mon Sep 17 00:00:00 2001 From: dapplion <35266934+dapplion@users.noreply.github.com> Date: Fri, 27 Oct 2023 12:10:29 +0300 Subject: [PATCH 01/30] Add blob sidecar inclusion proof --- specs/deneb/p2p-interface.md | 79 +++++++++++++++++++++++------------- specs/deneb/validator.md | 44 ++++++++++---------- 2 files changed, 71 insertions(+), 52 deletions(-) diff --git a/specs/deneb/p2p-interface.md b/specs/deneb/p2p-interface.md index a81e5d0bb6..feda3563b6 100644 --- a/specs/deneb/p2p-interface.md +++ b/specs/deneb/p2p-interface.md @@ -14,10 +14,11 @@ The specification of these changes continues in the same format as the network s - [Configuration](#configuration) - [Containers](#containers) - [`BlobSidecar`](#blobsidecar) - - [`SignedBlobSidecar`](#signedblobsidecar) - [`BlobIdentifier`](#blobidentifier) - [Helpers](#helpers) - [`verify_blob_sidecar_signature`](#verify_blob_sidecar_signature) + - [`verify_blob_sidecar_inclusion_proof`](#verify_blob_sidecar_inclusion_proof) + - [`is_valid_merkle_path`](#is_valid_merkle_path) - [The gossip domain: gossipsub](#the-gossip-domain-gossipsub) - [Topics and messages](#topics-and-messages) - [Global topics](#global-topics) @@ -51,6 +52,8 @@ The specification of these changes continues in the same format as the network s | `MAX_REQUEST_BLOB_SIDECARS` | `MAX_REQUEST_BLOCKS_DENEB * MAX_BLOBS_PER_BLOCK` | Maximum number of blob sidecars in a single request | | `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. | +| `KZG_COMMITMENT_INCLUSION_PROOF_DEPTH` | `17` | Merkle proof for `blob_kzg_commitments` list item | +| `BLOB_KZG_COMMITMENT_GINDEX` | `27` | Gindex path to `blob_kzg_commitments` on `BeaconBlockBody` container ### Containers @@ -60,24 +63,12 @@ The specification of these changes continues in the same format as the network s ```python class BlobSidecar(Container): - block_root: Root index: BlobIndex # Index of blob in block - slot: Slot - block_parent_root: Root # Proposer shuffling determinant - proposer_index: ValidatorIndex blob: Blob kzg_commitment: KZGCommitment kzg_proof: KZGProof # Allows for quick verification of kzg_commitment -``` - -#### `SignedBlobSidecar` - -*[New in Deneb:EIP4844]* - -```python -class SignedBlobSidecar(Container): - message: BlobSidecar - signature: BLSSignature + commitment_inclusion_proof: [Bytes32, KZG_COMMITMENT_INCLUSION_PROOF_DEPTH] + signed_block_header: SignedBeaconBlockHeader ``` #### `BlobIdentifier` @@ -95,10 +86,39 @@ class BlobIdentifier(Container): ##### `verify_blob_sidecar_signature` ```python -def verify_blob_sidecar_signature(state: BeaconState, signed_blob_sidecar: SignedBlobSidecar) -> bool: - proposer = state.validators[signed_blob_sidecar.message.proposer_index] - signing_root = compute_signing_root(signed_blob_sidecar.message, get_domain(state, DOMAIN_BLOB_SIDECAR)) - return bls.Verify(proposer.pubkey, signing_root, signed_blob_sidecar.signature) +def verify_blob_sidecar_signature(state: BeaconState, blob_sidecar: BlobSidecar) -> bool: + block_header = blob_sidecar.signed_block_header.message + proposer = state.validators[block_header.proposer_index] + domain = get_domain(state, DOMAIN_BEACON_PROPOSER, compute_epoch_at_slot(block_header.slot)) + signing_root = compute_signing_root(block_header, domain) + return bls.Verify(proposer.pubkey, signing_root, blob_sidecar.signed_block_header.signature) +``` + +##### `verify_blob_sidecar_inclusion_proof` + +```python +def verify_blob_sidecar_inclusion_proof(blob_sidecar: BlobSidecar) -> bool: + commitment_item_gindex = MAX_BLOB_COMMITMENTS_PER_BLOCK + blob_sidecar.index + gindex = BLOB_KZG_COMMITMENT_GINDEX + commitment_item_gindex << floorlog2(BLOB_ZKG_COMMITMENT_GINDEX) + return is_valid_merkle_path( + leaf=blob_sidecar.kzg_commitment.hash_tree_root(), + branch=blob_sidecar.commitment_inclusion_proof, + gindex=gindex, + root=blob_sidecar.signed_block_header.message.body_root, + ) +``` + +#### `is_valid_merkle_path` + +```python +def is_valid_merkle_path(leaf: Bytes32, branch: Sequence[Bytes32], gindex: int, root: Root) -> bool: + value = leaf + for i in range(len(branch)): + if (gindex >> i) & 1 == 0: + value = hash(branch[i] + value) + else: + value = hash(value + branch[i]) + return value == root ``` ### The gossip domain: gossipsub @@ -123,7 +143,7 @@ The new topics along with the type of the `data` field of a gossipsub message ar | Name | Message Type | | - | - | -| `blob_sidecar_{subnet_id}` | `SignedBlobSidecar` [New in Deneb:EIP4844] | +| `blob_sidecar_{subnet_id}` | `BlobSidecar` [New in Deneb:EIP4844] | ##### Global topics @@ -146,18 +166,19 @@ New validation: This topic is used to propagate signed blob sidecars, where each blob index maps to some `subnet_id`. -The following validations MUST pass before forwarding the `signed_blob_sidecar` on the network, assuming the alias `sidecar = signed_blob_sidecar.message`: +The following validations MUST pass before forwarding the `blob_sidecar` on the network, assuming the alias `sidecar = blob_sidecar` and `block_header = blob_sidecar.signed_block_header.message`: - _[REJECT]_ The sidecar's index is consistent with `MAX_BLOBS_PER_BLOCK` -- i.e. `sidecar.index < MAX_BLOBS_PER_BLOCK`. - _[REJECT]_ The sidecar is for the correct subnet -- i.e. `compute_subnet_for_blob_sidecar(sidecar.index) == subnet_id`. -- _[IGNORE]_ The sidecar is not from a future slot (with a `MAXIMUM_GOSSIP_CLOCK_DISPARITY` allowance) -- i.e. validate that `sidecar.slot <= current_slot` (a client MAY queue future sidecars for processing at the appropriate slot). -- _[IGNORE]_ The sidecar is from a slot greater than the latest finalized slot -- i.e. validate that `sidecar.slot > compute_start_slot_at_epoch(state.finalized_checkpoint.epoch)` -- _[IGNORE]_ The sidecar's block's parent (defined by `sidecar.block_parent_root`) has been seen (via both gossip and non-gossip sources) (a client MAY queue sidecars for processing once the parent block is retrieved). -- _[REJECT]_ The sidecar's block's parent (defined by `sidecar.block_parent_root`) passes validation. -- _[REJECT]_ The sidecar is from a higher slot than the sidecar's block's parent (defined by `sidecar.block_parent_root`). -- _[REJECT]_ The proposer signature, `signed_blob_sidecar.signature`, is valid as verified by `verify_blob_sidecar_signature`. -- _[IGNORE]_ The sidecar is the only sidecar with valid signature received for the tuple `(sidecar.block_root, sidecar.index)`. -- _[REJECT]_ The sidecar is proposed by the expected `proposer_index` for the block's slot in the context of the current shuffling (defined by `block_parent_root`/`slot`). +- _[IGNORE]_ The sidecar is not from a future slot (with a `MAXIMUM_GOSSIP_CLOCK_DISPARITY` allowance) -- i.e. validate that `block_header.slot <= current_slot` (a client MAY queue future sidecars for processing at the appropriate slot). +- _[IGNORE]_ The sidecar is from a slot greater than the latest finalized slot -- i.e. validate that `block_header.slot > compute_start_slot_at_epoch(state.finalized_checkpoint.epoch)` +- _[IGNORE]_ The sidecar's block's parent (defined by `block_header.parent_root`) has been seen (via both gossip and non-gossip sources) (a client MAY queue sidecars for processing once the parent block is retrieved). +- _[REJECT]_ The sidecar's block's parent (defined by `block_header.parent_root`) passes validation. +- _[REJECT]_ The sidecar is from a higher slot than the sidecar's block's parent (defined by `block_header.parent_root`). +- _[REJECT]_ The proposer signature in `blob_sidecar.signed_block_header`, is valid as verified by `verify_blob_sidecar_signature`. +- _[REJECT]_ The sidecar's inclusion proof is valid as verified by `verify_blob_sidecar_inclusion_proof`. +- _[IGNORE]_ The sidecar is the only sidecar with valid signature received for the tuple `(hash_tree_root(block_header), sidecar.index)`. +- _[REJECT]_ The sidecar is proposed by the expected `proposer_index` for the block's slot in the context of the current shuffling (defined by `parent_root`/`slot`). If the `proposer_index` cannot immediately be verified against the expected shuffling, the sidecar MAY be queued for later processing while proposers for the block's branch are calculated -- in such a case _do not_ `REJECT`, instead `IGNORE` this message. ###### `beacon_aggregate_and_proof` diff --git a/specs/deneb/validator.md b/specs/deneb/validator.md index 7b8a81b19a..aa9e4a83c5 100644 --- a/specs/deneb/validator.md +++ b/specs/deneb/validator.md @@ -21,8 +21,8 @@ - [Constructing the `BeaconBlockBody`](#constructing-the-beaconblockbody) - [ExecutionPayload](#executionpayload) - [Blob KZG commitments](#blob-kzg-commitments) - - [Constructing the `SignedBlobSidecar`s](#constructing-the-signedblobsidecars) - - [Sidecar](#sidecar) + - [Constructing the `BlobSidecar`s](#constructing-the-blobsidecars) + - [BlobSidecar](#blob-sidecar) @@ -133,49 +133,47 @@ use the `payload_id` to retrieve `blobs`, `blob_kzg_commitments`, and `blob_kzg_ via `get_payload(payload_id).blobs_bundle`. 2. Set `block.body.blob_kzg_commitments = blob_kzg_commitments`. -#### Constructing the `SignedBlobSidecar`s +#### Constructing the `BlobSidecar`s *[New in Deneb:EIP4844]* -To construct a `SignedBlobSidecar`, a `signed_blob_sidecar` is defined with the necessary context for block and sidecar proposal. +To construct a `BlobSidecar`, a `blob_sidecar` is defined with the necessary context for block and sidecar proposal. ##### Sidecar -Blobs associated with a block are packaged into sidecar objects for distribution to the network. +Blobs associated with a block are packaged into sidecar objects for distribution to the associated sidecar topic, the `blob_sidecar_{subnet_id}` pubsub topic. Each `sidecar` is obtained from: ```python -def get_blob_sidecars(block: BeaconBlock, +def get_blob_sidecars(signed_block: SignedBeaconBlock, blobs: Sequence[Blob], blob_kzg_proofs: Sequence[KZGProof]) -> Sequence[BlobSidecar]: + block_header = BeaconBlockHeader( + slot=block.slot, + proposer_index=block.proposer_index, + parent_root=block.parent_root, + state_root=Bytes32(), # Overwritten in the next process_slot call + body_root=hash_tree_root(block.body), + ) + signed_block_header = SignedBeaconBlockHeader(message=block_header, signature=signed_block.signature) return [ BlobSidecar( - block_root=hash_tree_root(block), index=index, - slot=block.slot, - block_parent_root=block.parent_root, blob=blob, - kzg_commitment=block.body.blob_kzg_commitments[index], + kzg_commitment=signed_block.message.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], + index, + ), + signed_block_header=signed_block_header, ) for index, blob in enumerate(blobs) ] ``` -Then for each sidecar, `signed_sidecar = SignedBlobSidecar(message=sidecar, signature=signature)` is constructed and published to the associated sidecar topic, the `blob_sidecar_{subnet_id}` pubsub topic. - -`signature` is obtained from: - -```python -def get_blob_sidecar_signature(state: BeaconState, - sidecar: BlobSidecar, - privkey: int) -> BLSSignature: - domain = get_domain(state, DOMAIN_BLOB_SIDECAR, compute_epoch_at_slot(sidecar.slot)) - signing_root = compute_signing_root(sidecar, domain) - return bls.Sign(privkey, signing_root) -``` - The `subnet_id` for the `signed_sidecar` is calculated with: - Let `blob_index = signed_sidecar.message.index`. - Let `subnet_id = compute_subnet_for_blob_sidecar(blob_index)`. From e8bccecba5493269755b340db1947b84c2cac65e Mon Sep 17 00:00:00 2001 From: dapplion <35266934+dapplion@users.noreply.github.com> Date: Fri, 27 Oct 2023 15:47:33 +0300 Subject: [PATCH 02/30] remove verify_blob_sidecar_signature --- specs/deneb/p2p-interface.md | 14 +------------- specs/deneb/validator.md | 2 +- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/specs/deneb/p2p-interface.md b/specs/deneb/p2p-interface.md index feda3563b6..5772589395 100644 --- a/specs/deneb/p2p-interface.md +++ b/specs/deneb/p2p-interface.md @@ -16,7 +16,6 @@ The specification of these changes continues in the same format as the network s - [`BlobSidecar`](#blobsidecar) - [`BlobIdentifier`](#blobidentifier) - [Helpers](#helpers) - - [`verify_blob_sidecar_signature`](#verify_blob_sidecar_signature) - [`verify_blob_sidecar_inclusion_proof`](#verify_blob_sidecar_inclusion_proof) - [`is_valid_merkle_path`](#is_valid_merkle_path) - [The gossip domain: gossipsub](#the-gossip-domain-gossipsub) @@ -83,17 +82,6 @@ class BlobIdentifier(Container): #### Helpers -##### `verify_blob_sidecar_signature` - -```python -def verify_blob_sidecar_signature(state: BeaconState, blob_sidecar: BlobSidecar) -> bool: - block_header = blob_sidecar.signed_block_header.message - proposer = state.validators[block_header.proposer_index] - domain = get_domain(state, DOMAIN_BEACON_PROPOSER, compute_epoch_at_slot(block_header.slot)) - signing_root = compute_signing_root(block_header, domain) - return bls.Verify(proposer.pubkey, signing_root, blob_sidecar.signed_block_header.signature) -``` - ##### `verify_blob_sidecar_inclusion_proof` ```python @@ -175,7 +163,7 @@ The following validations MUST pass before forwarding the `blob_sidecar` on the - _[IGNORE]_ The sidecar's block's parent (defined by `block_header.parent_root`) has been seen (via both gossip and non-gossip sources) (a client MAY queue sidecars for processing once the parent block is retrieved). - _[REJECT]_ The sidecar's block's parent (defined by `block_header.parent_root`) passes validation. - _[REJECT]_ The sidecar is from a higher slot than the sidecar's block's parent (defined by `block_header.parent_root`). -- _[REJECT]_ The proposer signature in `blob_sidecar.signed_block_header`, is valid as verified by `verify_blob_sidecar_signature`. +- _[REJECT]_ The proposer signature in `blob_sidecar.signed_block_header`, is valid with respect to the `proposer_index` pubkey. - _[REJECT]_ The sidecar's inclusion proof is valid as verified by `verify_blob_sidecar_inclusion_proof`. - _[IGNORE]_ The sidecar is the only sidecar with valid signature received for the tuple `(hash_tree_root(block_header), sidecar.index)`. - _[REJECT]_ The sidecar is proposed by the expected `proposer_index` for the block's slot in the context of the current shuffling (defined by `parent_root`/`slot`). diff --git a/specs/deneb/validator.md b/specs/deneb/validator.md index aa9e4a83c5..f80e015f77 100644 --- a/specs/deneb/validator.md +++ b/specs/deneb/validator.md @@ -152,7 +152,7 @@ def get_blob_sidecars(signed_block: SignedBeaconBlock, slot=block.slot, proposer_index=block.proposer_index, parent_root=block.parent_root, - state_root=Bytes32(), # Overwritten in the next process_slot call + state_root=block.state_root, body_root=hash_tree_root(block.body), ) signed_block_header = SignedBeaconBlockHeader(message=block_header, signature=signed_block.signature) From 29bbdf439331483da5eff39c414f600441891bf2 Mon Sep 17 00:00:00 2001 From: dapplion <35266934+dapplion@users.noreply.github.com> Date: Fri, 27 Oct 2023 16:14:17 +0300 Subject: [PATCH 03/30] compute KZG_COMMITMENT_INCLUSION_PROOF_DEPTH --- specs/deneb/p2p-interface.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/specs/deneb/p2p-interface.md b/specs/deneb/p2p-interface.md index 5772589395..e7fd55847f 100644 --- a/specs/deneb/p2p-interface.md +++ b/specs/deneb/p2p-interface.md @@ -51,8 +51,8 @@ The specification of these changes continues in the same format as the network s | `MAX_REQUEST_BLOB_SIDECARS` | `MAX_REQUEST_BLOCKS_DENEB * MAX_BLOBS_PER_BLOCK` | Maximum number of blob sidecars in a single request | | `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. | -| `KZG_COMMITMENT_INCLUSION_PROOF_DEPTH` | `17` | Merkle proof for `blob_kzg_commitments` list item | -| `BLOB_KZG_COMMITMENT_GINDEX` | `27` | Gindex path to `blob_kzg_commitments` on `BeaconBlockBody` container +| `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 | ### Containers @@ -87,7 +87,7 @@ class BlobIdentifier(Container): ```python def verify_blob_sidecar_inclusion_proof(blob_sidecar: BlobSidecar) -> bool: commitment_item_gindex = MAX_BLOB_COMMITMENTS_PER_BLOCK + blob_sidecar.index - gindex = BLOB_KZG_COMMITMENT_GINDEX + commitment_item_gindex << floorlog2(BLOB_ZKG_COMMITMENT_GINDEX) + gindex = BLOB_KZG_COMMITMENTS_GINDEX + commitment_item_gindex << floorlog2(BLOB_KZG_COMMITMENTS_GINDEX) return is_valid_merkle_path( leaf=blob_sidecar.kzg_commitment.hash_tree_root(), branch=blob_sidecar.commitment_inclusion_proof, From a4a29a164fe62a460a2605d580a16a4b668d311a Mon Sep 17 00:00:00 2001 From: dapplion <35266934+dapplion@users.noreply.github.com> Date: Fri, 27 Oct 2023 16:15:06 +0300 Subject: [PATCH 04/30] List typo --- specs/deneb/p2p-interface.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/deneb/p2p-interface.md b/specs/deneb/p2p-interface.md index e7fd55847f..023cd29c40 100644 --- a/specs/deneb/p2p-interface.md +++ b/specs/deneb/p2p-interface.md @@ -66,7 +66,7 @@ class BlobSidecar(Container): blob: Blob kzg_commitment: KZGCommitment kzg_proof: KZGProof # Allows for quick verification of kzg_commitment - commitment_inclusion_proof: [Bytes32, KZG_COMMITMENT_INCLUSION_PROOF_DEPTH] + commitment_inclusion_proof: List[Bytes32, KZG_COMMITMENT_INCLUSION_PROOF_DEPTH] signed_block_header: SignedBeaconBlockHeader ``` From 3dbe54e4b859dc47907bb011e3bad96756c3024e Mon Sep 17 00:00:00 2001 From: dapplion <35266934+dapplion@users.noreply.github.com> Date: Fri, 27 Oct 2023 16:16:10 +0300 Subject: [PATCH 05/30] doctoc --- specs/deneb/p2p-interface.md | 2 +- specs/deneb/validator.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/specs/deneb/p2p-interface.md b/specs/deneb/p2p-interface.md index 023cd29c40..7846c9a2a3 100644 --- a/specs/deneb/p2p-interface.md +++ b/specs/deneb/p2p-interface.md @@ -17,7 +17,7 @@ The specification of these changes continues in the same format as the network s - [`BlobIdentifier`](#blobidentifier) - [Helpers](#helpers) - [`verify_blob_sidecar_inclusion_proof`](#verify_blob_sidecar_inclusion_proof) - - [`is_valid_merkle_path`](#is_valid_merkle_path) + - [`is_valid_merkle_path`](#is_valid_merkle_path) - [The gossip domain: gossipsub](#the-gossip-domain-gossipsub) - [Topics and messages](#topics-and-messages) - [Global topics](#global-topics) diff --git a/specs/deneb/validator.md b/specs/deneb/validator.md index f80e015f77..49e1136000 100644 --- a/specs/deneb/validator.md +++ b/specs/deneb/validator.md @@ -22,7 +22,7 @@ - [ExecutionPayload](#executionpayload) - [Blob KZG commitments](#blob-kzg-commitments) - [Constructing the `BlobSidecar`s](#constructing-the-blobsidecars) - - [BlobSidecar](#blob-sidecar) + - [Sidecar](#sidecar) 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 06/30] 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 c32bee8305..612ae6cf67 100644 --- a/pysetup/spec_builders/deneb.py +++ b/pysetup/spec_builders/deneb.py @@ -23,7 +23,17 @@ def sundry_functions(cls) -> str: 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 @@ def hardcoded_custom_type_dep_constants(cls, spec_object) -> str: '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 7846c9a2a3..c7044dc1ed 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 49e1136000..13b816b6f5 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 876824107a..ef031693d0 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.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) - - signed_blob_sidecar = spec.SignedBlobSidecar(message=blob_sidecars[1], signature=sidecar_signature) + blob_sidecars = spec.get_blob_sidecars(spec, block, blobs, proofs) - 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) - - sidecar_signature = spec.get_blob_sidecar_signature(state, - blob_sidecars[1], - 123) - - signed_blob_sidecar = spec.SignedBlobSidecar(message=blob_sidecars[1], signature=sidecar_signature) + blob_sidecars = spec.get_blob_sidecars(spec, block, blobs, proofs) - 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) From caa79a5a02d5a421d820f121b3bee7075f6f2752 Mon Sep 17 00:00:00 2001 From: dapplion <35266934+dapplion@users.noreply.github.com> Date: Fri, 27 Oct 2023 18:08:05 +0300 Subject: [PATCH 07/30] build tree --- pysetup/spec_builders/deneb.py | 8 +++----- specs/deneb/beacon-chain.md | 13 +++++++++++++ specs/deneb/p2p-interface.md | 19 ++----------------- specs/deneb/validator.md | 3 +-- .../unittests/validator/test_validator.py | 8 +++----- 5 files changed, 22 insertions(+), 29 deletions(-) diff --git a/pysetup/spec_builders/deneb.py b/pysetup/spec_builders/deneb.py index 612ae6cf67..14087702a9 100644 --- a/pysetup/spec_builders/deneb.py +++ b/pysetup/spec_builders/deneb.py @@ -28,11 +28,9 @@ def retrieve_blobs_and_proofs(beacon_block_root: Root) -> Tuple[Sequence[Blob], 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 [] + index: GeneralizedIndex +) -> Sequence[Bytes32]: + return build_proof(body.get_backing(), index) ''' @classmethod diff --git a/specs/deneb/beacon-chain.md b/specs/deneb/beacon-chain.md index b98ac12592..64bcca5c28 100644 --- a/specs/deneb/beacon-chain.md +++ b/specs/deneb/beacon-chain.md @@ -187,6 +187,19 @@ def kzg_commitment_to_versioned_hash(kzg_commitment: KZGCommitment) -> Versioned return VERSIONED_HASH_VERSION_KZG + hash(kzg_commitment)[1:] ``` +#### `is_valid_merkle_path` + +```python +def is_valid_merkle_path(leaf: Bytes32, branch: Sequence[Bytes32], gindex: int, root: Root) -> bool: + value = leaf + for i in range(len(branch)): + if (gindex >> i) & 1 == 0: + value = hash(branch[i] + value) + else: + value = hash(value + branch[i]) + return value == root +``` + ### Beacon state accessors #### Modified `get_attestation_participation_flag_indices` diff --git a/specs/deneb/p2p-interface.md b/specs/deneb/p2p-interface.md index c7044dc1ed..13ac98f643 100644 --- a/specs/deneb/p2p-interface.md +++ b/specs/deneb/p2p-interface.md @@ -51,7 +51,7 @@ The specification of these changes continues in the same format as the network s | `MAX_REQUEST_BLOB_SIDECARS` | `MAX_REQUEST_BLOCKS_DENEB * MAX_BLOBS_PER_BLOCK` | Maximum number of blob sidecars in a single request | | `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 | +| `BLOB_KZG_COMMITMENTS_GINDEX` | `get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments')` (= 27) | `blob_kzg_commitments` field gindex on `BeaconBlockBody` container | | `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 @@ -86,29 +86,14 @@ class BlobIdentifier(Container): ```python def verify_blob_sidecar_inclusion_proof(blob_sidecar: BlobSidecar) -> bool: - commitment_item_gindex = MAX_BLOB_COMMITMENTS_PER_BLOCK + blob_sidecar.index - gindex = BLOB_KZG_COMMITMENTS_GINDEX + commitment_item_gindex << floorlog2(BLOB_KZG_COMMITMENTS_GINDEX) return is_valid_merkle_path( leaf=blob_sidecar.kzg_commitment.hash_tree_root(), branch=blob_sidecar.commitment_inclusion_proof, - gindex=gindex, + gindex=get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments', blob_sidecar.index), root=blob_sidecar.signed_block_header.message.body_root, ) ``` -#### `is_valid_merkle_path` - -```python -def is_valid_merkle_path(leaf: Bytes32, branch: Sequence[Bytes32], gindex: int, root: Root) -> bool: - value = leaf - for i in range(len(branch)): - if (gindex >> i) & 1 == 0: - value = hash(branch[i] + value) - else: - value = hash(value + branch[i]) - return value == root -``` - ### The gossip domain: gossipsub Some gossip meshes are upgraded in the fork of Deneb to support upgraded types. diff --git a/specs/deneb/validator.md b/specs/deneb/validator.md index 13b816b6f5..ba41adf0b9 100644 --- a/specs/deneb/validator.md +++ b/specs/deneb/validator.md @@ -165,8 +165,7 @@ def get_blob_sidecars(signed_block: SignedBeaconBlock, kzg_proof=blob_kzg_proofs[index], commitment_inclusion_proof=compute_commitment_inclusion_proof( block.body, - block.body.blob_kzg_commitments[index], - index, + get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments', index), ), signed_block_header=signed_block_header, ) 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 ef031693d0..a45acb0710 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 @@ -34,7 +34,6 @@ def get_blob_sidecars(spec, signed_block, blobs, blob_kzg_proofs): 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, @@ -43,10 +42,9 @@ def get_blob_sidecars(spec, signed_block, blobs, blob_kzg_proofs): ] -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 +def compute_commitment_inclusion_proof(spec, body, index): + gindex = spec.get_generalized_index(spec.BeaconBlockBody, 'blob_kzg_commitments', index) + return spec.build_proof(body, gindex) @with_deneb_and_later From 8712451c43c60095eee2e10aaf996cf63710632a Mon Sep 17 00:00:00 2001 From: Lion - dapplion <35266934+dapplion@users.noreply.github.com> Date: Fri, 27 Oct 2023 18:09:01 +0300 Subject: [PATCH 08/30] Update specs/deneb/p2p-interface.md Co-authored-by: Mikhail Kalinin --- specs/deneb/p2p-interface.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/deneb/p2p-interface.md b/specs/deneb/p2p-interface.md index 13ac98f643..346810295c 100644 --- a/specs/deneb/p2p-interface.md +++ b/specs/deneb/p2p-interface.md @@ -151,7 +151,7 @@ The following validations MUST pass before forwarding the `blob_sidecar` on the - _[REJECT]_ The proposer signature in `blob_sidecar.signed_block_header`, is valid with respect to the `proposer_index` pubkey. - _[REJECT]_ The sidecar's inclusion proof is valid as verified by `verify_blob_sidecar_inclusion_proof`. - _[IGNORE]_ The sidecar is the only sidecar with valid signature received for the tuple `(hash_tree_root(block_header), sidecar.index)`. -- _[REJECT]_ The sidecar is proposed by the expected `proposer_index` for the block's slot in the context of the current shuffling (defined by `parent_root`/`slot`). +- _[REJECT]_ The sidecar is proposed by the expected `proposer_index` for the block's slot in the context of the current shuffling (defined by `block_header.parent_root`/`block_header.slot`). If the `proposer_index` cannot immediately be verified against the expected shuffling, the sidecar MAY be queued for later processing while proposers for the block's branch are calculated -- in such a case _do not_ `REJECT`, instead `IGNORE` this message. ###### `beacon_aggregate_and_proof` From f2649f65ba36d3ea6bc0234547759245622226bc Mon Sep 17 00:00:00 2001 From: dapplion <35266934+dapplion@users.noreply.github.com> Date: Fri, 27 Oct 2023 18:51:43 +0300 Subject: [PATCH 09/30] fix unit tests --- specs/deneb/p2p-interface.md | 4 +- specs/deneb/validator.md | 2 +- .../unittests/validator/test_validator.py | 43 +++---------------- 3 files changed, 10 insertions(+), 39 deletions(-) diff --git a/specs/deneb/p2p-interface.md b/specs/deneb/p2p-interface.md index 346810295c..5ace66fde0 100644 --- a/specs/deneb/p2p-interface.md +++ b/specs/deneb/p2p-interface.md @@ -51,8 +51,8 @@ The specification of these changes continues in the same format as the network s | `MAX_REQUEST_BLOB_SIDECARS` | `MAX_REQUEST_BLOCKS_DENEB * MAX_BLOBS_PER_BLOCK` | Maximum number of blob sidecars in a single request | | `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` | `get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments')` (= 27) | `blob_kzg_commitments` field gindex on `BeaconBlockBody` container | -| `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 | +| `BLOB_KZG_COMMITMENTS_GINDEX` | `2**4 + 11` (= 27) | `blob_kzg_commitments` field gindex on `BeaconBlockBody` container | +| `KZG_COMMITMENT_INCLUSION_PROOF_DEPTH` | `4 + 1 + floorlog2(MAX_BLOB_COMMITMENTS_PER_BLOCK)` | Merkle proof for `blob_kzg_commitments` list item | ### Containers diff --git a/specs/deneb/validator.md b/specs/deneb/validator.md index ba41adf0b9..6b21ea30fd 100644 --- a/specs/deneb/validator.md +++ b/specs/deneb/validator.md @@ -165,7 +165,7 @@ def get_blob_sidecars(signed_block: SignedBeaconBlock, kzg_proof=blob_kzg_proofs[index], commitment_inclusion_proof=compute_commitment_inclusion_proof( block.body, - get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments', index), + get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments', index), # type: ignore ), signed_block_header=signed_block_header, ) 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 a45acb0710..52374d42f5 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 @@ -10,46 +10,15 @@ get_sample_opaque_tx, ) from eth2spec.test.helpers.block import ( - build_empty_block_for_next_slot + build_empty_block_for_next_slot, + sign_block ) 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, - index, - ), - signed_block_header=signed_block_header, - ) - for index, blob in enumerate(blobs) - ] - - -def compute_commitment_inclusion_proof(spec, body, index): - gindex = spec.get_generalized_index(spec.BeaconBlockBody, 'blob_kzg_commitments', index) - return spec.build_proof(body, gindex) - - @with_deneb_and_later @spec_state_test -def test_blob_sidecar_inclusion_proof(spec, state): +def test_blob_sidecar_inclusion_proof_correct(spec, state): """ Test `verify_blob_sidecar_inclusion_proof` """ @@ -60,7 +29,8 @@ def test_blob_sidecar_inclusion_proof(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(spec, block, blobs, proofs) + signed_block = sign_block(spec, state, block, proposer_index=0) + blob_sidecars = spec.get_blob_sidecars(signed_block, blobs, proofs) for blob_sidecar in blob_sidecars: assert spec.verify_blob_sidecar_inclusion_proof(blob_sidecar) @@ -80,7 +50,8 @@ def test_blob_sidecar_inclusion_proof_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(spec, block, blobs, proofs) + signed_block = sign_block(spec, state, block, proposer_index=0) + blob_sidecars = spec.get_blob_sidecars(signed_block, blobs, proofs) for blob_sidecar in blob_sidecars: block = blob_sidecar.signed_block_header.message From 83e5930b5ca98a9993e285f4dc92768d548c5599 Mon Sep 17 00:00:00 2001 From: dapplion <35266934+dapplion@users.noreply.github.com> Date: Fri, 27 Oct 2023 20:08:40 +0300 Subject: [PATCH 10/30] doctoc --- specs/deneb/beacon-chain.md | 1 + specs/deneb/p2p-interface.md | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/deneb/beacon-chain.md b/specs/deneb/beacon-chain.md index 64bcca5c28..fde75b0ed3 100644 --- a/specs/deneb/beacon-chain.md +++ b/specs/deneb/beacon-chain.md @@ -25,6 +25,7 @@ - [Helper functions](#helper-functions) - [Misc](#misc) - [`kzg_commitment_to_versioned_hash`](#kzg_commitment_to_versioned_hash) + - [`is_valid_merkle_path`](#is_valid_merkle_path) - [Beacon state accessors](#beacon-state-accessors) - [Modified `get_attestation_participation_flag_indices`](#modified-get_attestation_participation_flag_indices) - [New `get_validator_activation_churn_limit`](#new-get_validator_activation_churn_limit) diff --git a/specs/deneb/p2p-interface.md b/specs/deneb/p2p-interface.md index 5ace66fde0..3a4721cee8 100644 --- a/specs/deneb/p2p-interface.md +++ b/specs/deneb/p2p-interface.md @@ -17,7 +17,6 @@ The specification of these changes continues in the same format as the network s - [`BlobIdentifier`](#blobidentifier) - [Helpers](#helpers) - [`verify_blob_sidecar_inclusion_proof`](#verify_blob_sidecar_inclusion_proof) - - [`is_valid_merkle_path`](#is_valid_merkle_path) - [The gossip domain: gossipsub](#the-gossip-domain-gossipsub) - [Topics and messages](#topics-and-messages) - [Global topics](#global-topics) From 0bf9e75964515e62fb999f931138c948bff89ad7 Mon Sep 17 00:00:00 2001 From: dapplion <35266934+dapplion@users.noreply.github.com> Date: Fri, 27 Oct 2023 20:22:17 +0300 Subject: [PATCH 11/30] review PR --- pysetup/spec_builders/deneb.py | 5 +---- specs/deneb/p2p-interface.md | 8 ++++---- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/pysetup/spec_builders/deneb.py b/pysetup/spec_builders/deneb.py index 14087702a9..9d976c451e 100644 --- a/pysetup/spec_builders/deneb.py +++ b/pysetup/spec_builders/deneb.py @@ -26,10 +26,7 @@ def retrieve_blobs_and_proofs(beacon_block_root: Root) -> Tuple[Sequence[Blob], return [], [] -def compute_commitment_inclusion_proof( - body: BeaconBlockBody, - index: GeneralizedIndex -) -> Sequence[Bytes32]: +def compute_commitment_inclusion_proof(body: BeaconBlockBody, index: GeneralizedIndex) -> Sequence[Bytes32]: return build_proof(body.get_backing(), index) ''' diff --git a/specs/deneb/p2p-interface.md b/specs/deneb/p2p-interface.md index 3a4721cee8..b3c3850864 100644 --- a/specs/deneb/p2p-interface.md +++ b/specs/deneb/p2p-interface.md @@ -51,7 +51,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` | `2**4 + 11` (= 27) | `blob_kzg_commitments` field gindex on `BeaconBlockBody` container | -| `KZG_COMMITMENT_INCLUSION_PROOF_DEPTH` | `4 + 1 + floorlog2(MAX_BLOB_COMMITMENTS_PER_BLOCK)` | Merkle proof for `blob_kzg_commitments` list item | +| `KZG_COMMITMENT_INCLUSION_PROOF_DEPTH` | `4 + 1 + floorlog2(MAX_BLOB_COMMITMENTS_PER_BLOCK)` | Merkle proof depth for `blob_kzg_commitments` list item | ### Containers @@ -65,8 +65,8 @@ class BlobSidecar(Container): blob: Blob kzg_commitment: KZGCommitment kzg_proof: KZGProof # Allows for quick verification of kzg_commitment - commitment_inclusion_proof: List[Bytes32, KZG_COMMITMENT_INCLUSION_PROOF_DEPTH] signed_block_header: SignedBeaconBlockHeader + commitment_inclusion_proof: List[Bytes32, KZG_COMMITMENT_INCLUSION_PROOF_DEPTH] ``` #### `BlobIdentifier` @@ -147,9 +147,9 @@ The following validations MUST pass before forwarding the `blob_sidecar` on the - _[IGNORE]_ The sidecar's block's parent (defined by `block_header.parent_root`) has been seen (via both gossip and non-gossip sources) (a client MAY queue sidecars for processing once the parent block is retrieved). - _[REJECT]_ The sidecar's block's parent (defined by `block_header.parent_root`) passes validation. - _[REJECT]_ The sidecar is from a higher slot than the sidecar's block's parent (defined by `block_header.parent_root`). -- _[REJECT]_ The proposer signature in `blob_sidecar.signed_block_header`, is valid with respect to the `proposer_index` pubkey. +- _[REJECT]_ The proposer signature of `blob_sidecar.signed_block_header`, is valid with respect to the `block_header.proposer_index` pubkey. - _[REJECT]_ The sidecar's inclusion proof is valid as verified by `verify_blob_sidecar_inclusion_proof`. -- _[IGNORE]_ The sidecar is the only sidecar with valid signature received for the tuple `(hash_tree_root(block_header), sidecar.index)`. +- _[IGNORE]_ The sidecar is the first sidecar for the tuple (block_header.slot, block_header.proposer_index, sidecar.index) with valid header signature and sidecar inclusion proof - _[REJECT]_ The sidecar is proposed by the expected `proposer_index` for the block's slot in the context of the current shuffling (defined by `block_header.parent_root`/`block_header.slot`). If the `proposer_index` cannot immediately be verified against the expected shuffling, the sidecar MAY be queued for later processing while proposers for the block's branch are calculated -- in such a case _do not_ `REJECT`, instead `IGNORE` this message. From 26516ec3e96648b09e1b8ce978abb5d67bdc3332 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Sat, 28 Oct 2023 03:53:16 +0800 Subject: [PATCH 12/30] Move `KZG_COMMITMENT_INCLUSION_PROOF_DEPTH` to preset and cast `int()` --- presets/mainnet/deneb.yaml | 2 ++ presets/minimal/deneb.yaml | 2 ++ specs/deneb/p2p-interface.md | 10 ++++++++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/presets/mainnet/deneb.yaml b/presets/mainnet/deneb.yaml index 23889fd18e..38db7e90a0 100644 --- a/presets/mainnet/deneb.yaml +++ b/presets/mainnet/deneb.yaml @@ -8,3 +8,5 @@ FIELD_ELEMENTS_PER_BLOB: 4096 MAX_BLOB_COMMITMENTS_PER_BLOCK: 4096 # `uint64(6)` MAX_BLOBS_PER_BLOCK: 6 +# `4 + 1 + floorlog2(MAX_BLOB_COMMITMENTS_PER_BLOCK)` = 4 + 1 + 12 = 17 +KZG_COMMITMENT_INCLUSION_PROOF_DEPTH: 17 diff --git a/presets/minimal/deneb.yaml b/presets/minimal/deneb.yaml index 9721bec14a..cac61bdbf3 100644 --- a/presets/minimal/deneb.yaml +++ b/presets/minimal/deneb.yaml @@ -8,3 +8,5 @@ FIELD_ELEMENTS_PER_BLOB: 4096 MAX_BLOB_COMMITMENTS_PER_BLOCK: 16 # `uint64(6)` MAX_BLOBS_PER_BLOCK: 6 +# [customized] `4 + 1 + floorlog2(MAX_BLOB_COMMITMENTS_PER_BLOCK)` = 4 + 1 + 4 = 9 +KZG_COMMITMENT_INCLUSION_PROOF_DEPTH: 9 diff --git a/specs/deneb/p2p-interface.md b/specs/deneb/p2p-interface.md index b3c3850864..fa4378387a 100644 --- a/specs/deneb/p2p-interface.md +++ b/specs/deneb/p2p-interface.md @@ -40,6 +40,14 @@ The specification of these changes continues in the same format as the network s ## Modifications in Deneb +### Preset + +*[New in Deneb:EIP4844]* + +| Name | Value | Description | +|------------------------------------------|-----------------------------------|---------------------------------------------------------------------| +| `KZG_COMMITMENT_INCLUSION_PROOF_DEPTH` | `int(4 + 1 + floorlog2(MAX_BLOB_COMMITMENTS_PER_BLOCK))` | Merkle proof depth for `blob_kzg_commitments` list item | + ### Configuration *[New in Deneb:EIP4844]* @@ -50,8 +58,6 @@ The specification of these changes continues in the same format as the network s | `MAX_REQUEST_BLOB_SIDECARS` | `MAX_REQUEST_BLOCKS_DENEB * MAX_BLOBS_PER_BLOCK` | Maximum number of blob sidecars in a single request | | `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` | `2**4 + 11` (= 27) | `blob_kzg_commitments` field gindex on `BeaconBlockBody` container | -| `KZG_COMMITMENT_INCLUSION_PROOF_DEPTH` | `4 + 1 + floorlog2(MAX_BLOB_COMMITMENTS_PER_BLOCK)` | Merkle proof depth for `blob_kzg_commitments` list item | ### Containers From 1657d16afd5d8def1b4f33999e7043f9ca7cd407 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Sun, 29 Oct 2023 02:33:07 +0800 Subject: [PATCH 13/30] Add `BLOB_KZG_COMMITMENTS_GINDEX` to "Constant". Use pyspec parser trick to add assertion in pyspec --- presets/mainnet/deneb.yaml | 4 +++- presets/minimal/deneb.yaml | 4 +++- pysetup/helpers.py | 7 ++++++- pysetup/spec_builders/base.py | 4 ++++ pysetup/spec_builders/deneb.py | 15 ++++++++++++++- pysetup/typing.py | 1 + setup.py | 15 +++++++++++++++ specs/deneb/p2p-interface.md | 10 +++++++++- 8 files changed, 55 insertions(+), 5 deletions(-) diff --git a/presets/mainnet/deneb.yaml b/presets/mainnet/deneb.yaml index 38db7e90a0..2a1e02e1ca 100644 --- a/presets/mainnet/deneb.yaml +++ b/presets/mainnet/deneb.yaml @@ -8,5 +8,7 @@ FIELD_ELEMENTS_PER_BLOB: 4096 MAX_BLOB_COMMITMENTS_PER_BLOCK: 4096 # `uint64(6)` MAX_BLOBS_PER_BLOCK: 6 -# `4 + 1 + floorlog2(MAX_BLOB_COMMITMENTS_PER_BLOCK)` = 4 + 1 + 12 = 17 +# `get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments')` = 27 +BLOB_KZG_COMMITMENTS_GINDEX: 27 +# `floorlog2(BLOB_KZG_COMMITMENTS_GINDEX) + 1 + ceillog2(MAX_BLOB_COMMITMENTS_PER_BLOCK)` = 4 + 1 + 12 = 17 KZG_COMMITMENT_INCLUSION_PROOF_DEPTH: 17 diff --git a/presets/minimal/deneb.yaml b/presets/minimal/deneb.yaml index cac61bdbf3..19c22cf946 100644 --- a/presets/minimal/deneb.yaml +++ b/presets/minimal/deneb.yaml @@ -8,5 +8,7 @@ FIELD_ELEMENTS_PER_BLOB: 4096 MAX_BLOB_COMMITMENTS_PER_BLOCK: 16 # `uint64(6)` MAX_BLOBS_PER_BLOCK: 6 -# [customized] `4 + 1 + floorlog2(MAX_BLOB_COMMITMENTS_PER_BLOCK)` = 4 + 1 + 4 = 9 +# `get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments')` = 27 +BLOB_KZG_COMMITMENTS_GINDEX: 27 +# [customized] `floorlog2(BLOB_KZG_COMMITMENTS_GINDEX) + 1 + ceillog2(MAX_BLOB_COMMITMENTS_PER_BLOCK)` = 4 + 1 + 4 = 9 KZG_COMMITMENT_INCLUSION_PROOF_DEPTH: 9 diff --git a/pysetup/helpers.py b/pysetup/helpers.py index 692aaa0d7e..cfb4dec4e9 100644 --- a/pysetup/helpers.py +++ b/pysetup/helpers.py @@ -111,8 +111,9 @@ def format_constant(name: str, vardef: VariableDefinition) -> str: return out # Merge all constant objects - hardcoded_ssz_dep_constants = reduce(lambda obj, builder: {**obj, **builder.hardcoded_ssz_dep_constants()}, builders, {}) + hardcoded_ssz_dep_constants = reduce(lambda obj, builder: {**obj, **builder.hardcoded_ssz_dep_constants()}, builders, {}) hardcoded_custom_type_dep_constants = reduce(lambda obj, builder: {**obj, **builder.hardcoded_custom_type_dep_constants(spec_object)}, builders, {}) + hardcoded_func_dep_presets = reduce(lambda obj, builder: {**obj, **builder.hardcoded_func_dep_presets(spec_object)}, builders, {}) # Concatenate all strings imports = reduce(lambda txt, builder: (txt + "\n\n" + builder.imports(preset_name) ).strip("\n"), builders, "") preparations = reduce(lambda txt, builder: (txt + "\n\n" + builder.preparations() ).strip("\n"), builders, "") @@ -126,6 +127,7 @@ def format_constant(name: str, vardef: VariableDefinition) -> str: ssz_dep_constants = '\n'.join(map(lambda x: '%s = %s' % (x, hardcoded_ssz_dep_constants[x]), hardcoded_ssz_dep_constants)) ssz_dep_constants_verification = '\n'.join(map(lambda x: 'assert %s == %s' % (x, spec_object.ssz_dep_constants[x]), hardcoded_ssz_dep_constants)) custom_type_dep_constants = '\n'.join(map(lambda x: '%s = %s' % (x, hardcoded_custom_type_dep_constants[x]), hardcoded_custom_type_dep_constants)) + func_dep_presets_verification = '\n'.join(map(lambda x: 'assert %s == %s # noqa: E501' % (x, spec_object.func_dep_presets[x]), hardcoded_func_dep_presets)) spec_strs = [ imports, preparations, @@ -147,6 +149,7 @@ def format_constant(name: str, vardef: VariableDefinition) -> str: # Since some constants are hardcoded in setup.py, the following assertions verify that the hardcoded constants are # as same as the spec definition. ssz_dep_constants_verification, + func_dep_presets_verification, ] return "\n\n\n".join([str.strip("\n") for str in spec_strs if str]) + "\n" @@ -223,6 +226,7 @@ def combine_spec_objects(spec0: SpecObject, spec1: SpecObject) -> SpecObject: preset_vars = combine_dicts(spec0.preset_vars, spec1.preset_vars) config_vars = combine_dicts(spec0.config_vars, spec1.config_vars) ssz_dep_constants = combine_dicts(spec0.ssz_dep_constants, spec1.ssz_dep_constants) + func_dep_presets = combine_dicts(spec0.func_dep_presets, spec1.func_dep_presets) ssz_objects = combine_ssz_objects(spec0.ssz_objects, spec1.ssz_objects, custom_types) dataclasses = combine_dicts(spec0.dataclasses, spec1.dataclasses) return SpecObject( @@ -233,6 +237,7 @@ def combine_spec_objects(spec0: SpecObject, spec1: SpecObject) -> SpecObject: preset_vars=preset_vars, config_vars=config_vars, ssz_dep_constants=ssz_dep_constants, + func_dep_presets=func_dep_presets, ssz_objects=ssz_objects, dataclasses=dataclasses, ) diff --git a/pysetup/spec_builders/base.py b/pysetup/spec_builders/base.py index 44743682ab..ad9a2cb4c4 100644 --- a/pysetup/spec_builders/base.py +++ b/pysetup/spec_builders/base.py @@ -47,6 +47,10 @@ def hardcoded_custom_type_dep_constants(cls, spec_object) -> Dict[str, str]: # """ return {} + @classmethod + def hardcoded_func_dep_presets(cls, spec_object) -> Dict[str, str]: + return {} + @classmethod def implement_optimizations(cls, functions: Dict[str, str]) -> Dict[str, str]: return functions diff --git a/pysetup/spec_builders/deneb.py b/pysetup/spec_builders/deneb.py index 9d976c451e..febcd2742c 100644 --- a/pysetup/spec_builders/deneb.py +++ b/pysetup/spec_builders/deneb.py @@ -1,3 +1,4 @@ +from typing import Dict from .base import BaseSpecBuilder from ..constants import DENEB @@ -68,10 +69,22 @@ def verify_and_notify_new_payload(self: ExecutionEngine, @classmethod - def hardcoded_custom_type_dep_constants(cls, spec_object) -> str: + def hardcoded_custom_type_dep_constants(cls, spec_object) -> Dict[str, str]: return { '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, } + + @classmethod + def hardcoded_ssz_dep_constants(cls) -> Dict[str, str]: + return { + 'BLOB_KZG_COMMITMENTS_GINDEX': 'GeneralizedIndex(27)', + } + + @classmethod + def hardcoded_func_dep_presets(cls, spec_object) -> Dict[str, str]: + return { + 'KZG_COMMITMENT_INCLUSION_PROOF_DEPTH': spec_object.preset_vars['KZG_COMMITMENT_INCLUSION_PROOF_DEPTH'].value, + } diff --git a/pysetup/typing.py b/pysetup/typing.py index 56f0cf3b10..28c2220c50 100644 --- a/pysetup/typing.py +++ b/pysetup/typing.py @@ -22,6 +22,7 @@ class SpecObject(NamedTuple): preset_vars: Dict[str, VariableDefinition] config_vars: Dict[str, VariableDefinition] ssz_dep_constants: Dict[str, str] # the constants that depend on ssz_objects + func_dep_presets: Dict[str, str] # the constants that depend on functions ssz_objects: Dict[str, str] dataclasses: Dict[str, str] diff --git a/setup.py b/setup.py index f540094348..87a3dc3ca8 100644 --- a/setup.py +++ b/setup.py @@ -162,6 +162,7 @@ def get_spec(file_name: Path, preset: Dict[str, str], config: Dict[str, str], pr preset_vars: Dict[str, VariableDefinition] = {} config_vars: Dict[str, VariableDefinition] = {} ssz_dep_constants: Dict[str, str] = {} + func_dep_presets: Dict[str, str] = {} ssz_objects: Dict[str, str] = {} dataclasses: Dict[str, str] = {} custom_types: Dict[str, str] = {} @@ -214,6 +215,16 @@ def get_spec(file_name: Path, preset: Dict[str, str], config: Dict[str, str], pr value_cell = cells[1] value = value_cell.children[0].children + + description = None + if len(cells) >= 3: + description_cell = cells[2] + if len(description_cell.children) > 0: + description = description_cell.children[0].children + if isinstance(description, list): + # marko parses `**X**` as a list containing a X + description = description[0].children + if isinstance(value, list): # marko parses `**X**` as a list containing a X value = value[0].children @@ -228,6 +239,9 @@ def get_spec(file_name: Path, preset: Dict[str, str], config: Dict[str, str], pr ssz_dep_constants[name] = value continue + if description is not None and description.startswith(""): + func_dep_presets[name] = value + value_def = _parse_value(name, value) if name in preset: preset_vars[name] = VariableDefinition(value_def.type_name, preset[name], value_def.comment, None) @@ -256,6 +270,7 @@ def get_spec(file_name: Path, preset: Dict[str, str], config: Dict[str, str], pr preset_vars=preset_vars, config_vars=config_vars, ssz_dep_constants=ssz_dep_constants, + func_dep_presets=func_dep_presets, ssz_objects=ssz_objects, dataclasses=dataclasses, ) diff --git a/specs/deneb/p2p-interface.md b/specs/deneb/p2p-interface.md index fa4378387a..05c6b568d2 100644 --- a/specs/deneb/p2p-interface.md +++ b/specs/deneb/p2p-interface.md @@ -40,13 +40,21 @@ The specification of these changes continues in the same format as the network s ## Modifications in Deneb +### Constant + +*[New in Deneb:EIP4844]* + +| Name | Value | Description | +|------------------------------------------|-----------------------------------|---------------------------------------------------------------------| +| `BLOB_KZG_COMMITMENTS_GINDEX` | `get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments')` (= 27) | `blob_kzg_commitments` field gindex on `BeaconBlockBody` container | + ### Preset *[New in Deneb:EIP4844]* | Name | Value | Description | |------------------------------------------|-----------------------------------|---------------------------------------------------------------------| -| `KZG_COMMITMENT_INCLUSION_PROOF_DEPTH` | `int(4 + 1 + floorlog2(MAX_BLOB_COMMITMENTS_PER_BLOCK))` | Merkle proof depth for `blob_kzg_commitments` list item | +| `KZG_COMMITMENT_INCLUSION_PROOF_DEPTH` | `uint64(floorlog2(BLOB_KZG_COMMITMENTS_GINDEX) + 1 + ceillog2(MAX_BLOB_COMMITMENTS_PER_BLOCK))` (= 17) | Merkle proof depth for `blob_kzg_commitments` list item | ### Configuration From de3b6a24dc107834dce3e195a243b261d740a72a Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Sun, 29 Oct 2023 02:33:39 +0800 Subject: [PATCH 14/30] Fix toc --- specs/deneb/p2p-interface.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/specs/deneb/p2p-interface.md b/specs/deneb/p2p-interface.md index 05c6b568d2..9fd61c7edf 100644 --- a/specs/deneb/p2p-interface.md +++ b/specs/deneb/p2p-interface.md @@ -11,6 +11,8 @@ The specification of these changes continues in the same format as the network s - [Modifications in Deneb](#modifications-in-deneb) + - [Constant](#constant) + - [Preset](#preset) - [Configuration](#configuration) - [Containers](#containers) - [`BlobSidecar`](#blobsidecar) From b7e0b88cb18a9e726af9ff1e7828c778de899d77 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Sun, 29 Oct 2023 03:28:57 +0800 Subject: [PATCH 15/30] Fix test --- specs/deneb/p2p-interface.md | 6 ++++-- .../test/deneb/unittests/validator/test_validator.py | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/specs/deneb/p2p-interface.md b/specs/deneb/p2p-interface.md index 9fd61c7edf..5d640ddf54 100644 --- a/specs/deneb/p2p-interface.md +++ b/specs/deneb/p2p-interface.md @@ -101,10 +101,12 @@ class BlobIdentifier(Container): ```python def verify_blob_sidecar_inclusion_proof(blob_sidecar: BlobSidecar) -> bool: - return is_valid_merkle_path( + gindex = get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments', blob_sidecar.index) + return is_valid_merkle_branch( leaf=blob_sidecar.kzg_commitment.hash_tree_root(), branch=blob_sidecar.commitment_inclusion_proof, - gindex=get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments', blob_sidecar.index), + depth=floorlog2(gindex), + index=get_subtree_index(gindex), root=blob_sidecar.signed_block_header.message.body_root, ) ``` 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 52374d42f5..8adea7d31d 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 @@ -55,5 +55,5 @@ def test_blob_sidecar_inclusion_proof_incorrect(spec, state): 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 + block.body_root = spec.hash(block.body_root) # mutate body root to break proof assert not spec.verify_blob_sidecar_inclusion_proof(blob_sidecar) From b018fbc46a37c23ff57b27d0a9678cbf0082561a Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Sun, 29 Oct 2023 03:39:24 +0800 Subject: [PATCH 16/30] Remove `BLOB_KZG_COMMITMENTS_GINDEX` from the preset files --- presets/mainnet/deneb.yaml | 2 -- presets/minimal/deneb.yaml | 2 -- 2 files changed, 4 deletions(-) diff --git a/presets/mainnet/deneb.yaml b/presets/mainnet/deneb.yaml index 2a1e02e1ca..6d2fb4abde 100644 --- a/presets/mainnet/deneb.yaml +++ b/presets/mainnet/deneb.yaml @@ -8,7 +8,5 @@ FIELD_ELEMENTS_PER_BLOB: 4096 MAX_BLOB_COMMITMENTS_PER_BLOCK: 4096 # `uint64(6)` MAX_BLOBS_PER_BLOCK: 6 -# `get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments')` = 27 -BLOB_KZG_COMMITMENTS_GINDEX: 27 # `floorlog2(BLOB_KZG_COMMITMENTS_GINDEX) + 1 + ceillog2(MAX_BLOB_COMMITMENTS_PER_BLOCK)` = 4 + 1 + 12 = 17 KZG_COMMITMENT_INCLUSION_PROOF_DEPTH: 17 diff --git a/presets/minimal/deneb.yaml b/presets/minimal/deneb.yaml index 19c22cf946..340e8d8d5f 100644 --- a/presets/minimal/deneb.yaml +++ b/presets/minimal/deneb.yaml @@ -8,7 +8,5 @@ FIELD_ELEMENTS_PER_BLOB: 4096 MAX_BLOB_COMMITMENTS_PER_BLOCK: 16 # `uint64(6)` MAX_BLOBS_PER_BLOCK: 6 -# `get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments')` = 27 -BLOB_KZG_COMMITMENTS_GINDEX: 27 # [customized] `floorlog2(BLOB_KZG_COMMITMENTS_GINDEX) + 1 + ceillog2(MAX_BLOB_COMMITMENTS_PER_BLOCK)` = 4 + 1 + 4 = 9 KZG_COMMITMENT_INCLUSION_PROOF_DEPTH: 9 From ae6a9eba264c890179ad4b1c6b50debdd8d0519d Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Sun, 29 Oct 2023 03:39:39 +0800 Subject: [PATCH 17/30] Fix lint --- .../eth2spec/test/deneb/unittests/validator/test_validator.py | 1 - 1 file changed, 1 deletion(-) 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 8adea7d31d..6cd3011fcb 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 @@ -13,7 +13,6 @@ build_empty_block_for_next_slot, sign_block ) -from tests.core.pyspec.eth2spec.utils.ssz.ssz_impl import hash_tree_root @with_deneb_and_later From 0e4737eba29c6bba500cc5c1bc7b49990b7cf37a Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Mon, 30 Oct 2023 14:05:09 +0800 Subject: [PATCH 18/30] Add a general `compute_merkle_proof` helper to replace container-specific helpers --- pysetup/helpers.py | 3 +-- pysetup/spec_builders/altair.py | 6 +++--- pysetup/spec_builders/capella.py | 9 --------- pysetup/spec_builders/deneb.py | 4 ---- specs/altair/light-client/full-node.md | 18 +++++++++--------- specs/capella/light-client/full-node.md | 11 +---------- specs/deneb/light-client/full-node.md | 2 +- specs/deneb/validator.md | 2 +- .../light_client/test_single_merkle_proof.py | 9 +++------ .../light_client/test_single_merkle_proof.py | 3 +-- .../eth2spec/test/helpers/light_client.py | 6 ++---- 11 files changed, 22 insertions(+), 51 deletions(-) diff --git a/pysetup/helpers.py b/pysetup/helpers.py index cfb4dec4e9..b2fe00f75e 100644 --- a/pysetup/helpers.py +++ b/pysetup/helpers.py @@ -68,8 +68,7 @@ def format_protocol(protocol_name: str, protocol_def: ProtocolDefinition) -> str if k in [ "ceillog2", "floorlog2", - "compute_merkle_proof_for_block_body", - "compute_merkle_proof_for_state", + "compute_merkle_proof", ]: del spec_object.functions[k] diff --git a/pysetup/spec_builders/altair.py b/pysetup/spec_builders/altair.py index 4b35380de9..ff667b19ce 100644 --- a/pysetup/spec_builders/altair.py +++ b/pysetup/spec_builders/altair.py @@ -34,9 +34,9 @@ def get_generalized_index(ssz_class: Any, *path: Sequence[PyUnion[int, SSZVariab return GeneralizedIndex(ssz_path.gindex()) -def compute_merkle_proof_for_state(state: BeaconState, - index: GeneralizedIndex) -> Sequence[Bytes32]: - return build_proof(state.get_backing(), index)''' +def compute_merkle_proof(object: SSZObject, + index: GeneralizedIndex) -> Sequence[Bytes32]: + return build_proof(object.get_backing(), index)''' @classmethod diff --git a/pysetup/spec_builders/capella.py b/pysetup/spec_builders/capella.py index 03b619b66a..080bdb537f 100644 --- a/pysetup/spec_builders/capella.py +++ b/pysetup/spec_builders/capella.py @@ -13,15 +13,6 @@ def imports(cls, preset_name: str): from eth2spec.bellatrix import {preset_name} as bellatrix ''' - - @classmethod - def sundry_functions(cls) -> str: - return ''' -def compute_merkle_proof_for_block_body(body: BeaconBlockBody, - index: GeneralizedIndex) -> Sequence[Bytes32]: - return build_proof(body.get_backing(), index)''' - - @classmethod def hardcoded_ssz_dep_constants(cls) -> Dict[str, str]: return { diff --git a/pysetup/spec_builders/deneb.py b/pysetup/spec_builders/deneb.py index febcd2742c..486018f868 100644 --- a/pysetup/spec_builders/deneb.py +++ b/pysetup/spec_builders/deneb.py @@ -25,10 +25,6 @@ def sundry_functions(cls) -> str: def retrieve_blobs_and_proofs(beacon_block_root: Root) -> Tuple[Sequence[Blob], Sequence[KZGProof]]: # pylint: disable=unused-argument return [], [] - - -def compute_commitment_inclusion_proof(body: BeaconBlockBody, index: GeneralizedIndex) -> Sequence[Bytes32]: - return build_proof(body.get_backing(), index) ''' @classmethod diff --git a/specs/altair/light-client/full-node.md b/specs/altair/light-client/full-node.md index 7dc25448c1..27651af01f 100644 --- a/specs/altair/light-client/full-node.md +++ b/specs/altair/light-client/full-node.md @@ -10,7 +10,7 @@ - [Introduction](#introduction) - [Helper functions](#helper-functions) - - [`compute_merkle_proof_for_state`](#compute_merkle_proof_for_state) + - [`compute_merkle_proof`](#compute_merkle_proof) - [`block_to_light_client_header`](#block_to_light_client_header) - [Deriving light client data](#deriving-light-client-data) - [`create_light_client_bootstrap`](#create_light_client_bootstrap) @@ -27,11 +27,13 @@ This document provides helper functions to enable full nodes to serve light clie ## Helper functions -### `compute_merkle_proof_for_state` +### `compute_merkle_proof` + +This function return the Merkle proof of the given SSZ object `object` at generalized index `index`. ```python -def compute_merkle_proof_for_state(state: BeaconState, - index: GeneralizedIndex) -> Sequence[Bytes32]: +def compute_merkle_proof(object: SSZObject, + index: GeneralizedIndex) -> Sequence[Bytes32]: ... ``` @@ -73,7 +75,7 @@ def create_light_client_bootstrap(state: BeaconState, return LightClientBootstrap( header=block_to_light_client_header(block), current_sync_committee=state.current_sync_committee, - current_sync_committee_branch=compute_merkle_proof_for_state(state, CURRENT_SYNC_COMMITTEE_INDEX), + current_sync_committee_branch=compute_merkle_proof(state, CURRENT_SYNC_COMMITTEE_INDEX), ) ``` @@ -120,8 +122,7 @@ def create_light_client_update(state: BeaconState, # `next_sync_committee` is only useful if the message is signed by the current sync committee if update_attested_period == update_signature_period: update.next_sync_committee = attested_state.next_sync_committee - update.next_sync_committee_branch = compute_merkle_proof_for_state( - attested_state, NEXT_SYNC_COMMITTEE_INDEX) + update.next_sync_committee_branch = compute_merkle_proof(attested_state, NEXT_SYNC_COMMITTEE_INDEX) # Indicate finality whenever possible if finalized_block is not None: @@ -130,8 +131,7 @@ def create_light_client_update(state: BeaconState, assert hash_tree_root(update.finalized_header.beacon) == attested_state.finalized_checkpoint.root else: assert attested_state.finalized_checkpoint.root == Bytes32() - update.finality_branch = compute_merkle_proof_for_state( - attested_state, FINALIZED_ROOT_INDEX) + update.finality_branch = compute_merkle_proof(attested_state, FINALIZED_ROOT_INDEX) update.sync_aggregate = block.message.body.sync_aggregate update.signature_slot = block.message.slot diff --git a/specs/capella/light-client/full-node.md b/specs/capella/light-client/full-node.md index a406cd771b..c59af8ec74 100644 --- a/specs/capella/light-client/full-node.md +++ b/specs/capella/light-client/full-node.md @@ -10,7 +10,6 @@ - [Introduction](#introduction) - [Helper functions](#helper-functions) - - [`compute_merkle_proof_for_block_body`](#compute_merkle_proof_for_block_body) - [Modified `block_to_light_client_header`](#modified-block_to_light_client_header) @@ -22,14 +21,6 @@ This upgrade adds information about the execution payload to light client data a ## Helper functions -### `compute_merkle_proof_for_block_body` - -```python -def compute_merkle_proof_for_block_body(body: BeaconBlockBody, - index: GeneralizedIndex) -> Sequence[Bytes32]: - ... -``` - ### Modified `block_to_light_client_header` ```python @@ -55,7 +46,7 @@ def block_to_light_client_header(block: SignedBeaconBlock) -> LightClientHeader: transactions_root=hash_tree_root(payload.transactions), withdrawals_root=hash_tree_root(payload.withdrawals), ) - execution_branch = compute_merkle_proof_for_block_body(block.message.body, EXECUTION_PAYLOAD_INDEX) + execution_branch = compute_merkle_proof(block.message.body, EXECUTION_PAYLOAD_INDEX) else: # Note that during fork transitions, `finalized_header` may still point to earlier forks. # While Bellatrix blocks also contain an `ExecutionPayload` (minus `withdrawals_root`), diff --git a/specs/deneb/light-client/full-node.md b/specs/deneb/light-client/full-node.md index 281348167a..18b97ae43a 100644 --- a/specs/deneb/light-client/full-node.md +++ b/specs/deneb/light-client/full-node.md @@ -52,7 +52,7 @@ def block_to_light_client_header(block: SignedBeaconBlock) -> LightClientHeader: execution_header.blob_gas_used = payload.blob_gas_used execution_header.excess_blob_gas = payload.excess_blob_gas - execution_branch = compute_merkle_proof_for_block_body(block.message.body, EXECUTION_PAYLOAD_INDEX) + execution_branch = compute_merkle_proof(block.message.body, EXECUTION_PAYLOAD_INDEX) else: # Note that during fork transitions, `finalized_header` may still point to earlier forks. # While Bellatrix blocks also contain an `ExecutionPayload` (minus `withdrawals_root`), diff --git a/specs/deneb/validator.md b/specs/deneb/validator.md index 6b21ea30fd..e5141746a6 100644 --- a/specs/deneb/validator.md +++ b/specs/deneb/validator.md @@ -163,7 +163,7 @@ def get_blob_sidecars(signed_block: SignedBeaconBlock, blob=blob, kzg_commitment=block.body.blob_kzg_commitments[index], kzg_proof=blob_kzg_proofs[index], - commitment_inclusion_proof=compute_commitment_inclusion_proof( + commitment_inclusion_proof=compute_merkle_proof( block.body, get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments', index), # type: ignore ), diff --git a/tests/core/pyspec/eth2spec/test/altair/light_client/test_single_merkle_proof.py b/tests/core/pyspec/eth2spec/test/altair/light_client/test_single_merkle_proof.py index 7e9c6b7e2f..104d4774cf 100644 --- a/tests/core/pyspec/eth2spec/test/altair/light_client/test_single_merkle_proof.py +++ b/tests/core/pyspec/eth2spec/test/altair/light_client/test_single_merkle_proof.py @@ -10,8 +10,7 @@ @spec_state_test def test_current_sync_committee_merkle_proof(spec, state): yield "object", state - current_sync_committee_branch = spec.compute_merkle_proof_for_state( - state, spec.CURRENT_SYNC_COMMITTEE_INDEX) + current_sync_committee_branch = spec.compute_merkle_proof(state, spec.CURRENT_SYNC_COMMITTEE_INDEX) yield "proof", { "leaf": "0x" + state.current_sync_committee.hash_tree_root().hex(), "leaf_index": spec.CURRENT_SYNC_COMMITTEE_INDEX, @@ -31,8 +30,7 @@ def test_current_sync_committee_merkle_proof(spec, state): @spec_state_test def test_next_sync_committee_merkle_proof(spec, state): yield "object", state - next_sync_committee_branch = spec.compute_merkle_proof_for_state( - state, spec.NEXT_SYNC_COMMITTEE_INDEX) + next_sync_committee_branch = spec.compute_merkle_proof(state, spec.NEXT_SYNC_COMMITTEE_INDEX) yield "proof", { "leaf": "0x" + state.next_sync_committee.hash_tree_root().hex(), "leaf_index": spec.NEXT_SYNC_COMMITTEE_INDEX, @@ -52,8 +50,7 @@ def test_next_sync_committee_merkle_proof(spec, state): @spec_state_test def test_finality_root_merkle_proof(spec, state): yield "object", state - finality_branch = spec.compute_merkle_proof_for_state( - state, spec.FINALIZED_ROOT_INDEX) + finality_branch = spec.compute_merkle_proof(state, spec.FINALIZED_ROOT_INDEX) yield "proof", { "leaf": "0x" + state.finalized_checkpoint.root.hex(), "leaf_index": spec.FINALIZED_ROOT_INDEX, diff --git a/tests/core/pyspec/eth2spec/test/capella/light_client/test_single_merkle_proof.py b/tests/core/pyspec/eth2spec/test/capella/light_client/test_single_merkle_proof.py index 8d3bf8e3cb..8401a94442 100644 --- a/tests/core/pyspec/eth2spec/test/capella/light_client/test_single_merkle_proof.py +++ b/tests/core/pyspec/eth2spec/test/capella/light_client/test_single_merkle_proof.py @@ -15,8 +15,7 @@ def test_execution_merkle_proof(spec, state): block = state_transition_with_full_block(spec, state, True, False) yield "object", block.message.body - execution_branch = spec.compute_merkle_proof_for_block_body( - block.message.body, spec.EXECUTION_PAYLOAD_INDEX) + execution_branch = spec.compute_merkle_proof(block.message.body, spec.EXECUTION_PAYLOAD_INDEX) yield "proof", { "leaf": "0x" + block.message.body.execution_payload.hash_tree_root().hex(), "leaf_index": spec.EXECUTION_PAYLOAD_INDEX, diff --git a/tests/core/pyspec/eth2spec/test/helpers/light_client.py b/tests/core/pyspec/eth2spec/test/helpers/light_client.py index ceca145e94..1878832c37 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/light_client.py +++ b/tests/core/pyspec/eth2spec/test/helpers/light_client.py @@ -56,13 +56,11 @@ def create_update(spec, if with_next: update.next_sync_committee = attested_state.next_sync_committee - update.next_sync_committee_branch = spec.compute_merkle_proof_for_state( - attested_state, spec.NEXT_SYNC_COMMITTEE_INDEX) + update.next_sync_committee_branch = spec.compute_merkle_proof(attested_state, spec.NEXT_SYNC_COMMITTEE_INDEX) if with_finality: update.finalized_header = spec.block_to_light_client_header(finalized_block) - update.finality_branch = spec.compute_merkle_proof_for_state( - attested_state, spec.FINALIZED_ROOT_INDEX) + update.finality_branch = spec.compute_merkle_proof(attested_state, spec.FINALIZED_ROOT_INDEX) update.sync_aggregate, update.signature_slot = get_sync_aggregate( spec, attested_state, num_participants) From c680212183e67ff5895e6532861f85b4b4a9d557 Mon Sep 17 00:00:00 2001 From: dapplion <35266934+dapplion@users.noreply.github.com> Date: Mon, 30 Oct 2023 10:33:49 +0200 Subject: [PATCH 19/30] drop is_valid_merkle_path --- specs/deneb/beacon-chain.md | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/specs/deneb/beacon-chain.md b/specs/deneb/beacon-chain.md index fde75b0ed3..b98ac12592 100644 --- a/specs/deneb/beacon-chain.md +++ b/specs/deneb/beacon-chain.md @@ -25,7 +25,6 @@ - [Helper functions](#helper-functions) - [Misc](#misc) - [`kzg_commitment_to_versioned_hash`](#kzg_commitment_to_versioned_hash) - - [`is_valid_merkle_path`](#is_valid_merkle_path) - [Beacon state accessors](#beacon-state-accessors) - [Modified `get_attestation_participation_flag_indices`](#modified-get_attestation_participation_flag_indices) - [New `get_validator_activation_churn_limit`](#new-get_validator_activation_churn_limit) @@ -188,19 +187,6 @@ def kzg_commitment_to_versioned_hash(kzg_commitment: KZGCommitment) -> Versioned return VERSIONED_HASH_VERSION_KZG + hash(kzg_commitment)[1:] ``` -#### `is_valid_merkle_path` - -```python -def is_valid_merkle_path(leaf: Bytes32, branch: Sequence[Bytes32], gindex: int, root: Root) -> bool: - value = leaf - for i in range(len(branch)): - if (gindex >> i) & 1 == 0: - value = hash(branch[i] + value) - else: - value = hash(value + branch[i]) - return value == root -``` - ### Beacon state accessors #### Modified `get_attestation_participation_flag_indices` From 126e807d193229f56f4e21543dfc14ba3cbf00ea Mon Sep 17 00:00:00 2001 From: Lion - dapplion <35266934+dapplion@users.noreply.github.com> Date: Mon, 30 Oct 2023 10:34:51 +0200 Subject: [PATCH 20/30] Update specs/deneb/p2p-interface.md Co-authored-by: Mikhail Kalinin --- specs/deneb/p2p-interface.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/deneb/p2p-interface.md b/specs/deneb/p2p-interface.md index 5d640ddf54..65334c740b 100644 --- a/specs/deneb/p2p-interface.md +++ b/specs/deneb/p2p-interface.md @@ -82,7 +82,7 @@ class BlobSidecar(Container): kzg_commitment: KZGCommitment kzg_proof: KZGProof # Allows for quick verification of kzg_commitment signed_block_header: SignedBeaconBlockHeader - commitment_inclusion_proof: List[Bytes32, KZG_COMMITMENT_INCLUSION_PROOF_DEPTH] + commitment_inclusion_proof: Vector[Bytes32, KZG_COMMITMENT_INCLUSION_PROOF_DEPTH] ``` #### `BlobIdentifier` From b803f1ceeb04b5a4e6d0589eb905f86b18f40563 Mon Sep 17 00:00:00 2001 From: Lion - dapplion <35266934+dapplion@users.noreply.github.com> Date: Mon, 30 Oct 2023 10:38:48 +0200 Subject: [PATCH 21/30] Update specs/deneb/p2p-interface.md Co-authored-by: Pop Chunhapanya --- specs/deneb/p2p-interface.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/deneb/p2p-interface.md b/specs/deneb/p2p-interface.md index 65334c740b..349b4ec8c5 100644 --- a/specs/deneb/p2p-interface.md +++ b/specs/deneb/p2p-interface.md @@ -48,7 +48,7 @@ The specification of these changes continues in the same format as the network s | Name | Value | Description | |------------------------------------------|-----------------------------------|---------------------------------------------------------------------| -| `BLOB_KZG_COMMITMENTS_GINDEX` | `get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments')` (= 27) | `blob_kzg_commitments` field gindex on `BeaconBlockBody` container | +| `BLOB_KZG_COMMITMENTS_GINDEX` | `get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments')` (= 27) | `blob_kzg_commitments` field generalized index on `BeaconBlockBody` container | ### Preset From d323f0568fb286d732a3deb66758bf75151a7444 Mon Sep 17 00:00:00 2001 From: dapplion <35266934+dapplion@users.noreply.github.com> Date: Mon, 30 Oct 2023 10:37:17 +0200 Subject: [PATCH 22/30] drop sidecar alias --- specs/deneb/p2p-interface.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/specs/deneb/p2p-interface.md b/specs/deneb/p2p-interface.md index 349b4ec8c5..b712c1679b 100644 --- a/specs/deneb/p2p-interface.md +++ b/specs/deneb/p2p-interface.md @@ -156,10 +156,10 @@ New validation: This topic is used to propagate signed blob sidecars, where each blob index maps to some `subnet_id`. -The following validations MUST pass before forwarding the `blob_sidecar` on the network, assuming the alias `sidecar = blob_sidecar` and `block_header = blob_sidecar.signed_block_header.message`: +The following validations MUST pass before forwarding the `blob_sidecar` on the network, assuming the alias `block_header = blob_sidecar.signed_block_header.message`: -- _[REJECT]_ The sidecar's index is consistent with `MAX_BLOBS_PER_BLOCK` -- i.e. `sidecar.index < MAX_BLOBS_PER_BLOCK`. -- _[REJECT]_ The sidecar is for the correct subnet -- i.e. `compute_subnet_for_blob_sidecar(sidecar.index) == subnet_id`. +- _[REJECT]_ The sidecar's index is consistent with `MAX_BLOBS_PER_BLOCK` -- i.e. `blob_sidecar.index < MAX_BLOBS_PER_BLOCK`. +- _[REJECT]_ The sidecar is for the correct subnet -- i.e. `compute_subnet_for_blob_sidecar(blob_sidecar.index) == subnet_id`. - _[IGNORE]_ The sidecar is not from a future slot (with a `MAXIMUM_GOSSIP_CLOCK_DISPARITY` allowance) -- i.e. validate that `block_header.slot <= current_slot` (a client MAY queue future sidecars for processing at the appropriate slot). - _[IGNORE]_ The sidecar is from a slot greater than the latest finalized slot -- i.e. validate that `block_header.slot > compute_start_slot_at_epoch(state.finalized_checkpoint.epoch)` - _[IGNORE]_ The sidecar's block's parent (defined by `block_header.parent_root`) has been seen (via both gossip and non-gossip sources) (a client MAY queue sidecars for processing once the parent block is retrieved). @@ -167,7 +167,7 @@ The following validations MUST pass before forwarding the `blob_sidecar` on the - _[REJECT]_ The sidecar is from a higher slot than the sidecar's block's parent (defined by `block_header.parent_root`). - _[REJECT]_ The proposer signature of `blob_sidecar.signed_block_header`, is valid with respect to the `block_header.proposer_index` pubkey. - _[REJECT]_ The sidecar's inclusion proof is valid as verified by `verify_blob_sidecar_inclusion_proof`. -- _[IGNORE]_ The sidecar is the first sidecar for the tuple (block_header.slot, block_header.proposer_index, sidecar.index) with valid header signature and sidecar inclusion proof +- _[IGNORE]_ The sidecar is the first sidecar for the tuple (block_header.slot, block_header.proposer_index, blob_sidecar.index) with valid header signature and sidecar inclusion proof - _[REJECT]_ The sidecar is proposed by the expected `proposer_index` for the block's slot in the context of the current shuffling (defined by `block_header.parent_root`/`block_header.slot`). If the `proposer_index` cannot immediately be verified against the expected shuffling, the sidecar MAY be queued for later processing while proposers for the block's branch are calculated -- in such a case _do not_ `REJECT`, instead `IGNORE` this message. From a12441486c5a0ab1376b64b1d6cb8308bee63823 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Mon, 30 Oct 2023 17:42:32 +0800 Subject: [PATCH 23/30] Enhance `blob_sidecar_inclusion_proof` tests --- .../unittests/validator/test_validator.py | 62 +++++++++++-------- 1 file changed, 36 insertions(+), 26 deletions(-) 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 6cd3011fcb..77ae900671 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 @@ -1,5 +1,5 @@ +import random from eth2spec.test.context import ( - always_bls, spec_state_test, with_deneb_and_later, ) @@ -15,21 +15,30 @@ ) -@with_deneb_and_later -@spec_state_test -def test_blob_sidecar_inclusion_proof_correct(spec, state): - """ - Test `verify_blob_sidecar_inclusion_proof` - """ - blob_count = 4 +def _get_sample_sidecars(spec, state, rng): block = build_empty_block_for_next_slot(spec, state) - opaque_tx, blobs, blob_kzg_commitments, proofs = get_sample_opaque_tx(spec, blob_count=blob_count) - block.body.blob_kzg_commitments = blob_kzg_commitments - block.body.execution_payload.transactions = [opaque_tx] + + # 2 txs, each has 2 blobs + blob_count = 2 + opaque_tx_1, blobs_1, blob_kzg_commitments_1, proofs_1 = get_sample_opaque_tx(spec, blob_count=blob_count, rng=rng) + opaque_tx_2, blobs_2, blob_kzg_commitments_2, proofs_2 = get_sample_opaque_tx(spec, blob_count=blob_count, rng=rng) + assert opaque_tx_1 != opaque_tx_2 + + block.body.blob_kzg_commitments = blob_kzg_commitments_1 + blob_kzg_commitments_2 + block.body.execution_payload.transactions = [opaque_tx_1, opaque_tx_2] block.body.execution_payload.block_hash = compute_el_block_hash(spec, block.body.execution_payload) + blobs = blobs_1 + blobs_2 + proofs = proofs_1 + proofs_2 signed_block = sign_block(spec, state, block, proposer_index=0) - blob_sidecars = spec.get_blob_sidecars(signed_block, blobs, proofs) + return spec.get_blob_sidecars(signed_block, blobs, proofs) + + +@with_deneb_and_later +@spec_state_test +def test_blob_sidecar_inclusion_proof_correct(spec, state): + rng = random.Random(1234) + blob_sidecars = _get_sample_sidecars(spec, state, rng) for blob_sidecar in blob_sidecars: assert spec.verify_blob_sidecar_inclusion_proof(blob_sidecar) @@ -37,22 +46,23 @@ def test_blob_sidecar_inclusion_proof_correct(spec, state): @with_deneb_and_later @spec_state_test -@always_bls -def test_blob_sidecar_inclusion_proof_incorrect(spec, state): - """ - Test `get_blob_sidecar_signature` - """ - blob_count = 4 - block = build_empty_block_for_next_slot(spec, state) - opaque_tx, blobs, blob_kzg_commitments, proofs = get_sample_opaque_tx(spec, blob_count=blob_count) - block.body.blob_kzg_commitments = blob_kzg_commitments - block.body.execution_payload.transactions = [opaque_tx] - block.body.execution_payload.block_hash = compute_el_block_hash(spec, block.body.execution_payload) - - signed_block = sign_block(spec, state, block, proposer_index=0) - blob_sidecars = spec.get_blob_sidecars(signed_block, blobs, proofs) +def test_blob_sidecar_inclusion_proof_incorrect_wrong_body(spec, state): + rng = random.Random(1234) + blob_sidecars = _get_sample_sidecars(spec, state, rng) for blob_sidecar in blob_sidecars: block = blob_sidecar.signed_block_header.message block.body_root = spec.hash(block.body_root) # mutate body root to break proof assert not spec.verify_blob_sidecar_inclusion_proof(blob_sidecar) + + +@with_deneb_and_later +@spec_state_test +def test_blob_sidecar_inclusion_proof_incorrect_wrong_proof(spec, state): + rng = random.Random(1234) + blob_sidecars = _get_sample_sidecars(spec, state, rng) + + for blob_sidecar in blob_sidecars: + # wrong proof + blob_sidecar.commitment_inclusion_proof = spec.compute_merkle_proof(spec.BeaconBlockBody(), 0) + assert not spec.verify_blob_sidecar_inclusion_proof(blob_sidecar) From 51343f54fe73eaffa550d78414faed6e63df5b62 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Mon, 30 Oct 2023 18:24:19 +0800 Subject: [PATCH 24/30] Fix typing and delete the `signed_sidecar` --- pysetup/spec_builders/altair.py | 2 +- specs/deneb/validator.md | 8 ++++---- ssz/merkle-proofs.md | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pysetup/spec_builders/altair.py b/pysetup/spec_builders/altair.py index ff667b19ce..abd9b56c40 100644 --- a/pysetup/spec_builders/altair.py +++ b/pysetup/spec_builders/altair.py @@ -27,7 +27,7 @@ def preparations(cls): @classmethod def sundry_functions(cls) -> str: return ''' -def get_generalized_index(ssz_class: Any, *path: Sequence[PyUnion[int, SSZVariableName]]) -> GeneralizedIndex: +def get_generalized_index(ssz_class: Any, *path: PyUnion[int, SSZVariableName]) -> GeneralizedIndex: ssz_path = Path(ssz_class) for item in path: ssz_path = ssz_path / item diff --git a/specs/deneb/validator.md b/specs/deneb/validator.md index e5141746a6..cb87e13225 100644 --- a/specs/deneb/validator.md +++ b/specs/deneb/validator.md @@ -163,18 +163,18 @@ def get_blob_sidecars(signed_block: SignedBeaconBlock, blob=blob, kzg_commitment=block.body.blob_kzg_commitments[index], kzg_proof=blob_kzg_proofs[index], + signed_block_header=signed_block_header, commitment_inclusion_proof=compute_merkle_proof( block.body, - get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments', index), # type: ignore + get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments', index), ), - signed_block_header=signed_block_header, ) for index, blob in enumerate(blobs) ] ``` -The `subnet_id` for the `signed_sidecar` is calculated with: -- Let `blob_index = signed_sidecar.message.index`. +The `subnet_id` for the `blob_sidecar` is calculated with: +- Let `blob_index = blob_sidecar.index`. - Let `subnet_id = compute_subnet_for_blob_sidecar(blob_index)`. ```python diff --git a/ssz/merkle-proofs.md b/ssz/merkle-proofs.md index 6772026fe1..919ff07f59 100644 --- a/ssz/merkle-proofs.md +++ b/ssz/merkle-proofs.md @@ -167,7 +167,7 @@ def get_item_position(typ: SSZType, index_or_variable_name: Union[int, SSZVariab ``` ```python -def get_generalized_index(typ: SSZType, path: Sequence[Union[int, SSZVariableName]]) -> GeneralizedIndex: +def get_generalized_index(typ: SSZType, *path: PyUnion[int, SSZVariableName]) -> GeneralizedIndex: """ Converts a path (eg. `[7, "foo", 3]` for `x[7].foo[3]`, `[12, "bar", "__len__"]` for `len(x[12].bar)`) into the generalized index representing its position in the Merkle tree. From 1bac25a6230c9c509ae1058d35c7dadb2e7a6b01 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Tue, 31 Oct 2023 00:53:53 +0800 Subject: [PATCH 25/30] Add Merkle proof test --- .../test/deneb/merkle_proof/__init__.py | 0 .../merkle_proof/test_single_merkle_proof.py | 46 +++++++++++++++++++ tests/formats/merkle_proof/README.md | 4 ++ tests/generators/merkle_proof/README.md | 5 ++ tests/generators/merkle_proof/__init__.py | 0 tests/generators/merkle_proof/main.py | 14 ++++++ .../generators/merkle_proof/requirements.txt | 2 + 7 files changed, 71 insertions(+) create mode 100644 tests/core/pyspec/eth2spec/test/deneb/merkle_proof/__init__.py create mode 100644 tests/core/pyspec/eth2spec/test/deneb/merkle_proof/test_single_merkle_proof.py create mode 100644 tests/formats/merkle_proof/README.md create mode 100644 tests/generators/merkle_proof/README.md create mode 100644 tests/generators/merkle_proof/__init__.py create mode 100644 tests/generators/merkle_proof/main.py create mode 100644 tests/generators/merkle_proof/requirements.txt diff --git a/tests/core/pyspec/eth2spec/test/deneb/merkle_proof/__init__.py b/tests/core/pyspec/eth2spec/test/deneb/merkle_proof/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/core/pyspec/eth2spec/test/deneb/merkle_proof/test_single_merkle_proof.py b/tests/core/pyspec/eth2spec/test/deneb/merkle_proof/test_single_merkle_proof.py new file mode 100644 index 0000000000..bcc25a89d4 --- /dev/null +++ b/tests/core/pyspec/eth2spec/test/deneb/merkle_proof/test_single_merkle_proof.py @@ -0,0 +1,46 @@ +from eth2spec.test.context import ( + spec_state_test, + with_deneb_and_later, + with_test_suite_name, +) +from eth2spec.test.helpers.block import ( + build_empty_block_for_next_slot, + sign_block +) +from eth2spec.test.helpers.execution_payload import ( + compute_el_block_hash, +) +from eth2spec.test.helpers.sharding import ( + get_sample_opaque_tx, +) + + +@with_test_suite_name("BeaconBlockBody") +@with_deneb_and_later +@spec_state_test +def test_blob_kzg_commitment_merkle_proof(spec, state): + opaque_tx, blobs, blob_kzg_commitments, proofs = get_sample_opaque_tx(spec, blob_count=1) + block = build_empty_block_for_next_slot(spec, state) + block.body.blob_kzg_commitments = blob_kzg_commitments + block.body.execution_payload.transactions = [opaque_tx] + block.body.execution_payload.block_hash = compute_el_block_hash(spec, block.body.execution_payload) + signed_block = sign_block(spec, state, block, proposer_index=0) + blob_sidecars = spec.get_blob_sidecars(signed_block, blobs, proofs) + blob_index = 0 + blob_sidecar = blob_sidecars[blob_index] + + yield "object", block.body + commitment_inclusion_proof = blob_sidecar.commitment_inclusion_proof + gindex = spec.get_generalized_index(spec.BeaconBlockBody, 'blob_kzg_commitments', blob_index) + yield "proof", { + "leaf": "0x" + blob_sidecar.kzg_commitment.hash_tree_root().hex(), + "leaf_index": gindex, + "branch": ['0x' + root.hex() for root in commitment_inclusion_proof] + } + assert spec.is_valid_merkle_branch( + leaf=blob_sidecar.kzg_commitment.hash_tree_root(), + branch=blob_sidecar.commitment_inclusion_proof, + depth=spec.floorlog2(gindex), + index=spec.get_subtree_index(gindex), + root=blob_sidecar.signed_block_header.message.body_root, + ) diff --git a/tests/formats/merkle_proof/README.md b/tests/formats/merkle_proof/README.md new file mode 100644 index 0000000000..77822daabe --- /dev/null +++ b/tests/formats/merkle_proof/README.md @@ -0,0 +1,4 @@ +# Merkle proof tests + +Handlers: +- `single_merkle_proof`: see [Single leaf merkle proof test format](../light_client/single_merkle_proof.md) diff --git a/tests/generators/merkle_proof/README.md b/tests/generators/merkle_proof/README.md new file mode 100644 index 0000000000..fb4d05fda8 --- /dev/null +++ b/tests/generators/merkle_proof/README.md @@ -0,0 +1,5 @@ +# Merkle proof tests + +The purpose of this test-generator is to provide test-vectors for validating the correct implementation of the Merkle proof verification. + +Test-format documentation can be found [here](../../formats/merkle_proof/README.md). diff --git a/tests/generators/merkle_proof/__init__.py b/tests/generators/merkle_proof/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/generators/merkle_proof/main.py b/tests/generators/merkle_proof/main.py new file mode 100644 index 0000000000..59a0e8ce65 --- /dev/null +++ b/tests/generators/merkle_proof/main.py @@ -0,0 +1,14 @@ +from eth2spec.test.helpers.constants import DENEB +from eth2spec.gen_helpers.gen_from_tests.gen import run_state_test_generators + + +if __name__ == "__main__": + deneb_mods = {key: 'eth2spec.test.deneb.merkle_proof.test_' + key for key in [ + 'single_merkle_proof', + ]} + + all_mods = { + DENEB: deneb_mods, + } + + run_state_test_generators(runner_name="merkle_proof", all_mods=all_mods) diff --git a/tests/generators/merkle_proof/requirements.txt b/tests/generators/merkle_proof/requirements.txt new file mode 100644 index 0000000000..1822486863 --- /dev/null +++ b/tests/generators/merkle_proof/requirements.txt @@ -0,0 +1,2 @@ +pytest>=4.4 +../../../[generator] From 19883ec5922e3cd3b906f08c5293b30e311dbea0 Mon Sep 17 00:00:00 2001 From: dapplion <35266934+dapplion@users.noreply.github.com> Date: Tue, 31 Oct 2023 11:39:57 +0200 Subject: [PATCH 26/30] Add verify_blob_kzg_proof condition --- specs/deneb/p2p-interface.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/specs/deneb/p2p-interface.md b/specs/deneb/p2p-interface.md index b712c1679b..586c909e4e 100644 --- a/specs/deneb/p2p-interface.md +++ b/specs/deneb/p2p-interface.md @@ -167,7 +167,8 @@ The following validations MUST pass before forwarding the `blob_sidecar` on the - _[REJECT]_ The sidecar is from a higher slot than the sidecar's block's parent (defined by `block_header.parent_root`). - _[REJECT]_ The proposer signature of `blob_sidecar.signed_block_header`, is valid with respect to the `block_header.proposer_index` pubkey. - _[REJECT]_ The sidecar's inclusion proof is valid as verified by `verify_blob_sidecar_inclusion_proof`. -- _[IGNORE]_ The sidecar is the first sidecar for the tuple (block_header.slot, block_header.proposer_index, blob_sidecar.index) with valid header signature and sidecar inclusion proof +- _[REJECT]_ The sidecar's blob is valid as verified by `verify_blob_kzg_proof(blob_sidecar.blob, blob_sidecar.kzg_commitment, blob_sidecar.kzg_proof)`. +- _[IGNORE]_ The sidecar is the first sidecar for the tuple (block_header.slot, block_header.proposer_index, blob_sidecar.index) with valid header signature, sidecar inclusion proof, and kzg proof. - _[REJECT]_ The sidecar is proposed by the expected `proposer_index` for the block's slot in the context of the current shuffling (defined by `block_header.parent_root`/`block_header.slot`). If the `proposer_index` cannot immediately be verified against the expected shuffling, the sidecar MAY be queued for later processing while proposers for the block's branch are calculated -- in such a case _do not_ `REJECT`, instead `IGNORE` this message. From 4a609cec574eca635a9e0980fe448089f8690ce1 Mon Sep 17 00:00:00 2001 From: dapplion <35266934+dapplion@users.noreply.github.com> Date: Wed, 1 Nov 2023 15:57:41 +0200 Subject: [PATCH 27/30] rename to kzg_commitment_inclusion_proof --- specs/deneb/p2p-interface.md | 4 ++-- specs/deneb/validator.md | 2 +- .../test/deneb/merkle_proof/test_single_merkle_proof.py | 6 +++--- .../test/deneb/unittests/validator/test_validator.py | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/specs/deneb/p2p-interface.md b/specs/deneb/p2p-interface.md index 2cfdcde748..748a3f0d8d 100644 --- a/specs/deneb/p2p-interface.md +++ b/specs/deneb/p2p-interface.md @@ -82,7 +82,7 @@ class BlobSidecar(Container): kzg_commitment: KZGCommitment kzg_proof: KZGProof # Allows for quick verification of kzg_commitment signed_block_header: SignedBeaconBlockHeader - commitment_inclusion_proof: Vector[Bytes32, KZG_COMMITMENT_INCLUSION_PROOF_DEPTH] + kzg_commitment_inclusion_proof: Vector[Bytes32, KZG_COMMITMENT_INCLUSION_PROOF_DEPTH] ``` #### `BlobIdentifier` @@ -104,7 +104,7 @@ def verify_blob_sidecar_inclusion_proof(blob_sidecar: BlobSidecar) -> bool: gindex = get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments', blob_sidecar.index) return is_valid_merkle_branch( leaf=blob_sidecar.kzg_commitment.hash_tree_root(), - branch=blob_sidecar.commitment_inclusion_proof, + branch=blob_sidecar.kzg_commitment_inclusion_proof, depth=floorlog2(gindex), index=get_subtree_index(gindex), root=blob_sidecar.signed_block_header.message.body_root, diff --git a/specs/deneb/validator.md b/specs/deneb/validator.md index 3dc2e90f0d..3e2c91f817 100644 --- a/specs/deneb/validator.md +++ b/specs/deneb/validator.md @@ -162,7 +162,7 @@ def get_blob_sidecars(signed_block: SignedBeaconBlock, kzg_commitment=block.body.blob_kzg_commitments[index], kzg_proof=blob_kzg_proofs[index], signed_block_header=signed_block_header, - commitment_inclusion_proof=compute_merkle_proof( + kzg_commitment_inclusion_proof=compute_merkle_proof( block.body, get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments', index), ), diff --git a/tests/core/pyspec/eth2spec/test/deneb/merkle_proof/test_single_merkle_proof.py b/tests/core/pyspec/eth2spec/test/deneb/merkle_proof/test_single_merkle_proof.py index bcc25a89d4..75bcacc7fc 100644 --- a/tests/core/pyspec/eth2spec/test/deneb/merkle_proof/test_single_merkle_proof.py +++ b/tests/core/pyspec/eth2spec/test/deneb/merkle_proof/test_single_merkle_proof.py @@ -30,16 +30,16 @@ def test_blob_kzg_commitment_merkle_proof(spec, state): blob_sidecar = blob_sidecars[blob_index] yield "object", block.body - commitment_inclusion_proof = blob_sidecar.commitment_inclusion_proof + kzg_commitment_inclusion_proof = blob_sidecar.kzg_commitment_inclusion_proof gindex = spec.get_generalized_index(spec.BeaconBlockBody, 'blob_kzg_commitments', blob_index) yield "proof", { "leaf": "0x" + blob_sidecar.kzg_commitment.hash_tree_root().hex(), "leaf_index": gindex, - "branch": ['0x' + root.hex() for root in commitment_inclusion_proof] + "branch": ['0x' + root.hex() for root in kzg_commitment_inclusion_proof] } assert spec.is_valid_merkle_branch( leaf=blob_sidecar.kzg_commitment.hash_tree_root(), - branch=blob_sidecar.commitment_inclusion_proof, + branch=blob_sidecar.kzg_commitment_inclusion_proof, depth=spec.floorlog2(gindex), index=spec.get_subtree_index(gindex), root=blob_sidecar.signed_block_header.message.body_root, 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 77ae900671..7ed88cba45 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 @@ -64,5 +64,5 @@ def test_blob_sidecar_inclusion_proof_incorrect_wrong_proof(spec, state): for blob_sidecar in blob_sidecars: # wrong proof - blob_sidecar.commitment_inclusion_proof = spec.compute_merkle_proof(spec.BeaconBlockBody(), 0) + blob_sidecar.kzg_commitment_inclusion_proof = spec.compute_merkle_proof(spec.BeaconBlockBody(), 0) assert not spec.verify_blob_sidecar_inclusion_proof(blob_sidecar) From 71106f1d6486743cac6ecb415c0f03012398b337 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Thu, 2 Nov 2023 21:38:01 +0700 Subject: [PATCH 28/30] Remove `BLOB_KZG_COMMITMENTS_GINDEX` --- presets/mainnet/deneb.yaml | 2 +- presets/minimal/deneb.yaml | 2 +- pysetup/spec_builders/deneb.py | 6 ------ specs/deneb/p2p-interface.md | 11 +++-------- .../test/deneb/unittests/test_config_invariants.py | 3 +++ 5 files changed, 8 insertions(+), 16 deletions(-) diff --git a/presets/mainnet/deneb.yaml b/presets/mainnet/deneb.yaml index 6d2fb4abde..0f56b8bdfa 100644 --- a/presets/mainnet/deneb.yaml +++ b/presets/mainnet/deneb.yaml @@ -8,5 +8,5 @@ FIELD_ELEMENTS_PER_BLOB: 4096 MAX_BLOB_COMMITMENTS_PER_BLOCK: 4096 # `uint64(6)` MAX_BLOBS_PER_BLOCK: 6 -# `floorlog2(BLOB_KZG_COMMITMENTS_GINDEX) + 1 + ceillog2(MAX_BLOB_COMMITMENTS_PER_BLOCK)` = 4 + 1 + 12 = 17 +# `floorlog2(get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments')) + 1 + ceillog2(MAX_BLOB_COMMITMENTS_PER_BLOCK)` = 4 + 1 + 12 = 17 KZG_COMMITMENT_INCLUSION_PROOF_DEPTH: 17 diff --git a/presets/minimal/deneb.yaml b/presets/minimal/deneb.yaml index 340e8d8d5f..bc4fe4369a 100644 --- a/presets/minimal/deneb.yaml +++ b/presets/minimal/deneb.yaml @@ -8,5 +8,5 @@ FIELD_ELEMENTS_PER_BLOB: 4096 MAX_BLOB_COMMITMENTS_PER_BLOCK: 16 # `uint64(6)` MAX_BLOBS_PER_BLOCK: 6 -# [customized] `floorlog2(BLOB_KZG_COMMITMENTS_GINDEX) + 1 + ceillog2(MAX_BLOB_COMMITMENTS_PER_BLOCK)` = 4 + 1 + 4 = 9 +# [customized] `floorlog2(get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments')) + 1 + ceillog2(MAX_BLOB_COMMITMENTS_PER_BLOCK)` = 4 + 1 + 4 = 9 KZG_COMMITMENT_INCLUSION_PROOF_DEPTH: 9 diff --git a/pysetup/spec_builders/deneb.py b/pysetup/spec_builders/deneb.py index 486018f868..e1ad051849 100644 --- a/pysetup/spec_builders/deneb.py +++ b/pysetup/spec_builders/deneb.py @@ -73,12 +73,6 @@ def hardcoded_custom_type_dep_constants(cls, spec_object) -> Dict[str, str]: 'MAX_BLOB_COMMITMENTS_PER_BLOCK': spec_object.preset_vars['MAX_BLOB_COMMITMENTS_PER_BLOCK'].value, } - @classmethod - def hardcoded_ssz_dep_constants(cls) -> Dict[str, str]: - return { - 'BLOB_KZG_COMMITMENTS_GINDEX': 'GeneralizedIndex(27)', - } - @classmethod def hardcoded_func_dep_presets(cls, spec_object) -> Dict[str, str]: return { diff --git a/specs/deneb/p2p-interface.md b/specs/deneb/p2p-interface.md index 748a3f0d8d..f8124c92d3 100644 --- a/specs/deneb/p2p-interface.md +++ b/specs/deneb/p2p-interface.md @@ -46,17 +46,13 @@ The specification of these changes continues in the same format as the network s *[New in Deneb:EIP4844]* -| Name | Value | Description | -|------------------------------------------|-----------------------------------|---------------------------------------------------------------------| -| `BLOB_KZG_COMMITMENTS_GINDEX` | `get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments')` (= 27) | `blob_kzg_commitments` field generalized index on `BeaconBlockBody` container | - ### Preset *[New in Deneb:EIP4844]* | Name | Value | Description | |------------------------------------------|-----------------------------------|---------------------------------------------------------------------| -| `KZG_COMMITMENT_INCLUSION_PROOF_DEPTH` | `uint64(floorlog2(BLOB_KZG_COMMITMENTS_GINDEX) + 1 + ceillog2(MAX_BLOB_COMMITMENTS_PER_BLOCK))` (= 17) | Merkle proof depth for `blob_kzg_commitments` list item | +| `KZG_COMMITMENT_INCLUSION_PROOF_DEPTH` | `uint64(floorlog2(get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments')) + 1 + ceillog2(MAX_BLOB_COMMITMENTS_PER_BLOCK))` (= 17) | Merkle proof depth for `blob_kzg_commitments` list item | ### Configuration @@ -101,12 +97,11 @@ class BlobIdentifier(Container): ```python def verify_blob_sidecar_inclusion_proof(blob_sidecar: BlobSidecar) -> bool: - gindex = get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments', blob_sidecar.index) return is_valid_merkle_branch( leaf=blob_sidecar.kzg_commitment.hash_tree_root(), branch=blob_sidecar.kzg_commitment_inclusion_proof, - depth=floorlog2(gindex), - index=get_subtree_index(gindex), + depth=KZG_COMMITMENT_INCLUSION_PROOF_DEPTH, + index=get_subtree_index(get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments', blob_sidecar.index)), root=blob_sidecar.signed_block_header.message.body_root, ) ``` diff --git a/tests/core/pyspec/eth2spec/test/deneb/unittests/test_config_invariants.py b/tests/core/pyspec/eth2spec/test/deneb/unittests/test_config_invariants.py index 087bd63c60..f3fa956d0a 100644 --- a/tests/core/pyspec/eth2spec/test/deneb/unittests/test_config_invariants.py +++ b/tests/core/pyspec/eth2spec/test/deneb/unittests/test_config_invariants.py @@ -20,3 +20,6 @@ def test_networking(spec): assert spec.config.MAX_REQUEST_BLOB_SIDECARS == spec.config.MAX_REQUEST_BLOCKS_DENEB * spec.MAX_BLOBS_PER_BLOCK # Start with the same size, but `BLOB_SIDECAR_SUBNET_COUNT` could potentially increase later. assert spec.config.BLOB_SIDECAR_SUBNET_COUNT == spec.MAX_BLOBS_PER_BLOCK + for i in range(spec.MAX_BLOB_COMMITMENTS_PER_BLOCK): + gindex = spec.get_generalized_index(spec.BeaconBlockBody, 'blob_kzg_commitments', i) + assert spec.floorlog2(gindex) == spec.KZG_COMMITMENT_INCLUSION_PROOF_DEPTH From 3492c0a99a88d43fe3e4fb99f0fade0431822825 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Thu, 2 Nov 2023 22:10:06 +0700 Subject: [PATCH 29/30] minor refactoring --- specs/deneb/p2p-interface.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/specs/deneb/p2p-interface.md b/specs/deneb/p2p-interface.md index f8124c92d3..eedc2e661d 100644 --- a/specs/deneb/p2p-interface.md +++ b/specs/deneb/p2p-interface.md @@ -97,11 +97,12 @@ class BlobIdentifier(Container): ```python def verify_blob_sidecar_inclusion_proof(blob_sidecar: BlobSidecar) -> bool: + gindex = get_subtree_index(get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments', blob_sidecar.index)) return is_valid_merkle_branch( leaf=blob_sidecar.kzg_commitment.hash_tree_root(), branch=blob_sidecar.kzg_commitment_inclusion_proof, depth=KZG_COMMITMENT_INCLUSION_PROOF_DEPTH, - index=get_subtree_index(get_generalized_index(BeaconBlockBody, 'blob_kzg_commitments', blob_sidecar.index)), + index=gindex, root=blob_sidecar.signed_block_header.message.body_root, ) ``` From 7118c30b672eb5654926c580f903f8bcdfb989b1 Mon Sep 17 00:00:00 2001 From: danny Date: Thu, 2 Nov 2023 09:40:49 -0600 Subject: [PATCH 30/30] a few cleanups to sidecar gossip conditions --- specs/deneb/p2p-interface.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/specs/deneb/p2p-interface.md b/specs/deneb/p2p-interface.md index eedc2e661d..532246ecef 100644 --- a/specs/deneb/p2p-interface.md +++ b/specs/deneb/p2p-interface.md @@ -158,12 +158,12 @@ The following validations MUST pass before forwarding the `blob_sidecar` on the - _[REJECT]_ The sidecar is for the correct subnet -- i.e. `compute_subnet_for_blob_sidecar(blob_sidecar.index) == subnet_id`. - _[IGNORE]_ The sidecar is not from a future slot (with a `MAXIMUM_GOSSIP_CLOCK_DISPARITY` allowance) -- i.e. validate that `block_header.slot <= current_slot` (a client MAY queue future sidecars for processing at the appropriate slot). - _[IGNORE]_ The sidecar is from a slot greater than the latest finalized slot -- i.e. validate that `block_header.slot > compute_start_slot_at_epoch(state.finalized_checkpoint.epoch)` +- _[REJECT]_ The proposer signature of `blob_sidecar.signed_block_header`, is valid with respect to the `block_header.proposer_index` pubkey. - _[IGNORE]_ The sidecar's block's parent (defined by `block_header.parent_root`) has been seen (via both gossip and non-gossip sources) (a client MAY queue sidecars for processing once the parent block is retrieved). - _[REJECT]_ The sidecar's block's parent (defined by `block_header.parent_root`) passes validation. - _[REJECT]_ The sidecar is from a higher slot than the sidecar's block's parent (defined by `block_header.parent_root`). -- _[REJECT]_ The current finalized_checkpoint is an ancestor of the sidecar's block's parent -- i.e. `get_checkpoint_block(store, sidecar.block_parent_root, store.finalized_checkpoint.epoch) == store.finalized_checkpoint.root`. -- _[REJECT]_ The proposer signature of `blob_sidecar.signed_block_header`, is valid with respect to the `block_header.proposer_index` pubkey. -- _[REJECT]_ The sidecar's inclusion proof is valid as verified by `verify_blob_sidecar_inclusion_proof`. +- _[REJECT]_ The current finalized_checkpoint is an ancestor of the sidecar's block -- i.e. `get_checkpoint_block(store, block_header.parent_root, store.finalized_checkpoint.epoch) == store.finalized_checkpoint.root`. +- _[REJECT]_ The sidecar's inclusion proof is valid as verified by `verify_blob_sidecar_inclusion_proof(blob_sidecar)`. - _[REJECT]_ The sidecar's blob is valid as verified by `verify_blob_kzg_proof(blob_sidecar.blob, blob_sidecar.kzg_commitment, blob_sidecar.kzg_proof)`. - _[IGNORE]_ The sidecar is the first sidecar for the tuple (block_header.slot, block_header.proposer_index, blob_sidecar.index) with valid header signature, sidecar inclusion proof, and kzg proof. - _[REJECT]_ The sidecar is proposed by the expected `proposer_index` for the block's slot in the context of the current shuffling (defined by `block_header.parent_root`/`block_header.slot`).