AWS ManagedBlockchain + Hyperledger Explorer quickstart (2)

rdg7739
9 min readMar 7, 2020

--

Easy steps to connect Hyperledger explorer with AWS ManagedBlockchain.

In this post, I’ll walk through each step in the AWS workshop. If you have not setup your environment yet, please check my first post of this series. I have updated a few scripts from my Github repo after upload the first post. Please run ‘git pull’ to have an updated version.

Step 0: Start Cloud9 IDE

After login to AWS console, open the cloud9 IDE that you set up in session 1. When you open up the terminal, you will need to import your bash profile to get updated paths and variables

source ~/fabric_exports

If you like to load automatically, please add that line at the bottom of ‘~/.bash_profile’ file

Please run below to verify if variables loaded properly

echo $MEMBERID
echo $NETWORKID

Step 1: Deploy Chaincode on a Private Channel

Generate genesis block

A genesis block(i.e. block 0) is the first block of the blockchain. Your blockchain will use this genesis block to start the chain. A Genesis block contains the configuration of how a channel is created, from configtx.yaml.

So configtx.yaml file needs to be updated with the Name and ID fields with the member ID from Managed Blockchain.

cp ~/environment/bank-transfer-blockchain-reinvent2019-workshop/setup/private-configtx.yaml ~/configtx.yaml
sed -i "s|__MEMBERID__|$MEMBERID|g" ~/configtx.yaml

Let’s create our first blockchain block(genesis block)! Please provides the unique channel name without space

export CHANNEL=<UNIQUE CHANNEL NAME>echo "export CHANNEL=$CHANNEL" >> ~/fabric_exports
#Generates genesis block(i.e. Block 0)
docker exec cli configtxgen -outputCreateChannelTx /opt/home/$CHANNEL.pb -profile OneOrgChannel -channelID $CHANNEL --configPath /opt/home/

You should see the following output and generates genesis block as ‘~/opt/home/$CHANNEL.pb’

2019-11-20 23:34:56.190 UTC [common/tools/configtxgen] doOutputChannelCreateTx -> INFO 005 Writing new channel tx
  • If you are getting an error, such as
Error response from daemon: Container 543588c11295ab29f88d72d31c1960f68e60d88bc5f802281eeffc785634f2fd is not running

Then, please run below to start up the docker container

docker-compose -f ~/docker-compose-cli.yaml up &

Create and join to the Channel

We will run the blockchain commends inside of the ‘cli’ docker container. Let’s get inside!

docker exec -it -e "CORE_PEER_TLS_ENABLED=true" \
-e "CORE_PEER_TLS_ROOTCERT_FILE=/opt/home/managedblockchain-tls-chain.pem" \
-e "CORE_PEER_ADDRESS=$PEER" \
-e "CORE_PEER_LOCALMSPID=$MSP" \
-e "CORE_PEER_MSPCONFIGPATH=$MSP_PATH" \
-e "CHANNEL=$CHANNEL" \
-e "ORDERER=$ORDERER" \
cli /bin/bash
  1. Create a channel:
peer channel create -c $CHANNEL \
-f /opt/home/$CHANNEL.pb -o $ORDERER \
--cafile /opt/home/managedblockchain-tls-chain.pem --tls

You should see the output as

2020-03-05 01:29:31.503 UTC [cli/common] readBlock -> INFO 014 Got status: &{NOT_FOUND}
2020-03-05 01:29:31.515 UTC [channelCmd] InitCmdFactory -> INFO 015 Endorser and orderer connections initialized
2020-03-05 01:29:31.718 UTC [cli/common] readBlock -> INFO 016 Got status: &{NOT_FOUND}
2020-03-05 01:29:31.732 UTC [channelCmd] InitCmdFactory -> INFO 017 Endorser and orderer connections initialized
2020-03-05 01:29:31.939 UTC [cli/common] readBlock -> INFO 018 Received block: 0

2. Join your peer to the channel

peer channel join -b $CHANNEL.block \
-o $ORDERER --cafile /opt/home/managedblockchain-tls-chain.pem --tls

You should see the output as

2020-03-05 01:32:26.966 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2020-03-05 01:32:27.125 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel

Step 2: Install and Instantiate Chaincode

Now, We have set up the channel, we will deploy three Chaincodes to the channel, which contains blockchain’s business logic. If you are still inside of the docker container, please exit by typing ‘exit’ in the terminal.

Preparing Chaincode

Let’s copy Chaincode to $GOPATH and download dependencies

cp -r ~/environment/bank-transfer-blockchain-reinvent2019-workshop/chaincode/* ~/go/#Add symbolic link for easy modification from cloud9
cd ~/environment/
ln -s ~/go
cd ~/go/src/bank
go get -t

Install and Instantiate Chaincode

Let’s provides the Chaincode name of your virtual bank!

export BANKCHAINCODENAME=<YOUR FICTIONAL BANK NAME>
export FOREXCHAINCODENAME=$BANKCHAINCODENAME-forex
export INTENRBANKCHAINCODENAME=$BANKCHAINCODENAME-interbank
#Saving values to the ~/fabric_exports
echo "export BANKCHAINCODENAME=$BANKCHAINCODENAME" >> ~/fabric_exports
echo "export FOREXCHAINCODENAME=$BANKCHAINCODENAME-forex" >> ~/fabric_exports
echo "export INTENRBANKCHAINCODENAME=$BANKCHAINCODENAME-interbank" >> ~/fabric_exports

Again, We will run the blockchain commends inside of ‘cli’ docker container with Chaincode names. Let’s get inside!

docker exec -it -e "CORE_PEER_TLS_ENABLED=true" \
-e "CORE_PEER_TLS_ROOTCERT_FILE=/opt/home/managedblockchain-tls-chain.pem" \
-e "CORE_PEER_ADDRESS=$PEER" \
-e "CORE_PEER_LOCALMSPID=$MSP" \
-e "CORE_PEER_MSPCONFIGPATH=$MSP_PATH" \
-e "CHANNEL=$CHANNEL" \
-e "ORDERER=$ORDERER" \
-e "BANKCHAINCODENAME=$BANKCHAINCODENAME" \
-e "FOREXCHAINCODENAME=$FOREXCHAINCODENAME" \
-e "INTENRBANKCHAINCODENAME=$INTENRBANKCHAINCODENAME" \
cli /bin/bash

Install and Instantiate $BANKCHAINCODENAME

peer chaincode install -n $BANKCHAINCODENAME -v v0 -p bank/cmd
peer chaincode instantiate -o $ORDERER -C $CHANNEL -n $BANKCHAINCODENAME -v v0 -c '{"Args":["The Royal Bank of Cloud", "0001", "'$FOREXCHAINCODENAME'"]}' --cafile /opt/home/managedblockchain-tls-chain.pem --tls

Install and Instantiate $FOREXCHAINCODENAME

peer chaincode install -n $FOREXCHAINCODENAME -v v0 -p forex/cmd
peer chaincode instantiate -o $ORDERER -C $CHANNEL -n $FOREXCHAINCODENAME -v v0 -c '{"Args":[]}' --cafile /opt/home/managedblockchain-tls-chain.pem --tls

You will be able to check with below commends

#Check installed chaincode list
peer chaincode list --installed
#Check instantiated chaincode list in channel $CHANNEL
peer chaincode list --instantiated -C $CHANNEL

Invoke & Query Chaincode

Here are example commands to invoke and query from Chaincode. Feel free to play around with below commends

#Create Account 1
peer chaincode invoke -C $CHANNEL -n $BANKCHAINCODENAME -c '{"Args":["createAccount", "Jonathan Shapiro-Ward", "0000001", "500000", "USD"]}' --cafile /opt/home/managedblockchain-tls-chain.pem --tls
#Create Account 2
peer chaincode invoke -C $CHANNEL -n $BANKCHAINCODENAME -c '{"Args":["createAccount", "Joe Smith", "0000002", "100", "USD"]}' --cafile /opt/home/managedblockchain-tls-chain.pem --tls
#Create Account3 with 'GBP' currency
peer chaincode invoke -C $CHANNEL -n $BANKCHAINCODENAME -c '{"Args":["createAccount", "Lisa Simpson", "0000003", "50", "GBP"]}' --cafile /opt/home/managedblockchain-tls-chain.pem --tls
#Query Account
peer chaincode invoke -C $CHANNEL -n $BANKCHAINCODENAME -c '{"Args":["queryAccount", "0000001"]}' --cafile /opt/home/managedblockchain-tls-chain.pem --tls
#Transfer between accounts
peer chaincode invoke -C $CHANNEL -n $BANKCHAINCODENAME -c '{"Args":["transfer", "0000001", "0001", "0000002", "10"]}' --cafile /opt/home/managedblockchain-tls-chain.pem --tls
#Check Historical State of an Account
peer chaincode invoke -C $CHANNEL -n $BANKCHAINCODENAME -c '{"Args":["getTransactionHistory", "0000001"]}' --cafile /opt/home/managedblockchain-tls-chain.pem --tls
#exchange rate for USD to GBP
peer chaincode invoke -o $ORDERER -C $CHANNEL -n $FOREXCHAINCODENAME -c '{"Args":["createUpdateForexPair", "USD", "GBP", "0.8"]}' --cafile /opt/home/managedblockchain-tls-chain.pem --tls
#Query exchainge rate
peer chaincode query -C $CHANNEL -n $FOREXCHAINCODENAME -c '{"Args":["getForexPair","USD", "GBP"]}'

Step3: (Advanced, Optional) — Implement Interbank Transfer

The interbank Chaincode acts as a router between different banks. The BankChaincode sending the account invokes the interbankTransfer function of the InterbankChaincode. The transfer function looks up the Bank ID of the receiving bank on the ledger, if it finds a record it retrieves the name of the receiving bank’s Chaincode from that record, it subsequently invokes the deposit function of the receiving BankChaincode.

However, if you attempt an interbank transfer, the Chaincode will fail. The logic is not yet implemented. The updated Chaincode is provided at the ‘solution’ directory. To use Interbank Chaincode, we have to copy the implemented Chaincode to Chaincode repo and let the network use updated bank Chaincode. So this time, we have to use ‘update’ instead of ‘instantiatefunction

Install and update $BANKCHAINCODENAME

#Exit from 'cli' docker container
#Chaincode need to use transfer.go from /solution
cp ~/environment/bank-transfer-blockchain-reinvent2019-workshop/solution/transfer.go ~/go/src/bank/transfer.go
#Re-enter into the 'cli' container
docker exec -it -e "CORE_PEER_TLS_ENABLED=true" \
-e "CORE_PEER_TLS_ROOTCERT_FILE=/opt/home/managedblockchain-tls-chain.pem" \
-e "CORE_PEER_ADDRESS=$PEER" \
-e "CORE_PEER_LOCALMSPID=$MSP" \
-e "CORE_PEER_MSPCONFIGPATH=$MSP_PATH" \
-e "CHANNEL=$CHANNEL" \
-e "ORDERER=$ORDERER" \
-e "BANKCHAINCODENAME=$BANKCHAINCODENAME" \
-e "FOREXCHAINCODENAME=$FOREXCHAINCODENAME" \
-e "INTENRBANKCHAINCODENAME=$INTENRBANKCHAINCODENAME" \
cli /bin/bash
peer chaincode install -n $BANKCHAINCODENAME -v v1 -p bank/cmd
peer chaincode upgrade -o $ORDERER -C $CHANNEL -n $BANKCHAINCODENAME -v v1 -c '{"Args":["The Royal Bank of Cloud", "0001", "'$FOREXCHAINCODENAME'", "'$INTENRBANKCHAINCODENAME'"]}' --cafile /opt/home/managedblockchain-tls-chain.pem --tls

Install and instantiate $INTENRBANKCHAINCODENAME

peer chaincode install -n $INTENRBANKCHAINCODENAME -v v0 -p interbank/cmd
peer chaincode instantiate -n $INTENRBANKCHAINCODENAME -v v0 -C $CHANNEL -c '{"Args":[]}' --cafile /opt/home/managedblockchain-tls-chain.pem --tls

Create a Second Bank

Now, Let’s create a second bank. This time, we will pass two extra parameters: $FOREXCHAINCODENAME and $INTENRBANKCHAINCODENAME. This enables this new bank to perform the foreign exchange and interbank transfer. Replace “<BANK NAME 2>” with a name that is unique, and without spaces.

To use the BankChaincode a second time, we have to install it under a different name.

export SECONDBANKCHAINCODENAME=<FICTIONAL BANK NAME 2>
peer chaincode install -n $SECONDBANKCHAINCODENAME -v v0 -p bank/cmd
peer chaincode instantiate -o $ORDERER -C $CHANNEL -n $SECONDBANKCHAINCODENAME -v v0 -c '{"Args":["The Internet Bank", "0005", "'$FOREXCHAINCODENAME'", "'$INTENRBANKCHAINCODENAME'"]}' --cafile /opt/home/managedblockchain-tls-chain.pem --tls
#Create new account with new bank
peer chaincode invoke -C $CHANNEL -n $SECONDBANKCHAINCODENAME -c '{"Args":["createAccount", "Mert Hocanin", "101010", "5000", "USD"]}' --cafile /opt/home/managedblockchain-tls-chain.pem --tls

Register a Route To Each of Your Banks

Now, we need to register a route with the InterbankChaincode. This is a callback that allows the InterbankChaincode to invoke the deposit function on your BankChaincode.

We register a route from bank ID 0001 to $BANKCHAINCODENAME, and from 0005 to $SECONDBANKCHAINCODENAME.

peer chaincode invoke -n $INTENRBANKCHAINCODENAME  -C $CHANNEL -c '{"Args":["registerRoute", "0001","'$BANKCHAINCODENAME'", "'$FOREXCHAINCODENAME'"]}' --cafile /opt/home/managedblockchain-tls-chain.pem --tls
peer chaincode invoke -n $INTENRBANKCHAINCODENAME -C $CHANNEL -c '{"Args":["registerRoute", "0005","'$SECONDBANKCHAINCODENAME'", "'$FOREXCHAINCODENAME'"]}' --cafile /opt/home/managedblockchain-tls-chain.pem --tls

Perform an Interbank Transfer

Now, perform a transfer. Feel free to play around with below commends

#Transfer Money From Bank B to Bank A
peer chaincode invoke -C $CHANNEL -n $SECONDBANKCHAINCODENAME -c '{"Args":["transfer", "101010", "0001", "0000001", "1"]}' --cafile /opt/home/managedblockchain-tls-chain.pem --tls
#Check history
peer chaincode invoke -C $CHANNEL -n $BANKCHAINCODENAME -c '{"Args":["getTransactionHistory", "0000001"]}' --cafile /opt/home/managedblockchain-tls-chain.pem --tls
#Transfer Money From Bank A to Bank B
peer chaincode invoke -C $CHANNEL -n $BANKCHAINCODENAME -c '{"Args":["transfer", "0000001", "0005", "101010", "1"]}' --cafile /opt/home/managedblockchain-tls-chain.pem --tls

Step 4: Configure and Deploy the REST API

Now, we have set up the Blockchain Network. Now we will start configuring Rest API to connect to Blockchain. Start a new terminal by clicking on the (+) button in the tabs.

#Install dependencies
cd ~/environment/bank-transfer-blockchain-reinvent2019-workshop/api
npm install
#Generate Connection profile
chmod +x ~/environment/bank-transfer-blockchain-reinvent2019-workshop/api/connection-profile/gen-profile.sh
./connection-profile/gen-profile.sh

Confirm that the connection profile was generated correctly:

cat ~/environment/bank-transfer-blockchain-reinvent2019-workshop/tmp/connection-profile/bank-connection-profile.yaml

Edit the Config

Specify the name of your channel and the name of your bank Chaincode at the config.json file and start the Rest API.

echo $CHANNEL
echo $BANKCHAINCODENAME
vi ~/environment/bank-transfer-blockchain-reinvent2019-workshop/api/config.json#Start the REST APU
cd ~/environment/bank-transfer-blockchain-reinvent2019-workshop/api/
./start.sh

Invoke a Transfer Using the REST API

Start a new terminal by clicking on the (+) button in the tabs and start call Rest API. Feel free to play with different variables

source ~/fabric_exports#Get details of account 0000001
curl http://localhost:8081/account/0000001
#Output: [{"name":"Jonathan Shapiro-Ward","id":"0000001","balance":"50","currency":"USD"}]#Request Transfer
curl -s -X POST http://localhost:8081/transfer -H "Content-Type: application/json" -d '{"FromAccNumber":"0000001", "ToBankID": "0001", "ToAccNumber": "0000002", "Amount": "1"}'
#Check Transactions
curl http://localhost:8081/transactions/0000001

Step5: Deploy the Web App

Blockchain and Rest API is ready to use. Now we need to set up friendly UI for the End-user!

#Installing dependencies
cd ~/environment/bank-transfer-blockchain-reinvent2019-workshop/ui
npm install -g @angular/cli
npm install

Create ui_start.sh scripts at ‘~/environment’

cp ~/environment/bank-transfer-blockchain-reinvent2019-workshop/ui/src/environments/environment_template.ts ~/environment/bank-transfer-blockchain-reinvent2019-workshop/ui/src/environments/environment.ts
SERVER_IPADDRESS=$(curl http://169.254.169.254/latest/meta-data/public-ipv4)
echo SERVER_IPADDRESS: $SERVER_IPADDRESS
sed -i "s|%SERVER_IPADDRESS%|$SERVER_IPADDRESS|g" ~/environment/bank-transfer-blockchain-reinvent2019-workshop/ui/src/environments/environment.ts
sed -i "s|%BANK_NAME%|<YOUR BANK NAME>|g" ~/environment/bank-transfer-blockchain-reinvent2019-workshop/ui/src/environments/environment.ts
cd ~/environment/bank-transfer-blockchain-reinvent2019-workshop/ui
ng serve --port 8080 --disableHostCheck true --host 0.0.0.0

If you see the following error, re-run “npm install”:

An unhandled exception occurred: Could not find module "@angular-devkit/build-angular" from "/home/ec2-user/environment/bank-transfer-blockchain-reinvent2019-workshop/ui"

Open the Security Group To Allow 8080 and 8081 From Your IP to the Cloud9 Instance

  • Navigate to the EC2 console
  • Select Security Groups on the Left Hand Side
  • Select the security group for your Cloud9 instance (The Group Name will start with “aws-cloud9-”)
  • Add a new inbound rule allowing TCP ports 8080–8081 from your IP

View the Web App

In your browser, navigate to http://<public-ip-of-the-cloud-instance>:8080 you can find the public Ip of the instance by using the command below.

curl http://169.254.169.254/latest/meta-data/public-ipv4

In the web app, you can log in using an account number and then view the current balance, historical transactions, and make transactions. Try logging in as John using Account ID: 0000001.

  • Note: If you are not able to access the UI, it may be because of other firewall rules not allowing traffic to port 8080. If you are on a corporate VPN, then you may need to disconnect.

Voila! Now we have complete on setting up Blockchain network, Rest API and Web application. I’ll not cover ‘Events and Analytics’ or ‘Set up a shared channel’ from the AWS workshop in this series. If you want to follow these steps, please go ahead and come back for Hyperledger Explorer Configurations in the next post!

Thank you for reading! Let me know in a comment what you liked about this post or not.

--

--