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

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.

主站蜘蛛池模板: 万源市| 凤庆县| 卢氏县| 昌邑市| 鄱阳县| 监利县| 遂平县| 灵宝市| 建德市| 泽普县| 凌云县| 德惠市| 南召县| 靖远县| 南华县| 招远市| 苏州市| 会理县| 肥东县| 阿克苏市| 宜宾县| 呼和浩特市| 米泉市| 罗源县| 罗平县| 三河市| 石林| 隆德县| 勃利县| 汉寿县| 车致| 南京市| 鄢陵县| 仪陇县| 丽江市| 黄浦区| 连州市| 芷江| 邢台市| 鄂尔多斯市| 大同县|