Exemple #1
0
def test_staking_transaction():
    transaction = Transaction(
        chain=1,
        nonce=4_294_967_295,
        fee=57000,
        value=5_000_000,
        to_address=Config.STAKING_ADDRESS,
        unlock_sig=Config.COINBASE_UNLOCK_SIGNATURE,
    )

    assert transaction.validate() is False
    with pytest.raises(TransactionNotValid,
                       match=errors.TRANSACTION_INVALID_STAKING):
        transaction.validate_fields(raise_exception=True)

    transaction = Transaction(
        chain=1,
        nonce=4_294_967_295,
        fee=57000,
        value=5_000_000,
        to_address=Config.STAKING_ADDRESS,
    )

    transaction = transaction.sign(unhexlify(WALLET_1["private_key"]))

    assert transaction.validate() is True
Exemple #2
0
def test_validate_signatures(mocker):
    """
    Test to check that the signatures are valid.
    """
    transaction_original = Transaction(
        chain=1,
        nonce=4_294_967_295,
        fee=57000,
        value=5_000_000,
        to_address="1H7NtUENrEbwSVm52fHePzBnu4W3bCqimP",
    )

    transaction = transaction_original.sign(PRIVATE_KEY_1)
    assert transaction.validate() == True

    # Incorrect Public Key / Signature
    with mocker.patch(
            "luracoin.transactions.deserialize_unlocking_script",
            return_value=
        {
            "signature":
            "6cfa3bd7427ec06e8c0e93911db0932fa18f763f617f4115929501affd0f95aacc71c66d0c7c535f3843d537cb5c424bb0ee59e5baf6df8966c6b58faa2abfc2",
            "public_key":
            "533b17c5cf044c799a67ad974250aeb72cb529ae59f4b8804adc6eb602dd5ee35c6f8a1910dd7924f89a624456f3c0f4e2c6d4117ee0fa7419af6c7ca120d596",
            "address": "1C5fcCCJydpyQ7KqHBuGHJbwrwURHd62Lj",
        },
    ):
        assert transaction.validate() == False

    # Invalid Public Key
    with mocker.patch(
            "luracoin.transactions.deserialize_unlocking_script",
            return_value=
        {
            "signature":
            "6cfa3bd7427ec06e8c0e93911db0932fa18f763f617f4115929501affd0f95aacc71c66d0c7c535f3843d537cb5c424bb0ee59e5baf6df8966c6b58faa2abfc2",
            "public_key":
            "0000000000000455d0fd9a38f1befcf1c7116feedd7407f42fcf4ad321e4710014740c3c370109a585debfb082d0889b99fa74708c3f41f0b3d39498cb65b3ee",
            "address": "1C5fcCCJydpyQ7KqHBuGHJbwrwURHd62Lj",
        },
    ):
        assert transaction.validate() == False

    transaction.to_transaction_pool()
Exemple #3
0
def test_modify_transaction_after_signing(mocker):
    """
    Test to check that you can't modify a transaction after signing it.
    """
    transaction_original = Transaction(
        chain=0,
        nonce=4_294_967_295,
        fee=57000,
        value=5_000_000,
        to_address="1H7NtUENrEbwSVm52fHePzBnu4W3bCqimP",
    )

    transaction = transaction_original.sign(PRIVATE_KEY_1)
    transaction.value = 10_000_000

    assert transaction.validate() == False
    with pytest.raises(TransactionNotValid,
                       match=errors.TRANSACTION_INVALID_SIGNATURE):
        transaction.validate(raise_exception=True)
Exemple #4
0
def add_test_transactions():
    transaction1 = Transaction(
        chain=1,
        nonce=1,
        fee=57000,
        value=5_000_000,
        to_address=WALLET_2["address"],
    )

    transaction1 = transaction1.sign(unhexlify(WALLET_1["private_key"]))
    assert transaction1.validate() == True
    transaction1.to_transaction_pool()

    transaction2 = Transaction(
        chain=1,
        nonce=1,
        fee=157_000,
        value=15_000_000,
        to_address=WALLET_3["address"],
    )

    transaction2 = transaction2.sign(unhexlify(WALLET_2["private_key"]))
    assert transaction2.validate() == True
    transaction2.to_transaction_pool()

    transaction3 = Transaction(
        chain=1,
        nonce=2,
        fee=5700,
        value=500_000,
        to_address=WALLET_1["address"],
    )

    transaction3 = transaction3.sign(unhexlify(WALLET_2["private_key"]))
    assert transaction3.validate() == True
    transaction3.to_transaction_pool()

    transaction4 = Transaction(chain=1,
                               nonce=2,
                               fee=50,
                               value=100,
                               to_address=WALLET_1["address"])

    transaction4 = transaction4.sign(unhexlify(WALLET_2["private_key"]))
    assert transaction4.validate() == True
    transaction4.to_transaction_pool()

    transaction5 = Transaction(chain=1,
                               nonce=2,
                               fee=150,
                               value=600,
                               to_address=WALLET_2["address"])

    transaction5 = transaction5.sign(unhexlify(WALLET_3["private_key"]))
    assert transaction5.validate() == True
    transaction5.to_transaction_pool()

    return [
        transaction1,
        transaction2,
        transaction3,
        transaction4,
        transaction5,
    ]
Exemple #5
0
def test_full_blockchain():
    START_TIMESTAMP = 1639159886
    chain = Chain()
    assert chain.tip == 0

    block1 = Block(
        version=1,
        height=0,
        prev_block_hash="0" * 64,
        miner=WALLET_1["address"],
        timestamp=START_TIMESTAMP,
        bits=Config.STARTING_DIFFICULTY,
        nonce=156369,
        txns=[],
    )
    print("\nBlock 1 \n" + json.dumps(block1.json(), indent=4))
    block1.save()
    # Check chain Height is updated
    assert chain.tip == 0
    # Check block file number is updated
    assert chain.get_block_file_number(0) == 0
    # Check that the block file contains one block
    assert len(Block.get_blocks_from_file(0)) == 1
    # Check that the block retrieved from the files is the same
    assert Block.get(0).id == block1.id
    assert Block.get(0).serialize() == block1.serialize()
    
    # TODO: Check miner balance
    # TODO: Check file content
    # TODO: Check stacking

    # Transaction to stack 15 Luracoins
    transaction_1 = Transaction(
        chain=1,
        nonce=1,
        fee=0,
        value=Config.LURASHIS_PER_COIN * 15,
        to_address=Config.STAKING_ADDRESS,
    )

    transaction_1.sign(unhexlify(WALLET_1["private_key"]))
    assert transaction_1.validate() == True

    block2 = Block(
        version=1,
        height=1,
        prev_block_hash=block1.id,
        miner=WALLET_1["address"],
        timestamp=START_TIMESTAMP + (3*60),  # 3 minutes
        bits=Config.STARTING_DIFFICULTY,
        nonce=4358788,
        txns=[transaction_1],
    )
    print("\nBlock 2 \n" + json.dumps(block2.json(), indent=4))

    block2.save()
    # Check chain Height is updated
    assert chain.tip == 1
    # Check block file number is updated
    assert chain.get_block_file_number(0) == 0
    # Check that the block file contains two blocks
    assert len(Block.get_blocks_from_file(0)) == 2
    # Check that the block retrieved from the files is the same
    assert Block.get(1).id == block2.id
    assert Block.get(1).serialize() == block2.serialize()
    # TODO: Check miner balance
    # TODO: Check file content
    # TODO: Check stacking

    assert False