def create_transaction(self, blockchain, amount, recipient, transaction_pool): """ Creates or updates a transaction """ self.balance = self.calculate_balance(blockchain) if amount > self.balance: raise Exception( f'{amount} exceeds current balance: {self.balance}') transaction = transaction_pool.existing_transaction(self.address) if transaction: transaction.update(self, recipient, amount) else: transaction = Transaction.new_transaction(self, recipient, amount) transaction_pool.add_transaction(transaction) return transaction
if os.environ.get('PEER') == 'True': PORT = random.randint(5001, 6000) result = requests.get(f'http://localhost:{ROOT_PORT}/blockchain') new_blockchain = Blockchain.from_json(result.json()) try: blockchain.replace_chain(new_blockchain.chain) print('Synchronized the local blockchain') except Exception as e: print(f'Synchronization error: {e}') for i in range(10): blockchain.add_block([ Transaction.new_transaction(Wallet(), Wallet().address, random.randrange(10, 40, 10)).to_json(), Transaction.new_transaction(Wallet(), Wallet().address, random.randrange(10, 40, 10)).to_json() ]) for i in range(5): transaction_pool.add_transaction( Transaction.new_transaction(Wallet(), Wallet().address, random.randrange(10, 40, 10))) app.run(port=PORT)