Skip to content

Commit 69e8f3f

Browse files
committed
PR feedback
1 parent 8487575 commit 69e8f3f

File tree

3 files changed

+23
-85
lines changed

3 files changed

+23
-85
lines changed

eth/beacon/helpers.py

Lines changed: 13 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
from itertools import (
2-
repeat,
3-
)
4-
51
from typing import (
62
Any,
73
Iterable,
@@ -12,6 +8,7 @@
128

139
from eth_utils import (
1410
to_tuple,
11+
ValidationError,
1512
)
1613

1714
from eth_typing import (
@@ -80,21 +77,12 @@ def _get_element_from_recent_list(
8077
#
8178
def get_block_hash(
8279
latest_block_hashes: Sequence[Hash32],
83-
current_block_slot: int,
84-
slot: int,
85-
epoch_length: int) -> Hash32:
80+
current_slot: int,
81+
slot: int) -> Hash32:
8682
"""
8783
Returns the block hash at a recent ``slot``.
8884
"""
89-
if len(latest_block_hashes) != epoch_length * 2:
90-
raise ValueError(
91-
"Length of latest_block_hashes != epoch_length * 2"
92-
"\texpected: %s, found: %s" % (
93-
epoch_length * 2, len(latest_block_hashes)
94-
)
95-
)
96-
97-
slot_relative_position = current_block_slot - epoch_length * 2
85+
slot_relative_position = current_slot - len(latest_block_hashes)
9886
return _get_element_from_recent_list(
9987
latest_block_hashes,
10088
slot,
@@ -105,52 +93,20 @@ def get_block_hash(
10593
@to_tuple
10694
def get_hashes_from_latest_block_hashes(
10795
latest_block_hashes: Sequence[Hash32],
108-
current_block_slot: int,
96+
current_slot: int,
10997
from_slot: int,
110-
to_slot: int,
111-
epoch_length: int) -> Iterable[Hash32]:
98+
to_slot: int) -> Iterable[Hash32]:
11299
"""
113100
Returns the block hashes between ``from_slot`` and ``to_slot``.
114101
"""
115102
for slot in range(from_slot, to_slot + 1):
116103
yield get_block_hash(
117104
latest_block_hashes,
118-
current_block_slot,
105+
current_slot,
119106
slot,
120-
epoch_length,
121107
)
122108

123109

124-
@to_tuple
125-
def get_hashes_to_sign(latest_block_hashes: Sequence[Hash32],
126-
block: 'BaseBeaconBlock',
127-
epoch_length: int) -> Iterable[Hash32]:
128-
"""
129-
Given the head block to attest to, collect the list of hashes to be
130-
signed in the attestation.
131-
"""
132-
yield from get_hashes_from_latest_block_hashes(
133-
latest_block_hashes,
134-
block.slot,
135-
from_slot=block.slot - epoch_length + 1,
136-
to_slot=block.slot - 1,
137-
epoch_length=epoch_length,
138-
)
139-
yield block.hash
140-
141-
142-
@to_tuple
143-
def get_new_latest_block_hashes(old_block_hashes: Sequence[Hash32],
144-
parent_slot: int,
145-
current_slot: int,
146-
parent_hash: Hash32) -> Iterable[Hash32]:
147-
148-
shift_size = current_slot - parent_slot
149-
parent_hash_repeat = min(shift_size, len(old_block_hashes))
150-
yield from old_block_hashes[shift_size:]
151-
yield from repeat(parent_hash, parent_hash_repeat)
152-
153-
154110
#
155111
# Get shards_committees or indices
156112
#
@@ -314,11 +270,11 @@ def get_block_committees_info(parent_block: 'BaseBeaconBlock',
314270
try:
315271
shard_committee = shards_committees[0]
316272
except IndexError:
317-
raise ValueError("shards_committees should not be empty.")
273+
raise ValidationError("shards_committees should not be empty.")
318274

319275
proposer_committee_size = len(shard_committee.committee)
320276
if proposer_committee_size <= 0:
321-
raise ValueError(
277+
raise ValidationError(
322278
"The first committee should not be empty"
323279
)
324280

@@ -351,12 +307,12 @@ def get_beacon_proposer_index(state: 'BeaconState',
351307
try:
352308
first_shard_committee = shard_committees[0]
353309
except IndexError:
354-
raise ValueError("shard_committees should not be empty.")
310+
raise ValidationError("shard_committees should not be empty.")
355311

356312
proposer_committee_size = len(first_shard_committee.committee)
357313

358314
if proposer_committee_size <= 0:
359-
raise ValueError(
315+
raise ValidationError(
360316
"The first committee should not be empty"
361317
)
362318

@@ -395,10 +351,10 @@ def get_attestation_participants(state: 'BeaconState',
395351
try:
396352
shard_committee = shard_committees[0]
397353
except IndexError:
398-
raise ValueError("shard_committees should not be empty.")
354+
raise ValidationError("shard_committees should not be empty.")
399355

400356
if len(participation_bitfield) != get_bitfield_length(len(shard_committee.committee)):
401-
raise ValueError(
357+
raise ValidationError(
402358
'Invalid bitfield length,'
403359
"\texpected: %s, found: %s" % (
404360
get_bitfield_length(len(shard_committee.committee)),

tests/beacon/conftest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
import itertools
23

34
from eth_utils import denoms
45

@@ -80,7 +81,7 @@ def sample_beacon_block_params(epoch_length):
8081
'slot': 10,
8182
'randao_reveal': b'\x55' * 32,
8283
'candidate_pow_receipt_root': b'\x55' * 32,
83-
'ancestor_hashes': tuple([ZERO_HASH32 for _ in range(2 * epoch_length)]),
84+
'ancestor_hashes': tuple(itertools.repeat(ZERO_HASH32, 2 * epoch_length)),
8485
'state_root': b'\x55' * 32,
8586
'attestations': (),
8687
'specials': (),

tests/beacon/test_helpers.py

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import pytest
22

3+
from eth_utils import (
4+
ValidationError,
5+
)
6+
37
from eth.constants import (
48
ZERO_HASH32,
59
)
@@ -20,7 +24,6 @@
2024
get_block_hash,
2125
get_effective_balance,
2226
get_hashes_from_latest_block_hashes,
23-
get_hashes_to_sign,
2427
get_new_shuffling,
2528
get_new_validator_registry_delta_chain_tip,
2629
_get_shard_committees_at_slot,
@@ -137,7 +140,6 @@ def test_get_block_hash(
137140
latest_block_hashes,
138141
current_block_number,
139142
target_slot,
140-
epoch_length,
141143
)
142144
assert block_hash == blocks[target_slot].hash
143145
else:
@@ -146,7 +148,6 @@ def test_get_block_hash(
146148
latest_block_hashes,
147149
current_block_number,
148150
target_slot,
149-
epoch_length,
150151
)
151152

152153

@@ -176,30 +177,10 @@ def test_get_hashes_from_latest_block_hashes(
176177
current_block_slot,
177178
from_slot,
178179
to_slot,
179-
epoch_length,
180180
)
181181
assert len(result) == to_slot - from_slot + 1
182182

183183

184-
def test_get_hashes_to_sign(sample_block, epoch_length):
185-
epoch_length = epoch_length
186-
current_block_slot = 1
187-
blocks, latest_block_hashes = generate_mock_latest_block_hashes(
188-
sample_block,
189-
current_block_slot,
190-
epoch_length,
191-
)
192-
193-
block = blocks[current_block_slot]
194-
result = get_hashes_to_sign(
195-
latest_block_hashes,
196-
block,
197-
epoch_length,
198-
)
199-
assert len(result) == epoch_length
200-
assert result[-1] == block.hash
201-
202-
203184
#
204185
# Get shards_committees or indices
205186
#
@@ -384,7 +365,7 @@ def test_get_new_shuffling_handles_shard_wrap(genesis_validators,
384365
(100, [4, 5, 6, 7], 0, 4),
385366
(100, [4, 5, 6, 7], 2, 6),
386367
(100, [4, 5, 6, 7], 11, 7),
387-
(100, [], 1, ValueError()),
368+
(100, [], 1, ValidationError()),
388369
],
389370
)
390371
def test_get_block_committees_info(monkeypatch,
@@ -420,7 +401,7 @@ def mock_get_shard_committees_at_slot(state,
420401
)
421402

422403
if isinstance(result_proposer_index, Exception):
423-
with pytest.raises(ValueError):
404+
with pytest.raises(ValidationError):
424405
get_block_committees_info(
425406
parent_block,
426407
sample_state,
@@ -499,7 +480,7 @@ def mock_get_shard_committees_at_slot(state,
499480
)
500481
assert proposer_index == committee[slot % len(committee)]
501482
else:
502-
with pytest.raises(ValueError):
483+
with pytest.raises(ValidationError):
503484
get_beacon_proposer_index(
504485
sample_state,
505486
slot,
@@ -600,7 +581,7 @@ def mock_get_shard_committees_at_slot(state,
600581
)
601582

602583
if isinstance(expected, Exception):
603-
with pytest.raises(ValueError):
584+
with pytest.raises(ValidationError):
604585
get_attestation_participants(
605586
state=sample_state,
606587
slot=0,

0 commit comments

Comments
 (0)