简单的资产Chaincode
应用程序是一个基本样本链代码,用于在分类账上创建资产
安装Go及Docker, Docker-compose, 并配置Go相应的环境变量
创建目录
为chaincode应用程序创建一个目录作为其子目录
$ mkdir -p $GOPATH/src/test
$ cd $GOPATH/src/test
新建文件
新建一个文件,用于编写Go代码
$ touch test.go
$ vim test.go
编写代码
必须实现 Chaincode接口的Init
和Invoke
函数。因此,须在文件中添加go import语句以获取链代码的依赖。
导入chaincode shim包和 peer protobuf包。然后添加一个结构SimpleChaincdoe
作为Chaincode函数的接收器
package main
import (
"fmt"
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/protos/peer"
)
type SimpleChaincdoe struct {
}
初始化Chaincode
Init方法
- 获取参数, 使用GetStringArgs 函数检索调用 的参数
- 检查合法性, 检查参数数量是否为2个, 如果不是, 则返回错误信息
- 利用两参数, 调用PutState方法向账本中写入状态, 如果有错误则返回(shim.Error()), 否则返回nil(shim.Success)
func (t *SimpleChaincdoe) Init(stub shim.ChaincodeStubInterface) peer.Response {
}
调用实现
Invoke方法
验证函数名称为set
或get
,并调用那些链式代码应用程序