Skip to content

Commit de7f93e

Browse files
committed
Tests are executable! :)
1 parent 94a00e7 commit de7f93e

File tree

7 files changed

+65
-10
lines changed

7 files changed

+65
-10
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ venv_lint: venv_activate
2828
$(VENV_ACTIVATE) && flake8 --config=flake8.ini ./src ./tests && mypy --config-file mypy.ini ./src ./tests
2929

3030
venv_test: venv_activate
31-
@echo "TODO: implement tests"
31+
$(VENV_ACTIVATE) && ${VENV_NAME}/bin/python -m pytest tests/test.py

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ eth2spec==1.1.2
44
# dev requirements
55
mypy
66
flake8
7+
8+
# test requirements
9+
pytest

src/dvspec/consensus.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def consensus_is_valid_attestation_data(slashing_db: SlashingDB,
2424
"""Determines if the given attestation is valid for the attestation duty.
2525
"""
2626
assert attestation_data.slot == attestation_duty.slot
27-
assert attestation_data.committee_index == attestation_duty.committee_index
27+
assert attestation_data.index == attestation_duty.committee_index
2828
assert not is_slashable_attestation_data(slashing_db, attestation_data, attestation_duty.pubkey)
2929
return True
3030

@@ -36,6 +36,7 @@ def consensus_on_attestation(slashing_db: SlashingDB, attestation_duty: Attestat
3636
The consensus protocol must use `consensus_is_valid_attestation_data` to determine
3737
validity of the proposed attestation value.
3838
"""
39+
print("This is the wrong consensus_on_attestation")
3940
pass
4041

4142

tests/helpers/consensus.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def consensus_on_attestation(slashing_db: SlashingDB, attestation_duty: Attestat
3333
validity of the proposed attestation value.
3434
"""
3535
# TODO: Use this method in tests instead of dvspec.consensus.consensus_on_attestation
36+
print("This is the correct consensus_on_attestation")
3637
attestation_data = bn_produce_attestation_data(attestation_duty.slot, attestation_duty.committee_index)
3738
assert consensus_is_valid_attestation_data(slashing_db, attestation_data, attestation_duty)
3839
return attestation_data
@@ -46,6 +47,6 @@ def consensus_on_block(slashing_db: SlashingDB, proposer_duty: ProposerDuty) ->
4647
validity of the proposed block value.
4748
"""
4849
# TODO: Use this method in tests instead of dvspec.consensus.consensus_on_block
49-
block = bn_produce_block(proposer_duty.slot, BLSSignature(0x00), Bytes32(0x00))
50+
block = bn_produce_block(proposer_duty.slot, BLSSignature(b'0'*96), Bytes32(b'0'*32))
5051
assert consensus_is_valid_block(slashing_db, block, proposer_duty)
5152
return block

tests/helpers/eth_node_interface.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,13 @@ def fill_attestation_duties_with_val_index(state: State,
9494
return attestation_duties
9595

9696

97-
def fill_proposer_duties_with_val_index(state: State, proposer_duties: List[ProposerDuty]) -> List[ProposerDuty]:
97+
def filter_and_fill_proposer_duties_with_val_index(state: State, proposer_duties: List[ProposerDuty]) -> List[ProposerDuty]:
98+
filtered_proposer_duties = []
9899
val_index_to_pubkey = {}
99100
for dv in state.distributed_validators:
100101
val_index_to_pubkey[dv.validator_identity.index] = dv.validator_identity.pubkey
101102
for pro_duty in proposer_duties:
102-
pro_duty.pubkey = val_index_to_pubkey[pro_duty.validator_index]
103-
return proposer_duties
103+
if pro_duty.validator_index in val_index_to_pubkey:
104+
pro_duty.pubkey = val_index_to_pubkey[pro_duty.validator_index]
105+
filtered_proposer_duties.append(pro_duty)
106+
return filtered_proposer_duties

tests/helpers/state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
def build_distributed_validator(validator_identity: ValidatorIdentity,
2222
num_covalidators: int = 4) -> DistributedValidator:
2323
co_validators = []
24-
for i in range(1, num_covalidators):
24+
for i in range(num_covalidators):
2525
co_validators.append(CoValidator(validator_identity=validator_identity, pubkey=BLSPubkey(0x00), index=i))
2626
slashing_db = SlashingDB(interchange_format_version=5,
2727
genesis_validators_root=Root(),

tests/test.py

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
from pkgutil import iter_modules
2+
import importlib
3+
4+
from eth2spec.phase0.mainnet import SLOTS_PER_EPOCH
5+
6+
import dvspec
17
from dvspec.spec import (
28
serve_attestation_duty,
9+
serve_proposer_duty,
310
)
411

512
from helpers.time import (
@@ -11,11 +18,37 @@
1118
get_validator_indices,
1219
get_distributed_validator_by_index,
1320
)
21+
from tests.helpers.consensus import (
22+
consensus_on_attestation,
23+
consensus_on_block,
24+
)
1425
from tests.helpers.eth_node_interface import (
26+
SLOTS_PER_EPOCH,
1527
bn_get_attestation_duties_for_epoch,
28+
bn_produce_attestation_data,
29+
bn_submit_attestation,
30+
bn_get_proposer_duties_for_epoch,
31+
bn_produce_block,
1632
fill_attestation_duties_with_val_index,
33+
filter_and_fill_proposer_duties_with_val_index,
1734
)
1835

36+
# Replace unimplemented methods from dvspec by methods from the test module
37+
def replace_module_method(module, method_name_string, replacement_method) -> None:
38+
try:
39+
getattr(module, method_name_string)
40+
setattr(module, method_name_string, replacement_method)
41+
except AttributeError:
42+
pass
43+
44+
def replace_method_in_dvspec(method_name_string, replacement_method) -> None:
45+
for dvspec_submodule_info in iter_modules(dvspec.__path__):
46+
dvspec_submodule = importlib.import_module(dvspec.__name__ + '.' + dvspec_submodule_info.name)
47+
replace_module_method(dvspec_submodule, method_name_string, replacement_method)
48+
49+
replace_method_in_dvspec("consensus_on_attestation", consensus_on_attestation)
50+
replace_method_in_dvspec("consensus_on_block", consensus_on_block)
51+
1952

2053
def test_basic_attestation() -> None:
2154
state = build_state(5)
@@ -29,7 +62,21 @@ def test_basic_attestation() -> None:
2962

3063
distributed_validator = get_distributed_validator_by_index(state, attestation_duty.validator_index)
3164
slashing_db = distributed_validator.slashing_db
32-
33-
# TODO: Need to replace dvspec.consensus.consensus_on_attestation with
34-
# tests.helpers.consensus.consensus_on_attestation
3565
serve_attestation_duty(slashing_db, attestation_duty)
66+
67+
68+
def test_basic_block() -> None:
69+
state = build_state(5)
70+
time = get_current_time()
71+
72+
current_epoch = compute_epoch_at_time(time)
73+
validator_indices = get_validator_indices(state)
74+
proposer_duties = bn_get_proposer_duties_for_epoch(current_epoch+1)
75+
filled_proposer_duties = filter_and_fill_proposer_duties_with_val_index(state, proposer_duties)
76+
while len(filled_proposer_duties) == 0:
77+
proposer_duties = bn_get_proposer_duties_for_epoch(current_epoch+1)
78+
filled_proposer_duties = filter_and_fill_proposer_duties_with_val_index(state, proposer_duties)
79+
proposer_duty = filled_proposer_duties[0]
80+
distributed_validator = get_distributed_validator_by_index(state, proposer_duty.validator_index)
81+
slashing_db = distributed_validator.slashing_db
82+
serve_proposer_duty(slashing_db, proposer_duty)

0 commit comments

Comments
 (0)