Cross-Chaincode Invoking in Hyperledger Fabric
By: Kc Tam
Tags: Hyperledger Fabric, Fabric V2, Chaincode, Smart Contracts

Overview

Chaincode in Hyperledger Fabric comes with an API InvokeChaincode() to allow a chaincode invoking a function in another chaincode. Here is the description about this API in the documentation. (link)

For chaincode to chaincode interactions using the invokeChaincode() API, both chaincodes must be installed on the same peer. For interactions that only require the called chaincode’s world state to be queried, the invocation can be in a different channel to the caller’s chaincode. For interactions that require the called chaincode’s world state to be updated, the invocation must be in the same channel as the caller’s chaincode.

In this article we try to create some test cases to observe the behaviour of this cross chaincode function invoking.

InvokeChaincode() Interface

The interface of InvokeChaincode() is like this (link)

InvokeChaincode(chaincodeName string, args [][]byte, channel string) pb.Response

When using this API in the chaincode, we specify the target chaincode name and channel for this action. The arguments are placed as an array of byte arrays. We will see how it is constructed in the demonstration.

Test Cases

As our plan is to explore the behaviour of InvokeChaincode() , here we are building several test cases centering state querying and chaincode function invoking, and create a testing chaincode to perform these cases.

Case 1: Querying State We will use InvokeChaincode() to query state in another chaincode deployed on the same channel. In case 1.1 the result is simply shown. In case 1.2, we will write the value read into local ledger. And we make observations on the transaction recorded in the blockchain. For simplicity both chaincodes are deployed in the same channel (per discussion, this also works for chaincodes deployed in different channels).

Case 2: Invoking Chaincode Function We will use InvokeChaincode() to invoke chaincode function in another chaincode. In case 2.1 both chaincodes are deployed in the same channel, while in case 2.2 in different channels. We will observe the difference on the results.

Demonstration Preparation

Setup

This demonstration is largely using the script network.sh in test-network Test-network comes with a fully-equipped script (v2.2+) helping us to build a two-peer-org setup, customizable channel and chaincode deployment. With this, we can easily build our test cases.

Chaincode

To demonstrate these test cases, we are using a new chaincode with some testing functions. The chaincode is named ccctest (cross-chaincode test) is cloned from sacc (simple asset chaincode). We keep the existing set() and get() functions, and add four functions corresponding to our four test cases.

The testing chaincode ccctest is shown here.

What we need to do is to copy the chaincode/sacc directory to chaincode/testccc , and copy this chaincode to the file chaincode/testccc/sacc.go (or change the name if you wish)

cd fabric-samples/chaincode

cp -r sacc ccctest

cd ccctest << copy our test chaincode to replace sacc.go >>

Test Cases Demonstration

Case 1: Query State

Case 1.1: Read ledger through chaincode function from other chaincode.

The chaincode we are using is getsaccname(). This function in ccctest will invoke sacc chaincode function get() with given key name. The result is displayed.