更新时间:2021-12-30 GMT+08:00
分享

跨链智能合约方法定义

下表为涉及跨链资产交换的跨链智能合约必须实现的方法,且必须保证方法名相同,否则可能出现跨链资产交换接口调用超时或调用失败的情况:
表1 方法说明

方法名

说明

preCommitSend

在跨链资产交换发起方所属区块链上执行的预提交操作

preCommitRecv

在跨链资产交换接收方所属区块链上执行的预提交操作

commitSend

在跨链资产交换发起方所属区块链上执行的提交操作

commitRecv

在跨链资产交换接收方所属区块链上执行的提交操作

rollbackSend

在跨链资产交换发起方所属区块链上执行的回滚操作

rollbackRecv

在跨链资产交换接收方所属区块链上执行的回滚操作

可在跨链智能合约按以下示例定义方法名常量与Invoke方法,确保已实现了必选方法:
/* 
 * Function name list of the TCS example chaincode 
 * Note: the chaincode for TCS should have all of the functions below with the same function names 
 */
const (
    fromPreCommitFuncName = "preCommitSend"
    toPreCommitFuncName   = "preCommitRecv"
    fromCommitFuncName    = "commitSend"
    toCommitFuncName      = "commitRecv"   
    fromRollbackFuncName  = "rollbackSend"   
    toRollbackFuncName    = "rollbackRecv"
)

/*
 * Invoke is the entrance of the chaincode invoking
 */
func (t *TCSExampleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
	fmt.Println("TCS Example Function Invoke")
	function, args := stub.GetFunctionAndParameters()
	if function == fromPreCommitFuncName {
		return t.preCommitSend(stub, args)
	} else if function == toPreCommitFuncName {
		return t.preCommitRecv(stub, args)
	} else if function == fromCommitFuncName {
		return t.commitSend(stub, args)
	} else if function == toCommitFuncName {
		return t.commitRecv(stub, args)
	} else if function == fromRollbackFuncName {
		return t.rollbackSend(stub, args)
	} else if function == toRollbackFuncName {
		return t.rollbackRecv(stub, args)
	} else if function == "query" {
		return t.query(stub, args)
	} else if function == "init" {
		return t.Init(stub)
	}
	return shim.Error("Invalid invoke function name. Expecting \"preCommitSend\" \"preCommitRev\" " +
		"\"commitSend\" \"commitRecv\" \"rollbackSend\" \"rollbackRecv\" \"query\", but got: " + function)
}
分享:

    相关文档

    相关产品