Exemplo n.º 1
0
    def _set_as_canonical_chain_head(
        cls,
        db: BaseDB,
        block_hash: Hash32,
    ) -> Tuple[Tuple[BlockHeader, ...], Tuple[BlockHeader, ...]]:
        try:
            header = cls._get_block_header_by_hash(db, block_hash)
        except HeaderNotFound:
            raise ValueError(
                "Cannot use unknown block hash as canonical head: {}".format(
                    header.hash))

        new_canonical_headers = tuple(
            reversed(cls._find_new_ancestors(db, header)))
        old_canonical_headers = []

        # remove transaction lookups for blocks that are no longer canonical
        for h in new_canonical_headers:
            try:
                old_hash = cls._get_canonical_block_hash(db, h.block_number)
            except HeaderNotFound:
                # no old block, and no more possible
                break
            else:
                old_header = cls._get_block_header_by_hash(db, old_hash)
                old_canonical_headers.append(old_header)
                for transaction_hash in cls._get_block_transaction_hashes(
                        db, old_header):
                    cls._remove_transaction_from_canonical_chain(
                        db, transaction_hash)

        for h in new_canonical_headers:
            cls._add_block_number_to_hash_lookup(db, h)

        db.set(SchemaV1.make_canonical_head_hash_lookup_key(), header.hash)

        return new_canonical_headers, tuple(old_canonical_headers)
Exemplo n.º 2
0
 def _get_canonical_head(cls, db: BaseDB) -> BlockHeader:
     try:
         canonical_head_hash = db[SchemaV1.make_canonical_head_hash_lookup_key()]
     except KeyError:
         raise CanonicalHeadNotFound("No canonical head set for this chain")
     return cls._get_block_header_by_hash(db, canonical_head_hash)
Exemplo n.º 3
0
 def _get_canonical_head_hash(cls, db: DatabaseAPI) -> Hash32:
     try:
         return Hash32(db[SchemaV1.make_canonical_head_hash_lookup_key()])
     except KeyError:
         raise CanonicalHeadNotFound("No canonical head set for this chain")