Exemple #1
0
def test_nonce_in_tx_correct():
    # test if the nonce is correct in the tx
    w = wallet.generate_new_wallet()

    t = Transaction(sender=w.get_address(),
                    receivers=["01"],
                    amounts=[1],
                    nonce=w.get_nonce(),
                    fee=100)
    assert t.nonce == w.get_nonce()

    tx_hash_old = ""
    tx_sig_old = ""
    for i in range(10):
        # sign tx
        w.sign_transaction(t)
        # get sig
        signature = t.get_signature()
        # nonce gets bigger every signed tx
        assert t.nonce == w.get_nonce() - 1
        assert t.nonce == i
        # make sure tx_signature changes
        assert signature != tx_sig_old
        # make sure tx_hash changes
        assert t.hash() != tx_hash_old
        # finally test if verify works
        assert t.verify()
        # save values for next iteration
        tx_hash_old = t.hash()
        tx_sig_old = signature
    # assert that we counted correctly
    assert w.get_nonce() == 10
def test_unsigned_hash():
    """test the unisgned_hash of tx"""
    sender = "abcd"
    receivers = ["fe"]
    amounts = [1]
    nonce = 1
    fee = 100
    tx = Transaction(sender, receivers, amounts, nonce, fee)
    # get hashes
    tx_signed_hash = tx.hash()
    tx_unsigned_hash = tx.unsigned_hash()
    # set signature
    tx.signature = "abcd"
    # assert signed hash has changed
    assert tx.hash() != tx_signed_hash
    # assert unsigned hash has not changed
    assert tx.unsigned_hash() == tx_unsigned_hash
def test_tx_from_json():
    """test serialization and deserialization"""
    sender = "abcd"
    receivers = ["fe"]
    amounts = [1]
    fee = 100
    t1 = Transaction(sender, receivers, amounts, fee)
    t2 = transaction.from_json(t1.string())
    assert t1.string() == t2.string()
    assert t1.hash() == t2.hash()
    assert t1.unsigned_hash() == t2.unsigned_hash()
def test_transaction():
    """base test for transaction"""
    sender = "iWVjc8hWuRuePAv1X8nDZdcjKcqivDUH62YKhBXBHqp2yGfgeXyHJDj5XwCHwjWB6GevCjMYT59XSBiQvMYHQ4P"
    receivers = ["01", "02", "0a"]
    amounts = [1, 2, 3]
    fee = 100
    tx = Transaction(sender, receivers, amounts, fee)

    assert tx.string() == \
        '{"sender": "iWVjc8hWuRuePAv1X8nDZdcjKcqivDUH62YKhBXBHqp2yGfgeXyHJDj5XwCHwjWB6GevCjMYT59XSBiQvMYHQ4P", "receivers": ["01", "02", "0a"], "amounts": [1, 2, 3], "nonce": 0, "fee": 100, "signature": ""}'
    assert tx.hash(
    ) == "a564b3a98ed7d3f66b69ee4d9f7fab588a875ae06c6f7919c7f83121bb72f859"
def test_transaction():
    """base test for transaction"""
    sender = "iWVjc8hWuRuePAv1X8nDZdcjKcqivDUH62YKhBXBHqp2yGfgeXyHJDj5XwCHwjWB6GevCjMYT59XSBiQvMYHQ4P"
    receivers = ["01", "02", "0a"]
    amounts = [1, 2, 3]
    nonce = 1
    fee = 100
    tx = Transaction(sender, receivers, amounts, nonce, fee)

    tx_string = tx.string()
    assert tx_string == '{"sender_address": "iWVjc8hWuRuePAv1X8nDZdcjKcqivDUH62YKhBXBHqp2yGfgeXyHJDj5XwCHwjWB6GevCjMYT59XSBiQvMYHQ4P", "receivers": ["01", "02", "0a"], "amounts": [1, 2, 3], "nonce": 1, "fee": 100, "signature": ""}'
    tx_hash = tx.hash()
    assert tx_hash == "e9f65b7385ffb2e9c8809da75ff86bc10b8490dd02e3bfa26daf0c189a535b5b"
 def remove_transaction(self, transaction: Transaction):
     self.transactions.pop(transaction.hash(), None)