def mock_make_child(parent_state, parent, slot_number, attestations=None):
        if attestations is None:
            attestations = []

        crystallized_state, active_state = parent_state
        block = Block(parent_hash=parent.hash,
                      slot_number=slot_number,
                      randao_reveal=blake(
                          str(random.random()).encode('utf-8')),
                      attestations=attestations,
                      pow_chain_ref=b'\x00' * 32,
                      active_state_root=b'\x00' * 32,
                      crystallized_state_root=b'\x00' * 32)
        print('Generated preliminary block header')

        new_crystallized_state, new_active_state = compute_state_transition(
            (crystallized_state, active_state), parent, block, config=config)
        print('Calculated state transition')

        if crystallized_state == new_crystallized_state:
            block.crystallized_state_root = parent.crystallized_state_root
        else:
            block.crystallized_state_root = blake(
                serialize(crystallized_state))

        block.active_state_root = blake(serialize(active_state))

        return block, new_crystallized_state, new_active_state
def genesis_block(genesis_active_state, genesis_crystallized_state):
    active_state_root = blake(serialize(genesis_active_state))
    crystallized_state_root = blake(serialize(genesis_crystallized_state))

    return get_genesis_block(
        active_state_root=active_state_root,
        crystallized_state_root=crystallized_state_root,
    )
Exemple #3
0
def test_get_genesis_block(genesis_active_state, genesis_crystallized_state):
    active_state_root = blake(serialize(genesis_active_state))
    crystallized_state_root = blake(serialize(genesis_crystallized_state))

    block = get_genesis_block(
        active_state_root=active_state_root,
        crystallized_state_root=crystallized_state_root,
    )

    assert block.parent_hash == ZERO_HASH32
    assert block.slot_number == 0
    assert block.randao_reveal == ZERO_HASH32
    assert block.num_attestations == 0
    assert block.pow_chain_ref == ZERO_HASH32
    assert block.active_state_root == active_state_root
    assert block.crystallized_state_root == crystallized_state_root
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))
Exemple #5
0
def get_hashes_to_sign(
        active_state: 'ActiveState',
        block: 'Block',
        config: Dict[str, Any] = DEFAULT_CONFIG) -> List[Hash32]:
    cycle_length = config['cycle_length']

    hashes = get_hashes_from_active_state(
        active_state,
        block,
        from_slot=block.slot_number - cycle_length + 1,
        to_slot=block.slot_number - 1,
        config=config,
    ) + [blake(serialize(block))]

    return hashes
Exemple #6
0
def test_initialize_new_cycle(
        genesis_crystallized_state, genesis_active_state, genesis_block,
        last_state_recalc, last_justified_slot, justified_streak,
        last_finalized_slot, fraction_voted, result_last_state_recalc,
        result_justified_streak, result_last_finalized_slot, config):
    # Fill the parent_crystallized_state with parematers
    parent_crystallized_state = genesis_crystallized_state
    parent_crystallized_state.last_state_recalc = last_state_recalc
    parent_crystallized_state.last_justified_slot = last_justified_slot
    parent_crystallized_state.justified_streak = justified_streak
    parent_crystallized_state.last_finalized_slot = last_finalized_slot

    parent_active_state = genesis_active_state

    parent_block = genesis_block
    block = copy.deepcopy(genesis_block)
    block.slot_number = 258
    block.parent_hash = blake(serialize(parent_block))

    active_state = fill_recent_block_hashes(parent_active_state, parent_block,
                                            block)

    fraction_voted *= 1.01  # add margin for rounding error
    # Fill the total_voter_deposits to simulate the different committee results
    active_state.block_vote_cache[block.parent_hash] = {
        'voter_indices':
        set(),
        'total_voter_deposits':
        int(parent_crystallized_state.total_deposits * fraction_voted)
    }

    crystallized_state, active_state = initialize_new_cycle(
        parent_crystallized_state,
        active_state,
        block,
        config=config,
    )
    assert crystallized_state.last_state_recalc == result_last_state_recalc
    assert crystallized_state.justified_streak == result_justified_streak
    assert crystallized_state.last_finalized_slot == result_last_finalized_slot
def test_failed_serialization(value, typ):
    with pytest.raises(Exception):
        serialize(value, typ)
def test_basic_serialization(value, typ, data):
    assert serialize(value, typ) == data
Exemple #9
0
 def hash(self):
     return blake(serialize(self))