Multiple Smart Contracts in Hyperledger Fabric

蓝咒 提交于 2020-02-01 08:53:19

问题


How can I implement more than one smart contract in the same application of Hyperledger fabric ? Which configuration file contains the settings to achieve this ?


回答1:


I will answer your question using the Fabcar example from the Hyperledger Fabric version 1.4. And my answer is based on Javascript Implementation.

If you see the folder structure you would see that there is a Chaincode folder which contains the fabcar.js(chaincode/fabcar/javascript/lib/fabcar.js) chaincode.

A smart contract is defined within a chaincode. Multiple smart contracts can be defined within the same chaincode. When a chaincode is deployed, all smart contracts within it are made available to applications.

In this particular chaincode there is only one smart contract i.e FabCar.And you might observe the file only exports one contract.

If you want to add more smart contracts, Just create another Class in the same chaincode, which extends the Contract class and then export it. Below I have shown an example.

  class FabCar extends Contract {
     ...
     ...
     ...
   } 
   class NewContract1 extends Contract {
     ...
     ...
     ...
   } 
   class NewContract2 extends Contract {
     ...
     ...
     ...
   } 

   module.exports.FabCar = FabCar;
   module.exports.NewContract1 = NewContract1;
   module.exports.NewContract2 = NewContract2;

You need to make a few changes in the index.js (chaincode/fabcar/javascript/index.js).

'use strict';

const FabCar = require('./lib/fabcar').FabCar;
const NewContract1 = require('./lib/careonlineChaincode').NewContract1;
const NewContract2 = require('./lib/careonlineChaincode').NewContract2;
module.exports.FabCar = FabCar;
module.exports.NewContract1 = NewContract1;
module.exports.NewContract1 = NewContract2;
module.exports.contracts = [ FabCar, NewContract1, NewContract2 ];

And now for the Client Application, you can refer to the Fabar Client Implementations. (fabric-sample/fabcar/javascript/invoke.js). Below I have shown an example on how you can easily call these different contracts.

const contract = network.getContract('fabcar', 'FabCar');
//getContact(chaincode name, contract name)
const newContract1 = network.getContract('fabcar', 'NewContract1');
const newContract2 = network.getContract('fabcar', 'NewContract2');

Now you can individually call these contracts and use their transactions.

example:

await contract.submitTransaction('someTrancsaction', 'someData');
await newContract1.submitTransaction('someTrancsactionFromThisContract', 'someDataFromHere');
await newContract2.submitTransaction('differentTransacation', 'randomdata');

Hope you understood the entire example. If you need more clarification please comment. I didn't go to touch upon how to install and instantiate the chaincode. For that, you can refer the answer by @Artem. But his approach was installing 2 chaincodes on the same channel. That might also work, I'm not sure. But this is a simple approach with only one Chaincode.




回答2:


Let me clarify a bit on Sergio answer:

First of all you need to be aware that to run multiple smart contracts in same blockchain you have to create a custom channel between peers PER smart contract.

This is not precisely correct as you are not required to install chaincode (smartcontract) PER channel. It's completely up to your specific application, design, trust model and business case requirements. One channel could have as many chaincodes instantiated as need. The procedure to achieve this is fairly simple, you just need to install all required chaincodes into peer and instantiate each one of them on the channel you going to use them. For example you can install two chaincodes which aims to interact within scope of single channel say "MyChannel" the following way:

  1. Install first chaincode

    peer chaincode install -o localhost:7050 -n myCC1 -v 1.0 -p github.com/chaincode/mychaincode1

  2. Install second chaincode

    peer chaincode install -o localhost:7050 -n myCC2 -v 1.0 -p github.com/chaincode/mychaincode2

  3. Instantiate both

    peer chaincode instantiate -o localhost:7050 -n myCC1 -v 1.0 -C MyChannel -c '{"Args":[]}'

    peer chaincode instantiate -o localhost:7050 -n myCC2 -v 1.0 -C MyChannel -c '{"Args":[]}'

Thus you will have two chaincodes myCC1 and myCC2 installed and instantiated in the scope of channel MyChannel.

Please follow official tutorial and consider to get connected to Rocket.Chat to get more help from the expert community.




回答3:


Taking the same FabCar example and assuming the following:

1) you want one chaincode with multiple smart contracts and

2) each smart contract for that chaincode would be in a separate file.

To implement the solution noted above you would do the following given you have the same chaincode structure and application structure as shown in Hyperledger/fabric-samples.

Note, that you can have multiple chaincodes too, but I will not focus on that here. To read more about multiple chaincodes, go here.

The below code snippets for javascript chaincode language.

File 1: lib/fabcarcontract.js

class FabCarContract extends Contract {
   constructor() {
        // Unique smart contract name when multiple contracts per chaincode
        super('FabCarContract');
   }
   // ... (add smart contract functions here)
}
module.exports = FabCarContract;

File 2: lib/newcontract1.js

class NewContract1 extends Contract {
   constructor() {
       // Unique smart contract name when multiple contracts per chaincode
       super('NewContract1');
   }
  // ... (add smart contract functions here)
}
module.exports = NewContract1;

File 3: lib/newcontract2.js

class NewContract2 extends Contract {
   constructor() {
        //Unique smart contract name when multiple contracts per chaincode
        super('NewContract2');
   }
   // ... (add smart contract functions here)
}
module.exports = NewContract2;

Now we must reference these contracts within our index.js file which is at the root level of the smart contract directory.

Index file: index.js

'use strict';

const FabCarContract = require('./lib/fabcarcontract');
const NewContract1 = require('./lib/newcontract1');
const NewContract2 = require('./lib/newcontract2');

module.exports.FabCarContract = FabCarContract;
module.exports.NewContract1 = NewContract1;
module.exports.NewContract2 = NewContract2;
module.exports.contracts = [ FabCarContract, NewContract1, NewContract2 ];

Note that our name field within our package.json file creates our chaincode name and that the chaincode name is the default smart contract name if there is only one smart contract defined. Since we have multiple, we must reference each smart contract within our chaincode by referencing fabcar. NewContract1 and only then we can use the functions defined in our NewContract1 file.

Let's look at an example of how we would reference our contract now.

In our application folder -> given we have the proper credentials/wallet structure to access our smart contract, we can check the invoke.js file to see how to actually invoke different smart contract within our chaincode.

invoke.js

const network = await gateway.getNetwork('mychannel');

// first we get the chaincode i.e. fabcar, 
// then reference which smart contract within that chaincode we want. 
// here we do NewContract1 but it could have easily been NewContract2 or FabCarContract 
// because we have defined all three of those in our chaincode folder
const contract = network.getContract('fabcar', 'NewContract1');

// now we have access to all functions within NewContract1 file.
await contract.submitTransaction(.....);

Now, if we wanted to gain access to functions defined in NewContract2 we would just change the code above to get contract for a smart contract named NewContract2 and so on.




回答4:


First of all you need to be aware that to run multiple smart contracts in same blockchain you have to create a custom channel between peers. In that channel, as many as requested smart contract can be installed

Just following the same procedure to compile, instantiate and run the mart contract over one channel, you just need to repeat this process for every smart contract you want to use



来源:https://stackoverflow.com/questions/48334334/multiple-smart-contracts-in-hyperledger-fabric

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!