Ejemplo n.º 1
0
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
Ejemplo n.º 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
Ejemplo n.º 3
0
from backend.wallet.wallet import Wallet
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())

Ejemplo n.º 4
0
def test_replace_chain_invalid_incoming_chain(blockchain_ten_blocks):
    blockchain = BlockChain()
    blockchain_ten_blocks.chain[1].hash = "tampered hash"
    with pytest.raises(Exception,
                       match="Cannot replace. The incoming chain is invalid"):
        blockchain.replace_chain(blockchain_ten_blocks.chain)
Ejemplo n.º 5
0
def test_blockchain():
    blockchain = BlockChain()

    assert blockchain.chain[0].hash == GENESIS_DATA['hash']
Ejemplo n.º 6
0
def test_replace_chain_not_longer(blockchain_ten_blocks):
    blockchain = BlockChain()
    with pytest.raises(
            Exception,
            match="Incoming chain must be longer than the local chain"):
        blockchain_ten_blocks.replace_chain(blockchain.chain)
Ejemplo n.º 7
0
def test_replace_chain(blockchain_ten_blocks):
    blockchain = BlockChain()
    blockchain.replace_chain(blockchain_ten_blocks.chain)

    assert blockchain.chain == blockchain_ten_blocks.chain
Ejemplo n.º 8
0
def blockchain_ten_blocks():
    blockchain = BlockChain()
    for i in range(5):
        blockchain.add_block([Transaction(Wallet(), 'abcd', i).to_json()])
    return blockchain
Ejemplo n.º 9
0
def test_add_block():
    blockchain = BlockChain()
    data = "test_data"
    blockchain.add_block(data)

    assert blockchain.chain[1].data == data
Ejemplo n.º 10
0
from backend.blockchain.blockchain import BlockChain
from backend.blockchain.block import Block
from backend.pubsub import PubSub
from django.contrib.auth.decorators import login_required
from backend.wallet.wallet import Wallet
from backend.wallet.transaction import Transaction
from django.views.decorators.csrf import csrf_exempt
from backend.wallet.transaction_pool import TransactionPool
from backend.products.assets import Assets
from backend.products.assets_transact import AssetsTransaction
from backend.products.assets_transpool import AssetsTransPool
from products.models import Products
from django.forms.models import model_to_dict
from django.contrib.auth.models import User

block = BlockChain()
sender_assets = Assets(block)
wallet = Wallet(block)
assets = Assets(block)
transaction_pool = TransactionPool()
assetTranPool = AssetsTransPool()
pubsub = PubSub(block, transaction_pool, assetTranPool)


# Create your views here.
@login_required
def index(request):
    if assets.address != request.user.username:
        assets.address = request.user.username
    return render(request, 'index.html', {'Date': time.time()})