示例#1
0
def test_block_chain_create_block(fx_block_chain: BlockChain):
    transaction = Transaction(
        sender=bytes.fromhex('a9596e7414064c778bdc36b76bb2dc2c'),
        recipient=bytes.fromhex('619b9000222b457b978efbca2815d38a'),
        amount=decimal.Decimal('0.125'),
    )
    fx_block_chain.append_transaction(transaction)
    block = fx_block_chain.create_block(
        proof=1234,
        reward_recipient=bytes.fromhex('619b9000222b457b978efbca2815d38a'),
        timestamp=fx_block_chain.last_block.timestamp + 15000,
    )
    assert fx_block_chain.chain[-1] == block
    assert block.index == 3
    assert block.timestamp == 737511533930
    assert len(block.transactions) == 3
    assert transaction in block.transactions
    assert block.proof == 1234
    assert block.previous_hash == bytes.fromhex(
        '46e136b65129b85429f25d777cb20c4c44d91ae46e18b6f540c45135d340c2cb'
    )  # noqa
    assert not fx_block_chain.current_transactions
    assert fx_block_chain.balance_of(
        bytes.fromhex('a9596e7414064c778bdc36b76bb2dc2c')) == 0.125
    assert fx_block_chain.balance_of(
        bytes.fromhex('619b9000222b457b978efbca2815d38a')) == 1.125
示例#2
0
def test_block_chain_create_genesis_block():
    block_chain = BlockChain()
    block_chain.create_genesis_block(
        reward_recipient=bytes.fromhex('5ca60de0575441718094ea0ffcb02aa4'),
        timestamp=737511503930,  # 1993-05-16T09:18:23.930516
    )
    assert block_chain.chain == [
        Block(
            index=1,
            timestamp=737511503930,
            previous_hash=bytes.fromhex(
                '0000000000000000000000000000000000000000000000000000000000000000'
            ),  # noqa
            proof=1,
            transactions=[
                Transaction(
                    sender=bytes.fromhex('00000000000000000000000000000000'),
                    recipient=bytes.fromhex(
                        '5ca60de0575441718094ea0ffcb02aa4'),
                    amount=decimal.Decimal('1'),
                ),
            ],
        ),
    ]
    assert block_chain.current_transactions == []
示例#3
0
def test_block_chain_last_block(fx_block_chain: BlockChain):
    assert fx_block_chain.last_block.index == 2
    assert fx_block_chain.last_block.hash == bytes.fromhex(
        '46e136b65129b85429f25d777cb20c4c44d91ae46e18b6f540c45135d340c2cb'
    )  # noqa

    fx_block_chain.chain = []
    assert fx_block_chain.last_block is None
示例#4
0
def test_block_chain_append_transaction(fx_block_chain: BlockChain):
    transaction = Transaction(
        sender=bytes.fromhex('a9596e7414064c778bdc36b76bb2dc2c'),
        recipient=bytes.fromhex('619b9000222b457b978efbca2815d38a'),
        amount=decimal.Decimal('0.125'),
    )
    assert fx_block_chain.append_transaction(transaction) == 3
    assert len(fx_block_chain.current_transactions) == 2
    assert transaction in fx_block_chain.current_transactions

    invalid_transaction = Transaction(
        sender=bytes.fromhex('00000000000000000000000000000000'),
        recipient=bytes.fromhex('619b9000222b457b978efbca2815d38a'),
        amount=decimal.Decimal('0.00000001'),
    )
    with raises(ValueError) as e:
        fx_block_chain.append_transaction(invalid_transaction)
    assert str(e.value) == 'Mining reward must be 1'
示例#5
0
def test_block_chain_valid_transaction(fx_block_chain: BlockChain):
    invalid_reward_amount_transaction = Transaction(
        sender=bytes.fromhex('00000000000000000000000000000000'),
        recipient=bytes.fromhex('619b9000222b457b978efbca2815d38a'),
        amount=decimal.Decimal('0.00000001'),
    )
    with raises(ValueError) as e:
        fx_block_chain.valid_transaction(invalid_reward_amount_transaction)
    assert str(e.value) == 'Mining reward must be 1'

    lack_of_balance_transaction = Transaction(
        sender=bytes.fromhex('a9596e7414064c778bdc36b76bb2dc2c'),
        recipient=bytes.fromhex('619b9000222b457b978efbca2815d38a'),
        amount=decimal.Decimal('10000000'),
    )
    with raises(ValueError) as e:
        fx_block_chain.valid_transaction(lack_of_balance_transaction)
    assert str(
        e.value) == ('Sender a9596e7414064c778bdc36b76bb2dc2c '
                     'does not have sufficient balance: 10000000 (have 0.25)')
示例#6
0
def fx_block_chain(
    fx_block: Block,
    fx_transaction: Transaction,
) -> BlockChain:
    block_chain = BlockChain(
        chain=[fx_block],
        nodes={'main.uaena.com', 'sub.uaena.com'},
    )
    block_chain.append_transaction(fx_transaction)
    block_chain.create_block(
        proof=1111,
        reward_recipient=bytes.fromhex('33ee49f83681417e82660cb9585d13b1'),
        timestamp=fx_block.timestamp + 15000,  # 1993-05-16T09:18:38.935016
    )
    block_chain.append_transaction(
        Transaction(
            sender=bytes.fromhex('33ee49f83681417e82660cb9585d13b1'),
            recipient=bytes.fromhex('a9596e7414064c778bdc36b76bb2dc2c'),
            amount=decimal.Decimal('0.25'),
        ), )
    return block_chain
示例#7
0
def test_block_chain_valid_proof():
    assert not BlockChain.valid_proof(1234, 62593)
    assert BlockChain.valid_proof(1234, 62594)
    assert not BlockChain.valid_proof(1234, 62595)
示例#8
0
def test_block_chain_register_node(fx_block_chain: BlockChain):
    fx_block_chain.register_node('http://node.uaenapool.com/')
    assert len(fx_block_chain.nodes) == 3
    assert 'node.uaneapool.com' in fx_block_chain.nodes
示例#9
0
import decimal
import typing
import uuid

from flask import Flask, Response, jsonify, request
from typeguard import typechecked

from uaena.block_chain import MINING_REWARD, MINING_REWARD_SENDER, BlockChain

app = Flask(__name__)

node_identifier = str(uuid.uuid4()).replace('-', '')

block_chain = BlockChain()


@app.route('/mine/')
@typechecked
def mine() -> Response:
    last_block = block_chain.last_block
    last_proof = last_block.proof
    proof = block_chain.proof_of_work(last_proof)

    block_chain.new_transaction(
        sender=MINING_REWARD_SENDER,
        recipient=node_identifier,
        amount=MINING_REWARD,
    )

    previous_hash = last_block.hash
    block = block_chain.new_block(proof, previous_hash)