- 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.
推薦閱讀
- 普林斯頓微積分讀本(修訂版)
- 幾何原本
- 一個(gè)定理的誕生:我與菲爾茨獎(jiǎng)的一千個(gè)日夜
- 世界是隨機(jī)的:大數(shù)據(jù)時(shí)代的概率統(tǒng)計(jì)學(xué)
- 迷人的數(shù)學(xué)(全2冊)
- ANSYS Workbench 2020有限元分析從入門到精通(升級版)
- 圖像處理的分?jǐn)?shù)階微積分方法
- 線性代數(shù)
- 概率論與數(shù)理統(tǒng)計(jì)
- 數(shù)學(xué)故事總動(dòng)員
- 數(shù)理統(tǒng)計(jì)及其在數(shù)學(xué)建模中的實(shí)踐(使用MATLAB)
- 數(shù)學(xué)也可以這樣學(xué):大自然中的幾何學(xué)
- 數(shù)學(xué)思維的力量
- 在動(dòng)手實(shí)驗(yàn)中學(xué)習(xí)科學(xué)與數(shù)學(xué)
- 數(shù)字狂想