Exemplo n.º 1
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
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.º 3
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.º 4
0
def TestBlockchain():
    blockchain = Blockchain()

    assert blockchain.chain[0].hash == GENESIS_DATA['hash']
Exemplo n.º 5
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.º 6
0
def testReplaceChainNotLonger(Blockchain3Blocks):
    blockchain = Blockchain()

    with pytest.raises(Exception, match='The incoming chain must be longer'):
        Blockchain3Blocks.replaceChain(blockchain.chain)
Exemplo n.º 7
0
def testReplaceChain(Blockchain3Blocks):
    blockchain = Blockchain()
    blockchain.replaceChain(Blockchain3Blocks.chain)

    assert blockchain.chain == Blockchain3Blocks.chain
Exemplo n.º 8
0
def Blockchain3Blocks():
    blockchain = Blockchain()
    for i in range(3):
        blockchain.addBlock([Trasaction(Wallet(), 'recipient', i).to_json()])
        return blockchain
Exemplo n.º 9
0
def testAddBlock():
    blockchain = Blockchain()
    data = 'test-data'
    blockchain.addBlock(data)

    assert blockchain.chain[-1].data == data