Hyperledger Fabric中的多个智能合约 [英] Multiple Smart Contracts in Hyperledger Fabric

查看:809
本文介绍了Hyperledger Fabric中的多个智能合约的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Hyperledger Fabric的同一应用程序中实现多个智能合约?哪个配置文件包含实现此目的的设置?

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 ?

推荐答案

我将使用Hyperledger Fabric 1.4版中的Fabcar示例来回答您的问题.我的答案是基于Javascript实现的.

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

如果您看到文件夹结构,则会看到存在一个Chaincode文件夹,其中包含fabcar.js(chaincode/fabcar/javascript/lib/fabcar.js)链码.

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.

在此特定的链码中,只有一个智能合约,即FabCar,您可能会观察到该文件仅导出一个合约.

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

如果要添加更多智能合约,只需使用相同的链码创建另一个Class,即可扩展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;

您需要对index.js(chaincode/fabcar/javascript/index.js)进行一些更改.

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 ];

现在,对于客户端应用程序,您可以参考Fabar客户端实现. (fabric-sample/fabcar/javascript/invoke.js).下面我展示了一个有关如何轻松调用这些不同合约的示例.

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.

示例:

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

希望您理解了整个示例.如果您需要更多说明,请发表评论.我没有涉及如何安装和实例化chaincode.为此,您可以通过@Artem引用答案.但是他的方法是在同一频道上安装2个链码.我不确定,这也可能有用.但这是一种只有一个Chaincode的简单方法.

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.

这篇关于Hyperledger Fabric中的多个智能合约的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆