Hoy to get all keys (not all key+values) in Hyperledger Fabric - hyperledger-fabric

I need to have a Hyperledger Fabric chaincode method that returns all keys in my world state to perform an integrity check. I can obviously get all states with getStateByRange() and iterate just to get the keys, method but I have seen that it effectively returns all data which overloads my system (I have about 20.000 objects in my world state)
Is there a way to get just the keys in a way that does not force Fabric to fetch all world state?

Currently the chaincode shim APIs does not support a GetKeys(). You should be able to use GetStateByRangeWithPagination() to avoid overloading your system while building your list of keys.

Related

Is there a hyperledger fabric Ctx.stub.putStateList() method or putState for multiple states

I am new to the latest Hyperledger fabric 2. Before deprecating fabric-composer (a very well thought and intuitive framework) if you wanted to add multiple assests you could call AssetRegistry.addAll() method to add not just one asset but an array of assets. Is there an equivalent in the new hlf version 2? If I have an array of assets/states that I want added in the world-state database and I call putState() multiple times for each state/asset is it not less efficient than calling a putStateList() method that would take as param an array of states? (BTW the old naming of asset from the times of hyperledger composer is much more intuitive than the new name of state ... composer vs de-composer :)))
There is no putStateList in Hyperledger Fabric.
As you mentioned, the only way is to call PutState the number of times you want.
In terms of performance, the data to be sent by the chaincode to the peer is the same length, but I understand that if Peer node could accept a list of states, it could be more efficient on the server-side.
If we look at the internals of Node.JS library, we can see that handlePutState connects to the peer and waits:
And then, it will send the message to the peer, writing the msg to the stream:

Using HLF peers for off ledger communication

I would like to leverage Hyperledger Fabric Peers (specifically identities) to communicate data that does not need to be recorded as a world state.
The Private Data Collection seems a step in that direction but everything is ordered and recorded.
Is there a way to send a payload between participants that does not get persisted?
Of course, every parameter sent is recorded in the channel's chain (but not in the world state unless your chaincode specifically does it).
Maybe you can save the payload (encrypted if you need it) in a distributed storage system (IPFS, for instance), share the IPFS hash/index via Fabric and delete from IPFS when it is no longer needed. I don't know if it fits your use case.

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.

Transaction hash generation in Hyperledger Composer

When a call is made from Hyperledger Composer to Fabric runtime, is the transaction hash generated only after the commit in the entire network. If thats the case, should we wait synchronously and track the data from traditional system. Like in ethereum cant we get a transaction hash immediately and track the commit status later.What is the best way of handling this in Hyperledger Fabric.
Yes, just like in ethereum we can get transaction hash inside the smart-contract/chaincode in HL fabric too, it doesn't matter if later transaction is committed or not.
For every invocation request, a transaction ID is generated, it can be accessed by a method
getTxId()
(in java chaincode, similarly can be found in go/node cc) which returns a String transaction id for that request.
refer Java chaincode getTxId

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.

Resources