Query from ledger in hyperledger fabric - 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.

Related

How to use Amazon DynamoDB instead of couchDB for store off-chain data in hyperledger fabric

I want to implement Off-Chain data in hyperledger fabric I checked https://github.com/hyperledger/fabric-samples/tree/release-1.4/off_chain_data however instead of couch_DB I want to implement on Amazon DynamoDB.can we do that or there is no option to replace couchDB?
Any help will be great for me.
Thanks
You can be using any DB to store off-chain data.
Let see the process of this example do.
Listen to block events in code (document about addBlockListener function)
If detect a new block added, it will get data of this block and save to DB you config by call function writeValuesToCouchDBP. You should modify this to using Amazon DynamoDB (or any DB you want) instead of CouchDB

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

I would like to know if you can import files into Hyperldeger Fabric

I am new in this field and therefore I am still doing studies and researches, I would like to know if JSON files can be imported in Hyperldeger Fabric--if it is better Hyperledeger Fabric or Fabric Composer. more precisely I would like to understand if there is a way to populate the DLT of Hyperledger Fabric automatically.
for now, I have only tried Hyperledger Composer online playground
Fabric don't have any feature to automatically populate the ledger.
You have to develop a solution in order to upoload each Json file and put that on the ledger state.
Any type of data can be inserted on the ledged because it stores byte arrays so its up to you how to serialize.
in case you're asking to making your chaincode or smart contract like talking to the file system and read file or even call some API to collect JSON files,
it could be done but this will break your transaction flow specially during the endorsement process due to during the endorsement process it's expected from each peer to return the same value after executing the transaction against the chaincode to consider the transaction is a valid transaction,
so in case one of the endorsers failed to call the API or failed to read file from file system the transaction will considered to be invalid.
so it's not recommended to do any third party activity in your chaincode or smart contract even if it's possible to do so.
about populating the ledger it can be done it's eventually a database so you can dump it's data, However, if you're trying to backup to recover the ledger in case the whole network down it's impossible due to when you'll reinstall the network the whole config and certificates which were bounded to the transaction will be changed so it has no sense to do it.

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.

Hyperledger CouchDB asset values

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.

Resources