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

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.

主站蜘蛛池模板: 东光县| 明光市| 繁峙县| 镇赉县| 买车| 蓝田县| 郴州市| 星座| 兴仁县| 巨野县| 绥化市| 改则县| 方城县| 当涂县| 大英县| 石狮市| 海原县| 行唐县| 文成县| 阿荣旗| 济南市| 成武县| 大冶市| 磐石市| 新源县| 望谟县| 仙居县| 新闻| 沾化县| 安宁市| 宁陵县| 滨海县| 南康市| 紫金县| 浪卡子县| 从江县| 孝感市| 吉林省| 永宁县| 宁安市| 繁峙县|