示例#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 fx_transaction() -> Transaction:
    transaction = Transaction(
        sender=bytes.fromhex('5ca60de0575441718094ea0ffcb02aa4'),
        recipient=bytes.fromhex('33ee49f83681417e82660cb9585d13b1'),
        amount=decimal.Decimal('0.5'),
    )
    return transaction
示例#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() -> Block:
    block_reward = Transaction(
        sender=bytes.fromhex('00000000000000000000000000000000'),
        recipient=bytes.fromhex('5ca60de0575441718094ea0ffcb02aa4'),
        amount=decimal.Decimal('1'),
    )
    block = Block(
        index=1,
        timestamp=737511503930,  # 1993-05-16T09:18:23.930516
        proof=1,
        previous_hash=bytes.fromhex(
            '0000000000000000000000000000000000000000000000000000000000000000'
        ),  # noqa
        transactions=[block_reward],
    )
    return block
示例#7
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
示例#8
0
def test_transaction_serialize(fx_transaction: Transaction):
    assert fx_transaction.serialize() == {
        'sender': '5ca60de0575441718094ea0ffcb02aa4',
        'recipient': '33ee49f83681417e82660cb9585d13b1',
        'amount': '0.5',
    }