Store non-strings in Hyperledger Fabric 1.4

Viewed 260

From what I'm reading, it is only possible to pass string values from the Node SDK to the chaincode via submitTransaction.

However, from the chaincode point of view, there is no issue with creating an asset containing, say, booleans and integers and store them.

Hence my question: is storing non-string attributes considered a bad practice, and if not, what is the best way to implement it? Of course I can send strings that the smart contract will then convert back to bool/ints, but that sounds quite ugly. I also guess that instead of sending ...args I can send a JSON, but again, quite ugly.

3 Answers

You are always going to need to do some type of conversion | serialization/deserialization between the client and the actual chaincode itself.

Chaincode itself only accepts arrays of bytes as arguments. If you have simple/primitive types for your arguments (string, int, boolean, etc), then passing them as strings from the client and converting them in the chaincode itself is the most straightforward approach. I admit it's not super clean, but given we support clients and chaincode in multiple languages and allow you to mix and match, we can't use a language's native serialization for types.

Okay, this is from JAVA HLF implementation standpoint. Assuming Node JS and Java have the same capabilities, the 'putState' method takes in either 'byte' or 'String' data types, and putState is the only method as far as I know to put data to ledger. So, you cannot put anything other than String/ byte in my view. So, although it is ugly, I think putting String or bytes to ledger is the only way to put and get data (at least as of HLF 1.4).

All data written to ledger would actually get stored onto 'CouchDB' behind the scenes, so if you write JSON strings to ledger, you can later write 'CouchDB query' and get the data from ledger too (just like how you use SQL to get data from traditional RDBMS).

The point is you want to store non-string data types to the ledger.But right now,the Node JS / Java SDK only supports the putState method only takes string or bytes.

https://hyperledger.github.io/fabric-chaincode-node/release-1.4/api/fabric-shim.ChaincodeStub.html

For example, if you want to put Number or boolean on the putState method.So on the SDK side at the submitTransaction ,you have to JSON.stringify the input data (if it's not a String data type).Unfortunately,this is the only way we have right now.

Related