def __init__(self, chain: list = None): """ Create a new Blockchain instance. :param list chain: chain of blocks. """ self.chain = chain or [Block.genesis()]
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
def _get_genesis_block(self): return Block.genesis()