日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Hyperledger中数据存取的实现

發布時間:2023/12/10 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Hyperledger中数据存取的实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

簡介

本文介紹了在Hyperledger中數據存取的實現.

API接口

Hyperledger提供基于key/value的數據存儲,其中key是字符串,value則是二進制字節數組,Hyperledger的Go API提供了三個方法用于數據存取:PutState(key, value)用于向Hyperledger中存儲數據, GetState(key)用于從Hyperledger中提取數據,而DelState(key)則從Hyperledger中刪除數據。

數據存取 Chaincode 示例

以下是一個簡單的數據存取Chaincode, 以及其相應的REST請求。

package mainimport ("errors""fmt""github.com/hyperledger/fabric/core/chaincode/shim" )type SaveState1Chaincode struct { }func (t *SaveState1Chaincode) Init(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {fmt.Printf("Init called with function %s!\n", function)return nil, nil }func (t *SaveState1Chaincode) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {fmt.Printf("Invoke called with function %s!\n", function)var key, value stringkey = args[0]value = args[1]var err errorerr = stub.PutState(key, []byte(value))if err != nil {return nil, err} return nil, nil }func (t *SaveState1Chaincode) Query(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {fmt.Printf("Query called with function %s!\n", function)var key stringkey = args[0]valInBytes, err := stub.GetState(key)if err != nil {return nil, errors.New("Failed to get state for " + key)}message := "State for " + key + " = " + string(valInBytes)return []byte(message), nil; }func main() {err := shim.Start(new(SaveState1Chaincode))if err != nil {fmt.Printf("Error starting Save State chaincode: %s", err)} }

存儲數據的REST請求

{"jsonrpc": "2.0","method": "invoke","params": {"type": 1,"chaincodeID":{"name":"mycc"},"ctorMsg": {"function":"invoke","args":["testKey", "testValue"]},"secureContext": "jim"},"id": 3}

獲取數據的REST請求

{"jsonrpc": "2.0","method": "query","params": {"type": 1,"chaincodeID":{"name":"mycc"},"ctorMsg": {"function":"query","args":["testKey"]},"secureContext": "jim"},"id": 5}

關于Immutability

以上代碼也可以看出Hyperledger和BitCoin和Ethereum等區塊鏈對Immutability的不同理解, 在Hyperledger中,數據提交到區塊鏈后不僅可以改變,還甚至可以被刪除,而在BitCoin和Ethereum中數據一旦提交到區塊鏈后就不能再被改變。

這也體現在R3的Corda區塊鏈中,R3 CTO Richard Gendal Brown在這里 寫道:

Immutability

The fourth feature in the “Blockchain Bundle” is often, if misleadingly, termed “immutability”: data, once committed, cannot be changed.

This isn’t quite true: if I have a piece of data then of course I can change it. What we actually mean is that: once committed, nobody else will accept a transaction from me if it tries to build on a modified version of some data that has already been accepted by other stakeholders.

Blockchains achieve this by having transactions commit to the outputs of previous transactions and have blocks commit to the content of previous blocks. Each new step can only be valid if it really does build upon an unchangeable body of previous activity.

總結

本文介紹了在Hyperledger中數據存取的實現以及關于Immutability的討論.

轉載于:https://www.cnblogs.com/huyouhengbc/p/5967428.html

總結

以上是生活随笔為你收集整理的Hyperledger中数据存取的实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。