From 1ba455512fcac3ce8ebf1dd319cc9a8d2506ef7d Mon Sep 17 00:00:00 2001 From: Josh Kneubuhl Date: Fri, 11 Nov 2022 13:10:42 -0500 Subject: [PATCH] Move Test Network Basic tests from Azure to GHA Signed-off-by: Josh Kneubuhl --- .github/workflows/test-network-basic.yaml | 47 +++++++ .github/workflows/test-network-gateway.yaml | 47 +++++++ .github/workflows/test-network-hsm.yaml | 56 ++++++++ .../test-network-off-chain-data.yaml | 47 +++++++ .../application-gateway-go/assetTransfer.go | 13 +- .../src/main/java/App.java | 34 ++--- .../application-go/assetTransfer.go | 16 ++- .../application-typescript-hsm/src/app.ts | 16 ++- .../chaincode-java/settings.gradle | 3 +- ci/azure-pipelines.yml | 27 +--- ci/scripts/run-test-network-basic.sh | 124 +++--------------- ci/scripts/run-test-network-gateway.sh | 71 ++++++++++ ci/scripts/run-test-network-hsm.sh | 89 +++++++++++++ ci/scripts/run-test-network-off-chain.sh | 61 +++++++++ full-stack-asset-transfer-guide/README.md | 1 + .../application-go/hsm-sample.go | 16 ++- .../application-typescript/src/hsm-sample.ts | 15 ++- 17 files changed, 520 insertions(+), 163 deletions(-) create mode 100644 .github/workflows/test-network-basic.yaml create mode 100644 .github/workflows/test-network-gateway.yaml create mode 100644 .github/workflows/test-network-hsm.yaml create mode 100644 .github/workflows/test-network-off-chain-data.yaml create mode 100755 ci/scripts/run-test-network-gateway.sh create mode 100755 ci/scripts/run-test-network-hsm.sh create mode 100755 ci/scripts/run-test-network-off-chain.sh diff --git a/.github/workflows/test-network-basic.yaml b/.github/workflows/test-network-basic.yaml new file mode 100644 index 00000000..d679f4e6 --- /dev/null +++ b/.github/workflows/test-network-basic.yaml @@ -0,0 +1,47 @@ +name: Test Network Basic +run-name: ${{ github.actor }} is running the test-network basic tests + +on: + workflow_dispatch: + push: + branches: + - "main" + pull_request: + branches: + - "main" + +jobs: + test-network-basic: + runs-on: ubuntu-latest + + strategy: + matrix: + chaincode-language: + - go + - javascript + - typescript + - java + + steps: + - run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event." + + - name: checkout + uses: actions/checkout@v3 + + - name: Install fabric CLI + run: | + curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/install-fabric.sh | bash -s -- binary + echo ${PWD}/bin >> $GITHUB_PATH + + - name: Install retry CLI + run: curl -sSL https://raw.githubusercontent.com/kadwanev/retry/master/retry -o ./bin/retry && chmod +x ./bin/retry + + - name: Pull Fabric Docker Images + run: ./ci/scripts/pullFabricImages.sh + + - name: Run Test Network Basic + working-directory: test-network + env: + CHAINCODE_LANGUAGE: ${{ matrix.chaincode-language }} + run: ../ci/scripts/run-test-network-basic.sh + diff --git a/.github/workflows/test-network-gateway.yaml b/.github/workflows/test-network-gateway.yaml new file mode 100644 index 00000000..c58f9ea1 --- /dev/null +++ b/.github/workflows/test-network-gateway.yaml @@ -0,0 +1,47 @@ +name: Test Network Gateway +run-name: ${{ github.actor }} is running the test-network gateway tests + +on: + workflow_dispatch: + push: + branches: + - "main" + pull_request: + branches: + - "main" + +jobs: + test-network-gateway: + runs-on: ubuntu-latest + + strategy: + matrix: + chaincode-language: + - go + - javascript + - typescript + - java + + steps: + - run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event." + + - name: checkout + uses: actions/checkout@v3 + + - name: Install fabric CLI + run: | + curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/install-fabric.sh | bash -s -- binary + echo ${PWD}/bin >> $GITHUB_PATH + + - name: Install retry CLI + run: curl -sSL https://raw.githubusercontent.com/kadwanev/retry/master/retry -o ./bin/retry && chmod +x ./bin/retry + + - name: Pull Fabric Docker Images + run: ./ci/scripts/pullFabricImages.sh + + - name: Run Test Network Off Chain + working-directory: test-network + env: + CHAINCODE_LANGUAGE: ${{ matrix.chaincode-language }} + run: ../ci/scripts/run-test-network-gateway.sh + diff --git a/.github/workflows/test-network-hsm.yaml b/.github/workflows/test-network-hsm.yaml new file mode 100644 index 00000000..bc7d539e --- /dev/null +++ b/.github/workflows/test-network-hsm.yaml @@ -0,0 +1,56 @@ +name: Test Network HSM +run-name: ${{ github.actor }} is running the test-network HSM tests + +on: + workflow_dispatch: + push: + branches: + - "main" + pull_request: + branches: + - "main" + +jobs: + test-network-hsm: + runs-on: ubuntu-latest + + strategy: + matrix: + chaincode-language: + - go + - javascript + - typescript + - java + + steps: + - run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event." + + - name: checkout + uses: actions/checkout@v3 + + - name: Pull Fabric Docker Images + run: ./ci/scripts/pullFabricImages.sh + + - name: Install fabric CLI + run: | + curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/install-fabric.sh | bash -s -- binary + echo ${PWD}/bin >> $GITHUB_PATH + + - name: Install retry CLI + run: curl -sSL https://raw.githubusercontent.com/kadwanev/retry/master/retry -o ./bin/retry && chmod +x ./bin/retry + + - name: Install SoftHSM + run: sudo apt install -y softhsm2 + + - name: Set up SoftHSM + run: | + echo directories.tokendir = /tmp > $HOME/softhsm2.conf + export SOFTHSM2_CONF=$HOME/softhsm2.conf + softhsm2-util --init-token --slot 0 --label "ForFabric" --pin 98765432 --so-pin 1234 + + - name: Run Test Network HSM + working-directory: test-network + env: + CHAINCODE_LANGUAGE: ${{ matrix.chaincode-language }} + run: ../ci/scripts/run-test-network-hsm.sh + diff --git a/.github/workflows/test-network-off-chain-data.yaml b/.github/workflows/test-network-off-chain-data.yaml new file mode 100644 index 00000000..43405535 --- /dev/null +++ b/.github/workflows/test-network-off-chain-data.yaml @@ -0,0 +1,47 @@ +name: Test Network Off Chain +run-name: ${{ github.actor }} is running the test-network off chain data tests + +on: + workflow_dispatch: + push: + branches: + - "main" + pull_request: + branches: + - "main" + +jobs: + test-network-off-chain: + runs-on: ubuntu-latest + + strategy: + matrix: + chaincode-language: + - go + - javascript + - typescript + - java + + steps: + - run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event." + + - name: checkout + uses: actions/checkout@v3 + + - name: Install fabric CLI + run: | + curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/install-fabric.sh | bash -s -- binary + echo ${PWD}/bin >> $GITHUB_PATH + + - name: Install retry CLI + run: curl -sSL https://raw.githubusercontent.com/kadwanev/retry/master/retry -o ./bin/retry && chmod +x ./bin/retry + + - name: Pull Fabric Docker Images + run: ./ci/scripts/pullFabricImages.sh + + - name: Run Test Network Off Chain + working-directory: test-network + env: + CHAINCODE_LANGUAGE: ${{ matrix.chaincode-language }} + run: ../ci/scripts/run-test-network-off-chain.sh + diff --git a/asset-transfer-basic/application-gateway-go/assetTransfer.go b/asset-transfer-basic/application-gateway-go/assetTransfer.go index b20f043b..e9ccbe5a 100755 --- a/asset-transfer-basic/application-gateway-go/assetTransfer.go +++ b/asset-transfer-basic/application-gateway-go/assetTransfer.go @@ -34,8 +34,6 @@ const ( tlsCertPath = cryptoPath + "/peers/peer0.org1.example.com/tls/ca.crt" peerEndpoint = "localhost:7051" gatewayPeer = "peer0.org1.example.com" - channelName = "mychannel" - chaincodeName = "basic" ) var now = time.Now() @@ -67,6 +65,17 @@ func main() { } defer gw.Close() + // Override default values for chaincode and channel name as they may differ in testing contexts. + chaincodeName := "basic" + if ccname := os.Getenv("CHAINCODE_NAME"); ccname != "" { + chaincodeName = ccname + } + + channelName := "mychannel" + if cname := os.Getenv("CHANNEL_NAME"); cname != "" { + channelName = cname + } + network := gw.GetNetwork(channelName) contract := network.GetContract(chaincodeName) diff --git a/asset-transfer-basic/application-gateway-java/src/main/java/App.java b/asset-transfer-basic/application-gateway-java/src/main/java/App.java index 83cbb55b..7fcf3962 100644 --- a/asset-transfer-basic/application-gateway-java/src/main/java/App.java +++ b/asset-transfer-basic/application-gateway-java/src/main/java/App.java @@ -34,22 +34,22 @@ import java.time.Instant; import java.util.concurrent.TimeUnit; public final class App { - private static final String mspID = "Org1MSP"; - private static final String channelName = "mychannel"; - private static final String chaincodeName = "basic"; + private static final String MSP_ID = System.getenv().getOrDefault("MSP_ID", "Org1MSP"); + private static final String CHANNEL_NAME = System.getenv().getOrDefault("CHANNEL_NAME", "mychannel"); + private static final String CHAINCODE_NAME = System.getenv().getOrDefault("CHAINCODE_NAME", "basic"); // Path to crypto materials. - private static final Path cryptoPath = Paths.get("..", "..", "test-network", "organizations", "peerOrganizations", "org1.example.com"); + private static final Path CRYPTO_PATH = Paths.get("../../test-network/organizations/peerOrganizations/org1.example.com"); // Path to user certificate. - private static final Path certPath = cryptoPath.resolve(Paths.get("users", "User1@org1.example.com", "msp", "signcerts", "cert.pem")); + private static final Path CERT_PATH = CRYPTO_PATH.resolve(Paths.get("users/User1@org1.example.com/msp/signcerts/cert.pem")); // Path to user private key directory. - private static final Path keyDirPath = cryptoPath.resolve(Paths.get("users", "User1@org1.example.com", "msp", "keystore")); + private static final Path KEY_DIR_PATH = CRYPTO_PATH.resolve(Paths.get("users/User1@org1.example.com/msp/keystore")); // Path to peer tls certificate. - private static final Path tlsCertPath = cryptoPath.resolve(Paths.get("peers", "peer0.org1.example.com", "tls", "ca.crt")); + private static final Path TLS_CERT_PATH = CRYPTO_PATH.resolve(Paths.get("peers/peer0.org1.example.com/tls/ca.crt")); // Gateway peer end point. - private static final String peerEndpoint = "localhost:7051"; - private static final String overrideAuth = "peer0.org1.example.com"; + private static final String PEER_ENDPOINT = "localhost:7051"; + private static final String OVERRIDE_AUTH = "peer0.org1.example.com"; private final Contract contract; private final String assetId = "asset" + Instant.now().toEpochMilli(); @@ -75,19 +75,19 @@ public final class App { } private static ManagedChannel newGrpcConnection() throws IOException, CertificateException { - var tlsCertReader = Files.newBufferedReader(tlsCertPath); + var tlsCertReader = Files.newBufferedReader(TLS_CERT_PATH); var tlsCert = Identities.readX509Certificate(tlsCertReader); - return NettyChannelBuilder.forTarget(peerEndpoint) - .sslContext(GrpcSslContexts.forClient().trustManager(tlsCert).build()).overrideAuthority(overrideAuth) + return NettyChannelBuilder.forTarget(PEER_ENDPOINT) + .sslContext(GrpcSslContexts.forClient().trustManager(tlsCert).build()).overrideAuthority(OVERRIDE_AUTH) .build(); } private static Identity newIdentity() throws IOException, CertificateException { - var certReader = Files.newBufferedReader(certPath); + var certReader = Files.newBufferedReader(CERT_PATH); var certificate = Identities.readX509Certificate(certReader); - return new X509Identity(mspID, certificate); + return new X509Identity(MSP_ID, certificate); } private static Signer newSigner() throws IOException, InvalidKeyException { @@ -98,7 +98,7 @@ public final class App { } private static Path getPrivateKeyPath() throws IOException { - try (var keyFiles = Files.list(keyDirPath)) { + try (var keyFiles = Files.list(KEY_DIR_PATH)) { return keyFiles.findFirst().orElseThrow(); } } @@ -106,10 +106,10 @@ public final class App { public App(final Gateway gateway) { // Get a network instance representing the channel where the smart contract is // deployed. - var network = gateway.getNetwork(channelName); + var network = gateway.getNetwork(CHANNEL_NAME); // Get the smart contract from the network. - contract = network.getContract(chaincodeName); + contract = network.getContract(CHAINCODE_NAME); } public void run() throws GatewayException, CommitException { diff --git a/asset-transfer-basic/application-go/assetTransfer.go b/asset-transfer-basic/application-go/assetTransfer.go index eb102656..7eb62017 100644 --- a/asset-transfer-basic/application-go/assetTransfer.go +++ b/asset-transfer-basic/application-go/assetTransfer.go @@ -55,12 +55,24 @@ func main() { } defer gw.Close() - network, err := gw.GetNetwork("mychannel") + channelName := "mychannel" + if cname := os.Getenv("CHANNEL_NAME"); cname != "" { + channelName = cname + } + + log.Println("--> Connecting to channel", channelName) + network, err := gw.GetNetwork(channelName) if err != nil { log.Fatalf("Failed to get network: %v", err) } - contract := network.GetContract("basic") + chaincodeName := "basic" + if ccname := os.Getenv("CHAINCODE_NAME"); ccname != "" { + chaincodeName = ccname + } + + log.Println("--> Using chaincode", chaincodeName) + contract := network.GetContract(chaincodeName) log.Println("--> Submit Transaction: InitLedger, function creates the initial set of assets on the ledger") result, err := contract.SubmitTransaction("InitLedger") diff --git a/asset-transfer-basic/application-typescript-hsm/src/app.ts b/asset-transfer-basic/application-typescript-hsm/src/app.ts index 46b8ffed..84ffbe58 100644 --- a/asset-transfer-basic/application-typescript-hsm/src/app.ts +++ b/asset-transfer-basic/application-typescript-hsm/src/app.ts @@ -12,9 +12,10 @@ import { enrollUserToWallet, registerUser, UserToEnroll, UserToRegister } from ' const walletPath = path.join(__dirname, 'wallet'); -// define information about the channel and chaincode id that will be driven by this application -const channelName = 'mychannel'; -const chaincodeId = 'basic'; +// define information about the channel and chaincode that will be driven by this application +const channelName = envOrDefault('CHANNEL_NAME', 'mychannel'); +const chaincodeName = envOrDefault('CHAINCODE_NAME', 'default-basic'); + // define the CA Registrar const mspOrg1 = 'Org1MSP'; @@ -192,7 +193,7 @@ async function main() { const network = await gateway.getNetwork(channelName); // Get the contract from the network. - const contract = network.getContract(chaincodeId); + const contract = network.getContract(chaincodeName); // loop around all transactions to send, each one will be sent sequentially // through the same gateway/network/contract as subsequent transations expect the @@ -274,5 +275,12 @@ async function interactWithFabric(contract: Contract, transactionToPerform: Tran } } +/** + * envOrDefault() will return the value of an environment variable, or a default value if the variable is undefined. + */ +function envOrDefault(key: string, defaultValue: string): string { + return process.env[key] || defaultValue; +} + // execute the main function main(); diff --git a/asset-transfer-basic/chaincode-java/settings.gradle b/asset-transfer-basic/chaincode-java/settings.gradle index 2633c4b9..b8074969 100644 --- a/asset-transfer-basic/chaincode-java/settings.gradle +++ b/asset-transfer-basic/chaincode-java/settings.gradle @@ -1,5 +1,4 @@ /* * SPDX-License-Identifier: Apache-2.0 */ - -rootProject.name = 'basic' +rootProject.name = System.getenv('CHAINCODE_NAME') ?: 'basic' \ No newline at end of file diff --git a/ci/azure-pipelines.yml b/ci/azure-pipelines.yml index 1fbb352c..fc1d984e 100644 --- a/ci/azure-pipelines.yml +++ b/ci/azure-pipelines.yml @@ -23,6 +23,7 @@ variables: - group: credentials jobs: + - job: REST_Sample displayName: REST Server Sample pool: @@ -105,32 +106,6 @@ jobs: - script: ./ci/scripts/lint.sh displayName: Lint Code - - job: TestNetworkBasic - displayName: Test Network - pool: - vmImage: ubuntu-20.04 - strategy: - matrix: - Basic-Go: - CHAINCODE_NAME: basic - CHAINCODE_LANGUAGE: go - Basic-Java: - CHAINCODE_NAME: basic - CHAINCODE_LANGUAGE: java - Basic-Javascript: - CHAINCODE_NAME: basic - CHAINCODE_LANGUAGE: javascript - Basic-Typescript: - CHAINCODE_NAME: basic - CHAINCODE_LANGUAGE: typescript - - steps: - - template: templates/install-deps.yml - - template: templates/install-deps-hsm.yml - - script: ../ci/scripts/run-test-network-basic.sh - workingDirectory: test-network - displayName: Run Test Network Basic Chaincode - - job: KubeTestNetworkBasic displayName: Kube Test Network pool: diff --git a/ci/scripts/run-test-network-basic.sh b/ci/scripts/run-test-network-basic.sh index 5194fbd0..3f323eb1 100755 --- a/ci/scripts/run-test-network-basic.sh +++ b/ci/scripts/run-test-network-basic.sh @@ -1,7 +1,8 @@ +#!/bin/bash + set -euo pipefail CHAINCODE_LANGUAGE=${CHAINCODE_LANGUAGE:-go} -CHAINCODE_NAME=${CHAINCODE_NAME:-basic} CHAINCODE_PATH=${CHAINCODE_PATH:-../asset-transfer-basic} function print() { @@ -17,6 +18,9 @@ function createNetwork() { cd addOrg3 ./addOrg3.sh up -ca -s couchdb cd .. +} + +function deployChaincode() { print "Deploying ${CHAINCODE_NAME} chaincode" ./network.sh deployCC -ccn "${CHAINCODE_NAME}" -ccp "${CHAINCODE_PATH}/chaincode-${CHAINCODE_LANGUAGE}" -ccv 1 -ccs 1 -ccl "${CHAINCODE_LANGUAGE}" } @@ -26,46 +30,43 @@ function stopNetwork() { ./network.sh down } -# Run Go application +# Set up one test network to run each test scenario. +# Each test will create an independent scope by installing a new chaincode contract to the channel. createNetwork + + +# Run Go application print "Initializing Go application" +export CHAINCODE_NAME=basic_go +deployChaincode pushd ../asset-transfer-basic/application-go print "Executing AssetTransfer.go" go run . popd -stopNetwork # Run Java application -createNetwork print "Initializing Java application" +export CHAINCODE_NAME=basic_java +deployChaincode pushd ../asset-transfer-basic/application-java print "Executing Gradle Run" gradle run popd -stopNetwork - -# Run Java application using gateway -createNetwork -print "Initializing Java application" -pushd ../asset-transfer-basic/application-gateway-java -print "Executing Gradle Run" -./gradlew run -popd -stopNetwork # Run Javascript application -createNetwork print "Initializing Javascript application" +export CHAINCODE_NAME=basic_javascript +deployChaincode pushd ../asset-transfer-basic/application-javascript npm install print "Executing app.js" node app.js popd -stopNetwork # Run typescript application -createNetwork print "Initializing Typescript application" +export CHAINCODE_NAME=basic_typescript +deployChaincode pushd ../asset-transfer-basic/application-typescript npm install print "Building app.ts" @@ -73,95 +74,6 @@ npm run build print "Running the output app" node dist/app.js popd -stopNetwork -# Run gateway typescript application -createNetwork -print "Initializing Typescript gateway application" -pushd ../asset-transfer-basic/application-gateway-typescript -npm install -print "Building app.ts" -npm run build -print "Running the output app" -node dist/app.js -popd -stopNetwork -# Run typescript HSM application -createNetwork -print "Initializing Typescript HSM application" -pushd ../asset-transfer-basic/application-typescript-hsm -print "Setup SoftHSM" -export SOFTHSM2_CONF=$PWD/softhsm2.conf -print "install dependencies" -npm install -print "Building app.ts" -npm run build -print "Running the output app" -node dist/app.js -popd -stopNetwork - -# Run Typescript HSM gateway application -echo 'Delete fabric-ca-client from samples bin' -rm ../bin/fabric-ca-client -echo 'go install pkcs11 enabled fabric-ca-client' -go install -tags pkcs11 github.com/hyperledger/fabric-ca/cmd/fabric-ca-client@latest -createNetwork -print "Initializing Typescript HSM gateway application" -pushd ../hardware-security-module/scripts/ -print "Enroll and register User in HSM" -./generate-hsm-user.sh HSMUser -pushd ../application-typescript/ -print "install dependencies and prepare for running" -npm install -print "Running the output app" -npm run start -popd -popd -stopNetwork - -# Run Go HSM gateway application -createNetwork -print "Initializing Go HSM gateway application" -pushd ../hardware-security-module/scripts/ -print "Register and enroll user in HSM" -./generate-hsm-user.sh HSMUser -pushd ../application-go -print "Running the output app" -go run -tags pkcs11 . -popd -popd -stopNetwork - -# Run Go gateway application -createNetwork -print "Initializing Go gateway application" -pushd ../asset-transfer-basic/application-gateway-go -print "Executing AssetTransfer.go" -go run . -popd -stopNetwork - -# Run off-chain data TypeScript application -createNetwork -print "Initializing Typescript off-chain data application" -pushd ../off_chain_data/application-typescript -rm -f checkpoint.json store.log -npm install -print "Running the output app" -SIMULATED_FAILURE_COUNT=1 npm start getAllAssets transact getAllAssets listen -SIMULATED_FAILURE_COUNT=1 npm start listen -popd -stopNetwork - -# Run off-chain data Java application -createNetwork -print "Initializing Typescript off-chain data application" -pushd ../off_chain_data/application-java -rm -f app/checkpoint.json app/store.log -print "Running the output app" -SIMULATED_FAILURE_COUNT=1 ./gradlew run --quiet --args='getAllAssets transact getAllAssets listen' -SIMULATED_FAILURE_COUNT=1 ./gradlew run --quiet --args=listen -popd stopNetwork diff --git a/ci/scripts/run-test-network-gateway.sh b/ci/scripts/run-test-network-gateway.sh new file mode 100755 index 00000000..3b3def77 --- /dev/null +++ b/ci/scripts/run-test-network-gateway.sh @@ -0,0 +1,71 @@ +#!/bin/bash + +set -euo pipefail + +CHAINCODE_LANGUAGE=${CHAINCODE_LANGUAGE:-go} +CHAINCODE_PATH=${CHAINCODE_PATH:-../asset-transfer-basic} + +function print() { + GREEN='\033[0;32m' + NC='\033[0m' + echo + echo -e "${GREEN}${1}${NC}" +} + +function createNetwork() { + print "Creating 3 Org network" + ./network.sh up createChannel -ca -s couchdb + cd addOrg3 + ./addOrg3.sh up -ca -s couchdb + cd .. +} + +function deployChaincode() { + print "Deploying ${CHAINCODE_NAME} chaincode" + ./network.sh deployCC -ccn "${CHAINCODE_NAME}" -ccp "${CHAINCODE_PATH}/chaincode-${CHAINCODE_LANGUAGE}" -ccv 1 -ccs 1 -ccl "${CHAINCODE_LANGUAGE}" +} + +function stopNetwork() { + print "Stopping network" + ./network.sh down +} + +# Set up one test network to run each test scenario. +# Each test will create an independent scope by installing a new chaincode contract to the channel. +createNetwork + + +# Run Go gateway application +print "Initializing Go gateway application" +export CHAINCODE_NAME=go_gateway +deployChaincode +pushd ../asset-transfer-basic/application-gateway-go +print "Executing AssetTransfer.go" +go run . +popd + + +# Run gateway typescript application +print "Initializing Typescript gateway application" +export CHAINCODE_NAME=typescript_gateway +deployChaincode +pushd ../asset-transfer-basic/application-gateway-typescript +npm install +print "Building app.ts" +npm run build +print "Running the output app" +node dist/app.js +popd + + +# Run Java application using gateway +print "Initializing Java application" +export CHAINCODE_NAME=java_gateway +deployChaincode +pushd ../asset-transfer-basic/application-gateway-java +print "Executing Gradle Run" +./gradlew run +popd + + +stopNetwork diff --git a/ci/scripts/run-test-network-hsm.sh b/ci/scripts/run-test-network-hsm.sh new file mode 100755 index 00000000..5ecd1715 --- /dev/null +++ b/ci/scripts/run-test-network-hsm.sh @@ -0,0 +1,89 @@ +#!/bin/bash + +set -euo pipefail + +CHAINCODE_LANGUAGE=${CHAINCODE_LANGUAGE:-go} +CHAINCODE_PATH=${CHAINCODE_PATH:-../asset-transfer-basic} + +function print() { + GREEN='\033[0;32m' + NC='\033[0m' + echo + echo -e "${GREEN}${1}${NC}" +} + +function createNetwork() { + print "Creating 3 Org network" + ./network.sh up createChannel -ca -s couchdb + cd addOrg3 + ./addOrg3.sh up -ca -s couchdb + cd .. +} + +function deployChaincode() { + print "Deploying ${CHAINCODE_NAME} chaincode" + ./network.sh deployCC -ccn "${CHAINCODE_NAME}" -ccp "${CHAINCODE_PATH}/chaincode-${CHAINCODE_LANGUAGE}" -ccv 1 -ccs 1 -ccl "${CHAINCODE_LANGUAGE}" +} + +function stopNetwork() { + print "Stopping network" + ./network.sh down +} + +# Set up one test network to run each test scenario. +# Each test will create an independent scope by installing a new chaincode contract to the channel. +createNetwork + + +# Run typescript HSM application +print "Initializing Typescript HSM application" +export CHAINCODE_NAME=typescript_hsm +deployChaincode +pushd ../asset-transfer-basic/application-typescript-hsm +print "Setup SoftHSM" +export SOFTHSM2_CONF=$PWD/softhsm2.conf +print "install dependencies" +npm install +print "Building app.ts" +npm run build +print "Running the output app" +node dist/app.js +popd + +# Run Typescript HSM gateway application +print "Initializing Typescript HSM Gateway application" +export CHAINCODE_NAME=ts_hsm_gateway +deployChaincode +echo 'Delete fabric-ca-client from samples bin' +rm ../bin/fabric-ca-client +echo 'go install pkcs11 enabled fabric-ca-client' +GOBIN=${PWD}/../bin go install -tags pkcs11 github.com/hyperledger/fabric-ca/cmd/fabric-ca-client@latest +fabric-ca-client version +print "Initializing Typescript HSM gateway application" +pushd ../hardware-security-module/scripts/ +print "Enroll and register User in HSM" +./generate-hsm-user.sh HSMUser +pushd ../application-typescript/ +print "install dependencies and prepare for running" +npm install +print "Running the output app" +npm run start +popd +popd + +# Run Go HSM gateway application +print "Initializing Go HSM gateway application" +export CHAINCODE_NAME=go_hsm +deployChaincode +pushd ../hardware-security-module/scripts/ +print "Register and enroll user in HSM" +./generate-hsm-user.sh HSMUser +pushd ../application-go +print "Running the output app" +go run -tags pkcs11 . +popd +popd + + + +stopNetwork diff --git a/ci/scripts/run-test-network-off-chain.sh b/ci/scripts/run-test-network-off-chain.sh new file mode 100755 index 00000000..497aef26 --- /dev/null +++ b/ci/scripts/run-test-network-off-chain.sh @@ -0,0 +1,61 @@ +#!/bin/bash + +set -euo pipefail + +CHAINCODE_LANGUAGE=${CHAINCODE_LANGUAGE:-go} +CHAINCODE_PATH=${CHAINCODE_PATH:-../asset-transfer-basic} + +function print() { + GREEN='\033[0;32m' + NC='\033[0m' + echo + echo -e "${GREEN}${1}${NC}" +} + +function createNetwork() { + print "Creating 3 Org network" + ./network.sh up createChannel -ca -s couchdb + cd addOrg3 + ./addOrg3.sh up -ca -s couchdb + cd .. +} + +function deployChaincode() { + print "Deploying ${CHAINCODE_NAME} chaincode" + ./network.sh deployCC -ccn "${CHAINCODE_NAME}" -ccp "${CHAINCODE_PATH}/chaincode-${CHAINCODE_LANGUAGE}" -ccv 1 -ccs 1 -ccl "${CHAINCODE_LANGUAGE}" +} + +function stopNetwork() { + print "Stopping network" + ./network.sh down +} + +# Set up one test network to run each test scenario. +# Each test will create an independent scope by installing a new chaincode contract to the channel. +createNetwork + +# Run off-chain data TypeScript application +export CHAINCODE_NAME=ts_off_chain_data +deployChaincode +print "Initializing Typescript off-chain data application" +pushd ../off_chain_data/application-typescript +rm -f checkpoint.json store.log +npm install +print "Running the output app" +SIMULATED_FAILURE_COUNT=1 npm start getAllAssets transact getAllAssets listen +SIMULATED_FAILURE_COUNT=1 npm start listen +popd + +# Run off-chain data Java application +#createNetwork +export CHAINCODE_NAME=off_chain_data +deployChaincode +print "Initializing off-chain data application" +pushd ../off_chain_data/application-java +rm -f app/checkpoint.json app/store.log +print "Running the output app" +SIMULATED_FAILURE_COUNT=1 ./gradlew run --quiet --args='getAllAssets transact getAllAssets listen' +SIMULATED_FAILURE_COUNT=1 ./gradlew run --quiet --args=listen +popd + +stopNetwork diff --git a/full-stack-asset-transfer-guide/README.md b/full-stack-asset-transfer-guide/README.md index b9e4e04a..dd900ad2 100644 --- a/full-stack-asset-transfer-guide/README.md +++ b/full-stack-asset-transfer-guide/README.md @@ -106,3 +106,4 @@ We'll create a digital representation of these cards on the blockchain ledger. T - [Go Bananas](docs/CloudReady/40-bananas.md) - [Bring it Home](docs/CloudReady/90-teardown.md) + diff --git a/hardware-security-module/application-go/hsm-sample.go b/hardware-security-module/application-go/hsm-sample.go index 9663f764..54c92eed 100644 --- a/hardware-security-module/application-go/hsm-sample.go +++ b/hardware-security-module/application-go/hsm-sample.go @@ -76,8 +76,20 @@ func main() { } func exampleTransaction(gateway *client.Gateway) { - network := gateway.GetNetwork("mychannel") - contract := network.GetContract("basic") + + // Override default values for chaincode and channel name as they may differ in testing contexts. + channelName := "mychannel" + if cname := os.Getenv("CHANNEL_NAME"); cname != "" { + channelName = cname + } + + chaincodeName := "basic" + if ccname := os.Getenv("CHAINCODE_NAME"); ccname != "" { + chaincodeName = ccname + } + + network := gateway.GetNetwork(channelName) + contract := network.GetContract(chaincodeName) fmt.Printf("Submit Transaction: CreateAsset, creates new asset with ID, Color, Size, Owner and AppraisedValue arguments \n") diff --git a/hardware-security-module/application-typescript/src/hsm-sample.ts b/hardware-security-module/application-typescript/src/hsm-sample.ts index c99fcd68..40490718 100644 --- a/hardware-security-module/application-typescript/src/hsm-sample.ts +++ b/hardware-security-module/application-typescript/src/hsm-sample.ts @@ -62,8 +62,12 @@ async function main() { } async function exampleTransaction(gateway: Gateway):Promise { - const network = gateway.getNetwork('mychannel'); - const contract = network.getContract('basic'); + + const channelName = envOrDefault('CHANNEL_NAME', 'mychannel'); + const chaincodeName = envOrDefault('CHAINCODE_NAME', 'default-basic'); + + const network = gateway.getNetwork(channelName); + const contract = network.getContract(chaincodeName); console.log('\n--> Submit Transaction: CreateAsset, creates new asset with ID, Color, Size, Owner and AppraisedValue arguments'); @@ -154,4 +158,11 @@ function getUncompressedPointOnCurve(key: jsrsa.KJUR.crypto.ECDSA): Buffer { return uncompressedPoint; } +/** + * envOrDefault() will return the value of an environment variable, or a default value if the variable is undefined. + */ +function envOrDefault(key: string, defaultValue: string): string { + return process.env[key] || defaultValue; +} + main().catch(console.error);