Hyperledger Fabric如何实现第一个链码helloworld
1. 创建链码
首先,需要创建一个名为helloworld的链码,用于实现第一个helloworld示例。链码可以使用任何开发语言编写,如Go、JavaScript、Java等,本文以Go语言为例,编写如下helloworld.go文件:
package main
import (
"fmt"
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/protos/peer"
)
// HelloWorldChaincode example simple Chaincode implementation
type HelloWorldChaincode struct {
}
// Init is a no-op
func (t *HelloWorldChaincode) Init(stub shim.ChaincodeStubInterface) peer.Response {
return shim.Success(nil)
}
// Invoke is a no-op
func (t *HelloWorldChaincode) Invoke(stub shim.ChaincodeStubInterface) peer.Response {
return shim.Success(nil)
}
func main() {
err := shim.Start(new(HelloWorldChaincode))
if err != nil {
fmt.Printf("Error starting HelloWorldChaincode: %s", err)
}
}2. 编译链码
接着,需要使用Hyperledger Fabric的chaincode构建工具编译helloworld.go文件,生成helloworld.go文件,命令如下:
docker exec cli peer chaincode package helloworld.tar.gz --lang go --label helloworld_1.0 --path /opt/gopath/src/github.com/chaincode/helloworld/
3. 安装链码
安装链码,命令如下:
docker exec cli peer chaincode install helloworld.tar.gz
docker exec cli peer chaincode instantiate -n helloworld -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -C mychannel
docker exec cli peer chaincode invoke -n helloworld -c '{"Args":["invoke","a","b","10"]}' -C mychannel
docker exec cli peer chaincode query -n helloworld -c '{"Args":["query","a"]}' -C mychannel
4. 查看helloworld结果
最后,就可以查看helloworld的结果了,由于上面的操作中a的值被更新为90,因此查询结果应该为90。
猜您想看
-
tabix中怎么操作VCF文件
VCF文件介绍...
2023年07月23日 -
如何自定义快捷指令图标?
如何自定义快捷...
2023年04月17日 -
最全的网易云音乐使用攻略,让你的听歌更加更加懂音乐
一、网易云音乐...
2023年05月15日 -
怎么用Dreamweaver实现网页上漫天花雨效果
一、什么是漫天...
2023年05月26日 -
fist是什么工具
是什么工具?是...
2023年07月20日 -
RocketMQ中Client端架构是怎么样的
,不要使用ma...
2023年05月25日