Skip to content

Commit 493f40a

Browse files
committed
Fix test_aggregate_votes test and add verify_votes function
1 parent 3e0d764 commit 493f40a

File tree

3 files changed

+59
-29
lines changed

3 files changed

+59
-29
lines changed

eth/beacon/block_committees_info.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import (
2-
Iterable,
32
NamedTuple,
3+
Tuple,
44
TYPE_CHECKING,
55
)
66

@@ -15,6 +15,6 @@
1515
('proposer_index_in_committee', int),
1616
('proposer_shard_id', int),
1717
('proposer_committee_size', int),
18-
('shards_and_committees', Iterable['ShardAndCommittee'])
18+
('shards_and_committees', Tuple['ShardAndCommittee'])
1919
)
2020
)

eth/beacon/helpers.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -439,11 +439,16 @@ def aggregate_attestation_record(last_justified_slot: int,
439439
last_justified_slot,
440440
)
441441

442+
# Verify
443+
# TODO: Verification is very slow, needs to compute in parallel
444+
voting_sigs, committee_indices = verify_votes(message, votes)
445+
446+
# Aggregate the votes
442447
bitfield, sigs = aggregate_votes(
443-
message,
444-
votes,
445-
proposer_attestation.bitfield,
446-
proposer_attestation.sigs,
448+
bitfield=proposer_attestation.bitfield,
449+
sigs=proposer_attestation.aggregate_sig,
450+
voting_sigs=voting_sigs,
451+
voting_committee_indices=committee_indices
447452
)
448453

449454
return proposer_attestation.copy(
@@ -452,11 +457,12 @@ def aggregate_attestation_record(last_justified_slot: int,
452457
)
453458

454459

455-
def aggregate_votes(message: bytes,
456-
votes: Iterable[Tuple[int, bytes, int]],
457-
pre_bitfield: bytes,
458-
pre_sigs: Iterable[bytes]) -> Tuple[bytes, Iterable[int]]:
459-
# Update the bitfield and append the signatures
460+
def verify_votes(
461+
message: bytes,
462+
votes: Iterable[Tuple[int, bytes, int]]) -> Tuple[Tuple[bytes, ...], Tuple[int, ...]]:
463+
"""
464+
Verify the given votes
465+
"""
460466
sigs_with_committe_info = tuple(
461467
(sig, committee_index)
462468
for (committee_index, sig, public_key)
@@ -466,16 +472,26 @@ def aggregate_votes(message: bytes,
466472
try:
467473
sigs, committee_indices = zip(*sigs_with_committe_info)
468474
except ValueError:
469-
sigs = ()
470-
committee_indices = ()
475+
sigs = tuple()
476+
committee_indices = tuple()
477+
478+
return sigs, committee_indices
471479

472-
sigs = sigs + tuple(pre_sigs)
473-
bitfield = pre_bitfield
480+
481+
def aggregate_votes(bitfield: bytes,
482+
sigs: Iterable[bytes],
483+
voting_sigs: Iterable[bytes],
484+
voting_committee_indices: Iterable[int]) -> Tuple[bytes, Tuple[int]]:
485+
"""
486+
Aggregate the votes
487+
"""
488+
# Update the bitfield and append the signatures
489+
sigs = tuple(sigs) + tuple(voting_sigs)
474490
bitfield = pipe(
475491
bitfield,
476492
*(
477493
set_voted(index=committee_index)
478-
for committee_index in committee_indices
494+
for committee_index in voting_committee_indices
479495
)
480496
)
481497

tests/beacon/test_helpers.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import random
2-
31
import pytest
2+
from hypothesis import (
3+
given,
4+
settings,
5+
strategies as st,
6+
)
47

58
from eth.utils import bls
69
from eth.utils.bitfield import (
@@ -20,6 +23,7 @@
2023
get_shards_and_committees_for_slot,
2124
get_signed_parent_hashes,
2225
get_block_committees_info,
26+
verify_votes,
2327
)
2428

2529
from tests.beacon.helpers import (
@@ -398,43 +402,53 @@ def mock_get_shards_and_committees_for_slot(parent_block,
398402
)
399403

400404

405+
@settings(max_examples=1)
406+
@given(random=st.randoms())
401407
@pytest.mark.parametrize(
402408
(
403409
'votes_count'
404410
),
405411
[
406412
(0),
407-
(1),
408-
(5),
409413
(9),
410414
],
411415
)
412-
def test_aggregate_votes(votes_count, privkeys, pubkeys):
416+
def test_aggregate_votes(votes_count, random, privkeys, pubkeys):
413417
bit_count = 10
414418
pre_bitfield = get_empty_bitfield(bit_count)
415419
pre_sigs = ()
416420

417421
random_votes = random.sample([i for i in range(bit_count)], votes_count)
418422
message = b'hello'
419423

420-
# (committee_index, sig, public_key)
424+
# Get votes: (committee_index, sig, public_key)
421425
votes = [
422426
(committee_index, bls.sign(message, privkeys[committee_index]), pubkeys[committee_index])
423427
for committee_index in random_votes
424428
]
429+
430+
# Verify
431+
sigs, committee_indices = verify_votes(message, votes)
432+
433+
# Aggregate the votes
425434
bitfield, sigs = aggregate_votes(
426-
message,
427-
votes,
428-
pre_bitfield,
429-
pre_sigs,
435+
bitfield=pre_bitfield,
436+
sigs=pre_sigs,
437+
voting_sigs=sigs,
438+
voting_committee_indices=committee_indices
430439
)
431440

432441
try:
433442
_, _, pubs = zip(*votes)
434443
except ValueError:
435444
pubs = ()
436445

446+
voted_index = [
447+
committee_index
448+
for committee_index in random_votes
449+
if has_voted(bitfield, committee_index)
450+
]
451+
assert len(voted_index) == len(votes)
452+
437453
aggregated_pubs = bls.aggregate_pubs(pubs)
438-
for committee_index in random_votes:
439-
if has_voted(bitfield, committee_index):
440-
assert bls.verify(message, aggregated_pubs, sigs)
454+
assert bls.verify(message, aggregated_pubs, sigs)

0 commit comments

Comments
 (0)