Skip to content

Commit

Permalink
add shutdown channel restart
Browse files Browse the repository at this point in the history
  • Loading branch information
gpBlockchain authored and 15168316096 committed Dec 30, 2024
1 parent e536a0a commit f0c0da2
Show file tree
Hide file tree
Showing 4 changed files with 381 additions and 117 deletions.
91 changes: 54 additions & 37 deletions framework/basic_fiber.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ def teardown_class(cls):
cls.node.clean()

def faucet(
self,
account_private_key,
ckb_balance,
udt_owner_private_key=None,
udt_balance=1000 * 1000000000,
self,
account_private_key,
ckb_balance,
udt_owner_private_key=None,
udt_balance=1000 * 1000000000,
):
if ckb_balance > 60:
account = self.Ckb_cli.util_key_info_by_private_key(account_private_key)
Expand All @@ -192,7 +192,7 @@ def faucet(
self.Miner.miner_until_tx_committed(self.node, tx_hash)

def generate_account(
self, ckb_balance, udt_owner_private_key=None, udt_balance=1000 * 1000000000
self, ckb_balance, udt_owner_private_key=None, udt_balance=1000 * 1000000000
):
# error
# if self.debug:
Expand Down Expand Up @@ -249,15 +249,19 @@ def start_new_fiber(self, account_private_key, config=None):
fiber.start(self.node)
return fiber

def wait_for_channel_state(self, client, peer_id, expected_state, timeout=120):
def wait_for_channel_state(
self, client, peer_id, expected_state, timeout=120, include_closed=False
):
"""Wait for a channel to reach a specific state.
1. NEGOTIATING_FUNDING
2. CHANNEL_READY
3. Closed
3. CLOSED
"""
for _ in range(timeout):
channels = client.list_channels({"peer_id": peer_id})
channels = client.list_channels(
{"peer_id": peer_id, "include_closed": include_closed}
)
if len(channels["channels"]) == 0:
time.sleep(1)
continue
Expand All @@ -282,28 +286,41 @@ def get_account_udt_script(self, account_private_key):
"args": self.udtContract.get_owner_arg_by_lock_arg(account1["lock_arg"]),
}

def open_channel(self, fiber1: Fiber, fiber2: Fiber, fiber1_balance, fiber2_balance, fiber1_fee=1000,
fiber2_fee=1000):
fiber1.get_client().open_channel({
"peer_id": fiber2.get_peer_id(),
"funding_amount": hex(fiber1_balance + fiber2_balance + 62 * 100000000),
"tlc_fee_proportional_millionths": hex(fiber1_fee),
"public": True,
})
self.wait_for_channel_state(fiber1.get_client(), fiber2.get_peer_id(), "CHANNEL_READY")
channels = fiber1.get_client().list_channels(
{"peer_id": fiber2.get_peer_id()}
def open_channel(
self,
fiber1: Fiber,
fiber2: Fiber,
fiber1_balance,
fiber2_balance,
fiber1_fee=1000,
fiber2_fee=1000,
):
fiber1.get_client().open_channel(
{
"peer_id": fiber2.get_peer_id(),
"funding_amount": hex(fiber1_balance + fiber2_balance + 62 * 100000000),
"tlc_fee_proportional_millionths": hex(fiber1_fee),
"public": True,
}
)
self.wait_for_channel_state(
fiber1.get_client(), fiber2.get_peer_id(), "CHANNEL_READY"
)
channels = fiber1.get_client().list_channels({"peer_id": fiber2.get_peer_id()})
payment = fiber1.get_client().send_payment(
{
"target_pubkey": fiber2.get_client().node_info()["public_key"],
"amount": hex(fiber2_balance),
"keysend": True,
}
)
fiber2.get_client().update_channel(
{
"channel_id": channels["channels"][0]["channel_id"],
"tlc_fee_proportional_millionths": hex(fiber2_fee),
}
)
payment = fiber1.get_client().send_payment({
"target_pubkey": fiber2.get_client().node_info()["public_key"],
"amount": hex(fiber2_balance),
"keysend": True,
})
fiber2.get_client().update_channel({
"channel_id": channels["channels"][0]["channel_id"],
"tlc_fee_proportional_millionths": hex(fiber2_fee),
})
self.wait_payment_state(fiber1, payment['payment_hash'], "Success")
self.wait_payment_state(fiber1, payment["payment_hash"], "Success")
# channels = fiber1.get_client().list_channels({"peer_id": fiber2.get_peer_id()})
# assert channels["channels"][0]["local_balance"] == hex(fiber1_balance)
# assert channels["channels"][0]["remote_balance"] == hex(fiber2_balance)
Expand Down Expand Up @@ -346,15 +363,15 @@ def wait_and_check_tx_pool_fee(self, fee_rate, check=True, try_size=120):
)
if check:
assert (
int(pool_tx_detail_info["score_sortkey"]["fee"], 16)
* 1000
/ int(pool_tx_detail_info["score_sortkey"]["weight"], 16)
== fee_rate
int(pool_tx_detail_info["score_sortkey"]["fee"], 16)
* 1000
/ int(pool_tx_detail_info["score_sortkey"]["weight"], 16)
== fee_rate
)
return pool["pending"][0]

def wait_invoice_state(
self, client, payment_hash, status="Paid", timeout=120, interval=1
self, client, payment_hash, status="Paid", timeout=120, interval=1
):
"""
status:
Expand Down Expand Up @@ -497,11 +514,11 @@ def get_fiber_env(self, new_fiber_count=0):
state_name = channel["state"]["state_name"]
local_balance = int(channel["local_balance"], 16) / 100000000
offered_tlc_balance = (
int(channel["offered_tlc_balance"], 16) / 100000000
int(channel["offered_tlc_balance"], 16) / 100000000
)
remote_balance = int(channel["remote_balance"], 16) / 100000000
received_tlc_balance = (
int(channel["received_tlc_balance"], 16) / 100000000
int(channel["received_tlc_balance"], 16) / 100000000
)
created_at_hex = int(channel["created_at"], 16) / 1000
created_at = datetime.datetime.fromtimestamp(created_at_hex).strftime(
Expand Down
93 changes: 93 additions & 0 deletions test_cases/fiber/devnet/shutdown_channel/test_force_restart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import time
from framework.basic_fiber import FiberTest


class TestForceRestart(FiberTest):
# FiberTest.debug = True

def test_force_restart_fiber_node_shutdown_channel(self):
"""
shutdown过程强制重启
1.发起方强制重启
2.接受方强制重启
3.检查通道是否被正常关闭,并且检查balance是否返还
Returns:
"""
self.fiber1.get_client().open_channel(
{
"peer_id": self.fiber2.get_peer_id(),
"funding_amount": hex(1000 * 100000000),
"public": True,
}
)
open_channel_tx_hash = self.wait_and_check_tx_pool_fee(1000, False)
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().graph_channels()

before_balance1 = self.Ckb_cli.wallet_get_capacity(
self.account1["address"]["testnet"]
)
before_balance2 = self.Ckb_cli.wallet_get_capacity(
self.account2["address"]["testnet"]
)
# shutdown channel
N1N2_CHANNEL_ID = self.fiber1.get_client().list_channels({})["channels"][0][
"channel_id"
]
print(N1N2_CHANNEL_ID)
cell = self.node.getClient().get_live_cell("0x0", open_channel_tx_hash)
assert cell["status"] == "live"

shutdown_content = self.fiber1.get_client().shutdown_channel(
{
"channel_id": N1N2_CHANNEL_ID,
"close_script": {
"code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
"hash_type": "type",
"args": self.account1["lock_arg"],
},
"fee_rate": "0x3FC",
}
)
print(f"shutdown_content:{shutdown_content}") # return None
result = self.node.getClient().get_live_cell("0x0", open_channel_tx_hash)
assert result["status"] == "live"
# 1.接收方stop
self.fiber2.force_stop()
time.sleep(10)
result = self.node.getClient().get_live_cell("0x0", open_channel_tx_hash)
assert result["status"] == "unknown"
# 2.发送方重启
self.fiber1.force_stop()
time.sleep(10)
self.fiber1.start()
time.sleep(10)
# 3.接收方start
self.fiber2.start()
time.sleep(10)
result = self.node.getClient().get_live_cell("0x0", open_channel_tx_hash)
assert result["status"] == "unknown"
print(f"result=:{result}")
# 2.检查channel被close成功
node_info = self.fiber1.get_client().node_info()
print("node info :", node_info)
assert node_info["channel_count"] == "0x0"
after_balance1 = self.Ckb_cli.wallet_get_capacity(
self.account1["address"]["testnet"]
)
after_balance2 = self.Ckb_cli.wallet_get_capacity(
self.account2["address"]["testnet"]
)
print("before_balance1:", before_balance1)
print("before_balance2:", before_balance2)
print("after_balance1:", after_balance1)
print("after_balance2:", after_balance2)
# 3.检查关闭后balance被正常返还
assert after_balance2 - before_balance2 == 62.0
Loading

0 comments on commit f0c0da2

Please sign in to comment.