def test_get_static_epochs(): VALIDATOR1 = b"\x00" * 20 VALIDATOR2 = b"\x01" * 20 CONTRACT_ADDRESS = b"\xff" * 20 ranges = [ ValidatorDefinitionRange( enter_height=0, leave_height=5, is_contract=False, contract_address=None, validators=[VALIDATOR1], ), ValidatorDefinitionRange( enter_height=5, leave_height=10, is_contract=True, contract_address=CONTRACT_ADDRESS, validators=None, ), ValidatorDefinitionRange( enter_height=10, leave_height=None, is_contract=False, contract_address=None, validators=[VALIDATOR2], ), ] epochs = get_static_epochs(ranges) assert epochs == [Epoch(0, [VALIDATOR1], 0), Epoch(10, [VALIDATOR2], 2)]
def test_remove_irrelevant_epoch(): primary_oracle = PrimaryOracle() primary_oracle.add_epoch(Epoch(0, [VALIDATOR1], 0)) primary_oracle.add_epoch(Epoch(5, [VALIDATOR2], 0)) primary_oracle.add_epoch(Epoch(2, [VALIDATOR3], 1)) primary_oracle.max_height = 5 assert primary_oracle.get_primary(height=5, step=0) == VALIDATOR3
def test_epoch_sorting(): primary_oracle = PrimaryOracle() primary_oracle.add_epoch(Epoch(0, [VALIDATOR1], 0)) primary_oracle.add_epoch(Epoch(10, [VALIDATOR3], 0)) primary_oracle.add_epoch(Epoch(5, [VALIDATOR2], 0)) primary_oracle.max_height = 14 for height in range(5): assert primary_oracle.get_primary(height=height, step=0) == VALIDATOR1 for height in range(5, 10): assert primary_oracle.get_primary(height=height, step=0) == VALIDATOR2 for height in range(10, 15): assert primary_oracle.get_primary(height=height, step=0) == VALIDATOR3
def test_epoch_fetcher_fetches_from_all_contracts(w3, tester, validator_set_contract): val_def, (contract1, contract2) = initialize_scenario(validator_set_contract, transition_heights=[100, 200]) fetcher = EpochFetcher(w3, val_def) validators1 = initialize_validators(contract1) validators2 = initialize_validators(contract2) epochs = fetcher.fetch_new_epochs() assert epochs == [Epoch(100, validators1, 0), Epoch(200, validators2, 1)]
def test_contract_epoch_fetcher_sets_latest_epoch(w3, tester, validator_set_contract): val_def, (contract, ) = initialize_scenario(validator_set_contract, transition_heights=[100]) validators1 = initialize_validators(contract) fetcher = ContractEpochFetcher(w3, val_def[0], 0) fetcher.fetch_new_epochs() assert fetcher.latest_fetched_epoch == Epoch(100, validators1, 0) mine_until(w3, tester, 105) validators2, height2 = change_validators(contract) fetcher.fetch_new_epochs() assert fetcher.latest_fetched_epoch == Epoch(height2, validators2, 0)
def test_get_primary_late_epoch(): primary_oracle = PrimaryOracle() primary_oracle.add_epoch(Epoch(5, [VALIDATOR1], 0)) primary_oracle.max_height = 4 for height in range(5): for step in range(10): with pytest.raises(ValueError): primary_oracle.get_primary(height=height, step=step)
def primary_oracle(validators): primary_oracle = PrimaryOracle() primary_oracle.add_epoch( Epoch(start_height=0, validators=validators, validator_definition_index=0)) primary_oracle.max_height = math.inf return primary_oracle
def test_fetch_first_epoch(w3, validator_set_contract): val_def, (contract, ) = initialize_scenario(validator_set_contract, transition_heights=[100]) validators = initialize_validators(contract) fetcher = ContractEpochFetcher(w3, val_def[0], 0) epochs = fetcher.fetch_new_epochs() assert epochs == [Epoch(100, validators, 0)]
def test_get_primary_two_epochs(): primary_oracle = PrimaryOracle() primary_oracle.add_epoch(Epoch(0, [VALIDATOR1, VALIDATOR2], 0)) primary_oracle.add_epoch(Epoch(5, [VALIDATOR3, VALIDATOR4], 0)) primary_oracle.max_height = 9 for height in range(5): for step in range(0, 10, 2): assert primary_oracle.get_primary(height=height, step=step) == VALIDATOR1 for step in range(1, 10, 2): assert primary_oracle.get_primary(height=height, step=step) == VALIDATOR2 for height in range(5, 10): for step in range(0, 10, 2): assert primary_oracle.get_primary(height=height, step=step) == VALIDATOR3 for step in range(1, 10, 2): assert primary_oracle.get_primary(height=height, step=step) == VALIDATOR4
def test_fetch_multiple_updates(w3, tester, validator_set_contract): val_def, (contract, ) = initialize_scenario(validator_set_contract, transition_heights=[100]) initialize_validators(contract) fetcher = ContractEpochFetcher(w3, val_def[0], 0) fetcher.fetch_new_epochs() mine_until(w3, tester, 105) validators1, height1 = change_validators(contract) mine_until(w3, tester, height1 + 10) validators2, height2 = change_validators(contract) epochs = fetcher.fetch_new_epochs() assert epochs == [ Epoch(height1, validators1, 0), Epoch(height2, validators2, 0) ]
def test_fetch_single_update(w3, tester, validator_set_contract): val_def, (contract, ) = initialize_scenario(validator_set_contract, transition_heights=[100]) initialize_validators(contract) fetcher = ContractEpochFetcher(w3, val_def[0], 0) fetcher.fetch_new_epochs() mine_until(w3, tester, 105) validators, height = change_validators(contract) epochs = fetcher.fetch_new_epochs() assert epochs == [Epoch(height, validators, 0)]
def test_add_empty_epoch(): primary_oracle = PrimaryOracle() with pytest.raises(ValueError): primary_oracle.add_epoch(Epoch(0, [], 0))
def test_get_too_far_ahead(): primary_oracle = PrimaryOracle() primary_oracle.add_epoch(Epoch(0, [VALIDATOR1], 0)) primary_oracle.max_height = 5 with pytest.raises(ValueError): primary_oracle.get_primary(height=6, step=0)