Skip to content

GitLab

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
  • Sign in
issue
issue
  • Project overview
    • Project overview
    • Details
    • Activity
    • Releases
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 23
    • Issues 23
    • List
    • Boards
    • Labels
    • Service Desk
    • Milestones
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Members
    • Members
  • Activity
  • Graph
  • Create a new issue
  • Commits
  • Issue Boards
Collapse sidebar

新注册的用户请输入邮箱并保存,随后登录邮箱激活账号。后续可直接使用邮箱登录!

  • chainmaker
  • issueissue
  • Issues
  • #1174

Closed
Open
Opened Sep 02, 2024 by yufei wang@feifei1 of 19 tasks completed1/19 tasks

长安链合约调用出现的问题,怎么完善代码

【问题分类】

  • bug
  • P2P网络相关(包含libp2p,liquid)
  • 链账户身份与权限相关(证书问题、public、多签投票问题)
  • 核心交易引擎相关(交易池、DAG)
  • 共识相关
  • 智能合约相关
  • 存储相关
  • SDK相关
  • 长安链CMC工具
  • 长安链管理台
  • 长安链浏览器
  • 长安链合约IDE
  • 长安链web签名插件
  • 跨链相关
  • 轻节点相关
  • 隐私计算相关
  • 密码学相关
  • 环境依赖
  • 其他补充:

【问题描述】(请对问题进行描述,方便定位问题)

./main.go:53:9: undefined: NewSimContext

./main.go:58:13: undefined: SUCCESS

./main.go:83:19: undefined: SUCCESS

./main.go:85:109: undefined: convert

具体的现象,操作步骤,前置条件等,尽可能描述清楚,有截图说明更佳 /* SPDX-License-Identifier: Apache-2.0 */

package main

import ( "chainmaker.org/chainmaker/contract-sdk-go/v2/pb/protogo" "chainmaker.org/chainmaker/contract-sdk-go/v2/sandbox" "chainmaker.org/chainmaker/contract-sdk-go/v2/sdk" "log" )

// 合约结构体 type FactContract struct { }

// 安装合约时会执行此方法,必须 //export init_contract func (f *FactContract) InitContract() protogo.Response { return sdk.Success([]byte("合约初始化成功")) }

// 升级合约时会执行此方法,必须 //export upgrade func (f *FactContract) UpgradeContract() protogo.Response { return sdk.Success([]byte("合约升级成功")) }

// 用于合约的调用 //export invoke_contract func (f *FactContract) InvokeContract(method string) protogo.Response { switch method { case "crossCallGoContract": return f.crossCallGoContract() default: return sdk.Error("invalid method") } }

// 存证对象结构体 type Fact struct { contractType string json:"contractType" methodName string json:"methodName" RoleList []string json:"roleList" Functions []string json:"functions" }

// 对外暴露 crossCallGoContract 方法,供用户由 SDK 调用 //export crossCallGoContract func (f *FactContract) crossCallGoContract() protogo.Response { // 获取上下文 ctx := NewSimContext() contractType, err1 := ctx.ArgString("contractType") methodName, err2 := ctx.ArgString("methodName") calldata, err3 := ctx.Arg("calldata")

if err1 != SUCCESS || err2 != SUCCESS || err3 != SUCCESS {
	ctx.Log("get arg fail.")
	return sdk.Error("get arg fail.")
}

// 根据 contractType 选择合约名称
var contractName string
switch contractType {
case "Merkle":
	contractName = "chaindid9294"
case "DID":
	contractName = "chaindid9293"
case "Acc":
	contractName = "chaindid9292"
default:
	ctx.Log("Invalid contract type: " + contractType)
	return sdk.Error("invalid contract type")
}

// 设置参数
params := make(map[string][]byte, 1)
params["data"] = calldata

// 执行跨合约调用
result, resultCode := ctx.CallContract(contractName, methodName, params)
if resultCode != SUCCESS {
	// 返回失败结果并记录日志
	ctx.Log("Failed to call contract: " + contractName + ", method: " + methodName + ", with result code: " + convert.Int32ToString(int32(resultCode)))
	return sdk.Error("failed to call contract: " + contractName)
}

// 返回成功结果并记录日志
ctx.Log("Successfully called contract: " + contractName + ", method: " + methodName + ", result: " + string(result))
return sdk.Success(result)

}

func main() { // main()方法中,下面的代码为必须代码,不建议修改main()方法当中的代码 // 其中,FactContract为用户实现合约的具体名称 err := sandbox.Start(new(FactContract)) // 使用 sandbox.Start() if err != nil { log.Fatal(err) } }

【相关日志文件】(如果有报错日志请贴图,或者上传附件)

chainmaker-go 的 log/system.log & bin/panic.log file *

SDK 的 sdk.log file

【系统信息】(请填写系统信息,方便定位问题)

  • chainmaker-go version * : [v2.3.0]
  • OS & version * :
  • docker镜像 version:
  • 管理台 version:
    • 如果是管理台的相关问题,请附带管理台版本,如果不是可忽略。
  • 区块浏览器 version:
    • 如果是浏览器的相关问题,请附带浏览器版本,如果不是可忽略。
  • 合约IDE version:
    • 如果是合约IDE的相关问题,请附带合约IDE版本,如果不是可忽略。
  • web签名插件 version:
    • 如果是web签名插件的相关问题,请附带web签名插件版本,如果不是可忽略。
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
None
Milestone
None
Assign milestone
Time tracking
None
Due date
None
Reference: chainmaker/issue#1174

Copyright © 2021 ChainMaker Org. All Rights Reserved. 长安链 版权所有。