def test_unvalid_verify_tx():
    sender = "a"
    receivers = ["fe"]
    amounts = [1]
    fee = 100
    t = Transaction(sender, receivers, amounts, fee)
    assert False == t.verify()
Esempio n. 2
0
def test_pool_multi_add_same_tx():
    p = TransactionPool()
    assert p.get_size() == 0
    assert p.get_tx_list() == []

    sender = "iWVjc8hWuRuePAv1X8nDZdcjKcqivDUH62YKhBXBHqp2yGfgeXyHJDj5XwCHwjWB6GevCjMYT59XSBiQvMYHQ4P"
    receivers = ["01", "02", "0a"]
    amounts = [1, 2, 3]
    fee = 100
    tx = Transaction(sender, receivers, amounts, fee)

    p.add(tx)

    assert p.get_size() == 1
    assert p.get_tx_list()[0].string() == tx.string()

    p.add(tx)

    assert p.get_size() == 1
    assert p.get_tx_list()[0].string() == tx.string()

    p.add(tx)

    assert p.get_size() == 1
    assert p.get_tx_list()[0].string() == tx.string()

    p.remove(tx)

    assert p.get_size() == 0
    assert p.get_tx_list() == []
Esempio n. 3
0
 def json_string_tx():
     """creates a main block for testing purposes"""
     sender = "iWVjc8hWuRuePAv1X8nDZdcjKcqivDUH62YKhBXBHqp2yGfgeXyHJDj5XwCHwjWB6GevCjMYT59XSBiQvMYHQ4P"
     receivers = ["01", "02", "0a"]
     amounts = [1, 2, 3]
     fee = 100
     tx = Transaction(sender, receivers, amounts, fee)
     return tx.string()
Esempio n. 4
0
def test_sign_transaction():
    # test if transactions can be signed
    w = wallet.generate_new_wallet()
    t = Transaction(sender=w.get_address(),
                    receivers=["01"],
                    amounts=[1],
                    nonce=1,
                    fee=100)
    w.sign_transaction(t)
    assert t.verify()
Esempio n. 5
0
    def json_string_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()
        return tx_string
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 test_verify_tx():
    """
    tests if a signed transaction can be validated
    we just need the address for this, because address=public_key
    """
    w = wallet.generate_new_wallet()
    sender = w.get_address()
    receivers = ["fe"]
    amounts = [1]
    fee = 100
    t = Transaction(sender, receivers, amounts, fee)
    w.sign_transaction(t)
    assert cryptoutil.verify_transaction_sig(t, t.get_signature())
    assert t.verify()
Esempio n. 9
0
def test_pool():
    """test that adding the same tx twice is not possible"""
    p = TransactionPool()
    assert p.get_size() == 0
    assert p.get_tx_list() == []

    sender = "iWVjc8hWuRuePAv1X8nDZdcjKcqivDUH62YKhBXBHqp2yGfgeXyHJDj5XwCHwjWB6GevCjMYT59XSBiQvMYHQ4P"
    receivers = ["01", "02", "0a"]
    amounts = [1, 2, 3]
    fee = 100
    tx1 = Transaction(sender, receivers, amounts, fee)

    p.add(tx1)
    assert p.get_size() == 1
    assert p.get_tx_list()[0].string() == tx1.string()

    tx2 = Transaction(sender, receivers, amounts, fee)

    p.add(tx2)

    assert p.get_size() == 1
    assert p.get_tx_list()[0].string() == tx1.string()
    assert p.get_tx_list()[0].string() == tx2.string()

    p.remove(tx1)

    assert p.get_size() == 0
    assert p.get_tx_list() == []
Esempio n. 10
0
def test_sign_unvalid_tx():
    # test if signing unvalid transaction is rejected
    w = wallet.generate_new_wallet()
    t = Transaction(sender="different sender than wallet",
                    receivers=["01"],
                    amounts=[1],
                    nonce=1,
                    fee=100)
    with pytest.raises(ValueError):
        w.sign_transaction(t)
Esempio n. 11
0
def test_nonce_correctly():
    # test if the nonce increases when signing a tx
    # test if loaded wallet (w_copy) has correct nonce
    w = wallet.generate_new_wallet()
    t = Transaction(sender=w.get_address(),
                    receivers=["01"],
                    amounts=[1],
                    nonce=1,
                    fee=100)
    assert w.get_nonce() == 0
    w.sign_transaction(t)
    assert w.get_nonce() == 1
Esempio n. 12
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
Esempio n. 13
0
def test_pool_two_different_tx():
    p = TransactionPool()

    sender = "iWVjc8hWuRuePAv1X8nDZdcjKcqivDUH62YKhBXBHqp2yGfgeXyHJDj5XwCHwjWB6GevCjMYT59XSBiQvMYHQ4P"
    receivers = ["01", "02", "0a"]
    amounts = [1, 2, 3]
    fee = 100
    tx1 = Transaction(sender, receivers, amounts, fee)
    tx1.nonce = 1
    tx2 = Transaction(sender, receivers, amounts, fee)
    tx2.nonce = 2

    p.add(tx1)
    p.add(tx2)

    assert p.get_size() == 2

    p.remove(tx2)

    assert p.get_size() == 1

    p.remove(tx2)

    assert p.get_size() == 1

    p.remove(tx1)

    assert p.get_size() == 0
def test_sign_transaction():
    """test that signing and verifying transaction works"""
    private_key = cryptoutil.generate_key()
    sender = cryptoutil.key_to_address(private_key.get_verifying_key())
    receivers = ["fe"]
    amounts = [1]
    fee = 100
    tx = Transaction(sender, receivers, amounts, fee)

    # get the signature
    tx_sig = cryptoutil.get_transaction_sig(private_key, tx)
    # validate transaction
    assert cryptoutil.verify_transaction_sig(tx, tx_sig)
def test_trie():
    txs = []
    for i in range(10):
        sender = "a" + str(i)
        receivers = [str(i)]
        amounts = [str(i + 100)]
        fee = 100 * i
        txs.append(Transaction(sender, receivers, amounts, fee))
    txs = [tx.hash() for tx in txs]

    trie = Trie(txs)

    assert trie.size() == 10
    trie_root = trie.root()
    assert trie_root == "f12419db9e522427fa0fddff624b8d024becb4749fc75018dec2512048a21492"
def test_trie():
    txs = []
    for i in range(10):
        sender = "a" + str(i)
        receivers = [str(i)]
        amounts = [str(i + 100)]
        nonce = i
        fee = 100 * i
        txs.append(Transaction(sender, receivers, amounts, nonce, fee))
    txs = [tx.hash() for tx in txs]

    trie = Trie(txs)

    assert trie.size() == 10
    trie_root = trie.root()
    assert trie_root == "819208b7314e74647711cb43a22e518fea7d67541bd691ef7914a368883b43c6"
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_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 remove_transaction(self, transaction: Transaction):
     self.transactions.pop(transaction.hash(), None)
 def handle_new_transaction(self, tx: Transaction):
     self.tx_pool.add_transaction(tx)
     loggerutil.debug("got new tx: " + tx.str())
 def get_tx_list(_range):
     return [Transaction("a" + str(i), ["b" + str(i)], [i], 10 * i) for i in range(_range)]