Hyperledger fabric Chaincode API:https://godoc.org/github.com/hyperledger/fabric/core/chaincode/shim
shim.ChaincodeStubInterface 接口有三个方法,分别是:Init、Query 和 Invoke
下载地址:https://github.com/hyperledger/fabric
package main
import (
"fmt"
"strconv"
"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer" //pb 是对后面包的命名
)
//定义一个结构体,继承ChainCode接口
type SimpleChaincode struct {
}
/***************** ChainCode接口类型 ***********************
type Chaincode interface {
Init(stub ChaincodeStubInterface) pb.Response
Invoke(stub ChaincodeStubInterface) pb.Response
}
**************************************************************
*/
负责初始化工作,链码首次部署到区块链网络时调用,将由部署自己的链代码实例的每个对等节点执行。此方法可用于任何与初始化、引导或设置相关的任务。
func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
}
只要在区块链状态上执行任何读取/获取/查询操作,就需要调用 Query 方法。如果尝试在 Query 方法内修改区块链的状态,将会抛出异常。
此方法主要是做修改操作,但是很多例子中一些用户也在 Invoke 做查询。
put, get, del 等操作都在可以在 Invoke 中运行
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
}
参考例子
func (s *SmartContract) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
// Retrieve the requested Smart Contract function and arguments
function, args := stub.GetFunctionAndParameters()
// Route to the appropriate handler function to interact with the ledger appropriately
if function == "balanceToken" {
return s.balanceToken(stub, args) //balanceToken为自定义的属于SmartContract的方法
} else if function == "initLedger" {
return s.initLedger(stub) //initLedger为自定义的属于SmartContract的方法
} else if function == "transferToken" {
return s.transferToken(stub, args) //transferToken为自定义的属于SmartContract的方法
}
return shim.Error("Invalid Smart Contract function name.")
}
在 Invoke 函数中,首先使用 stub.GetFunctionAndParameters() 获取合约函数 function, args := stub.GetFunctionAndParameters() 然后判断函数名称,实现对应的逻辑关系。
if function == "balanceToken" { return s.balanceToken(stub, args) } else if function == "initLedger" {
return s.initLedger(stub)
} else if function == "transferToken" {
return s.transferToken(stub, args)
}
任何 Go 程序的都需要 main 函数,他是程序的入口,因此该函数被用于引导/启动链代码。当对peer节点部署chaincode并实例化时,就会执行 main 函数。
func main() {
err := shim.Start(new(SimpleChaincode))
if err != nil {
fmt.Printf("Error starting Simple chaincode: %s", err)
}
}
shim.Start(new(SampleChaincode)) 启动链代码并注册到peer 节点。
Hyperledger提供基于key/value的数据存储,其中key是字符串,value则是二进制字节数组,Hyperledger的Go API提供了三个方法用于数据存取:PutState(key, value)用于向Hyperledger中存储数据, GetState(key)用于从Hyperledger中提取数据,而DelState(key)则从Hyperledger中删除数据。
写入数据如果是 struct 结构体,需要序列化,通常使用 json,其他形式的序列化也可以,只要能反序列化即可。
8.1.1. PutState(key, value)写入区块
写入区块
func (s *SmartContract) initLedger(stub shim.ChaincodeStubInterface) sc.Response {
token := &Token{
Owner: "netkiller",
TotalSupply: 10000,
TokenName: "财富通正",
TokenSymbol: "COIN",
BalanceOf: map[string]uint{}}
token.initialSupply()
tokenAsBytes, _ := json.Marshal(token)
stub.PutState("Token", tokenAsBytes)
fmt.Println("Added", tokenAsBytes)
return shim.Success(nil)
}
8.1.2. GetState(key) 读取区块
通过key获取区块信息
func (s *SmartContract) balanceToken(stub shim.ChaincodeStubInterface, args []string) sc.Response {
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting 1")
}
tokenAsBytes, _ := stub.GetState(args[0])
token := Token{}
json.Unmarshal(tokenAsBytes, &token)
amount := token.balance(args[1])
return shim.Success(amount)
}
8.1.3. DelState(key) 删除区块
删除区块信息(注:区块链是不能参数数据的,删除数据在区块链中只是新添加一条数据,记录某一条数据被删除。)
func (s *SmartContract) deleteData(stub shim.ChaincodeStubInterface, args []string) sc.Response {
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting 1")
}
err= stub.DelState(args[0])
if err != nil {
return shim.Error("Failed to delete Student from DB, key is: "+key)
}
return shim.Success(nil)
}
8.1.4. 修改数据
State 数据库并没有提供修改功能,修改数据可以先读取,再修改,最后写入
func (s *SmartContract) transferToken(stub shim.ChaincodeStubInterface, args []string) sc.Response {
if len(args) != 3 {
return shim.Error("Incorrect number of arguments. Expecting 2")
}
tokenAsBytes, _ := stub.GetState(args[0])
token := Token{}
json.Unmarshal(tokenAsBytes, &token)
token.transfer(args[1],args[2],args[3])
tokenAsBytes, _ = json.Marshal(token)
stub.PutState(args[0], tokenAsBytes)
return shim.Success(nil)
}
8.1.5. GetStateByRange(startKey, endKey) 范围查找
区块链是一个线性的数据结构,只要知道开始位置,结束位置,就能够取出中间部分的数据。
func (s *SmartContract) queryAllCars(APIstub shim.ChaincodeStubInterface) sc.Response {
startKey := "CAR0"
endKey := "CAR999"
resultsIterator, err := APIstub.GetStateByRange(startKey, endKey)
if err != nil {
return shim.Error(err.Error())
}
defer resultsIterator.Close()
// buffer is a JSON array containing QueryResults
var buffer bytes.Buffer
buffer.WriteString("[")
bArrayMemberAlreadyWritten := false
for resultsIterator.HasNext() {
queryResponse, err := resultsIterator.Next()
if err != nil {
return shim.Error(err.Error())
}
// Add a comma before array members, suppress it for the first array member
if bArrayMemberAlreadyWritten == true {
buffer.WriteString(",")
}
buffer.WriteString("{\"Key\":")
buffer.WriteString("\"")
buffer.WriteString(queryResponse.Key)
buffer.WriteString("\"")
buffer.WriteString(", \"Record\":")
// Record is a JSON object, so we write as-is
buffer.WriteString(string(queryResponse.Value))
buffer.WriteString("}")
bArrayMemberAlreadyWritten = true
}
buffer.WriteString("]")
fmt.Printf("- queryAllCars:\n%s\n", buffer.String())
return shim.Success(buffer.Bytes())
}
8.1.6. GetQueryResult(query string) CouchDB 查询
//<<<<<<<<<<<<<<<<<<<<<<<<<< CouchDB简单介绍 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
CouchDB特点:
面向文档数据库,不需要范式,直接存储JSON就可以,CouchDB默认会生成 _id,_rev 两个键,_id是一条记录(文档)的唯一标识,如果不提供_id,_id会自动生成,也可以手动指定_id,比如用手机号做主键:
{'_id' : '+86186*****', name: '' }
_rev是其版本号,每更新一次 _rev就会自动发生变化,格式为
5-6a8617596d2adfea245662df0df611ao
////<<<<<<<<<<<<<<<<<<<<<<<<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
GetQueryResult 能查询 json 里面的数据
下面例子是 Name = Neo Chen 的所有数据。
func (t *SimpleChaincode) getQueryResult(stub shim.ChaincodeStubInterface, args []string) pb.Response{
name:="Neo Chen" //需要查询的名字
queryString := fmt.Sprintf("{\"selector\":{\"Name\":\"%s\"}}", name)
resultsIterator,err:= stub.GetQueryResult(queryString)//必须是CouchDB才行
if err!=nil{
return shim.Error("query failed")
}
person,err:=getListResult(resultsIterator)
if err!=nil{
return shim.Error("query failed")
}
return shim.Success(person)
}
8.1.7. stub.GetHistoryForKey
通过key获取历史数据
func (t *SimpleChaincode) historyQuery(stub shim.ChaincodeStubInterface, args []string) pb.Response{
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting 1")
}
it,err:= stub.GetHistoryForKey(args[0])
if err!=nil{
return shim.Error(err.Error())
}
var result,_= getHistoryListResult(it)
return shim.Success(result)
}
8.1.8. shim.HistoryQueryIteratorInterface 接口
func getHistoryListResult(resultsIterator shim.HistoryQueryIteratorInterface) ([]byte,error){
defer resultsIterator.Close()
// buffer is a JSON array containing QueryRecords
var buffer bytes.Buffer
buffer.WriteString("[")
bArrayMemberAlreadyWritten := false
for resultsIterator.HasNext() {
queryResponse, err := resultsIterator.Next()
if err != nil {
return nil, err
}
// Add a comma before array members, suppress it for the first array member
if bArrayMemberAlreadyWritten == true {
buffer.WriteString(",")
}
item,_:= json.Marshal( queryResponse)
buffer.Write(item)
bArrayMemberAlreadyWritten = true
}
buffer.WriteString("]")
fmt.Printf("queryResult:\n%s\n", buffer.String())
return buffer.Bytes(), nil
}
16.8.2.1. 创建复合键
// maintain the index
indexName := "color~name"
colorNameIndexKey, err := stub.CreateCompositeKey(indexName, []string{marbleJSON.Color, marbleJSON.Name})
if err != nil {
return shim.Error(err.Error())
}
// Delete index entry to state.
err = stub.DelState(colorNameIndexKey)
if err != nil {
return shim.Error("Failed to delete state:" + err.Error())
}
8.2.2. 分解复合键
// get the color and name from color~name composite key
objectType, compositeKeyParts, err := stub.SplitCompositeKey(responseRange.Key)
if err != nil {
return shim.Error(err.Error())
}
returnedColor := compositeKeyParts[0]
returnedMarbleName := compositeKeyParts[1]
Hyperledger Fabic 事件实现了发布/订阅消息队列。您可以自由地在链码中创建和发出自定义事件。例如,区块链的状态发生改变,就会生成一个事件。通过向区块链上的事件中心注册一个事件适配器,客户端应用程序可以订阅和使用这些事件。
func (t *SimpleChaincode) testEvent(stub shim.ChaincodeStubInterface, args []string) pb.Response{
message := "Event send data is here!"
err := stub.SetEvent("evtsender", []byte(message))
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
func (t *SimpleChaincode) testEvent(stub shim.ChaincodeStubInterface, args []string) pb.Response{
event := &Token{
Owner: "netkiller",
TotalSupply: 10000,
TokenName: "代币通正",
TokenSymbol: "COIN",
BalanceOf: map[string]uint{}}
eventBytes, err ;= json.Marshal(&event)
if err != nil {
return nil, err
}
err := stub.SetEvent("evtSender", eventBytes)
if err != nil {
fmt.Println("Could not set event for loan application creation", err)
}
}
在当前连码中调用另一个连码,调用连码需要提供连码名和通道名 stub.InvokeChaincode("连码名",调用函数,"通道")
//InvokeChaincode(chaincodeName string, args [][]byte, channel string) pb.Response 调用其他链码
func (t *SimpleChaincode) testInvokeChainCode(stub shim.ChaincodeStubInterface, args []string) pb.Response{
trans:=[][]byte{[]byte("invoke"),[]byte("transfer"),[]byte("netkiller"),[]byte("neo"),[]byte("100")}
response:= stub.InvokeChaincode("token",trans,"mychannel")
fmt.Println(response.Message)
return shim.Success([]byte( response.Message))
}
//溯源
//溯源:(1)首先根据 Sale的ID找到对应的值 值中有厂家ID(FormId)
// (2)根据厂家的ID,在MachainInfo中找到 牛奶厂家的ID(FormId)
// (3)根据牛奶厂家的ID,找到对应的牛奶信息
// 根据keyID查询历史记录
func(t* Sale)trace(stub shim.ChaincodeStubInterface, args []string) peer.Response {
text := t.query(stub, args)
if text.Status != shim.OK {
return shim.Error("query error ......")
}
var myList []string
var fromID string
// 获取需要的信息
var saleItem SaleInfo
json.Unmarshal(text.Payload, &saleItem)
fromID = saleItem.FromId
myList = append(myList, saleItem.Name)
// 找奶牛场
myArgs := [][]byte{[]byte("query"), []byte(fromID)}
response := stub.InvokeChaincode("machincc", myArgs, "tracechannel")
if response.Status != shim.OK {
return shim.Error("InvokeChaincode error ......")
}
var machItem MachinInfo
json.Unmarshal(response.Payload, &machItem)
fromID = machItem.FromId
myList = append(myList, machItem.Name)
// 搜索奶牛场信息
response = stub.InvokeChaincode("dairycc", myArgs, "tracechannel")
if response.Status != shim.OK {
return shim.Error("InvokeChaincode error ......")
}
var farmIitem FarmInfo
json.Unmarshal(response.Payload, &farmIitem)
myList = append(myList, farmIitem.Name)
// 数据格式化为json
jsonText, error := json.Marshal(myList)
if error != nil {
return shim.Error("json.Marshal(myList) fail...")
}
return shim.Success(jsonText)
}
9. 案例
9.1. 模仿以太坊 ERC20 规范的 Hyperledger Fabric 实现
package main
import (
"bytes"
"encoding/json"
"fmt"
"strconv"
"github.com/hyperledger/fabric/core/chaincode/shim"
sc "github.com/hyperledger/fabric/protos/peer"
)
// Define the Smart Contract structure
type SmartContract struct {
}
type Token struct {
Owner string `json:"Owner"`
TotalSupply uint `json:"TotalSupply"`
TokenName string `json:"TokenName"`
TokenSymbol string `json:"TokenSymbol"`
BalanceOf map[string]uint `json:"BalanceOf"`
}
func (token *Token) initialSupply(){
token.BalanceOf[token.Owner] = token.TotalSupply;
}
func (token *Token) transfer (_from string, _to string, _value uint){
if(token.BalanceOf[_from] >= _value){
token.BalanceOf[_from] -= _value;
token.BalanceOf[_to] += _value;
}
}
func (token *Token) balance (_from string) uint{
return token.BalanceOf[_from]
}
func (token *Token) burn(_value uint) {
if(token.BalanceOf[token.Owner] >= _value){
token.BalanceOf[token.Owner] -= _value;
token.TotalSupply -= _value;
}
}
func (token *Token) burnFrom(_from string, _value uint) {
if(token.BalanceOf[_from] >= _value){
token.BalanceOf[_from] -= _value;
token.TotalSupply -= _value;
}
}
func (token *Token) mint(_value uint) {
token.BalanceOf[token.Owner] += _value;
token.TotalSupply += _value;
}
func (s *SmartContract) Init(stub shim.ChaincodeStubInterface) sc.Response {
return shim.Success(nil)
}
func (s *SmartContract) initLedger(stub shim.ChaincodeStubInterface) sc.Response {
token := &Token{
Owner: "netkiller",
TotalSupply: 10000,
TokenName: "财富通正",
TokenSymbol: "COIN",
BalanceOf: map[string]uint{}}
token.initialSupply()
tokenAsBytes, _ := json.Marshal(token)
stub.PutState("Token", tokenAsBytes)
fmt.Println("Added", tokenAsBytes)
return shim.Success(nil)
}
func (s *SmartContract) transferToken(stub shim.ChaincodeStubInterface, args []string) sc.Response {
if len(args) != 3 {
return shim.Error("Incorrect number of arguments. Expecting 2")
}
tokenAsBytes, _ := stub.GetState(args[0])
token := Token{}
json.Unmarshal(tokenAsBytes, &token)
token.transfer(args[1],args[2],args[3])
tokenAsBytes, _ = json.Marshal(token)
stub.PutState(args[0], tokenAsBytes)
return shim.Success(nil)
}
func (s *SmartContract) balanceToken(stub shim.ChaincodeStubInterface, args []string) sc.Response {
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting 1")
}
tokenAsBytes, _ := stub.GetState(args[0])
token := Token{}
json.Unmarshal(tokenAsBytes, &token)
amount := token.balance(args[1])
return shim.Success(amount)
}
func (s *SmartContract) Invoke(stub shim.ChaincodeStubInterface) sc.Response {
// Retrieve the requested Smart Contract function and arguments
function, args := stub.GetFunctionAndParameters()
// Route to the appropriate handler function to interact with the ledger appropriately
if function == "balanceToken" {
return s.balanceToken(stub, args)
} else if function == "initLedger" {
return s.initLedger(stub)
} else if function == "transferToken" {
return s.transferToken(stub, args)
}
return shim.Error("Invalid Smart Contract function name.")
}
// The main function is only relevant in unit test mode. Only included here for completeness.
func main() {
// Create a new Smart Contract
err := shim.Start(new(SmartContract))
if err != nil {
fmt.Printf("Error creating new Smart Contract: %s", err)
}
}