예제 #1
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
예제 #2
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)
예제 #3
0
def test_chaindb_get_score(chaindb, fixture_sm_class, genesis_block,
                           fork_choice_scoring):
    chaindb.persist_block(genesis_block, genesis_block.__class__,
                          fork_choice_scoring)

    genesis_score_key = SchemaV1.make_block_root_to_score_lookup_key(
        genesis_block.message.hash_tree_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_block.message)

    assert genesis_score == expected_genesis_score
    assert (chaindb.get_score(genesis_block.message.hash_tree_root,
                              genesis_score_class) == expected_genesis_score)

    block1 = SignedBeaconBlock.from_parent(genesis_block,
                                           FromBlockParams(slot=1))

    chaindb.persist_block(block1, block1.__class__, fork_choice_scoring)

    block1_score_key = SchemaV1.make_block_root_to_score_lookup_key(
        block1.message.hash_tree_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.message)
    assert block1_score == expected_block1_score
    assert (chaindb.get_score(block1.message.hash_tree_root,
                              genesis_score_class) == expected_block1_score)
예제 #4
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
예제 #5
0
 def _update_justified_head(self, justified_root: Root,
                            epoch: Epoch) -> None:
     """
     Unconditionally write the ``justified_root`` as the root of the highest
     justified block.
     """
     self.db.set(SchemaV1.make_justified_head_root_lookup_key(),
                 justified_root)
     self.db.set(
         SchemaV1.make_justified_head_epoch_lookup_key(),
         ssz.encode(epoch, ssz.uint64),
     )
예제 #6
0
 def _get_score(db: BaseDB, block_root: Hash32) -> int:
     try:
         encoded_score = db[SchemaV1.make_block_root_to_score_lookup_key(block_root)]
     except KeyError:
         raise BlockNotFound("No block with hash {0} found".format(
             encode_hex(block_root)))
     return rlp.decode(encoded_score, sedes=rlp.sedes.big_endian_int)
예제 #7
0
 def _update_finalized_head(self, finalized_root: Root) -> None:
     """
     Unconditionally write the ``finalized_root`` as the root of the currently
     finalized block.
     """
     self.db.set(SchemaV1.make_finalized_head_root_lookup_key(), finalized_root)
     self._finalized_root = finalized_root
예제 #8
0
 def _add_block_hash_tree_root_to_signing_root_lookup(
     db: DatabaseAPI, block: BaseBeaconBlock
 ) -> None:
     key = SchemaV1.make_block_hash_tree_root_to_signing_root_lookup_key(
         block.hash_tree_root
     )
     db.set(key, block.signing_root)
예제 #9
0
def test_chaindb_persist_block_and_block_to_root(chaindb, block,
                                                 fork_choice_scoring):
    block_to_root_key = SchemaV1.make_block_root_to_score_lookup_key(
        block.message.hash_tree_root)
    assert not chaindb.exists(block_to_root_key)
    chaindb.persist_block(block, block.__class__, fork_choice_scoring)
    assert chaindb.exists(block_to_root_key)
예제 #10
0
 def _persist_canonical_epoch_info(db: DatabaseAPI, state: BeaconState) -> None:
     epoch_info = EpochInfo(
         previous_justified_checkpoint=state.previous_justified_checkpoint,
         current_justified_checkpoint=state.current_justified_checkpoint,
         finalized_checkpoint=state.finalized_checkpoint,
     )
     db.set(SchemaV1.make_canonical_epoch_info_lookup_key(), ssz.encode(epoch_info))
예제 #11
0
 def _get_finalized_head_root(db: DatabaseAPI) -> Hash32:
     try:
         finalized_head_root = db[
             SchemaV1.make_finalized_head_root_lookup_key()]
     except KeyError:
         raise FinalizedHeadNotFound("No finalized head set for this chain")
     return finalized_head_root
예제 #12
0
 def _get_canonical_head_root(db: DatabaseAPI) -> Hash32:
     try:
         canonical_head_root = db[
             SchemaV1.make_canonical_head_root_lookup_key()]
     except KeyError:
         raise CanonicalHeadNotFound("No canonical head set for this chain")
     return canonical_head_root
예제 #13
0
 def _get_justified_head_root(db: DatabaseAPI) -> Hash32:
     try:
         justified_head_root = db[
             SchemaV1.make_justified_head_root_lookup_key()]
     except KeyError:
         raise JustifiedHeadNotFound("No justified head set for this chain")
     return justified_head_root
예제 #14
0
 def _add_block_slot_to_root_lookup(db: DatabaseAPI, block: BaseBeaconBlock) -> None:
     """
     Set a record in the database to allow looking up this block by its
     block slot.
     """
     block_slot_to_root_key = SchemaV1.make_block_slot_to_root_lookup_key(block.slot)
     db.set(block_slot_to_root_key, block.message.hash_tree_root)
예제 #15
0
 def _update_justified_head(self, justified_root: SigningRoot, epoch: Epoch) -> None:
     """
     Unconditionally write the ``justified_root`` as the root of the highest
     justified block.
     """
     self.db.set(SchemaV1.make_justified_head_root_lookup_key(), justified_root)
     self._highest_justified_epoch = epoch
예제 #16
0
 def _add_slot_to_state_root_lookup(self, slot: Slot, state_root: Hash32) -> None:
     """
     Set a record in the database to allow looking up the state root by
     slot number.
     """
     slot_to_state_root_key = SchemaV1.make_slot_to_state_root_lookup_key(slot)
     self.db.set(slot_to_state_root_key, state_root)
예제 #17
0
def test_chaindb_add_block_number_to_root_lookup(chaindb, block,
                                                 fork_choice_scoring):
    block_slot_to_root_key = SchemaV1.make_block_slot_to_root_lookup_key(
        block.message.slot)
    assert not chaindb.exists(block_slot_to_root_key)
    chaindb.persist_block(block, block.__class__, fork_choice_scoring)
    assert chaindb.exists(block_slot_to_root_key)
예제 #18
0
 def _get_canonical_epoch_info(db: DatabaseAPI) -> EpochInfo:
     key = SchemaV1.make_canonical_epoch_info_lookup_key()
     try:
         epoch_info = db[key]
     except KeyError:
         raise EpochInfoNotFound("Canonical EpochInfo not found")
     else:
         return ssz.decode(epoch_info, EpochInfo)
예제 #19
0
 def _get_canonical_block_root(db: DatabaseAPI, slot: Slot) -> Root:
     slot_to_root_key = SchemaV1.make_block_slot_to_root_lookup_key(slot)
     try:
         root = db[slot_to_root_key]
     except KeyError:
         raise BlockNotFound("No canonical block for block slot #{0}".format(slot))
     else:
         return cast(Root, root)
예제 #20
0
 def _get_finalized_head(cls,
                         db: BaseDB,
                         block_class: Type[BaseBeaconBlock]) -> BaseBeaconBlock:
     try:
         finalized_head_root = db[SchemaV1.make_finalized_head_root_lookup_key()]
     except KeyError:
         raise CanonicalHeadNotFound("No finalized head set for this chain")
     return cls._get_block_by_root(db, Hash32(finalized_head_root), block_class)
예제 #21
0
    def _load_justified_head(self) -> None:
        encoded_epoch = self.db.get(
            SchemaV1.make_justified_head_epoch_lookup_key())
        if not encoded_epoch:
            self._highest_justified_epoch = GENESIS_EPOCH
            return

        self._highest_justified_epoch = ssz.decode(encoded_epoch, ssz.uint64)
예제 #22
0
 def _get_score(db: DatabaseAPI, block_root: Hash32) -> int:
     try:
         encoded_score = db[SchemaV1.make_block_root_to_score_lookup_key(
             block_root)]
     except KeyError:
         raise BlockNotFound("No block with hash {0} found".format(
             encode_hex(block_root)))
     return ssz.decode(encoded_score, sedes=ssz.sedes.uint64)
예제 #23
0
 def _add_head_state_slot_lookup(self, slot: Slot) -> None:
     """
     Write head state slot into the database.
     """
     self.db.set(
         SchemaV1.make_head_state_slot_lookup_key(),
         ssz.encode(slot, sedes=ssz.sedes.uint64),
     )
예제 #24
0
 def _get_head_state_slot(db: DatabaseAPI) -> Slot:
     try:
         encoded_head_state_slot = db[
             SchemaV1.make_head_state_slot_lookup_key()]
         head_state_slot = ssz.decode(encoded_head_state_slot,
                                      sedes=ssz.sedes.uint64)
     except KeyError:
         raise HeadStateSlotNotFound("No head state slot found")
     return head_state_slot
예제 #25
0
 def _get_slot_by_root(db: DatabaseAPI, block_root: Hash32) -> Slot:
     validate_word(block_root, title="block root")
     try:
         encoded_slot = db[SchemaV1.make_block_root_to_slot_lookup_key(
             block_root)]
     except KeyError:
         raise BlockNotFound("No block with root {0} found".format(
             encode_hex(block_root)))
     return Slot(ssz.decode(encoded_slot, sedes=ssz.sedes.uint64))
예제 #26
0
 def _get_score(db: DatabaseAPI, block_root: Root,
                score_class: Type[BaseScore]) -> BaseScore:
     try:
         encoded_score = db[SchemaV1.make_block_root_to_score_lookup_key(
             block_root)]
     except KeyError:
         raise BlockNotFound("No block with root {0} found".format(
             encode_hex(block_root)))
     return cast(BaseScore, score_class.deserialize(encoded_score))
예제 #27
0
 def _add_block_root_to_slot_lookup(db: DatabaseAPI, block: BaseBeaconBlock) -> None:
     """
     Set a record in the database to allow looking up the slot number by its
     block root.
     """
     block_root_to_slot_key = SchemaV1.make_block_root_to_slot_lookup_key(
         block.message.hash_tree_root
     )
     db.set(block_root_to_slot_key, ssz.encode(block.slot, sedes=ssz.sedes.uint64))
예제 #28
0
 def _get_slot_by_root(db: BaseDB,
                       block_root: Hash32) -> SlotNumber:
     validate_word(block_root, title="block root")
     try:
         encoded_slot = db[SchemaV1.make_block_root_to_slot_lookup_key(block_root)]
     except KeyError:
         raise BlockNotFound("No block with root {0} found".format(
             encode_hex(block_root)))
     return SlotNumber(rlp.decode(encoded_slot, sedes=rlp.sedes.big_endian_int))
예제 #29
0
 def _get_canonical_block_root(db: DatabaseAPI, slot: Slot) -> Hash32:
     slot_to_root_key = SchemaV1.make_block_slot_to_root_lookup_key(slot)
     try:
         encoded_key = db[slot_to_root_key]
     except KeyError:
         raise BlockNotFound(
             "No canonical block for block slot #{0}".format(slot))
     else:
         return ssz.decode(encoded_key, sedes=ssz.sedes.bytes32)
예제 #30
0
def test_chaindb_persist_block_and_slot_to_root(chaindb, block, fork_choice_scoring):
    with pytest.raises(BlockNotFound):
        chaindb.get_block_by_root(block.signing_root, block.__class__)
    slot_to_root_key = SchemaV1.make_block_root_to_score_lookup_key(block.signing_root)
    assert not chaindb.exists(slot_to_root_key)

    chaindb.persist_block(block, block.__class__, fork_choice_scoring)

    assert chaindb.get_block_by_root(block.signing_root, block.__class__) == block
    assert chaindb.exists(slot_to_root_key)