Hyperledger Fabric CouchDB as the State Database——使用CouchDB

使用CouchDB做为状态数据库html

状态数据库选项node

状态数据库包括LevelDB和CouchDB。LevelDB是嵌入在peer进程中的默认键/值状态数据库,CouchDB是一个可选的外部状态数据库。与LevelDB键/值存储同样,CouchDB能够存储任何以chaincode建模的二进制数据(CouchDB附件函数在内部用于非json二进制数据)。可是,当chaincode值(例如,资产)被建模为JSON数据时,做为JSON文档存储,CouchDB支持对chaincode数据进行丰富的查询。git

LevelDB和CouchDB都支持核心chaincode操做,例如获取和设置一个键(资产),并根据键进行查询。键能够经过范围查询,能够对组合键进行建模,以支持针对多个参数的等价查询。例如,做为全部者的组合键,资产id能够用于查询某个实体拥有的全部资产。这些基于key的查询能够用于针对帐本的只读查询,以及更新总帐的事务。github

若是将资产建模为JSON并使用CouchDB,那么就可使用chaincode中的CouchDB JSON查询语言对chaincode数据值执行复杂的富查询,这些类型的查询对于理解帐本上的内容颇有帮助。对于这些类型的查询,事务协议响应一般对客户端应用程序有用,但一般不会做为事务提交到排序服务。事实上,也没法保证结果集在chaincode执行与富查询提交时间之间的稳定性,所以使用富查询的结果去执行最终的事务更新操做是不合适的,除非能够保证结果集在chaincode执行时间与提交时间之间的稳定性,或者能够处理在后续交易中的潜在变化。例如,若是对Alice所拥有的全部资产执行一个富查询并将其传输给Bob,那么一个新的资产可能会被另外一个事务分配给Alice,这是在chaincode执行时间和提交时间之间的另外一个事务,可能此过程当中会错过这个“虚值”。docker

CouchDB做为一个独立的数据库进程与peer一块儿运行,所以在设置、管理和操做方面有额外的考虑。咱们能够考虑从默认的嵌入式LevelDB开始,若是须要额外的复杂的富查询,能够转移到CouchDB。将chaincode资产数据建模为JSON是一种很好的作法,这样咱们就能够在未来执行须要的复杂的富查询。数据库

 

使用Chaincode中的CouchDBjson

大多数chaincode的api均可以使用LevelDB或CouchDB状态数据库,例如GetState、PutState、GetStateByRange以及GetStateByPartialCompositeKey等。只有当使用CouchDB做为状态数据库和在chaincode中做为JSON的模型资产时,可使用GetQueryResult API和传递一个CouchDB查询字符串来对状态数据库中的JSON执行富查询。查询字符串遵循CouchDB JSON查询语法api

fabric项目中的marbles02示例演示了使用来自chaincode的CouchDB查询。它包括一个getMarblesByOwner()函数,它经过将一个owner id传递给chaincode来演示参数化查询,而后查询与“marble”docType匹配的JSON文档的状态数据,以及owner id使用JSON查询语法:app

{
  "selector":{
    "docType":"marble",
    "owner":<OWNER_ID>
  }
}

为了使JSON查询更高效,而且对于任何具备排序的JSON查询,都须要使用CouchDB中的索引。索引能够和chaincode一块儿打包 /META-INF/statedb/couchdb/indexes目录。每一个索引必须在其本身的文本文件中经过扩展*.json被定义,其中索引定义为JSON格式并引用CouchDB索引JSON语法,例如,为了支持上面的marble查询,提供了一个关于docType和全部者字段的示例索引:
ide

{
  "index":{
    "fields":[
      "docType",
      "owner"

    ]
  },
  "ddoc":"indexOwnerDoc",
  "name":"indexOwner",
  "type":"json"
}

这里能够找到示例索引。

注意:在1.1 alpha中,必须为索引定义中引用的每一个字段指定“数据”封装器。在随后的版本中,指定“数据”封装器的需求可能会被取消。

 在chaincode的META-INF/statedb/couchdb/indexes目录中的任何索引都将与chaincode一块儿被打包和安装在peer上。当chaincode即安装在peer上,且又在peer的channel上实例化时,该索引将自动部署到peer的channel状态数据库(若是已配置为使用CouchDB)。若是是先安装了chaincode,而后才在channel上实例化该chaincode,那么该索引将会在chaincode实例化的时候进行部署。若是chaincode已经在一个channel上实例化了,而后又在一个peer上安装了这个chaincode,该索引仍是会在chaincode实例化的时候进行部署。

在部署时,索引将自动被chaincode查询利用。CouchDB能够根据查询中使用的字段自动肯定要使用的索引。或者,在选择器查询中,可使用use_index关键字指定索引。

在安装的chaincode的后续版本中可能存在相同的索引。要更改索引,使用相同的索引名称,但要更改索引定义。在安装/实例化,索引定义会从新部署到peer的状态数据库。

若是所在channle已经拥有大量的数据,而且随后安装了chaincode,那么索引的建立在实例化上可能须要一些时间。相似地,若是channel已经拥有大量的数据而且实例化了后续版本的chaincode,那么建立索引也可能须要一些时间。因此须要尽可能避免在这些时间段内调用查询状态数据库的chaincode函数,由于当索引被初始化时,chaincode查询可能会超时。在事务处理期间,由于块被提交给帐本总帐,因此索引将自动刷新。

 

CouchDB配置

经过将状态数据库配置选项从goleveldb更改成CouchDB,能够将CouchDB做为状态数据库启用。此外,couchDBAddress须要配置为指向由peer使用的CouchDB。若是将CouchDB配置一个用户名和密码,那么用户名和密码属性应该包含管理员用户名和密码。在couchDBConfig中提供了更多的选项,并在适当的地方进行了记录。在重启peer以后,修改过的core.yaml将会当即生效。

固然还能够经过docker环境变量来覆盖core.yaml值,例如CORE_LEDGER_STATE_STATEDATABASE和CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS。

如下是来自core.yaml的状态数据库选项:

state:
  # stateDatabase - options are "goleveldb", "CouchDB"
  # goleveldb - default state database stored in goleveldb.
  # CouchDB - store state database in CouchDB
  stateDatabase: goleveldb
  couchDBConfig:
     # It is recommended to run CouchDB on the same server as the peer, and
     # not map the CouchDB container port to a server port in docker-compose.
     # Otherwise proper security must be provided on the connection between
     # CouchDB client (on the peer) and server.
     couchDBAddress: couchdb:5984
     # This username must have read and write authority on CouchDB
     username:
     # The password is recommended to pass as an environment variable
     # during start up (e.g. LEDGER_COUCHDBCONFIG_PASSWORD).
     # If it is stored here, the file must be access control protected
     # to prevent unintended users from discovering the password.
     password:
     # Number of retries for CouchDB errors
     maxRetries: 3
     # Number of retries for CouchDB errors during peer startup
     maxRetriesOnStartup: 10
     # CouchDB request timeout (unit: duration, e.g. 20s)
     requestTimeout: 35s
     # Limit on the number of records to return per query
     queryLimit: 10000

CouchDB被托管在docker容器中,经过使用Docker Compose脚原本利用Hyperledger Fabric设置CouchDB的username和password的能力,经过已有的环境变量设置,即COUCHDB_USER和COUCHDB_PASSWORD环境变量。

对于在使用Fabric提供的docker映像以外的CouchDB安装,本地安装。必须编辑ini文件以设置admin用户名和密码。

 

Docker Compose脚本只在建立容器时设置用户名和密码。若是要在容器建立后更改用户名或密码,则必须对本地.ini文件进行编辑。

注意:在每一个peer启动时都读取CouchDB的peer选项。

 

补充

Hyperledger Fabric 1.0 从零开始(九)——Fabric多节点集群生产启动中已经设置了peer和cli的启动配置,其中没有加入couchdb,这里补充下一个新的peer编写方法,但愿对各位有帮助。

后续在写到ca的时候也会将ca启动部分加入进来。

# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#

version: '2'

services:

  couchdb:
    container_name: couchdb
    image: hyperledger/fabric-couchdb
    # Comment/Uncomment the port mapping if you want to hide/expose the CouchDB service,
    # for example map it to utilize Fauxton User Interface in dev environments.
    ports:
      - "5984:5984"

  peer0.xxx.example.com:
    container_name: peer0.xxx.example.com
    image: hyperledger/fabric-peer
    environment:
      - CORE_LEDGER_STATE_STATEDATABASE=CouchDB
      - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb:5984

      - CORE_PEER_ID=peer0.xxx.example.com
      - CORE_PEER_NETWORKID=anti
      - CORE_PEER_ADDRESS=peer0.xxx.example.com:7051
      - CORE_PEER_CHAINCODELISTENADDRESS=peer0.xxx.example.com:7052
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.xxx.example.com:7051
      - CORE_PEER_LOCALMSPID=xxxMSP

      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      # the following setting starts chaincode containers on the same
      # bridge network as the peers
      # https://docs.docker.com/compose/networking/
      - CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=anti_default
      - CORE_LOGGING_LEVEL=ERROR
      # - CORE_LOGGING_LEVEL=DEBUG
      - CORE_PEER_GOSSIP_SKIPHANDSHAKE=true
      - CORE_PEER_GOSSIP_USELEADERELECTION=true
      - CORE_PEER_GOSSIP_ORGLEADER=false
      - CORE_PEER_PROFILE_ENABLED=false
      - CORE_PEER_TLS_ENABLED=false
      - CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt
      - CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key
      - CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt
    volumes:
        - /var/run/:/host/var/run/
        - ./crypto-config/peerOrganizations/xxx.example.com/peers/peer0.xxx.example.com/msp:/etc/hyperledger/fabric/msp
        - ./crypto-config/peerOrganizations/xxx.example.com/peers/peer0.xxx.example.com/tls:/etc/hyperledger/fabric/tls
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
    command: peer node start
    ports:
      - 7051:7051
      - 7052:7052
      - 7053:7053
    depends_on:
      - couchdb
    networks:
      default:
        aliases:
          - anti
    extra_hosts:
     - "orderer0.example.com:x.x.x.41"
     - "orderer1.example.com:x.x.x.42"
     - "orderer2.example.com:x.x.x.43"

  cli:
    container_name: cli
    image: hyperledger/fabric-tools
    tty: true
    environment:
      - GOPATH=/opt/gopath
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - CORE_LOGGING_LEVEL=ERROR
      # - CORE_LOGGING_LEVEL=DEBUG
      - CORE_PEER_ID=cli
      - CORE_PEER_ADDRESS=peer0.xxx.example.com:7051
      - CORE_PEER_LOCALMSPID=xxxMSP
      - CORE_PEER_TLS_ENABLED=false
      - CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/xxx.example.com/peers/peer0.xxx.example.com/tls/server.crt
      - CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/xxx.example.com/peers/peer0.xxx.example.com/tls/server.key
      - CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/xxx.example.com/peers/peer0.xxx.example.com/tls/ca.crt
      - CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/xxx.example.com/users/Admin@xxx.example.com/msp
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
    volumes:
        - /var/run/:/host/var/run/
        - ./chaincode/go/:/opt/gopath/src/github.com/hyperledger/fabric/antimoth/anti/chaincode/go
        - ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
        - ./scripts:/opt/gopath/src/github.com/hyperledger/fabric/peer/scripts/
        - ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
    depends_on:
      - peer0.xxx.example.com
    extra_hosts:
     - "orderer0.example.com:x.x.x.41"
     - "orderer1.example.com:x.x.x.42"
     - "orderer2.example.com:x.x.x.43"
     - "peer0.xxxx.example.com:x.x.xx.251"
相关文章
相关标签/搜索