8.3.4 Interchain Services based on Hyperledger Fabric
Application Contract Development Guide
1. Preparation:
Run the code below to obtain the BSN interchain consuming contract help package (ICH.git). Currently, only the GO language version is supported but more versions will be added later.
ccd $GOPATH
mkdir -p src/github.com/BSNDA && cd src/github.com/BSNDA
git clone https://github.com/BSNDA/ICH.git
2. Initiate the interchain service request
After creating the Fabric chain code struct and invoke function, import below package:
import (
"github.com/BSNDA/ICH/sample/irita/consumers/fabric/crosschaincode"
)
Call the crosschaincode.CallService method in the invoke function using the parameters of the method as follows:
- stub: shim.ChaincodeStubInterface
- serviceName: the interchain service name of permissioned chain is “cross_service”
- input: the input object for interchain service
- callbackCC: callback chaincode name
- callbackFcn: callback chaincode function name
- timeout: timeout
The input parameter varies according to the interchain service and type passed in. In the Fabric service, the input structure is as follows:
type Input struct {
ChainId uint64 `json:"chainId"`
ChainCode string `json:"chainCode"`
FunType string `json:"funType"`
Args []string `json:"args"`
}
In the FISCO BCOS service, the input structure is as follows:
type BcosInput struct {
OptType string `json:"optType"`
ChainID uint64 `json:"chainId"`
ContractAddress string `json:"contractAddress"`
CallData string `json:"callData"`
}
A unique request ID will be returned after successful invocation. Keep this value and use it to determine the cross-chain results in the callback function.
3. Implement the callback interface:
After the cross-chain contract receives the service response from Fabric Relayer, the callback method name and callback chaincode name passed in will be called to return the service response. The first parameter of the call returns a JSON-formatted string. Below is the service response structure:
type ServiceResponse struct {
RequestId string `json:"requestID,omitempty"`
ErrMsg string `json:"errMsg,omitempty"`
Output string `json:"output,omitempty"`
IcRequestId string `json:"icRequestID,omitempty"`
}
Method crosschaincode.GetCallBackInfo() could be called to Serialize the value. The requestID is unique and can be used to conduct the business processing. Output is a JSON-formatted string which is the returned value of the cross-chain response. Below is the input data structure:
type InputData struct {
Header interface{} `json:"header"`
Body interface{} `json:"body"`
}
Parameter “Body” is the output object for the service. In Fabric service, the input structure is as follows:
type Output struct {
TxValidationCode int32 `json:"txValidationCode"`
ChaincodeStatus int32 `json:"chaincodeStatus"`
TxId string `json:"txId"`
Payload string `json:"payload"`
}
In FISCO BCOS service, the output structure is as follows:
type BcosOutput struct {
Result string `json:"result,omitempty"`
Status bool `json:"status,omitempty"`
TxHash string `json:"tx_hash,omitempty"`
}
4. Package the chaincode
ICH.git, which is imported by the chaincode, needs to be packaged with the chaincode together by govendor. If you haven’t installed govendor, you can install it as below: Install govendor:
go get -u -v github.com/kardianos/govendor
Execute in the main method directory:
govendor init
govendor add -tree github.com/BSNDA/ICH/sample/irita/consumers/fabric/crosschaincode
After execution, the vendor folder will be generated. For the last step, compress the project and vendor folder together, then upload and deploy it in BSN portal.
Application Contract Example:
GitHub: https://github.com/BSNDA/ICH/tree/bsnirita/sample/irita/consumers/fabric/chaincode