fabric-samples/test-network-k8s/docs/CHAINCODE_AS_A_SERVICE.md
jkneubuh eeb4e611c7
Provide clear guidance for debugging Java chaincode as a service #684 (#724)
* Addresses Issue #548 by providing a simple guide for running Java chaincode as a service with a local debugger.

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* missed a couple of bash syntax errors

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* Add metadata and activate examples to the CC README

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* move ccpackage/ contents into network script

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* Fix CI test - Azure mounts git checkout at a different folder root path

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* Update test-network-k8s README with updated cc deploy commands

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* Run basic-asset transfer CI tests with Java + golang CC in Azure

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* remove (obsolete) test-net chaincode/ folder

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* Address some PR review feedback points - README reorg

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* Use the SDKs contract router Main, not a local entrypoint

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* bump the build - remove trailing newlines from a README

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>
2022-04-26 15:33:14 +01:00

4.7 KiB

Debugging Chaincode

In this sample we will employ the Kubernetes Test Network to illustrate a scenario of building, running, and debugging chaincode on a development workstation.

While this guide targets the Java asset-transfer-basic sample, the approach may be applied to any sample and chaincode implementation language.

When debugging chaincode as a service, the chaincode process is launched on the local system, binding to a port on the host's network interface. In this mode the developer has complete flexibility in determining how and where the process runs - it can be launched as a native binary from a CLI, attached to an active debugging session from an IDE, as a Docker container, or even behind a reverse network proxy for diagnosing issues in a remote / cloud-based Fabric network.

TL/DR

export PATH=${PWD}/test-network-k8s:$PATH

cd asset-transfer-basic/chaincode-java 

network kind 
network up 
network channel create
network chaincode deploy    asset-transfer-basic basic_1.0 ${PWD}
network chaincode metadata  asset-transfer-basic
network chaincode invoke    asset-transfer-basic '{"Args":["InitLedger"]}'
network chaincode query     asset-transfer-basic '{"Args":["ReadAsset","asset1"]}' | jq 

Detailed Guide

network down
network up
network channel create
# Build the chaincode docker image 
docker build -t fabric-samples/asset-transfer-basic/chaincode-java .

# Load the docker image directly to the KIND control plane.  
# (Alternately, build/tag/push the image to a remote container registry, e.g. localhost:5000 or ghcr.io) 
kind load docker-image fabric-samples/asset-transfer-basic/chaincode-java
# Assemble the chaincode package archive 
network chaincode package basic_1.0 asset-transfer-basic $PWD/build/asset-transfer.tgz

# Determine the ID for the chaincode package 
CORE_CHAINCODE_ID_NAME=$(network chaincode id $PWD/build/asset-transfer.tgz)

# Launch the chaincode in k8s as Deployment + Service 
network chaincode launch asset-transfer-basic $CORE_CHAINCODE_ID_NAME fabric-samples/asset-transfer-basic/chaincode-java

# Complete the chaincode lifecycle 
network chaincode install $PWD/build/asset-transfer.tgz 
network chaincode approve asset-transfer-basic $CORE_CHAINCODE_ID_NAME
network chaincode commit  asset-transfer-basic
# execute the smart contract by name 
network chaincode metadata  asset-transfer-basic
network chaincode invoke    asset-transfer-basic '{"Args":["InitLedger"]}'
network chaincode query     asset-transfer-basic '{"Args":["ReadAsset","asset1"]}'
kubectl -n test-network logs -f deployment/org1peer1-ccaas-asset-transfer-basic

Debugging

Build

./gradlew shadowJar

or

docker build -t fabric-samples/asset-transfer-basic/chaincode-java . 

Package

By instructing the peer to connect to chaincode at the Docker host alias host.docker.internal, pods running in Kubernetes will access the local process via a special loopback interface established by KIND.

Set the "address" attribute in the package connection.json descriptor and assemble the chaincode package:

export TEST_NETWORK_CHAINCODE_ADDRESS=host.docker.internal:9999

network cc package basic_1.0 asset-transfer-debug $PWD/build/asset-transfer-debug.tgz

Launch

When chaincode is launched locally, it must declare the package ID in the environment as if the process had been managed by the peer's chaincode lifecycle manager. Calculate the package ID and start the chaincode, binding to port 9999 on the local system:

export CHAINCODE_SERVER_ADDRESS=0.0.0.0:9999
export CORE_CHAINCODE_ID_NAME=$(network chaincode id $PWD/build/asset-transfer-debug.tgz)

java -jar build/libs/chaincode.jar

Or using the editor/debugger/IDE of your choice, create a launch target for ContractMain.main(), specifying the environment as above.

Or launch the chaincode in a Docker container, binding to port 9999 on the host system:

docker run \
  --rm \
  --name basic_1.0 \
  -p 9999:9999 \
  -e CHAINCODE_SERVER_ADDRESS \
  -e CORE_CHAINCODE_ID_NAME \
  fabric-samples/asset-transfer-basic/chaincode-java

Approve, Invoke, and Query

After the contract main has launched, install, approve, commit, and invoke the chaincode:

# Complete the chaincode lifecycle 
network cc activate asset-transfer-debug $PWD/build/asset-transfer-debug.tgz 
# execute the smart contract by name 
network cc metadata asset-transfer-debug
network cc invoke   asset-transfer-debug '{"Args":["InitLedger"]}'
network cc query    asset-transfer-debug '{"Args":["ReadAsset","asset1"]}'

Tear Down

network down 

or

network unkind