def test_get_sample_result(smc_handler): # noqa: F811 w3 = smc_handler.web3 # Register notary 0~8 and fast forward to next period batch_register(smc_handler, 0, 8) fast_forward(smc_handler, 1) # Update notary sample size current_period = w3.eth.blockNumber // smc_handler.config['PERIOD_LENGTH'] update_notary_sample_size(smc_handler) # Get all committee of current period committee_group = [] for shard_id in range(smc_handler.config['SHARD_COUNT']): committee_group.append(get_committee_list(smc_handler, shard_id)) # Get sampling result for notary 0 notary_0 = NotaryAccount(0) _, notary_0_pool_index = smc_handler.get_notary_info( notary_0.checksum_address) notary_0_sampling_result = get_sample_result(smc_handler, notary_0_pool_index) for (period, shard_id, sampling_index) in notary_0_sampling_result: assert period == current_period # Check that notary is correctly sampled in get_committee_list assert committee_group[shard_id][ sampling_index] == notary_0.canonical_address # Check that notary is correctly sampled in SMC assert smc_handler.get_member_of_committee(shard_id, sampling_index) \ == notary_0.canonical_address
def test_committee_change_with_deregister_then_register( smc_handler): # noqa: F811 w3 = smc_handler.web3 # Register notary 0~8 and fast forward to next period batch_register(smc_handler, 0, 8) fast_forward(smc_handler, 1) # Update notary sample size update_notary_sample_size(smc_handler) notary_pool_list = get_notary_pool_list(smc_handler) # Choose the first sampled notary and deregister it notary = get_committee_list(smc_handler, 0)[0] notary_index = notary_pool_list.index(notary) smc_handler.deregister_notary( private_key=NotaryAccount(notary_index).private_key) mine(w3, 1) # Check that first slot in committee is now empty assert smc_handler.get_member_of_committee(0, 0) == b'\x00' * 20 # Register notary 9 smc_handler.register_notary(private_key=NotaryAccount(9).private_key) mine(w3, 1) # Check that first slot in committee is replaced by notary 9 assert smc_handler.get_member_of_committee( 0, 0) == NotaryAccount(9).canonical_address
def test_get_member_of_committee_with_updated_sample_size( smc_handler): # noqa: F811 w3 = smc_handler.web3 # Register notary 0~8 and fast forward to next period batch_register(smc_handler, 0, 8) fast_forward(smc_handler, 1) # Update notary sample size update_notary_sample_size(smc_handler) # Check that sample-size-related values match current_period = w3.eth.blockNumber // smc_handler.config['PERIOD_LENGTH'] notary_sample_size_updated_period = smc_handler.notary_sample_size_updated_period( ) assert notary_sample_size_updated_period == current_period current_period_notary_sample_size = smc_handler.current_period_notary_sample_size( ) assert current_period_notary_sample_size == 9 next_period_notary_sample_size = smc_handler.next_period_notary_sample_size( ) assert next_period_notary_sample_size == 9 shard_0_committee_list = get_committee_list(smc_handler, 0) for (i, notary) in enumerate(shard_0_committee_list): assert smc_handler.get_member_of_committee(0, i) == notary
def test_get_member_of_committee_without_updating_sample_size( smc_handler): # noqa: F811 w3 = smc_handler.web3 # Register notary 0~5 and fast forward to next period batch_register(smc_handler, 0, 5) fast_forward(smc_handler, 1) # Register notary 6~8 batch_register(smc_handler, 6, 8) # Check that sample-size-related values match current_period = w3.eth.blockNumber // smc_handler.config['PERIOD_LENGTH'] notary_sample_size_updated_period = smc_handler.notary_sample_size_updated_period( ) assert notary_sample_size_updated_period == current_period current_period_notary_sample_size = smc_handler.current_period_notary_sample_size( ) assert current_period_notary_sample_size == 6 next_period_notary_sample_size = smc_handler.next_period_notary_sample_size( ) assert next_period_notary_sample_size == 9 # Fast forward to next period fast_forward(smc_handler, 1) current_period = w3.eth.blockNumber // smc_handler.config['PERIOD_LENGTH'] notary_sample_size_updated_period = smc_handler.notary_sample_size_updated_period( ) assert notary_sample_size_updated_period == current_period - 1 shard_0_committee_list = get_committee_list(smc_handler, 0) # Check that get_committee_list did generate committee list assert len(shard_0_committee_list) > 0 for (i, notary) in enumerate(shard_0_committee_list): assert smc_handler.get_member_of_committee(0, i) == notary
def test_committee_lists_generated_are_different(smc_handler): # noqa: F811 # Register notary 0~8 and fast forward to next period batch_register(smc_handler, 0, 8) fast_forward(smc_handler, 1) # Update notary sample size update_notary_sample_size(smc_handler) shard_0_committee_list = get_committee_list(smc_handler, 0) shard_1_committee_list = get_committee_list(smc_handler, 1) assert shard_0_committee_list != shard_1_committee_list # Fast forward to next period fast_forward(smc_handler, 1) # Update notary sample size update_notary_sample_size(smc_handler) new_shard_0_committee_list = get_committee_list(smc_handler, 0) assert new_shard_0_committee_list != shard_0_committee_list
def test_get_member_of_committee_with_non_member(smc_handler): # noqa: F811 # Register notary 0~8 and fast forward to next period batch_register(smc_handler, 0, 8) fast_forward(smc_handler, 1) # Update notary sample size update_notary_sample_size(smc_handler) notary_pool_list = get_notary_pool_list(smc_handler) shard_0_committee_list = get_committee_list(smc_handler, 0) for (i, notary) in enumerate(shard_0_committee_list): notary_index = notary_pool_list.index(notary) next_notary_index = notary_index + 1 \ if notary_index < len(notary_pool_list) - 1 else 0 next_notary = notary_pool_list[next_notary_index] assert not (smc_handler.get_member_of_committee(0, i) == next_notary)