From af653a4db97318ae703defa49a2a9407ecb7ca09 Mon Sep 17 00:00:00 2001 From: gpBlockchain <744158715@qq.com> Date: Fri, 15 Nov 2024 11:01:47 +0800 Subject: [PATCH] add test --- framework/basic_fiber.py | 32 +- framework/fiber_rpc.py | 20 ++ framework/helper/contract.py | 11 +- framework/test_fiber.py | 22 +- .../accept_channel/test_shutdown_script.py | 6 +- .../cancel_invoice/test_cancel_invoice.py | 339 ++++++++++++++++++ .../devnet/get_invoice/test_get_invoice.py | 81 +++++ .../list_channels/test_list_channels.py | 254 +++++++++++++ .../fiber/devnet/new_invoice/test_amount.py | 9 + .../fiber/devnet/new_invoice/test_currency.py | 10 + .../devnet/new_invoice/test_description.py | 9 + .../fiber/devnet/new_invoice/test_expiry.py | 8 + .../new_invoice/test_fallback_address.py | 0 .../new_invoice/test_final_expiry_delta.py | 0 .../devnet/new_invoice/test_hash_algorithm.py | 7 + .../new_invoice/test_payment_preimage.py | 8 + .../new_invoice/test_udt_type_script.py | 0 17 files changed, 799 insertions(+), 17 deletions(-) create mode 100644 test_cases/fiber/devnet/cancel_invoice/test_cancel_invoice.py create mode 100644 test_cases/fiber/devnet/get_invoice/test_get_invoice.py create mode 100644 test_cases/fiber/devnet/list_channels/test_list_channels.py create mode 100644 test_cases/fiber/devnet/new_invoice/test_amount.py create mode 100644 test_cases/fiber/devnet/new_invoice/test_currency.py create mode 100644 test_cases/fiber/devnet/new_invoice/test_description.py create mode 100644 test_cases/fiber/devnet/new_invoice/test_expiry.py create mode 100644 test_cases/fiber/devnet/new_invoice/test_fallback_address.py create mode 100644 test_cases/fiber/devnet/new_invoice/test_final_expiry_delta.py create mode 100644 test_cases/fiber/devnet/new_invoice/test_hash_algorithm.py create mode 100644 test_cases/fiber/devnet/new_invoice/test_payment_preimage.py create mode 100644 test_cases/fiber/devnet/new_invoice/test_udt_type_script.py diff --git a/framework/basic_fiber.py b/framework/basic_fiber.py index afd4d13..ed82208 100644 --- a/framework/basic_fiber.py +++ b/framework/basic_fiber.py @@ -36,7 +36,7 @@ def setup_class(cls): cls.account2_private_key ) cls.node = cls.CkbNode.init_dev_by_port( - cls.CkbNodeConfigPath.CURRENT_FIBER, "contract/node", 8114, 8115 + cls.CkbNodeConfigPath.CURRENT_FIBER, "contract/node", 8114, 8125 ) if cls.debug: @@ -87,7 +87,7 @@ def setup_method(cls, method): "transactions" ][0]["hash"] # - cls.udtContract = UdtContract(xudt_contract_hash, 10) + cls.udtContract = UdtContract(xudt_contract_hash, 9) # deploy_hash, deploy_index = cls.udtContract.get_deploy_hash_and_index() @@ -245,7 +245,12 @@ def start_new_fiber(self, account_private_key, config=None): return fiber def wait_for_channel_state(self, client, peer_id, expected_state, timeout=120): - """Wait for a channel to reach a specific state.""" + """Wait for a channel to reach a specific state. + 1. NEGOTIATING_FUNDING + 2. CHANNEL_READY + 3. Closed + + """ for _ in range(timeout): channels = client.list_channels({"peer_id": peer_id}) if len(channels["channels"]) == 0: @@ -288,6 +293,27 @@ def wait_payment_state(self, client, payment_hash, status="Success", timeout=120 f"status did not reach state {expected_state} within timeout period." ) + def wait_invoice_state( + self, client, payment_hash, status="Success", timeout=120, interval=1 + ): + """ + status: + 1. 状态为Open + 2. 状态为Cancelled + 3. 状态为Expired + 4. 状态为Received + 5. 状态为Paid + + """ + for i in range(timeout): + result = client.get_client().get_invoice({"payment_hash": payment_hash}) + if result["status"] == status: + return + time.sleep(interval) + raise TimeoutError( + f"status did not reach state {expected_state} within timeout period." + ) + def get_fiber_env(self, new_fiber_count=0): # print ckb tip number for i in range(new_fiber_count): diff --git a/framework/fiber_rpc.py b/framework/fiber_rpc.py index 912ebfb..c310a54 100644 --- a/framework/fiber_rpc.py +++ b/framework/fiber_rpc.py @@ -86,6 +86,26 @@ def parse_invoice(self, param): def connect_peer(self, param): return self.call("connect_peer", [param]) + def cancel_invoice(self, param): + return self.call("cancel_invoice", [param]) + + def get_invoice(self, param): + """ + curl --location 'http://127.0.0.1:8228' --header 'Content-Type: application/json' --data '{ + "id": 42, + "jsonrpc": "2.0", + "method": "get_invoice", + "params": [ + { + "payment_hash": "0x08f64fbcb38189aedb109ff049b6c8a20ba9b61a836fef35a6699c2004fe6902" + } + ] + }' + response: + {"jsonrpc": "2.0", "result": {"invoice_address": "fibd11peseucdphcxgfw0pnm6vktap96klrlajceukj0q8wrshgjsryppgqzusjgf4ex560xfzxrj0et3hnws4zlgf3lly8csvwz2t4a6h7wap50qxf2mx4xw585ze4497k48f9y4g7rzg6h2gla7evwzu296y20t8jlwl0rjutcteyhyp6ymt4lfzyjf8k22l0q8gqnwm87jrj5knq45dqlw6yxqe0tlqg8638uhq9l0nh3ejrjc03mxam88u3v6wx46dqvy7chc2p0qn3lunckjmchxk9jc48qf34jzelpksqcgtt9c0pa87xnu2kza7x6r2dyg3flp9pkg6wsnyx54lrlqh0mnlafcqs2scnz7gqh8xyxs", "invoice": {"currency": "Fibd", "amount": "0x1", "signature": "0c041e1817180a010f0013111f1c131816121b181706160512181507000911151202191f0116100018080b0b05180f011d071e06131c0a16021d1e061a030a0d040811091f01050116081a0e1013040614151f031f00170f1b131f1d091800100a101813021e0800", "data": {"timestamp": "0x193287ae635", "payment_hash": "0x08f64fbcb38189aedb109ff049b6c8a20ba9b61a836fef35a6699c2004fe6902", "attrs": [{"Description": "test invoice generated by node2"}, {"ExpiryTime": {"secs": 3600, "nanos": 0}}, {"HashAlgorithm": "sha256"}, {"PayeePublicKey": "0299cbc950e551a2dc509000d801d768e714bf9bcfb11dde976e314bb1bb5c1af9"}]}}, "status": "Cancelled"}, "id": 42} + """ + return self.call("get_invoice", [param]) + def disconnect_peer(self, param): return self.call("disconnect_peer", [param]) diff --git a/framework/helper/contract.py b/framework/helper/contract.py index f045693..bdc18af 100644 --- a/framework/helper/contract.py +++ b/framework/helper/contract.py @@ -161,12 +161,12 @@ def invoke_ckb_contract( hex(input_cells[i]["index"]), input_cells[i]["tx_hash"], True ) input_cell_cap += int(cell["cell"]["output"]["capacity"], 16) - input_cells_hashs = [input_cell["tx_hash"] for input_cell in input_cells] + input_cells_hashes = [input_cell["tx_hash"] for input_cell in input_cells] for i in range(len(account_live_cells["live_cells"])): - if account_live_cells["live_cells"][i]["tx_hash"] in input_cells_hashs: + if account_live_cells["live_cells"][i]["tx_hash"] in input_cells_hashes: continue - if input_cell_cap > 10000000000: + if input_cell_cap > 20000000000: break input_cell_out_point = { "tx_hash": account_live_cells["live_cells"][i]["tx_hash"], @@ -183,7 +183,7 @@ def invoke_ckb_contract( ) input_cell_out_points.append(input_cell_out_point) - if input_cell_cap > 10000000000: + if input_cell_cap > 20000000000: break # get output_cells.cap = input_cell.cap - fee @@ -271,9 +271,10 @@ def invoke_ckb_contract( ) data = "0x" # add dep - tx_add_cell_dep(cell_dep["tx_hash"], cell_dep["index"], tmp_tx_file) for cell_dep_tmp in cell_deps: tx_add_cell_dep(cell_dep_tmp["tx_hash"], cell_dep_tmp["index"], tmp_tx_file) + tx_add_cell_dep(cell_dep["tx_hash"], cell_dep["index"], tmp_tx_file) + # sign sign_data = tx_sign_inputs(account_private, tmp_tx_file, api_url) tx_add_signature( diff --git a/framework/test_fiber.py b/framework/test_fiber.py index 57d09dd..ec4a88f 100644 --- a/framework/test_fiber.py +++ b/framework/test_fiber.py @@ -77,6 +77,12 @@ def prepare(self, update_config=None): self.fiber_config_enum.fiber_config_path, self.fiber_config_path, ) + shutil.copy( + "{root_path}/source/template/ckb/fiber/dev.toml".format( + root_path=get_project_root() + ), + self.tmp_path, + ) target_dir = os.path.join(self.tmp_path, "ckb") os.makedirs(target_dir, exist_ok=True) # 创建文件夹,如果已存在则不报错 with open(f"{self.tmp_path}/ckb/key", "w") as f: @@ -132,15 +138,15 @@ def read_ckb_key(self): return self.account_private def start(self, node=None): - env_map = dict(os.environ) # Make a copy of the current environment - if node: - contract_map = self.get_contract_env_map(node) - env_map.update(contract_map) - for key in env_map: - print(f"{key}={env_map[key]}") + # env_map = dict(os.environ) # Make a copy of the current environment + # if node: + # contract_map = self.get_contract_env_map(node) + # env_map.update(contract_map) + # for key in env_map: + # print(f"{key}={env_map[key]}") run_command( - f"RUST_LOG=info,fnn=debug {get_project_root()}/{self.fiber_config_enum.fiber_bin_path} -c {self.tmp_path}/config.yml -d {self.tmp_path} > {self.tmp_path}/node.log 2>&1 &", - env=env_map, + f"RUST_LOG=info,fnn=debug {get_project_root()}/{self.fiber_config_enum.fiber_bin_path} -c {self.tmp_path}/config.yml -d {self.tmp_path} > {self.tmp_path}/node.log 2>&1 &" + # env=env_map, ) # wait rpc start time.sleep(2) diff --git a/test_cases/fiber/devnet/accept_channel/test_shutdown_script.py b/test_cases/fiber/devnet/accept_channel/test_shutdown_script.py index 9d70aa8..5972c2e 100644 --- a/test_cases/fiber/devnet/accept_channel/test_shutdown_script.py +++ b/test_cases/fiber/devnet/accept_channel/test_shutdown_script.py @@ -6,7 +6,7 @@ class TestShutdownScript(FiberTest): - FiberTest.debug = True + # FiberTest.debug = True @pytest.mark.skip("repeat") def test_shutdown_script_none(self): @@ -300,3 +300,7 @@ def test_shutdown_script_too_big(self): "fee_rate": "0x3FC", } ) + time.sleep(10) + self.fiber1.get_client().list_channels({}) + self.fiber2.get_client().list_channels({}) + # todo close 失败,需要能查到channels diff --git a/test_cases/fiber/devnet/cancel_invoice/test_cancel_invoice.py b/test_cases/fiber/devnet/cancel_invoice/test_cancel_invoice.py new file mode 100644 index 0000000..0c84d31 --- /dev/null +++ b/test_cases/fiber/devnet/cancel_invoice/test_cancel_invoice.py @@ -0,0 +1,339 @@ +import time + +import pytest + +from framework.basic_fiber import FiberTest + + +class TestCancelInvoice(FiberTest): + """ + 1. 不存在的payment_hash + 2. 存在的invoice + 1. 状态为Open + 2. 状态为Cancelled + 3. 状态为Expired + 4. 状态为Received + 5. 状态为Paid + + 取消成功后,需要的断言 + 1. 查询get_invoice 状态为Cancelled + 2. 用户无法使用该invoice + 3. 能否再次创建该invoice? + """ + + # FiberTest.debug = True + + def test_not_exist_payment_hash(self): + """ + 1. 不存在的payment hash + Returns: + err: "invoice not found" + """ + with pytest.raises(Exception) as exc_info: + self.fiber1.get_client().cancel_invoice( + {"payment_hash": self.generate_random_preimage()} + ) + expected_error_message = "invoice not found" + assert expected_error_message in exc_info.value.args[0], ( + f"Expected substring '{expected_error_message}' " + f"not found in actual string '{exc_info.value.args[0]}'" + ) + + def test_cancel_invoice_that_statue_is_open(self): + """ + 1. new invoice + 2. cancel invoice + 3. query invoice + Returns: + """ + preimage = self.generate_random_preimage() + invoice = self.fiber1.get_client().new_invoice( + { + "amount": hex(1), + "currency": "Fibd", + "description": "test invoice generated by node2", + "expiry": "0xe10", + "final_cltv": "0x28", + "payment_preimage": preimage, + "hash_algorithm": "sha256", + } + ) + self.fiber1.get_client().cancel_invoice( + {"payment_hash": invoice["invoice"]["data"]["payment_hash"]} + ) + result = self.fiber1.get_client().get_invoice( + {"payment_hash": invoice["invoice"]["data"]["payment_hash"]} + ) + assert result["status"] == "Cancelled" + + def test_cancel_invoice_that_statue_is_cancelled(self): + """ + 1. new invoice + 2. cancel invoice + 3. cancel invoice again + invoice can not be canceled, current status: Cancelled + Returns: + """ + preimage = self.generate_random_preimage() + invoice = self.fiber1.get_client().new_invoice( + { + "amount": hex(1), + "currency": "Fibd", + "description": "test invoice generated by node2", + "expiry": "0xe10", + "final_cltv": "0x28", + "payment_preimage": preimage, + "hash_algorithm": "sha256", + } + ) + self.fiber1.get_client().cancel_invoice( + {"payment_hash": invoice["invoice"]["data"]["payment_hash"]} + ) + result = self.fiber1.get_client().get_invoice( + {"payment_hash": invoice["invoice"]["data"]["payment_hash"]} + ) + assert result["status"] == "Cancelled" + + with pytest.raises(Exception) as exc_info: + self.fiber1.get_client().cancel_invoice( + {"payment_hash": invoice["invoice"]["data"]["payment_hash"]} + ) + expected_error_message = ( + "invoice can not be canceled, current status: Cancelled" + ) + assert expected_error_message in exc_info.value.args[0], ( + f"Expected substring '{expected_error_message}' " + f"not found in actual string '{exc_info.value.args[0]}'" + ) + + def test_cancel_invoice_that_statue_is_expired(self): + """ + Expired + Returns: + + """ + preimage = self.generate_random_preimage() + invoice = self.fiber1.get_client().new_invoice( + { + "amount": hex(1), + "currency": "Fibd", + "description": "test invoice generated by node2", + "expiry": "0x0", + "final_cltv": "0x28", + "payment_preimage": preimage, + "hash_algorithm": "sha256", + } + ) + # time.sleep(1) + result = self.fiber1.get_client().get_invoice( + {"payment_hash": invoice["invoice"]["data"]["payment_hash"]} + ) + + assert result["status"] == "Expired" + result = self.fiber1.get_client().cancel_invoice( + {"payment_hash": invoice["invoice"]["data"]["payment_hash"]} + ) + assert result["status"] == "Cancelled" + result = self.fiber1.get_client().get_invoice( + {"payment_hash": invoice["invoice"]["data"]["payment_hash"]} + ) + assert result["status"] == "Cancelled" + + @pytest.mark.skip("https://github.com/nervosnetwork/fiber/issues/305") + def test_send_failed_that_invoice_cancel(self): + """ + 取消后,发送失败,下次交易不受影响 + + Returns: + + """ + temporary_channel_id = self.fiber2.get_client().open_channel( + { + "peer_id": self.fiber1.get_peer_id(), + "funding_amount": hex(1000 * 100000000), + "public": True, + # "tlc_fee_proportional_millionths": "0x4B0", + } + ) + self.wait_for_channel_state( + self.fiber2.get_client(), + self.fiber1.get_peer_id(), + "CHANNEL_READY", + 120, + ) + payment_preimage = self.generate_random_preimage() + invoice_balance = 1 * 100000000 + invoice = self.fiber1.get_client().new_invoice( + { + "amount": hex(invoice_balance), + "currency": "Fibd", + "description": "test invoice generated by node2", + "expiry": "0xe10", + "final_cltv": "0x28", + "payment_preimage": payment_preimage, + "hash_algorithm": "sha256", + } + ) + + self.fiber1.get_client().cancel_invoice( + {"payment_hash": invoice["invoice"]["data"]["payment_hash"]} + ) + payment = self.fiber2.get_client().send_payment( + { + "invoice": invoice["invoice_address"], + } + ) + + self.wait_payment_state(self.fiber2, payment["payment_hash"], "Failed") + payment_preimage = self.generate_random_preimage() + invoice_balance = 1 * 100000000 + invoice = self.fiber1.get_client().new_invoice( + { + "amount": hex(invoice_balance), + "currency": "Fibd", + "description": "test invoice generated by node2", + "expiry": "0xe10", + "final_cltv": "0x28", + "payment_preimage": payment_preimage, + "hash_algorithm": "sha256", + } + ) + before_channel = self.fiber2.get_client().list_channels({}) + payment = self.fiber2.get_client().send_payment( + { + "invoice": invoice["invoice_address"], + } + ) + self.wait_payment_state(self.fiber2, payment["payment_hash"], "Success") + after_channel = self.fiber2.get_client().list_channels({}) + assert ( + int(before_channel["channels"][0]["local_balance"], 16) + - int(after_channel["channels"][0]["local_balance"], 16) + == invoice_balance + ) + + def test_cancel_invoice_that_statue_is_paid(self): + temporary_channel_id = self.fiber2.get_client().open_channel( + { + "peer_id": self.fiber1.get_peer_id(), + "funding_amount": hex(1000 * 100000000), + "public": True, + # "tlc_fee_proportional_millionths": "0x4B0", + } + ) + self.wait_for_channel_state( + self.fiber2.get_client(), + self.fiber1.get_peer_id(), + "CHANNEL_READY", + 120, + ) + payment_preimage = self.generate_random_preimage() + invoice_balance = 1 * 100000000 + invoice = self.fiber1.get_client().new_invoice( + { + "amount": hex(invoice_balance), + "currency": "Fibd", + "description": "test invoice generated by node2", + "expiry": "0xe10", + "final_cltv": "0x28", + "payment_preimage": payment_preimage, + "hash_algorithm": "sha256", + } + ) + + payment_preimage = self.generate_random_preimage() + invoice_balance = 1 * 100000000 + invoice = self.fiber1.get_client().new_invoice( + { + "amount": hex(invoice_balance), + "currency": "Fibd", + "description": "test invoice generated by node2", + "expiry": "0xe10", + "final_cltv": "0x28", + "payment_preimage": payment_preimage, + "hash_algorithm": "sha256", + } + ) + before_channel = self.fiber2.get_client().list_channels({}) + payment = self.fiber2.get_client().send_payment( + { + "invoice": invoice["invoice_address"], + } + ) + self.wait_payment_state(self.fiber2, payment["payment_hash"], "Success") + after_channel = self.fiber2.get_client().list_channels({}) + assert ( + int(before_channel["channels"][0]["local_balance"], 16) + - int(after_channel["channels"][0]["local_balance"], 16) + == invoice_balance + ) + + with pytest.raises(Exception) as exc_info: + self.fiber1.get_client().cancel_invoice( + {"payment_hash": invoice["invoice"]["data"]["payment_hash"]} + ) + expected_error_message = "invoice can not be canceled, current status: Paid" + assert expected_error_message in exc_info.value.args[0], ( + f"Expected substring '{expected_error_message}' " + f"not found in actual string '{exc_info.value.args[0]}'" + ) + + def test_cancel_invoice_that_statue_is_receive(self): + temporary_channel_id = self.fiber2.get_client().open_channel( + { + "peer_id": self.fiber1.get_peer_id(), + "funding_amount": hex(1000 * 100000000), + "public": True, + # "tlc_fee_proportional_millionths": "0x4B0", + } + ) + self.wait_for_channel_state( + self.fiber2.get_client(), + self.fiber1.get_peer_id(), + "CHANNEL_READY", + 120, + ) + payment_preimage = self.generate_random_preimage() + invoice_balance = 1 * 100000000 + invoice = self.fiber1.get_client().new_invoice( + { + "amount": hex(invoice_balance), + "currency": "Fibd", + "description": "test invoice generated by node2", + "expiry": "0xe10", + "final_cltv": "0x28", + "payment_preimage": payment_preimage, + "hash_algorithm": "sha256", + } + ) + + payment_preimage = self.generate_random_preimage() + invoice_balance = 1 * 100000000 + invoice = self.fiber1.get_client().new_invoice( + { + "amount": hex(invoice_balance), + "currency": "Fibd", + "description": "test invoice generated by node2", + "expiry": "0xe10", + "final_cltv": "0x28", + "payment_preimage": payment_preimage, + "hash_algorithm": "sha256", + } + ) + before_channel = self.fiber2.get_client().list_channels({}) + payment = self.fiber2.get_client().send_payment( + { + "invoice": invoice["invoice_address"], + } + ) + self.wait_invoice_state( + self.fiber1, + invoice["invoice"]["data"]["payment_hash"], + "Received", + 20, + 0.0001, + ) + self.fiber1.get_client().cancel_invoice( + {"payment_hash": invoice["invoice"]["data"]["payment_hash"]} + ) + self.wait_payment_state(self.fiber2, payment["payment_hash"], "Failed") diff --git a/test_cases/fiber/devnet/get_invoice/test_get_invoice.py b/test_cases/fiber/devnet/get_invoice/test_get_invoice.py new file mode 100644 index 0000000..dfeef14 --- /dev/null +++ b/test_cases/fiber/devnet/get_invoice/test_get_invoice.py @@ -0,0 +1,81 @@ +import datetime +import time + +import pytest + +from framework.basic_fiber import FiberTest + + +class TestGetInvoice(FiberTest): + """ + 1. 能够查询到各种状态的invoice + cancel_invoice 测试了 + + """ + + # FiberTest.debug = True + + def test_get_exist_new_invoice(self): + """ + 1. new invoice + - parse invoice 能够解析 invoice_address ,解析结果和invoice 一致 + - + Returns: + + """ + invoice = self.fiber1.get_client().new_invoice( + { + "amount": hex(1), + "currency": "Fibd", + "description": "test invoice generated by node2", + "expiry": "0xe10", + "final_cltv": "0x28", + "payment_preimage": self.generate_random_preimage(), + "hash_algorithm": "sha256", + } + ) + + result = self.fiber1.get_client().get_invoice( + {"payment_hash": invoice["invoice"]["data"]["payment_hash"]} + ) + node_info = self.fiber1.get_client().node_info() + + assert ( + node_info["public_key"] + == result["invoice"]["data"]["attrs"][3]["PayeePublicKey"] + ) + parse_invoice = self.fiber1.get_client().parse_invoice( + {"invoice": invoice["invoice_address"]} + ) + assert parse_invoice["invoice"] == invoice["invoice"] + assert invoice["invoice"]["currency"] == "Fibd" + assert invoice["invoice"]["amount"] == "0x1" + assert ( + invoice["invoice"]["data"]["attrs"][0]["Description"] + == "test invoice generated by node2" + ) + assert invoice["invoice"]["data"]["attrs"][1]["ExpiryTime"]["secs"] == 3600 + assert invoice["invoice"]["data"]["attrs"][2]["HashAlgorithm"] == "sha256" + + # assert invoice['invoice']['data']['timestamp'] + assert int(int(invoice["invoice"]["data"]["timestamp"], 16) / 1000) == int( + datetime.datetime.now().timestamp() + ) + + def test_get_not_exist_invoice(self): + """ + not exist invoice + return err "invoice not found" + Returns: + + """ + + with pytest.raises(Exception) as exc_info: + result = self.fiber1.get_client().get_invoice( + {"payment_hash": self.generate_random_preimage()} + ) + expected_error_message = "invoice not found" + assert expected_error_message in exc_info.value.args[0], ( + f"Expected substring '{expected_error_message}' " + f"not found in actual string '{exc_info.value.args[0]}'" + ) diff --git a/test_cases/fiber/devnet/list_channels/test_list_channels.py b/test_cases/fiber/devnet/list_channels/test_list_channels.py new file mode 100644 index 0000000..4e56272 --- /dev/null +++ b/test_cases/fiber/devnet/list_channels/test_list_channels.py @@ -0,0 +1,254 @@ +import datetime +import time + +import pytest + +from framework.basic_fiber import FiberTest + + +class TestListChannels(FiberTest): + """ + 状态 + - 显示 pending 的channels + - 显示待 accept 的channel + - 显示 closed channels 信息 + - 显示 channel 的其他信息 + - 显示 accept dan没成功的channel + - 显示 下线的channel? 如何判断channels 是否在线 + """ + + # FiberTest.debug = True + + def test_peer_id(self): + """ + only show peer id 's channels + + Returns: + """ + account3_private_key = self.generate_account(1000) + fiber3 = self.start_new_fiber(account3_private_key) + fiber3.connect_peer(self.fiber1) + temporary_channel_id = self.fiber1.get_client().open_channel( + { + "peer_id": self.fiber2.get_peer_id(), + "funding_amount": hex(1000 * 100000000), + "public": True, + "funding_udt_type_script": self.get_account_udt_script( + self.fiber1.account_private + ), + } + ) + self.wait_for_channel_state( + self.fiber1.get_client(), self.fiber2.get_peer_id(), "CHANNEL_READY", 120 + ) + + temporary_channel_id = self.fiber1.get_client().open_channel( + { + "peer_id": fiber3.get_peer_id(), + "funding_amount": hex(1000 * 100000000), + "public": True, + } + ) + self.wait_for_channel_state( + self.fiber1.get_client(), fiber3.get_peer_id(), "CHANNEL_READY", 120 + ) + n12_channels = self.fiber1.get_client().list_channels( + {"peer_id": self.fiber2.get_peer_id()} + ) + + n21_channels = self.fiber2.get_client().list_channels( + {"peer_id": self.fiber1.get_peer_id()} + ) + assert ( + n12_channels["channels"][0]["channel_id"] + == n21_channels["channels"][0]["channel_id"] + ) + + n13_channels = self.fiber1.get_client().list_channels( + {"peer_id": fiber3.get_peer_id()} + ) + n31_channels = fiber3.get_client().list_channels( + {"peer_id": self.fiber1.get_peer_id()} + ) + assert ( + n13_channels["channels"][0]["channel_id"] + == n31_channels["channels"][0]["channel_id"] + ) + + def test_empty(self): + """ + show all channels + Returns: + """ + account3_private_key = self.generate_account(1000) + fiber3 = self.start_new_fiber(account3_private_key) + fiber3.connect_peer(self.fiber1) + temporary_channel_id = self.fiber1.get_client().open_channel( + { + "peer_id": self.fiber2.get_peer_id(), + "funding_amount": hex(1000 * 100000000), + "public": True, + "funding_udt_type_script": self.get_account_udt_script( + self.fiber1.account_private + ), + } + ) + self.wait_for_channel_state( + self.fiber1.get_client(), self.fiber2.get_peer_id(), "CHANNEL_READY", 120 + ) + + temporary_channel_id = self.fiber1.get_client().open_channel( + { + "peer_id": fiber3.get_peer_id(), + "funding_amount": hex(1000 * 100000000), + "public": True, + } + ) + self.wait_for_channel_state( + self.fiber1.get_client(), fiber3.get_peer_id(), "CHANNEL_READY", 120 + ) + channels = self.fiber1.get_client().list_channels({}) + assert len(channels["channels"]) == 2 + + def test_funding_udt_type_script(self): + """ + funding_udt_type_script + + Returns: + """ + account3_private_key = self.generate_account(1000) + fiber3 = self.start_new_fiber(account3_private_key) + fiber3.connect_peer(self.fiber1) + temporary_channel_id = self.fiber1.get_client().open_channel( + { + "peer_id": self.fiber2.get_peer_id(), + "funding_amount": hex(1000 * 100000000), + "public": True, + "funding_udt_type_script": self.get_account_udt_script( + self.fiber1.account_private + ), + } + ) + self.wait_for_channel_state( + self.fiber1.get_client(), self.fiber2.get_peer_id(), "CHANNEL_READY", 120 + ) + channels = self.fiber1.get_client().list_channels({}) + assert channels["channels"][0][ + "funding_udt_type_script" + ] == self.get_account_udt_script(self.fiber1.account_private) + + @pytest.mark.skip("pass") + def test_funding_udt_type_script_none(self): + """ + funding_udt_type_script: none == ckb + Returns: + """ + + def test_created_at(self): + """ + + Returns: + + """ + account3_private_key = self.generate_account(1000) + fiber3 = self.start_new_fiber(account3_private_key) + fiber3.connect_peer(self.fiber1) + temporary_channel_id = self.fiber1.get_client().open_channel( + { + "peer_id": self.fiber2.get_peer_id(), + "funding_amount": hex(1000 * 100000000), + "public": True, + "funding_udt_type_script": self.get_account_udt_script( + self.fiber1.account_private + ), + } + ) + self.wait_for_channel_state( + self.fiber1.get_client(), self.fiber2.get_peer_id(), "CHANNEL_READY", 120 + ) + channels = self.fiber1.get_client().list_channels({}) + created_at_hex = int(channels["channels"][0]["created_at"], 16) / 1000000 + + assert int(created_at_hex / 100) == int( + int(datetime.datetime.now().timestamp()) / 100 + ) + + def test_is_public(self): + """ + Returns: + """ + temporary_channel_id = self.fiber1.get_client().open_channel( + { + "peer_id": self.fiber2.get_peer_id(), + "funding_amount": hex(1000 * 100000000), + "public": True, + "funding_udt_type_script": self.get_account_udt_script( + self.fiber1.account_private + ), + } + ) + self.wait_for_channel_state( + self.fiber1.get_client(), self.fiber2.get_peer_id(), "CHANNEL_READY", 120 + ) + channels = self.fiber1.get_client().list_channels({}) + assert channels["channels"][0]["is_public"] == True + + def test_channel_outpoint(self): + """ + Returns: + """ + temporary_channel_id = self.fiber1.get_client().open_channel( + { + "peer_id": self.fiber2.get_peer_id(), + "funding_amount": hex(1000 * 100000000), + "public": True, + "funding_udt_type_script": self.get_account_udt_script( + self.fiber1.account_private + ), + } + ) + # self.wait_for_channel_state( + # self.fiber1.get_client(), self.fiber2.get_peer_id(), "NEGOTIATING_FUNDING", 120 + # ) + # channels = self.fiber1.get_client().list_channels({"peer_id": self.fiber2.get_peer_id()}) + # assert channels['channels'][0]['channel_outpoint'] is None + self.wait_for_channel_state( + self.fiber1.get_client(), self.fiber2.get_peer_id(), "CHANNEL_READY", 120 + ) + channels = self.fiber1.get_client().list_channels( + {"peer_id": self.fiber2.get_peer_id()} + ) + assert channels["channels"][0]["channel_outpoint"] is not None + print("channel_outpoint:", channels["channels"][0]["channel_outpoint"]) + + @pytest.mark.skip("close channels can't found") + def test_close_channels(self): + temporary_channel_id = self.fiber1.get_client().open_channel( + { + "peer_id": self.fiber2.get_peer_id(), + "funding_amount": hex(1000 * 100000000), + "public": True, + "funding_udt_type_script": self.get_account_udt_script( + self.fiber1.account_private + ), + } + ) + + self.wait_for_channel_state( + self.fiber1.get_client(), self.fiber2.get_peer_id(), "CHANNEL_READY", 120 + ) + channels = self.fiber1.get_client().list_channels( + {"peer_id": self.fiber2.get_peer_id()} + ) + N1N2_CHANNEL_ID = channels["channels"][0]["channel_id"] + self.fiber1.get_client().shutdown_channel( + { + "channel_id": N1N2_CHANNEL_ID, + "close_script": self.get_account_script(self.Config.ACCOUNT_PRIVATE_1), + "fee_rate": "0x3FC", + } + ) + # todo query shutdown_channel + # self.wait_for_channel_state(self.fiber1.get_client(), self.fiber2.get_peer_id(), "Closed") + # time.sleep(5) + # self.fiber1.get_client().list_channels({}) diff --git a/test_cases/fiber/devnet/new_invoice/test_amount.py b/test_cases/fiber/devnet/new_invoice/test_amount.py new file mode 100644 index 0000000..d093275 --- /dev/null +++ b/test_cases/fiber/devnet/new_invoice/test_amount.py @@ -0,0 +1,9 @@ +from framework.basic_fiber import FiberTest + + +class TestAmount(FiberTest): + """ + - 0 + - 溢出 + - 正常值 + """ diff --git a/test_cases/fiber/devnet/new_invoice/test_currency.py b/test_cases/fiber/devnet/new_invoice/test_currency.py new file mode 100644 index 0000000..81f9c7d --- /dev/null +++ b/test_cases/fiber/devnet/new_invoice/test_currency.py @@ -0,0 +1,10 @@ +from framework.basic_fiber import FiberTest + + +class TestCurrency(FiberTest): + """ + - Fibb, + - Fibt, + - Fibd, + - 其他 + """ diff --git a/test_cases/fiber/devnet/new_invoice/test_description.py b/test_cases/fiber/devnet/new_invoice/test_description.py new file mode 100644 index 0000000..4159beb --- /dev/null +++ b/test_cases/fiber/devnet/new_invoice/test_description.py @@ -0,0 +1,9 @@ +from framework.basic_fiber import FiberTest + + +class TestDescription(FiberTest): + """ + - none + - 随机值 英文字母 特殊符号 表情包🤔️ + - 特别长1mb + """ diff --git a/test_cases/fiber/devnet/new_invoice/test_expiry.py b/test_cases/fiber/devnet/new_invoice/test_expiry.py new file mode 100644 index 0000000..a964a93 --- /dev/null +++ b/test_cases/fiber/devnet/new_invoice/test_expiry.py @@ -0,0 +1,8 @@ +from framework.basic_fiber import FiberTest + + +class TestExpiry(FiberTest): + """ + - Hash256 + - 重复的 payment_preimage + """ diff --git a/test_cases/fiber/devnet/new_invoice/test_fallback_address.py b/test_cases/fiber/devnet/new_invoice/test_fallback_address.py new file mode 100644 index 0000000..e69de29 diff --git a/test_cases/fiber/devnet/new_invoice/test_final_expiry_delta.py b/test_cases/fiber/devnet/new_invoice/test_final_expiry_delta.py new file mode 100644 index 0000000..e69de29 diff --git a/test_cases/fiber/devnet/new_invoice/test_hash_algorithm.py b/test_cases/fiber/devnet/new_invoice/test_hash_algorithm.py new file mode 100644 index 0000000..0491f3b --- /dev/null +++ b/test_cases/fiber/devnet/new_invoice/test_hash_algorithm.py @@ -0,0 +1,7 @@ +from framework.basic_fiber import FiberTest + + +class TestHashAlgorithm(FiberTest): + + def test_01(self): + pass diff --git a/test_cases/fiber/devnet/new_invoice/test_payment_preimage.py b/test_cases/fiber/devnet/new_invoice/test_payment_preimage.py new file mode 100644 index 0000000..4e48806 --- /dev/null +++ b/test_cases/fiber/devnet/new_invoice/test_payment_preimage.py @@ -0,0 +1,8 @@ +from framework.basic_fiber import FiberTest + + +class TestPaymentPreimage(FiberTest): + """ + - Hash256 + - 重复的 payment_preimage + """ diff --git a/test_cases/fiber/devnet/new_invoice/test_udt_type_script.py b/test_cases/fiber/devnet/new_invoice/test_udt_type_script.py new file mode 100644 index 0000000..e69de29