IBM/android-kubernetes-blockchain

Name: android-kubernetes-blockchain

Owner: International Business Machines

Description: **work-in-progress**

Created: 2018-04-25 22:11:01.0

Updated: 2018-05-17 07:11:13.0

Pushed: 2018-05-17 07:11:12.0

Homepage: null

Size: 12347

Language: Java

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Create an Android app with Blockchain Integration

In this code pattern, we will create an Android app that tracks the user steps with Blockchain capabalities using Kubernetes. The users are registered to the Blockchain network anonymously and gets rewarded with some “coins” for the steps they take. The users can trade their coins for some swag and these transactions are executed in the Blockchain network. This Android app was used in KubeCon Europe 2018.

This code pattern is for developers who wish to provide data anonymity and security to their users. Their users will be more confident to use their app if it gives them more control over their privacy. The developers can also extend the pattern to use the backend from different platforms.

When you have completed this code pattern, you will understand how to:

Flow

  1. The REST API is how the mobile app will interact with the blockchain network. The API will acknowledge the request and the mobile app will receive a unique key (random numbers and letters) which will be used to get the blockchain?s response later.
  2. The API just stores the request in a queue in RabbitMQ. The queue has 2 channels, which are for the user (Fitcoin org) and the seller (Shop org). The requests can either be:
    • User enrollment
    • Query data from the blockchain network (number of kubecoins of a user, products that are for sale, contracts, etc?)
    • Invoke or perform a transaction (send steps to receive kubecoins, claim a product, complete a transaction, etc?)
  3. The execution workers use the Hyperledger Fabric Node.js SDK to perform above requests. They are listening to the requests from RabbitMQ.
  4. The workers send the requests to the Blockchain network and are then processed. The blockchain network uses NFS to persist the ledger and state database.
  5. The workers receive the response and then persists it in the redis database with the unique key from (# 1).
  6. The mobile app will continue to wait for the blockchain?s response results that should be in the redis database. This is where the unique key is used. The mobile app will query the redis database with the unique key.
  7. The Registration microservice is used to create a user or update the user?s steps. When the blockchain network enrolls a user, this service uses the user id assigned by the blockchain network.
  8. When a user is registered, it calls a Cloud Function to generate a random name and avatar for the user.
  9. The data from the microservices are persisted in a MongoDB. This is where the user?s data (steps, name and avatar) and mobile assets (booklet/articles in the first view of mobile app) are persisted.
  10. The mobile assets microservice is used to query the MongoDB to get dynamic data for the mobile app. The booklet in the first view uses the database for its content.
  11. The leaderboard microservice is used to get the standings of the users.
Summary

The first time the user opens the app, he gets anonymously assigned a unique user ID in the Blockchain network. They also get assigned a random avatar and name which will be stored in MongoDB. This data will be used for the Leaderboard. The 3 microservices (Leaderboard, Mobile Assets, Registration) outside of the blockchain network are Node.js web apps to get data from the MonoDB. As users walk around, their steps will be sent to the blockchain network and they will be rewarded with “Kubecoins”. These coins can be used in the blockchain network to trade assets. The assets we have are some swags(stickers, bandanas, etc.) for the KubeCon conference. Users can see and claim them using the app. Once they claim a product, they'll get a Contract ID. They'll show the Contract ID to the seller (us) and we will complete and verify the transaction in our dashboard and give them the swag. The users can also check how they are doing with their steps compared to other people in the Standings view in the mobile app or in the Kubecoin dashboard.

Included Components
Featured Technologies

Prerequisite

Create a Kubernetes cluster with either Minikube for local testing, or with IBM Bluemix Container Service to deploy in cloud. The code here is regularly tested against Kubernetes Cluster from Bluemix Container Service using Travis.

Install Docker by following the instructions here for your preferrerd operating system. You would need docker if you want to build and use your own images.

Install Android Studio.

Steps

1. Clone the repo
t clone https://github.com/IBM/android-kubernetes-blockchain
2. Create IBM Cloud services

Create the following services:

3. Configure the Blockchain Network

ncodePath: 'bcfit',
itmq: 'amqps://admin:QWERTY@portal-ssl334-23.bmix-dal-yp-abc10717-6f73-4f63-b039-a1d2485c1566.devadvo-us-ibm-com.composedb.com:38919/bmix-dal-yp-abc10717-6f73-4f63-b039-a1d2485c1566',
sUrl: 'redis://admin:QWERTY@sl-us-south-1-portal.23.dblayer.com:38916',
 containers/blockchain
port FABRIC_CFG_PATH=$(pwd)
generate-certs.sh
rt DOCKERHUB_USERNAME=<your-dockerhub-username>

er build -t $DOCKERHUB_USERNAME/kubecon-orderer-peer:latest orderer/
er build -t $DOCKERHUB_USERNAME/kubecon-shop-peer:latest shopPeer/
er build -t $DOCKERHUB_USERNAME/kubecon-fitcoin-peer:latest fitcoinPeer/
er build -t $DOCKERHUB_USERNAME/kubecon-shop-ca:latest shopCertificateAuthority/
er build -t $DOCKERHUB_USERNAME/kubecon-fitcoin-ca:latest fitcoinCertificateAuthority/
er build -t $DOCKERHUB_USERNAME/kubecon-blockchain-setup:latest blockchainNetwork/
er build -t $DOCKERHUB_USERNAME/kubecon-backend:latest backend/
er build -t $DOCKERHUB_USERNAME/kubecon-rabbitclient-api:latest rabbitClient/

er push $DOCKERHUB_USERNAME/kubecon-orderer-peer:latest
er push $DOCKERHUB_USERNAME/kubecon-shop-peer:latest
er push $DOCKERHUB_USERNAME/kubecon-fitcoin-peer:latest
er push $DOCKERHUB_USERNAME/kubecon-shop-ca:latest
er push $DOCKERHUB_USERNAME/kubecon-fitcoin-ca:latest
er push $DOCKERHUB_USERNAME/kubecon-blockchain-setup:latest
er push $DOCKERHUB_USERNAME/kubecon-backend:latest
er push $DOCKERHUB_USERNAME/kubecon-rabbitclient-api:latest
t configuration/config.json | base64
t configuration/channel.tx | base64

place them in kube-configs/secrets.yaml


:
nfig.json: '<configruation/config.json in base64>'
annel.tx: '<configruation/channel.tx in base64>'

4. Deploy the Blockchain Network in Kubernetes
ube-configs
bectl create -f persistent-volume

it for them to get provisioned using kubectl get

bectl get pv,pvc

u should see something like this:

ME                                          CAPACITY   ACCESSMODES   # RECLAIMPOLICY   STATUS    CLAIM                   STORAGECLASS     REASON    AGE
/pvc-f57460fc-500f-11e8-a7d6-36f53fdc1872   20Gi       RWX           Delete          Bound     default/peer-claim      ibmc-file-gold             5d
/pvc-f585de1d-500f-11e8-a7d6-36f53fdc1872   20Gi       RWX           Delete          Bound     default/org-ca-claim    ibmc-file-gold             5d
/pvc-f5970726-500f-11e8-a7d6-36f53fdc1872   20Gi       RWX           Delete          Bound     default/couchdb-claim   ibmc-file-gold             5d

ME                STATUS    VOLUME                                     CAPACITY   ACCESSMODES   STORAGECLASS     AGE
c/couchdb-claim   Bound     pvc-f5970726-500f-11e8-a7d6-36f53fdc1872   20Gi       RWX           ibmc-file-gold   5d
c/org-ca-claim    Bound     pvc-f585de1d-500f-11e8-a7d6-36f53fdc1872   20Gi       RWX           ibmc-file-gold   5d
c/peer-claim      Bound     pvc-f57460fc-500f-11e8-a7d6-36f53fdc1872   20Gi       RWX           ibmc-file-gold   5d
bectl create -f secrets.yaml
bectl apply -f shop-ca.yaml
bectl apply -f fitcoin-ca.yaml

it for them to run

bectl get pods
bectl apply -f ca-datastore.yaml
bectl apply -f fitcoin-statedb.yaml
bectl apply -f shop-statedb.yaml

it for them to run

bectl get pods
bectl apply -f orderer0.yaml
bectl apply -f shop-peer.yaml
bectl apply -f fitcoin-peer.yaml

it for them to run

bectl get pods
bectl apply -f blockchain-setup.yaml
bectl logs -l app=blockchain-setup

ould result into:

fault channel not found, attempting creation...
ccessfully created a new default channel.
ining peers to the default channel.
aincode is not installed, attempting installation...
se container image present.
fo: [packager/Golang.js]: packaging GOLANG from bcfit
fo: [packager/Golang.js]: packaging GOLANG from bcfit
ccessfully installed chaincode on the default channel.
ccessfully instantiated chaincode on all peers.
ockchain newtork setup complete.
bectl apply -f shop-backend.yaml
bectl apply -f fitcoin-backend.yaml
bectl apply -f rabbitclient-api.yaml

it for them to run

bectl get pods
bectl get svc rabbitclient-api

ME               CLUSTER-IP      EXTERNAL-IP     PORT(S)          AGE
bbitclient-api   172.21.40.201   169.61.17.000   3000:30726/TCP   14m
port URL="http://169.61.17.000:3000"

rl -H "Content-Type: application/json" -X POST -d '{"type":"enroll","queue":"user_queue","params":{}}' "$URL/api/execute"

u should get something like this
status":"success","resultId":"7f90764a-8660-45f2-904d-47d8fb87a900"}
rl $URL/api/results/RESULT_ID

SULT_ID from the previous step is 7f90764a-8660-45f2-904d-47d8fb87a900
u should get something like this
status":"done","result":"{\"message\":\"success\",\"result\":{\"user\":\"50f97085-376d-40b3-8992-a9a2e6d18668\",\"txId\":\"2fcddeae9ddece5c818fed626601fba80711f3583944f8e53632972792954e09\"}}"}

 you {"status":"pending"}
y again or check if the resultId you copied is correct
5. Configure and Deploy Microservices in Kubernetes
 back to the root directory of the repository.

 containers/

cker build -t $DOCKERHUB_USERNAME/leaderboard:latest leaderboard/
cker build -t $DOCKERHUB_USERNAME/mobile-assets:latest mobile-assets/
cker build -t $DOCKERHUB_USERNAME/registeree-api:latest registeree-api/

cker push $DOCKERHUB_USERNAME/leaderboard:latest
cker push $DOCKERHUB_USERNAME/mobile-assets:latest
cker push $DOCKERHUB_USERNAME/registeree-api:latest

mongo credentials

certificate

ho -n "< Paste the certificate here>" | base64

tput would be like
0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURlVENDQW1HZ0F3SUJBZ0lFV3V0Z...

:
ngo.cert: 'LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURlVENDQW1HZ0F3SUJBZ0lFV3V0Z...'



name: MONGODB_URL
value: 'mongodb://admin:QWERTY@sl-us-south-1-po...'


ntainers:
- image: <the corresponding image name that you have built>

ply the certificate as a kubernetes secret
bectl apply -f mongo-cert-secret.yaml

ploy the microservices
bectl apply -f leaderboard-api.yaml
bectl apply -f mobile-assets.yaml
bectl apply -f registeree-api.yaml
6. Expose the backend with Kubernetes Ingress
 cs cluster-get <Your cluster name here>

u should look for these values

gress Subdomain:    anthony-blockchain.us-south.containers.mybluemix.net
gress Secret:       anthony-blockchain


:
s:
hosts:
- <Your-ingress-SUBDOMAIN-here>
secretName: <Your-ingress-SECRET-here>
ckend:
serviceName: mobile-assets
servicePort: 80
les:
host: <Your-ingress-SUBDOMAIN-here>

bectl apply -f ingress-prod.yaml
7. Configure the Android app

alt text

The line would look something like:


ng BACKEND_URL = "https://anthony-blockchain.us-south.containers.mybluemix.net";

8. Test the Android app

port URL="https://<Your-ingress-subdomain>"
rl -H "Content-Type: application/json" -X POST -d '{"type":"enroll","queue":"seller_queue","params":{}}' "$URL/api/execute"

u will get something in the format of
status":"success","resultId":"dfb6ef43-4eb2-467d-8c05-93ed5d0b517f"}
ing the result ID you got above, do:

rl "$URL/api/results/dfb6ef43-4eb2-467d-8c05-93ed5d0b517f"

u will get:
"status":"done",
"result": "{\"message\":\"success\",\"result\":{\"user\":\"351a9993-c204-4f75-a38c-ccaba49929bc\",\"txId\":\"15698a529b7c1bc98725cb61177c621bac9de03347567c405bd68e1ff96b120a\"}}"}

e ID of the seller would be 351a9993-c204-4f75-a38c-ccaba49929bc
port SELLER_ID=<seller ID you got from step above>

rl -H "Content-Type: application/json" -X POST -d '{"type":"invoke","queue":"seller_queue","params":{"userId": "'"$SELLER_ID"'", "fcn":"createProduct","args":["'"$SELLER_ID"'","eye-sticker","Eye sticker","100","1"]}}' "$URL/api/execute"
rl -H "Content-Type: application/json" -X POST -d '{"type":"invoke","queue":"seller_queue","params":{"userId": "'"$SELLER_ID"'", "fcn":"createProduct","args":["'"$SELLER_ID"'","bee-sticker","Bee sticker","100","1"]}}' "$URL/api/execute"
rl -H "Content-Type: application/json" -X POST -d '{"type":"invoke","queue":"seller_queue","params":{"userId": "'"$SELLER_ID"'", "fcn":"createProduct","args":["'"$SELLER_ID"'","em-sticker","M sticker","100","1"]}}' "$URL/api/execute"

Learn more about the chaincode functions for this project here

Links

Learn more

License

Apache 2.0


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.