Skip to content

Commit 802acc1

Browse files
committed
Fix get_block_hash
1 parent 13bdd94 commit 802acc1

File tree

4 files changed

+74
-69
lines changed

4 files changed

+74
-69
lines changed

eth/beacon/helpers.py

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -74,34 +74,33 @@ def _get_element_from_recent_list(
7474
# Get block hash(es)
7575
#
7676
def get_block_hash(
77-
recent_block_hashes: Sequence[Hash32],
78-
current_block_slot_number: int,
77+
latest_block_hashes: Sequence[Hash32],
78+
current_block_slot: int,
7979
slot: int,
8080
epoch_length: int) -> Hash32:
8181
"""
82-
Return the blockhash from ``ActiveState.recent_block_hashes`` by
83-
``current_block_slot_number``.
82+
Returns the block hash at a recent ``slot``.
8483
"""
85-
if len(recent_block_hashes) != epoch_length * 2:
84+
if len(latest_block_hashes) != epoch_length * 2:
8685
raise ValueError(
87-
"Length of recent_block_hashes != epoch_length * 2"
86+
"Length of latest_block_hashes != epoch_length * 2"
8887
"\texpected: %s, found: %s" % (
89-
epoch_length * 2, len(recent_block_hashes)
88+
epoch_length * 2, len(latest_block_hashes)
9089
)
9190
)
9291

93-
slot_relative_position = current_block_slot_number - epoch_length * 2
92+
slot_relative_position = current_block_slot - epoch_length * 2
9493
return _get_element_from_recent_list(
95-
recent_block_hashes,
94+
latest_block_hashes,
9695
slot,
9796
slot_relative_position,
9897
)
9998

10099

101100
@to_tuple
102-
def get_hashes_from_recent_block_hashes(
103-
recent_block_hashes: Sequence[Hash32],
104-
current_block_slot_number: int,
101+
def get_hashes_from_latest_block_hashes(
102+
latest_block_hashes: Sequence[Hash32],
103+
current_block_slot: int,
105104
from_slot: int,
106105
to_slot: int,
107106
epoch_length: int) -> Iterable[Hash32]:
@@ -110,43 +109,43 @@ def get_hashes_from_recent_block_hashes(
110109
"""
111110
for slot in range(from_slot, to_slot + 1):
112111
yield get_block_hash(
113-
recent_block_hashes,
114-
current_block_slot_number,
112+
latest_block_hashes,
113+
current_block_slot,
115114
slot,
116115
epoch_length,
117116
)
118117

119118

120119
@to_tuple
121-
def get_hashes_to_sign(recent_block_hashes: Sequence[Hash32],
120+
def get_hashes_to_sign(latest_block_hashes: Sequence[Hash32],
122121
block: 'BaseBeaconBlock',
123122
epoch_length: int) -> Iterable[Hash32]:
124123
"""
125124
Given the head block to attest to, collect the list of hashes to be
126125
signed in the attestation.
127126
"""
128-
yield from get_hashes_from_recent_block_hashes(
129-
recent_block_hashes,
130-
block.slot_number,
131-
from_slot=block.slot_number - epoch_length + 1,
132-
to_slot=block.slot_number - 1,
127+
yield from get_hashes_from_latest_block_hashes(
128+
latest_block_hashes,
129+
block.slot,
130+
from_slot=block.slot - epoch_length + 1,
131+
to_slot=block.slot - 1,
133132
epoch_length=epoch_length,
134133
)
135134
yield block.hash
136135

137136

138137
@to_tuple
139-
def get_signed_parent_hashes(recent_block_hashes: Sequence[Hash32],
138+
def get_signed_parent_hashes(latest_block_hashes: Sequence[Hash32],
140139
block: 'BaseBeaconBlock',
141140
attestation: 'AttestationRecord',
142141
epoch_length: int) -> Iterable[Hash32]:
143142
"""
144143
Given an attestation and the block they were included in,
145144
the list of hashes that were included in the signature.
146145
"""
147-
yield from get_hashes_from_recent_block_hashes(
148-
recent_block_hashes,
149-
block.slot_number,
146+
yield from get_hashes_from_latest_block_hashes(
147+
latest_block_hashes,
148+
block.slot,
150149
from_slot=attestation.slot - epoch_length + 1,
151150
to_slot=attestation.slot - len(attestation.oblique_parent_hashes),
152151
epoch_length=epoch_length,
@@ -155,7 +154,7 @@ def get_signed_parent_hashes(recent_block_hashes: Sequence[Hash32],
155154

156155

157156
@to_tuple
158-
def get_new_recent_block_hashes(old_block_hashes: Sequence[Hash32],
157+
def get_new_latest_block_hashes(old_block_hashes: Sequence[Hash32],
159158
parent_slot: int,
160159
current_slot: int,
161160
parent_hash: Hash32) -> Iterable[Hash32]:
@@ -339,7 +338,7 @@ def get_block_committees_info(parent_block: 'BaseBeaconBlock',
339338
epoch_length: int) -> BlockCommitteesInfo:
340339
shards_committees = get_shard_committees_at_slot(
341340
crystallized_state,
342-
parent_block.slot_number,
341+
parent_block.slot,
343342
epoch_length,
344343
)
345344
"""
@@ -360,7 +359,7 @@ def get_block_committees_info(parent_block: 'BaseBeaconBlock',
360359
)
361360

362361
proposer_index_in_committee = (
363-
parent_block.slot_number %
362+
parent_block.slot %
364363
proposer_committee_size
365364
)
366365

tests/beacon/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ def sample_attestation_data_params():
7575

7676

7777
@pytest.fixture
78-
def sample_beacon_block_params():
78+
def sample_beacon_block_params(epoch_length):
7979
return {
8080
'slot': 10,
8181
'randao_reveal': b'\x55' * 32,
8282
'candidate_pow_receipt_root': b'\x55' * 32,
83-
'ancestor_hashes': (),
83+
'ancestor_hashes': tuple([ZERO_HASH32 for _ in range(2 * epoch_length)]),
8484
'state_root': b'\x55' * 32,
8585
'attestations': (),
8686
'specials': (),

tests/beacon/helpers.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
from eth.beacon.types.validator_records import (
55
ValidatorRecord,
66
)
7-
from eth.constants import (
8-
ZERO_HASH32,
9-
)
107

118

129
def mock_validator_record(pubkey, max_deposit):
@@ -23,15 +20,24 @@ def mock_validator_record(pubkey, max_deposit):
2320

2421

2522
def get_pseudo_chain(length, genesis_block):
26-
"""Get a pseudo chain, only slot_number and parent_hash are valid.
23+
"""
24+
Get a pseudo chain, only slot and parent_hash are valid.
2725
"""
2826
blocks = []
27+
ancestor_hashes_len = len(genesis_block.ancestor_hashes)
2928
for slot in range(length * 3):
29+
if slot > 0:
30+
ancestor_hashes = (
31+
(blocks[slot - 1].hash, ) +
32+
blocks[slot - 1].ancestor_hashes[:ancestor_hashes_len]
33+
)
34+
else:
35+
ancestor_hashes = genesis_block.ancestor_hashes
3036
blocks.append(
3137
genesis_block.copy(
32-
slot_number=slot,
33-
parent_hash=blocks[slot - 1].hash if slot > 0 else ZERO_HASH32
38+
slot=slot,
39+
ancestor_hashes=ancestor_hashes
3440
)
3541
)
3642

37-
return blocks
43+
return tuple(blocks)

tests/beacon/test_helpers.py

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
ValidatorStatusCode,
55
)
66
from eth.beacon.types.attestation_records import AttestationRecord
7+
from eth.beacon.types.blocks import BaseBeaconBlock
78
from eth.beacon.types.shard_committees import ShardCommittee
89
from eth.beacon.types.validator_records import ValidatorRecord
910
from eth.beacon.helpers import (
1011
_get_element_from_recent_list,
1112
get_active_validator_indices,
1213
get_attestation_indices,
1314
get_block_hash,
14-
get_hashes_from_recent_block_hashes,
15+
get_hashes_from_latest_block_hashes,
1516
get_hashes_to_sign,
1617
get_new_shuffling,
1718
_get_shard_committees_at_slot,
@@ -39,18 +40,18 @@ def get_sample_shard_committees_at_slots(num_slot,
3940
)
4041

4142

42-
def generate_mock_recent_block_hashes(
43+
def generate_mock_latest_block_hashes(
4344
genesis_block,
4445
current_block_number,
4546
epoch_length):
4647
chain_length = (current_block_number // epoch_length + 1) * epoch_length
4748
blocks = get_pseudo_chain(chain_length, genesis_block)
48-
recent_block_hashes = [
49+
latest_block_hashes = [
4950
b'\x00' * 32
5051
for i
5152
in range(epoch_length * 2 - current_block_number)
5253
] + [block.hash for block in blocks[:current_block_number]]
53-
return blocks, recent_block_hashes
54+
return blocks, latest_block_hashes
5455

5556

5657
@pytest.mark.parametrize(
@@ -88,7 +89,6 @@ def test_get_element_from_recent_list(target_list,
8889
#
8990
# Get block hashes
9091
#
91-
@pytest.mark.xfail(reason="Need to be fixed")
9292
@pytest.mark.parametrize(
9393
(
9494
'current_block_number,target_slot,success'
@@ -103,22 +103,22 @@ def test_get_element_from_recent_list(target_list,
103103
],
104104
)
105105
def test_get_block_hash(
106-
genesis_block,
107106
current_block_number,
108107
target_slot,
109108
success,
110-
epoch_length):
111-
epoch_length = epoch_length
109+
epoch_length,
110+
sample_beacon_block_params):
111+
sample_block = BaseBeaconBlock(**sample_beacon_block_params)
112112

113-
blocks, recent_block_hashes = generate_mock_recent_block_hashes(
114-
genesis_block,
113+
blocks, latest_block_hashes = generate_mock_latest_block_hashes(
114+
sample_block,
115115
current_block_number,
116116
epoch_length,
117117
)
118118

119119
if success:
120120
block_hash = get_block_hash(
121-
recent_block_hashes,
121+
latest_block_hashes,
122122
current_block_number,
123123
target_slot,
124124
epoch_length,
@@ -127,7 +127,7 @@ def test_get_block_hash(
127127
else:
128128
with pytest.raises(ValueError):
129129
get_block_hash(
130-
recent_block_hashes,
130+
latest_block_hashes,
131131
current_block_number,
132132
target_slot,
133133
epoch_length,
@@ -137,28 +137,28 @@ def test_get_block_hash(
137137
@pytest.mark.xfail(reason="Need to be fixed")
138138
@pytest.mark.parametrize(
139139
(
140-
'epoch_length,current_block_slot_number,from_slot,to_slot'
140+
'epoch_length,current_block_slot,from_slot,to_slot'
141141
),
142142
[
143143
(20, 10, 2, 7),
144144
(20, 30, 10, 20),
145145
],
146146
)
147-
def test_get_hashes_from_recent_block_hashes(
147+
def test_get_hashes_from_latest_block_hashes(
148148
genesis_block,
149-
current_block_slot_number,
149+
current_block_slot,
150150
from_slot,
151151
to_slot,
152152
epoch_length):
153-
_, recent_block_hashes = generate_mock_recent_block_hashes(
153+
_, latest_block_hashes = generate_mock_latest_block_hashes(
154154
genesis_block,
155-
current_block_slot_number,
155+
current_block_slot,
156156
epoch_length,
157157
)
158158

159-
result = get_hashes_from_recent_block_hashes(
160-
recent_block_hashes,
161-
current_block_slot_number,
159+
result = get_hashes_from_latest_block_hashes(
160+
latest_block_hashes,
161+
current_block_slot,
162162
from_slot,
163163
to_slot,
164164
epoch_length,
@@ -169,16 +169,16 @@ def test_get_hashes_from_recent_block_hashes(
169169
@pytest.mark.xfail(reason="Need to be fixed")
170170
def test_get_hashes_to_sign(genesis_block, epoch_length):
171171
epoch_length = epoch_length
172-
current_block_slot_number = 1
173-
blocks, recent_block_hashes = generate_mock_recent_block_hashes(
172+
current_block_slot = 1
173+
blocks, latest_block_hashes = generate_mock_latest_block_hashes(
174174
genesis_block,
175-
current_block_slot_number,
175+
current_block_slot,
176176
epoch_length,
177177
)
178178

179-
block = blocks[current_block_slot_number]
179+
block = blocks[current_block_slot]
180180
result = get_hashes_to_sign(
181-
recent_block_hashes,
181+
latest_block_hashes,
182182
block,
183183
epoch_length,
184184
)
@@ -187,25 +187,25 @@ def test_get_hashes_to_sign(genesis_block, epoch_length):
187187

188188

189189
@pytest.mark.xfail(reason="Need to be fixed")
190-
def test_get_new_recent_block_hashes(genesis_block,
190+
def test_get_new_latest_block_hashes(genesis_block,
191191
epoch_length,
192192
sample_attestation_record_params):
193193
epoch_length = epoch_length
194-
current_block_slot_number = 15
195-
blocks, recent_block_hashes = generate_mock_recent_block_hashes(
194+
current_block_slot = 15
195+
blocks, latest_block_hashes = generate_mock_latest_block_hashes(
196196
genesis_block,
197-
current_block_slot_number,
197+
current_block_slot,
198198
epoch_length,
199199
)
200200

201-
block = blocks[current_block_slot_number]
201+
block = blocks[current_block_slot]
202202
oblique_parent_hashes = [b'\x77' * 32]
203203
attestation = AttestationRecord(**sample_attestation_record_params).copy(
204204
slot=10,
205205
oblique_parent_hashes=oblique_parent_hashes,
206206
)
207207
result = get_signed_parent_hashes(
208-
recent_block_hashes,
208+
latest_block_hashes,
209209
block,
210210
attestation,
211211
epoch_length,
@@ -449,12 +449,12 @@ def mock_get_shard_committees_at_slot(parent_block,
449449
monkeypatch.setattr(
450450
helpers,
451451
'_get_shard_committees_at_slot',
452-
mock__get_shard_committees_at_slot
452+
mock_get_shard_committees_at_slot
453453
)
454454

455455
parent_block = genesis_block
456456
parent_block = genesis_block.copy(
457-
slot_number=parent_block_number,
457+
slot=parent_block_number,
458458
)
459459

460460
if isinstance(result_proposer_index_in_committee, Exception):

0 commit comments

Comments
 (0)