Beispiel #1
0
    def __init__(self, chain: list = None):
        """
        Create a new Blockchain instance.

        :param list chain: chain of blocks.
        """
        self.chain = chain or [Block.genesis()]
Beispiel #2
0
    def valid_chain(cls, value: list):
        """
        Validate chain items values.

        :param list value: provided chain value.
        :return list: validated chain value.
        :raise ValueError: on invalid block in chain.
        """
        genesis = last_block = value[0]
        if not isinstance(genesis, Block) or genesis != Block.genesis():
            message = f'Invalid chain genesis block: {genesis}.'
            logger.error(f'[BlockchainSchema] Validation error. {message}')
            raise ValueError(message)
        for block in value[1:]:
            try:
                assert isinstance(block, Block)
                Block.is_valid(last_block, block)
            except (AssertionError, BlockError) as err:
                message = err.message if hasattr(err, 'message') else f'Invalid block: {block}.'
                logger.error(f'[BlockchainSchema] Validation error. {message}')
                raise ValueError(message)
            last_block = block
        return value
Beispiel #3
0
 def _get_genesis_block(self):
     return Block.genesis()