官术网_书友最值得收藏!

  • Hyperledger Cookbook
  • Xun (Brian) Wu Chuanfeng Zhang Andrew Zhang
  • 187字
  • 2021-06-24 14:59:38

Writing chaincode using Go

Every chaincode needs to implement a Chaincode interface. There are two methods defined in the interface:

type Chaincode interface {
Init (stub ChaincodeStubInterface) pb.Response
Invoke (stub ChaincodeStubInterface) pb.Response
}

Here, the Init method is called to allow the chaincode to create an initial state and the data initialization after the chaincode container has been established for the first time. The Invoke method is called to interact with the ledger (to query or update the asset) in the proposed transaction.

ChaincodeStubInterface provides the API for apps to access and modify their ledgers. Here are some important APIs:

type ChaincodeStubInterface interface {
InvokeChaincode(chaincodeName string, args [][]byte, channel string) pb.Response
GetState(key string) ([]byte, error)
PutState(key string, value []byte) error
DelState(key string) error
GetQueryResult(query string) (StateQueryIteratorInterface, error)
GetTxTimestamp() (*timestamp.Timestamp, error)
GetTxID() string
GetChannelID() string
}

Examples of important APIs include the following:

  • InvokeChaincode: Calls the chaincode function
  • GetState: Returns the value of the specified key from the ledger
  • PutState: Adds the key and the value to the ledger

Now that we understand some basic chaincode APIs, let's start to write our chaincode for IT asset management.

主站蜘蛛池模板: 松江区| 平顺县| 临清市| 横峰县| 沁阳市| 禹州市| 邹平县| 康平县| 静宁县| 天祝| 康马县| 陵水| 焉耆| 元氏县| 黄浦区| 呼伦贝尔市| 休宁县| 安龙县| 光泽县| 辽阳县| 白银市| 黄浦区| 峨眉山市| 孝感市| 庄浪县| 嘉义市| 余姚市| 宁陕县| 渭南市| 房山区| 山东省| 平远县| 麻栗坡县| 芮城县| 邢台县| 襄垣县| 敦化市| 灵川县| 渭源县| 陆良县| 津南区|