コード例 #1
0
def test_is_transaction_chain_valid_multiple_rewards(blockchain_ten_blocks):
    reward1 = Transaction.reward(Wallet()).to_json()
    reward2 = Transaction.reward(Wallet()).to_json()
    blockchain_ten_blocks.add_block([reward1, reward2])

    with pytest.raises(Exception, match="Duplicate mining reward in block"):
        BlockChain.is_transaction_chain_valid(blockchain_ten_blocks.chain)
コード例 #2
0
def test_is_transaction_chain_valid_corrupt_transaction(blockchain_ten_blocks):
    corrupt_transaction = Transaction(Wallet(), 'recipient', 1)

    corrupt_transaction.input['signature'] = Wallet().sign({"a": "b"})
    blockchain_ten_blocks.add_block([corrupt_transaction.to_json()])
    with pytest.raises(Exception):
        BlockChain.is_transaction_chain_valid(blockchain_ten_blocks.chain)
コード例 #3
0
def test_is_transaction_chain_valid_balance_tillnow(blockchain_ten_blocks):
    wallet = Wallet()
    corrupt_transaction = Transaction(wallet, "abcd", 10)
    corrupt_transaction.output[wallet.address] = 9000
    corrupt_transaction.input['amount'] = 9010
    corrupt_transaction.input['signature'] = wallet.sign(
        corrupt_transaction.output)

    blockchain_ten_blocks.add_block([corrupt_transaction.to_json()])

    with pytest.raises(Exception):
        BlockChain.is_transaction_chain_valid(blockchain_ten_blocks.chain)
コード例 #4
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
コード例 #5
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
コード例 #6
0
def test_blockchain():
    blockchain = BlockChain()

    assert blockchain.chain[0].hash == GENESIS_DATA['hash']
コード例 #7
0
def test_add_block():
    blockchain = BlockChain()
    data = "test_data"
    blockchain.add_block(data)

    assert blockchain.chain[1].data == data
コード例 #8
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)
コード例 #9
0
def test_replace_chain(blockchain_ten_blocks):
    blockchain = BlockChain()
    blockchain.replace_chain(blockchain_ten_blocks.chain)

    assert blockchain.chain == blockchain_ten_blocks.chain
コード例 #10
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)
コード例 #11
0
def test_is_chain_valid_bad_genesis(blockchain_ten_blocks):
    blockchain_ten_blocks.chain[0].hash = "bad_genesis_block"
    with pytest.raises(Exception, match="The genesis block is not valid"):
        BlockChain.is_chain_valid(blockchain_ten_blocks.chain)
コード例 #12
0
def test_is_chain_valid(blockchain_ten_blocks):
    BlockChain.is_chain_valid(blockchain_ten_blocks.chain)
コード例 #13
0
def blockchain_ten_blocks():
    blockchain = BlockChain()
    for i in range(5):
        blockchain.add_block([Transaction(Wallet(), 'abcd', i).to_json()])
    return blockchain
コード例 #14
0
from backend.blockchain.blockchain import BlockChain
import time
from backend.config import SECONDS
blockchain = BlockChain()

times = []

for i in range(1000):
    start_time = time.time_ns()
    blockchain.addBlock(i)
    end_time = time.time_ns()
    time_to_mine = (end_time - start_time) / SECONDS
    times.append(time_to_mine)
    average_time = sum(times) / len(times)
    print(blockchain.chain[-1])
    print(time_to_mine)
    print(average_time)
コード例 #15
0
def test_is_transaction_chain_valid(blockchain_ten_blocks):
    BlockChain.is_transaction_chain_valid(blockchain_ten_blocks.chain)
コード例 #16
0
from backend.products.assets import Assets
from backend.blockchain.blockchain import BlockChain

blockchain = BlockChain()
assets = Assets(blockchain=blockchain)
product_id = 2
recipient = '12'
assetTran = AssetsTransaction(sender_wallet=assets,
                              recipient=recipient,
                              product_id=product_id)
コード例 #17
0
def test_is_transaction_chain_valid_duplicate_transactions(
        blockchain_ten_blocks):
    blockchain_ten_blocks.chain.append(blockchain_ten_blocks.chain[3])
    with pytest.raises(Exception, match="is not unique"):
        BlockChain.is_transaction_chain_valid(blockchain_ten_blocks.chain)
コード例 #18
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())

コード例 #19
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()})