Hyperledger CouchDB asset values - couchdb

I have a question regarding using CouchDB as world state DB in Hyperledger Fabric. When I setup CouchDB as my state DB, I can see the database collection with the name same as my channel name, and I can see all transactions executed among my chain-code.
Where (if that is possible) can I see the individual values for asset created within chain-code transactions? Are they stored within CouchDB?
For example when I try to instantiate new marbles in example explained here: https://hyperledger-fabric.readthedocs.io/en/latest/build_network.html#using-couchdb , I am able to see my transactions within CouchDB, but I cannot see the individual values for created mrbles.
Thank you for the answers.

Value stored in state DB prefixed with chaincode name, e.g. for example key1 for chaincode mycc will look in DB as following: mycc%00key1. In order to query for key value you can do it by simply running curl command as following:
curl -X GET "http://localhost:5984/mychannel/mycc%00key1?attachments=true"
You can see more information about how to read values from CouchDB here.

Related

How to view the data stored on transaction log?

I'm using hyperledger fabric 2.0. I have successfully ran chaincodes on the fabric network and my data is storing on the couch db. I'm viewing the data stored on the couch DB using
But I want to prove to my faculty that the data is being stored on the transaction log too. Is there any way to view the data that is stored on the transaction log?
If I understand it correctly you basically want to retrieve the ledger data while you're already able to access the world state (couch-db).
One way could be, depending upon what SDK you're using, you can use an instance of 'Channel' and run queries against the ledger data.
Following are the methods available in NodeSDK which could help you to run queries against your ledger:
queryBlock
queryBlockByHash
queryBlockByTxID
queryTransaction

Where are Hyperledger Fabric events stored?

I understand there is world state and ledger.
1) World state is stored in Level DB or Couch DB
2) Ledger is stored in the form of LevelDB - /var/hyperledger
How about the events ? Where are they stored ? Especially what will happen when a new peer needs to pull the existing events. I was not able to find any documentation. It will be nice if someone can point me to the right information
A peer can't subscribe to historical events, and they are not stored anywhere in Fabric.
Hyperledger Composer stores transaction details (including events) in the Historian, which you can query to extract event details. See this answer for an example.

Best Practices to follow while writing Hyperledger Fabric Chaincode

What should be some of the best practices to follow to avoid bugs and write efficient Hyperledger Fabric Chaincode?
General Guidelines for writing Hyperledger Fabric Chaincodes.
Refer to the below link for a detailed description on the same:
https://gist.github.com/arnabkaycee/d4c10a7f5c01f349632b42b67cee46db
Some steps are concisely mentioned below:
Use Chaincode DevMode
Use Chaincode Logging
Using logging is simple and easy. Use Fabric's inbuilt logger. Fabric provides logging mechanism as follows:
For Golang: https://godoc.org/github.com/hyperledger/fabric/core/chaincode/shim#ChaincodeLogger
For NodeJS: https://fabric-shim.github.io/Shim.html#.newLogger__anchor
For Java: You can use any standard logging framework like Log4J
Avoid using Global Keys - Hyperledger Fabric uses an Optimistic Locking Model while committing transactions. In the two-stage process of endorsement & committment, if some versions of the keys that you had read in the Endorsement has changed till your transactions reach the committing stage, you get an MVCC_READ_CONFLICT error. This often is a probability when one or more concurrent transactions are updating the same key.
Use Couch DB Queries wisely
Couch DB Queries DO NOT alter the READ SET of a transaction -
Mongo Queries are for querying the Key Value store aka StateDB only. It does not alter the read set of a transaction. This might lead to phantom reads in the transaction.
Only the DATA that you have stored in the couchDB is searchable - Do not be tempted to search for a key by its name using the MangoQuery. Although you can access the Fauxton console of the CouchDB, you cannot access a key by querying a key by which it is stored in the database. Example : Querying by channelName\0000KeyName is not allowed. It is better to store your key as a property in your data itself.
Write Deterministic Chaincode - Never write chaincode that is not deterministic. It means that if I execute the chaincode in 2 or more different environments at different times, result should always be the same, like setting the value as the current time or setting a random number. For example: Avoid statements like calling rand.New(...) , t := time.Now() or even relying on a global variable (check ) that is not persisted to the ledger.
This is because, that if the read write sets generated are not the same, the Validation System chaincode might reject it and throw an ENDORSEMENT_POLICY_FAILURE.
Be cautions when calling Other Chaincodes from your chaincode. - Invoking a chaincode from another is okay when both chaincodes are on the same channel. But be aware that if it is on the other channel then you get only what the chaincode function returns (only if the current invoker has rights to access data on that channel). NO data will be committed in the other channel, even if it attempts to write some. Currently, cross channel chaincode chaincode invocation does not alter data (change writesets) on the other channel. So, it is only possible to write to one channel at a time per transaction.
Remember to Set Chaincode Execution Timeout - Often it might so happen that during high load your chaincode might not complete its execution under 30s. It is a good practice to custom set your timeout as per your needs. This is goverened by the parameter in the core.yaml of the peer. You can override it by setting the environment variable in your docker compose file :
Example: CORE_CHAINCODE_EXECUTETIMEOUT=60s
Refrain from Accessing External Resources - Accessing external resources (http) might expose vulnerability and security threats to your chaincode. You do not want malicous code from external sources to influence your chaincode logic in any way. So keep away from external calls as much as possible.

Query from ledger in hyperledger fabric

I am able to save my data in CouchDB.As per my understanding, the ledger(stored inside blocks) contains the history of changes.
Is there any way to query that ledger?
How will I know which block I need to query (There are 100s of blocks and the data is in particular ledger in the block) Basically, I just want to see a history of particular data from the block and I do not know the block number.
Is it possible?
Any help/suggestion/comment would be appreciated.
Using the Node.js SDK you can query the ledger in multiple ways:
Query via chaincode: In this you write your code in the chaincode to query the ledger and call this using the Node.js SDK. This is when you don't have the TX ID or the Block ID.
Follow the query method in this example to write the query chaincode: https://github.com/hyperledger/fabric-samples/blob/release-1.1/balance-transfer/artifacts/src/github.com/example_cc/node/example_cc.js
Query Couch DB directly: In this you write the SDK code to query the ledger directly without going through the chaincode. This is when you know the TX ID or the Block ID.
Follow this example to write the SDK for the queries: https://github.com/hyperledger/fabric-samples/blob/release-1.1/balance-transfer/app/query.js
You can install composer-rest-server. It will start loopback server and exposes rest APIs for all the assets and historian. You can query historian to get all the transactions.

How to regenerate CouchBD data from ledger

I am using fabric 1.1 Alfa and running a 3 Organizations network in a single channel. My three Organizations are Manufacturer, Transporter and Retailer with 1 peer each. I can go to any of my couch Db node and modify any key-value pair eg (I change value of amount) but what I do not get here is how would I know which org has the corrupted or modified data? or who did that? Because on the client side we show the state data only, which is modifiable.
is there any way i can repopulate couchDB data from ledger or get to know who tried to corrupt the data?
One (indirect) way to do this, would be to join a new peer to the network with his own couchdb attached. When new blocks are gossiped to this new peer, the peer automatically updates the couchdb until he receives the latest block. Then you can have a look at the couchdb world state which will be the "correct" state.

Resources