Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
gpBlockchain committed Nov 15, 2024
1 parent abb6152 commit af653a4
Show file tree
Hide file tree
Showing 17 changed files with 799 additions and 17 deletions.
32 changes: 29 additions & 3 deletions framework/basic_fiber.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def setup_class(cls):
cls.account2_private_key
)
cls.node = cls.CkbNode.init_dev_by_port(
cls.CkbNodeConfigPath.CURRENT_FIBER, "contract/node", 8114, 8115
cls.CkbNodeConfigPath.CURRENT_FIBER, "contract/node", 8114, 8125
)

if cls.debug:
Expand Down Expand Up @@ -87,7 +87,7 @@ def setup_method(cls, method):
"transactions"
][0]["hash"]
#
cls.udtContract = UdtContract(xudt_contract_hash, 10)
cls.udtContract = UdtContract(xudt_contract_hash, 9)
#
deploy_hash, deploy_index = cls.udtContract.get_deploy_hash_and_index()

Expand Down Expand Up @@ -245,7 +245,12 @@ def start_new_fiber(self, account_private_key, config=None):
return fiber

def wait_for_channel_state(self, client, peer_id, expected_state, timeout=120):
"""Wait for a channel to reach a specific state."""
"""Wait for a channel to reach a specific state.
1. NEGOTIATING_FUNDING
2. CHANNEL_READY
3. Closed
"""
for _ in range(timeout):
channels = client.list_channels({"peer_id": peer_id})
if len(channels["channels"]) == 0:
Expand Down Expand Up @@ -288,6 +293,27 @@ def wait_payment_state(self, client, payment_hash, status="Success", timeout=120
f"status did not reach state {expected_state} within timeout period."
)

def wait_invoice_state(
self, client, payment_hash, status="Success", timeout=120, interval=1
):
"""
status:
1. 状态为Open
2. 状态为Cancelled
3. 状态为Expired
4. 状态为Received
5. 状态为Paid
"""
for i in range(timeout):
result = client.get_client().get_invoice({"payment_hash": payment_hash})
if result["status"] == status:
return
time.sleep(interval)
raise TimeoutError(
f"status did not reach state {expected_state} within timeout period."
)

def get_fiber_env(self, new_fiber_count=0):
# print ckb tip number
for i in range(new_fiber_count):
Expand Down
20 changes: 20 additions & 0 deletions framework/fiber_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ def parse_invoice(self, param):
def connect_peer(self, param):
return self.call("connect_peer", [param])

def cancel_invoice(self, param):
return self.call("cancel_invoice", [param])

def get_invoice(self, param):
"""
curl --location 'http://127.0.0.1:8228' --header 'Content-Type: application/json' --data '{
"id": 42,
"jsonrpc": "2.0",
"method": "get_invoice",
"params": [
{
"payment_hash": "0x08f64fbcb38189aedb109ff049b6c8a20ba9b61a836fef35a6699c2004fe6902"
}
]
}'
response:
{"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}
"""
return self.call("get_invoice", [param])

def disconnect_peer(self, param):
return self.call("disconnect_peer", [param])

Expand Down
11 changes: 6 additions & 5 deletions framework/helper/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,12 @@ def invoke_ckb_contract(
hex(input_cells[i]["index"]), input_cells[i]["tx_hash"], True
)
input_cell_cap += int(cell["cell"]["output"]["capacity"], 16)
input_cells_hashs = [input_cell["tx_hash"] for input_cell in input_cells]
input_cells_hashes = [input_cell["tx_hash"] for input_cell in input_cells]

for i in range(len(account_live_cells["live_cells"])):
if account_live_cells["live_cells"][i]["tx_hash"] in input_cells_hashs:
if account_live_cells["live_cells"][i]["tx_hash"] in input_cells_hashes:
continue
if input_cell_cap > 10000000000:
if input_cell_cap > 20000000000:
break
input_cell_out_point = {
"tx_hash": account_live_cells["live_cells"][i]["tx_hash"],
Expand All @@ -183,7 +183,7 @@ def invoke_ckb_contract(
)

input_cell_out_points.append(input_cell_out_point)
if input_cell_cap > 10000000000:
if input_cell_cap > 20000000000:
break

# get output_cells.cap = input_cell.cap - fee
Expand Down Expand Up @@ -271,9 +271,10 @@ def invoke_ckb_contract(
)
data = "0x"
# add dep
tx_add_cell_dep(cell_dep["tx_hash"], cell_dep["index"], tmp_tx_file)
for cell_dep_tmp in cell_deps:
tx_add_cell_dep(cell_dep_tmp["tx_hash"], cell_dep_tmp["index"], tmp_tx_file)
tx_add_cell_dep(cell_dep["tx_hash"], cell_dep["index"], tmp_tx_file)

# sign
sign_data = tx_sign_inputs(account_private, tmp_tx_file, api_url)
tx_add_signature(
Expand Down
22 changes: 14 additions & 8 deletions framework/test_fiber.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ def prepare(self, update_config=None):
self.fiber_config_enum.fiber_config_path,
self.fiber_config_path,
)
shutil.copy(
"{root_path}/source/template/ckb/fiber/dev.toml".format(
root_path=get_project_root()
),
self.tmp_path,
)
target_dir = os.path.join(self.tmp_path, "ckb")
os.makedirs(target_dir, exist_ok=True) # 创建文件夹,如果已存在则不报错
with open(f"{self.tmp_path}/ckb/key", "w") as f:
Expand Down Expand Up @@ -132,15 +138,15 @@ def read_ckb_key(self):
return self.account_private

def start(self, node=None):
env_map = dict(os.environ) # Make a copy of the current environment
if node:
contract_map = self.get_contract_env_map(node)
env_map.update(contract_map)
for key in env_map:
print(f"{key}={env_map[key]}")
# env_map = dict(os.environ) # Make a copy of the current environment
# if node:
# contract_map = self.get_contract_env_map(node)
# env_map.update(contract_map)
# for key in env_map:
# print(f"{key}={env_map[key]}")
run_command(
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,
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
time.sleep(2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


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

@pytest.mark.skip("repeat")
def test_shutdown_script_none(self):
Expand Down Expand Up @@ -300,3 +300,7 @@ def test_shutdown_script_too_big(self):
"fee_rate": "0x3FC",
}
)
time.sleep(10)
self.fiber1.get_client().list_channels({})
self.fiber2.get_client().list_channels({})
# todo close 失败,需要能查到channels
Loading

0 comments on commit af653a4

Please sign in to comment.