Example #1
0
 def get_hash_last_block(self):
     """ Return the hash of the last block of the
         blockchain. If there are not blocks, return
         None.
     """
     if len(blocks) > 0:
         return hash_sha256(blocks[-1])
     else:
         return None
Example #2
0
    def check_blockchain(self):
        """ Check the blockchain to find inconsistencies """
        blocks = self.blocks

        # The list must have at least one block (the genesis block)
        if len(blocks) == 0:
            return False

        for ind in range(len(blocks) - 1, 0, -1):
            if blocks[ind].hash_previous_block != hash_sha256(blocks[ind - 1]):
                return False
        return True
Example #3
0
    def add_block(self, block):
        """ Add a block to the blockchain.
            Return the hash of the block.
        """
        if len(self.blocks) > 0:
            block.hash_previous_block = hash_sha256(str(self.blocks[-1]).encode('utf-8'))
        else:
            block.hash_previous_block = None
        block.transaction.id = len(self.blocks)

        coin_num = 0
        for coin in block.transaction.created_coins:
            coin.id = CoinId(coin_num, block.transaction.id)
            coin_num += 1

        self.blocks.append(block)
        return block