def test_invalid_reward_transaction_invalid_amount(): miner_wallet = Wallet() reward_transaction = Transaction.reward_transaction(miner_wallet) reward_transaction.output[miner_wallet.address] = 9001 with pytest.raises(Exception, match='Invalid mining reward'): Transaction.is_valid_transaction(reward_transaction)
def test_valid_transaction_with_invalid_outputs(): sender_wallet = Wallet() transaction = Transaction(sender_wallet, 'recipient', 50) transaction.output[sender_wallet.address] = 9001 with pytest.raises(Exception, match='Invalid transaction output values'): Transaction.is_valid_transaction(transaction)
def test_is_valid_transaction_chain_multiple_rewards(blockchain_three_blocks): reward_1 = Transaction.reward_transaction(Wallet()).to_json() reward_2 = Transaction.reward_transaction(Wallet()).to_json() blockchain_three_blocks.add_block([reward_1, reward_2]) with pytest.raises(Exception, match='one mining reward per block'): Blockchain.is_valid_transaction_chain(blockchain_three_blocks.chain)
def test_is_valid_transaction_chain_bad_transaction(blockchain_three_blocks): bad_transaction = Transaction(Wallet(), 'recipient', 1) bad_transaction.input['signature'] = Wallet().sign(bad_transaction.output) blockchain_three_blocks.add_block([bad_transaction.to_json()]) # No exception match, in case does any of the transaction is corrupted with pytest.raises(Exception): Blockchain.is_valid_transaction_chain(blockchain_three_blocks.chain)
def test_calulate_balance(): blockchain = Blockchain() wallet = Wallet() assert Wallet.calculate_balance(blockchain, wallet.address) == STARTING_BALANCE amount = 50 transaction = Transaction(wallet, 'recipient', amount) blockchain.add_block([transaction.to_json()]) assert Wallet.calculate_balance(blockchain, wallet.address) == \ STARTING_BALANCE - amount received_amount_1 = 25 received_transaction_1 = Transaction(Wallet(), wallet.address, received_amount_1) received_amount_2 = 123 received_transaction_2 = Transaction(Wallet(), wallet.address, received_amount_2) blockchain.add_block( [received_transaction_1.to_json(), received_transaction_2.to_json()]) assert Wallet.calculate_balance(blockchain, wallet.address) == \ STARTING_BALANCE - amount + received_amount_1 + received_amount_2
def test_is_valid_transaction_chain_bad_historic_balance(blockchain_three_blocks): wallet = Wallet() bad_transaction = Transaction(wallet, 'recipient', 1) bad_transaction.output[wallet.address] = 9000 bad_transaction.input['amount'] = 9001 bad_transaction.input['signature'] = wallet.sign(bad_transaction.output) blockchain_three_blocks.add_block([bad_transaction.to_json()]) with pytest.raises(Exception, match='has an invalid input amount'): Blockchain.is_valid_transaction_chain(blockchain_three_blocks.chain)
def route_wallet_transact(): transaction_data = request.get_json() transaction = transaction_pool.existing_transaction(wallet.address) if transaction: transaction.update(wallet, transaction_data['recipient'], transaction_data['amount']) else: transaction = Transaction(wallet, transaction_data['recipient'], transaction_data['amount']) PubSub.broadcast_transaction(transaction) return jsonify(transaction.to_json())
def route_blockchain_mine(): transaction_data = transaction_pool.transaction_data() transaction_data.append(Transaction.reward_transaction(wallet).to_json()) blockchain.add_block(transaction_data) block = blockchain.chain[-1] pubsub.broadcast_block(block) transaction_pool.clear_blockchain_transactions(blockchain)
def test_is_valid_transaction_chain_duplicate_transactions(blockchain_three_blocks): transaction = Transaction(Wallet(), 'recipient', 1).to_json() blockchain_three_blocks.add_block([transaction, transaction]) with pytest.raises(Exception, match='is not unique'): Blockchain.is_valid_transaction_chain(blockchain_three_blocks.chain)
def is_valid_transaction_chain(chain): """ Enforce the rules of a chain composed of blocks of transactions. - Each transaction must only appear once in the chain. - There can be one mining reward per block. - Each transaction must be valid. """ transaction_ids = set() for i in range(len(chain)): block = chain[i] has_mining_reward = False for transaction_json in block.data: transaction = Transaction.from_json(transaction_json) if transaction.id in transaction_ids: raise Exception( f'Transaction: {transaction.id} is not unique') transaction_ids.add(transaction.id) if transaction.input == MINING_REWARD_INPUT: if has_mining_reward: raise Exception( 'There can only be one mining reward per block.'\ f'Check block with hash: {block.hash}' ) has_mining_reward = True else: historic_blockchain = Blockchain() historic_blockchain.chain = chain[0:i] historic_balance = Wallet.calculate_balance( historic_blockchain, transaction.input['address']) if historic_balance != transaction.input['amount']: raise Exception( f'Transaction {transaction.id} has an invalid '\ 'input amount') Transaction.is_valid_transaction(transaction)
def test_transaction_update(): sender_wallet = Wallet() first_recipient = 'first_recipient' first_amount = 50 transaction = Transaction(sender_wallet, first_recipient, first_amount) next_recipient = 'next_recipient' next_amount = 75 transaction.update(sender_wallet, next_recipient, next_amount) assert transaction.output[next_recipient] == next_amount assert transaction.output[sender_wallet.address] ==\ sender_wallet.balance - first_amount - next_amount assert Wallet.verify(transaction.input['public_key'], transaction.output, transaction.input['signature']) to_first_again_amount = 25 transaction.update(sender_wallet, first_recipient, to_first_again_amount) assert transaction.output[first_recipient] ==\ first_amount + to_first_again_amount assert transaction.output[sender_wallet.address] ==\ sender_wallet.balance - first_amount - next_amount - to_first_again_amount assert Wallet.verify(transaction.input['public_key'], transaction.output, transaction.input['signature'])
def test_transaction(): sender_wallet = Wallet() recipient = 'recipient' amount = 50 transaction = Transaction(sender_wallet, recipient, amount) assert transaction.output[recipient] == amount assert transaction.output[ sender_wallet.address] == sender_wallet.balance - amount assert 'timestamp' in transaction.input assert transaction.input['amount'] == sender_wallet.balance assert transaction.input['address'] == sender_wallet.address assert transaction.input['public_key'] == sender_wallet.public_key assert Wallet.verify(transaction.input['public_key'], transaction.output, transaction.input['signature'])
def message(self, pubnub, message_object): print( f'\n-- Channel: {message_object.channel} | Message: {message_object.message}' ) if message_object.channel == CHANNELS['BLOCK']: block = Block.from_json(message_object.message) # [:] -> all the elements in the array, in that case all blocks in chain potential_chain = self.blockchain.chain[:] potential_chain.append(block) try: self.blockchain.replace_chain(potential_chain) self.transaction_pool.clear_blockchain_transactions( self.blockchain) print('\n -- Successfully replaced the local chain') except Exception as e: print(f'\n -- Did not replace chain: {e}') elif message_object.channel == CHANNELS['TRANSACTION']: transaction = Transaction.from_json(message_object.message) self.transaction_pool.set_transaction(transaction) print('\n -- Set the new transaction in the transaction pool')
def test_clear_blockchain_transaction(): transaction_pool = TransactionPool() transaction_1 = Transaction(Wallet(), 'recipient', 1) transaction_2 = Transaction(Wallet(), 'recipient', 2) transaction_pool.set_transaction(transaction_1) transaction_pool.set_transaction(transaction_2) blockchain = Blockchain() blockchain.add_block([transaction_1.to_json(), transaction_2.to_json()]) assert transaction_1.id in transaction_pool.transaction_map assert transaction_2.id in transaction_pool.transaction_map transaction_pool.clear_blockchain_transactions(blockchain) assert not transaction_1.id in transaction_pool.transaction_map assert not transaction_2.id in transaction_pool.transaction_map
def blockchain_three_blocks(): blockchain = Blockchain() for i in range(3): blockchain.add_block([Transaction(Wallet(), 'recipient', i).to_json()]) return blockchain
@BLOCKCHAIN.route('/mine') def route_blockchain_mine(): transaction_data = transaction_pool.transaction_data() transaction_data.append(Transaction.reward_transaction(wallet).to_json()) blockchain.add_block(transaction_data) block = blockchain.chain[-1] pubsub.broadcast_block(block) transaction_pool.clear_blockchain_transactions(blockchain) @BLOCKCHAIN.route('known-addresses', methods=['GET']) def route_known_adresses(): known_addresses = set() for block in blockchain.chain: for transaction in block.data: known_addresses.update(transaction['output'].keys()) return jsonify(list(known_addresses)) if os.environ.get('SEED_DATA') == 'True': for i in range(10): blockchain.add_block([ Transaction(Wallet(), Wallet().address, random.randint(5, 100)).to_json(), Transaction(Wallet(), Wallet().address, random.randint(5, 100)).to_json() ])
def test_transaction_exceeds_balance(): with pytest.raises(Exception, match='Amount exceeds balance'): Transaction(Wallet(), 'recipient', 9001)
def test_transaction_update_exceeds_balance(): sender_wallet = Wallet() transaction = Transaction(sender_wallet, 'recipient', 50) with pytest.raises(Exception, match='Amount exceeds balance'): transaction.update(sender_wallet, 'new_recipient', 9001)
def test_valid_transaction(): Transaction.is_valid_transaction(Transaction(Wallet(), 'recipient', 50))
def test_valid_reward_transaction(): reward_transaction = Transaction.reward_transaction(Wallet()) Transaction.is_valid_transaction(reward_transaction)
def test_reward_transaction(): miner_wallet = Wallet() transaction = Transaction.reward_transaction(miner_wallet) assert transaction.input == MINING_REWARD_INPUT assert transaction.output[miner_wallet.address] == MINING_REWARD
def test_valid_transaction_invalid_signature(): transaction = Transaction(Wallet(), 'recipient', 50) transaction.input['signature'] = Wallet().sign(transaction.output) with pytest.raises(Exception, match='Invalid signature'): Transaction.is_valid_transaction(transaction)
def test_set_transaction(): transaction_pool = TransactionPool() transaction = Transaction(Wallet(), 'recipient', 1) transaction_pool.set_transaction(transaction) assert transaction_pool.transaction_map[transaction.id] == transaction
def test_invalid_reward_transaction_extra_recipient(): reward_transaction = Transaction.reward_transaction(Wallet()) reward_transaction.output['extra_recipient'] = 60 with pytest.raises(Exception, match='Invalid mining reward'): Transaction.is_valid_transaction(reward_transaction)