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
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
Beispiel #3
0
def testAddBlock():
    blockchain = Blockchain()
    data = 'test-data'
    blockchain.addBlock(data)

    assert blockchain.chain[-1].data == data
Beispiel #4
0
def Blockchain3Blocks():
    blockchain = Blockchain()
    for i in range(3):
        blockchain.addBlock([Trasaction(Wallet(), 'recipient', i).to_json()])
        return blockchain
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')