Skip to content

Commit 3695461

Browse files
authored
Merge pull request #87 from sunchengzhu/main
add cases for ckb-cli 1.13.0
2 parents 76cf511 + 7eca748 commit 3695461

File tree

3 files changed

+169
-1
lines changed

3 files changed

+169
-1
lines changed

framework/helper/ckb_cli.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,11 @@ def version():
174174
:return:
175175
"""
176176
cmd = "{ckb_cli} --version".format(ckb_cli=cli_path)
177-
return run_command(cmd)
177+
output = run_command(cmd)
178+
print("\n=============== CKB-CLI Version ===============")
179+
print(output.strip())
180+
print("===============================================\n")
181+
return output
178182

179183

180184
def deploy_gen_txs(
@@ -1082,3 +1086,29 @@ def clear_tx_verify_queue(
10821086

10831087
cmd = f"export API_URL={api_url} && {cli_path} {cmd}"
10841088
return json.loads(run_command(cmd))
1089+
1090+
1091+
def get_transaction(
1092+
hash_value,
1093+
raw_data=False,
1094+
no_color=False,
1095+
debug=False,
1096+
local_only=False,
1097+
api_url="http://127.0.0.1:8114",
1098+
):
1099+
cmd = "rpc get_transaction"
1100+
if raw_data:
1101+
cmd += " --raw-data"
1102+
if no_color:
1103+
cmd += " --no-color"
1104+
if debug:
1105+
cmd += " --debug"
1106+
if local_only:
1107+
cmd += " --local-only"
1108+
cmd += f" --hash {hash_value}"
1109+
cmd += f" --output-format yaml"
1110+
1111+
cmd = f"export API_URL={api_url} && {cli_path} {cmd}"
1112+
yaml_data = yaml.safe_dump(yaml.safe_load(run_command(cmd)))
1113+
parsed_data = yaml.safe_load(yaml_data)
1114+
return parsed_data

framework/test_node.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,13 @@ def restart(self, config={}, clean_data=False):
268268
self.start()
269269

270270
def start(self):
271+
version = run_command(
272+
"cd {ckb_dir} && ./ckb --version".format(ckb_dir=self.ckb_dir)
273+
)
274+
print("\n================= CKB Version =================")
275+
print(version.strip())
276+
print("===============================================\n")
277+
271278
self.ckb_pid = run_command(
272279
"cd {ckb_dir} && ./ckb run --indexer --skip-spec-check > node.log 2>&1 &".format(
273280
ckb_dir=self.ckb_dir
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import json
2+
3+
from framework.basic import CkbTest
4+
from framework.rpc import RPCClient
5+
6+
7+
class TestCkbCliRpc200(CkbTest):
8+
@classmethod
9+
def setup_class(cls):
10+
"""
11+
1. start 1 ckb node in tmp/ckb_cli/node dir
12+
2. miner 2 block
13+
Returns:
14+
15+
"""
16+
# 1. start 1 ckb node in tmp/ckb_cli/node dir
17+
cls.node = cls.CkbNode.init_dev_by_port(
18+
cls.CkbNodeConfigPath.CURRENT_TEST, "ckb_cli/node", 8314, 8315
19+
)
20+
cls.node.prepare()
21+
cls.node.start()
22+
# 2. miner 2 block
23+
cls.Miner.make_tip_height_number(cls.node, 2)
24+
25+
@classmethod
26+
def teardown_class(cls):
27+
"""
28+
1. stop ckb node
29+
2. clean ckb node tmp dir
30+
Returns:
31+
32+
"""
33+
print("stop node and clean")
34+
cls.node.stop()
35+
cls.node.clean()
36+
37+
def test_01_get_transaction(self):
38+
"""
39+
1.use ckb-cli to get transaction by its hash
40+
2.use rpc to get transaction by its hash
41+
3.compare ckb-cli fee == rpc fee
42+
Returns:
43+
44+
"""
45+
self.Ckb_cli.version()
46+
47+
# 1. generate account and build normal tx
48+
account = self.Ckb_cli.util_key_info_by_private_key(
49+
self.Config.ACCOUNT_PRIVATE_1
50+
)
51+
father_tx_hash = self.Ckb_cli.wallet_transfer_by_private_key(
52+
self.Config.ACCOUNT_PRIVATE_1,
53+
account["address"]["testnet"],
54+
100000,
55+
self.node.getClient().url,
56+
"1500000",
57+
)
58+
59+
tx = self.Tx.build_send_transfer_self_tx_with_input(
60+
[father_tx_hash],
61+
["0x0"],
62+
self.Config.ACCOUNT_PRIVATE_1,
63+
output_count=15,
64+
fee=15000,
65+
api_url=self.node.getClient().url,
66+
)
67+
# 2. send the normal tx
68+
tx_hash = self.node.getClient().send_transaction(tx)
69+
70+
tx_info = self.Ckb_cli.get_transaction(
71+
tx_hash, api_url=self.node.getClient().url
72+
)
73+
74+
print("\n=========== Transaction Information ===========")
75+
print("Fee: ", tx_info.get("fee"))
76+
print("Min Replace Fee: ", tx_info.get("min_replace_fee"))
77+
print("Time Added to Pool: ", tx_info.get("time_added_to_pool"))
78+
print("===============================================\n")
79+
80+
rpc_tx_info = self.node.getClient().get_transaction(tx_hash)
81+
fee_int = int(rpc_tx_info.get("fee", "0x0"), 16)
82+
min_replace_fee_int = int(rpc_tx_info.get("min_replace_fee", "0x0"), 16)
83+
84+
fee = f"{fee_int / 100_000_000:.8f}".rstrip("0").rstrip(".")
85+
min_replace_fee = f"{min_replace_fee_int / 100_000_000:.8f}".rstrip("0").rstrip(
86+
"."
87+
)
88+
time_added_to_pool = int(rpc_tx_info.get("time_added_to_pool", "0x0"), 16)
89+
90+
assert tx_info.get("fee") == fee
91+
assert tx_info.get("min_replace_fee") == min_replace_fee
92+
assert tx_info.get("time_added_to_pool") == time_added_to_pool
93+
94+
self.Miner.miner_until_tx_committed(self.node, tx_hash, 1000)
95+
96+
def test_02_tx_add_input_skip_check(self):
97+
tmp_tx_file = "/tmp/skip_check.json"
98+
# joyid lock script
99+
address = "ckt1qrfrwcdnvssswdwpn3s9v8fp87emat306ctjwsm3nmlkjg8qyza2cqgqq9cd7mgucy3fgs6et4j7cc2w6lsuulajhy3k08sj"
100+
# 1. generate account and build normal tx
101+
tx_hash = self.Ckb_cli.wallet_transfer_by_private_key(
102+
self.Config.ACCOUNT_PRIVATE_1,
103+
address,
104+
100000,
105+
self.node.getClient().url,
106+
"1500000",
107+
)
108+
109+
tx = self.Ckb_cli.get_transaction(tx_hash, api_url=self.node.getClient().url)
110+
code_hash_int = tx["transaction"]["outputs"][0]["lock"]["code_hash"]
111+
code_hash = hex(code_hash_int)
112+
assert (
113+
code_hash
114+
== "0xd23761b364210735c19c60561d213fb3beae2fd6172743719eff6920e020baac"
115+
)
116+
117+
self.Ckb_cli.tx_init(tmp_tx_file, self.node.getClient().url)
118+
self.Ckb_cli.tx_add_input(tx_hash, 0, tmp_tx_file, self.node.getClient().url)
119+
120+
with open(tmp_tx_file, "r") as f:
121+
data = json.load(f)
122+
123+
inputs = data.get("transaction", {}).get("inputs", [])
124+
if inputs:
125+
previous_output = inputs[0].get("previous_output", {})
126+
print("previous_output.tx_hash:", previous_output.get("tx_hash"))
127+
print("previous_output.index:", previous_output.get("index"))
128+
assert previous_output.get("tx_hash") == tx_hash
129+
assert previous_output.get("index") == "0x0"
130+
else:
131+
assert False, "No inputs found in the tx_file."

0 commit comments

Comments
 (0)