Thinktecture Blogs

Deploying And Running .NET Chaincode On Hyperledger Fabric's Development Mode

/ 3 min read / Hyperledger Fabric

After you've developed your first chaincode, you are curious how to start it within the Hyperledger Fabric development network. Please make sure, you have all prerequisites done so far and your test network is running (remember to start it with docker-compose -f docker-compose-simple.yaml up). The following code samples are based on the simple Asset Holding sample (see here for the blog post).

Please note, that .NET Core is not yet officially supported by Hyperledger Fabric. The current .NET Core package is in development, the API will evolve over time.

First, connect to the Hyperledger Fabric CLI via docker exec -it cli bash. After a moment, you should be connected.

Next, we need to install the chaincode on the peer. This is the part, where it may look at bit strange what we're doing. As stated, there is currently no official support of .NET for Hyperledger Fabric. By that, it only can run Node.js and Go chaincode. But since we are in development mode, it's not a Hyperledger Fabric peer which is running the chaincode but our host, so we can debug and modify it without having to redeploy it everytime. Currently, we simply do not care, if a peer could not run the code (if you are interested in this as well, here is a sample Dockerfile modifying the fabric-ccenv to include .NET Core - this is highly experimental). But still, we need to install something, so we simply create a file with content dev  by invoking the following command within the cli bash you've just connected to.

mkdir /opt/gopath/src/chaincodedev/chaincode/netcore
echo "dev" > /opt/gopath/src/chaincodedev/chaincode/netcore/dev

peer chaincode install -p /opt/gopath/src/chaincodedev/chaincode/netcore -n assetholding -v 0 -l node

The second command installs the "empty" chaincode using the name assetholding with version 0 and language node. Please make sure to use language node here, so the code is just copied to the peer.

Now, start your chaincode within the IDE. But make sure, you have two environment variables set:

  • CORE_PEER_ADDRESS=localhost:7052
  • CORE_CHAINCODE_ID_NAME=assetholding:0

Your chaincode is ready to be instantiated if the log reads Successfully established communication with peer node. State transferred to "ready". Let's go back to the cli bash and execute:

peer chaincode instantiate -n assetholding -v 0 -c '{"Args": ["Init", "a", "100", "b", "200"]}' -C myc

With this command we are going to instantiate our chaincode. The argument -c sends a generic JSON command to the chaincode, in this case the arguments for the instantiation. The actual arguments are placed as an array within Args. The first "Init" does not do anything at all in our case, since we don't take a look at the actual function to be executed within the chaincode's Init() method in our sample chaincode. The last four parameters are the same as we expect them in our chaincode's init method. a is the name of the first account with a value of 100 and b is the second account with a value of 200.

After the command's invocation, our chaincode has been initialized and is ready to be invoked, for example if you want to transfer 50 money from a to b, you can execute via cli bash:

peer chaincode invoke -n assetholding -c '{"Args":["Transfer","a","b","10"]}' -C myc

As you can see here, we're using the identifier "Transfer" to invoke the method InternalTransfer() in our chaincode. The second parameter a is the name of the account to take the money from and transfer it to the third parameter b. The amount to take is specified in the forth parameter which is 10. By that, we are transferring 10 money from a to b.

To check, if everything has worked so far, we can query an account for its value by executing this command:

peer chaincode query -n assetholding -c '{"Args":["Query","a"]}' -C myc

The result should be 90, since the account has been initialized with 100 and we transferred 10 to another account. If you check account b, the result should be 210.

Congratulations, you now have developed and tested your first .NET-based chaincode for Hyperledger Fabric!

After .NET get's official support, some of the tricks, like using -l node won't be necessary anymore. Until then, the trick is working quite well for creating .NET-based chaincodes.

Manuel Rauber

Manuel Rauber

Manuel loves creating cross-platform applications with HTML5 and JavaScript in the frontend, and blockchain technologies like Hyperledger Fabric in the backend.