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 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'
Пример #3
0
def test_transaction():
    sender_wallet = Wallet()
    recipient = 'deadly_recipient'
    amount = 50
    transaction = Transaction(sender_wallet, recipient, amount)

    assert transaction.output.get(recipient) == amount
    assert transaction.output.get(
        sender_wallet.address) == sender_wallet.balance - amount
    assert 'timestamp' in transaction.input
    assert transaction.input.get('amount') == sender_wallet.balance
    assert transaction.input.get('address') == sender_wallet.address
    assert transaction.input.get('public_key') == sender_wallet.public_key
    assert Wallet.verify(transaction.input.get('public_key'),
                         transaction.output,
                         transaction.input.get('signature'))
Пример #4
0
def test_transaction():
    """
    Test and verify transaction after creation
    """
    sender_wallet = Wallet()
    recipient = 'recipient'
    amount = 50

    transaction = Transaction().new_transaction(sender_wallet, recipient,
                                                amount)

    assert transaction.outputs[
        'sender_amount'] == sender_wallet.balance - amount
    assert transaction.outputs['recipient_amount'] == amount
    assert transaction.input['amount'] == sender_wallet.balance
    assert Transaction.verify_transaction(transaction) is True
Пример #5
0
def test_transaction_update():
    # Block Wallet that will be subtracting amounts from.
    sender_wallet = Wallet()

    # Set the first recipient, the first amount that will be subtracted from
    # the Block's wallet, and the transaction item that will be used in the
    # process.
    recipient1 = 'first_recipient'
    amount1 = 50
    trans = Transaction(sender_wallet, recipient1, amount1)

    # Set the second recipient, the second amount that will be subtracted from
    # the Block's wallet, and the transaction item that will be used in the
    # process.
    recipient2 = 'next_recipient'
    amount2 = 75
    trans.update(sender_wallet, recipient2, amount2)

    # Assert that the transaction output for the recipient2 is amount2.
    assert(trans.output[recipient2] == amount2)
    # Assert that the sender wallet in the output is updated.
    updated_balance = sender_wallet.balance - amount1 - amount2
    assert(trans.output[sender_wallet.address] == updated_balance)
    # Run transaction through the Wallet.verify() to verify the signature and
    # assert that it is verified.
    assert(Wallet.verify(trans.input['public_key'], trans.output,
           trans.input['signature']))

    # Make an update to the first recipient using the wallet and new third
    # amount.
    amount3 = 25
    trans.update(sender_wallet, recipient1, amount3)

    # Assert that the total amount recieved by recipient1 during the
    # transactions are the sum total of amount1 + amount3.
    total_amount_added = amount1 + amount3
    assert(trans.output[recipient1] == total_amount_added)

    # Assert amount1, amount2, and amount3 was subtracted from wallet.
    total_update_change = (sender_wallet.balance - amount1 - amount2 - amount3)
    assert(trans.output[sender_wallet.address] == total_update_change)

    # Run transaction through the Wallet.verify() to verify the signature and
    # assert that it is verified.
    assert(Wallet.verify(trans.input['public_key'], trans.output,
           trans.input['signature']))
def test_transaction_update_new_recipient():
    sender_wallet = Wallet()
    recipient = "recipient"
    amount = 100
    transaction = Transaction(sender_wallet, recipient, amount)

    new_recipient = 'new_recipient'
    new_amount = 50
    transaction.update_transaction(sender_wallet, new_recipient, new_amount)

    assert transaction.output[
        sender_wallet.wallet_id] == sender_wallet.balance - (amount +
                                                             new_amount)
    assert transaction.output[new_recipient] == new_amount
    assert sender_wallet.verify(transaction.input['public_key'],
                                transaction.output,
                                transaction.input['signature'])
Пример #7
0
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"]
    )
Пример #8
0
def test_transaction_update():
    sender_wallet = Wallet()
    first_recipient = 'recipient1'
    first_amount = 50

    transaction = Transaction(sender_wallet, first_recipient, first_amount)

    next_recipient = 'recipient2'
    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 - next_amount - first_amount
    assert Wallet.verify(transaction.input['public_key'], transaction.output,
                         transaction.input['signature'])
Пример #9
0
def test_update_transaction_same_recipient():
    """
    Test update transaction with the same recipient
    """
    sender_wallet = Wallet()
    recipient = 'recipient'
    amount = 50

    transaction = Transaction().new_transaction(sender_wallet, recipient,
                                                amount)

    new_amount = 30
    new_recipient = 'recipient'

    transaction = transaction.update(sender_wallet, new_recipient, new_amount)

    assert transaction.outputs['recipient_address'] == new_recipient
    assert transaction.outputs['recipient_amount'] == new_amount
    assert transaction.outputs[
        'sender_amount'] == sender_wallet.balance - amount - new_amount
Пример #10
0
def test_transaction():
    wallet_for_sender = Wallet()
    recipient = 'recipient'
    amount = 50
    transaction = SystemTransactions(wallet_for_sender, recipient, amount)

    assert transaction.transaction_output[recipient] == amount
    assert transaction.transaction_output[
        wallet_for_sender.address] == wallet_for_sender.wallet_balance - amount

    assert 'timestamp' in transaction.transaction_input
    assert transaction.transaction_input[
        'amount'] == wallet_for_sender.wallet_balance
    assert transaction.transaction_input[
        'address'] == wallet_for_sender.address
    assert transaction.transaction_input[
        'public_key'] == wallet_for_sender.public_key

    assert Wallet.verify_signature(transaction.transaction_input['public_key'],
                                   transaction.transaction_output,
                                   transaction.transaction_input['signature'])
def test_transaction():
    sender_wallet = Wallet()
    amount = 20
    recipient = 'recipient'
    transaction = Transaction(sender_wallet, recipient, amount)

    assert isinstance(transaction, Transaction)
    assert isinstance(sender_wallet, Wallet)

    assert transaction.output[recipient] == amount
    assert transaction.output[
        sender_wallet.wallet_id] == sender_wallet.balance - amount

    assert 'timestamp' in transaction.input
    assert transaction.input['amount'] == sender_wallet.balance
    assert transaction.input['public_key'] == sender_wallet.public_key
    assert transaction.input['address'] == sender_wallet.wallet_id

    assert sender_wallet.verify(transaction.input['public_key'],
                                transaction.output,
                                transaction.input['signature'])
Пример #12
0
def test_transaction_update():
    sender_wallet = Wallet()
    recipient = 'recipient'
    new_recipient = 'new_recipient'
    amount = 50
    new_amount = 150
    transaction = Transaction(sender_wallet, recipient, amount)
    transaction.update(sender_wallet, new_recipient, new_amount)
    assert transaction.output[new_recipient] == new_amount
    assert transaction.output[
        sender_wallet.address] == sender_wallet.balance - new_amount - amount
    assert Wallet.verify(transaction.input['public_key'], transaction.output,
                         transaction.input['signature'])

    to_first_again_amount = 25
    transaction.update(sender_wallet, recipient, to_first_again_amount)
    assert transaction.output[recipient] == amount + to_first_again_amount
    assert transaction.output[
        sender_wallet.
        address] == sender_wallet.balance - new_amount - amount - to_first_again_amount
    assert Wallet.verify(transaction.input['public_key'], transaction.output,
                         transaction.input['signature'])
Пример #13
0
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"],
    )
Пример #14
0
def test_transaction():
    sender_wallet = Wallet()
    recipient = 'recipient'
    amount = 50
    trans = Transaction(sender_wallet, recipient, amount)
    # Assert that transaction amount of recipient is equal to the true amount
    assert(trans.output[recipient] == amount)
    # Assert that the change in the transaction amount is valid.
    trans_delta = sender_wallet.balance - amount 
    assert(trans.output[sender_wallet.address] == trans_delta)
    # Assert that the timestamp exists in the transaction input to verify that
    # it is a valide transaction.
    assert('timestamp' in trans.input)
    # Assert that the transaction input's amount is equal the wallet's balance.
    assert(trans.input['amount'] == sender_wallet.balance)
    # Assert that the transaction input's address is the true wallet address.
    assert(trans.input['address'] == sender_wallet.address)
    # Assert that the transaction input's public key is the true public key
    # of the wallet.
    assert(trans.input['public_key'] == sender_wallet.public_key)
    # At this point, all data seems to be correct. Run transaction through the
    # Wallet.verify() to verify the signature and assert that it is verified.
    assert(Wallet.verify(trans.input['public_key'], trans.output,
           trans.input['signature']))
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)
import os
import random
import requests
from backend.config import FRONTEND_ADDRESS
from flask import Flask, jsonify, request
from flask_cors import CORS
from backend.blockchain.blockchain import Blockchain
from backend.pubsub import PubSub
from backend.wallet.wallet import Wallet
from backend.wallet.transaction import Transaction
from backend.wallet.transaction_pool import TransactionPool

app = Flask(__name__)
CORS(app, resources={ r"/*": { "origins": f"{FRONTEND_ADDRESS}"} })  # add CORS Policy for all endpoints
blockchain = Blockchain()
wallet = Wallet(blockchain)
transaction_pool = TransactionPool()
pubsub = PubSub(blockchain, transaction_pool)

@app.route("/")
def default_route():
    return "Welcome"

@app.route("/blockchain")
def blockchain_route():
    return jsonify(blockchain.serialize_to_json())

@app.route("/blockchain/range")
def paginate_blockchain_route():
    # http://localhost:5000/blockchain/range?start={start}&end={end}
    start = int(request.args.get("start"))
def test_transaction_exceeds_balance():
    with pytest.raises(Exception, match='Amount exceeds balance'):
        Transaction(Wallet(), 'recipient', 9001)
Пример #18
0
def test_verify_valid_signature():
    data = {'foo': 'test_data'}
    wallet = Wallet()
    signature = wallet.sign(data)
    assert Wallet.verify(wallet.public_key, data, signature)
def test_transaction_exceeds_balance():
    with pytest.raises(Exception, match="The amount exceeds sender's balance"):
        Transaction(Wallet(), 'recipient', 9000)
Пример #20
0

def post_wallet_transact(recipient, amount):
    return requests.post(f'{BASE_URL}/wallet/transact',
                         json={
                             'recipient': recipient,
                             'amount': amount
                         }).json()


def get_wallet_info():
    return requests.get(f'{BASE_URL}/wallet/info').json()


start_blockchain = get_blockchain()
print(f'start_blockchain: {start_blockchain}')

recipient = Wallet().address
post_wallet_transact_1 = post_wallet_transact(recipient, 21)
print(f'\n post_wallet_transact_1: {post_wallet_transact_1}')

time.sleep(1)
post_wallet_transact_2 = post_wallet_transact(recipient, 13)
print(f'\n post_wallet_transact_2: {post_wallet_transact_2}')

time.sleep(1)
mined_block = get_blockchain_mine()
print(f'\n mined_block: {mined_block}')

wallet_info = get_wallet_info()
print(f'\nwallet_info: {wallet_info}')
Пример #21
0
def test_for_duplicate_transactions_in_is_valid_transaction_chain(three_block_blockchain):
    transaction = SystemTransactions(Wallet(), 'recipient', 1).convert_transaction_data_to_json()
    three_block_blockchain.add_block([transaction, transaction])

    with pytest.raises(Exception, match='is not unique'):
        Blockchain.transaction_chain_is_valid(three_block_blockchain.chain)
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():
    Transaction.is_valid_transaction(Transaction(Wallet(), 'recipient', 50))
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)
def test_valid_transaction_with_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)
Пример #26
0
def three_block_blockchain():
    blockchain = Blockchain()
    for i in range(3):
        blockchain.add_block([SystemTransactions(Wallet(), 'recipient', i).convert_transaction_data_to_json()])
    return blockchain
def test_valid_reward_transaction():
    reward_transaction = Transaction.reward_transaction(Wallet())
    Transaction.is_valid_transaction(reward_transaction)
Пример #28
0
from backend.wallet.transaction import Transaction
from backend.wallet.transaction_pool import TransactionPool
from flask_cors import CORS
from waitress import serve
import logging
from paste.translogger import TransLogger

logger = logging.getLogger('waitress')
logger.setLevel(logging.INFO)

app = Flask(__name__)

CORS(app, resoources={r'/*': {'origins': 'http://*****:*****@app.route('/')
def route_home():
    return "Home"


@app.route('/blockchain')
def route_blockchain():
    return jsonify(blockchain.to_json())


@app.route('/blockchain/range')
Пример #29
0
import os
import requests
import random

from flask import Flask, jsonify, request

from backend.blockchain.blockchain import Blockchain
from backend.wallet.wallet import Wallet
from backend.wallet.transaction import Transaction
from backend.wallet.transaction_pool import TransactionPool
from backend.pubsub import PubSub

app = Flask(__name__)
blockchain = Blockchain()
wallet = Wallet(blockchain)
transaction_pool = TransactionPool()
pubsub = PubSub(blockchain, transaction_pool)


@app.route("/")
def route_default():
    return "Welcome to the blockchain"


@app.route("/blockchain")
def route_blockchain():
    return jsonify(blockchain.to_json())


@app.route("/blockchain/mine")
def route_blockchain_mine():
Пример #30
0
def blockchain_three_blocks():
    blockchain = Blockchain()
    for i in range(3):
        blockchain.add_block(
            list([Transaction(Wallet(), 'recipient', i).to_json()]))
    return blockchain