A step-by-step guide to deploying your first Hello World chaincode onto Hyperledger Fabric.
These slides were created by James Bowkett, Principal Consultant at Excelian.
2. Architecture
Fabric has several framework
components:
Membership services for security:
audit, registration & identity
management
Blockchain services: Consensus,
Storage & transactions
Chaincode services: Secure
container & registry
For the hackathon we will focus on
the container and blockchain
storage
2
Chaincode
REST ENDPOINTS
Hyperledger-java
API
Application code
ValidatingPeerNode
Dockercontainer RocksDB storage
Other
Validating
PeersREST
Consensus
Consensus
Consensus
3. Prerequisites
Docker (I tested on 1.12.1 on Mac OS X)
Docker Compose (kitematic is also helpful)
Java/Golang environment for chaincode development (I used JDK 8 + Gradle 3.2)
For application development:
Java/Golang environment
Your favourite development language, as long as you have a REST API available
This guide loosely based on:
http://hyperledger-fabric.readthedocs.io/en/latest/starter/fabric-starter-kit/
3
4. The Environment
4
Starter
container
Member SVCs
Peer node
Container for the member
security services (not strictly
required for the hackathon)
Runs and builds the Fabric Shim
for interaction with Fabric
Runs and builds the java
chaincode
Requires Java + gradle to be
installed in the container
Deploys the Java chaincode
running on peer node into
Fabric
Used for executing
commands against the
running chaincode
5. Docker-compose.yml
membersrvc:
# try 'docker ps' to see the container status after starting this compose
container_name: membersrvc
image: hyperledger/fabric-membersrvc
command: membersrvc
environment:
- COMPOSE_HTTP_TIMEOUT=120
volumes:
- /Users/jbowkett/Documents/Excelian/blockchain/dev-team/git-clone/smart-cheques/starter-
kit/docker-containers/mapped-fs-member:/user/docker-containers/mapped-fs-member
peer:
container_name: peer
image: hyperledger/fabric-peer
environment:
- CORE_PEER_ADDRESSAUTODETECT=true
- CORE_VM_ENDPOINT=unix:///var/run/docker.sock
- CORE_LOGGING_LEVEL=DEBUG
- CORE_PEER_ID=vp0
- CORE_SECURITY_ENABLED=false
# - CORE_SECURITY_PRIVACY=false
- CORE_PEER_PKI_ECA_PADDR=membersrvc:7054
- CORE_PEER_PKI_TCA_PADDR=membersrvc:7054
- CORE_PEER_PKI_TLSCA_PADDR=membersrvc:7054
- CORE_PEER_VALIDATOR_CONSENSUS_PLUGIN=noops
- COMPOSE_HTTP_TIMEOUT=120
# this gives access to the docker host daemon to deploy chain code in network mode
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /Users/jbowkett/Documents/Excelian/blockchain/dev-team/git-clone/smart-cheques/starter-
kit/docker-containers/mapped-fs-peer:/user/docker-containers/mapped-fs-peer
5
# have the peer wait 10 sec for membersrvc to start
# the following is to run the peer in Developer mode - also set sample DEPLOY_MODE=dev
command: sh -c "sleep 10; peer node start --peer-chaincodedev"
#command: sh -c "sleep 10; peer node start"
links:
- membersrvc
ports:
- "5005:5005"
starter:
container_name: starter
image: hyperledger/fabric-starter-kit
volumes:
# tweak this to map a local development directory tree into the container
- /Users/jbowkett/Documents/Excelian/blockchain/dev-team/git-clone/smart-cheques/starter-
kit/docker-containers/mapped-fs-starter:/user/docker-containers/mapped-fs-starter
environment:
- MEMBERSRVC_ADDRESS=membersrvc:7054
- PEER_ADDRESS=peer:7051
- KEY_VALUE_STORE=/tmp/hl_sdk_node_key_value_store
# set to following to 'dev' if peer running in Developer mode
- COMPOSE_HTTP_TIMEOUT=120
- DEPLOY_MODE=dev
- CORE_CHAINCODE_ID_NAME=mycc
- CORE_PEER_ADDRESS=peer:7051
# the following command will start the chain code when this container starts and ready it for
deployment by the app
# command: sh -c "sleep 20;
/opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02/chai
ncode_example02"
stdin_open: true
tty: true
links:
- membersrvc
- peer
6. Bootstrap the Docker environments
Edit the volumes sections of your docker-compose.yml to map appropriate
directories for getting code onto your nodes
Make sure those paths exist on your development (and “production” machine)
Go to the directory the docker-compose.yml is installed:
$ docker-compose up -d
6
10. Install the chaincode shim into the gradle repo
peer$ cd $GOPATH/src/github.com/hyperledger/fabric/core/chaincode/shim/java
peer$ gradle clean build
10
11. The story so far….
We’ve created the 3 Hyperledger docker containers (starter, membersvc and peer)
with mounted directories on our development machine
Provisioned the peer node by installing the correct version of Java and Gradle
Built the Hyperledger shim on the peer node and installed it as an available
dependency for later Gradle builds on the peer node
Now let’s deploy an example chaincode application….
11
12. Install your chaincode on the peer node and run the service
import org.hyperledger.java.shim.ChaincodeBase;
import org.hyperledger.java.shim.ChaincodeStub;
public class HelloWorldChaincode extends ChaincodeBase {
public HelloWorldChaincode() { }
public String run(ChaincodeStub chaincodeStub, String function, String[] args) {
return "hello world!";
}
public String query(ChaincodeStub chaincodeStub, String function, String[] args) {
return "hello world query";
}
public String getChaincodeID() {
return "HelloWorldChaincode";
}
public static void main(String[] args) {
new HelloWorldChaincode().start(args);
}
}12
$ cp <hello world chaincode src> <mapped dir for peer>
14. A quick recap….
1. Create the 3 Hyperledger Docker containers (starter, membersvc and peer) with
mapped directories to our development machine
2. Provision the peer node by logging into the Docker container and installing the
correct version of Java and Gradle
3. Build the Hyperledger shim on the peer node and install it as an available
dependency for later Gradle builds on the peer node
4. Create a HelloWorld chaincode on our development machine & copy the code+build
script to the peer node using our Docker mapped-directory
5. On the starter node, deploy the chaincode into Fabric, and then use the starter node
to query the running chaincode within fabric
14
15. Some APIs and Examples to be aware of
Table creation:
final List<TableProto.ColumnDefinition> columnDefs = new ArrayList<>();
columnDefs.add(newBuilder().setName("hash").setKey(true).setType(STRING).build());
columnDefs.add(newBuilder().setName("messages").setKey(false).setType(STRING).build());
final boolean success = chaincodeStub.createTable(TABLE_NAME, columnDefs);
Lookup is based on the key column only
https://github.com/hyperledger/fabric/tree/master/examples/chaincode/java
Examples:
LinkExample – Invoking one chaincode from another
MapExample – Simple map storage
RangeExample – Query storage with range
SimpleExample – Transferring value using Arguments
TableExample – C/R/D Using underlying storage tables
15
16. Useful links
https://docs.docker.com/engine/installation/
https://docs.docker.com/engine/installation/linux/linux-postinstall/ Important on
Linux if running Docker on boot
http://hyperledger-fabric.readthedocs.io
http://hyperledger-fabric.readthedocs.io/en/latest/starter/fabric-starter-kit/
https://github.com/jbowkett/zug-hackathon
https://github.com/hyperledger/fabric/tree/master/examples/chaincode/java
https://gradle.org/documentation/
16