版权声明:博客中的文章版权归博主所有,未经授权,禁止转载,转载请注明出处
https://godoc.org/github.com/hyperledger/fabric/core/chaincode/shim
func (stub *ChaincodeStub) GetFunctionAndParameters() (function string, params []string)
# {"Args":["set","tom","100"]}
fn, args := stub.GetFunctionAndParameters()
fmt.Println(fn, args)
# 输出结果
set ["tom", "100"]
func (stub *ChaincodeStub) GetStringArgs() []string
# {"Args":["set","tom","100"]}
args = stub.GetStringArgs()
fmt.Println(args)
# 输出结果
["set","tom","100"]
func (stub *ChaincodeStub) PutState(key string, value []byte) error
err := stub.PutState("str",[]byte("hello"))
if err != nil {
fmt.Println("str PutState error: "+err.Error())
}else{
fmt.Println("str PutState success!")
}
func (stub *ChaincodeStub) GetState(key string) ([]byte, error)
strValue , err := stub.GetState("str")
if err != nil {
fmt.Println("str GetState error: "+err.Error())
}else {
fmt.Printf("str value: %s \n",string(strValue))
}
# 输出结果
str value: hello
func (stub *ChaincodeStub) DelState(key string) error
err = stub.DelState("str")
func (stub *ChaincodeStub) GetStateByRange(startKey, endKey string) (StateQueryIteratorInterface, error)
err := stub.PutState("str",[]byte("hello"))
err = stub.PutState("str1",[]byte("hello1"))
err = stub.PutState("str2",[]byte("hello2"))
resultIterator , err := stub.GetStateByRange("str" , "str2")
defer resultIterator.Close()
fmt.Println("-----start resultIterator-----")
for resultIterator.HasNext() {
item, _ := resultIterator.Next()
fmt.Println(string(item.Value))
}
fmt.Println("-----end resultIterator-----")
# 运行结果
-----start resultIterator-----
hello
hello1
-----end resultIterator-----
func (stub *ChaincodeStub) GetHistoryForKey(key string) (HistoryQueryIteratorInterface, error)
historyIterator,err := stub.GetHistoryForKey("str")
defer historyIterator.Close()
fmt.Println("-----start historyIterator-----")
for resultIterator.HasNext() {
item, _ := historyIterator.Next()
fmt.Println(string(item.TxId))
fmt.Println(string(item.Value))
}
fmt.Println("-----end historyIterator-----")
func (stub *ChaincodeStub) CreateCompositeKey(objectType string, attributes []string) (string, error)
indexName := "sex~name"
indexKey , err := stub.CreateCompositeKey(indexName,[]string{"boy","xiaowang"})
value := []byte{0x00}
stub.PutState(indexKey,value)
fmt.Println(indexKey)
indexKey , err = stub.CreateCompositeKey(indexName,[]string{"boy","xiaoli"})
stub.PutState(indexKey,value)
fmt.Println(indexKey)
indexKey , err = stub.CreateCompositeKey(indexName,[]string{"girl","xiaofang"})
fmt.Println(indexKey)
stub.PutState(indexKey,value)
# 运行结果
sex~nameboyxiaowang
sex~nameboyxiaoli
sex~namegirlxiaofang
func (stub *ChaincodeStub) SplitCompositeKey(compositeKey string) (string, []string, error)
func (stub *ChaincodeStub) GetStateByPartialCompositeKey(objectType string, attributes []string) (StateQueryIteratorInterface, error)
resultIterator,err = stub.GetStateByPartialCompositeKey(indexName, []string{"boy"})
defer resultIterator.Close()
fmt.Println("-----start resultIterator-----")
for resultIterator.HasNext() {
item, _ := resultIterator.Next()
objectType, compositeKeyParts, err := stub.SplitCompositeKey(item.Key)
if err != nil {
return shim.Error(err.Error())
}
fmt.Println("objectType: "+objectType)
fmt.Println("sex : "+compositeKeyParts[0])
fmt.Println("name : "+compositeKeyParts[1])
}
fmt.Println("-----end resultIterator-----")
# 运行结果
-----start resultIterator-----
objectType: sex~name
sex : boy
name : xiaoli
objectType: sex~name
sex : boy
name : xiaowang
-----end resultIterator----
func (stub *ChaincodeStub) GetQueryResult(query string) (StateQueryIteratorInterface, error)
resultIterator , err = stub.GetQueryResult("{\"selector\": {\"sex\": \"boy\"}}" )
defer resultIterator.Close()
fmt.Println("-----start resultIterator-----")
for resultIterator.HasNext() {
item, _ := resultIterator.Next()
fmt.Println(string(item.Value))
}
fmt.Println("-----end resultIterator-----")
func (stub *ChaincodeStub) InvokeChaincode(chaincodeName string, args [][]byte, channel string) pb.Response
# chaincode_example02 中 a向b 转账
trans:=[][]byte{[]byte("invoke"),[]byte("a"),[]byte("b"),[]byte("11")}
stub.InvokeChaincode("mycc",trans,"mychannel")
关注公众号