fabric鏈碼操作和編寫鏈碼約束
阿新 • • 發佈:2022-04-20
docker exec -it cli /bin/bash
建立通道
peer channel create -o orderer.example.com:7050 -c zlktchannel -f ./channel-artifacts/channel.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/msp/tlscacerts/tlsca.example.com-cert.pem
當前節點加入通道
peer channel join -b zlktchannel.block
其他節點在cli配置環境
export CORE_PEER_ID=org2peer0
export CORE_PEER_ADDRESS=peer0.org2.example.com:7051
export CORE_PEER_LOCALMSPID=Org2MSP
export CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/server.crt
export CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/server.key
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp
其他節點加入通道
peer channel join -b zlktchannel.block
安裝鏈碼
peer chaincode install -n example02 -p github.com/hyperledger/fabric/examples/chaincode/go -v 1.0.0
鏈碼例項化
peer chaincode instantiate
-o orderer.example.com:7050
-C zlktchannel
-c '{"Args":["init","a","2","b","3"]}'
-n example02
-P "AND ('OrdererOrg.member','Org1.member','Org2.member')"
-v 1.0.0
--tls true
--cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
peer chaincode instantiate -o orderer.example.com:7050 -C zlktchannel -c '{"Args":["init","a","2","b","3"]}' -n example02 -P "AND ('OrdererOrg.member','Org1.member','Org2.member')" -v 1.0.0 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
驗證是否有新增容器
exit
docker ps -a
鏈碼操作
//查詢賬戶狀態
peer chaincode query -C zlktchannel -n example02 -c '{"Args":["query","a"]}'
//賬戶轉賬
-c '{"Args":["invoke","a","b","1"]}'
//刪除指定賬戶:
-c '{"Args":["delete","a"]}'
編寫鏈碼相關約束
-
1.package必須是main
-
2.必須要有main函式,main函式中執行了shim.Start
-
shim.Start(new(SimpleChaincode)) // SimpleChaincode是個結構體
-
3.必須要有個結構體,除了main函式,其他的函式必須都掛到這個結構體下,使用指標,包括Init函式
-
4.必須要有Init函式,不是小寫的init
-
5.必須要用Invoke函式
peer包
type Response struct {
// A status code that should follow the HTTP status codes.
Status int32 `protobuf:"varint,1,opt,name=status" json:"status,omitempty"`
// A message associated with the response code.
Message string `protobuf:"bytes,2,opt,name=message" json:"message,omitempty"`
// A payload that can be used to include metadata with this response.
Payload []byte `protobuf:"bytes,3,opt,name=payload,proto3" json:"payload,omitempty"`
}
shim.Success(nil)
func Success(payload []byte) pb.Response {
return pb.Response{
Status: OK,
Payload: payload,
}
}
shim包
1.Chaincode interface
Init(stub ChaincodeStubInterface) pb.Response
Invoke(stub ChaincodeStubInterface) pb.Response
2.ChaincodeStubInterface interface
GetArgs() [][]byte // 獲取所有的引數,包括函式和引數兩個
GetStringArgs() []string // 獲取所有的引數,包括函式和引數兩個,轉成string型別
GetFunctionAndParameters() (string, []string) // 獲取所有的引數,把函式和引數分成兩部分
GetArgsSlice() ([]byte, error) // 獲取所有的引數,包括函式和引數兩個,封裝成切片型別
GetTxID() string // 獲取交易的id
GetChannelID() string // 獲取通道id
InvokeChaincode(chaincodeName string, args [][]byte, channel string) pb.Response // 根據鏈碼名和channel名呼叫鏈碼
GetState(key string) ([]byte, error) // 根據指定使用者檢視狀態
PutState(key string, value []byte) error // 寫入使用者狀態到賬本
DelState(key string) error // 刪除指定使用者
GetStateByRange(startKey, endKey string) (StateQueryIteratorInterface, error) // 通過range方式獲取多個使用者狀態
GetStateByPartialCompositeKey(objectType string, keys []string) (StateQueryIteratorInterface, error) // 獲取組合鍵的集合
CreateCompositeKey(objectType string, attributes []string) (string, error) // 建立組合鍵
SplitCompositeKey(compositeKey string) (string, []string, error) // 切割組合鍵
GetQueryResult(query string) (StateQueryIteratorInterface, error) // CouchDB查詢結果,couchdb 文件 https://github.com/cloudant/mango
GetHistoryForKey(key string) (HistoryQueryIteratorInterface, error) // 獲取key的歷史值
GetPrivateData(collection, key string) ([]byte, error) // 獲取私有資料
PutPrivateData(collection string, key string, value []byte) error
DelPrivateData(collection, key string) error
GetPrivateDataByRange(collection, startKey, endKey string) (StateQueryIteratorInterface, error)
GetPrivateDataByPartialCompositeKey(collection, objectType string, keys []string) (StateQueryIteratorInterface, error) // 根據組合鍵獲取私有資料
GetPrivateDataQueryResult(collection, query string) (StateQueryIteratorInterface, error)
GetCreator() ([]byte, error) // 獲取當前使用者
GetTransient() (map[string][]byte, error) // 返回可以被鏈碼使用但沒有儲存在賬本中的臨時對映表,例如用於加密和解密的密碼學資訊
GetBinding() ([]byte, error) // 交易的nonce、creator和epoch拼接結果的SHA256雜湊
GetDecorations() map[string][]byte // 獲取map結構的附加資料
GetSignedProposal() (*pb.SignedProposal, error) // 返回完全解碼的簽名交易物件
GetTxTimestamp() (*timestamp.Timestamp, error) // 返回交易建立時的時間戳
SetEvent(name string, payload []byte) error // 當ChainCode提交完畢,會通過Event的方式通知Client。而通知的內
3.CommonIteratorInterface interface
HasNext() bool
Close() error
4.StateQueryIteratorInterface interface
CommonIteratorInterface
Next() (*queryresult.KV, error)
5.HistoryQueryIteratorInterface interface
CommonIteratorInterface 繼承
Next() (*queryresult.KeyModification, error)
6.MockQueryIteratorInterface interface
StateQueryIteratorInterface 繼承
介面文件: