Add compute_subscribed_subnets unittests and fix typing errors

This commit is contained in:
Hsiao-Wei Wang
2023-04-25 12:57:42 +08:00
parent 6e423f6c42
commit 745d529598
2 changed files with 45 additions and 4 deletions

View File

@@ -616,10 +616,14 @@ Because Phase 0 does not have shards and thus does not have Shard Committees, th
```python
def compute_subscribed_subnet(node_id: int, epoch: Epoch, index: int) -> int:
node_id_prefix = node_id >> (256 - ATTESTATION_SUBNET_PREFIX_BITS)
node_id_prefix = node_id >> (256 - int(ATTESTATION_SUBNET_PREFIX_BITS))
node_offset = node_id % EPOCHS_PER_SUBNET_SUBSCRIPTION
permutation_seed = hash(uint_to_bytes((epoch + node_offset) // EPOCHS_PER_SUBNET_SUBSCRIPTION))
permutated_prefix = compute_shuffled_index(node_id_prefix, 1 << ATTESTATION_SUBNET_PREFIX_BITS, permutation_seed)
permutation_seed = hash(uint_to_bytes(uint64((epoch + node_offset) // EPOCHS_PER_SUBNET_SUBSCRIPTION)))
permutated_prefix = compute_shuffled_index(
node_id_prefix,
1 << int(ATTESTATION_SUBNET_PREFIX_BITS),
permutation_seed,
)
return (permutated_prefix + index) % ATTESTATION_SUBNET_COUNT
```

View File

@@ -1,6 +1,12 @@
import random
from eth2spec.test.context import (
single_phase,
spec_state_test,
always_bls, with_phases, with_all_phases,
spec_test,
always_bls,
with_phases,
with_all_phases,
)
from eth2spec.test.helpers.constants import PHASE0
from eth2spec.test.helpers.attestations import build_attestation_data, get_valid_attestation
@@ -476,3 +482,34 @@ def test_get_aggregate_and_proof_signature(spec, state):
privkey=privkey,
pubkey=pubkey,
)
def run_compute_subscribed_subnets_arguments(spec, rng=random.Random(1111)):
node_id = rng.randint(0, 2**40 - 1) # try VALIDATOR_REGISTRY_LIMIT
epoch = rng.randint(0, 2**64 - 1)
subnets = spec.compute_subscribed_subnets(node_id, epoch)
assert len(subnets) == spec.SUBNETS_PER_NODE
@with_all_phases
@spec_test
@single_phase
def test_compute_subscribed_subnets_random_1(spec):
rng = random.Random(1111)
run_compute_subscribed_subnets_arguments(spec, rng)
@with_all_phases
@spec_test
@single_phase
def test_compute_subscribed_subnets_random_2(spec):
rng = random.Random(2222)
run_compute_subscribed_subnets_arguments(spec, rng)
@with_all_phases
@spec_test
@single_phase
def test_compute_subscribed_subnets_random_3(spec):
rng = random.Random(3333)
run_compute_subscribed_subnets_arguments(spec, rng)