Now you will create your own permissioned blockchain. For that, you will bring up a network, create a channel, join a peer to that channel, install and instantiate a smart contract (aka chaincode in Hyperledger Fabric). After that, you will issue a transaction in the chaincode, change the source code of the chaincode and upgrade the network with the new smart contract. In Part I we will provide step-by-step instructions and in Part II you must complete the instructions on your own.
To accomplish that, we recommend you use a UNIX-like machine (Linux/macOS). Besides that, we will need to install cURL and Docker.
Note: Whenever you want to start again fresh, make sure you clear your Docker containers and volumes.
-
Install cURL (https://curl.haxx.se/download.html)
-
Install Docker and Docker Compose (https://www.docker.com/)
-
Install Go (https://golang.org/dl/)
-
Fork the repository https://github.com/goledgerdev/goledger-challenge
- Fork it, do NOT clone it, since you will need to send us your forked repository
- If you cannot fork it, create a private repository and give access to
samuelvenzi
- Make sure you the repository is in your
$GOPATH
-
Download the Docker images of Hyperledger
curl -sSL http://bit.ly/2ysbOFE | bash -s -- 2.5.12 2.5.12 1.5.15
- Make sure that the Fabric binaries are in your
$PATH
- Make sure that the Fabric binaries are in your
Go inside the folder /goledger-network. Now, we need to generate the certificates that will be used by the peer and the orderer so that they can sign and validate transactions.
./bgldgr.sh generate
This command will create the needed certificates and the channel artifacts.
Assuming that you have installed Docker and Docker Compose, the following command will bring up the network elements.
docker-compose -f docker-compose-cli.yaml up -d
You should see the following output:
Creating orderer.example.com ... done
Creating peer0.org1.example.com ... done
Creating cli ... done
When deploying a local network, we can use the cli
container to do operations in the network. For that, we need to be inside the container. Use the following command to get inside it.
docker exec -it cli bash
Create the channel.
peer channel create \
-o orderer.example.com:7050 \
-c mychannel \
-f ./channel-artifacts/channel.tx \
--tls \
--cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
Join the channel.
peer channel join -b mychannel.block
From the /chaincode/fabcar/go
folder, download the chaincode package dependencies.
go mod vendor
Inside the cli
container navigate to /opt/gopath/src/github.com/chaincode/fabcar/go
.
cd /opt/gopath/src/github.com/chaincode/fabcar/go
Package the chaincode.
peer lifecycle chaincode package fabcar.tar.gz \
--path github.com/chaincode/fabcar/go/ \
--lang golang \
--label fabcar_1.0
With the fabcar.tar.gz
package generated, install the chaincode.
peer lifecycle chaincode install fabcar.tar.gz
Check the installation status of the chaincode inside the peer.
peer lifecycle chaincode queryinstalled
Print your terminal and save it as inst0.jpg
From the previous step, copy and save the Package ID from the installed chaincode. (Ex: fabcar_1.0:93c9df9bf47532689ed6d78654787557740439fdd7435e06b5dcc63e07fe3cea
)
Now, approve the chaincode definition for the organization. Substitute <PACKAGE_ID>
on the command with the value saved before.
peer lifecycle chaincode approveformyorg \
-o orderer.example.com:7050 \
--ordererTLSHostnameOverride orderer.example.com \
--channelID mychannel \
--name fabcar \
--version 1.0 \
--package-id <PACKAGE_ID> \
--sequence 1 \
--tls \
--cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
Check the commit readiness from the chaincode. It's expected that the approval value is set to true
.
peer lifecycle chaincode checkcommitreadiness \
--channelID mychannel \
--name fabcar \
--version 1.0 \
--sequence 1 \
--tls \
--cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem \
--output json
Print your terminal and save it as inst1.jpg
Commit the chaincode defintion to the channel
peer lifecycle chaincode commit \
-o orderer.example.com:7050 \
--ordererTLSHostnameOverride orderer.example.com \
--channelID mychannel \
--name fabcar \
--version 1.0 \
--sequence 1 \
--tls \
--cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem \
--peerAddresses peer0.org1.example.com:7051 \
--tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
Check if the chaincode was successfully commited to the channel.
peer lifecycle chaincode querycommitted \
--channelID mychannel \
--name fabcar
Print your terminal and save it as inst2.jpg
The chaincode you just instantiated in your newly created channel is called FABCAR and is one of the sample chaincodes provided by Hyperledger. This chaincodes takes care of registering and managing cars and their characteristics like (manufacturer, model, color and owner). The source code is under /chaincode/fabcar/go
in the root folder of the repository. Make sure you take a look at the code so that you know what it’s about.
Your chaincode is instantiated clean, so let’s initialize the ledger with a few cars. The FABCAR chaincode provides a operation so that you can do that with ease. To invoke a transaction you can run the following command (inside the cli
container):
peer chaincode invoke -o orderer.example.com:7050 -C mychannel -n fabcar -c '{"function":"initLedger","Args":[]}' --waitForEvent --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
Now check the cars in the ledger:
peer chaincode query \
-C mychannel \
-n fabcar \
-c '{"Args":["queryAllCars"]}'
Print your terminal and save it as query0.jpg
Now that you have your network up and running with the FABCAR chaincode, you must accomplish the following tasks:
-
Query only the car identified by key
CAR0
- print your terminal, save it as
query1.jpg
- print your terminal, save it as
-
Change the owner of that car to anyone you want
- query that car again, print your terminal, save it as
query2.jpg
- query that car again, print your terminal, save it as
-
Change the source code of the chaincode and add the optional attribute “Year" to the cars’ attributes
-
Install and upgrade your new chaincode in the network
- check the instantiated chaincodes, print your terminal, save it as
inst1.jpg
- check the instantiated chaincodes, print your terminal, save it as
-
Create one new car
- query your newly created car, print your terminal, save it as
query3.jpg
- query your newly created car, print your terminal, save it as
-
Create a folder
prints
in the root of the repository and put the images you generated inside it -
Commit your changes (
prints
folder and source code) to your forked repository
To complete the challenge, you must send us the link to your repository with the alterations you made.