Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Blockchain by (4.1k points)

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"
}

1 Answer

0 votes
by (14.4k points)

When a key is persisted into DB, it is prefixed with the name of the chaincode. In the above case, it is mycc and as a separator []byte{0x00} is used. 

To get the value of this key you can simply run a curl command by adding query parameter attachements=true, for example:

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

With this added, the output will be: 

--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--%

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...