Skip to content

Commit

Permalink
Enhance test network sample
Browse files Browse the repository at this point in the history
- Simplify test-network setup
- Remove fpc ccaas scripts
- Use network.sh CCaaS install script
- Update Blockchain explorer setup

Signed-off-by: Marcus Brandenburger <bur@zurich.ibm.com>
  • Loading branch information
mbrandenburger committed Jul 23, 2024
1 parent b3dbcfc commit 3f10103
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 355 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,3 @@
[submodule "common/crypto/pdo"]
path = common/crypto/pdo
url = https://github.com/hyperledger-labs/private-data-objects
[submodule "samples/deployment/test-network/fabric-samples"]
path = samples/deployment/test-network/fabric-samples
url = https://github.com/hyperledger/fabric-samples.git
2 changes: 2 additions & 0 deletions build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ include $(TOP)/config.mk

# define composites only here and not in config.mk so we can override parts in config.override.mk
DOCKER := $(DOCKER_CMD) $(DOCKERFLAGS)
DOCKER_COMPOSE := $(DOCKER_CMD) compose

ifeq (${SGX_MODE}, HW)
GOTAGS = -tags sgx_hw_mode
endif
Expand Down
5 changes: 3 additions & 2 deletions samples/deployment/test-network/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# SPDX-License-Identifier: Apache-2.0

**/packages/*
blockchain-explorer
blockchain-explorer/
fabric-samples/
.env
24 changes: 6 additions & 18 deletions samples/deployment/test-network/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,6 @@ ifeq ($(CC_PATH),)
$(error CC_PATH is not set)
endif


# Define default value for CONTAINER_CLI if not set
CONTAINER_CLI ?= docker

# Check if ${CONTAINER_CLI}-compose command exists
ifeq ($(shell command -v $(CONTAINER_CLI)-compose > /dev/null 2>&1 && echo yes), yes)
CONTAINER_CLI_COMPOSE ?= $(CONTAINER_CLI)-compose
else
CONTAINER_CLI_COMPOSE ?= $(CONTAINER_CLI) compose
endif

# Define a target to display the chosen container CLI tools
infoln:
@echo "Using $(CONTAINER_CLI) and $(CONTAINER_CLI_COMPOSE)"

build: ercc-container ecc-container

ercc-container:
Expand All @@ -45,7 +30,7 @@ ecc-container:
make -C ${CC_PATH} && \
make -C ${FPC_PATH}/ecc DOCKER_IMAGE=fpc/fpc-${CC_ID}$${HW_EXTENSION} DOCKER_ENCLAVE_SO_PATH=$${ECC_PATH} all docker

ercc-ecc-start: infoln
ercc-ecc-start:
if [ ! -z "${DOCKERD_FPC_PATH}" ]; then \
export SGX_CREDENTIALS_PATH="${DOCKERD_FPC_PATH}/integration/config/ias"; \
fi && \
Expand All @@ -55,7 +40,10 @@ ercc-ecc-start: infoln
SGX_DEVICE_PATH=$$(if [ -e "/dev/isgx" ]; then echo "/dev/isgx"; elif [ -e "/dev/sgx/enclave" ]; then echo "/dev/sgx/enclave"; else echo "none"; fi) && \
[ "$${SGX_DEVICE_PATH}" != "none" ] || ( echo "ERROR: SGX_MODE is HW but no sgx device found"; exit 1; ) \
fi && \
env CC_ID=${CC_ID} FPC_VERSION=${FPC_VERSION} ${CONTAINER_CLI_COMPOSE} up
env CC_ID=${CC_ID} FPC_VERSION=${FPC_VERSION} ${DOCKER_COMPOSE} up -d

ercc-ecc-stop:
env CC_ID=${CC_ID} FPC_VERSION=${FPC_VERSION} ${CONTAINER_CLI_COMPOSE} down
env CC_ID=${CC_ID} FPC_VERSION=${FPC_VERSION} ${DOCKER_COMPOSE} down

clobber:
rm -rf blockchain-explorer fabric-samples .env
84 changes: 42 additions & 42 deletions samples/deployment/test-network/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# FPC and the Fabric-Samples Test Network

This guide shows how to deploy and run a FPC Chaincode on test-network provided by [fabric-samples](https://github.com/hyperledger/fabric-samples).
We provide fabric-samples as a submodule in `$FPC_PATH/samples/deployment/test-network/fabric-samples`.

Before moving forward, follow the main [README](../../../README.md) to set up your environment. This guide works also with
the FPC Dev docker container.
Before moving forward, follow the main [README](../../../README.md) to set up your environment.
This guide works also with the FPC Dev docker container.

## Prepare FPC Containers and the Test Network
## Prepare FPC Containers

FPC requires a special docker container to execute a FPC chaincode, similar to Fabric's `ccenv` container image but with additional support for Intel SGX.
You can pull the FPC chaincode environment image (`fabric-private-chaincode-ccenv`) from our Github repository or build them manually as follows:
Expand All @@ -26,68 +25,51 @@ Use `CC_ID` and `CC_PATH` to define the FPC Chaincode you want to build.
cd $FPC_PATH/samples/deployment/test-network
export CC_ID=echo
export CC_PATH=$FPC_PATH/samples/chaincode/echo
export CC_VER=$(cat CC_PATH/)
make build
```

Note: If you want to build with [mock-enclave](../../../ecc/chaincode/enclave/mock_enclave.go) rather than the real enclave-based one, build with
`make build GOTAGS="-tags mock_ecc"` instead.

Next, setup fabric sample network, binaries and docker images. Here we follow the official Fabric [instructions](https://hyperledger-fabric.readthedocs.io/en/release-2.3/install.html).

```bash
cd $FPC_PATH/samples/deployment/test-network
git clone https://github.com/hyperledger/fabric-samples
cd $FPC_PATH/samples/deployment/test-network/fabric-samples
git checkout -b "works" e334b75
curl -sSL https://bit.ly/2ysbOFE | bash -s -- 2.5.9 1.5.7 -s
```
## Prepare the Test Network

Before we can start the network, we need to update the Fabric peer configuration to enable FPC support.
That is, we need to install the external builders by adding the following lines to the peers `core.yaml`:
```yaml
chaincode:
externalBuilders:
- path: /opt/gopath/src/github.com/hyperledger/fabric-private-chaincode/fabric/externalBuilder/chaincode_server
name: fpc-external-launcher
propagateEnvironment:
- CORE_PEER_ID
- FABRIC_LOGGING_SPEC
```
Next, we set up the Fabric samples [test-network](https://github.com/hyperledger/fabric-samples/tree/main/test-network).

Since the Fabric-Samples test network uses docker and docker-compose, we need to ensure that the external Builder
scripts are also available inside the peer container. For this reason, we mount `$FPC_PATH/fabric/externalBuilder/chaincode_server` into
`/opt/gopath/src/github.com/hyperledger/fabric-private-chaincode/fabric/externalBuilder/chaincode_server`.
Since the Fabric samples test network uses docker (and docker-compose) to run the Fabric components, we need to make sure that it works within our FPC dev container environment.
In particular, as we use the host docker daemon, there may be issues with docker mounts.

For convenience, we provide a `setup.sh` script to update the `core.yaml` and the docker compose files to mount the external
Builder.
For convenience, we provide a `setup.sh` script that clones the fabric samples repository, downloads all required fabric container images and binaries, and replaces all relative mount paths in the compose files with absolute path on the host system.

```bash
cd $FPC_PATH/samples/deployment/test-network
./setup.sh
```

## Start the Network

## Start the Test Network

Let's start the Fabric-Samples test network.

```bash
cd $FPC_PATH/samples/deployment/test-network/fabric-samples/test-network
./network.sh up -ca
./network.sh createChannel -c mychannel
./network.sh up createChannel -ca
```

Next, we install the FPC Enclave Registry and our FPC Chaincode on the network by using the standard Lifecycle commands,
including chaincode `package`, `install`, `approveformyorg`, and `commit`. An important detail here is that the
chaincode packaging differs from traditional chaincode. Since we are using the external Builder and run the FPC Chaincode
as `Chaincode as a Service (CaaS)`, the packaging artifact will contain information including the CaaS endpoint rather
than the actual Chaincode.

### Install and run the FPC Chaincode

Next, we install the FPC Enclave Registry and the FPC Chaincode on the network by using the [Chaincode as a Service deployment method](https://github.com/hyperledger/fabric-samples/blob/main/test-network/CHAINCODE_AS_A_SERVICE_TUTORIAL.md).
For convenience, the test network provides a single command `./network.sh deployCCAAS` that executes the necessary steps of the [Fabric chaincode lifecycle](https://hyperledger-fabric.readthedocs.io/en/latest/chaincode_lifecycle.html),
in particular, it internally calls `package`, `install`, `approveformyorg`, and `commit`.

We continue with the following command to install the FPC Chaincode as just described.
Please make sure that you have set `CC_ID` and `CC_VER` variables correctly as described before.

```bash
cd $FPC_PATH/samples/deployment/test-network
./installFPC.sh
# IMPORTANT: a successfully install will show you an `export ...`
# statement as stdout on the command-line. Copy/Paste this statement
# into your shell or below starting of FPC containers will not work properly
# (but also would not give you clear errors that it doesn't!!)
```

Now we have the FPC Chaincode installed on the Fabric peers, but we still need to start our chaincode containers.
Expand All @@ -97,7 +79,9 @@ Make sure you have set `CC_ID` to the same chaincode ID as used in the earlier s
# Start FPC container
make ercc-ecc-start
```

You should see two instances of the FPC Echo chaincode and two instances of the FPC Enclave Registry chaincode running such as in the following example:

```bash
Creating ercc.peer0.org2.example.com ... done
Creating ercc.peer0.org1.example.com ... done
Expand All @@ -111,9 +95,11 @@ ercc.peer0.org1.example.com | 2021-05-11 16:11:32.108 UTC [cgo] 0 -> INFO 001
ercc.peer0.org2.example.com | 2021-05-11 16:11:32.170 UTC [ercc] main -> INFO 002 starting enclave registry (ercc_1.0:db2e97768a87d2b9ed9d86a729a767ef1040fb2e20d2f2a907e727ece7256028)
ercc.peer0.org1.example.com | 2021-05-11 16:11:32.199 UTC [ercc] main -> INFO 002 starting enclave registry (ercc_1.0:0919bd1be2e779a582a537da8e29fba241972c3531472006b170b0c26a1d71fb)
```

The FPC Chaincode is now up and running, ready for processing invocations!
Note that the containers are running in foreground in your terminal using docker-compose.


## Interact with the FPC Chaincode

Now we show how to use the [FPC Client SDK](../../../client_sdk/go) to interact with the FPC Chaincode running on the test network.
Expand All @@ -136,17 +122,18 @@ cd $FPC_PATH/samples/deployment/test-network
Now we will use the go app in `$FPC_PATH/samples/application/simple-go` to demonstrate the usage of the FPC Client SDK.
In order to initiate the FPC Chaincode enclave and register it with the FPC Enclave Registry, run the app with the `-withLifecycleInitEnclave` flag.


```bash
# for SGX HW mode make sure you set the SGX_CREDENTIALS_PATH path; for simulation mode this is not necessary
export SGX_CREDENTIALS_PATH=$FPC_PATH/config/ias

cd $FPC_PATH/samples/application/simple-go
CC_ID=echo ORG_NAME=Org1 go run . -withLifecycleInitEnclave
```

Note that we execute the go app as `Org1`, thereby creating and registering the FPC Chaincode enclave at `peer0.org1.example.com`. Alternatively, we could run this as `Org2` to initiate the enclave at `peer0.org2.example.com`.

Afterwards you _must_ run the application without the `withLifecycleInitEnclave` flag and you can play with multiple organizations.

```bash
cd $FPC_PATH/samples/application/simple-go
CC_ID=echo ORG_NAME=Org1 go run .
Expand Down Expand Up @@ -188,7 +175,9 @@ export SGX_CREDENTIALS_PATH=$FPC_PATH/config/ias
```

## Shutdown network

Since we opened a new terminal to interact with the FPC Chaincode, to be able to shutdown the FPC chaincode you need to define the environment variables that set the chaincode name and path.

```bash
export CC_ID=echo
export CC_PATH=$FPC_PATH/samples/chaincode/echo
Expand All @@ -198,12 +187,16 @@ cd $FPC_PATH/samples/deployment/test-network/fabric-samples/test-network
```

## Using HelloWorld chaincode on the test-network

In this section we show how you can run a different FPC chaincode on the test network. For example, you can use the [HelloWorld](../../chaincode/helloworld/README.md) code instead of the echo code. To do so you have to change the values of the environment variables set at the beginning `CC_ID` and `CC_PATH`.

```bash
export CC_ID=helloworld
export CC_PATH=$FPC_PATH/samples/chaincode/helloworld
```

Afterwards to test you would use [simple-cli-go](#How-to-use-simple-cli-go). You would need to verify that the `CC_NAME` variable is set to helloworld and then to interact you would execute the chaincode as follows:

```bash
# interact with the FPC Chaincode
./fpcclient invoke storeAsset asset1 100
Expand All @@ -213,25 +206,31 @@ Afterwards to test you would use [simple-cli-go](#How-to-use-simple-cli-go). You
## Debugging

For diagnostics, you can run the following to see logs for `peer0.org1.example.com`.

```bash
docker logs -f peer0.org1.example.com
docker logs -f ercc.peer0.org1.example.com
docker logs -f ecc.peer0.org1.example.com
```

To interact with the peer using the `peer CLI`, run the following

```bash
cd $FPC_PATH/samples/deployment/test-network/fabric-samples/test-network;
export FABRIC_CFG_PATH=$FPC_PATH/samples/deployment/test-network/fabric-samples/config
export PATH=$(readlink -f ../bin):$PATH
source ./scripts/envVar.sh; \
setGlobals 1;
```

and you will be able to run the usual peer cli commands, e.g.,

```bash
peer lifecycle chaincode queryinstalled
```

and, in particular, also access ercc to see the registry state, e.g.,

```bash
peer chaincode query -C mychannel -n ercc -c '{"Function": "queryChaincodeEndPoints", "Args" : ["echo"]}'
peer chaincode query -C mychannel -n ercc -c '{"Function": "queryListProvisionedEnclaves", "Args" : ["echo"]}'
Expand All @@ -252,9 +251,10 @@ You can find the Blockchain Explorer configuration files in the `blockchain-expl
Note that `setup.sh` may ask you to override any existing configuration files in `blockchain-explorer` and restore the default configuration.

To start Blockchain Explorer we use docker compose. Just run the following

```bash
cd $FPC_PATH/samples/deployment/test-network/blockchain-explorer
docker-compose up -d
docker compose up -d
```

Once it is up and running you can access the web interface using your browser.
Expand Down
11 changes: 0 additions & 11 deletions samples/deployment/test-network/core_ext.yaml

This file was deleted.

26 changes: 10 additions & 16 deletions samples/deployment/test-network/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
#
# SPDX-License-Identifier: Apache-2.0

version: '3'

services:
# org1
ecc.peer0.org1.example.com:
container_name: ${CC_ID}.peer0.org1.example.com
hostname: ${CC_ID}.peer0.org1.example.com
container_name: peer0org1_${CC_ID}_ccaas
image: fpc/fpc-${CC_ID}${HW_EXTENSION:-}:${FPC_VERSION}
environment:
- CHAINCODE_SERVER_ADDRESS=${CC_ID}.peer0.org1.example.com:9999
- CHAINCODE_SERVER_ADDRESS=0.0.0.0:9999
- CHAINCODE_PKG_ID=${ORG1_ECC_PKG_ID}
- FABRIC_LOGGING_SPEC=${FABRIC_LOGGING_SPEC:-DEBUG}
- SGX_MODE=${SGX_MODE:-SIM}
Expand All @@ -25,11 +22,10 @@ services:
- ${SGX_DEVICE_PATH:-/dev/null}:${SGX_DEVICE_PATH:-/dev/null}

ercc.peer0.org1.example.com:
container_name: ercc.peer0.org1.example.com
hostname: ercc.peer0.org1.example.com
container_name: peer0org1_ercc_ccaas
image: fpc/ercc:${FPC_VERSION}
environment:
- CHAINCODE_SERVER_ADDRESS=ercc.peer0.org1.example.com:9999
- CHAINCODE_SERVER_ADDRESS=0.0.0.0:9999
- CHAINCODE_PKG_ID=${ORG1_ERCC_PKG_ID}
- FABRIC_LOGGING_SPEC=${FABRIC_LOGGING_SPEC:-DEBUG}
- SGX_MODE=${SGX_MODE:-SIM}
Expand All @@ -38,11 +34,10 @@ services:

# org2
ecc.peer0.org2.example.com:
container_name: ${CC_ID}.peer0.org2.example.com
hostname: ${CC_ID}.peer0.org2.example.com
container_name: peer0org2_${CC_ID}_ccaas
image: fpc/fpc-${CC_ID}${HW_EXTENSION:-}:${FPC_VERSION}
environment:
- CHAINCODE_SERVER_ADDRESS=${CC_ID}.peer0.org2.example.com:9999
- CHAINCODE_SERVER_ADDRESS=0.0.0.0:9999
- CHAINCODE_PKG_ID=${ORG2_ECC_PKG_ID}
- FABRIC_LOGGING_SPEC=${FABRIC_LOGGING_SPEC:-DEBUG}
- SGX_MODE=${SGX_MODE:-SIM}
Expand All @@ -55,11 +50,10 @@ services:
- ${SGX_DEVICE_PATH:-/dev/null}:${SGX_DEVICE_PATH:-/dev/null}

ercc.peer0.org2.example.com:
container_name: ercc.peer0.org2.example.com
hostname: ercc.peer0.org2.example.com
container_name: peer0org2_ercc_ccaas
image: fpc/ercc:${FPC_VERSION}
environment:
- CHAINCODE_SERVER_ADDRESS=ercc.peer0.org2.example.com:9999
- CHAINCODE_SERVER_ADDRESS=0.0.0.0:9999
- CHAINCODE_PKG_ID=${ORG2_ERCC_PKG_ID}
- FABRIC_LOGGING_SPEC=${FABRIC_LOGGING_SPEC:-DEBUG}
- SGX_MODE=${SGX_MODE:-SIM}
Expand All @@ -68,5 +62,5 @@ services:

networks:
default:
external:
name: fabric_test
name: fabric_test
external: true
1 change: 0 additions & 1 deletion samples/deployment/test-network/fabric-samples
Submodule fabric-samples deleted from 36ad01
Loading

0 comments on commit 3f10103

Please sign in to comment.