Skip to content

Commit 72a7fb6

Browse files
committed
cleanup hash -> root. misc cleanup
1 parent e048182 commit 72a7fb6

File tree

2 files changed

+64
-53
lines changed

2 files changed

+64
-53
lines changed

eth/beacon/state_machines/validation.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
)
1616
from eth.beacon.helpers import (
1717
get_attestation_participants,
18-
get_block_hash,
18+
get_block_root,
1919
get_domain,
2020
)
2121
from eth.utils import (
2222
bls
2323
)
2424
from eth.beacon.utils.hash import (
25-
hash_,
25+
hash_eth2,
2626
)
2727
from eth.beacon.types.states import BeaconState # noqa: F401
2828
from eth.beacon.types.attestations import Attestation # noqa: F401
@@ -59,8 +59,8 @@ def validate_attestation(state: BeaconState,
5959

6060
validate_attestation_justified_block_root(
6161
attestation_data=attestation.data,
62-
justified_block_root=get_block_hash(
63-
state.latest_block_hashes,
62+
justified_block_root=get_block_root(
63+
state.latest_block_roots,
6464
current_slot=state.slot,
6565
slot=attestation.data.justified_slot,
6666
),
@@ -144,16 +144,16 @@ def validate_attestation_justified_slot(attestation_data: AttestationData,
144144
def validate_attestation_justified_block_root(attestation_data: AttestationData,
145145
justified_block_root: Hash32) -> None:
146146
"""
147-
Validate ``justified_block_hash`` field of ``attestation_data``.
147+
Validate ``justified_block_root`` field of ``attestation_data``.
148148
Raise ``ValidationError`` if it's invalid.
149149
"""
150-
if attestation_data.justified_block_hash != justified_block_root:
150+
if attestation_data.justified_block_root != justified_block_root:
151151
raise ValidationError(
152-
"Attestation ``justified_block_hash`` is not equal to the "
152+
"Attestation ``justified_block_root`` is not equal to the "
153153
"``justified_block_root`` at the ``justified_slot``:\n"
154154
"\tFound: %s, Expected %s at slot %s" %
155155
(
156-
attestation_data.justified_block_hash,
156+
attestation_data.justified_block_root,
157157
justified_block_root,
158158
attestation_data.justified_slot,
159159
)
@@ -163,41 +163,41 @@ def validate_attestation_justified_block_root(attestation_data: AttestationData,
163163
def validate_attestation_latest_crosslink_root(attestation_data: AttestationData,
164164
latest_crosslink_shard_block_root: Hash32) -> None:
165165
"""
166-
Validate that either the ``latest_crosslink_hash`` or ``shard_block_hash``
166+
Validate that either the ``latest_crosslink_root`` or ``shard_block_root``
167167
field of ``attestation_data`` is the provided ``latest_crosslink_shard_block_root``.
168168
Raise ``ValidationError`` if it's invalid.
169169
"""
170170
acceptable_shard_block_roots = [
171-
attestation_data.latest_crosslink_hash,
172-
attestation_data.shard_block_hash,
171+
attestation_data.latest_crosslink_root,
172+
attestation_data.shard_block_root,
173173
]
174174
if latest_crosslink_shard_block_root not in acceptable_shard_block_roots:
175175
raise ValidationError(
176-
"Neither the attestation ``latest_crosslink_hash`` nor the attestation "
177-
"``shard_block_hash`` are equal to the ``latest_crosslink_shard_block_root``.\n"
176+
"Neither the attestation ``latest_crosslink_root`` nor the attestation "
177+
"``shard_block_root`` are equal to the ``latest_crosslink_shard_block_root``.\n"
178178
"\tFound: %s and %s, Expected %s" %
179179
(
180-
attestation_data.latest_crosslink_hash,
181-
attestation_data.shard_block_hash,
180+
attestation_data.latest_crosslink_root,
181+
attestation_data.shard_block_root,
182182
latest_crosslink_shard_block_root,
183183
)
184184
)
185185

186186

187187
def validate_attestation_shard_block_root(attestation_data: AttestationData) -> None:
188188
"""
189-
Validate ``shard_block_hash`` field of `attestation_data`.
189+
Validate ``shard_block_root`` field of `attestation_data`.
190190
Raise ``ValidationError`` if it's invalid.
191191
192-
Note: This is the Phase 0 version of ``shard_block_hash`` validation.
192+
Note: This is the Phase 0 version of ``shard_block_root`` validation.
193193
This is a built-in stub and will be changed in phase 1.
194194
"""
195-
if attestation_data.shard_block_hash != ZERO_HASH32:
195+
if attestation_data.shard_block_root != ZERO_HASH32:
196196
raise ValidationError(
197-
"Attestation ``shard_block_hash`` is not ZERO_HASH32.\n"
197+
"Attestation ``shard_block_root`` is not ZERO_HASH32.\n"
198198
"\tFound: %s, Expected %s" %
199199
(
200-
attestation_data.shard_block_hash,
200+
attestation_data.shard_block_root,
201201
ZERO_HASH32,
202202
)
203203
)
@@ -228,7 +228,7 @@ def validate_attestation_aggregate_signature(state: BeaconState,
228228
]
229229
group_public_key = bls.aggregate_pubkeys(pubkeys)
230230

231-
message = hash_(
231+
message = hash_eth2(
232232
rlp.encode(attestation.data) +
233233
(0).to_bytes(1, "big")
234234
)

tests/beacon/state_machines/test_attestation_validation.py

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,19 @@
2121

2222
@pytest.mark.parametrize(
2323
(
24-
'attestation_slot,current_slot,epoch_length,'
25-
'min_attestation_inclusion_delay,is_valid'
24+
'attestation_slot,'
25+
'current_slot,'
26+
'epoch_length,'
27+
'min_attestation_inclusion_delay,'
28+
'is_valid,'
2629
),
2730
[
2831
(0, 5, 5, 1, True),
2932
(0, 5, 5, 5, True),
30-
(0, 5, 5, 6, False), # not past min inclusion delay
31-
(7, 5, 10, 1, False), # attestation slot in future
33+
(0, 5, 5, 6, False), # attestation_slot + in_attestation_inclusion_delay > current_slot
34+
(7, 5, 10, 1, False), # attestation_slot > current_slot
3235
(10, 20, 10, 2, True),
33-
(9, 20, 10, 2, False), # more than epoch_length slots have past
36+
(9, 20, 10, 2, False), # attestation_slot + EPOCH_LENGTH < current_slot
3437
]
3538
)
3639
def test_validate_attestation_slot(sample_attestation_data_params,
@@ -39,8 +42,9 @@ def test_validate_attestation_slot(sample_attestation_data_params,
3942
epoch_length,
4043
min_attestation_inclusion_delay,
4144
is_valid):
42-
sample_attestation_data_params['slot'] = attestation_slot
43-
attestation_data = AttestationData(**sample_attestation_data_params)
45+
attestation_data = AttestationData(**sample_attestation_data_params).copy(
46+
slot=attestation_slot,
47+
)
4448

4549
if is_valid:
4650
validate_attestation_slot(
@@ -67,15 +71,15 @@ def test_validate_attestation_slot(sample_attestation_data_params,
6771
'previous_justified_slot,'
6872
'justified_slot,'
6973
'epoch_length,'
70-
'is_valid'
74+
'is_valid,'
7175
),
7276
[
7377
(13, 5, 14, 0, 5, 5, True),
74-
(13, 0, 14, 0, 5, 5, False), # targeting previous but should be targeting current
75-
(13, 20, 14, 0, 5, 5, False), # targeting future slot but should be targeting current
78+
(13, 0, 14, 0, 5, 5, False), # targeting previous_justified_slot, should be targeting justified_slot # noqa: E501
79+
(13, 20, 14, 0, 5, 5, False), # targeting future slot, should be targeting justified_slot
7680
(29, 10, 30, 10, 20, 10, True),
77-
(29, 20, 30, 10, 20, 10, False), # targeting current but should be targeting previous
78-
(29, 36, 30, 10, 20, 10, False), # targeting future slot but should be targeting previous
81+
(29, 20, 30, 10, 20, 10, False), # targeting justified_slot, should be targeting previous_justified_slot # noqa: E501
82+
(29, 36, 30, 10, 20, 10, False), # targeting future slot, should be targeting previous_justified_slot # noqa: E501
7983
(10, 10, 10, 10, 10, 10, True),
8084
]
8185
)
@@ -87,9 +91,10 @@ def test_validate_attestation_justified_slot(sample_attestation_data_params,
8791
justified_slot,
8892
epoch_length,
8993
is_valid):
90-
sample_attestation_data_params['slot'] = attestation_slot
91-
sample_attestation_data_params['justified_slot'] = attestation_justified_slot
92-
attestation_data = AttestationData(**sample_attestation_data_params)
94+
attestation_data = AttestationData(**sample_attestation_data_params).copy(
95+
slot=attestation_slot,
96+
justified_slot=attestation_justified_slot,
97+
)
9398

9499
if is_valid:
95100
validate_attestation_justified_slot(
@@ -114,19 +119,20 @@ def test_validate_attestation_justified_slot(sample_attestation_data_params,
114119
(
115120
'attestation_justified_block_root,'
116121
'justified_block_root,'
117-
'is_valid'
122+
'is_valid,'
118123
),
119124
[
120-
(b'\x42' * 32, b'\x35' * 32, False),
125+
(b'\x42' * 32, b'\x35' * 32, False), # attestation.justified_block_root != justified_block_root # noqa: E501
121126
(b'\x42' * 32, b'\x42' * 32, True),
122127
]
123128
)
124129
def test_validate_attestation_justified_block_root(sample_attestation_data_params,
125130
attestation_justified_block_root,
126131
justified_block_root,
127132
is_valid):
128-
sample_attestation_data_params['justified_block_hash'] = attestation_justified_block_root
129-
attestation_data = AttestationData(**sample_attestation_data_params)
133+
attestation_data = AttestationData(**sample_attestation_data_params).copy(
134+
justified_block_root=attestation_justified_block_root,
135+
)
130136

131137
if is_valid:
132138
validate_attestation_justified_block_root(
@@ -143,10 +149,10 @@ def test_validate_attestation_justified_block_root(sample_attestation_data_param
143149

144150
@pytest.mark.parametrize(
145151
(
146-
'attestation_latest_crosslink_hash,'
147-
'attestation_shard_block_hash,'
152+
'attestation_latest_crosslink_root,'
153+
'attestation_shard_block_root,'
148154
'latest_crosslink_shard_block_root,'
149-
'is_valid'
155+
'is_valid,'
150156
),
151157
[
152158
(b'\x66' * 32, b'\x42' * 32, b'\x35' * 32, False),
@@ -157,13 +163,16 @@ def test_validate_attestation_justified_block_root(sample_attestation_data_param
157163
]
158164
)
159165
def test_validate_attestation_latest_crosslink_root(sample_attestation_data_params,
160-
attestation_latest_crosslink_hash,
161-
attestation_shard_block_hash,
166+
attestation_latest_crosslink_root,
167+
attestation_shard_block_root,
162168
latest_crosslink_shard_block_root,
163169
is_valid):
164-
sample_attestation_data_params['latest_crosslink_hash'] = attestation_latest_crosslink_hash
165-
sample_attestation_data_params['shard_block_hash'] = attestation_shard_block_hash
166-
attestation_data = AttestationData(**sample_attestation_data_params)
170+
sample_attestation_data_params['latest_crosslink_root'] = attestation_latest_crosslink_root
171+
sample_attestation_data_params['shard_block_root'] = attestation_shard_block_root
172+
attestation_data = AttestationData(**sample_attestation_data_params).copy(
173+
latest_crosslink_root=attestation_latest_crosslink_root,
174+
shard_block_root=attestation_shard_block_root,
175+
)
167176

168177
if is_valid:
169178
validate_attestation_latest_crosslink_root(
@@ -180,7 +189,8 @@ def test_validate_attestation_latest_crosslink_root(sample_attestation_data_para
180189

181190
@pytest.mark.parametrize(
182191
(
183-
'attestation_shard_block_hash,is_valid'
192+
'attestation_shard_block_root,'
193+
'is_valid,'
184194
),
185195
[
186196
(ZERO_HASH32, True),
@@ -189,10 +199,11 @@ def test_validate_attestation_latest_crosslink_root(sample_attestation_data_para
189199
]
190200
)
191201
def test_validate_attestation_shard_block_root(sample_attestation_data_params,
192-
attestation_shard_block_hash,
202+
attestation_shard_block_root,
193203
is_valid):
194-
sample_attestation_data_params['shard_block_hash'] = attestation_shard_block_hash
195-
attestation_data = AttestationData(**sample_attestation_data_params)
204+
attestation_data = AttestationData(**sample_attestation_data_params).copy(
205+
shard_block_root=attestation_shard_block_root,
206+
)
196207

197208
if is_valid:
198209
validate_attestation_shard_block_root(
@@ -206,4 +217,4 @@ def test_validate_attestation_shard_block_root(sample_attestation_data_params,
206217

207218

208219
def test_validate_attestation_aggregate_signature():
209-
pass
220+
pass

0 commit comments

Comments
 (0)