Does anyone know how I can deploy multiple smart contracts in javascripts in one chaincode and invoke all smart contracts with different files in CouchDB?
Smart contracts are essentially different functions within your chaincode file and you can have multiple smart contracts within the same chaincode. You have to create a class which extends the Contract class from fabric-contract-api, and define your functions inside. Finally, you need to export your class.
A simple example of this can be found here: https://github.com/hyperledger/fabric-samples/tree/main/asset-transfer-basic/chaincode-javascript.
Invoking smart contracts can be done by using applications.
A javascript application example could be found here: https://github.com/hyperledger/fabric-samples/tree/main/asset-transfer-basic/application-javascript
One chaincode install and instantiation would result into one database (I think this is what you meant by one file) within couch DB. All putState and getState would have the data put and got from this database within CouchDB. If a second chaincode is installed in the HLF network with a different name, then a new DB is created in CouchDB and all data put and got by this second chaincode would be from the second database. This is how HLF ensures that two chaincodes dont see each other's data as they are maintained separate to begin with.
Now, as said in above answer, each smart contract is a function in chaincode. You can author all of your API/ functions in one single chaincode, or start writing single chaincode for each function. This changes the number of databases created in your CouchDB by HLF, but it really depends on what you want to do.
Related
I have been researching online a lot, and I have hit a roadblock.
My current situation is that I have a hash inside my blockchain, and this would reference a file that will return this from off chain. My question is that would using IPFS and Storj even be possible using Hyperledger Composer? If so, how would the integration process work? My case right now would require the use of Hyperledger due to its architecture over Ethereum, so it is much preferable to use Hyperledger.
Besides this, I would also like to create a custom front end. Is it possible to work on custom CSS and code, for example, using Angular?
yes - firstly see storage options discussed in this thread -> Best practice to save files in blockchain -
In Composer you could store the hash as a string on an asset in your model etc etc and you can retrieve via IPFS client search etc once you've got your asset (eg by name or ID) etc .- lastly see this announce for current status of Composer FYI
The Composer's model consists of javascript code, entities, query definitions, and access rules. As I understand, everything becomes some kind of executable code after deployment, since Fabric doesn't have anything like that.
What exactly is happening during the deployment process? Is everything compiled into a sort of bytecode? For example, Composer's transaction functions are somehow becoming a part of the Fabric's Invoke method, but how exactly one maps onto the other?
I am asking because I want to add a capability to Composer to run js-based queries, and they need to be somehow mapped to the Fabric's Query method.
You need to study the architecture of Hyperledger Composer.
What you are looking for is the composer-runtime which interact with the underlaying Fabric.
Check out these slides :
link
I'm studying Hyperledger Fabric with the documentation(https://hyperledger-fabric.readthedocs.io/en/release-1.0/write_first_app.html)
I'm done with two samples, which is "Building Your First network" & "Writing Your First Application"
I'm also done adding 1 extra peer to each organization, by modifying certain files, as well as done trying all commands on "Writing Your Fist Application" session.
Now, I'd like to execute the same commands(e.g. Querying all cars, Adding new cars or whatever) on the first network where I have built up, not on the test Fabcar network.
The thing is that I really have no idea what to do and how to do, even though I know how to handle NodeJS program(by Writing Application webpage)
So I'd like to ask you some questions.
Should I modify some files in order to "move" all necessary things to my network? if so, which file should I modify?
By any chance, Could you please tell me the correct steps to make it? I feel like I need to install and instantiate the required smart contract on my peers. Am I right?
I really appreciate your help in advance.
To answer your question, you would need to read some documentation online that will help you understand the architecture and how you can build up the packages as hyperledger fabric provided freedom to users to create use case specific configurations.
To start with.
Make sure you have have understood the concept of peer, orderer, couchdb, ca authority. Those 4 things you need to play with most of the times.
Assuming you have installed nodejs and able to run node through terminal, Read through the following example
Tuna Fish example, it help you to understand the concept of injecting, updating, querying blockchin ledger. Also, It will help understand the usage of Nodejs backend and angularjs basic UI. or use the other examples as you have seen on the fabric-samples github repository. Fabric Samples. Balance Transfer will help you understand the channel and chaincode operations using Node JS. Build your first network will help you understand the configuration files(concentrate on docker-compose.yaml, scipt.js and byfn.js)
Then to answer your questions.
you just have to modify the mount drive config variables in docker-componse-cli.yaml. Then you can edit your startup script based on how you want to move chaincode to your peer.
you need to install chaincode on all the peers that are part of that channel. And you only need to instantiate chaincode once per channel.
Installation and instantiation combination is a powerful feature because it allows for a peer to interact with the same chaincode container across multiple channels. The only prerequisite is for the actual chaincode source files to be installed on the peer's file system. As such, if a piece of common chaincode is being used across dozens of channels, a peer would need only a single chaincode container to perform read/writes on all the channel ledgers.
To run the node js files on the fabric network.
welcome to blockchain world :)
I am developing a business network with hyperledger composer. I have followed the tutorial on composer web site at composer playground and have made the tutorial-network as well. Now I am facing one issue. after finishing defining your business network, how is it possible to get the underlying Fabric code that is generated? I can't think of a reason why it should be necessary to have it directly, but lets say if for any reason, like having to give Fabric code to your customer for whom you have developed a poc, you need to have that. Is Fabric code being saved somewhere? I have looked into the business network folder; there are only subfolders for models, test, the business network itself, and after you generate the archive file there is only one file with .bna extension. Nothing related to Fabric there!
I appreciate your help.
As part of the deployment of your business network, an associated Composer runtime is included that can parse model files, query file and acl files, it is also capable of executing your transaction processor functions. In 0.16.x this was achieved by including a JavaScript VM implementation using Go Chaincode to execute the Composer runtime (written in JavaScript) and the transaction processor functions. In Fabric 1.1, chaincode can be written natively as it now supports Node.js chaincode so with Composer beyond 0.16.x (currently 0.19.x at the time of writing) Composer utilizes this and all Go chaincode it used to contain was removed, along with the Javascript VM. It's the composer runtime that parses the model, query and acl files, and the transaction processor functions run natively inside the node.js fabric chaincode container. Nothing is ever compiled to Go.
The smart contract will be saved in different manner in fabric, as composer is just an abstraction over it. You need to search for the location where the chaincode is present inside the peer. There are some chaincode related commands which can help you in finding out where the chain code is installed. And to the client you can give the .bna file if you are using composer for the development because I am not sure whether the code will be easily readable at fabric level if you are using composer.
I'm wondering how you can get a hyperledger fabric smart contract to read from external API's ?
A service called oraclize (http://www.oraclize.it/) is used to do this operation in Ethereum blockchains - Is there anything similar for hyperledger fabric smart contracts or any workarounds?
In Hyperledger Fabric - smart contracts (the chaincode), could be implemented at the moment with either golang or Java, both of which are Turing complete and provides you with reach set of libraries and frameworks. Among which you can you any network library to get connected with external services, while you need to be extra cautious doing this to make sure your chaincode is deterministic (e.g. two independent executions of the same chaincode calling same external API will get same result back).
Riccardo from Oraclize here.
We are actually working on a Oraclize integration with Hyperledger.