Fabric应用开发流程

一、 开发范围

1.1 开发chaincode与application

开发者首先需要建立一个Fabric网络,在此之上开发chaincode,然后在SDK基础上开发业务应用,与Fabric网络进行业务连接,应用还需要通过Fabric CA签发证书。如下图所示:
在这里插入图片描述

1.2 chaincode接口

编写chaincode所需的接口在core/chaincode/shim/interfaces.go文件中,部分内容如下所示,调用GetState接口根据key查找对应value,调用PutState接口可以根据key值修改value,调用InvokeChaincode接口调用另一个chaincode。

// ChaincodeStubInterface is used by deployable chaincode apps to access and
// modify their ledgers
type ChaincodeStubInterface interface {
	// GetArgs returns the arguments intended for the chaincode Init and Invoke
	// as an array of byte arrays.
	GetArgs() [][]byte
	InvokeChaincode(chaincodeName string, args [][]byte, channel string) pb.Response
	// GetState returns the value of the specified `key` from the
	// ledger. Note that GetState doesn't read data from the writeset, which
	// has not been committed to the ledger. In other words, GetState doesn't
	// consider data modified by PutState that has not been committed.
	// If the key does not exist in the state database, (nil, nil) is returned.
	GetState(key string) ([]byte, error)
	// PutState puts the specified `key` and `value` into the transaction's
	// writeset as a data-write proposal. PutState doesn't effect the ledger
	// until the transaction is validated and successfully committed.
	// Simple keys must not be an empty string and must not start with null
	// character (0x00), in order to avoid range query collisions with
	// composite keys, which internally get prefixed with 0x00 as composite
	// key namespace.
	PutState(key string, value []byte) error

二、开发步骤

2.1 确定应用场景(Scenario)

编写chaincode需要首先确定应用场景,比如某公司需要发行一种票据进行融资,期货平台需要返还相应的利息,核心企业需要发行票据和支持兑现业务,银行、基金等公司会购买票据、转卖票据、兑现,还会有评级机构对票据进行评级,收到一些通知提醒,如下图所示:
在这里插入图片描述

2.2 数据的生命周期(Lifecycle)

确定了业务场景后,需要细化业务数据的生命周期,例如下图所示,信封表示event,书表示state:
在这里插入图片描述

  1. 首先是核心企业发起一个发行(issue)票据的交易,包含票据ID、发行时间、到期时间、面值等信息;
  2. 发行交易完成后会产生一个状态,包括发行者、票据ID、拥有者、发行日期、到期时间、面值、票据状态(已发行)等数据;
  3. 这时另一个公司可以发起一个购买(buy)交易,包含票据ID、新的拥有者、购买时间、购买价格(小于票据面值,打折)等信息;
  4. 购买交易完成后切换到一个新的状态,拥有者变成了购买方(Digibank),拥有者有权对票据进行转售;
  5. 拥有者可以发起兑现(redeem)交易,包含当前拥有者、兑现日期等信息;
  6. 兑现交易完成后切换到新状态,拥有者重新变成发行方,票据状态变成了已兑现(redeemed)。

以上就是票据业务数据的生命周期,涉及到各种交易、以及对应的状态。

2.3 数据结构(Data Structure)

编码前需要确认数据存储的数据结构,根据业务需求设计出对应的value结构,如下图所示:
在这里插入图片描述
根据查询需求可以设计出key值的前缀,方便根据key值进行范围查询,比如设计key值为org.papaernet.paperMagnetoCorp00001,可以根据“org.papaernet.paperMagnetoCorp”查询出核心企业拥有的所有票据,如下图所示:
在这里插入图片描述

2.4 编写智能合约(Smart Contract)

数据结构设计完成后,就可以开始编写智能合约了。如下图所示:
在这里插入图片描述

2.5 开发客户端应用(Application)

除了编写chaincode,还要基于SDK开发客户端应用,需要确定应用如何通过SDK与智能合约交互,比如提交发行(issue)交易需要调用智能合约中的issue分支,如下图所示:
在这里插入图片描述

2.6 涉及的其他问题

开发一个Fabric应用还需考虑其他各种问题,包括智能合约命名空间(Chaincode namespace)、背书策略(Endorsement policies)、连接配置文件(Connection Profile)、身份(Idendities)等,贯穿开发中的各个环节。

发布了29 篇原创文章 · 获赞 3 · 访问量 5583

猜你喜欢

转载自blog.csdn.net/ice_fire_x/article/details/104390602