zoukankan      html  css  js  c++  java
  • 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 main
    
    import (
    	"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!
    ", function)
    
        return nil, nil
    }
    
    func (t *SaveState1Chaincode) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
        fmt.Printf("Invoke called with function %s!
    ", function)
    
    	var key, value string
    	key = args[0]
    	value = args[1]
    
    	var err error
    	err = 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!
    ", function)
    
        var key string
    	key = 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的讨论.

  • 相关阅读:
    016.CI4框架CodeIgniter数据库操作之:Insert插入一条数据
    015.CI4框架CodeIgniter数据库操作之:Query带参数查询数
    014.CI4框架CodeIgniter数据库操作之:查询数据库,并让数据以对象的方式返回查询结果
    013.CI4框架CodeIgniter数据库操作之:查询数据库,并让数据以数组的方式返回查询结果
    012.CI4框架CodeIgniter, 加载并调用自己的Libraries库
    033.SAP上查看IDOC接口,PI接口查不到的日志记录,可能在IDOC接口日志里面
    032.SAP上用户无法打开PPE模块,查看并开通用户的PPE权限
    011.CI4框架CodeIgniter, 获取查看用户的IP地址和浏览器信息
    010.CI4框架CodeIgniter, autoload自动加载自己的helper函数类
    009.CI4框架CodeIgniter, 网页访问GET的URL参数获取,分段输出URL参数
  • 原文地址:https://www.cnblogs.com/huyouhengbc/p/5967428.html
Copyright © 2011-2022 走看看