Skip to content

Commit 44ff11a

Browse files
committed
watch tower test
1 parent b70e8e3 commit 44ff11a

File tree

3 files changed

+3267
-0
lines changed

3 files changed

+3267
-0
lines changed
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
import time
2+
3+
from framework.basic_fiber import FiberTest
4+
5+
6+
class TestMutilShutdown(FiberTest):
7+
8+
def test_mutil_shutdown(self):
9+
"""
10+
Test the shutdown process for multiple channels.
11+
Steps:
12+
1. Open CKB channels.
13+
2. Open UDT channels.
14+
3. Shutdown channels in order.
15+
4. Wait for shutdown commit.
16+
5. Check open_channel cell cost.
17+
6. node1 and node2 stop
18+
7. Generate epoch.
19+
8. node1 and node2 start
20+
9. Wait for channel split
21+
Returns:
22+
23+
"""
24+
ckb_channel_size = 4
25+
udt_channel_size = 4
26+
27+
# 1. Open CKB channels.
28+
for i in range(ckb_channel_size):
29+
self.fiber1.get_client().open_channel(
30+
{
31+
"peer_id": self.fiber2.get_peer_id(),
32+
"funding_amount": hex(1000 * 100000000),
33+
"public": True,
34+
"shutdown_script": {
35+
"code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
36+
"hash_type": "type",
37+
"args": f"0x000{i}",
38+
},
39+
}
40+
)
41+
self.wait_for_channel_state(
42+
self.fiber1.get_client(), self.fiber2.get_peer_id(), "CHANNEL_READY"
43+
)
44+
time.sleep(1)
45+
# 2. Open UDT channels.
46+
for i in range(udt_channel_size):
47+
self.fiber1.get_client().open_channel(
48+
{
49+
"peer_id": self.fiber2.get_peer_id(),
50+
"funding_amount": hex(200 * 100000000),
51+
"public": True,
52+
"funding_udt_type_script": self.get_account_udt_script(
53+
self.fiber1.account_private
54+
),
55+
"shutdown_script": {
56+
"code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
57+
"hash_type": "type",
58+
"args": f"0x010{i}",
59+
},
60+
}
61+
)
62+
time.sleep(1)
63+
self.wait_for_channel_state(
64+
self.fiber1.get_client(), self.fiber2.get_peer_id(), "CHANNEL_READY"
65+
)
66+
67+
# send tx
68+
69+
# 3. Shutdown channels in order.
70+
channels = self.fiber1.get_client().list_channels({})
71+
isNode1 = True
72+
for channel in channels["channels"]:
73+
print(
74+
{
75+
"channel_id": channel["channel_id"],
76+
}
77+
)
78+
if isNode1:
79+
self.fiber1.get_client().shutdown_channel(
80+
{
81+
"channel_id": channel["channel_id"],
82+
"close_script": {
83+
"code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
84+
"hash_type": "type",
85+
"args": self.account1["lock_arg"],
86+
},
87+
"fee_rate": "0x3FC",
88+
"force": True,
89+
}
90+
)
91+
isNode1 = False
92+
continue
93+
self.fiber2.get_client().shutdown_channel(
94+
{
95+
"channel_id": channel["channel_id"],
96+
"close_script": {
97+
"code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
98+
"hash_type": "type",
99+
"args": self.account2["lock_arg"],
100+
},
101+
"fee_rate": "0x3FC",
102+
"force": True,
103+
}
104+
)
105+
isNode1 = True
106+
107+
# 4. Wait for shutdown commit.
108+
self.wait_tx_pool(1, 1000)
109+
for i in range(10):
110+
self.Miner.miner_with_version(self.node, "0x0")
111+
# 5. todo Check open_channel cell cost.
112+
time.sleep(5)
113+
114+
# 6. node1 and node2 stop
115+
self.fiber1.stop()
116+
self.fiber2.stop()
117+
118+
# 7. Generate epoch.
119+
self.node.getClient().generate_epochs("0xa")
120+
121+
# 8. node1 and node2 start
122+
self.fiber1.start()
123+
self.fiber2.start()
124+
125+
# 9. Wait for channel split
126+
for i in range(ckb_channel_size):
127+
self.wait_cell_len(
128+
{
129+
"code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
130+
"hash_type": "type",
131+
"args": f"0x000{i}",
132+
},
133+
1,
134+
)
135+
cells = self.node.getClient().get_cells(
136+
{
137+
"script": {
138+
"code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
139+
"hash_type": "type",
140+
"args": f"0x000{i}",
141+
},
142+
"script_search_mode": "exact",
143+
"script_type": "lock",
144+
},
145+
"asc",
146+
"0x64",
147+
None,
148+
)
149+
message = self.get_tx_message(cells["objects"][0]["out_point"]["tx_hash"])
150+
assert {
151+
"capacity": 99999999545,
152+
"args": f"0x000{i}",
153+
} in message["output_cells"]
154+
155+
for i in range(udt_channel_size):
156+
self.wait_cell_len(
157+
{
158+
"code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
159+
"hash_type": "type",
160+
"args": f"0x010{i}",
161+
},
162+
1,
163+
)
164+
cells = self.node.getClient().get_cells(
165+
{
166+
"script": {
167+
"code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
168+
"hash_type": "type",
169+
"args": f"0x010{i}",
170+
},
171+
"script_search_mode": "exact",
172+
"script_type": "lock",
173+
},
174+
"asc",
175+
"0x64",
176+
None,
177+
)
178+
message = self.get_tx_message(cells["objects"][0]["out_point"]["tx_hash"])
179+
assert {
180+
"capacity": 12499999407,
181+
"args": f"0x010{i}",
182+
"udt_capacity": 20000000000,
183+
"udt_args": self.get_account_udt_script(self.fiber1.account_private)[
184+
"args"
185+
],
186+
} in message["output_cells"]
187+
188+
def wait_cell_len(self, script, length, timeout=300):
189+
for i in range(timeout):
190+
cells = self.node.getClient().get_cells(
191+
{
192+
"script": script,
193+
"script_search_mode": "exact",
194+
"script_type": "lock",
195+
},
196+
"asc",
197+
"0x64",
198+
None,
199+
)
200+
if len(cells["objects"]) < length:
201+
time.sleep(1)
202+
continue
203+
return
204+
raise Exception(f"timeout: {timeout}, scrit: {script}, length: {length}")

0 commit comments

Comments
 (0)