From 79eb7d5070c06d84a7fb3995d6e93dcf7775ef3b Mon Sep 17 00:00:00 2001 From: Ouziel Slama Date: Fri, 24 Jan 2025 08:36:34 +0000 Subject: [PATCH] migrate move.py tests --- .../counterpartycore/pytest/mocks/bitcoind.py | 3 + .../pytest/units/messages/detach_test.py | 20 ++- .../pytest/units/messages/move_test.py | 127 ++++++++++++++++++ 3 files changed, 139 insertions(+), 11 deletions(-) create mode 100644 counterparty-core/counterpartycore/pytest/units/messages/move_test.py diff --git a/counterparty-core/counterpartycore/pytest/mocks/bitcoind.py b/counterparty-core/counterpartycore/pytest/mocks/bitcoind.py index 9214e0d4da..37288db6ae 100644 --- a/counterparty-core/counterpartycore/pytest/mocks/bitcoind.py +++ b/counterparty-core/counterpartycore/pytest/mocks/bitcoind.py @@ -47,6 +47,9 @@ def get_vin_info(self, vin): return value, script_pub_key, is_segwit def get_utxo_address_and_value(self, utxo): + print("get_utxo_address_and_value", utxo) + if utxo == "0000000000000000000000000000000000000000000000000000000000000000:0": + return list(self.source_by_txid.values())[0], int(10 * config.UNIT) txid, vout = utxo.split(":") return self.address_and_value_by_utxo[f"{txid}:0"] diff --git a/counterparty-core/counterpartycore/pytest/units/messages/detach_test.py b/counterparty-core/counterpartycore/pytest/units/messages/detach_test.py index df3c753550..04f1ea7e82 100644 --- a/counterparty-core/counterpartycore/pytest/units/messages/detach_test.py +++ b/counterparty-core/counterpartycore/pytest/units/messages/detach_test.py @@ -35,12 +35,16 @@ def test_unpack(defaults): } -def test_parse_detach_to_destination(ledger_db, blockchain_mock, defaults, test_helpers): - utxo = ledger_db.execute( +def get_utxo(ledger_db, address): + return ledger_db.execute( "SELECT * FROM balances WHERE utxo_address = ? AND quantity > 0", - (defaults["addresses"][0],), + (address,), ).fetchone()["utxo"] + +def test_parse_detach_to_destination(ledger_db, blockchain_mock, defaults, test_helpers): + utxo = get_utxo(ledger_db, defaults["addresses"][0]) + tx = blockchain_mock.dummy_tx(ledger_db, defaults["addresses"][0], utxo_source=utxo) message = bytes(defaults["addresses"][1], "utf-8") detach.parse(ledger_db, tx, message) @@ -69,10 +73,7 @@ def test_parse_detach_to_destination(ledger_db, blockchain_mock, defaults, test_ def test_parse_detach_no_destination(ledger_db, blockchain_mock, defaults, test_helpers): - utxo = ledger_db.execute( - "SELECT * FROM balances WHERE utxo_address = ? AND quantity > 0", - (defaults["addresses"][0],), - ).fetchone()["utxo"] + utxo = get_utxo(ledger_db, defaults["addresses"][0]) tx = blockchain_mock.dummy_tx(ledger_db, defaults["addresses"][0], utxo_source=utxo) message = b"0" @@ -102,10 +103,7 @@ def test_parse_detach_no_destination(ledger_db, blockchain_mock, defaults, test_ def test_parse_detach_invalid_destination(ledger_db, blockchain_mock, defaults, test_helpers): - utxo = ledger_db.execute( - "SELECT * FROM balances WHERE utxo_address = ? AND quantity > 0", - (defaults["addresses"][0],), - ).fetchone()["utxo"] + utxo = get_utxo(ledger_db, defaults["addresses"][0]) tx = blockchain_mock.dummy_tx(ledger_db, defaults["addresses"][0], utxo_source=utxo) message = bytes("invalidadress", "utf-8") diff --git a/counterparty-core/counterpartycore/pytest/units/messages/move_test.py b/counterparty-core/counterpartycore/pytest/units/messages/move_test.py new file mode 100644 index 0000000000..070f387d3d --- /dev/null +++ b/counterparty-core/counterpartycore/pytest/units/messages/move_test.py @@ -0,0 +1,127 @@ +from counterpartycore.lib.messages import move + +DUMMY_UTXO = 64 * "0" + ":0" + + +def get_utxo(ledger_db, address, asset="XCP"): + return ledger_db.execute( + "SELECT * FROM balances WHERE utxo_address = ? and asset = ? AND quantity > 0", + ( + address, + asset, + ), + ).fetchone()["utxo"] + + +def test_move_assets_xcp(ledger_db, defaults, blockchain_mock, test_helpers, current_block_index): + utxo = get_utxo(ledger_db, defaults["addresses"][0]) + tx = blockchain_mock.dummy_tx( + ledger_db, defaults["addresses"][0], utxo_source=utxo, utxo_destination=DUMMY_UTXO + ) + move.move_assets(ledger_db, tx) + + test_helpers.check_records( + ledger_db, + [ + { + "table": "debits", + "values": { + "utxo": utxo, + "address": None, + "asset": "XCP", + "quantity": 100, + "event": tx["tx_hash"], + "block_index": current_block_index, + "tx_index": tx["tx_index"], + "action": "utxo move", + }, + }, + { + "table": "credits", + "values": { + "utxo": DUMMY_UTXO, + "address": None, + "asset": "XCP", + "quantity": 100, + "event": tx["tx_hash"], + "block_index": current_block_index, + "tx_index": tx["tx_index"], + "calling_function": "utxo move", + }, + }, + { + "table": "sends", + "values": { + "tx_index": tx["tx_index"], + "tx_hash": tx["tx_hash"], + "block_index": tx["block_index"], + "status": "valid", + "source": utxo, + "source_address": defaults["addresses"][0], + "destination": DUMMY_UTXO, + "destination_address": defaults["addresses"][0], + "asset": "XCP", + "quantity": 100, + "fee_paid": 0, + }, + }, + ], + ) + + +def test_move_assets_divisible( + ledger_db, defaults, blockchain_mock, test_helpers, current_block_index +): + utxo = get_utxo(ledger_db, defaults["addresses"][0], "DIVISIBLE") + tx = blockchain_mock.dummy_tx( + ledger_db, defaults["addresses"][0], utxo_source=utxo, utxo_destination=DUMMY_UTXO + ) + move.move_assets(ledger_db, tx) + + test_helpers.check_records( + ledger_db, + [ + { + "table": "debits", + "values": { + "utxo": utxo, + "address": None, + "asset": "DIVISIBLE", + "quantity": 1, + "event": tx["tx_hash"], + "block_index": current_block_index, + "tx_index": tx["tx_index"], + "action": "utxo move", + }, + }, + { + "table": "credits", + "values": { + "utxo": DUMMY_UTXO, + "address": None, + "asset": "DIVISIBLE", + "quantity": 1, + "event": tx["tx_hash"], + "block_index": current_block_index, + "tx_index": tx["tx_index"], + "calling_function": "utxo move", + }, + }, + { + "table": "sends", + "values": { + "tx_index": tx["tx_index"], + "tx_hash": tx["tx_hash"], + "block_index": tx["block_index"], + "status": "valid", + "source": utxo, + "source_address": defaults["addresses"][0], + "destination": DUMMY_UTXO, + "destination_address": defaults["addresses"][0], + "asset": "DIVISIBLE", + "quantity": 1, + "fee_paid": 0, + }, + }, + ], + )