-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding examples of CCAAS and support into the test-network
- Updated the test-network with examples of runnig CCAAS - Updating the asset transfer basic with how to run chaincode as a service. Signed-off-by: Matthew B White <[email protected]>
- Loading branch information
Showing
12 changed files
with
677 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Running Chaincode as Service with the Test Network | ||
|
||
The chaincode-as-a-service feature is a very useful and practical way to run 'Smart Contracts'. Traditionally the Fabric Peer has taken on the role of orchestrating the complete lifecycle of the chaincode. It required access to the Docker Daemon to create images, and start containers. Java, NodeJS and Go where explicitly known to the peer including how they should be built and started. | ||
|
||
As a result this makes it very hard to deploy into K8S style environments, or to run in any form of debug mode. Plus the code is being rebuilt by the peer so production deployments can have some degree of uncertainty about what dependencies have been pulled in. | ||
|
||
Chaincode-as-service requires you to orchestrate the build and deployment phase yourself. Whilst this is an additional step, it gives control back. The Peer stills requires a 'chaincode package' to be installed. In this case this doesn't contain code, but the information about where the chaincode is hosted. (Hostname,Port,TLS config etc) | ||
|
||
## Prereqs | ||
|
||
We need to use the latest 2.4.1 release as this contains some improvements to make this process easier. The core functionality is available in earlier releases, but isn't quite as easy to use. | ||
|
||
- ... clone samples repo etc.. TBC | ||
|
||
|
||
## Start the test network | ||
|
||
It's useful to have two terminal windows open, one for starting the Fabric Network, and a second for monitoring all the docker containers. | ||
|
||
In your 'monitoring' window, run this | ||
|
||
```bash | ||
# from the fabric-samples repo | ||
./test-network/monitordocker.sh | ||
``` | ||
|
||
All the containers on the `fabric-test` network will be monitored; remember to terminate this as it starts a docker image on the `fabric-test` network. | ||
|
||
|
||
In the 'Fabric Network' window, start the test network | ||
|
||
```bash | ||
cd test-network | ||
./network.sh up createChannel | ||
``` | ||
|
||
Once this has started, you can run the 'Chaincode-as-a-service'. This will do the following. | ||
|
||
- Build a docker container of the contract, in this example `/asset-transfer-basic/chaincode-typescript` | ||
- Install, Approve and Commit a chaincode definition. This is unchanged, but the chaincode package contains connection inforamtion, not code | ||
- Start the docker container containing the contract | ||
|
||
``` | ||
./network.sh deployCCAAS -ccn basic -ccp ../asset-transfer-basic/chaincode-typescript -cport 9999 | ||
``` | ||
|
||
This is very similar to the `deployCC` command, it needs the name, and path. But also needs to have the port the chaincode container is going use. As each container is on the `fabric-test` network, you might wish to alter this so there are no collisions with other chaincode containers. | ||
|
||
You should be able to see the contract starting in the monitoring window. | ||
|
||
To test things are working you can invoke the 'Contract Metadata' function. | ||
|
||
```bash | ||
# set the environment variables to work as org1 | ||
export $(./setOrgEnv.sh | xargs) | ||
|
||
# invoke the function | ||
peer chaincode query -C mychannel -n basic -c '{"Args":["org.hyperledger.fabric:GetMetadata"]}' | jq | ||
``` | ||
|
||
If you don't have `jq` installed omit ` | jq`. The metadata shows the details of the deployed contract, and is JSON. | ||
|
||
|
||
## Debug and standalone running | ||
|
||
TBC |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# the first stage | ||
FROM gradle:jdk11 AS GRADLE_BUILD | ||
ARG CC_SERVER_PORT | ||
|
||
# copy the build.gradle and src code to the container | ||
COPY src/ src/ | ||
COPY build.gradle ./ | ||
|
||
# Build and package our code | ||
RUN gradle --no-daemon build shadowJar -x checkstyleMain -x checkstyleTest | ||
|
||
|
||
# the second stage of our build just needs the compiled files | ||
FROM openjdk:11-jre-slim | ||
# copy only the artifacts we need from the first stage and discard the rest | ||
COPY --from=GRADLE_BUILD /home/gradle/build/libs/chaincode.jar /chaincode.jar | ||
|
||
|
||
ENV PORT $CC_SERVER_PORT | ||
EXPOSE $CC_SERVER_PORT | ||
|
||
# set the startup command to execute the jar | ||
CMD ["java", "-jar", "/chaincode.jar"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
FROM node:16 AS builder | ||
ARG CC_SERVER_PORT | ||
|
||
WORKDIR /usr/src/app | ||
|
||
# Copy node.js source and build, changing owner as well | ||
COPY --chown=node:node . /usr/src/app | ||
RUN npm ci && npm run package | ||
|
||
|
||
FROM node:16 AS production | ||
|
||
# Setup tini to work better handle signals | ||
ENV TINI_VERSION v0.19.0 | ||
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /tini | ||
RUN chmod +x /tini | ||
|
||
|
||
WORKDIR /usr/src/app | ||
COPY --chown=node:node --from=builder /usr/src/app/dist ./dist | ||
COPY --chown=node:node --from=builder /usr/src/app/package.json ./ | ||
COPY --chown=node:node --from=builder /usr/src/app/npm-shrinkwrap.json ./ | ||
COPY --chown=node:node docker/docker-entrypoint.sh /usr/src/app/docker-entrypoint.sh | ||
RUN npm ci --only=production | ||
|
||
ENV PORT $CC_SERVER_PORT | ||
EXPOSE $CC_SERVER_PORT | ||
ENV NODE_ENV=production | ||
|
||
USER node | ||
ENTRYPOINT [ "/tini", "--", "/usr/src/app/docker-entrypoint.sh" ] |
12 changes: 12 additions & 0 deletions
12
asset-transfer-basic/chaincode-typescript/docker/docker-entrypoint.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env bash | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
set -euo pipefail | ||
: ${CORE_PEER_TLS_ENABLED:="false"} | ||
|
||
if [ "${CORE_PEER_TLS_ENABLED,,}" = "true" ]; then | ||
npm run start:server | ||
else | ||
npm run start:server-nontls | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/bin/bash | ||
|
||
# This script uses the logspout and http stream tools to let you watch the docker containers | ||
# in action. | ||
# | ||
# More information at https://github.com/gliderlabs/logspout/tree/master/httpstream | ||
|
||
if [ -z "$1" ]; then | ||
DOCKER_NETWORK=fabric_test | ||
else | ||
DOCKER_NETWORK="$1" | ||
fi | ||
|
||
if [ -z "$2" ]; then | ||
PORT=8000 | ||
else | ||
PORT="$2" | ||
fi | ||
|
||
echo Starting monitoring on all containers on the network ${DOCKER_NETWORK} | ||
|
||
docker kill logspout 2> /dev/null 1>&2 || true | ||
docker rm logspout 2> /dev/null 1>&2 || true | ||
|
||
trap "docker kill logspout" SIGINT | ||
|
||
docker run -d --rm --name="logspout" \ | ||
--volume=/var/run/docker.sock:/var/run/docker.sock \ | ||
--publish=127.0.0.1:${PORT}:80 \ | ||
--network ${DOCKER_NETWORK} \ | ||
gliderlabs/logspout | ||
sleep 3 | ||
curl http://127.0.0.1:${PORT}/logs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.