

class TransactionPool():
    def __init__(self):
        self.transaction_map = {}

    def set_transaction(self, transaction):
        """
        Append a transaction to the transaction pool
        :param transaction:
        :return:
        """
        self.transaction_map[transaction.id] = transaction

    def existing_transaction(self, address):
        """
        Find a transaction generated by the address
        :param address:
        :return:
        """
        for transaction in self.transaction_map.values():
            if transaction.input.get('address') == address:
                return transaction

    def transaction_data(self):
        """
        Return transaction_pool data in json format
        :return:
        """
        return list(map(lambda transaction: transaction.to_json(), self.transaction_map.values()))

    def clear_blockchain_transactions(self, blockchain):
        """
        Delete blockchain recorded transactions from the transaction pool
        :param blockchain:
        :return:
        """
        for block in blockchain.chain:
            for transaction in block.data:
                try:
                    del self.transaction_map[transaction.get('id')]
                except KeyError:
                    pass
