Ejemplo n.º 1
0
 def get_block_by_hash(self, block_hash: Hash32) -> BaseBlock:
     """
     Returns the requested block as specified by block hash.
     """
     validate_word(block_hash, title="Block Hash")
     block_header = self.get_block_header_by_hash(block_hash)
     return self.get_block_by_header(block_header)
Ejemplo n.º 2
0
    def get_block_header_by_hash(self, block_hash: Hash32) -> BlockHeader:
        """
        Returns the requested block header as specified by block hash.

        Raises BlockNotFound if there's no block header with the given hash in the db.
        """
        validate_word(block_hash, title="Block Hash")
        return self.chaindb.get_block_header_by_hash(block_hash)
Ejemplo n.º 3
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))
Ejemplo n.º 4
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))
Ejemplo n.º 5
0
    def get_block_by_root(self, block_root: SigningRoot) -> BaseBeaconBlock:
        """
        Return the requested block as specified by block hash.

        Raise ``BlockNotFound`` if there's no block with the given hash in the db.
        """
        validate_word(block_root, title="Block Signing Root")

        block_class = self.get_block_class(block_root)
        return self.chaindb.get_block_by_root(block_root, block_class)
Ejemplo n.º 6
0
 def get_block_uncles(self, uncles_hash: Hash32) -> Tuple[BlockHeaderAPI, ...]:
     validate_word(uncles_hash, title="Uncles Hash")
     if uncles_hash == EMPTY_UNCLE_HASH:
         return ()
     try:
         encoded_uncles = self.db[uncles_hash]
     except KeyError as exc:
         raise HeaderNotFound(
             f"No uncles found for hash {uncles_hash!r}"
         ) from exc
     else:
         return tuple(rlp.decode(encoded_uncles, sedes=rlp.sedes.CountableList(BlockHeader)))
Ejemplo n.º 7
0
 def _get_block_signing_root_by_hash_tree_root(
         db: DatabaseAPI, block_root: HashTreeRoot) -> SigningRoot:
     validate_word(block_root, title="block hash tree root")
     key = SchemaV1.make_block_hash_tree_root_to_signing_root_lookup_key(
         block_root)
     try:
         signing_root = db[key]
     except KeyError:
         raise BlockNotFound(
             "No block with hash tree root {0} found".format(
                 encode_hex(block_root)))
     return cast(SigningRoot, signing_root)
Ejemplo n.º 8
0
    def _get_block_header_by_hash(db: DatabaseAPI, block_hash: Hash32) -> BlockHeaderAPI:
        """
        Returns the requested block header as specified by block hash.

        Raises BlockNotFound if it is not present in the db.
        """
        validate_word(block_hash, title="Block Hash")
        try:
            header_rlp = db[block_hash]
        except KeyError:
            raise HeaderNotFound(f"No header with hash {encode_hex(block_hash)} found")
        return _decode_block_header(header_rlp)
Ejemplo n.º 9
0
    def get_block_header_by_hash(self, block_hash: Hash32) -> BlockHeader:
        """
        Returns the requested block header as specified by block hash.

        Raises BlockNotFound if it is not present in the db.
        """
        validate_word(block_hash, title="Block Hash")
        try:
            header_rlp = self.db[block_hash]
        except KeyError:
            raise HeaderNotFound("No header with hash {0} found".format(
                encode_hex(block_hash)))
        return _decode_block_header(header_rlp)
Ejemplo n.º 10
0
    def _get_block_by_hash(db: BaseDB, block_hash: Hash32) -> BaseBeaconBlock:
        """
        Return the requested block header as specified by block hash.

        Raise BlockNotFound if it is not present in the db.
        """
        validate_word(block_hash, title="Block Hash")
        try:
            block_rlp = db[block_hash]
        except KeyError:
            raise BlockNotFound("No block with hash {0} found".format(
                encode_hex(block_hash)))
        return _decode_block(block_rlp)
Ejemplo n.º 11
0
    def _get_block_by_root(
            db: DatabaseAPI, block_root: Hash32,
            block_class: Type[BaseBeaconBlock]) -> BaseBeaconBlock:
        """
        Return the requested block header as specified by block root.

        Raise BlockNotFound if it is not present in the db.
        """
        validate_word(block_root, title="block root")
        try:
            block_ssz = db[block_root]
        except KeyError:
            raise BlockNotFound("No block with root {0} found".format(
                encode_hex(block_root)))
        return _decode_block(block_ssz, block_class)
Ejemplo n.º 12
0
 def get_block_uncles(self, uncles_hash: Hash32) -> List[BlockHeader]:
     """
     Returns an iterable of uncle headers specified by the given uncles_hash
     """
     validate_word(uncles_hash, title="Uncles Hash")
     if uncles_hash == EMPTY_UNCLE_HASH:
         return []
     try:
         encoded_uncles = self.db[uncles_hash]
     except KeyError:
         raise HeaderNotFound(
             "No uncles found for hash {0}".format(uncles_hash))
     else:
         return rlp.decode(encoded_uncles,
                           sedes=rlp.sedes.CountableList(BlockHeader))
Ejemplo n.º 13
0
    def _get_block_by_root(
            db: DatabaseAPI, block_root: Root,
            block_class: Type[BaseSignedBeaconBlock]) -> BaseBeaconBlock:
        """
        Return the requested block header as specified by block root.

        Raise BlockNotFound if it is not present in the db.
        """
        validate_word(block_root, title="block root")

        if block_root in block_cache and block_root in db:
            return block_cache[block_root]

        try:
            block_ssz = db[block_root]
        except KeyError:
            raise BlockNotFound("No block with root {0} found".format(
                encode_hex(block_root)))

        block = ssz.decode(block_ssz, block_class)
        block_cache[block_root] = block
        return block
Ejemplo n.º 14
0
 def _header_exists(db: BaseDB, block_hash: Hash32) -> bool:
     validate_word(block_hash, title="Block Hash")
     return block_hash in db
Ejemplo n.º 15
0
def test_validate_word(value, is_valid):
    if is_valid:
        validate_word(value)
    else:
        with pytest.raises(ValidationError):
            validate_word(value)
Ejemplo n.º 16
0
 def _block_exists(db: DatabaseAPI, block_root: Hash32) -> bool:
     validate_word(block_root, title="block root")
     return block_root in db
Ejemplo n.º 17
0
 def header_exists(self, block_hash: Hash32) -> bool:
     validate_word(block_hash, title="Block Hash")
     return block_hash in self.db
Ejemplo n.º 18
0
 def get_block_by_root(root):
     validate_word(root)
     if root in mock_root_to_block_db:
         return mock_root_to_block_db[root]
     else:
         raise BlockNotFound
Ejemplo n.º 19
0
 def get_block_header_by_hash(self, block_hash: Hash32) -> BlockHeaderAPI:
     validate_word(block_hash, title="Block Hash")
     return self.chaindb.get_block_header_by_hash(block_hash)
Ejemplo n.º 20
0
 def get_block_by_hash_tree_root(self, block_root: HashTreeRoot) -> BaseBeaconBlock:
     validate_word(block_root, title="Block Hash Tree Root")
     signing_root = self.chaindb.get_block_signing_root_by_hash_tree_root(block_root)
     return self.get_block_by_root(signing_root)
Ejemplo n.º 21
0
 def get_block_by_hash(self, block_hash: Hash32) -> BlockAPI:
     validate_word(block_hash, title="Block Hash")
     block_header = self.get_block_header_by_hash(block_hash)
     return self.get_block_by_header(block_header)