def test_clear_transactions_added_to_blockchain():
    transaction_pool = TransactionPool()
    transaction_1 = Transaction(Wallet(), "abcd", 10)
    transaction_2 = Transaction(Wallet(), "abcd", 30)

    transaction_pool.add_transaction(transaction_1)
    transaction_pool.add_transaction(transaction_2)

    blockchain = BlockChain()
    blockchain.add_block([transaction_1.to_json(), transaction_2.to_json()])

    assert transaction_1.id in transaction_pool.all_transactions
    assert transaction_2.id in transaction_pool.all_transactions

    transaction_pool.clear_transactions_added_to_blockchain(blockchain)

    assert not transaction_1.id in transaction_pool.all_transactions
    assert not transaction_2.id in transaction_pool.all_transactions
Beispiel #2
0
def test_calculate_balance():
    blockchain = BlockChain()
    wallet = Wallet()
    assert wallet.calculate_balance(blockchain,
                                    wallet.address) == STARTING_BALANCE

    sent_amount = 100
    transaction = Transaction(wallet, "abcd", sent_amount)
    blockchain.add_block([transaction.to_json()])
    assert wallet.calculate_balance(
        blockchain, wallet.address) == STARTING_BALANCE - sent_amount

    received_amount_1 = 100
    received_1 = Transaction(Wallet(), wallet.address, received_amount_1)

    received_amount_2 = 100
    received_2 = Transaction(Wallet(), wallet.address, received_amount_2)

    blockchain.add_block([received_1.to_json(), received_2.to_json()])
    assert wallet.calculate_balance(blockchain, wallet.address) == STARTING_BALANCE - \
        sent_amount + received_amount_1 + received_amount_2
Beispiel #3
0
            known_address.update(transaction['output'].keys())
    return jsonify(list(known_address))


@app.route('/all-transactions')
def route_all_transactions():
    return jsonify(transaction_pool.transaction_data())


PORT = 5000

if os.environ.get('SEED_DATA') == 'True':
    for i in range(10):
        blockchain.add_block([
            Transaction(Wallet(),
                        Wallet().address, random.randint(2, 20)).to_json(),
            Transaction(Wallet(),
                        Wallet().address, random.randint(2, 20)).to_json()
        ])

    for i in range(5):
        transaction_pool.add_transaction(
            Transaction(Wallet(),
                        Wallet().address, random.randint(2, 50)))

if os.environ.get('PEER') == "True":
    result = requests.get('http://localhost:5000/blockchain')
    full_blockchain = BlockChain.from_json(result.json())
    PORT = random.randint(5001, 6000)
    try:
        blockchain.replace_chain(full_blockchain.chain)
        print("\n -- Successfully synchronized the local blockchain")
Beispiel #4
0
def blockchain_ten_blocks():
    blockchain = BlockChain()
    for i in range(5):
        blockchain.add_block([Transaction(Wallet(), 'abcd', i).to_json()])
    return blockchain
Beispiel #5
0
def test_add_block():
    blockchain = BlockChain()
    data = "test_data"
    blockchain.add_block(data)

    assert blockchain.chain[1].data == data
from backend.blockchain.blockchain import BlockChain
import time
from backend.config import SECONDS

blockchain = BlockChain()

times = []

for i in range(100):
    start_time = time.time_ns()
    blockchain.add_block(i)
    time_to_mine = (time.time_ns() - start_time) / SECONDS
    times.append(time_to_mine)
    print(f"Time to mine: {time_to_mine}")
    print(f"Difficulty of the block: {blockchain.chain[-1].difficulty}")
    print(f"Average time to mine: {sum(times)/len(times)}")

print(sum(times) / len(times))