def sample_attestation_data_params(sample_crosslink_record_params): return { "beacon_block_root": b"\x11" * 32, "source": Checkpoint(epoch=11, root=b"\x22" * 32), "target": Checkpoint(epoch=12, root=b"\x33" * 32), "crosslink": Crosslink(**sample_crosslink_record_params), }
def create_mock_slashable_attestation( state: BeaconState, config: Eth2Config, keymap: Dict[BLSPubkey, int], attestation_slot: Slot, ) -> IndexedAttestation: """ Create an `IndexedAttestation` that is signed by one attester. """ attester_index = ValidatorIndex(0) committee = (attester_index, ) shard = Shard(0) # Use genesis block root as `beacon_block_root`, only for tests. beacon_block_root = get_block_root_at_slot( state, attestation_slot, config.SLOTS_PER_HISTORICAL_ROOT) # Get `target_root` target_root = _get_target_root(state, config, beacon_block_root) # Get `source_root` source_root = get_block_root_at_slot( state, compute_start_slot_of_epoch(state.current_justified_checkpoint.epoch, config.SLOTS_PER_EPOCH), config.SLOTS_PER_HISTORICAL_ROOT, ) previous_crosslink = state.current_crosslinks[shard] attestation_data = AttestationData( beacon_block_root=beacon_block_root, source=Checkpoint(epoch=state.current_justified_checkpoint.epoch, root=source_root), target=Checkpoint( epoch=compute_epoch_of_slot(attestation_slot, config.SLOTS_PER_EPOCH), root=target_root, ), crosslink=previous_crosslink, ) message_hash = _get_mock_message(attestation_data) attesting_indices = _get_mock_attesting_indices(committee, num_voted_attesters=1) signature = sign_transaction( message_hash=message_hash, privkey=keymap[state.validators[attesting_indices[0]].pubkey], state=state, slot=attestation_slot, signature_domain=SignatureDomain.DOMAIN_ATTESTATION, slots_per_epoch=config.SLOTS_PER_EPOCH, ) validator_indices = tuple(committee[i] for i in attesting_indices) return IndexedAttestation( custody_bit_0_indices=validator_indices, custody_bit_1_indices=tuple(), data=attestation_data, signature=signature, )
def test_chaindb_get_justified_head( chaindb_at_genesis, genesis_block, genesis_state, sample_beacon_block_params, fork_choice_scoring, config, ): chaindb = chaindb_at_genesis block = BeaconBlock(**sample_beacon_block_params).copy( parent_root=genesis_block.signing_root) assert chaindb.get_finalized_head(genesis_block.__class__) == genesis_block assert chaindb.get_justified_head(genesis_block.__class__) == genesis_block # test that there is only one justified head per epoch state_with_bad_epoch = genesis_state.copy( current_justified_checkpoint=Checkpoint(root=block.signing_root, epoch=config.GENESIS_EPOCH)) chaindb.persist_state(state_with_bad_epoch) chaindb.persist_block(block, BeaconBlock, fork_choice_scoring) assert chaindb.get_finalized_head(genesis_block.__class__) == genesis_block assert chaindb.get_justified_head(genesis_block.__class__) == genesis_block # test that the we can update justified head if we satisfy the invariants state_with_justified_block = genesis_state.copy( current_justified_checkpoint=Checkpoint( root=block.signing_root, epoch=config.GENESIS_EPOCH + 1)) chaindb.persist_state(state_with_justified_block) assert chaindb.get_finalized_head(genesis_block.__class__) == genesis_block assert chaindb.get_justified_head( BeaconBlock).signing_root == block.signing_root
def test_validate_attestation_data( genesis_state, sample_attestation_data_params, attestation_source_epoch, attestation_target_epoch, current_epoch, previous_justified_epoch, current_justified_epoch, slots_per_epoch, config, is_valid, ): state = genesis_state.copy( slot=compute_start_slot_at_epoch(current_epoch, slots_per_epoch) + 5, previous_justified_checkpoint=Checkpoint( epoch=previous_justified_epoch), current_justified_checkpoint=Checkpoint(epoch=current_justified_epoch), ) target_slot = compute_start_slot_at_epoch(current_epoch, config.SLOTS_PER_EPOCH) committee_index = 0 attestation_data = AttestationData(**sample_attestation_data_params).copy( slot=target_slot, index=committee_index, source=Checkpoint(epoch=attestation_source_epoch), target=Checkpoint(epoch=attestation_target_epoch), ) if is_valid: _validate_attestation_data(state, attestation_data, config) else: with pytest.raises(ValidationError): _validate_attestation_data(state, attestation_data, config)
def test_get_matching_head_attestations(genesis_state, config): some_epoch = config.GENESIS_EPOCH + 20 some_slot = ( compute_start_slot_of_epoch(some_epoch, config.SLOTS_PER_EPOCH) + config.SLOTS_PER_EPOCH // 4) some_target_root = b"\x33" * 32 target_attestations = tuple((PendingAttestation(data=AttestationData( beacon_block_root=some_target_root, target=Checkpoint(epoch=some_epoch - 1), crosslink=Crosslink(shard=i), )) for i in range(3))) current_epoch_attestations = target_attestations + tuple( (PendingAttestation(data=AttestationData( beacon_block_root=b"\x44" * 32, target=Checkpoint(epoch=some_epoch - 1), )) for _ in range(3))) state = genesis_state.copy( slot=some_slot - 1, block_roots=tuple(some_target_root for _ in range(config.SLOTS_PER_HISTORICAL_ROOT)), current_epoch_attestations=current_epoch_attestations, ) attestations = get_matching_head_attestations(state, some_epoch, config) assert attestations == target_attestations
def sample_beacon_state_params( config, genesis_slot, genesis_epoch, sample_fork_params, sample_eth1_data_params, sample_block_header_params, sample_crosslink_record_params, ): return { # Versioning "genesis_time": 0, "slot": genesis_slot + 100, "fork": Fork(**sample_fork_params), # History "latest_block_header": BeaconBlockHeader(**sample_block_header_params), "block_roots": (ZERO_HASH32, ) * config.SLOTS_PER_HISTORICAL_ROOT, "state_roots": (ZERO_HASH32, ) * config.SLOTS_PER_HISTORICAL_ROOT, "historical_roots": (), # Eth1 "eth1_data": Eth1Data(**sample_eth1_data_params), "eth1_data_votes": (), "eth1_deposit_index": 0, # Registry "validators": (), "balances": (), # Shuffling "start_shard": 1, "randao_mixes": (ZERO_HASH32, ) * config.EPOCHS_PER_HISTORICAL_VECTOR, "active_index_roots": (ZERO_HASH32, ) * config.EPOCHS_PER_HISTORICAL_VECTOR, "compact_committees_roots": (ZERO_HASH32, ) * config.EPOCHS_PER_HISTORICAL_VECTOR, # Slashings "slashings": (0, ) * config.EPOCHS_PER_SLASHINGS_VECTOR, # Attestations "previous_epoch_attestations": (), "current_epoch_attestations": (), # Crosslinks "previous_crosslinks": ((Crosslink(**sample_crosslink_record_params), ) * config.SHARD_COUNT), "current_crosslinks": ((Crosslink(**sample_crosslink_record_params), ) * config.SHARD_COUNT), # Justification "justification_bits": (False, ) * JUSTIFICATION_BITS_LENGTH, "previous_justified_checkpoint": Checkpoint(epoch=0, root=b"\x99" * 32), "current_justified_checkpoint": Checkpoint(epoch=0, root=b"\x55" * 32), # Finality "finalized_checkpoint": Checkpoint(epoch=0, root=b"\x33" * 32), }
def create_mock_signed_attestations_at_slot( state: BeaconState, config: Eth2Config, state_machine: BaseBeaconStateMachine, attestation_slot: Slot, beacon_block_root: Hash32, keymap: Dict[BLSPubkey, int], voted_attesters_ratio: float = 1.0) -> Iterable[Attestation]: """ Create the mocking attestations of the given ``attestation_slot`` slot with ``keymap``. """ crosslink_committees_at_slot = get_crosslink_committees_at_slot( state, attestation_slot, config, ) # Get `target_root` target_root = _get_target_root(state, config, beacon_block_root) target_epoch = compute_epoch_of_slot( state.slot, config.SLOTS_PER_EPOCH, ) for crosslink_committee in crosslink_committees_at_slot: committee, shard = crosslink_committee parent_crosslink = state.current_crosslinks[shard] attestation_data = AttestationData( beacon_block_root=beacon_block_root, source=Checkpoint( epoch=state.current_justified_checkpoint.epoch, root=state.current_justified_checkpoint.root, ), target=Checkpoint( root=target_root, epoch=target_epoch, ), crosslink=Crosslink( shard=shard, parent_root=parent_crosslink.hash_tree_root, start_epoch=parent_crosslink.end_epoch, end_epoch=min( target_epoch, parent_crosslink.end_epoch + config.MAX_EPOCHS_PER_CROSSLINK), )) num_voted_attesters = int(len(committee) * voted_attesters_ratio) yield _create_mock_signed_attestation( state, attestation_data, attestation_slot, committee, num_voted_attesters, keymap, config.SLOTS_PER_EPOCH, )
def sample_attestation_data_params(): return { "slot": 5, "index": 1, "beacon_block_root": b"\x11" * 32, "source": Checkpoint(epoch=11, root=b"\x22" * 32), "target": Checkpoint(epoch=12, root=b"\x33" * 32), }
def test_validate_attestation_data(genesis_state, sample_attestation_data_params, attestation_source_epoch, attestation_target_epoch, current_epoch, previous_justified_epoch, current_justified_epoch, slots_per_epoch, config, is_valid): state = genesis_state.copy( slot=compute_start_slot_of_epoch(current_epoch, slots_per_epoch) + 5, previous_justified_checkpoint=Checkpoint( epoch=previous_justified_epoch, ), current_justified_checkpoint=Checkpoint( epoch=current_justified_epoch, ), ) start_shard = get_start_shard( state, current_epoch, CommitteeConfig(config), ) if attestation_target_epoch == current_epoch: crosslinks = state.current_crosslinks else: crosslinks = state.previous_crosslinks parent_crosslink = crosslinks[start_shard] attestation_data = AttestationData(**sample_attestation_data_params).copy( source=Checkpoint( epoch=attestation_source_epoch, ), target=Checkpoint( epoch=attestation_target_epoch, ), crosslink=Crosslink( start_epoch=parent_crosslink.end_epoch, end_epoch=attestation_target_epoch, parent_root=parent_crosslink.hash_tree_root, shard=start_shard, ), ) if is_valid: _validate_attestation_data( state, attestation_data, config, ) else: with pytest.raises(ValidationError): _validate_attestation_data( state, attestation_data, config, )
def create_signed_attestation_at_slot(state: BeaconState, config: Eth2Config, state_machine: BaseBeaconStateMachine, attestation_slot: Slot, beacon_block_root: Hash32, validator_privkeys: Dict[ValidatorIndex, int], committee: Tuple[ValidatorIndex, ...], shard: Shard) -> Attestation: """ Create the attestations of the given ``attestation_slot`` slot with ``validator_privkeys``. """ state_transition = state_machine.state_transition state = state_transition.apply_state_transition( state, future_slot=attestation_slot, ) target_epoch = compute_epoch_of_slot( attestation_slot, config.SLOTS_PER_EPOCH, ) target_root = _get_target_root(state, config, beacon_block_root) parent_crosslink = state.current_crosslinks[shard] attestation_data = AttestationData( beacon_block_root=beacon_block_root, source=Checkpoint( epoch=state.current_justified_checkpoint.epoch, root=state.current_justified_checkpoint.root, ), target=Checkpoint( root=target_root, epoch=target_epoch, ), crosslink=Crosslink( shard=shard, parent_root=parent_crosslink.hash_tree_root, start_epoch=parent_crosslink.end_epoch, end_epoch=target_epoch, )) return _create_mock_signed_attestation( state, attestation_data, attestation_slot, committee, len(committee), keymapper(lambda index: state.validators[index].pubkey, validator_privkeys), config.SLOTS_PER_EPOCH, )
def sample_attestation_data_params(sample_crosslink_record_params): return { 'beacon_block_root': b'\x11' * 32, 'source': Checkpoint( epoch=11, root=b'\x22' * 32, ), 'target': Checkpoint( epoch=12, root=b'\x33' * 32, ), 'crosslink': Crosslink(**sample_crosslink_record_params), }
def test_process_justification_and_finalization( genesis_state, current_epoch, current_epoch_justifiable, previous_epoch_justifiable, previous_justified_epoch, current_justified_epoch, justification_bits, finalized_epoch, justified_epoch_after, justification_bits_after, finalized_epoch_after, config, ): justification_bits = _convert_to_bitfield(justification_bits) justification_bits_after = _convert_to_bitfield(justification_bits_after) previous_epoch = max(current_epoch - 1, 0) slot = (current_epoch + 1) * config.SLOTS_PER_EPOCH - 1 state = genesis_state.copy( slot=slot, previous_justified_checkpoint=Checkpoint(epoch=previous_justified_epoch), current_justified_checkpoint=Checkpoint(epoch=current_justified_epoch), justification_bits=justification_bits, finalized_checkpoint=Checkpoint(epoch=finalized_epoch), block_roots=tuple( i.to_bytes(32, "little") for i in range(config.SLOTS_PER_HISTORICAL_ROOT) ), ) if previous_epoch_justifiable: attestations = mk_all_pending_attestations_with_full_participation_in_epoch( state, previous_epoch, config ) state = state.copy(previous_epoch_attestations=attestations) if current_epoch_justifiable: attestations = mk_all_pending_attestations_with_full_participation_in_epoch( state, current_epoch, config ) state = state.copy(current_epoch_attestations=attestations) post_state = process_justification_and_finalization(state, config) assert ( post_state.previous_justified_checkpoint.epoch == state.current_justified_checkpoint.epoch ) assert post_state.current_justified_checkpoint.epoch == justified_epoch_after assert post_state.justification_bits == justification_bits_after assert post_state.finalized_checkpoint.epoch == finalized_epoch_after
def create_mock_signed_attestations_at_slot( state: BeaconState, config: Eth2Config, state_machine: BaseBeaconStateMachine, attestation_slot: Slot, beacon_block_root: SigningRoot, keymap: Dict[BLSPubkey, int], voted_attesters_ratio: float = 1.0, ) -> Iterable[Attestation]: """ Create the mocking attestations of the given ``attestation_slot`` slot with ``keymap``. """ committees_per_slot = get_committee_count_at_slot( state, attestation_slot, config.MAX_COMMITTEES_PER_SLOT, config.SLOTS_PER_EPOCH, config.TARGET_COMMITTEE_SIZE, ) # Get `target_root` target_root = _get_target_root(state, config, beacon_block_root) target_epoch = compute_epoch_at_slot(state.slot, config.SLOTS_PER_EPOCH) for committee, committee_index, _ in iterate_committees_at_slot( state, attestation_slot, committees_per_slot, CommitteeConfig(config)): attestation_data = AttestationData( slot=attestation_slot, index=CommitteeIndex(committee_index), beacon_block_root=beacon_block_root, source=Checkpoint( epoch=state.current_justified_checkpoint.epoch, root=state.current_justified_checkpoint.root, ), target=Checkpoint(root=target_root, epoch=target_epoch), ) num_voted_attesters = int(len(committee) * voted_attesters_ratio) yield _create_mock_signed_attestation( state, attestation_data, attestation_slot, committee, num_voted_attesters, keymap, config.SLOTS_PER_EPOCH, )
def _find_collision(state, config, validator_index, epoch, block_producer): """ Given a target epoch, make the attestation expected for the validator w/ the given ``validator_index``. """ for committee, committee_index, slot in iterate_committees_at_epoch( state, epoch, config ): if validator_index in committee: # TODO(ralexstokes) refactor w/ tools/builder block = block_producer(slot) root = block.signing_root attestation_data = AttestationData( slot=slot, index=committee_index, target=Checkpoint(epoch=epoch, root=root), beacon_block_root=root, ) committee_count = len(committee) aggregation_bits = bitfield.get_empty_bitfield(committee_count) for i in range(committee_count): aggregation_bits = bitfield.set_voted(aggregation_bits, i) return { index: (slot, (aggregation_bits, attestation_data)) for index in committee } else: raise Exception("should have found a duplicate validator")
def _determine_new_finalized_checkpoint( state: BeaconState, justification_bits: Bitfield, config: Eth2Config ) -> Checkpoint: current_epoch = state.current_epoch(config.SLOTS_PER_EPOCH) new_finalized_epoch = _determine_new_finalized_epoch( state.finalized_checkpoint.epoch, state.previous_justified_checkpoint.epoch, state.current_justified_checkpoint.epoch, current_epoch, justification_bits, ) if new_finalized_epoch != state.finalized_checkpoint.epoch: # NOTE: we only want to call ``get_block_root`` # upon some change, not unconditionally # Given the way it reads the block roots, it can cause # validation problems with some configurations, esp. in testing. # This is implicitly happening above for the justified roots. new_finalized_root = get_block_root( state, new_finalized_epoch, config.SLOTS_PER_EPOCH, config.SLOTS_PER_HISTORICAL_ROOT, ) else: new_finalized_root = state.finalized_checkpoint.root return Checkpoint(epoch=new_finalized_epoch, root=new_finalized_root)
def _determine_new_justified_checkpoint_and_bitfield( state: BeaconState, config: Eth2Config) -> Tuple[Checkpoint, Bitfield]: ( new_current_justified_epoch, justification_bits, ) = _determine_new_justified_epoch_and_bitfield( state, config, ) if new_current_justified_epoch != state.current_justified_checkpoint.epoch: new_current_justified_root = get_block_root( state, new_current_justified_epoch, config.SLOTS_PER_EPOCH, config.SLOTS_PER_HISTORICAL_ROOT, ) else: new_current_justified_root = state.current_justified_checkpoint.root return ( Checkpoint( epoch=new_current_justified_epoch, root=new_current_justified_root, ), justification_bits, )
def _mk_attestation_inputs_in_epoch(epoch, block_producer, state, config): for committee, committee_index, slot in iterate_committees_at_epoch( state, epoch, config ): if not committee: # empty committee this slot continue block = block_producer(slot) root = block.signing_root attestation_data = AttestationData( slot=slot, index=committee_index, target=Checkpoint(epoch=epoch, root=root), beacon_block_root=root, ) committee_size = len(committee) aggregation_bits = bitfield.get_empty_bitfield(committee_size) for index in range(committee_size): aggregation_bits = bitfield.set_voted(aggregation_bits, index) for index in committee: yield ( index, (attestation_data.slot, (aggregation_bits, attestation_data)), )
def test_get_unslashed_attesting_indices(genesis_state, config): state = genesis_state.copy( slot=compute_start_slot_of_epoch(3, config.SLOTS_PER_EPOCH)) target_epoch = state.current_epoch(config.SLOTS_PER_EPOCH) target_shard = (state.start_shard + 3) % config.SHARD_COUNT some_committee = get_crosslink_committee(state, target_epoch, target_shard, CommitteeConfig(config)) data = AttestationData(target=Checkpoint(epoch=target_epoch), crosslink=Crosslink(shard=target_shard)) some_subset_count = random.randrange(1, len(some_committee) // 2) some_subset = random.sample(some_committee, some_subset_count) bitfield = get_empty_bitfield(len(some_committee)) for i, index in enumerate(some_committee): if index in some_subset: if random.choice([True, False]): state = state.update_validator_with_fn( index, lambda v, *_: v.copy(slashed=True)) bitfield = set_voted(bitfield, i) some_subset = tuple( filter(lambda index: not state.validators[index].slashed, some_subset)) indices = get_unslashed_attesting_indices( state, (PendingAttestation(data=data, aggregation_bits=bitfield), ), CommitteeConfig(config), ) assert set(indices) == set(some_subset) assert len(indices) == len(some_subset)
def create_signed_attestation_at_slot( state: BeaconState, config: Eth2Config, state_machine: BaseBeaconStateMachine, attestation_slot: Slot, beacon_block_root: SigningRoot, validator_privkeys: Dict[ValidatorIndex, int], committee: Tuple[ValidatorIndex, ...], committee_index: CommitteeIndex, attesting_indices: Sequence[CommitteeValidatorIndex], ) -> Attestation: """ Create the attestations of the given ``attestation_slot`` slot with ``validator_privkeys``. """ state_transition = state_machine.state_transition state = state_transition.apply_state_transition( state, future_slot=attestation_slot) target_epoch = compute_epoch_at_slot(attestation_slot, config.SLOTS_PER_EPOCH) target_root = _get_target_root(state, config, beacon_block_root) attestation_data = AttestationData( slot=attestation_slot, index=committee_index, beacon_block_root=beacon_block_root, source=Checkpoint( epoch=state.current_justified_checkpoint.epoch, root=state.current_justified_checkpoint.root, ), target=Checkpoint(root=target_root, epoch=target_epoch), ) return _create_mock_signed_attestation( state, attestation_data, attestation_slot, committee, len(committee), keymapper(lambda index: state.validators[index].pubkey, validator_privkeys), config.SLOTS_PER_EPOCH, is_for_simulation=False, attesting_indices=attesting_indices, )
def _mk_attestation_inputs_in_epoch(epoch, state, config): active_validators_indices = get_active_validator_indices(state.validators, epoch) epoch_committee_count = get_committee_count( len(active_validators_indices), config.SHARD_COUNT, config.SLOTS_PER_EPOCH, config.TARGET_COMMITTEE_SIZE, ) epoch_start_shard = get_start_shard( state, epoch, CommitteeConfig(config), ) for shard_offset in random.sample(range(epoch_committee_count), epoch_committee_count): shard = Shard((epoch_start_shard + shard_offset) % config.SHARD_COUNT) committee = get_crosslink_committee( state, epoch, shard, CommitteeConfig(config), ) if not committee: # empty crosslink committee this epoch continue attestation_data = AttestationData( target=Checkpoint( epoch=epoch, ), crosslink=Crosslink( shard=shard, ), ) committee_count = len(committee) aggregation_bits = bitfield.get_empty_bitfield(committee_count) for index in range(committee_count): aggregation_bits = bitfield.set_voted(aggregation_bits, index) for index in committee: yield ( index, ( get_attestation_data_slot( state, attestation_data, config, ), ( aggregation_bits, attestation_data, ), ), )
def test_get_matching_target_attestations(genesis_state, config): some_epoch = config.GENESIS_EPOCH + 20 some_slot = compute_start_slot_of_epoch(some_epoch, config.SLOTS_PER_EPOCH) some_target_root = b"\x33" * 32 target_attestations = tuple((PendingAttestation(data=AttestationData( target=Checkpoint(root=some_target_root))) for _ in range(3))) current_epoch_attestations = target_attestations + tuple( (PendingAttestation(data=AttestationData(target=Checkpoint( root=b"\x44" * 32))) for _ in range(3))) state = genesis_state.copy( slot=some_slot + 1, block_roots=update_tuple_item( genesis_state.block_roots, some_slot % config.SLOTS_PER_HISTORICAL_ROOT, some_target_root, ), current_epoch_attestations=current_epoch_attestations, ) attestations = get_matching_target_attestations(state, some_epoch, config) assert attestations == target_attestations
def _mk_pending_attestation( bitfield: Bitfield = default_bitfield, target_root: SigningRoot = ZERO_SIGNING_ROOT, target_epoch: Epoch = default_epoch, slot: Slot = default_slot, committee_index: CommitteeIndex = default_committee_index, ) -> PendingAttestation: return PendingAttestation( aggregation_bits=bitfield, data=AttestationData( slot=slot, index=committee_index, target=Checkpoint(epoch=target_epoch, root=target_root), ), )
def _mk_attestation_for_block_with_committee(block, committee, shard, config): committee_count = len(committee) aggregation_bits = bitfield.get_empty_bitfield(committee_count) for index in range(committee_count): aggregation_bits = bitfield.set_voted(aggregation_bits, index) attestation = Attestation( aggregation_bits=aggregation_bits, data=AttestationData( beacon_block_root=block.signing_root, target=Checkpoint(epoch=compute_epoch_of_slot( block.slot, config.SLOTS_PER_EPOCH)), crosslink=Crosslink(shard=shard), ), ) return attestation
def _find_collision(state, config, index, epoch): """ Given a target epoch, make the attestation expected for the validator w/ the given index. """ active_validators = get_active_validator_indices(state.validators, epoch) committees_per_slot = get_committee_count( len(active_validators), config.SHARD_COUNT, config.SLOTS_PER_EPOCH, config.TARGET_COMMITTEE_SIZE, ) // config.SLOTS_PER_EPOCH epoch_start_slot = compute_start_slot_of_epoch( epoch, config.SLOTS_PER_EPOCH, ) epoch_start_shard = get_start_shard(state, epoch, CommitteeConfig(config)) for slot in range(epoch_start_slot, epoch_start_slot + config.SLOTS_PER_EPOCH): offset = committees_per_slot * (slot % config.SLOTS_PER_EPOCH) slot_start_shard = (epoch_start_shard + offset) % config.SHARD_COUNT for i in range(committees_per_slot): shard = Shard((slot_start_shard + i) % config.SHARD_COUNT) committee = get_crosslink_committee(state, epoch, shard, CommitteeConfig(config)) if index in committee: # TODO(ralexstokes) refactor w/ tools/builder attestation_data = AttestationData( target=Checkpoint( epoch=epoch, ), crosslink=Crosslink( shard=shard, ), ) committee_count = len(committee) aggregation_bits = bitfield.get_empty_bitfield(committee_count) for i in range(committee_count): aggregation_bits = bitfield.set_voted(aggregation_bits, i) return { index: ( slot, (aggregation_bits, attestation_data) ) for index in committee } else: raise Exception("should have found a duplicate validator")
def test_chaindb_get_finalized_head( chaindb_at_genesis, genesis_block, genesis_state, sample_beacon_block_params, fork_choice_scoring, ): chaindb = chaindb_at_genesis block = BeaconBlock(**sample_beacon_block_params).copy( parent_root=genesis_block.signing_root) assert chaindb.get_finalized_head(genesis_block.__class__) == genesis_block assert chaindb.get_justified_head(genesis_block.__class__) == genesis_block state_with_finalized_block = genesis_state.copy( finalized_checkpoint=Checkpoint(root=block.signing_root)) chaindb.persist_state(state_with_finalized_block) chaindb.persist_block(block, BeaconBlock, fork_choice_scoring) assert chaindb.get_finalized_head( BeaconBlock).signing_root == block.signing_root assert chaindb.get_justified_head(genesis_block.__class__) == genesis_block
def _mk_pending_attestation( bitfield: Bitfield = default_bitfield, target_root: Hash32 = ZERO_HASH32, target_epoch: Epoch = default_epoch, shard: Shard = default_shard, start_epoch: Epoch = default_epoch, parent_root: Hash32 = ZERO_HASH32, data_root: Hash32 = ZERO_HASH32, ) -> PendingAttestation: return PendingAttestation( aggregation_bits=bitfield, data=AttestationData( target=Checkpoint(epoch=target_epoch, root=target_root), crosslink=Crosslink( shard=shard, parent_root=parent_root, start_epoch=start_epoch, end_epoch=target_epoch, data_root=data_root, ), ), )
def test_get_attesting_indices(genesis_state, config): state = genesis_state.copy( slot=compute_start_slot_of_epoch(3, config.SLOTS_PER_EPOCH)) target_epoch = state.current_epoch(config.SLOTS_PER_EPOCH) target_shard = (state.start_shard + 3) % config.SHARD_COUNT some_committee = get_crosslink_committee(state, target_epoch, target_shard, CommitteeConfig(config)) data = AttestationData(target=Checkpoint(epoch=target_epoch), crosslink=Crosslink(shard=target_shard)) some_subset_count = random.randrange(1, len(some_committee) // 2) some_subset = random.sample(some_committee, some_subset_count) bitfield = get_empty_bitfield(len(some_committee)) for i, index in enumerate(some_committee): if index in some_subset: bitfield = set_voted(bitfield, i) indices = get_attesting_indices(state, data, bitfield, CommitteeConfig(config)) assert set(indices) == set(some_subset) assert len(indices) == len(some_subset)
def sample_beacon_state_params(config, genesis_slot, genesis_epoch, sample_fork_params, sample_eth1_data_params, sample_block_header_params, sample_crosslink_record_params): return { # Versioning 'genesis_time': 0, 'slot': genesis_slot + 100, 'fork': Fork(**sample_fork_params), # History 'latest_block_header': BeaconBlockHeader(**sample_block_header_params), 'block_roots': (ZERO_HASH32, ) * config.SLOTS_PER_HISTORICAL_ROOT, 'state_roots': (ZERO_HASH32, ) * config.SLOTS_PER_HISTORICAL_ROOT, 'historical_roots': (), # Eth1 'eth1_data': Eth1Data(**sample_eth1_data_params), 'eth1_data_votes': (), 'eth1_deposit_index': 0, # Registry 'validators': (), 'balances': (), # Shuffling 'start_shard': 1, 'randao_mixes': (ZERO_HASH32, ) * config.EPOCHS_PER_HISTORICAL_VECTOR, 'active_index_roots': (ZERO_HASH32, ) * config.EPOCHS_PER_HISTORICAL_VECTOR, 'compact_committees_roots': (ZERO_HASH32, ) * config.EPOCHS_PER_HISTORICAL_VECTOR, # Slashings 'slashings': (0, ) * config.EPOCHS_PER_SLASHINGS_VECTOR, # Attestations 'previous_epoch_attestations': (), 'current_epoch_attestations': (), # Crosslinks 'previous_crosslinks': ((Crosslink(**sample_crosslink_record_params), ) * config.SHARD_COUNT), 'current_crosslinks': ((Crosslink(**sample_crosslink_record_params), ) * config.SHARD_COUNT), # Justification 'justification_bits': (False, ) * JUSTIFICATION_BITS_LENGTH, 'previous_justified_checkpoint': Checkpoint( epoch=0, root=b'\x99' * 32, ), 'current_justified_checkpoint': Checkpoint( epoch=0, root=b'\x55' * 32, ), # Finality 'finalized_checkpoint': Checkpoint( epoch=0, root=b'\x33' * 32, ) }
def test_process_rewards_and_penalties_for_crosslinks( genesis_state, config, slots_per_epoch, target_committee_size, shard_count, current_slot, num_attesting_validators, max_effective_balance, min_attestation_inclusion_delay, sample_attestation_data_params, sample_pending_attestation_record_params): state = genesis_state.copy(slot=current_slot, ) previous_epoch = state.previous_epoch(config.SLOTS_PER_EPOCH, config.GENESIS_EPOCH) prev_epoch_start_slot = compute_start_slot_of_epoch( previous_epoch, slots_per_epoch) prev_epoch_crosslink_committees = [ get_crosslink_committees_at_slot( state, slot, CommitteeConfig(config), )[0] for slot in range(prev_epoch_start_slot, prev_epoch_start_slot + slots_per_epoch) ] # Record which validators attest during each slot for reward collation. each_slot_attestion_validators_list = [] epoch_start_shard = get_start_shard( state, previous_epoch, CommitteeConfig(config), ) shard_delta = get_shard_delta( state, previous_epoch, CommitteeConfig(config), ) a = epoch_start_shard b = epoch_start_shard + shard_delta if a > b: valid_shards_for_epoch = range(b, a) else: valid_shards_for_epoch = range(a, b) indices_to_check = set() previous_epoch_attestations = [] for committee, shard in prev_epoch_crosslink_committees: if shard not in valid_shards_for_epoch: continue for index in committee: indices_to_check.add(index) # Randomly sample `num_attesting_validators` validators # from the committee to attest in this slot. crosslink_attesting_validators = random.sample( committee, num_attesting_validators, ) each_slot_attestion_validators_list.append( crosslink_attesting_validators) participants_bitfield = get_empty_bitfield(len(committee)) for index in crosslink_attesting_validators: participants_bitfield = set_voted(participants_bitfield, committee.index(index)) previous_epoch_attestations.append( PendingAttestation( **sample_pending_attestation_record_params).copy( aggregation_bits=participants_bitfield, data=AttestationData( **sample_attestation_data_params).copy( target=Checkpoint(epoch=previous_epoch, ), crosslink=Crosslink( shard=shard, parent_root=Crosslink().hash_tree_root, ), ), )) state = state.copy( previous_epoch_attestations=tuple(previous_epoch_attestations), ) rewards_received, penalties_received = get_crosslink_deltas( state, config, ) expected_rewards_received = { index: 0 for index in range(len(state.validators)) } validator_balance = max_effective_balance for i in range(slots_per_epoch): crosslink_committee, shard = prev_epoch_crosslink_committees[i] if shard not in valid_shards_for_epoch: continue attesting_validators = each_slot_attestion_validators_list[i] total_attesting_balance = len(attesting_validators) * validator_balance total_committee_balance = len(crosslink_committee) * validator_balance for index in crosslink_committee: if index in attesting_validators: reward = get_base_reward( state=state, index=index, config=config, ) * total_attesting_balance // total_committee_balance expected_rewards_received[index] += reward else: penalty = get_base_reward( state=state, index=index, config=config, ) expected_rewards_received[index] -= penalty # Check the rewards/penalties match for index in range(len(state.validators)): if index not in indices_to_check: continue assert (rewards_received[index] - penalties_received[index] == expected_rewards_received[index])
def test_get_attestation_deltas(genesis_state, config, slots_per_epoch, target_committee_size, shard_count, min_attestation_inclusion_delay, inactivity_penalty_quotient, finalized_epoch, current_slot, sample_pending_attestation_record_params, sample_attestation_data_params): state = genesis_state.copy(slot=current_slot, finalized_checkpoint=Checkpoint( epoch=finalized_epoch, )) previous_epoch = state.previous_epoch(config.SLOTS_PER_EPOCH, config.GENESIS_EPOCH) epoch_start_shard = get_start_shard( state, previous_epoch, CommitteeConfig(config), ) shard_delta = get_shard_delta( state, previous_epoch, CommitteeConfig(config), ) a = epoch_start_shard b = epoch_start_shard + shard_delta if a > b: valid_shards_for_epoch = range(b, a) else: valid_shards_for_epoch = range(a, b) indices_to_check = set() prev_epoch_start_slot = compute_start_slot_of_epoch( previous_epoch, slots_per_epoch) prev_epoch_attestations = tuple() for slot in range(prev_epoch_start_slot, prev_epoch_start_slot + slots_per_epoch): committee, shard = get_crosslink_committees_at_slot( state, slot, CommitteeConfig(config), )[0] if not committee: continue if shard not in valid_shards_for_epoch: continue participants_bitfield = get_empty_bitfield(len(committee)) for i, index in enumerate(committee): indices_to_check.add(index) participants_bitfield = set_voted(participants_bitfield, i) prev_epoch_attestations += (PendingAttestation( **sample_pending_attestation_record_params).copy( aggregation_bits=participants_bitfield, inclusion_delay=min_attestation_inclusion_delay, proposer_index=get_beacon_proposer_index( state.copy(slot=slot, ), CommitteeConfig(config), ), data=AttestationData(**sample_attestation_data_params).copy( crosslink=Crosslink(shard=shard, ), target=Checkpoint( epoch=previous_epoch, root=get_block_root( state, previous_epoch, config.SLOTS_PER_EPOCH, config.SLOTS_PER_HISTORICAL_ROOT, ), ), beacon_block_root=get_block_root_at_slot( state, slot, config.SLOTS_PER_HISTORICAL_ROOT, ), ), ), ) state = state.copy(previous_epoch_attestations=prev_epoch_attestations, ) rewards_received, penalties_received = get_attestation_deltas( state, config, ) # everyone attested, no penalties assert (sum(penalties_received) == 0) the_reward = rewards_received[0] # everyone performed the same, equal rewards assert (sum(rewards_received) // len(rewards_received) == the_reward)