Skip to content

Commit 075c093

Browse files
committed
Fix validate_attestation and add more tests
1 parent 1e87a64 commit 075c093

File tree

5 files changed

+423
-94
lines changed

5 files changed

+423
-94
lines changed

eth/beacon/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ def aggregate_attestation_record(last_justified_slot: int,
435435
block.slot_number,
436436
parent_hashes,
437437
proposer_attestation.shard_id,
438-
block.shard_block_hash,
438+
proposer_attestation.shard_block_hash,
439439
last_justified_slot,
440440
)
441441

eth/beacon/state_machines/base.py

Lines changed: 9 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@
3535
from eth.beacon.db.chain import BaseBeaconChainDB
3636
from eth.beacon.helpers import (
3737
create_signing_message,
38-
get_attestation_indices,
38+
get_block_committees_info,
3939
get_hashes_to_sign,
4040
get_new_recent_block_hashes,
41-
get_block_committees_info,
42-
get_signed_parent_hashes,
4341
)
4442
from eth.beacon.types.active_states import ActiveState
4543
from eth.beacon.types.attestation_records import AttestationRecord # noqa: F401
@@ -48,13 +46,9 @@
4846
from eth.beacon.state_machines.configs import BeaconConfig # noqa: F401
4947

5048
from .validation import (
51-
validate_aggregate_sig,
52-
validate_bitfield,
53-
validate_justified,
49+
validate_attestation,
5450
validate_parent_block_proposer,
55-
validate_slot,
5651
validate_state_roots,
57-
validate_version,
5852
)
5953

6054

@@ -344,13 +338,12 @@ def compute_per_block_transtion(cls,
344338

345339
# TODO: to implement the RANDAO reveal validation.
346340
cls.validate_randao_reveal()
347-
348341
for attestation in block.attestations:
349-
cls.validate_attestation(
342+
validate_attestation(
350343
block,
351344
parent_block,
352345
crystallized_state,
353-
active_state,
346+
recent_block_hashes,
354347
attestation,
355348
chaindb,
356349
cycle_length,
@@ -401,7 +394,7 @@ def propose_block(
401394
crystallized_state_root=post_crystallized_state.hash,
402395
active_state_root=post_active_state.hash,
403396
)
404-
block_proposal = BlockProposal(
397+
filled_block_proposal = BlockProposal(
405398
block=post_block,
406399
shard_id=block_proposal.shard_id,
407400
shard_block_hash=block_proposal.shard_block_hash,
@@ -410,7 +403,8 @@ def propose_block(
410403
proposer_attestation = self.attest_proposed_block(
411404
post_crystallized_state,
412405
post_active_state,
413-
block_proposal,
406+
active_state,
407+
filled_block_proposal,
414408
chaindb,
415409
config.CYCLE_LENGTH,
416410
private_key,
@@ -426,6 +420,7 @@ def _update_the_states(self,
426420
def attest_proposed_block(self,
427421
post_crystallized_state: CrystallizedState,
428422
post_active_state: ActiveState,
423+
pre_active_state: ActiveState,
429424
block_proposal: 'BlockProposal',
430425
chaindb: BaseBeaconChainDB,
431426
cycle_length: int,
@@ -456,6 +451,7 @@ def attest_proposed_block(self,
456451
block_proposal.block,
457452
cycle_length,
458453
)
454+
459455
message = create_signing_message(
460456
block_proposal.block.slot_number,
461457
parent_hashes,
@@ -495,55 +491,3 @@ def attest_proposed_block(self,
495491
def validate_randao_reveal(cls) -> None:
496492
# TODO: it's a stub
497493
return
498-
499-
#
500-
# Attestation validation
501-
#
502-
@staticmethod
503-
def validate_attestation(block: BaseBeaconBlock,
504-
parent_block: BaseBeaconBlock,
505-
crystallized_state: CrystallizedState,
506-
active_state: ActiveState,
507-
attestation: 'AttestationRecord',
508-
chaindb: BaseBeaconChainDB,
509-
cycle_length: int) -> None:
510-
"""
511-
Validate the given ``attestation``.
512-
513-
Raise ``ValidationError`` if it's invalid.
514-
"""
515-
validate_slot(
516-
parent_block,
517-
attestation,
518-
cycle_length,
519-
)
520-
521-
validate_justified(
522-
crystallized_state,
523-
attestation,
524-
chaindb,
525-
)
526-
527-
attestation_indices = get_attestation_indices(
528-
crystallized_state,
529-
attestation,
530-
cycle_length,
531-
)
532-
533-
validate_bitfield(attestation, attestation_indices)
534-
535-
# TODO: implement versioning
536-
validate_version(crystallized_state, attestation)
537-
538-
parent_hashes = get_signed_parent_hashes(
539-
active_state.recent_block_hashes,
540-
block,
541-
attestation,
542-
cycle_length,
543-
)
544-
validate_aggregate_sig(
545-
crystallized_state,
546-
attestation,
547-
attestation_indices,
548-
parent_hashes,
549-
)

eth/beacon/state_machines/validation.py

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
ValidationError,
1111
)
1212

13+
from eth.exceptions import (
14+
BlockNotFound,
15+
)
1316
from eth.utils import bls
1417
from eth.utils.bitfield import (
1518
get_bitfield_length,
@@ -18,10 +21,14 @@
1821

1922
from eth.beacon.helpers import (
2023
create_signing_message,
24+
get_attestation_indices,
2125
get_block_committees_info,
26+
get_signed_parent_hashes,
2227
)
2328

2429
from eth.beacon.db.chain import BaseBeaconChainDB # noqa: F401
30+
31+
from eth.beacon.types.active_states import ActiveState # noqa: F401
2532
from eth.beacon.types.attestation_records import AttestationRecord # noqa: F401
2633
from eth.beacon.types.blocks import BaseBeaconBlock # noqa: F401
2734
from eth.beacon.types.crystallized_states import CrystallizedState # noqa: F401
@@ -79,6 +86,57 @@ def validate_parent_block_proposer(crystallized_state: 'CrystallizedState',
7986
#
8087
# Attestation validation
8188
#
89+
90+
def validate_attestation(
91+
block: BaseBeaconBlock,
92+
parent_block: BaseBeaconBlock,
93+
crystallized_state: CrystallizedState,
94+
recent_block_hashes: Iterable[Hash32],
95+
attestation: 'AttestationRecord',
96+
chaindb: BaseBeaconChainDB,
97+
cycle_length: int) -> None:
98+
"""
99+
Validate the given ``attestation``.
100+
101+
Raise ``ValidationError`` if it's invalid.
102+
"""
103+
validate_slot(
104+
parent_block,
105+
attestation,
106+
cycle_length,
107+
)
108+
109+
validate_justified(
110+
crystallized_state,
111+
attestation,
112+
chaindb,
113+
)
114+
115+
attestation_indices = get_attestation_indices(
116+
crystallized_state,
117+
attestation,
118+
cycle_length,
119+
)
120+
121+
validate_bitfield(attestation, attestation_indices)
122+
123+
# TODO: implement versioning
124+
validate_version(crystallized_state, attestation)
125+
126+
parent_hashes = get_signed_parent_hashes(
127+
recent_block_hashes,
128+
block,
129+
attestation,
130+
cycle_length,
131+
)
132+
validate_aggregate_sig(
133+
crystallized_state,
134+
attestation,
135+
attestation_indices,
136+
parent_hashes,
137+
)
138+
139+
82140
def validate_slot(parent_block: 'BaseBeaconBlock',
83141
attestation: 'AttestationRecord',
84142
cycle_length: int) -> None:
@@ -120,9 +178,9 @@ def validate_justified(crystallized_state: 'CrystallizedState',
120178
crystallized_state.last_justified_slot,
121179
)
122180
)
123-
124-
justified_block = chaindb.get_block_by_hash(attestation.justified_block_hash)
125-
if justified_block is None:
181+
try:
182+
justified_block = chaindb.get_block_by_hash(attestation.justified_block_hash)
183+
except BlockNotFound:
126184
raise ValidationError(
127185
"justified_block_hash %s is not in the canonical chain" %
128186
attestation.justified_block_hash

0 commit comments

Comments
 (0)