首页 > 技术文章 > package contractapi学习笔记

SilverhandFan 2020-12-30 18:48 原文

基于hyperledger-fabric 2.3

type Contract:

contract类可以被嵌入在一个struct里,便捷地使该struct满足ContractInterface的要求。type contract定义了用来设置或者获取unknown transaction\ before transaction\ after transaction \ name(类名)的函数。

type contract里的函数:

func (c *Contract) GetAfterTransaction() interface{}: 获取当前的after transaction set(感觉应该是数据状态?),可以为nil

func (c *Contract) GetBeforeTransaction() interface{}:获取当前的 before transaction set,可以为nil

func (c *Contract) GetInfo() metadata.InfoMetadata:取得当前描述智能合约的元数据

func (c *Contract) GetName() string:取得智能合约的名字

func (c *Contract) GetTransactionContextHandler() SettableTransactionContextInterface:取得当前合约正在被set的那个transaction的内容,如果没有一个transaction被set到这个合约上,则返回TransactionContext(这好像是个常量)

func (c *Contract) GetUnknownTransaction() interface{}:获取unkonwn transaction, 可以为nil

 

type ContractChaincode:

contractchaincode是一个用于接用chaincode接口,并提供调用contract的接口的路由

func NewChaincode(contracts ...ContractInterface) (*ContractChaincode, error):根据作为参数传入的chaincode,创建一个新的chaincode

func (cc *ContractChaincode) Init(stub shim.ChaincodeStubInterface) peer.Response:在chaincode被establish的时候init就被调用了。如果有函数被传进来的话就将请求的详细内容传给invoke函数,否则直接返回shim.success

func (cc *ContractChaincode) Invoke(stub shim.ChaincodeStubInterface) peer.Response:用于查找、更新账本

func (cc *ContractChaincode) Start() error:启动chaincode

 

type ContractInterface:

定义了一个“合法”的合约应该有哪些接口,用于链码的合约必须实现这个接口

GetInfo() metadata.InfoMetadata:返回contract的信息,这些信息被用于创建元数据

GetUnknownTransaction() interface{}:返回用于一个contract的未知函数(unknown function)。当合约被用于产生一个新的链码,这个函数会被调用,然后返回的未知的tx会被存储

GetBeforeTransaction() interface{}:返回要被一个contract使用的前函数(before function)。当合约被用于产生一个新的链码,这个函数会被调用,然后返回的之前的tx(before transaction)会被存储

GetAfterTransaction() interface{}:返回要被一个contract使用的after function。当合约被用于产生一个新的链码,这个函数会被调用,然后after transaction会被储存。

GetName() string:返回contract的名字

GetTransactionContextHandler() SettableTransactionContextInterface:返回被这个contract的functions使用的SettableTransactionContextInterface

“The after function is then called after the named function on each Init/Invoke of that contract via the chaincode. When called the after function is passed the returned value of the named function and the transaction context (if the function takes the transaction context). If nil is returned then no after function is called on Init.”:在named function在contract的init或者invoke环节被调用的之后,after function会被调用。当被调用时,这个after function会被传入named function的返回值和tx内容,(如果这个named function里面有tx内容)。如果返回了nil,意思是没有后续function在init时被调用了(named function是 init)。

type EvaluationContractInterface:

扩展了ContractInterface,可以给contract的function打标签,标示这些function应该被query(查询)而不是被invoke(调用)

type IgnoreContractInterface:

扩展了ContractInterface,打标签,标示哪些function不应该被query和invoke

type SettableTransactionContextInterface:定义了一个合法的tx context应该有的功能,用于chaincode的tx 内容集必须有这个interface

SetStub(shim.ChaincodeStubInterface):从一个transaction的调用传递stub到tx context。这个函数会被带有stub的init或者invoke调用

SetClientIdentity(ci cid.ClientIdentity):从一个transaction的调用传递client的身份到tx context。

type SystemContract:

这个contract被添加给所有的chaincode,以提供接通metadata的通道

func (sc *SystemContract) GetMetadata() string:返回JSON格式的chaincode的metadata,这个chaincode包含这个systemcontract

func (sc *SystemContract) GetEvaluateTransactions() []string:返回存在于system contract的tx,这些tx应该被标记为evaluate tx(应该被query tx调用)

type TransactionContext:

一个被用于contract的 基本的transaction context。包含最少需要的功能,提供与stub、client身份的通道。

是默认的被使用的tx context。

func (ctx *TransactionContext) SetStub(stub shim.ChaincodeStubInterface):在tx context中存入stub

func (ctx *TransactionContext) SetClientIdentity(ci cid.ClientIdentity):在tx context中存入client身份

func (ctx *TransactionContext) GetStub() shim.ChaincodeStubInterface:返回当前的set stub

func (ctx *TransactionContext) GetClientIdentity() cid.ClientIdentity:返回当前的set ClientIdentity

 

type TransactionContextInterface:

定义了TransactionContext需要的接口。可以被一个“没有set用户交易内容去允许tx函数去调用一个接口去简化单元测试”的函数调用。

GetStub() shim.ChaincodeStubInterface:提供与“被init或者invoke设置的”stub的通道

GetClientIdentity() cid.ClientIdentity:提供与“被init或者invoke设置的”client identity的通道

推荐阅读