Esempio n. 1
0
def test_update_attestations(sample_attestation_params, sample_beacon_block_params):
    block = BeaconBlock(**sample_beacon_block_params)
    attestations = block.body.attestations
    attestations = list(attestations)
    attestations.append(Attestation(**sample_attestation_params))
    body2 = block.body.copy(attestations=attestations)
    block2 = block.copy(body=body2)
    assert len(block2.body.attestations) == 1
Esempio n. 2
0
async def test_get_blocks_from_fork_chain_by_root(
    monkeypatch, fork_chain_block_slots, slot_of_requested_blocks, expected_block_slots
):
    node = NodeFactory()

    mock_block = BeaconBlock(
        slot=0,
        parent_root=ZERO_HASH32,
        state_root=ZERO_HASH32,
        signature=EMPTY_SIGNATURE,
        body=BeaconBlockBody(),
    )

    # Mock up fork chain block database
    fork_chain_blocks = []
    for slot in fork_chain_block_slots:
        if len(fork_chain_blocks) == 0:
            fork_chain_blocks.append(mock_block.copy(slot=slot))
        else:
            fork_chain_blocks.append(
                mock_block.copy(
                    slot=slot, parent_root=fork_chain_blocks[-1].signing_root
                )
            )
    mock_root_to_block_db = {block.signing_root: block for block in fork_chain_blocks}

    def get_block_by_root(root):
        if root in mock_root_to_block_db:
            return mock_root_to_block_db[root]
        else:
            raise BlockNotFound

    monkeypatch.setattr(node.chain, "get_block_by_root", get_block_by_root)

    requested_blocks = node._get_blocks_from_fork_chain_by_root(
        start_slot=slot_of_requested_blocks[0],
        peer_head_block=fork_chain_blocks[-1],
        slot_of_requested_blocks=slot_of_requested_blocks,
    )

    expected_blocks = [
        block for block in fork_chain_blocks if block.slot in expected_block_slots
    ]
    assert len(requested_blocks) == len(expected_blocks)
    assert set(requested_blocks) == set(expected_blocks)
Esempio n. 3
0
def test_validate_proposer_signature(
        slots_per_epoch,
        shard_count,
        proposer_privkey,
        proposer_pubkey,
        is_valid_signature,
        sample_beacon_block_params,
        sample_beacon_state_params,
        target_committee_size,
        max_effective_balance,
        config):

    state = BeaconState(**sample_beacon_state_params).copy(
        validators=tuple(
            create_mock_validator(proposer_pubkey, config)
            for _ in range(10)
        ),
        balances=(max_effective_balance,) * 10,
    )

    block = BeaconBlock(**sample_beacon_block_params)
    header = block.header

    proposed_block = block.copy(
        signature=bls.sign(
            message_hash=header.signing_root,
            privkey=proposer_privkey,
            domain=get_domain(
                state,
                SignatureDomain.DOMAIN_BEACON_PROPOSER,
                slots_per_epoch,
            ),
        ),
    )

    if is_valid_signature:
        validate_proposer_signature(
            state,
            proposed_block,
            CommitteeConfig(config),
        )
    else:
        with pytest.raises(ValidationError):
            validate_proposer_signature(
                state,
                proposed_block,
                CommitteeConfig(config),
            )
def test_validate_proposer_signature(
        slots_per_epoch, shard_count, proposer_privkey, proposer_pubkey,
        is_valid_signature, sample_beacon_block_params,
        sample_beacon_state_params, target_committee_size, max_deposit_amount,
        config):

    state = BeaconState(**sample_beacon_state_params).copy(
        validator_registry=tuple(
            mock_validator(proposer_pubkey, config) for _ in range(10)),
        validator_balances=(max_deposit_amount, ) * 10,
    )

    block = BeaconBlock(**sample_beacon_block_params)
    header = block.header

    proposed_block = block.copy(signature=bls.sign(
        message_hash=header.signing_root,
        privkey=proposer_privkey,
        domain=get_domain(
            Fork(
                config.GENESIS_FORK_VERSION.to_bytes(4, 'little'),
                config.GENESIS_FORK_VERSION.to_bytes(4, 'little'),
                config.GENESIS_EPOCH,
            ),
            slot_to_epoch(state.slot, slots_per_epoch),
            SignatureDomain.DOMAIN_BEACON_BLOCK,
        ),
    ), )

    if is_valid_signature:
        validate_proposer_signature(
            state,
            proposed_block,
            CommitteeConfig(config),
        )
    else:
        with pytest.raises(ValidationError):
            validate_proposer_signature(
                state,
                proposed_block,
                CommitteeConfig(config),
            )
Esempio n. 5
0
async def test_request_recent_beacon_blocks(monkeypatch):
    async with ConnectionPairFactory() as (alice, bob):

        # Mock up block database
        head_block = BeaconBlock(
            slot=0,
            parent_root=ZERO_HASH32,
            state_root=ZERO_HASH32,
            signature=EMPTY_SIGNATURE,
            body=BeaconBlockBody(),
        )
        blocks = [head_block.copy(slot=slot) for slot in range(5)]
        mock_root_to_block_db = {block.hash_tree_root: block for block in blocks}

        def get_block_by_hash_tree_root(root):
            validate_word(root)
            if root in mock_root_to_block_db:
                return mock_root_to_block_db[root]
            else:
                raise BlockNotFound

        monkeypatch.setattr(
            bob.chain, "get_block_by_hash_tree_root", get_block_by_hash_tree_root
        )

        requesting_block_roots = [
            blocks[0].hash_tree_root,
            b"\x12" * 32,  # Unknown block root
            blocks[1].hash_tree_root,
            b"\x23" * 32,  # Unknown block root
            blocks[3].hash_tree_root,
        ]
        requested_blocks = await alice.request_recent_beacon_blocks(
            peer_id=bob.peer_id, block_roots=requesting_block_roots
        )

        expected_blocks = [blocks[0], blocks[1], blocks[3]]
        assert len(requested_blocks) == len(expected_blocks)
        assert set(requested_blocks) == set(expected_blocks)