From 17439b550fa6dc300ebd17b193f78c6b2fab5cec Mon Sep 17 00:00:00 2001 From: Ouziel Slama Date: Mon, 13 Jan 2025 13:36:58 +0000 Subject: [PATCH] Move python tests from counterparty-rs to counterparty-core --- .../{pycoin_rs_test.py => rust_lib_test.py} | 105 +++++++++++++++++- counterparty-rs/tests/test_b58.py | 12 -- counterparty-rs/tests/test_utils.py | 95 ---------------- 3 files changed, 104 insertions(+), 108 deletions(-) rename counterparty-core/counterpartycore/test/{pycoin_rs_test.py => rust_lib_test.py} (65%) delete mode 100644 counterparty-rs/tests/test_b58.py delete mode 100644 counterparty-rs/tests/test_utils.py diff --git a/counterparty-core/counterpartycore/test/pycoin_rs_test.py b/counterparty-core/counterpartycore/test/rust_lib_test.py similarity index 65% rename from counterparty-core/counterpartycore/test/pycoin_rs_test.py rename to counterparty-core/counterpartycore/test/rust_lib_test.py index ee32f5b802..2e2ef3286f 100644 --- a/counterparty-core/counterpartycore/test/pycoin_rs_test.py +++ b/counterparty-core/counterpartycore/test/rust_lib_test.py @@ -3,8 +3,9 @@ import bitcoin as bitcoinlib import pytest # noqa: F401 +from bitcoin import bech32 as bech32lib from bitcoin.core.script import CScript -from counterparty_rs import utils +from counterparty_rs import b58, utils from counterpartycore.lib import config from counterpartycore.lib.opcodes import * # noqa: F403 @@ -186,3 +187,105 @@ def decode_p2w(script_pubkey): print(f"{iterations} decode_p2w with rust: {rust_duration}s") assert python_duration > rust_duration + + +def test_b58(): + assert b58.b58_encode(b"hello world") == "3vQB7B6MrGQZaxCuFg4oh" + assert bytes(b58.b58_decode("3vQB7B6MrGQZaxCuFg4oh")) == b"hello world" + + with pytest.raises(ValueError) as excinfo: + b58.b58_decode("hello world") + assert str(excinfo.value) == "Bad input" + + +def decode_p2w(script_pubkey): + try: + bech32 = bech32lib.CBech32Data.from_bytes(0, script_pubkey[2:22]) + return str(bech32), None + except TypeError as e: # noqa: F841 + raise Exception("bech32 decoding error") # noqa: B904 + + +def inverse_hash_py(hashstring): + return "".join([hashstring[i : i + 2][::-1] for i in range(0, len(hashstring), 2)])[::-1] + + +def test_inverse_hash(): + h = "b5276739a3e0f32147bd4a921f936c6013dee4a5ca426ee2de868810b068ec0d" + assert ( + utils.inverse_hash(h) == "0dec68b0108886dee26e42caa5e4de13606c931f924abd4721f3e0a3396727b5" + ) + + # iterations = 1000000 + iterations = 10 + + start_time = time.time() + for i in range(iterations): # noqa: B007 + inverse_hash_py(h) + python_duration = time.time() - start_time + print(f"{iterations} inverse hash with python: {python_duration}s") + + start_time = time.time() + for i in range(iterations): # noqa: B007 + utils.inverse_hash(h) + rust_duration = time.time() - start_time + print(f"{iterations} inverse hash with rust: {rust_duration}s") + + assert python_duration > rust_duration + + +def test_script_to_asm(): + asm = utils.script_to_asm(b"v\xa9\x14H8\xd8\xb3X\x8cL{\xa7\xc1\xd0o\x86n\x9b79\xc607\x88\xac") + assert asm == [ + b"v", + b"\xa9", + b"H8\xd8\xb3X\x8cL{\xa7\xc1\xd0o\x86n\x9b79\xc607", + b"\x88", + b"\xac", + ] + """ [ + 'OP_DUP', + 'OP_HASH160', + b'H8\xd8\xb3X\x8cL{\xa7\xc1\xd0o\x86n\x9b79\xc607', + 'OP_EQUALVERIFY', + 'OP_CHECKSIG' + ] """ + + asm = utils.script_to_asm( + b"Q!\x03\\\xa5\x1e\xa1u\xf1\x08\xa1\xc65\x88h=\xc4\xc4:qF\xc4g\x99\xf8d\xa3\x00&<\x08\x13\xf5\xfe5!\x020\x9a\x14\xa1\xa3\x02\x02\xf2\xe7oF\xac\xdb)\x17u#q\xcaB\xb9t`\xf7\x92\x8a\xde\x8e\xcb\x02\xea\x17!\x03\x19\xf6\xe0{\x0b\x8duaV9K\x9d\xcf;\x01\x1f\xe9\xac\x19\xf2p\x0b\xd6\xb6\x9aj\x17\x83\xdb\xb8\xb9wS\xae" + ) + assert asm == [ + b"\x01", + b"\x03\\\xa5\x1e\xa1u\xf1\x08\xa1\xc65\x88h=\xc4\xc4:qF\xc4g\x99\xf8d\xa3\x00&<\x08\x13\xf5\xfe5", + b"\x020\x9a\x14\xa1\xa3\x02\x02\xf2\xe7oF\xac\xdb)\x17u#q\xcaB\xb9t`\xf7\x92\x8a\xde\x8e\xcb\x02\xea\x17", + b"\x03\x19\xf6\xe0{\x0b\x8duaV9K\x9d\xcf;\x01\x1f\xe9\xac\x19\xf2p\x0b\xd6\xb6\x9aj\x17\x83\xdb\xb8\xb9w", + b"\x03", + b"\xae", # OP_CHECKMULTISIG + ] + + +def test_decode_p2w(): + script_pubkey = b"\x00\x14u\x1ev\xe8\x19\x91\x96\xd4T\x94\x1cE\xd1\xb3\xa3#\xf1C;\xd6" + bech32 = utils.script_to_address(script_pubkey, "testnet") + assert bech32 == "tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx" + + bech32 = utils.script_to_address(script_pubkey, "mainnet") + assert bech32 == "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4" + + bitcoinlib.SelectParams("mainnet") + + assert decode_p2w(script_pubkey)[0] == utils.script_to_address(script_pubkey, "mainnet") + + script_pubkey = binascii.unhexlify( + "0020dcbc2340bd1f6cc3ab0a3887020647ec471a279e3c889fb4414df30e3dd59f96" + ) + assert decode_p2w(script_pubkey) == ("bc1qmj7zxs9arakv82c28zrsypj8a3r35fu7pure55", None) + assert decode_p2w(script_pubkey)[0] == utils.script_to_address(script_pubkey, "mainnet") + + script_pubkey = binascii.unhexlify( + "0020dfe1739dc0711f64ced999a2306691ff98fff038b2f40aec2b5ae917610ea0ac" + ) + assert decode_p2w(script_pubkey) == ("bc1qmlsh88wqwy0kfnkenx3rqe53l7v0lupc6q5xx6", None) + assert decode_p2w(script_pubkey)[0] == utils.script_to_address(script_pubkey, "mainnet") + + bitcoinlib.SelectParams("testnet") diff --git a/counterparty-rs/tests/test_b58.py b/counterparty-rs/tests/test_b58.py deleted file mode 100644 index 59589c958e..0000000000 --- a/counterparty-rs/tests/test_b58.py +++ /dev/null @@ -1,12 +0,0 @@ -import pytest -from counterparty_rs import b58 # noqa: F401 - - -# TODO -def test_b58(): - assert b58.b58_encode(b"hello world") == "3vQB7B6MrGQZaxCuFg4oh" - assert b58.b58_decode("3vQB7B6MrGQZaxCuFg4oh") == b"hello world" - - with pytest.raises(ValueError) as excinfo: - b58.b58_decode("hello world") - assert str(excinfo.value) == "Bad input" diff --git a/counterparty-rs/tests/test_utils.py b/counterparty-rs/tests/test_utils.py deleted file mode 100644 index 16daadb6af..0000000000 --- a/counterparty-rs/tests/test_utils.py +++ /dev/null @@ -1,95 +0,0 @@ -import binascii -import time - -import bitcoin as bitcoinlib # noqa: F401 -from bitcoin import bech32 as bech32lib -from counterparty_rs import utils - - -def decode_p2w(script_pubkey): - try: - bech32 = bech32lib.CBech32Data.from_bytes(0, script_pubkey[2:22]) - return str(bech32), None - except TypeError as e: # noqa: F841 - raise Exception("bech32 decoding error") # noqa: B904 - - -def inverse_hash_py(hashstring): - return "".join([hashstring[i : i + 2][::-1] for i in range(0, len(hashstring), 2)])[::-1] - - -def test_inverse_hash(): - h = "b5276739a3e0f32147bd4a921f936c6013dee4a5ca426ee2de868810b068ec0d" - assert ( - utils.inverse_hash(h) == "0dec68b0108886dee26e42caa5e4de13606c931f924abd4721f3e0a3396727b5" - ) - - # iterations = 1000000 - iterations = 10 - - start_time = time.time() - for i in range(iterations): # noqa: B007 - inverse_hash_py(h) - python_duration = time.time() - start_time - print(f"{iterations} inverse hash with python: {python_duration}s") - - start_time = time.time() - for i in range(iterations): # noqa: B007 - utils.inverse_hash(h) - rust_duration = time.time() - start_time - print(f"{iterations} inverse hash with rust: {rust_duration}s") - - assert python_duration > rust_duration - - -def test_script_to_asm(): - asm = utils.script_to_asm(b"v\xa9\x14H8\xd8\xb3X\x8cL{\xa7\xc1\xd0o\x86n\x9b79\xc607\x88\xac") - assert asm == [ - b"v", - b"\xa9", - b"H8\xd8\xb3X\x8cL{\xa7\xc1\xd0o\x86n\x9b79\xc607", - b"\x88", - b"\xac", - ] - """ [ - 'OP_DUP', - 'OP_HASH160', - b'H8\xd8\xb3X\x8cL{\xa7\xc1\xd0o\x86n\x9b79\xc607', - 'OP_EQUALVERIFY', - 'OP_CHECKSIG' - ] """ - - asm = utils.script_to_asm( - b"Q!\x03\\\xa5\x1e\xa1u\xf1\x08\xa1\xc65\x88h=\xc4\xc4:qF\xc4g\x99\xf8d\xa3\x00&<\x08\x13\xf5\xfe5!\x020\x9a\x14\xa1\xa3\x02\x02\xf2\xe7oF\xac\xdb)\x17u#q\xcaB\xb9t`\xf7\x92\x8a\xde\x8e\xcb\x02\xea\x17!\x03\x19\xf6\xe0{\x0b\x8duaV9K\x9d\xcf;\x01\x1f\xe9\xac\x19\xf2p\x0b\xd6\xb6\x9aj\x17\x83\xdb\xb8\xb9wS\xae" - ) - assert asm == [ - b"\x01", - b"\x03\\\xa5\x1e\xa1u\xf1\x08\xa1\xc65\x88h=\xc4\xc4:qF\xc4g\x99\xf8d\xa3\x00&<\x08\x13\xf5\xfe5", - b"\x020\x9a\x14\xa1\xa3\x02\x02\xf2\xe7oF\xac\xdb)\x17u#q\xcaB\xb9t`\xf7\x92\x8a\xde\x8e\xcb\x02\xea\x17", - b"\x03\x19\xf6\xe0{\x0b\x8duaV9K\x9d\xcf;\x01\x1f\xe9\xac\x19\xf2p\x0b\xd6\xb6\x9aj\x17\x83\xdb\xb8\xb9w", - b"\x03", - b"\xae", # OP_CHECKMULTISIG - ] - - -def test_decode_p2w(): - script_pubkey = b"\x00\x14u\x1ev\xe8\x19\x91\x96\xd4T\x94\x1cE\xd1\xb3\xa3#\xf1C;\xd6" - bech32 = utils.script_to_address(script_pubkey, "testnet") - assert bech32 == "tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx" - - bech32 = utils.script_to_address(script_pubkey, "mainnet") - assert bech32 == "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4" - - assert decode_p2w(script_pubkey)[0] == utils.script_to_address(script_pubkey, "mainnet") - - script_pubkey = binascii.unhexlify( - "0020dcbc2340bd1f6cc3ab0a3887020647ec471a279e3c889fb4414df30e3dd59f96" - ) - assert decode_p2w(script_pubkey) == ("bc1qmj7zxs9arakv82c28zrsypj8a3r35fu7pure55", None) - assert decode_p2w(script_pubkey)[0] == utils.script_to_address(script_pubkey, "mainnet") - - script_pubkey = binascii.unhexlify( - "0020dfe1739dc0711f64ced999a2306691ff98fff038b2f40aec2b5ae917610ea0ac" - ) - assert decode_p2w(script_pubkey) == ("bc1qmlsh88wqwy0kfnkenx3rqe53l7v0lupc6q5xx6", None) - assert decode_p2w(script_pubkey)[0] == utils.script_to_address(script_pubkey, "mainnet")