Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
gpBlockchain committed Oct 30, 2024
1 parent 798b269 commit 8e146ac
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 6 deletions.
26 changes: 22 additions & 4 deletions framework/basic_fiber.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ class FiberTest(CkbTest):

@classmethod
def setup_class(cls):
"""
部署一个ckb 节点
启动 ckb 节点
Returns:
"""
cls.account1_private_key = cls.Config.ACCOUNT_PRIVATE_1
cls.account2_private_key = cls.Config.ACCOUNT_PRIVATE_2
cls.account1 = cls.Ckb_cli.util_key_info_by_private_key(
Expand All @@ -46,6 +53,16 @@ def setup_class(cls):
cls.Miner.make_tip_height_number(cls.node, 20)

def setup_method(cls, method):
"""
启动2个fiber
给 fiber1 充值udt 金额
连接2个fiber
Args:
method:
Returns:
"""
cls.did_pass = None
cls.fibers = []
cls.new_fibers = []
Expand Down Expand Up @@ -161,20 +178,20 @@ def faucet(
if udt_owner_private_key is None:
return account_private_key
tx_hash = issue_udt_tx(
cls.udtContract,
self.udtContract,
self.node.rpcUrl,
udt_owner_private_key,
account_private_key,
udt_balance,
)
self.Miner.miner_until_tx_committed(cls.node, tx_hash)
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
):
# error
if self.debug:
raise Exception("debug not support generate_account")
# if self.debug:
# raise Exception("debug not support generate_account")
account_private_key = generate_account_privakey()
self.faucet(
account_private_key, ckb_balance, udt_owner_private_key, udt_balance
Expand All @@ -192,6 +209,7 @@ def start_new_mock_fiber(self, account_private_key, config=None):
)
fiber.read_ckb_key()
self.new_fibers.append(fiber)
self.fibers.append(fiber)
return fiber

def start_new_fiber(self, account_private_key, config=None):
Expand Down
20 changes: 20 additions & 0 deletions framework/fiber_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ def shutdown_channel(self, param):
return self.call("shutdown_channel", [param])

def new_invoice(self, param):
"""
payment_preimage = self.generate_random_preimage()
invoice_balance = 100 * 100000000
invoice = self.fiber2.get_client().new_invoice(
{
"amount": hex(invoice_balance),
"currency": "Fibb",
"description": "test invoice generated by node2",
"expiry": "0xe10",
"final_cltv": "0x28",
"payment_preimage": payment_preimage,
"hash_algorithm": "sha256",
}
)
Args:
param:
Returns:
"""
return self.call("new_invoice", [param])

def parse_invoice(self, param):
Expand Down
3 changes: 2 additions & 1 deletion framework/helper/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def invoke_ckb_contract(
account = util_key_info_by_private_key(account_private)
account_address = account["address"]["testnet"]
account_live_cells = wallet_get_live_cells(account_address, api_url=api_url)
input_cell_lock = account["lock_arg"]
assert len(account_live_cells["live_cells"]) > 0
input_cell_out_points = []
input_cell_cap = 0
Expand Down Expand Up @@ -213,7 +214,7 @@ def invoke_ckb_contract(
"lock": {
"code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
"hash_type": "type",
"args": output_lock_arg,
"args": input_cell_lock,
},
}
)
Expand Down
2 changes: 1 addition & 1 deletion framework/test_fiber.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def start(self, node=None):
for key in env_map:
print(f"{key}={env_map[key]}")
run_command(
f"RUST_LOG=info,fnn=info {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 &",
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
Expand Down
93 changes: 93 additions & 0 deletions test_cases/fiber/devnet/test_fiber_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,96 @@ def test_ckb(self):
print("before_balance2:", before_balance2)
print("after_balance1:", after_balance1)
print("after_balance2:", after_balance2)

def test_new_fiber_test(self):
"""
多跳
1-2 100 ckb
2-3 100 ckb
1->3 转钱
1. 启动1个fiber3
2. 连接fiber3 和fiber2
3. 1 openchan 2
4 2 open channel 3
5. node3 new invoice
6. node1 transfer node3
8. query 是否成功
9 shut down
Returns:
"""
# 1. 启动1个fiber3
account3_privake_key = self.generate_account(1000)
fiber3 = self.start_new_fiber(account3_privake_key)

# 2. 连接fiber3 和fiber2
fiber3.connect_peer(self.fiber2)
time.sleep(3)
# 3. 1 openchan 2
self.fiber1.get_client().open_channel(
{
"peer_id": self.fiber2.get_peer_id(),
"funding_amount": hex(200 * 100000000),
"public": True,
}
)
self.wait_for_channel_state(
self.fiber1.get_client(), self.fiber2.get_peer_id(), "CHANNEL_READY", 120
)
# 4 2 open channel 3
self.fiber2.get_client().open_channel(
{
"peer_id": fiber3.get_peer_id(),
"funding_amount": hex(200 * 100000000),
"public": True,
}
)
self.wait_for_channel_state(
self.fiber2.get_client(), fiber3.get_peer_id(), "CHANNEL_READY", 120
)
# 5. node3 new invoice
payment_preimage = self.generate_random_preimage()
invoice_balance = 100 * 100000000
invoice = fiber3.get_client().new_invoice(
{
"amount": hex(invoice_balance),
"currency": "Fibb",
"description": "test invoice generated by node2",
"expiry": "0xe10",
"final_cltv": "0x28",
"payment_preimage": payment_preimage,
"hash_algorithm": "sha256",
}
)

# 6. node1 transfer node3
payment = self.fiber1.get_client().send_payment(
{
"invoice": invoice["invoice_address"],
}
)
# 8. query 是否成功
self.wait_payment_state(self.fiber1, payment["payment_hash"], "Success")
# todo fiber3 query payment
# self.wait_payment_state(fiber3, payment['payment_hash'], "Success")

# check account
channelsN12 = self.fiber1.get_client().list_channels({})
channelsN32 = fiber3.get_client().list_channels({})
# 9 shut down

def test_faucet(self):
account_private_key = self.generate_account(0)
self.faucet(
account_private_key, 1000, self.fiber2.account_private, 1000 * 10000000
)

ret = self.udtContract.list_cell(
self.node.getClient(),
self.get_account_script(self.fiber2.account_private)["args"],
self.get_account_script(account_private_key)["args"],
)

assert ret[0]["balance"] == 10000000000
assert ret[0]["ckb"] == 50000000000

0 comments on commit 8e146ac

Please sign in to comment.