コード例 #1
0
def test_blockchain_valid_transaction_duplicate_rewardss():
    blockchain = Blockchain()
    transaction = Transactions(Wallet(), 'recipient', 100).to_json()
    reward_transaction = Transactions.transaction_reward(Wallet()).to_json()
    blockchain.add_block([transaction, reward_transaction, reward_transaction])
    with pytest.raises(Exception, match="has multiple rewards"):
        Blockchain.is_chain_transaction_valid(blockchain.chain)
コード例 #2
0
def test_wallet_balance_amount():
	wallet = Wallet()
	blockchain = Blockchain()
	amount = 34
	tr1 = Transactions(wallet, 'recp1', amount)
	blockchain.add_block([tr1.to_json()])
	assert Wallet.calculate_balance(blockchain, wallet.address) == STARTING_BALANCE - amount
コード例 #3
0
def blockchain():
    blockchain = Blockchain()
    for i in range(3):
        miner_wallet = Wallet()
        transaction = Transactions(Wallet(), 'recipient', i)
        reward_transaction = Transactions.transaction_reward(miner_wallet)
        blockchain.add_block([transaction.to_json()])
        return blockchain
コード例 #4
0
def test_blockchain_valid_transaction_incorrect_historical_balance(blockchain):
    wallet = Wallet()
    bad_transaction = Transactions(wallet, 'recipient', 1)
    bad_transaction.output[wallet.address] = 9000
    bad_transaction.input['amount'] = 9001
    bad_transaction.input['signature'] = wallet.sign(bad_transaction.output)
    blockchain.add_block([bad_transaction.to_json()])
    with pytest.raises(Exception, match="did not have the correct balance"):
        Blockchain.is_chain_transaction_valid(blockchain.chain)
コード例 #5
0
def make_transaction():
    transaction_json = request.get_json()
    #check if transaction exists
    transaction = transaction_pool.existing_transaction(wallet.address)
    if transaction:
        transaction.update_transaction(wallet, transaction_json['recipient'],
                                       transaction_json['amount'])
    else:
        transaction = Transactions(wallet, transaction_json['recipient'],
                                   transaction_json['amount'])

    #broadcast the transaction object
    pubsub.broadcast_transaction(transaction)
    return jsonify(transaction.to_json())
コード例 #6
0
def test_transaction_clear_transaction():
    transaction_pool = TransactionPool()
    transaction1 = Transactions(Wallet(), 'recp1', 10)
    transaction_pool.set_transaction(transaction1)
    blockchain = Blockchain()
    blockchain.add_block([transaction1.to_json()])
    # assert len(blockchain.chain) > 1
    print(transaction_pool.transaction_map)
    transaction_pool.clear_transaction(blockchain)
    assert transaction1.id not in transaction_pool.transaction_map.keys()
    # for index, block in enumerate(blockchain.chain):
    # 	if index == 0:
    # 		continue
    # 	for tdata in block.data:
    # 		# tdata = ast.literal_eval(tdata)
    # 		assert tdata['id'] == 'str'
コード例 #7
0
def test_transactions_basic():
    wallet = Wallet()
    Transactions(wallet, 'recp1', 100)
コード例 #8
0
def test_verify_transactions_invalid_signature():
    wallet = Wallet()
    transactions = Transactions(wallet, 'recp1', 100)
    transactions.input['signature'] = Wallet().sign(transactions.output)
    with pytest.raises(Exception, match="Invalid Signature"):
        Transactions.verify_transaction(transactions)
コード例 #9
0
def test_verify_transactions_output_values_wrong():
    wallet = Wallet()
    transactions = Transactions(wallet, 'recp1', 100)
    transactions.input['amount'] = 78
    with pytest.raises(Exception, match="Invalid output values"):
        Transactions.verify_transaction(transactions)
コード例 #10
0
def test_verify_transactions():
    wallet = Wallet()
    transactions = Transactions(wallet, 'recp1', 100)
    Transactions.verify_transaction(transactions)
コード例 #11
0
def test_update_same_recp_transactions_invalid():
    wallet = Wallet()
    transactions = Transactions(wallet, 'recp1', 100)
    with pytest.raises(Exception, match="Amount greater than balance"):
        transactions.update_transaction(wallet, 'recp1', 901)
コード例 #12
0
def test_transactions_amt_greater_than_balance():
    wallet = Wallet()
    with pytest.raises(Exception, match="Amount greater than balance"):
        Transactions(wallet, 'recp1', 1001)
コード例 #13
0
def test_blockchain_valid_transaction_duplicate_transactions():
    blockchain = Blockchain()
    transaction = Transactions(Wallet(), 'recipient', 100).to_json()
    blockchain.add_block([transaction, transaction])
    with pytest.raises(Exception, match="is not unique"):
        Blockchain.is_chain_transaction_valid(blockchain.chain)
コード例 #14
0
def test_transaction_pool():
    transaction_pool = TransactionPool()
    transaction1 = Transactions(Wallet(), 'recp1', 10)
    transaction_pool.set_transaction(transaction1)
    assert transaction1.id in transaction_pool.transaction_map.keys()
コード例 #15
0
@app.route('/transactions')
def get_all_transactions():
    transaction_data = transaction_pool.transaction_data()
    return jsonify(transaction_data)


ROOT_PORT = 5000
PORT = ROOT_PORT
if os.getenv('PEER'):
    PORT = random.randint(5001, 6000)
    result = requests.get("http://localhost:%s/blockchain" % (ROOT_PORT))
    result_blockchain = Blockchain.from_json(result.json())
    try:
        blockchain.replace_chain(result_blockchain)
        print("Chain replacement with root node was successful")
    except Exception as e:
        print("Chain replacement was not successful: %s" % (e))

#Seed a few data into the backend to help us evaluate the frontend design
if os.getenv('SEED'):
    for i in range(0, 10):
        blockchain.add_block(
            [Transactions(Wallet(),
                          Wallet().address, i).to_json()])
    for i in range(0, 10):
        transaction_pool.set_transaction(
            Transactions(Wallet(),
                         Wallet().address, i))

app.run(port=PORT)
コード例 #16
0
def test_update_same_recp_transactions():
    wallet = Wallet()
    transactions = Transactions(wallet, 'recp1', 100)
    transactions.update_transaction(wallet, 'recp1', 100)
コード例 #17
0
    @staticmethod
    def calculate_balance(blockchain, address):
        """
		Method to calculate the balances of a Wallet given its address.
		If the wallet's address is in the transaction's input, then we set the balance to the amount in input field.
		If the wallet's address is in the transaction's output, then we add the value in output field to the balance. 
		"""
        balance = STARTING_BALANCE
        #handle the scenario where blockchain is None
        if blockchain is None:
            return balance
        for index, block in enumerate(blockchain.chain):
            if index == 0:
                continue
            for transaction in block.data:
                if transaction['input']['address'] == address:
                    balance = transaction['output'][address]
                elif address in transaction['output']:
                    balance = balance + transaction['output'][address]
        return balance


if __name__ == '__main__':
    from backend.wallet.transactions import Transactions
    from backend.blockchain.blockchain import Blockchain
    wallet = Wallet()
    blockchain = Blockchain()
    amount = 34
    tr1 = Transactions(wallet, 'recp1', amount)
    blockchain.add_block([tr1.to_json()])
    print(Wallet.calculate_balance(blockchain, wallet.address))