From 0dd8db76cd7d21a2853f0aad5995d027daf8c0e3 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Thu, 30 Mar 2023 14:51:41 +0800 Subject: [PATCH] Make linter happy. Add `SUBNET_DURATION_IN_EPOCHS` definition. --- specs/phase0/validator.md | 27 ++++++++++--------- .../unittests/test_config_invariants.py | 2 +- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/specs/phase0/validator.md b/specs/phase0/validator.md index 2ed047f0f..4df4437d0 100644 --- a/specs/phase0/validator.md +++ b/specs/phase0/validator.md @@ -91,8 +91,10 @@ All terminology, constants, functions, and protocol mechanics defined in the [Ph | `TARGET_AGGREGATORS_PER_COMMITTEE` | `2**4` (= 16) | validators | | `EPOCHS_PER_SUBNET_SUBSCRIPTION` | `2**8` (= 256) | epochs | ~27 hours | | `ATTESTATION_SUBNET_COUNT` | `64` | The number of attestation subnets used in the gossipsub protocol. | -| `ATTESTATION_SUBNET_EXTRA_BITS` | 0 | The number of extra bits of a NodeId to use when mapping to a subscribed subnet | -| `SUBNETS_PER_NODE` | 2 | The number of long-lived subnets a beacon node should be subscribed to. | +| `ATTESTATION_SUBNET_EXTRA_BITS` | `0` | The number of extra bits of a NodeId to use when mapping to a subscribed subnet | +| `SUBNETS_PER_NODE` | `2` | The number of long-lived subnets a beacon node should be subscribed to. | +| `ATTESTATION_SUBNET_PREFIX_BITS` | `(ceillog2(ATTESTATION_SUBNET_COUNT) + ATTESTATION_SUBNET_EXTRA_BITS)` | | +| `SUBNET_DURATION_IN_EPOCHS` | `2` | | ## Containers @@ -611,20 +613,19 @@ Because Phase 0 does not have shards and thus does not have Shard Committees, th * Remain subscribed to `SUBNETS_PER_NODE` for `SUBNET_DURATION_IN_EPOCHS` epochs. * Maintain advertisement of the selected subnets in their node's ENR `attnets` entry by setting the selected `subnet_id` bits to `True` (e.g. `ENR["attnets"][subnet_id] = True`) for all persistent attestation subnets. -* Select these subnets based on their node-id as specified by the following - `compute_subnets(node_id,epoch)` function. +* Select these subnets based on their node-id as specified by the following `compute_subnets(node_id,epoch)` function. ```python -ATTESTATION_SUBNET_PREFIX_BITS = ceil(log2(ATTESTATION_SUBNET_COUNT)) + ATTESTATION_SUBNET_EXTRA_BITS +def compute_subnet(node_id: int, epoch: Epoch, index: int) -> int: + node_id_prefix = node_id >> (256 - ATTESTATION_SUBNET_PREFIX_BITS) + permutation_seed = hash(uint_to_bytes(epoch // SUBNET_DURATION_IN_EPOCHS)) + permutated_prefix = compute_shuffled_index(node_id_prefix, 1 << ATTESTATION_SUBNET_PREFIX_BITS, permutation_seed) + return (permutated_prefix + index) % ATTESTATION_SUBNET_COUNT +``` -def compute_subnet(node_id, epoch, index): - node_id_prefix = node_id >> (256 - ATTESTATION_SUBNET_PREFIX_BITS) - permutation_seed = hash(uint_to_bytes(epoch // SUBNET_DURATION_IN_EPOCHS)) - permutated_prefix = compute_shuffled_index(node_id_prefix, 1 << ATTESTATION_SUBNET_PREFIX_BITS, permutation_seed) - return (permutated_prefix + index) % ATTESTATION_SUBNET_COUNT - -def compute_subnets(node_id, epoch): - return [compute_subnet(node_id, epoch, idx) for idx in range(SUBNETS_PER_NODE)] +```python +def compute_subnets(node_id: int, epoch: Epoch) -> Sequence[int]: + return [compute_subnet(node_id, epoch, idx) for idx in range(SUBNETS_PER_NODE)] ``` *Note*: Nodes should subscribe to new subnets and remain subscribed to old subnets for at least one epoch. Nodes should pick a random duration to unsubscribe from old subnets to smooth the transition on the exact epoch boundary of which the shuffling changes. diff --git a/tests/core/pyspec/eth2spec/test/phase0/unittests/test_config_invariants.py b/tests/core/pyspec/eth2spec/test/phase0/unittests/test_config_invariants.py index 9b27d1deb..69aa3eb2a 100644 --- a/tests/core/pyspec/eth2spec/test/phase0/unittests/test_config_invariants.py +++ b/tests/core/pyspec/eth2spec/test/phase0/unittests/test_config_invariants.py @@ -75,7 +75,7 @@ def test_time(spec, state): @with_all_phases @spec_state_test def test_networking(spec, state): - assert spec.RANDOM_SUBNETS_PER_VALIDATOR <= spec.ATTESTATION_SUBNET_COUNT + assert spec.SUBNETS_PER_NODE <= spec.ATTESTATION_SUBNET_COUNT @with_all_phases