Hyperledger Fabric v1.4(5.6.1):Developing Applications- Application design elements-Contract Names

Hyperledger Fabric v1.4(LTS) 系列(5.6.1):Developing Applications- Application design elements-Contract Names

5.6是一个大章节,连文章标题都长到必须要删掉一些词才可以写全了。

Application design elements

This section elaborates the key features for client application and smart
contract development found in Hyperledger Fabric. A solid understanding of
the features will help you design and implement efficient and effective
solutions.

这个章节开始较为复杂的应用和合约开发。

包括

  • contractname
  • chaincodenamespace
  • transactioncontext
  • transactionhandler
  • endorsementpolicies
  • connectionprofile
  • connectionoptions
  • wallet
  • gateway

Contract names

Audience: Architects, application and smart contract developers,
administrators

A chaincode is a generic container for deploying code to a Hyperledger Fabric
blockchain network. One or more related smart contracts are defined within a
chaincode. Every smart contract has a name that uniquely identifies it within a
chaincode. Applications access a particular smart contract within an
instantiated chaincode using its contract name.

链码是部署代码到Fabric网络的通用容器。一个链码会有多个智能合约,链码中的合约需要独一无二的标识符,应用通过合约名称访问合约。

In this topic, we’re going to cover:

Chaincode

In the Developing Applications topic, we can
see how the Fabric SDKs provide high level programming abstractions which help
application and smart contract developers to focus on their business problem,
rather than the low level details of how to interact with a Fabric network.
前边讲到,Fabric SDK提供了高层编程抽象让应用合约开发者关注商业问题。

Smart contracts are one example of a high level programming abstraction, and it
is possible to define smart contracts within in a chaincode container. When a
chaincode is installed and instantiated, all the smart contracts within it are
made available to the corresponding channel.
智能合约是抽象的一个例子,一个链码容器可以有多个合约。一个链码安装并实例化以后,其内部合约对相应通道都可以访问。

[外链图片转存失败(img-EZWUpkCF-1564282109578)(https://hyperledger-fabric.readthedocs.io/en/release-1.4/_images/develop.diagram.20.png)]
Multiple smart contracts can be defined within a chaincode. Each is uniquely identified by their name within a chaincode.

In the diagram above, chaincode A has three smart contracts defined within it, whereas chaincode B has four smart contracts. See how the chaincode name is used to fully qualify a particular smart contract.

The ledger structure is defined by a set of deployed smart contracts. That’s because the ledger contains facts about the business objects of interest to the network (such as commercial paper within PaperNet), and these business objects are moved through their lifecycle (e.g. issue, buy, redeem) by the transaction functions defined within a smart contract.
账本的结构是被一系列智能合约定义的,因为账本包括业务对象的事实,且业务对象被智能合约中的交易函数操作而在其生命周期中变动。

In most cases, a chaincode will only have one smart contract defined within it. However, it can make sense to keep related smart contracts together in a single chaincode. For example, commercial papers denominated in different currencies might have contracts EuroPaperContract, DollarPaperContract, YenPaperContract which might need to be kept synchronized with each other in the channel to which they are deployed.
一般情况下,一个链码只有一个合约。但是很多时候为了多个合约的同步更新,要把他们放在同一个链码里。

Name

Each smart contract within a chaincode is uniquely identified by its contract name. A smart contract can explicitly assign this name when the class is constructed, or let the Contract class implicitly assign a default name.
类被结构化时合约分配名称,或者合约类显式分配一个默认名称。

Examine the papercontract.js chaincode file:

class CommercialPaperContract extends Contract {

    constructor() {
        // Unique name when multiple contracts per chaincode file
        super('org.papernet.commercialpaper');
    }

See how the CommercialPaperContract constructor specifies the contract name as org.papernet.commercialpaper. The result is that within the papercontract chaincode, this smart contract is now associated with the contract name org.papernet.commercialpaper.

If an explicit contract name is not specified, then a default name is assigned – the name of the class. In our example, the default contract name would be CommercialPaperContract.

通过声明,在papercontract链码里,合约分配了名称org.papernet.commercialpaper,或者分配一个默认的名称CommercialPaperContract

Choose your names carefully. It’s not just that each smart contract must have a unique name; a well-chosen name is illuminating. Specifically, using an explicit DNS-style naming convention is recommended to help organize clear and meaningful names; org.papernet.commercialpaper conveys that the PaperNet network has defined a standard commercial paper smart contract.
定义名称要认真,每个合约要有唯一名称。推荐用类似DNSname风格来命名。

Contract names are also helpful to disambiguate different smart contract transaction functions with the same name in a given chaincode. This happens when smart contracts are closely related; their transaction names will tend to be the same. We can see that a transaction is uniquely defined within a channel by the combination of its chaincode and smart contract name.
合约名也帮助避免交易函数名混淆。

Contract names must be unique within a chaincode file. Some code editors will detect multiple definitions of the same class name before deployment. Regardless the chaincode will return an error if multiple classes with the same contract name are explicitly or implicitly specified.

Application

Once a chaincode has been installed on a peer and instantiated on a channel, the smart contracts in it are accessible to an application:

const network = await gateway.getNetwork(`papernet`);

const contract = await network.getContract('papercontract', 'org.papernet.commercialpaper');

const issueResponse = await contract.submitTransaction('issue', 'MagnetoCorp', '00001', '2020-05-31', '2020-11-30', '5000000');

See how the application accesses the smart contract with the contract.getContract() method. The papercontract chaincode name org.papernet.commercialpaper returns a contract reference which can be used to submit transactions to issue commercial paper with the contract.submitTransaction() API.

Default contract

The first smart contract defined in a chaincode is the called the default smart contract. A default is helpful because a chaincode will usually have one smart contract defined within it; a default allows the application to access those transactions directly – without specifying a contract name.
链码里第一个合约为默认合约,默认合约允许应用直接访问交易而不用指定合约。

译注
类似java里的默认构造函数

[外链图片转存失败(img-HDqaDEHM-1564282109579)(https://hyperledger-fabric.readthedocs.io/en/release-1.4/_images/develop.diagram.21.png)]

A default smart contract is the first contract defined in a chaincode.

In this diagram, CommercialPaperContract is the default smart contract. Even though we have two smart contracts, the default smart contract makes our previous example easier to write:

const network = await gateway.getNetwork(`papernet`);

const contract = await network.getContract('papercontract');

const issueResponse = await contract.submitTransaction('issue', 'MagnetoCorp', '00001', '2020-05-31', '2020-11-30', '5000000');

This works because the default smart contract in papercontract is CommercialPaperContract and it has an issue transaction. Note that the issue transaction in BondContract can only be invoked by explicitly addressing it. Likewise, even though the cancel transaction is unique, because BondContract is not the default smart contract, it must also be explicitly addressed.
默认合约CommercialPaperContract的issue交易可以直接访问,而BondContract的issue需要明确指定。

In most cases, a chaincode will only contain a single smart contract, so careful naming of the chaincode can reduce the need for developers to care about chaincode as a concept. In the example code above it feels like papercontract is a smart contract.
大多数情况下,链码只有一个合约,所以好的链码命名可以减少开发者对链码的关注。

In summary, contract names are a straightforward mechanism to identify individual smart contracts within a given chaincode. Contract names make it easy for applications to find a particular smart contract and use it to access the ledger.
总之,合约名称是识别合约中链码的机制。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值