Sebin Benjamin
Sebin Benjamin

Reputation: 1898

How is data store in Hyperledger Fabric when using CouchDB as the Ledger State database?

I'm running the code in Chaincode for Developers Tutorial, to run a basic sample chaincode to create assets (key-value pairs) on the ledger.

I'm able to invoke the chaincode using the cli

peer chaincode invoke -n mycc -c '{"Args":["set", "a", "20"]}' -C myc

and also run queries

peer chaincode query -n mycc -c '{"Args":["query","a"]}' -C myc

Now I want to see how the key value pair gets stored in CouchDB. So I changed the environment variables below in the fabric-samples/chaincode-docker-devmode/docker-compose-simple.yaml

CORE_LEDGER_STATE_STATEDATABASE=CouchDB
CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb0:5984

I see the documents created like below in CouchDB UI (http://localhost:5984/myc/_all_docs) when I run set.

{
  "total_rows": 3,
  "offset": 0,
  "rows": [{
      "id": "lscc\u0000mycc",
      "key": "lscc\u0000mycc",
      "value": {
        "rev": "1-dc6dc8ff92efd35358cf5b89e7949c25"
      }
    },
    {
      "id": "mycc\u0000a",
      "key": "mycc\u0000a",
      "value": {
        "rev": "3-7ad1349ec711a99a2a2f1dd1c8b08a20"
      }
    },
    {
      "id": "statedb_savepoint",
      "key": "statedb_savepoint",
      "value": {
        "rev": "6-2c3d131fc75772cc9e70311998bdde9d"
      }
    }
  ]
}

How/Where is the value for the key stored and retrieved? It is seen as below, when checking the document in the DB, but is retrieved properly when running the chaincode get query.

"value": {
  "rev": "3-7ad1349ec711a99a2a2f1dd1c8b08a20"
}

When

Upvotes: 4

Views: 6702

Answers (2)

Pranoy Gn
Pranoy Gn

Reputation: 509

Since the data is saved in binary, you won’t find exact values(instead you will find hashes) but will see the records having key containing mycc

Upvotes: 2

Artem Barger
Artem Barger

Reputation: 41222

While key persisted into DB it is prefixed with name of the chaincode, in your example it's mycc and as a separator used []byte{0x00} value. Therefore you see in you example, following output:

{
  "id": "mycc\u0000a",
  "key": "mycc\u0000a",
  "value": {
    "rev": "3-7ad1349ec711a99a2a2f1dd1c8b08a20"
  }
},

which stands for key a of chaincode mycc. To get the value of this key you can simply run a curl command as following adding query parameter attachements=true, for example:

curl -X GET "http://localhost:5984/mychannel/mycc%00a?attachments=true"

will result with something similar to this:

--bdb0a91d2e233fdc193f2359e6a50472
Content-Type: application/json

{"_id":"mycc\u0000a","_rev":"2-2af72e502c2b43c73064728852103fbf","chaincodeid":"mycc","version":"4:0","_attachments":{"valueBytes":{"content_type":"application/octet-stream","revpos":2,"digest":"md5-qpvq4/JGMCgu7WtvFu5zbg==","length":2,"follows":true,"encoding":"gzip","encoded_length":22}}}
--bdb0a91d2e233fdc193f2359e6a50472
Content-Disposition: attachment; filename="valueBytes"
Content-Type: application/octet-stream
Content-Length: 22
Content-Encoding: gzip

4鯄i
--bdb0a91d2e233fdc193f2359e6a50472--%

For more information about how to read data from CouchDB you might find following tutorial useful.

Upvotes: 4

Related Questions