fabric2.2学习笔记1
fabric2.2学习笔记1
20201303张奕博 2023年1月9日
hyperledger fabric 结构分析
每个Server作用:
AdminServer:控制该节点的命运,可以删除该节点所在的进程。(Start Stop GetStatus )
EventHubServer:Peer节点支持客户端对指定事件进行监听,例如Rejection等。客户端需要先注册自己关心的Events,当事件发生时trigger 监听者。
OpenChainServer:对外提供ledger的访问接口,涉及GetBlockchainInfo GetBlockByNumber等。
DevopsServer:负责与CLI Client对接,外部进行CC操作的入口,Deploy invoke query。
ChaincodeSupportServer:负责与shim/Chaincode通信,ChainCode的所有调用接收发送都要与该Server信息交互。
PeerServer:该Server是一个Engine,Engine关联了内部消息响应实现,同时为周围Peer节点创建Client与之通信。
RESTServer:该Server没有进行分析,应该是REST接口格式相关。
fabric智能合约分析
部分代码截图:
package main
import (
"encoding/json"
"fmt"
"strconv"
"github.com/hyperledger/fabric-chaincode-go/shim"
"github.com/hyperledger/fabric-protos-go/peer"
)
type Trace struct {
}
type CropUpload struct {
CropID string
Timestamp string
Healthy string
Pic string
Action string
Name string
}
type ProcessUpload struct {
ProductID string
Timestamp string
Pic string
Process string
Name string
}
type BindUpload struct {
ProductID string
CropID string
Timestamp string
Name string
}
type TransportUpload struct {
ProductID string
Src string
Dst string
State string
Timestamp string
Name string
}
type SaleUpload struct {
ProductID string
Timestamp string
ConsumerName string
Name string
}
func (t *Trace) Init(stub shim.ChaincodeStubInterface) peer.Response {
return shim.Success(nil)
}
func (t *Trace) Invoke(stub shim.ChaincodeStubInterface) peer.Response {
function, args := stub.GetFunctionAndParameters()
switch function {
case "crop_upload":
return t.recordCrops(stub, args)
case "processing_upload":
return t.recordProcess(stub, args)
case "crop_product_bind":
return t.recordBind(stub, args)
case "transport_upload":
return t.recordTransport(stub, args)
case "sale_upload":
return t.recordSale(stub, args)
case "query":
return t.query(stub, args)
}
return shim.Error("Invalid invoke function name. " + function)
}
func (t *Trace) recordCrops(stub shim.ChaincodeStubInterface, args []string) peer.Response {
if len(args) != 6 {
return shim.Error("Invalid args length != 6, length: " + strconv.Itoa(len(args)))
}
//将参数填入数据结构中
uploadData := &CropUpload{
CropID: args[0],
Timestamp: args[1],
Healthy: args[2],
Pic: args[3],
Action: args[4],
Name: args[5],
}
//将对象序列化成数组存储
m, _ := json.Marshal(uploadData)
// 根据设计文档 创建索引
err := stub.PutState("crpoid#"+uploadData.CropID+"#"+uploadData.Timestamp, m)
if err != nil {
return shim.Error(err.Error())
}
err = stub.PutState("uploadname#"+uploadData.Name+"#"+uploadData.Timestamp, m)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
func (t *Trace) recordProcess(stub shim.ChaincodeStubInterface, args []string) peer.Response {
if len(args) != 5 {
return shim.Error("Invalid args length != 5, length: " + strconv.Itoa(len(args)))
}
//将参数填入数据结构中
uploadData := &ProcessUpload{
ProductID: args[0],
Timestamp: args[1],
Pic: args[2],
Process: args[3],
Name: args[4],
}
//将对象序列化成数组存储
m, _ := json.Marshal(uploadData)
// 根据设计文档 创建索引
err := stub.PutState("productid#"+uploadData.ProductID+"#"+uploadData.Timestamp, m)
if err != nil {
return shim.Error(err.Error())
}
err = stub.PutState("uploadname#"+uploadData.Name+"#"+uploadData.Timestamp, m)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
func main() {
if err := shim.Start(new(Trace)); err != nil {
fmt.Printf("Error starting SimpleAsset chaincode: %s", err)
}
}
chaincode验证
进入区块链浏览器验证
数据验证正确