Exemplo n.º 1
0
def test_block_by_slot_number(slot_number):
    block = Block(slot_number=slot_number)
    chain = Chain(head=block, blocks=[block])

    assert chain.get_block_by_slot_number(block.slot_number) == block
    assert chain.get_block_by_slot_number(block.slot_number + 1) is None
    assert chain.get_block_by_slot_number(-1) is None
Exemplo n.º 2
0
def attestation_validation_fixture(genesis_crystallized_state,
                                   genesis_active_state, genesis_block,
                                   mock_make_child, mock_make_attestations,
                                   config):
    crystallized_state = genesis_crystallized_state
    active_state = genesis_active_state
    parent_block = genesis_block
    active_state.chain = Chain(head=parent_block, blocks=[parent_block])
    attestations_of_genesis = mock_make_attestations(
        (crystallized_state, active_state), parent_block, attester_share=1.0)
    block, _, _ = mock_make_child(
        (crystallized_state, active_state),
        parent_block,
        1,
        attestations_of_genesis,
    )
    attestation = attestations_of_genesis[0]

    return (
        crystallized_state,
        active_state,
        attestation,
        block,
        parent_block,
    )
Exemplo n.º 3
0
def test_chain():
    block = None
    parent_hash = ZERO_HASH32
    blocks = []
    for slot_number in range(1, 10):
        block = Block(slot_number=slot_number, parent_hash=parent_hash)
        blocks.append(block)
        parent_hash = block.hash

    extra_block = Block(slot_number=1000000)
    chain = Chain(head=block, blocks=blocks + [extra_block])
    assert len(chain.chain) == len(blocks)
    for block in blocks:
        assert block in chain
    assert extra_block not in chain
Exemplo n.º 4
0
def test_state_transition_integration(genesis_crystallized_state,
                                      genesis_active_state, genesis_block,
                                      mock_make_child, mock_make_attestations,
                                      config):
    c = genesis_crystallized_state
    a = genesis_active_state
    block = genesis_block
    a.chain = Chain(head=block, blocks=[block])
    print('Generated genesis state')
    print('Crystallized state length:',
          len(serialize(genesis_crystallized_state)))
    print('Active state length:', len(serialize(genesis_active_state)))
    print('Block size:', len(serialize(genesis_block)))

    attestations_of_genesis = mock_make_attestations((c, a),
                                                     block,
                                                     attester_share=0.8)

    block2, c2, a2 = mock_make_child((c, a), block, 1, attestations_of_genesis)
    assert block2.slot_number == 1
    assert len(block2.attestations) == len(attestations_of_genesis)
    assert block2.crystallized_state_root == block.crystallized_state_root
    assert block2.active_state_root != b'\x00' * 32

    t = time.time()
    assert compute_state_transition((c, a), block, block2, config=config)
    print(
        "Normal block with %s attestations of size %s processed in %.4f sec" %
        (len(attestations_of_genesis),
         len(c.shard_and_committee_for_slots[attestations_of_genesis[0].slot]
             [0].committee), (time.time() - t)))
    print('Verified a block!')

    attestations_of_2 = mock_make_attestations((c2, a2),
                                               block2,
                                               attester_share=0.8)

    cycle_transition_slot = c2.last_state_recalc + config['cycle_length']

    block3, c3, a3 = mock_make_child((c2, a2), block2, cycle_transition_slot,
                                     attestations_of_2)

    t = time.time()
    assert compute_state_transition((c2, a2), block2, block3, config=config)
    print("Cycle transition processed in %.4f sec" % (time.time() - t))
Exemplo n.º 5
0
def test_calculate_crosslink_rewards(genesis_crystallized_state,
                                     genesis_active_state, genesis_block,
                                     config, mock_make_attestations,
                                     mock_make_child):
    c = genesis_crystallized_state
    a = genesis_active_state
    block = genesis_block
    a.chain = Chain(head=block, blocks=[block])

    # progress past first cycle transition
    # rewards on the following cycle recalc will be based
    # on what happened during this cycle
    attestations = mock_make_attestations(
        (c, a),
        block,
        # enough attesters to get a reward but not form a crosslink
        attester_share=0.58)
    block2, c2, a2 = mock_make_child(
        (c, a), block, block.slot_number + config['cycle_length'],
        attestations)

    # attestation used for testing
    attestation = attestations[0]

    # create a block to trigger next cycle transition
    attestations2 = mock_make_attestations((c2, a2),
                                           block2,
                                           attester_share=0.0)
    block3, c3, a3 = mock_make_child(
        (c2, a2), block2, block2.slot_number + config['cycle_length'],
        attestations2)

    rewards_and_penalties = calculate_crosslink_rewards(c2, a2, block3, config)

    shard_and_committee = get_shards_and_committees_for_slot(
        c2, block2.slot_number, config)[0]
    for committee_index, validator_index in enumerate(
            shard_and_committee.committee):
        if has_voted(attestation.attester_bitfield, committee_index):
            assert rewards_and_penalties[validator_index] > 0
        else:
            assert rewards_and_penalties[validator_index] < 0
Exemplo n.º 6
0
def test_pos_finalization(monkeypatch, genesis_crystallized_state,
                          genesis_active_state, genesis_block, mock_make_child,
                          mock_make_attestations, config):
    from beacon_chain.state import state_transition

    def mock_validate_parent_block_proposer(block, parent_block,
                                            crystallized_state, config):
        return None

    monkeypatch.setattr(state_transition, 'validate_parent_block_proposer',
                        mock_validate_parent_block_proposer)

    c = genesis_crystallized_state
    a = genesis_active_state
    block = genesis_block
    a.chain = Chain(head=block, blocks=[block])
    expected_streak = 0
    assert c.justified_streak == expected_streak

    # create 100% full vote blocks to one block before cycle transition
    for i in range(config['cycle_length'] - 1):
        attestations = mock_make_attestations((c, a),
                                              block,
                                              attester_share=1.0)
        block, c, a = mock_make_child((c, a), block, block.slot_number + 1,
                                      attestations)

    assert c.last_state_recalc == genesis_crystallized_state.last_state_recalc
    assert c.justified_streak == 0

    # do cycle transition
    attestations = mock_make_attestations((c, a), block, attester_share=1.0)
    block, c, a = mock_make_child((c, a), block, block.slot_number + 1,
                                  attestations)

    assert c.last_state_recalc == (
        genesis_crystallized_state.last_state_recalc + config['cycle_length'])
    assert c.justified_streak == config['cycle_length']
    assert c.last_justified_slot == 0
    assert c.last_finalized_slot == 0

    # check that the expected crosslinks were updated
    expected_shards = [
        shard_and_committee.shard_id
        for indices in c.shard_and_committee_for_slots
        for shard_and_committee in indices
    ]
    for shard_id, crosslink in enumerate(c.crosslink_records):
        if shard_id in expected_shards:
            assert crosslink.slot == c.last_state_recalc
        else:
            assert crosslink.slot == 0

    # create 100% full vote blocks to one block before cycle transition
    for i in range(config['cycle_length'] - 1):
        attestations = mock_make_attestations((c, a),
                                              block,
                                              attester_share=1.0)
        block, c, a = mock_make_child((c, a), block, block.slot_number + 1,
                                      attestations)

        # Nothing occurs because we haven't triggered cycle transition
        assert c.last_state_recalc == (
            genesis_crystallized_state.last_state_recalc +
            config['cycle_length'])
        assert c.justified_streak == config['cycle_length']
        assert c.last_justified_slot == 0
        assert c.last_finalized_slot == 0

    # do cycle transition
    attestations = mock_make_attestations((c, a), block, attester_share=1.0)
    block, c, a = mock_make_child((c, a), block, block.slot_number + 1,
                                  attestations)

    assert c.last_state_recalc == (
        genesis_crystallized_state.last_state_recalc +
        config['cycle_length'] * 2)
    assert c.justified_streak == config['cycle_length'] * 2
    assert c.last_justified_slot == c.last_state_recalc - config[
        'cycle_length'] - 1
    # still 0 because CYCLE_LENGTH + 1 before lsat_justified_slot is negative
    assert c.last_finalized_slot == 0

    # One more cycle!
    for i in range(config['cycle_length'] - 1):
        attestations = mock_make_attestations((c, a),
                                              block,
                                              attester_share=1.0)
        block, c, a = mock_make_child((c, a), block, block.slot_number + 1,
                                      attestations)

    # do cycle transition
    attestations = mock_make_attestations((c, a), block, attester_share=1.0)
    block, c, a = mock_make_child((c, a), block, block.slot_number + 1,
                                  attestations)

    assert c.last_state_recalc == (
        genesis_crystallized_state.last_state_recalc +
        config['cycle_length'] * 3)
    assert c.justified_streak == config['cycle_length'] * 3
    assert c.last_justified_slot == c.last_state_recalc - config[
        'cycle_length'] - 1
    # still 0 because CYCLE_LENGTH + 1 before last_justified_slot is negative
    assert c.last_finalized_slot == c.last_justified_slot - config[
        'cycle_length'] - 1

    # force and check dynasty transition
    if block.slot_number < config['min_dynasty_length']:
        block, c, a = mock_make_child((c, a), block,
                                      config['min_dynasty_length'], [])

    assert c.current_dynasty == genesis_crystallized_state.current_dynasty + 1
    assert c.dynasty_start == c.last_state_recalc
Exemplo n.º 7
0
def test_block_by_hash():
    block = Block()
    chain = Chain(head=block, blocks=[block])

    assert chain.get_block_by_hash(block.hash) == block
    assert chain.get_block_by_hash(b'\x35' * 32) is None
Exemplo n.º 8
0
def test_head():
    block = Block()
    chain = Chain(head=block)

    assert chain.head == block