1.fabric链码版本区别
Fabric链码分了两个大版本,1.x和2.x版本,两者主要区别为:
1、导入包的不同
1.x导入的包为:
"[github.com/hyperledger/fabric/core/chaincode/shim](http://github.com/hyperledger/fabric/core/chaincode/shim)"
pb "[github.com/hyperledger/fabric/protos/peer](http://github.com/hyperledger/fabric/protos/peer)"
2.0导入的包为:
"[github.com/hyperledger/fabric-contract-api-go/contractapi](http://github.com/hyperledger/fabric-contract-api-go/contractapi)"
2、方法结构不同
Fabric 2.0链码不需要 Invoke 和Init 方法。
3、方法中调用形式参数类型、返回值不同
1.x方法为:
createCar1(stub shim.ChaincodeStubInterface, args []string) pb.Response { }
2.0方法为:
Create(ctx contractapi.TransactionContextInterface,key string,value string)error { }
freerent链码还没开始编写,该篇文章主要对链码编写的主要思路和部分API进行进行梳理。
2. contractapi包简单分析
从官方的fabirc-samples提供各种简单的Go链码可以看到,一般我们链码方法都是自定义的一个SmartContract
struct,里面包含了contractapi包中的Contract
结构体。Contract
struct实现了ContractInterface
接口,自定义的结构体通过水平组合