Skip to content

Commit

Permalink
Merge pull request #76 from cryptape/ckb-lua
Browse files Browse the repository at this point in the history
add lua
  • Loading branch information
gpBlockchain authored Nov 21, 2024
2 parents 2d8bce3 + bcb182d commit eae6ac7
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 1 deletion.
2 changes: 1 addition & 1 deletion framework/helper/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@ def invoke_ckb_contract(
tmp_tx_file,
)
# 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
Binary file added source/contract/lua_vm/lua-loader
Binary file not shown.
31 changes: 31 additions & 0 deletions source/contract/lua_vm/spawn.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
local callee_code = [[
local m = arg[2] .. arg[3]
local inherited_fds, err = ckb.inherited_fds()
local n, err = ckb.write(inherited_fds[1], m)
ckb.close(inherited_fds[1])
assert(n == 10)
local pid = ckb.process_id()
assert(pid >= 1)
]]

local fds, err = ckb.pipe()
assert(err == nil)
local pid, err = ckb.spawn(0, ckb.SOURCE_CELL_DEP, 0, 0, {"-e", callee_code, "hello", "world"}, {fds[2]})
assert(err == nil)
local ret = ckb.read(fds[1], 10)
assert(ret == "helloworld")
ckb.wait(pid)

local code_hash, err = ckb.load_cell_by_field(0, ckb.SOURCE_CELL_DEP, ckb.CELL_FIELD_DATA_HASH)
assert(err == nil)
local fds, err = ckb.pipe()
assert(err == nil)
local pid, err = ckb.spawn_cell(code_hash, 4, 0, 0, {"-e", callee_code, "hello", "world"}, {fds[2]})
assert(err == nil)
local ret = ckb.read(fds[1], 10)
assert(ret == "helloworld")
ckb.wait(pid)

local buf,error = ckb.load_block_extension(0, ckb.SOURCE_INPUT)
assert(not error)
assert(#buf == 32)
100 changes: 100 additions & 0 deletions test_cases/ckb2023/test_07_ckb_lua_vm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
from framework.basic import CkbTest
from framework.util import get_project_root


class TestCkbLuaVm(CkbTest):
cluster: CkbTest.Cluster
ckb_js_vm_deploy_hash: str
ckb_js_vm_codeHash: str

@classmethod
def setup_class(cls):
"""
1. star 4 node in tmp/cluster/hardFork dir
2. link ckb node each other
3. deploy contract
4. miner 1000 block
5. deploy ckb-lua-vm
Returns:
"""

# 1. star 4 node in tmp/cluster/hardFork dir
nodes = [
cls.CkbNode.init_dev_by_port(
cls.CkbNodeConfigPath.CURRENT_TEST,
"cluster1/hardFork/node{i}".format(i=i),
8114 + i,
8225 + i,
)
for i in range(1, 5)
]
cls.cluster = cls.Cluster(nodes)
cls.cluster.prepare_all_nodes()
cls.cluster.start_all_nodes()

# 2. link ckb node each other
cls.cluster.connected_all_nodes()

# 4. miner 1000 block
cls.Miner.make_tip_height_number(cls.cluster.ckb_nodes[0], 1000)
cls.Node.wait_cluster_height(cls.cluster, 1000, 100)

# 5. deploy ckb-js-vm
cls.ckb_js_vm_deploy_hash = cls.Contract.deploy_ckb_contract(
cls.Config.MINER_PRIVATE_1,
f"{get_project_root()}/source/contract/lua_vm/lua-loader",
enable_type_id=False,
api_url=cls.cluster.ckb_nodes[0].getClient().url,
)
cls.Miner.miner_until_tx_committed(
cls.cluster.ckb_nodes[0], cls.ckb_js_vm_deploy_hash, with_unknown=True
)
ckb_js_vm_codeHash = cls.Contract.get_ckb_contract_codehash(
cls.ckb_js_vm_deploy_hash,
0,
False,
cls.cluster.ckb_nodes[0].getClient().url,
)
print("ckb_js_vm_codeHash:", ckb_js_vm_codeHash)

@classmethod
def teardown_class(cls):
print("\nTeardown TestClass1")
cls.cluster.stop_all_nodes()
cls.cluster.clean_all_nodes()

def test_01_deploy(self):
invoke_hash = self.deploy_lua_code(
self.Config.ACCOUNT_PRIVATE_1,
f"{get_project_root()}/source/contract/lua_vm/spawn.lua",
)
self.Miner.miner_until_tx_committed(
self.cluster.ckb_nodes[0], invoke_hash, with_unknown=True
)

def deploy_lua_code(self, account, code_path):
js_code_deploy_hash = self.Contract.deploy_ckb_contract(
account,
code_path,
enable_type_id=True,
api_url=self.cluster.ckb_nodes[0].getClient().url,
)
self.Miner.miner_until_tx_committed(
self.cluster.ckb_nodes[0], js_code_deploy_hash, with_unknown=True
)
js_code_hash = self.Contract.get_ckb_contract_codehash(
js_code_deploy_hash, 0, True, self.cluster.ckb_nodes[0].getClient().url
)

js_code_hash = js_code_hash.replace("0x", "")
return self.Contract.invoke_ckb_contract(
account_private=account,
contract_out_point_tx_hash=self.ckb_js_vm_deploy_hash,
contract_out_point_tx_index=0,
type_script_arg=f"0x0000{js_code_hash}01",
data="0x",
hash_type="data2",
api_url=self.cluster.ckb_nodes[0].getClient().url,
cell_deps=[{"tx_hash": js_code_deploy_hash, "index": "0x0"}],
)

0 comments on commit eae6ac7

Please sign in to comment.