1+ from pkgutil import iter_modules
2+ import importlib
3+
4+ from eth2spec .phase0 .mainnet import SLOTS_PER_EPOCH
5+
6+ import dvspec
17from dvspec .spec import (
28 serve_attestation_duty ,
9+ serve_proposer_duty ,
310)
411
512from helpers .time import (
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+ )
1425from 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
2053def 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