Skip to content

Commit f0c0da2

Browse files
gpBlockchain15168316096
authored andcommitted
add shutdown channel restart
1 parent e536a0a commit f0c0da2

File tree

4 files changed

+381
-117
lines changed

4 files changed

+381
-117
lines changed

framework/basic_fiber.py

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,11 @@ def teardown_class(cls):
164164
cls.node.clean()
165165

166166
def faucet(
167-
self,
168-
account_private_key,
169-
ckb_balance,
170-
udt_owner_private_key=None,
171-
udt_balance=1000 * 1000000000,
167+
self,
168+
account_private_key,
169+
ckb_balance,
170+
udt_owner_private_key=None,
171+
udt_balance=1000 * 1000000000,
172172
):
173173
if ckb_balance > 60:
174174
account = self.Ckb_cli.util_key_info_by_private_key(account_private_key)
@@ -192,7 +192,7 @@ def faucet(
192192
self.Miner.miner_until_tx_committed(self.node, tx_hash)
193193

194194
def generate_account(
195-
self, ckb_balance, udt_owner_private_key=None, udt_balance=1000 * 1000000000
195+
self, ckb_balance, udt_owner_private_key=None, udt_balance=1000 * 1000000000
196196
):
197197
# error
198198
# if self.debug:
@@ -249,15 +249,19 @@ def start_new_fiber(self, account_private_key, config=None):
249249
fiber.start(self.node)
250250
return fiber
251251

252-
def wait_for_channel_state(self, client, peer_id, expected_state, timeout=120):
252+
def wait_for_channel_state(
253+
self, client, peer_id, expected_state, timeout=120, include_closed=False
254+
):
253255
"""Wait for a channel to reach a specific state.
254256
1. NEGOTIATING_FUNDING
255257
2. CHANNEL_READY
256-
3. Closed
258+
3. CLOSED
257259
258260
"""
259261
for _ in range(timeout):
260-
channels = client.list_channels({"peer_id": peer_id})
262+
channels = client.list_channels(
263+
{"peer_id": peer_id, "include_closed": include_closed}
264+
)
261265
if len(channels["channels"]) == 0:
262266
time.sleep(1)
263267
continue
@@ -282,28 +286,41 @@ def get_account_udt_script(self, account_private_key):
282286
"args": self.udtContract.get_owner_arg_by_lock_arg(account1["lock_arg"]),
283287
}
284288

285-
def open_channel(self, fiber1: Fiber, fiber2: Fiber, fiber1_balance, fiber2_balance, fiber1_fee=1000,
286-
fiber2_fee=1000):
287-
fiber1.get_client().open_channel({
288-
"peer_id": fiber2.get_peer_id(),
289-
"funding_amount": hex(fiber1_balance + fiber2_balance + 62 * 100000000),
290-
"tlc_fee_proportional_millionths": hex(fiber1_fee),
291-
"public": True,
292-
})
293-
self.wait_for_channel_state(fiber1.get_client(), fiber2.get_peer_id(), "CHANNEL_READY")
294-
channels = fiber1.get_client().list_channels(
295-
{"peer_id": fiber2.get_peer_id()}
289+
def open_channel(
290+
self,
291+
fiber1: Fiber,
292+
fiber2: Fiber,
293+
fiber1_balance,
294+
fiber2_balance,
295+
fiber1_fee=1000,
296+
fiber2_fee=1000,
297+
):
298+
fiber1.get_client().open_channel(
299+
{
300+
"peer_id": fiber2.get_peer_id(),
301+
"funding_amount": hex(fiber1_balance + fiber2_balance + 62 * 100000000),
302+
"tlc_fee_proportional_millionths": hex(fiber1_fee),
303+
"public": True,
304+
}
305+
)
306+
self.wait_for_channel_state(
307+
fiber1.get_client(), fiber2.get_peer_id(), "CHANNEL_READY"
308+
)
309+
channels = fiber1.get_client().list_channels({"peer_id": fiber2.get_peer_id()})
310+
payment = fiber1.get_client().send_payment(
311+
{
312+
"target_pubkey": fiber2.get_client().node_info()["public_key"],
313+
"amount": hex(fiber2_balance),
314+
"keysend": True,
315+
}
316+
)
317+
fiber2.get_client().update_channel(
318+
{
319+
"channel_id": channels["channels"][0]["channel_id"],
320+
"tlc_fee_proportional_millionths": hex(fiber2_fee),
321+
}
296322
)
297-
payment = fiber1.get_client().send_payment({
298-
"target_pubkey": fiber2.get_client().node_info()["public_key"],
299-
"amount": hex(fiber2_balance),
300-
"keysend": True,
301-
})
302-
fiber2.get_client().update_channel({
303-
"channel_id": channels["channels"][0]["channel_id"],
304-
"tlc_fee_proportional_millionths": hex(fiber2_fee),
305-
})
306-
self.wait_payment_state(fiber1, payment['payment_hash'], "Success")
323+
self.wait_payment_state(fiber1, payment["payment_hash"], "Success")
307324
# channels = fiber1.get_client().list_channels({"peer_id": fiber2.get_peer_id()})
308325
# assert channels["channels"][0]["local_balance"] == hex(fiber1_balance)
309326
# assert channels["channels"][0]["remote_balance"] == hex(fiber2_balance)
@@ -346,15 +363,15 @@ def wait_and_check_tx_pool_fee(self, fee_rate, check=True, try_size=120):
346363
)
347364
if check:
348365
assert (
349-
int(pool_tx_detail_info["score_sortkey"]["fee"], 16)
350-
* 1000
351-
/ int(pool_tx_detail_info["score_sortkey"]["weight"], 16)
352-
== fee_rate
366+
int(pool_tx_detail_info["score_sortkey"]["fee"], 16)
367+
* 1000
368+
/ int(pool_tx_detail_info["score_sortkey"]["weight"], 16)
369+
== fee_rate
353370
)
354371
return pool["pending"][0]
355372

356373
def wait_invoice_state(
357-
self, client, payment_hash, status="Paid", timeout=120, interval=1
374+
self, client, payment_hash, status="Paid", timeout=120, interval=1
358375
):
359376
"""
360377
status:
@@ -497,11 +514,11 @@ def get_fiber_env(self, new_fiber_count=0):
497514
state_name = channel["state"]["state_name"]
498515
local_balance = int(channel["local_balance"], 16) / 100000000
499516
offered_tlc_balance = (
500-
int(channel["offered_tlc_balance"], 16) / 100000000
517+
int(channel["offered_tlc_balance"], 16) / 100000000
501518
)
502519
remote_balance = int(channel["remote_balance"], 16) / 100000000
503520
received_tlc_balance = (
504-
int(channel["received_tlc_balance"], 16) / 100000000
521+
int(channel["received_tlc_balance"], 16) / 100000000
505522
)
506523
created_at_hex = int(channel["created_at"], 16) / 1000
507524
created_at = datetime.datetime.fromtimestamp(created_at_hex).strftime(
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import time
2+
from framework.basic_fiber import FiberTest
3+
4+
5+
class TestForceRestart(FiberTest):
6+
# FiberTest.debug = True
7+
8+
def test_force_restart_fiber_node_shutdown_channel(self):
9+
"""
10+
shutdown过程强制重启
11+
1.发起方强制重启
12+
2.接受方强制重启
13+
3.检查通道是否被正常关闭,并且检查balance是否返还
14+
Returns:
15+
16+
"""
17+
self.fiber1.get_client().open_channel(
18+
{
19+
"peer_id": self.fiber2.get_peer_id(),
20+
"funding_amount": hex(1000 * 100000000),
21+
"public": True,
22+
}
23+
)
24+
open_channel_tx_hash = self.wait_and_check_tx_pool_fee(1000, False)
25+
self.wait_for_channel_state(
26+
self.fiber1.get_client(), self.fiber2.get_peer_id(), "CHANNEL_READY", 120
27+
)
28+
channels = self.fiber1.get_client().list_channels(
29+
{"peer_id": self.fiber2.get_peer_id()}
30+
)
31+
N1N2_CHANNEL_ID = channels["channels"][0]["channel_id"]
32+
self.fiber1.get_client().graph_channels()
33+
34+
before_balance1 = self.Ckb_cli.wallet_get_capacity(
35+
self.account1["address"]["testnet"]
36+
)
37+
before_balance2 = self.Ckb_cli.wallet_get_capacity(
38+
self.account2["address"]["testnet"]
39+
)
40+
# shutdown channel
41+
N1N2_CHANNEL_ID = self.fiber1.get_client().list_channels({})["channels"][0][
42+
"channel_id"
43+
]
44+
print(N1N2_CHANNEL_ID)
45+
cell = self.node.getClient().get_live_cell("0x0", open_channel_tx_hash)
46+
assert cell["status"] == "live"
47+
48+
shutdown_content = self.fiber1.get_client().shutdown_channel(
49+
{
50+
"channel_id": N1N2_CHANNEL_ID,
51+
"close_script": {
52+
"code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
53+
"hash_type": "type",
54+
"args": self.account1["lock_arg"],
55+
},
56+
"fee_rate": "0x3FC",
57+
}
58+
)
59+
print(f"shutdown_content:{shutdown_content}") # return None
60+
result = self.node.getClient().get_live_cell("0x0", open_channel_tx_hash)
61+
assert result["status"] == "live"
62+
# 1.接收方stop
63+
self.fiber2.force_stop()
64+
time.sleep(10)
65+
result = self.node.getClient().get_live_cell("0x0", open_channel_tx_hash)
66+
assert result["status"] == "unknown"
67+
# 2.发送方重启
68+
self.fiber1.force_stop()
69+
time.sleep(10)
70+
self.fiber1.start()
71+
time.sleep(10)
72+
# 3.接收方start
73+
self.fiber2.start()
74+
time.sleep(10)
75+
result = self.node.getClient().get_live_cell("0x0", open_channel_tx_hash)
76+
assert result["status"] == "unknown"
77+
print(f"result=:{result}")
78+
# 2.检查channel被close成功
79+
node_info = self.fiber1.get_client().node_info()
80+
print("node info :", node_info)
81+
assert node_info["channel_count"] == "0x0"
82+
after_balance1 = self.Ckb_cli.wallet_get_capacity(
83+
self.account1["address"]["testnet"]
84+
)
85+
after_balance2 = self.Ckb_cli.wallet_get_capacity(
86+
self.account2["address"]["testnet"]
87+
)
88+
print("before_balance1:", before_balance1)
89+
print("before_balance2:", before_balance2)
90+
print("after_balance1:", after_balance1)
91+
print("after_balance2:", after_balance2)
92+
# 3.检查关闭后balance被正常返还
93+
assert after_balance2 - before_balance2 == 62.0

0 commit comments

Comments
 (0)