Skip to content

Latest commit

 

History

History
86 lines (67 loc) · 3.43 KB

v0.6_migration.md

File metadata and controls

86 lines (67 loc) · 3.43 KB

Migrating chaincode to Hyperledger fabric v0.6

This document outlines the programmatic changes you will need to implement in your Go code for compatibility with the Hyperledger fabric v0.6 codebase, and provides a high-level overview of the new features.

Shim interface change

The chaincode shim interface changes for compatibility with the latest Hyperledger shim:

The chaincode interface has changed from shim.ChaincodeStub to shim.ChaincodeStubInterface. See the interfaces.go file for the shim source code. The following code snippet from line 74 of chaincode_example02 will highlight this alteration.

This change applies to all transaction types: Deploy, Invoke, and Query.

func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStub, function string, args []string) ([]byte, error) {

func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {

Chaincode calling chaincode

Chaincode calling chaincode is included in the shim package. There are two functions available - InvokeChaincode and QueryChaincode. First, these functions no longer accept the function name as a parameter; instead the function must be passed in as an argument. Second, the arguments are passed in as a byte array, not a string. A utility is provided to convert your strings to a byte array.

The following code snippets from chaincode_example04 demonstrate the difference. Make note of f, representing the function invoke. It is removed from the InvokeChaincode parameters, and instead passed as an argument to the invokeArgs element. The arguments are then converted to a byte array before being passed to InvokeChaincode. This change is not optional and must be implemented in your code.

// v0.5 fabric code
f := "invoke"
invokeArgs := []string{"a", "b", "10"}
response, err := stub.InvokeChaincode(chainCodeToCall, f, invokeArgs)
// v0.6 fabric code
f := "invoke"
// function is removed from InvokeChaincode, now passed as an argument within invokeArgs.  invokeArgs is converted to byte array and then passed along.
invokeArgs := util.ToChaincodeArgs(f, "a", "b", "10")
response, err := stub.InvokeChaincode(chainCodeToCall, invokeArgs)

API signatures

Chaincode API signatures have changed when constructing REST API payloads and CLI commands. The function can now be passed within the "args" element as the first argument.

The following code snippets will demonstrate the changes to a basic chaincode invoking transaction from the CLI and through the REST API.

peer chaincode invoke -1 golang -n mycc -c '{"Function": "invoke", "Args": ["a", "b", "10"]}'

peer chaincode invoke -1 golang -n mycc -c '{"Args": ["invoke", "a", "b", "10"]}'
{
  "jsonrpc": "2.0",
  "method": "invoke",
  "params": {
      "type": 1,
      "chaincodeID":{
          "name":"mycc"
      },
      "ctorMsg": {
         "function":"invoke",
         "args":["a", "b", "10"]
      }
  },
  "id": 3
}
{
  "jsonrpc": "2.0",
  "method": "invoke",
  "params": {
      "type": 1,
      "chaincodeID":{
          "name":"mycc"
      },
      "ctorMsg": {
         "args":["invoke", "a", "b", "10"]
      }
  },
  "id": 3
}

Note: REST API and CLI developed in v0.5-developer-preview will work with fabric v0.6. However, when using the Java SDK you must implement the new format, where the function is passed within the "args" element.