Ejemplo n.º 1
0
def test_chaindb_get_score(chaindb, fixture_sm_class,
                           sample_beacon_block_params, fork_choice_scoring):
    genesis = BeaconBlock.create(**sample_beacon_block_params).mset(
        "parent_root", GENESIS_PARENT_HASH, "slot", 0)
    chaindb.persist_block(genesis, genesis.__class__, fork_choice_scoring)

    genesis_score_key = SchemaV1.make_block_root_to_score_lookup_key(
        genesis.signing_root)
    genesis_score_data = chaindb.db.get(genesis_score_key)
    genesis_score_class = fork_choice_scoring.get_score_class()
    genesis_score = genesis_score_class.deserialize(genesis_score_data)

    expected_genesis_score = fork_choice_scoring.score(genesis)

    assert genesis_score == expected_genesis_score
    assert (chaindb.get_score(genesis.signing_root,
                              genesis_score_class) == expected_genesis_score)

    block1 = BeaconBlock.create(**sample_beacon_block_params).mset(
        "parent_root", genesis.signing_root, "slot", 1)
    chaindb.persist_block(block1, block1.__class__, fork_choice_scoring)

    block1_score_key = SchemaV1.make_block_root_to_score_lookup_key(
        block1.signing_root)
    block1_score_data = chaindb.db.get(block1_score_key)
    block1_score = genesis_score_class.deserialize(block1_score_data)
    expected_block1_score = fork_choice_scoring.score(block1)
    assert block1_score == expected_block1_score
    assert (chaindb.get_score(block1.signing_root,
                              genesis_score_class) == expected_block1_score)
Ejemplo n.º 2
0
def test_chaindb_get_finalized_head(
    chaindb_at_genesis,
    genesis_block,
    genesis_state,
    sample_beacon_block_params,
    fork_choice_scoring,
):
    chaindb = chaindb_at_genesis
    block = BeaconBlock.create(**sample_beacon_block_params).set(
        "parent_root", genesis_block.signing_root)

    assert chaindb.get_finalized_head(genesis_block.__class__) == genesis_block
    assert chaindb.get_justified_head(genesis_block.__class__) == genesis_block

    state_with_finalized_block = genesis_state.set(
        "finalized_checkpoint", Checkpoint.create(root=block.signing_root))
    chaindb.persist_state(state_with_finalized_block)
    chaindb.persist_block(block, BeaconBlock, fork_choice_scoring)

    assert chaindb.get_finalized_head(
        BeaconBlock).signing_root == block.signing_root
    assert chaindb.get_justified_head(genesis_block.__class__) == genesis_block
Ejemplo n.º 3
0
def test_chaindb_get_justified_head(
    chaindb_at_genesis,
    genesis_block,
    genesis_state,
    sample_beacon_block_params,
    fork_choice_scoring,
    config,
):
    chaindb = chaindb_at_genesis
    block = BeaconBlock.create(**sample_beacon_block_params).set(
        "parent_root", genesis_block.signing_root)

    assert chaindb.get_finalized_head(genesis_block.__class__) == genesis_block
    assert chaindb.get_justified_head(genesis_block.__class__) == genesis_block

    # test that there is only one justified head per epoch
    state_with_bad_epoch = genesis_state.set(
        "current_justified_checkpoint",
        Checkpoint.create(root=block.signing_root, epoch=config.GENESIS_EPOCH),
    )
    chaindb.persist_state(state_with_bad_epoch)
    chaindb.persist_block(block, BeaconBlock, fork_choice_scoring)

    assert chaindb.get_finalized_head(genesis_block.__class__) == genesis_block
    assert chaindb.get_justified_head(genesis_block.__class__) == genesis_block

    # test that the we can update justified head if we satisfy the invariants
    state_with_justified_block = genesis_state.set(
        "current_justified_checkpoint",
        Checkpoint.create(root=block.signing_root,
                          epoch=config.GENESIS_EPOCH + 1),
    )
    chaindb.persist_state(state_with_justified_block)

    assert chaindb.get_finalized_head(genesis_block.__class__) == genesis_block
    assert chaindb.get_justified_head(
        BeaconBlock).signing_root == block.signing_root
Ejemplo n.º 4
0
def block(request):
    return SignedBeaconBlock.create(message=BeaconBlock.create(
        parent_root=GENESIS_PARENT_HASH, slot=request.param))
Ejemplo n.º 5
0
def block(request, sample_beacon_block_params):
    return BeaconBlock.create(**sample_beacon_block_params).mset(
        "parent_root", GENESIS_PARENT_HASH, "slot", request.param)