Beispiel #1
0
    def attest_proposed_block(cls, post_crystallized_state: CrystallizedState,
                              post_active_state: ActiveState,
                              block_proposal: 'BlockProposal',
                              chaindb: BaseBeaconChainDB,
                              private_key: int) -> 'AttestationRecord':
        """
        Return the initial attestation by the block proposer.

        The proposer broadcasts their attestation with the proposed block.
        """
        block_committees_info = get_block_committees_info(
            block_proposal.block,
            post_crystallized_state,
            cls.config.CYCLE_LENGTH,
        )
        # Vote
        attester_bitfield = set_voted(
            get_empty_bitfield(block_committees_info.proposer_committee_size),
            block_committees_info.proposer_index_in_committee,
        )

        # Get justified_slot and justified_block_hash
        justified_slot = post_crystallized_state.last_justified_slot
        justified_block_hash = chaindb.get_canonical_block_hash_by_slot(
            justified_slot)

        # Get signing message and sign it
        parent_hashes = get_hashes_to_sign(
            post_active_state.recent_block_hashes,
            block_proposal.block,
            cls.config.CYCLE_LENGTH,
        )

        message = create_signing_message(
            block_proposal.block.slot_number,
            parent_hashes,
            block_proposal.shard_id,
            block_proposal.shard_block_hash,
            justified_slot,
        )
        sig = bls.sign(
            message,
            private_key,
        )

        return cls.get_attestation_record_class()(
            slot=block_proposal.block.slot_number,
            shard_id=block_proposal.shard_id,
            oblique_parent_hashes=(),
            shard_block_hash=block_proposal.shard_block_hash,
            attester_bitfield=attester_bitfield,
            justified_slot=justified_slot,
            justified_block_hash=justified_block_hash,
            aggregate_sig=sig,
        )
Beispiel #2
0
def test_get_hashes_to_sign(genesis_block, beacon_config):
    cycle_length = beacon_config.cycle_length
    current_block_slot_number = 1
    blocks, recent_block_hashes = generate_mock_recent_block_hashes(
        genesis_block,
        current_block_slot_number,
        cycle_length,
    )

    block = blocks[current_block_slot_number]
    result = get_hashes_to_sign(
        recent_block_hashes,
        block,
        cycle_length,
    )
    assert len(result) == cycle_length
    assert result[-1] == blake(block.hash)