HyperledgerFabric中鏈碼shimAPI怎么用

這篇文章給大家分享的是有關(guān)Hyperledger Fabric中鏈碼shim API怎么用的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來(lái)看看吧。

塔城網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),塔城網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為塔城上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的塔城做網(wǎng)站的公司定做!

用 Go 的鏈碼的 shim API 主要方法詳解

  • GetFunctionAndParameters
    獲取方法名和參數(shù)
    invoke的參數(shù) : {"Args":["set","a","100"]}

    	fn, args := stub.GetFunctionAndParameters()
    	fmt.Println("GetFunctionAndParameters方法獲取方法名和參數(shù):", fn, args)
    	stub.PutState(args[0], []byte(args[1]))

  • GetStringArgs
    獲取所有參數(shù)字符串?dāng)?shù)組,包含方法名

    	args = stub.GetStringArgs()
    	fmt.Println("GetStringArgs方法獲取所有參數(shù)字符串?dāng)?shù)組,包含方法名:", args)

  • PutState
    設(shè)置值
    設(shè)置key-value 4個(gè)(str0-hello0,str1-hello1,str2-hello2,str3-hello3)

    	stub.PutState("str0", []byte("hello0"))
    	stub.PutState("str1", []byte("hello1"))
    	stub.PutState("str2", []byte("hello2"))
    	stub.PutState("str3", []byte("hello3"))

  • GetStateByRange
    獲取從key為str0到key為str2的值,返回k-v的迭代器

    	resultIterator, _ := stub.GetStateByRange("str0", "str2")
    	defer resultIterator.Close()
    	fmt.Println("GetStateByRange獲取從key為str0到key為str2的值,返回k-v的迭代器,遍歷:")
    	for resultIterator.HasNext() {
    		item, _ := resultIterator.Next()
    		fmt.Println(string(item.Key), string(item.Value))
    	}

  • GetState 獲取對(duì)應(yīng)key的值

    	a, _ := stub.GetState("a")
    	fmt.Println("GetState方法獲取a的值:", string(a))

  • GetHistoryForKey
    獲取key的歷史值,返回歷史迭代器,可以獲取歷史所在交易id和值
    GetHistoryForKey 請(qǐng)求節(jié)點(diǎn)配置 core.ledger.history.enableHistoryDatabase 為 true

    	historyIterator, err := stub.GetHistoryForKey("a")
    	if err != nil {
    		fmt.Println(err)
    	} else {
    		defer historyIterator.Close()
    		for historyIterator.HasNext() {
    			item, _ := historyIterator.Next()
    			fmt.Println(item.TxId, string(item.Value))
    		}
    	}

  • DelState
    刪除key為a的鍵值

    	stub.DelState("a")
    	fmt.Printf("DeDelState方法刪除key為a的鍵值,")
    	a, _ = stub.GetState("a")
    	fmt.Println("刪除后獲取a的值:", string(a))

  • CreateCompositeKey 創(chuàng)建組合鍵

    	indexName := "sex~name"
    	indexKey, _ := stub.CreateCompositeKey(indexName, []string{"boy", "jon"})
    	fmt.Println("CreateCompositeKey方法創(chuàng)建組合鍵:", indexKey)
    	stub.PutState(indexKey, []byte("0"))
    
    	indexKey, _ = stub.CreateCompositeKey(indexName, []string{"boy", "luo"})
    	fmt.Println("CreateCompositeKey方法創(chuàng)建組合鍵:", indexKey)
    	stub.PutState(indexKey, []byte("0"))
    
    	indexKey, _ = stub.CreateCompositeKey(indexName, []string{"girl", "wen"})
    	fmt.Println("CreateCompositeKey方法創(chuàng)建組合鍵:", indexKey)
    	stub.PutState(indexKey, []byte("0"))

  • GetStateByPartialCompositeKey
    獲取組合鍵的集合迭代器

    	resultIterator, _ = stub.GetStateByPartialCompositeKey(indexName, []string{"boy"})
    	defer resultIterator.Close()
    	fmt.Println("GetStateByPartialCompositeKey方法獲取有boy的集合迭代器,遍歷:")
    	for resultIterator.HasNext() {
    		item, _ := resultIterator.Next()
    		fmt.Println("key: " + item.Key)
    		fmt.Println("value: " + string(item.Value))
    		objectType, compositeKeyParts, _ := stub.SplitCompositeKey(item.Key)
    		fmt.Println("objectType: " + objectType)
    		fmt.Println("sex : " + compositeKeyParts[0])
    		fmt.Println("name : " + compositeKeyParts[1])
    	}

  • GetQueryResult
    couchdb 文檔 https://github.com/cloudant/mango
    只支持支持豐富查詢的狀態(tài)數(shù)據(jù)庫(kù)CouchDB

    queryIterator, err1 := stub.GetQueryResult(`{"selector": {"sex": "boy"}}`)
    if err1 != nil {
    		fmt.Println(err1)
    } else {
    		defer queryIterator.Close()
    		for queryIterator.HasNext() {
    		item, _ := queryIterator.Next()
    			fmt.Println(item.Key, string(item.Value))
    		}
    }

  • InvokeChaincode
    InvokeChaincode方法在本地調(diào)用指定的chaincode的方法
    同一通道的鏈碼調(diào)用另一鏈碼會(huì)影響另一鏈碼狀態(tài)
    不同通道的鏈碼調(diào)用另一鏈碼不會(huì)影響被調(diào)用的鏈碼狀態(tài),相當(dāng)于一個(gè)查詢

    trans:=[][]byte{[]byte("invoke"),[]byte("a"),[]byte("b"),[]byte("11")}
    result := stub.InvokeChaincode("mycc",trans,"myc")
    fmt.Println(result)

  • GetCreator
    獲取當(dāng)前用戶

    	creatorByte, _ := stub.GetCreator()
    	certStart := bytes.IndexAny(creatorByte, "-----BEGIN")
    	if certStart == -1 {
        fmt.Errorf("No certificate found")
    }
     certText := creatorByte[certStart:]
    	bl, _ := pem.Decode(certText)
    	if bl == nil {
    	   fmt.Errorf("Could not decode the PEM structure")
    	}
    	cert, err := x509.ParseCertificate(bl.Bytes)
    	if err != nil {
        fmt.Errorf("ParseCertificate failed")
    	}
    	uname := cert.Subject.CommonName
    	fmt.Println("Name:" + uname)

  • GetQueryResultWithPagination
    所有分頁(yè)功能的方法是1.3版本后才有的
    分頁(yè)豐富查詢
    只支持支持豐富查詢的狀態(tài)數(shù)據(jù)庫(kù)CouchDB
    關(guān)鍵的參數(shù) :
    pageSize 是分頁(yè)大小
    bookmark 索引, 第一頁(yè)為空字符串,每次查詢都會(huì)有下一頁(yè)的索引的返回,用來(lái)做參數(shù)繼續(xù)往下查

    resultsIterator, responseMetadata, err := stub.GetQueryResultWithPagination(queryString, pageSize, bookmark)
    	if err != nil {
    		return nil, err
    	}
    	defer resultsIterator.Close()
    
    	var buffer bytes.Buffer
    	buffer.WriteString("[")
    
    	bArrayMemberAlreadyWritten := false
    	for resultsIterator.HasNext() {
    		queryResponse, err := resultsIterator.Next()
    		if err != nil {
    			return nil, err
    		}
    
    		if bArrayMemberAlreadyWritten == true {
    			buffer.WriteString(",")
    		}
    		buffer.WriteString("{\"Key\":")
    		buffer.WriteString("\"")
    		buffer.WriteString(queryResponse.Key)
    		buffer.WriteString("\"")
    
    		buffer.WriteString(", \"Record\":")
    
    		buffer.WriteString(string(queryResponse.Value))
    		buffer.WriteString("}")
    		bArrayMemberAlreadyWritten = true
    	}
    	buffer.WriteString("]")
    
    	if err != nil {
    		return nil, err
    	}
    
    	buffer.WriteString("[{\"ResponseMetadata\":{\"RecordsCount\":")
    	buffer.WriteString("\"")
    	buffer.WriteString(fmt.Sprintf("%v", responseMetadata.FetchedRecordsCount))
    	buffer.WriteString("\"")
    	buffer.WriteString(", \"Bookmark\":")
    	buffer.WriteString("\"")
    	buffer.WriteString(responseMetadata.Bookmark)
    	buffer.WriteString("\"}}]")
    	fmt.Printf("- getQueryResultForQueryString queryResult:\n%s\n", buffer.String())

感謝各位的閱讀!關(guān)于“Hyperledger Fabric中鏈碼shim API怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

標(biāo)題名稱:HyperledgerFabric中鏈碼shimAPI怎么用
文章地址:http://muchs.cn/article38/ipjosp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、搜索引擎優(yōu)化網(wǎng)站維護(hù)、建站公司定制網(wǎng)站、軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)