Beispiel #1
0
    def validate_persist_block(self,
                               block: BamiBlock,
                               peer: Peer = None) -> bool:
        """
        Validate a block and if it's valid, persist it.
        Raises:
            InvalidBlockException - if block is not valid
        """
        block = (BamiBlock.unpack(block, self.serializer)
                 if type(block) is bytes else block)
        block_blob = block if type(block) is bytes else block.pack()

        if not block.block_invariants_valid():
            # React on invalid block
            raise InvalidBlockException("Block invalid", str(block), peer)
        else:
            if not self.persistence.has_block(block.hash):
                self.process_block_unordered(block, peer)
                chain_id = block.com_id
                prefix = block.com_prefix
                if (self.persistence.get_chain(prefix + chain_id)
                        and self.persistence.get_chain(prefix +
                                                       chain_id).versions.get(
                                                           block.com_seq_num)
                        and block.short_hash in self.persistence.get_chain(
                            prefix + chain_id).versions[block.com_seq_num]):
                    raise Exception(
                        "Inconsisistency between block store and chain store",
                        self.persistence.get_chain(prefix + chain_id).versions,
                        block.com_dot,
                    )
                self.persistence.add_block(block_blob, block)
Beispiel #2
0
def insert_to_chain_or_blk_store(
    chain_obj: Union[BaseChain, BaseDB], blk: BamiBlock, personal_chain: bool = True,
):
    if isinstance(chain_obj, BaseChain):
        yield from insert_to_chain(chain_obj, blk, personal_chain)
    else:
        yield chain_obj.add_block(blk.pack(), blk)