Skip to content

Commit af653a4

Browse files
committed
add test
1 parent abb6152 commit af653a4

File tree

17 files changed

+799
-17
lines changed

17 files changed

+799
-17
lines changed

framework/basic_fiber.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def setup_class(cls):
3636
cls.account2_private_key
3737
)
3838
cls.node = cls.CkbNode.init_dev_by_port(
39-
cls.CkbNodeConfigPath.CURRENT_FIBER, "contract/node", 8114, 8115
39+
cls.CkbNodeConfigPath.CURRENT_FIBER, "contract/node", 8114, 8125
4040
)
4141

4242
if cls.debug:
@@ -87,7 +87,7 @@ def setup_method(cls, method):
8787
"transactions"
8888
][0]["hash"]
8989
#
90-
cls.udtContract = UdtContract(xudt_contract_hash, 10)
90+
cls.udtContract = UdtContract(xudt_contract_hash, 9)
9191
#
9292
deploy_hash, deploy_index = cls.udtContract.get_deploy_hash_and_index()
9393

@@ -245,7 +245,12 @@ def start_new_fiber(self, account_private_key, config=None):
245245
return fiber
246246

247247
def wait_for_channel_state(self, client, peer_id, expected_state, timeout=120):
248-
"""Wait for a channel to reach a specific state."""
248+
"""Wait for a channel to reach a specific state.
249+
1. NEGOTIATING_FUNDING
250+
2. CHANNEL_READY
251+
3. Closed
252+
253+
"""
249254
for _ in range(timeout):
250255
channels = client.list_channels({"peer_id": peer_id})
251256
if len(channels["channels"]) == 0:
@@ -288,6 +293,27 @@ def wait_payment_state(self, client, payment_hash, status="Success", timeout=120
288293
f"status did not reach state {expected_state} within timeout period."
289294
)
290295

296+
def wait_invoice_state(
297+
self, client, payment_hash, status="Success", timeout=120, interval=1
298+
):
299+
"""
300+
status:
301+
1. 状态为Open
302+
2. 状态为Cancelled
303+
3. 状态为Expired
304+
4. 状态为Received
305+
5. 状态为Paid
306+
307+
"""
308+
for i in range(timeout):
309+
result = client.get_client().get_invoice({"payment_hash": payment_hash})
310+
if result["status"] == status:
311+
return
312+
time.sleep(interval)
313+
raise TimeoutError(
314+
f"status did not reach state {expected_state} within timeout period."
315+
)
316+
291317
def get_fiber_env(self, new_fiber_count=0):
292318
# print ckb tip number
293319
for i in range(new_fiber_count):

framework/fiber_rpc.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,26 @@ def parse_invoice(self, param):
8686
def connect_peer(self, param):
8787
return self.call("connect_peer", [param])
8888

89+
def cancel_invoice(self, param):
90+
return self.call("cancel_invoice", [param])
91+
92+
def get_invoice(self, param):
93+
"""
94+
curl --location 'http://127.0.0.1:8228' --header 'Content-Type: application/json' --data '{
95+
"id": 42,
96+
"jsonrpc": "2.0",
97+
"method": "get_invoice",
98+
"params": [
99+
{
100+
"payment_hash": "0x08f64fbcb38189aedb109ff049b6c8a20ba9b61a836fef35a6699c2004fe6902"
101+
}
102+
]
103+
}'
104+
response:
105+
{"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}
106+
"""
107+
return self.call("get_invoice", [param])
108+
89109
def disconnect_peer(self, param):
90110
return self.call("disconnect_peer", [param])
91111

framework/helper/contract.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,12 @@ def invoke_ckb_contract(
161161
hex(input_cells[i]["index"]), input_cells[i]["tx_hash"], True
162162
)
163163
input_cell_cap += int(cell["cell"]["output"]["capacity"], 16)
164-
input_cells_hashs = [input_cell["tx_hash"] for input_cell in input_cells]
164+
input_cells_hashes = [input_cell["tx_hash"] for input_cell in input_cells]
165165

166166
for i in range(len(account_live_cells["live_cells"])):
167-
if account_live_cells["live_cells"][i]["tx_hash"] in input_cells_hashs:
167+
if account_live_cells["live_cells"][i]["tx_hash"] in input_cells_hashes:
168168
continue
169-
if input_cell_cap > 10000000000:
169+
if input_cell_cap > 20000000000:
170170
break
171171
input_cell_out_point = {
172172
"tx_hash": account_live_cells["live_cells"][i]["tx_hash"],
@@ -183,7 +183,7 @@ def invoke_ckb_contract(
183183
)
184184

185185
input_cell_out_points.append(input_cell_out_point)
186-
if input_cell_cap > 10000000000:
186+
if input_cell_cap > 20000000000:
187187
break
188188

189189
# get output_cells.cap = input_cell.cap - fee
@@ -271,9 +271,10 @@ def invoke_ckb_contract(
271271
)
272272
data = "0x"
273273
# add dep
274-
tx_add_cell_dep(cell_dep["tx_hash"], cell_dep["index"], tmp_tx_file)
275274
for cell_dep_tmp in cell_deps:
276275
tx_add_cell_dep(cell_dep_tmp["tx_hash"], cell_dep_tmp["index"], tmp_tx_file)
276+
tx_add_cell_dep(cell_dep["tx_hash"], cell_dep["index"], tmp_tx_file)
277+
277278
# sign
278279
sign_data = tx_sign_inputs(account_private, tmp_tx_file, api_url)
279280
tx_add_signature(

framework/test_fiber.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ def prepare(self, update_config=None):
7777
self.fiber_config_enum.fiber_config_path,
7878
self.fiber_config_path,
7979
)
80+
shutil.copy(
81+
"{root_path}/source/template/ckb/fiber/dev.toml".format(
82+
root_path=get_project_root()
83+
),
84+
self.tmp_path,
85+
)
8086
target_dir = os.path.join(self.tmp_path, "ckb")
8187
os.makedirs(target_dir, exist_ok=True) # 创建文件夹,如果已存在则不报错
8288
with open(f"{self.tmp_path}/ckb/key", "w") as f:
@@ -132,15 +138,15 @@ def read_ckb_key(self):
132138
return self.account_private
133139

134140
def start(self, node=None):
135-
env_map = dict(os.environ) # Make a copy of the current environment
136-
if node:
137-
contract_map = self.get_contract_env_map(node)
138-
env_map.update(contract_map)
139-
for key in env_map:
140-
print(f"{key}={env_map[key]}")
141+
# env_map = dict(os.environ) # Make a copy of the current environment
142+
# if node:
143+
# contract_map = self.get_contract_env_map(node)
144+
# env_map.update(contract_map)
145+
# for key in env_map:
146+
# print(f"{key}={env_map[key]}")
141147
run_command(
142-
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 &",
143-
env=env_map,
148+
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 &"
149+
# env=env_map,
144150
)
145151
# wait rpc start
146152
time.sleep(2)

test_cases/fiber/devnet/accept_channel/test_shutdown_script.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
class TestShutdownScript(FiberTest):
9-
FiberTest.debug = True
9+
# FiberTest.debug = True
1010

1111
@pytest.mark.skip("repeat")
1212
def test_shutdown_script_none(self):
@@ -300,3 +300,7 @@ def test_shutdown_script_too_big(self):
300300
"fee_rate": "0x3FC",
301301
}
302302
)
303+
time.sleep(10)
304+
self.fiber1.get_client().list_channels({})
305+
self.fiber2.get_client().list_channels({})
306+
# todo close 失败,需要能查到channels

0 commit comments

Comments
 (0)