Ejemplo n.º 1
0
def get_fresh_chain_db():
    db = AtomicDB()
    genesis_block = create_test_block(slot=0)

    chain_db = BeaconChainDB(db)
    chain_db.persist_block(genesis_block)

    return chain_db
Ejemplo n.º 2
0
def initial_chaindb(base_db, genesis_block, genesis_crystallized_state,
                    genesis_active_state):
    chaindb = BeaconChainDB(base_db)
    chaindb.persist_block(genesis_block)
    chaindb.persist_crystallized_state(genesis_crystallized_state)
    chaindb.persist_active_state(genesis_active_state,
                                 genesis_crystallized_state.hash)
    return chaindb
Ejemplo n.º 3
0
def get_fresh_chain_db():
    db = AtomicDB()
    genesis_block = BaseBeaconBlock(
        slot=0,
        randao_reveal=ZERO_HASH32,
        candidate_pow_receipt_root=ZERO_HASH32,
        ancestor_hashes=[ZERO_HASH32] * 32,
        state_root=ZERO_HASH32,  # note: not the actual genesis state root
        attestations=[],
        specials=[],
        proposer_signature=None,
    )

    chain_db = BeaconChainDB(db)
    chain_db.persist_block(genesis_block)
    return chain_db
Ejemplo n.º 4
0
def test_demo(base_db, sample_beacon_block_params, sample_beacon_state_params,
              fixture_sm_class):
    chaindb = BeaconChainDB(base_db)
    block = BaseBeaconBlock(**sample_beacon_block_params)
    state = BeaconState(**sample_beacon_state_params)

    sm = fixture_sm_class(chaindb, block, state)
    result_state, result_block = sm.import_block(block)

    assert state.slot == 0
    assert result_state.slot == sm.config.ZERO_BALANCE_VALIDATOR_TTL
Ejemplo n.º 5
0
def test_demo(base_db,
              sample_beacon_block_params,
              genesis_state,
              fixture_sm_class,
              config,
              privkeys,
              pubkeys):
    chaindb = BeaconChainDB(base_db)
    state = genesis_state
    block = SerenityBeaconBlock(**sample_beacon_block_params).copy(
        slot=state.slot + 2,
        state_root=state.root,
    )

    # Sign block
    beacon_proposer_index = get_beacon_proposer_index(
        state,
        block.slot,
        config.EPOCH_LENGTH,
    )
    index_in_privkeys = pubkeys.index(
        state.validator_registry[beacon_proposer_index].pubkey
    )
    beacon_proposer_privkey = privkeys[index_in_privkeys]
    empty_signature_block_root = block.block_without_signature_root
    proposal_root = ProposalSignedData(
        block.slot,
        config.BEACON_CHAIN_SHARD_NUMBER,
        empty_signature_block_root,
    ).root
    block = block.copy(
        signature=bls.sign(
            message=proposal_root,
            privkey=beacon_proposer_privkey,
            domain=SignatureDomain.DOMAIN_PROPOSAL,
        ),
    )

    # Store in chaindb
    chaindb.persist_block(block, SerenityBeaconBlock)
    chaindb.persist_state(state)

    # Get state machine instance
    sm = fixture_sm_class(chaindb, block.root, SerenityBeaconBlock)
    result_state, _ = sm.import_block(block)

    assert state.slot == 0
    assert result_state.slot == block.slot
    assert isinstance(sm.block, SerenityBeaconBlock)
Ejemplo n.º 6
0
def chaindb(base_db):
    return BeaconChainDB(base_db)
Ejemplo n.º 7
0
def test_state_machine(base_db, genesis_block, genesis_crystallized_state,
                       genesis_active_state):
    chaindb = BeaconChainDB(base_db)
    chaindb.persist_block(genesis_block)
    chaindb.persist_crystallized_state(genesis_crystallized_state)
    chaindb.persist_active_state(genesis_active_state,
                                 genesis_crystallized_state.hash)

    block_1 = genesis_block.copy(
        parent_hash=genesis_block.hash,
        slot_number=1,
        active_state_root=b'\x11' * 32,
    )
    chaindb.persist_block(block_1)

    block_2 = block_1.copy(
        parent_hash=block_1.hash,
        slot_number=2,
        active_state_root=b'\x22' * 32,
    )
    # canonical head is block_2
    chaindb.persist_block(block_2)

    # building block_3
    block_3 = block_2.copy(
        parent_hash=block_2.hash,
        slot_number=3,
        active_state_root=b'\x33' * 32,
    )

    sm = SerenityBeaconStateMachine(chaindb, block_3)

    assert sm.crystallized_state == genesis_crystallized_state
    expect = [ZERO_HASH32] * (sm.config.CYCLE_LENGTH * 2 - 2) + \
        [genesis_block.hash] + [block_1.hash]
    expect = tuple(expect)
    assert sm.active_state.recent_block_hashes == expect