虽然 Fabric v2.2 已经发布了很久了,但之前因为项目历史问题,一直使用的都是 Fabric v1.4.8,所以智能合约也一直使用的都是 github.com/hyperledger/fabric/core/chaincode/shim 包。  
在合约开发过程中,我一般都是使用下面的接口格式来定义合约的业务逻辑:
func create(stub shim.ChaincodeStubInterface, payload string) ([]byte, error)在开发 Fabric v2.2 的智能合约时, 使用 github.com/hyperledger/fabric-contract-api-go/contractapi 替换 github.com/hyperledger/fabric/core/chaincode/shim,接口格式如下:  
func create(ctx contractapi.TransactionContextInterface, payload string) ([]byte, error)然而这样的接口在合约示例化的时候翻车了:
Error compiling schema for SmartContract [create]. Return schema invalid. required items must be unique [recovered]翻阅 github.com/hyperledger/fabric-contract-api-go 时,在其使用教程发现一些限制:  
仔细阅读会发现 func create(ctx contractapi.TransactionContextInterface, payload string) ([]byte, error) 并没有违法上面的规则,但示例化的时候就是无法通过。  
上面的报错信息也明确说了是返回值不对,那就改下接口的返回值:
func create(ctx contractapi.TransactionContextInterface, payload string) (string, error)
func create(ctx contractapi.TransactionContextInterface, payload string) (*Company, error)
func create(ctx contractapi.TransactionContextInterface, payload string) (int, error)修改后在进行实例化,这次不再报错了。
但是明明之前的也没有违反规则,为什么会报错呢?想不通为什么,所以准备给官方提个Issue,万一真是个bug呢?
结果就在issues里发现了这个Possible issues with byte[] as return type,一看日期Oct 20, 2021,快两年了也没官方的回应......
最后搜了一圈也没找到原因,查看源码,感觉问题可能是出在 contractFunctionReturns,具体还得等研究完源码之后才能有答案
type contractChaincodeContract struct {
	info                      metadata.InfoMetadata
	functions                 map[string]*internal.ContractFunction
	unknownTransaction        *internal.TransactionHandler
	beforeTransaction         *internal.TransactionHandler
	afterTransaction          *internal.TransactionHandler
	transactionContextHandler reflect.Type
}
// ContractChaincode a struct to meet the chaincode interface and provide routing of calls to contracts
type ContractChaincode struct {
	DefaultContract       string
	contracts             map[string]contractChaincodeContract
	metadata              metadata.ContractChaincodeMetadata
	Info                  metadata.InfoMetadata
	TransactionSerializer serializer.TransactionSerializer
}
// ContractFunction contains a description of a function so that it can be called by a chaincode
type ContractFunction struct {
	function reflect.Value
	callType CallType
	params   contractFunctionParams
	returns  contractFunctionReturns
}
type contractFunctionReturns struct {
	success reflect.Type
	error   bool
}我正在参与2023腾讯技术创作特训营第三期有奖征文,组队打卡瓜分大奖!
声明:本作品采用署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)进行许可,使用时请注明出处。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。