Example #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)
Example #2
0
def test_chaindb_get_score(chaindb, sample_beacon_block_params):
    genesis = BeaconBlock(**sample_beacon_block_params).copy(
        parent_root=GENESIS_PARENT_HASH,
        slot=0,
    )
    chaindb.persist_block(genesis, genesis.__class__)

    genesis_score_key = SchemaV1.make_block_root_to_score_lookup_key(
        genesis.root)
    genesis_score = rlp.decode(chaindb.db.get(genesis_score_key),
                               sedes=rlp.sedes.big_endian_int)
    assert genesis_score == 0
    assert chaindb.get_score(genesis.root) == 0

    block1 = BeaconBlock(**sample_beacon_block_params).copy(
        parent_root=genesis.root,
        slot=1,
    )
    chaindb.persist_block(block1, block1.__class__)

    block1_score_key = SchemaV1.make_block_root_to_score_lookup_key(
        block1.root)
    block1_score = rlp.decode(chaindb.db.get(block1_score_key),
                              sedes=rlp.sedes.big_endian_int)
    assert block1_score == 1
    assert chaindb.get_score(block1.root) == 1
Example #3
0
def test_chaindb_get_score(chaindb, sample_beacon_block_params,
                           fork_choice_scoring):
    genesis = BeaconBlock(**sample_beacon_block_params).copy(
        previous_block_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 = ssz.decode(chaindb.db.get(genesis_score_key),
                               sedes=ssz.sedes.uint64)
    assert genesis_score == 0
    assert chaindb.get_score(genesis.signing_root) == 0

    block1 = BeaconBlock(**sample_beacon_block_params).copy(
        previous_block_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 = ssz.decode(chaindb.db.get(block1_score_key),
                              sedes=ssz.sedes.uint64)
    assert block1_score == 1
    assert chaindb.get_score(block1.signing_root) == 1
Example #4
0
def test_chaindb_get_justified_head(chaindb_at_genesis,
                                    genesis_block,
                                    genesis_state,
                                    sample_beacon_block_params,
                                    config):
    chaindb = chaindb_at_genesis
    block = BeaconBlock(**sample_beacon_block_params).copy(
        previous_block_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.copy(
        current_justified_root=block.signing_root,
        current_justified_epoch=config.GENESIS_EPOCH,
    )
    chaindb.persist_state(state_with_bad_epoch)
    chaindb.persist_block(block, BeaconBlock)

    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.copy(
        current_justified_root=block.signing_root,
        current_justified_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
Example #5
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(**sample_beacon_block_params).copy(
        previous_block_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.copy(
        finalized_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
Example #6
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
Example #7
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
Example #8
0
def block(request, sample_beacon_block_params):
    return BeaconBlock(**sample_beacon_block_params).copy(
        parent_root=GENESIS_PARENT_HASH, slot=request.param
    )
Example #9
0
def block(request):
    return SignedBeaconBlock.create(message=BeaconBlock.create(
        parent_root=GENESIS_PARENT_HASH, slot=request.param))
Example #10
0
def block(request, sample_beacon_block_params):
    return BeaconBlock.create(**sample_beacon_block_params).mset(
        "parent_root", GENESIS_PARENT_HASH, "slot", request.param)