|
| 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