Merge pull request #3715 from ethereum/das-test-vectors

Make `get_custody_columns` return sorted values and add `get_custody_columns` tests
This commit is contained in:
Hsiao-Wei Wang 2024-04-25 03:01:51 +08:00 committed by GitHub
commit e1d71216bb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 148 additions and 3 deletions

View File

@ -8,6 +8,8 @@
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
- [Constants](#constants)
- [Misc](#misc)
- [Custom types](#custom-types)
- [Configuration](#configuration)
- [Data size](#data-size)
@ -39,6 +41,16 @@
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
<!-- /TOC -->
## Constants
The following values are (non-configurable) constants used throughout the specification.
### Misc
| Name | Value |
| - | - |
| `UINT256_MAX` | `uint256(2**256 - 1)` |
## Custom types
We define the following Python custom types for type hinting and readability:
@ -96,8 +108,11 @@ def get_custody_columns(node_id: NodeID, custody_subnet_count: uint64) -> Sequen
subnet_ids = []
i = 0
while len(subnet_ids) < custody_subnet_count:
if node_id == UINT256_MAX:
node_id = 0
subnet_id = (
bytes_to_uint64(hash(uint_to_bytes(uint64(node_id + i)))[0:8])
bytes_to_uint64(hash(uint_to_bytes(uint256(node_id + i)))[0:8])
% DATA_COLUMN_SIDECAR_SUBNET_COUNT
)
if subnet_id not in subnet_ids:
@ -106,11 +121,11 @@ def get_custody_columns(node_id: NodeID, custody_subnet_count: uint64) -> Sequen
assert len(subnet_ids) == len(set(subnet_ids))
columns_per_subnet = NUMBER_OF_COLUMNS // DATA_COLUMN_SIDECAR_SUBNET_COUNT
return [
return sorted([
ColumnIndex(DATA_COLUMN_SIDECAR_SUBNET_COUNT * i + subnet_id)
for i in range(columns_per_subnet)
for subnet_id in subnet_ids
]
])
```
#### `compute_extended_matrix`

View File

@ -0,0 +1,89 @@
import random
from eth2spec.test.context import (
single_phase,
spec_test,
with_eip7594_and_later,
)
def _run_get_custody_columns(spec, rng, node_id=None, custody_subnet_count=None):
if node_id is None:
node_id = rng.randint(0, 2**256 - 1)
if custody_subnet_count is None:
custody_subnet_count = rng.randint(0, spec.config.DATA_COLUMN_SIDECAR_SUBNET_COUNT)
result = spec.get_custody_columns(node_id, custody_subnet_count)
yield 'node_id', 'meta', node_id
yield 'custody_subnet_count', 'meta', custody_subnet_count
assert len(result) == len(set(result))
assert len(result) == (
custody_subnet_count * spec.config.NUMBER_OF_COLUMNS // spec.config.DATA_COLUMN_SIDECAR_SUBNET_COUNT
)
assert all(i < spec.config.NUMBER_OF_COLUMNS for i in result)
python_list_result = [int(i) for i in result]
yield 'result', 'meta', python_list_result
@with_eip7594_and_later
@spec_test
@single_phase
def test_get_custody_columns__min_node_id_min_custody_subnet_count(spec):
rng = random.Random(1111)
yield from _run_get_custody_columns(spec, rng, node_id=0, custody_subnet_count=0)
@with_eip7594_and_later
@spec_test
@single_phase
def test_get_custody_columns__min_node_id_max_custody_subnet_count(spec):
rng = random.Random(1111)
yield from _run_get_custody_columns(
spec, rng, node_id=0,
custody_subnet_count=spec.config.DATA_COLUMN_SIDECAR_SUBNET_COUNT)
@with_eip7594_and_later
@spec_test
@single_phase
def test_get_custody_columns__max_node_id_min_custody_subnet_count(spec):
rng = random.Random(1111)
yield from _run_get_custody_columns(spec, rng, node_id=2**256 - 1, custody_subnet_count=0)
@with_eip7594_and_later
@spec_test
@single_phase
def test_get_custody_columns__max_node_id_max_custody_subnet_count(spec):
rng = random.Random(1111)
yield from _run_get_custody_columns(
spec, rng, node_id=2**256 - 1,
custody_subnet_count=spec.config.DATA_COLUMN_SIDECAR_SUBNET_COUNT,
)
@with_eip7594_and_later
@spec_test
@single_phase
def test_get_custody_columns__1(spec):
rng = random.Random(1111)
yield from _run_get_custody_columns(spec, rng)
@with_eip7594_and_later
@spec_test
@single_phase
def test_get_custody_columns__2(spec):
rng = random.Random(2222)
yield from _run_get_custody_columns(spec, rng)
@with_eip7594_and_later
@spec_test
@single_phase
def test_get_custody_columns__3(spec):
rng = random.Random(3333)
yield from _run_get_custody_columns(spec, rng)

View File

@ -0,0 +1,6 @@
# Networking tests
The aim of the networking tests is to set a base-line on what really needs to pass, i.e. the essentials.
Handlers:
- [`get_custody_columns`](./get_custody_columns.md): `get_custody_columns` helper tests

View File

@ -0,0 +1,14 @@
# `get_custody_columns` tests
`get_custody_columns` tests provide sanity check of the correctness of `get_custody_columns` helper.
## Test case format
### `meta.yaml`
```yaml
description: string -- optional: description of test case, purely for debugging purposes.
node_id: int -- argument: the NodeId input.
custody_subnet_count: int -- argument: the count of custody subnets.
result: list of int -- output: the list of resulting column indices.
```

View File

@ -0,0 +1,5 @@
# Networking tests
The purpose of this test-generator is to provide test-vectors for validating the correct implementation of the networking protocol.
Test-format documentation can be found [here](../../formats/networking/README.md).

View File

View File

@ -0,0 +1,14 @@
from eth2spec.test.helpers.constants import EIP7594
from eth2spec.gen_helpers.gen_from_tests.gen import run_state_test_generators
if __name__ == "__main__":
eip7594_mods = {key: 'eth2spec.test.eip7594.networking.test_' + key for key in [
'get_custody_columns',
]}
all_mods = {
EIP7594: eip7594_mods
}
run_state_test_generators(runner_name="networking", all_mods=all_mods)

View File

@ -0,0 +1,2 @@
pytest>=4.4
../../../[generator]