Skip to content

Commit eae6ac7

Browse files
authored
Merge pull request #76 from cryptape/ckb-lua
add lua
2 parents 2d8bce3 + bcb182d commit eae6ac7

File tree

4 files changed

+132
-1
lines changed

4 files changed

+132
-1
lines changed

framework/helper/contract.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,9 @@ def invoke_ckb_contract(
235235
tmp_tx_file,
236236
)
237237
# add dep
238-
tx_add_cell_dep(cell_dep["tx_hash"], cell_dep["index"], tmp_tx_file)
239238
for cell_dep_tmp in cell_deps:
240239
tx_add_cell_dep(cell_dep_tmp["tx_hash"], cell_dep_tmp["index"], tmp_tx_file)
240+
tx_add_cell_dep(cell_dep["tx_hash"], cell_dep["index"], tmp_tx_file)
241241
# sign
242242
sign_data = tx_sign_inputs(account_private, tmp_tx_file, api_url)
243243
tx_add_signature(

source/contract/lua_vm/lua-loader

233 KB
Binary file not shown.

source/contract/lua_vm/spawn.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
local callee_code = [[
2+
local m = arg[2] .. arg[3]
3+
local inherited_fds, err = ckb.inherited_fds()
4+
local n, err = ckb.write(inherited_fds[1], m)
5+
ckb.close(inherited_fds[1])
6+
assert(n == 10)
7+
local pid = ckb.process_id()
8+
assert(pid >= 1)
9+
]]
10+
11+
local fds, err = ckb.pipe()
12+
assert(err == nil)
13+
local pid, err = ckb.spawn(0, ckb.SOURCE_CELL_DEP, 0, 0, {"-e", callee_code, "hello", "world"}, {fds[2]})
14+
assert(err == nil)
15+
local ret = ckb.read(fds[1], 10)
16+
assert(ret == "helloworld")
17+
ckb.wait(pid)
18+
19+
local code_hash, err = ckb.load_cell_by_field(0, ckb.SOURCE_CELL_DEP, ckb.CELL_FIELD_DATA_HASH)
20+
assert(err == nil)
21+
local fds, err = ckb.pipe()
22+
assert(err == nil)
23+
local pid, err = ckb.spawn_cell(code_hash, 4, 0, 0, {"-e", callee_code, "hello", "world"}, {fds[2]})
24+
assert(err == nil)
25+
local ret = ckb.read(fds[1], 10)
26+
assert(ret == "helloworld")
27+
ckb.wait(pid)
28+
29+
local buf,error = ckb.load_block_extension(0, ckb.SOURCE_INPUT)
30+
assert(not error)
31+
assert(#buf == 32)
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)