1

I've installed Hyperledger fabric 2.0. I'm running the fabcar example in the fabric samples directory. The fabcar chaincode is using test-network as fabric-network. I want to know which stateDB is being used by fabcar example.

When I run command docker ps -a I'm getting output as couchdb as shown below.

bdf6370d6f5d        hyperledger/fabric-couchdb                                                                                                                                            "tini -- /docker-ent…"   16 minutes ago      Up 16 minutes              4369/tcp, 9100/tcp, 0.0.0.0:5984->5984/tcp   couchdb0



660ba3f7d2a4        hyperledger/fabric-couchdb                                                                                                                                            "tini -- /docker-ent…"   16 minutes ago      Up 16 minutes              4369/tcp, 9100/tcp, 0.0.0.0:7984->5984/tcp   couchdb1

But the chaincode is storing data in the form of key-value pair in fab-car chaincode. Key-value pair storage is for level-db

        await ctx.stub.putState(carNumber, Buffer.from(JSON.stringify(car)));

I'm really confused about whether I'm using level-db or state-db for fabcar example.

S C
  • 13
  • 2

2 Answers2

0

In Hyperledger Fabric's fabcar example, it's optional to have couchdb. According to that option, script adds docker-compose-couch.yaml file or not. As you stated that you already see couchdb containers, most likely your peers are configured for using couchdb.

Methods putState and getState these are common to any DB implementation underneath, since those are mandatory methods. However if you want to run rich queries, then key-value pair DB implementation is not sufficient, you need to use couchdb (or similar).

I think your confusion is how that rich queries work on simple key-value pairs. Briefly, queries are based on values. i.e. if you put such data ABC={"color":"red"} on ledger, then you can write a query based on color attribute. However this won't be possible with level-db (you'll get error on runtime).

Also, index definition on single or composite attributes is possible to improve query performance (see fabric-samples: marble example).

hsnkhrmn
  • 961
  • 7
  • 20
0

'State DB' = DB in which state is stored. This is terminology, not software.

Either 'LevelDB' or 'CouchDB' can act as state DBs (these are softwares). This depends on what switches are passed onto 'byfn.sh' in 'startFabric.sh' as of HLF 2.0

Following command in 'startFacric.sh' would result into 'CouchDB' being used as 'State DB'. remove '-s couchdb' from this, and the HLF would use 'LevelDB'.

echo y | ./byfn.sh up -a -n -s couchdb

Also in 'fabric-samples\first-network\docker-compose-couch.yaml' you'd have the following kind of entries which would say to which couch DB instance is your peer connected to.

peer0.org1.example.com:
environment:
  - CORE_LEDGER_STATE_STATEDATABASE=CouchDB
  - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb0:5984
  # The CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME and CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD
  # provide the credentials for ledger to connect to CouchDB.  The username and password must
  # match the username and password set for the associated CouchDB.
  - CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=
  - CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=
depends_on:
  - couchdb0

In this case, 'peer0.org1.example.com' is connected to 'couchdb0', on port 5984. Assuming the whole setup is running on 'localhost' following URL will show up the CouchDB instance, and you can Monkey around and see the 'state' saved to it too.

http://localhost:5984/_utils/

Assuming your channel name is 'mychannel' and the chaincode name is 'mycc', you'll have a database named 'mychannel_mycc' in 'CouchDB'. This holds world state for 'mycc' chain code.

Siva
  • 598
  • 3
  • 11