Exemplo n.º 1
0
def testIsValidTransactionChainBadTransaction(Blockchain3Blocks):
    badTransaction = Trasaction(Wallet(), 'recipient', 1)
    badTransaction.input['signature'] = Wallet().sign(badTransaction.output)
    Blockchain3Blocks.addBlock([badTransaction.to_json()])

    with pytest.raises(Exception):
        Blockchain.isValidTransactionChain(Blockchain3Blocks)
Exemplo n.º 2
0
def testisValidTransactionChainDuplicate(Blockchain3Blocks):
    transaction = Trasaction(Wallet(), 'recipient', 1).to_json()

    Blockchain3Blocks.addBlock([transaction, transaction])

    with pytest.raises(Exception, match='is not unique'):
        Blockchain.isValidTransactionChain(Blockchain3Blocks.chain)
Exemplo n.º 3
0
def testIsValidTransactionChainMultiRewards(Blockchain3Blocks):
    rewardOne = Trasaction.rewardTransaction(Wallet()).to_json()
    rewardTwo = Trasaction.rewardTransaction(Wallet()).to_json()

    Blockchain3Blocks.addBlock([rewardOne, rewardTwo])

    with pytest.raises(Exception, match='one mining reward per block'):
        Blockchain.isValidTransactionChain(Blockchain3Blocks.chain)
Exemplo n.º 4
0
def testIsValidTransactionChainBadHistoricBalance(Blockchain3Blocks):
    wallet = Wallet()
    badTransaction = Trasaction(wallet, 'recipient', 1)
    badTransaction.output[wallet.address] = 9000
    badTransaction.input['amount'] = 9001
    badTransaction.input['signature'] = wallet.sign(badTransaction.output)

    Blockchain3Blocks.addBlock([badTransaction.to_json()])

    with pytest.raises(Exception, match='has an invalid input amount'):
        Blockchain.isValidTransactionChain(Blockchain3Blocks.chain)
def testClear():
    transactionPool = TransactionPool()
    transaction1 = Trasaction(Wallet(), 'recipient', 1)
    transaction2 = Trasaction(Wallet(), 'recipient', 2)

    transactionPool.setTransaction(transaction1)
    transactionPool.setTransaction(transaction2)

    blockchain = Blockchain()
    blockchain.addBlock([transaction1.to_json(), transaction2.to_json()])

    assert transaction1.id in transactionPool.transcationMap
    assert transaction2.id in transactionPool.transcationMap

    transactionPool.clearTransaction(blockchain)

    assert not transaction1.id in transactionPool.transcationMap
    assert not transaction2.id in transactionPool.transcationMap
Exemplo n.º 6
0
def testCalculateBalance():
    blockchain = Blockchain()
    wallet = Wallet()

    assert Wallet.calculateBalance(blockchain,
                                   wallet.address) == STARTING_BALANCE

    amount = 50
    transaction = Trasaction(wallet, 'recipient', amount)
    blockchain.addBlock(transaction.to_json())

    assert Wallet.calculateBalance(blockchain,
                                   wallet.address) == STARTING_BALANCE - amount

    recoveryAmount1 = 25
    recoveryTransaction1 = Trasaction(Wallet(), wallet.address,
                                      recoveryAmount1)

    recoveryAmount2 = 43
    recoveryTransaction2 = Trasaction(Wallet(), wallet.address,
                                      recoveryAmount2)

    blockchain.addBlock(
        [recoveryTransaction1.to_json(),
         recoveryTransaction2.to_json()])

    assert Wallet.calculateBalance(blockchain, wallet.address) == \
        STARTING_BALANCE - amount + recoveryAmount1 + recoveryAmount2
Exemplo n.º 7
0
def testValidTransactionChain(Blockchain3Blocks):
    Blockchain.isValidTransactionChain(Blockchain3Blocks.chain)
Exemplo n.º 8
0
def TestBlockchain():
    blockchain = Blockchain()

    assert blockchain.chain[0].hash == GENESIS_DATA['hash']
Exemplo n.º 9
0
def testReplaceChainBadChain(Blockchain3Blocks):
    blockchain = Blockchain()
    Blockchain3Blocks.chain[1].hash = 'evil_hash'

    with pytest.raises(Exception, match='The incoming chain is invalid'):
        blockchain.replaceChain(Blockchain3Blocks.chain)
Exemplo n.º 10
0
def testReplaceChain(Blockchain3Blocks):
    blockchain = Blockchain()
    blockchain.replaceChain(Blockchain3Blocks.chain)

    assert blockchain.chain == Blockchain3Blocks.chain
Exemplo n.º 11
0
def testIsValidChainBadGenesis(Blockchain3Blocks):
    Blockchain3Blocks.chain[0].hash = 'evil_hash'

    with pytest.raises(Exception, match='The genesis block must be valid'):
        Blockchain.isValidChain(Blockchain3Blocks.chain)
Exemplo n.º 12
0
def testIsValidChain(Blockchain3Blocks):
    Blockchain.isValidChain(Blockchain3Blocks.chain)
Exemplo n.º 13
0
def Blockchain3Blocks():
    blockchain = Blockchain()
    for i in range(3):
        blockchain.addBlock([Trasaction(Wallet(), 'recipient', i).to_json()])
        return blockchain
Exemplo n.º 14
0
def testAddBlock():
    blockchain = Blockchain()
    data = 'test-data'
    blockchain.addBlock(data)

    assert blockchain.chain[-1].data == data
Exemplo n.º 15
0
import os
import random
import requests

from flask import Flask, jsonify, request
from BackendSide.Blockchain.Blockchain import Blockchain
from BackendSide.Wallet.Transaction import Trasaction
from BackendSide.Wallet.wallet import Wallet
from BackendSide.Wallet.TransactionPool import TransactionPool
from BackendSide.PubNubSub import PubSub

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


@app.route('/')
def Default():
    return 'Welcome to blockchain'


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


@app.route('/wallet/transaction', methods=['POST'])
def routeWalletTransacation():
    # {'recipient': 'foo', 'amount': 15}
Exemplo n.º 16
0
def testReplaceChainNotLonger(Blockchain3Blocks):
    blockchain = Blockchain()

    with pytest.raises(Exception, match='The incoming chain must be longer'):
        Blockchain3Blocks.replaceChain(blockchain.chain)
Exemplo n.º 17
0
import time

from BackendSide.Blockchain.Blockchain import Blockchain
from BackendSide.config import SEC

blockchain = Blockchain()

times = []

for i in range(1000):
    startTime = time.time_ns()
    blockchain.addBlock(i)
    endTime = time.time_ns()
    # Czas kopania
    timeMine = endTime - startTime / SEC
    # Pojedyńczy element dla czasu kopania
    times.append(timeMine)

    averageTime = sum(times) / len(times)

    print(f'New block difficulty:{blockchain.chain[-1].dificulty}')
    print(f'Time mine to new block:{timeMine}s')
    print(f'Average Time to add a new block:{averageTime}s\n')