This docker-compose example has been adapted from a Fabric 101 Workshop which was adapted from the basic network and fabcar example in the Fabric Samples. This example does not use TLS, which means the Fabric Go SDK cannot be used to interact with the network. Currently, there are two orgs, one peer, one orderer, and one fabric-ca in the network.
- core-fpc.yaml : Peer configuration that has the SGX plugins and locations relative to location within docker image
- core.yaml : Regular Peer configuration
without FPC. Used if
$USE_FPC
is setfalse
. - orderer.yaml : Orderer configuration
- crypto-config.yaml : File used with cryptogen to generate certs for specified number of orgs, peers, users, and orderer. The CA credentials can be used to start instances of fabric-ca
- configtx.yaml : File used with configtxgen to generate the genesis block which is used as the basis of the specified channel
- docker-compose.yml : Configuration of the
fabric network to be used with
docker-compose
. This file depends on two environment variables to properly bring up a network.$FPC_CFG
can be set to-fpc
or shall be empty. If set to-fpc
thecore-fpc.yaml
& FPC peer image is used. Otherwise it will usecore.yaml
and the regular peer image.$PEER_CMD
must also be set to the location of binary or script that will start the peer. Docker version 17.06.2-ce or higher is needed
The quickest way to get up and running is to simply execute
scripts/start.sh
This will create all necessary installation artifacts and start the
network.
If your environment variable SGX_MODE
is set to hardware, the network will run
the peer also with SGX hardware mode enabled, otherwise it will run in SGX simulation mode.
If you set the environment variable USE_EXPLORER
to true
, the network will include
and start the Hyperledger Explorer on
port 8090. This will enable you to inspect the networks,
e.g., processed transactions.
If you set the environment variable USE_COUCHDB
to true
, the peer will use couchdb
to store the local version of the ledger and you can inspect the peer's ledger state
on port 5984 (login as user admin
with password adminPassword
).
For more information in the steps involved, continue reading the following section. Otherwise, you can skip to the Section on Chaincode Installation.
-
Build the peer image in
utils/docker/peer
directory which is defined by the peer Dockerfile. This step assumes you have already built the fabric-private-chaincode base image. Take a look at building the docker dev environment in the main README. After you have created the base image, run the following to create a modified peer image and the plugins necessary to start the peer.$FPC_PATH
is the location fabric-private-chaincode repository on your host machine.cd $FPC_PATH/utils/docker/peer docker build -t hyperledger/fabric-peer-fpc .
By default the image will clone the master branch on https://github.com/hyperledger-labs/fabric-private-chaincode. If you want to use a different fork of the repo or a different branch you provide
FPC_REPO_URL
andFPC_REPO_BRANCH_TAG_OR_COMMIT
as build args.cd $FPC_PATH/utils/docker/peer docker build -t hyperledger/fabric-peer-fpc --build-arg FPC_REPO_URL=<repo-url> --build-arg FPC_REPO_BRANCH_TAG_OR_COMMIT=<repo-branch> .
If you want to build the peer image using your local copy of your repo you can use the same build args, but specify
file:///tmp/build-src/.git
as theFPC_REPO_URL
. You will also need to create the image at the root of this repo so that the local repo will be in the build context for the docker daemon.cd $FPC_PATH docker build -t hyperledger/fabric-peer-fpc -f utils/docker/peer/Dockerfile --build-arg FPC_REPO_URL=file:///tmp/build-src/.git --build-arg FPC_REPO_BRANCH_TAG_OR_COMMIT=$(git rev-parse HEAD) .
Note: as this last scenario might be a common development action, it is defined as a makefile target
peer
in$FPC_PATH/utils/docker/Makefile
. -
Download the necessary fabric binaries. Run the bootstrap script which will download the Fabric 1.4.3 binaries into
$FPC_PATH/utils/docker-compose/bin
directory as well as download also all fabric docker images that version. If you already have the binaries downloaded and in located in yourPATH
, this step can be skipped. If you don't want download the docker images, you can also skip that part by passing option-d
to thebootstrap.sh
script (docker-compose will download them later if they are missing locally.) Fabric 1.4.3 versions of configtxgen and are required to use the configurations above.cd $FPC_PATH/utils/docker-compose scripts/bootstrap.sh
-
Generate the cryptographic material needed for the network by running the generate script. Cryptogen will be used to generate all the credentials needed based on the configuration filesabove and place them in the
network-config/crypto-config
directory. Configtxgen will be used to create the genesis block which is used to start up the orderer as well as the peer create channel configuration transaction. These will be placed in thenetwork-config/config
directory. Thecrypto-config
&config
directory will be mounted into every container of the FPC network as specified in the docker-compose file. This script is not idempotent and will delete the contents ofcrypto-config
&config
when run to ensure a clean start.scripts/generate.sh
-
Start the network. Run the start script. This will use docker-compose to start the network as well as starting the channel
mychannel
. By default, this script will use FPC peers. If non FPC peers are desired, set$USE_FPC
tofalse
.scripts/start.sh
Note
- if some of steps 1 to 3 were omitted before running start.sh, the script will perform the missing steps in the default configuration
- the script returns to you an export statements with environment variables
which enable you to easily run
docker-compose
commands such asps
,top
,logs
and alike. Just copy/paste the export statement into your shell and you can get, e.g., the container status with${DOCKER_COMPOSE} ps
.
The examples and demo directories has been
mounted into the peer container for convenience, under
/project/src/github.com/hyperledger-labs/fabric-private-chaincode/examples
and
/project/src/github.com/hyperledger-labs/fabric-private-chaincode/demo
.
NOTE If you are running a normal fabric network, the rest of the tutorial
will not work.
-
Follow the steps in the tutorial to build your chaincode outside of the peer container. Do not continue to the testing step. Though this tutorial references the hello world example, users can also deploy other FPC examples, e.g., the echo or auction examples, where the code is provided in the git repo out-of-the-box. Follow similar steps as outlined below with corresponding changes of chaincode name and queries/transactions.
The rest of these steps should be done within the peer container.
-
Exec into the peer container.
docker exec -it peer0.org1.example.com bash
-
There also some a number of useful predefined environment variables such as the orderer address, the channel name, the peer command to use and the credentials used by fabric.
echo CORE_PEER_MSPCONFIGPATH=${CORE_PEER_MSPCONFIGPATH}\ ORDERER_ADDR=${ORDERER_ADDR}\ CHANNEL_NAME=${CHANNEL_NAME}\ PEER_CMD=${PEER_CMD}\ CORE_PEER_MSPCONFIGPATH=${CORE_PEER_MSPCONFIGPATH}
Note that to save you from explicitly having to specify CORE_PEER_MSPCONFIGPATH on each call, we defined them in
docker-compose.yml
as admin credentials. This means though also that your peer will run with admin instead of peer credentials. If you have role-specific endorsement policies, you might have to comment out the corresponding definition in docker-compose.yml to peer credentials and then manually define the corresponding value here in the docker exec shell. -
Install your chaincode.
${PEER_CMD} chaincode install -l fpc-c -n helloworld_test -v 0 -p examples/helloworld/_build/lib
-
Instantiate your chaincode
${PEER_CMD} chaincode instantiate -C mychannel -n helloworld_test -v 0 -c '{"Args":["init"]}' -V ecc-vscc
-
Store asset1 with a value of a 100
${PEER_CMD} chaincode invoke -C mychannel -n helloworld_test -c '{"Args":["storeAsset","asset1","100"]}'
-
Retrieve the current value of asset1.
${PEER_CMD} chaincode query -C mychannel -n helloworld_test -c '{"Args":["retrieveAsset","asset1"]}'
The response should look like the following:
{ "ResponseData":"YXNzZXQxOjEwMA==", "Signature":<signature>, "PublicKey": <public-key> }
-
Verify the encrypted response data shows that asset1 is equal to a hundred.
> echo "YXNzZXQxOjEwMA==" | base64 -d asset1:100
-
Enter into the
node-sdk
directory, to use the node sdk scripts to create new users.cd node-sdk
-
Ensure you have all the node modules
npm install
-
Enroll as the admin download the admin credentials
node enrollAdmin.js
After running this, the directory
wallet/admin
should have been created and have public and private key pair. NOTE These credentials are not an admin in the network, but just the admin for Fabric-CA and have the ability to register more users. -
Register another user and download the credentials.
node registerUser.js <username>
After running this with your desired username, the directory
wallet/<username>
should have been created and have the public and private key pair.
NOTE: You must run peer invoke for this chaincode once using the peer cli commands in the peer container before you can use these node sdk scripts
-
Ensure you have all the node modules
npm install
-
Query the asset you stored previously
node query.js <username> mychannel helloworld_test retrieveAsset asset1
The response should look similar to what you saw above when you queried using the peer cli.
Transaction has been submitted, result is: { "ResponseData":"YXNzZXQxOjEwMA==", "Signature":<signature>, "PublicKey":<public-key> }
In general the query script works as:
node query.js <identity-to-use> <channel-name> <chaincode-id> <args>...
-
To invoke a transaction:
node invoke.js <username> mychannel helloworld_test storeAsset asset2 200
The response should look like the following:
Transaction has been submitted, result is: { "ResponseData":"T0s=", "Signature":<signature>, "PublicKey":<public-key> }
In general the invoke script works as:
node invoke.js <identity-to-use> <channel-name> <chaincode-id> <args>...
- Run the teardown script to clean up your environment.
To do a clean state teardown, add option
--clean-slate
. NOTE--clean-slate
will try to remove all anonymouse volumes (which includes the state of the CA, the wallets in${FPC_PATH}/utils/docker-compse/node-sdk
as well as all your containers and left-over chaincode containers.scripts/teardown.sh