|
| 1 | +from framework.basic import CkbTest |
| 2 | +from framework.util import get_project_root |
| 3 | + |
| 4 | + |
| 5 | +class TestCkbLuaVm(CkbTest): |
| 6 | + cluster: CkbTest.Cluster |
| 7 | + ckb_js_vm_deploy_hash: str |
| 8 | + ckb_js_vm_codeHash: str |
| 9 | + |
| 10 | + @classmethod |
| 11 | + def setup_class(cls): |
| 12 | + """ |
| 13 | + 1. star 4 node in tmp/cluster/hardFork dir |
| 14 | + 2. link ckb node each other |
| 15 | + 3. deploy contract |
| 16 | + 4. miner 1000 block |
| 17 | + 5. deploy ckb-lua-vm |
| 18 | + Returns: |
| 19 | +
|
| 20 | + """ |
| 21 | + |
| 22 | + # 1. star 4 node in tmp/cluster/hardFork dir |
| 23 | + nodes = [ |
| 24 | + cls.CkbNode.init_dev_by_port( |
| 25 | + cls.CkbNodeConfigPath.CURRENT_TEST, |
| 26 | + "cluster1/hardFork/node{i}".format(i=i), |
| 27 | + 8114 + i, |
| 28 | + 8225 + i, |
| 29 | + ) |
| 30 | + for i in range(1, 5) |
| 31 | + ] |
| 32 | + cls.cluster = cls.Cluster(nodes) |
| 33 | + cls.cluster.prepare_all_nodes() |
| 34 | + cls.cluster.start_all_nodes() |
| 35 | + |
| 36 | + # 2. link ckb node each other |
| 37 | + cls.cluster.connected_all_nodes() |
| 38 | + |
| 39 | + # 4. miner 1000 block |
| 40 | + cls.Miner.make_tip_height_number(cls.cluster.ckb_nodes[0], 1000) |
| 41 | + cls.Node.wait_cluster_height(cls.cluster, 1000, 100) |
| 42 | + |
| 43 | + # 5. deploy ckb-js-vm |
| 44 | + cls.ckb_js_vm_deploy_hash = cls.Contract.deploy_ckb_contract( |
| 45 | + cls.Config.MINER_PRIVATE_1, |
| 46 | + f"{get_project_root()}/source/contract/lua_vm/lua-loader", |
| 47 | + enable_type_id=False, |
| 48 | + api_url=cls.cluster.ckb_nodes[0].getClient().url, |
| 49 | + ) |
| 50 | + cls.Miner.miner_until_tx_committed( |
| 51 | + cls.cluster.ckb_nodes[0], cls.ckb_js_vm_deploy_hash, with_unknown=True |
| 52 | + ) |
| 53 | + ckb_js_vm_codeHash = cls.Contract.get_ckb_contract_codehash( |
| 54 | + cls.ckb_js_vm_deploy_hash, |
| 55 | + 0, |
| 56 | + False, |
| 57 | + cls.cluster.ckb_nodes[0].getClient().url, |
| 58 | + ) |
| 59 | + print("ckb_js_vm_codeHash:", ckb_js_vm_codeHash) |
| 60 | + |
| 61 | + @classmethod |
| 62 | + def teardown_class(cls): |
| 63 | + print("\nTeardown TestClass1") |
| 64 | + cls.cluster.stop_all_nodes() |
| 65 | + cls.cluster.clean_all_nodes() |
| 66 | + |
| 67 | + def test_01_deploy(self): |
| 68 | + invoke_hash = self.deploy_lua_code( |
| 69 | + self.Config.ACCOUNT_PRIVATE_1, |
| 70 | + f"{get_project_root()}/source/contract/lua_vm/spawn.lua", |
| 71 | + ) |
| 72 | + self.Miner.miner_until_tx_committed( |
| 73 | + self.cluster.ckb_nodes[0], invoke_hash, with_unknown=True |
| 74 | + ) |
| 75 | + |
| 76 | + def deploy_lua_code(self, account, code_path): |
| 77 | + js_code_deploy_hash = self.Contract.deploy_ckb_contract( |
| 78 | + account, |
| 79 | + code_path, |
| 80 | + enable_type_id=True, |
| 81 | + api_url=self.cluster.ckb_nodes[0].getClient().url, |
| 82 | + ) |
| 83 | + self.Miner.miner_until_tx_committed( |
| 84 | + self.cluster.ckb_nodes[0], js_code_deploy_hash, with_unknown=True |
| 85 | + ) |
| 86 | + js_code_hash = self.Contract.get_ckb_contract_codehash( |
| 87 | + js_code_deploy_hash, 0, True, self.cluster.ckb_nodes[0].getClient().url |
| 88 | + ) |
| 89 | + |
| 90 | + js_code_hash = js_code_hash.replace("0x", "") |
| 91 | + return self.Contract.invoke_ckb_contract( |
| 92 | + account_private=account, |
| 93 | + contract_out_point_tx_hash=self.ckb_js_vm_deploy_hash, |
| 94 | + contract_out_point_tx_index=0, |
| 95 | + type_script_arg=f"0x0000{js_code_hash}01", |
| 96 | + data="0x", |
| 97 | + hash_type="data2", |
| 98 | + api_url=self.cluster.ckb_nodes[0].getClient().url, |
| 99 | + cell_deps=[{"tx_hash": js_code_deploy_hash, "index": "0x0"}], |
| 100 | + ) |
0 commit comments