def test_verify_slashable_vote_data_signature(num_validators, privkeys, sample_beacon_state_params, activated_genesis_validators, genesis_balances, sample_slashable_vote_data_params, sample_fork_data_params): state = BeaconState(**sample_beacon_state_params).copy( validator_registry=activated_genesis_validators, validator_balances=genesis_balances, fork_data=ForkData(**sample_fork_data_params), ) # NOTE: we can do this before "correcting" the params as they # touch disjoint subsets of the provided params messages = _create_slashable_vote_data_messages(sample_slashable_vote_data_params) valid_params = _correct_slashable_vote_data_params( num_validators, sample_slashable_vote_data_params, messages, privkeys, state.fork_data, ) valid_votes = SlashableVoteData(**valid_params) assert verify_slashable_vote_data_signature(state, valid_votes) invalid_params = _corrupt_signature(valid_params, state.fork_data) invalid_votes = SlashableVoteData(**invalid_params) assert not verify_slashable_vote_data_signature(state, invalid_votes)
def _run_verify_slashable_vote(params, state, max_casper_votes, should_succeed): votes = SlashableVoteData(**params) result = verify_slashable_vote_data(state, votes, max_casper_votes) if should_succeed: assert result else: assert not result
def test_generate_aggregate_pubkeys(activated_genesis_validators, sample_slashable_vote_data_params, data): max_value_for_list = len(activated_genesis_validators) - 1 (indices, some_index) = _list_and_index( data, elements=st.integers( min_value=0, max_value=max_value_for_list, ) ) custody_bit_0_indices = indices[:some_index] custody_bit_1_indices = indices[some_index:] key = "custody_bit_0_indices" sample_slashable_vote_data_params[key] = custody_bit_0_indices key = "custody_bit_1_indices" sample_slashable_vote_data_params[key] = custody_bit_1_indices votes = SlashableVoteData(**sample_slashable_vote_data_params) keys = generate_aggregate_pubkeys(activated_genesis_validators, votes) assert len(keys) == 2 (poc_0_key, poc_1_key) = keys poc_0_keys = get_pubkey_for_indices(activated_genesis_validators, custody_bit_0_indices) poc_1_keys = get_pubkey_for_indices(activated_genesis_validators, custody_bit_1_indices) assert bls.aggregate_pubkeys(poc_0_keys) == poc_0_key assert bls.aggregate_pubkeys(poc_1_keys) == poc_1_key
def test_messages(sample_slashable_vote_data_params): vote_data = SlashableVoteData(**sample_slashable_vote_data_params) assert vote_data.messages == ( AttestationDataAndCustodyBit(vote_data.data, False).root, AttestationDataAndCustodyBit(vote_data.data, True).root, )
def test_root(sample_slashable_vote_data_params): vote_data = SlashableVoteData(**sample_slashable_vote_data_params) # NOTE: see note in `test_hash`, this test will need to be updated # once ssz tree hash lands... assert vote_data.root == vote_data.hash
def test_defaults(sample_slashable_vote_data_params): vote_data = SlashableVoteData(**sample_slashable_vote_data_params) assert (vote_data.custody_bit_0_indices == sample_slashable_vote_data_params['custody_bit_0_indices']) assert (vote_data.custody_bit_1_indices == sample_slashable_vote_data_params['custody_bit_1_indices']) assert vote_data.data == sample_slashable_vote_data_params['data'] assert vote_data.aggregate_signature == sample_slashable_vote_data_params[ 'aggregate_signature']
def test_vote_count(sample_slashable_vote_data_params): vote_data = SlashableVoteData(**sample_slashable_vote_data_params) key = "custody_bit_0_indices" custody_bit_0_indices = sample_slashable_vote_data_params[key] key = "custody_bit_1_indices" custody_bit_1_indices = sample_slashable_vote_data_params[key] assert vote_data.vote_count == len(custody_bit_0_indices) + len( custody_bit_1_indices)
def test_hash(sample_slashable_vote_data_params): vote_data = SlashableVoteData(**sample_slashable_vote_data_params) # NOTE: this hash was simply copied from the existing implementation # which should be the keccak-256 of the rlp serialization of `votes`. # Given that this value will change soon (cf. ssz tree hash), we just # do this to get the test passing for now and will need to update later # if we expect the hash computation is not working correctly hash_hex = "7e4b4cf3ac47988865d693a29b6aa5a825f27e065cf21a80af5e077ea102e297" assert vote_data.hash == bytes.fromhex(hash_hex)
def test_verify_vote_count(max_casper_votes, sample_slashable_vote_data_params, data): (indices, some_index) = _list_and_index(data, max_size=max_casper_votes) custody_bit_0_indices = indices[:some_index] custody_bit_1_indices = indices[some_index:] key = "custody_bit_0_indices" sample_slashable_vote_data_params[key] = custody_bit_0_indices key = "custody_bit_1_indices" sample_slashable_vote_data_params[key] = custody_bit_1_indices votes = SlashableVoteData(**sample_slashable_vote_data_params) assert verify_vote_count(votes, max_casper_votes)
def sample_casper_slashing_params(sample_slashable_vote_data_params): vote_data = SlashableVoteData(**sample_slashable_vote_data_params) return { 'slashable_vote_data_1': vote_data, 'slashable_vote_data_2': vote_data, }
def _create_slashable_vote_data_messages(params): # TODO update when we move to `ssz` tree hash votes = SlashableVoteData(**params) return votes.messages