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
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
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:
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.
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.
t clone https://github.com/IBM/android-kubernetes-blockchain
Create the following services:
containers/blockchain/configuration/config.js
, modify the values for rabbitmq
and redis
with your own credentials.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
Edit these YAML files in kube-configs
folder to use your images
blockchain-setup.yaml
orderer0.yaml
shop-peer.yaml
fitcoin-peer.yaml
shop-ca.yaml
fitcoin-ca.yaml
fitcoin-backend.yaml
rabbitclient-api.yaml
Modify kube-configs/secrets.yaml
to use your configuration and the channel.tx
generated
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>'
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"}
resultId
you got above. You should get a user ID.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
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
ho -n "< Paste the certificate here>" | base64
tput would be like
0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURlVENDQW1HZ0F3SUJBZ0lFV3V0Z...
mongo-cert-secret.yaml
:
ngo.cert: 'LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURlVENDQW1HZ0F3SUJBZ0lFV3V0Z...'
leaderboard-api.yaml
, mobile-assets.yaml
, registeree-api.yaml
in the environment variable of MONGODB_URL
name: MONGODB_URL
value: 'mongodb://admin:QWERTY@sl-us-south-1-po...'
leaderboard-api.yaml
, mobile-assets.yaml
and registeree-api.yaml
.
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
kubectl get pods
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
ingress-prod.yaml
to use the provided subdomain you have
:
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
android
folder in the Android Studio IDE. The project is configured to do REST calls to the Kubernetes backend. You would need to change the BACKEND_URL
variables to your own Ingress Subdomain in these Java files:The line would look something like:
ng BACKEND_URL = "https://anthony-blockchain.us-south.containers.mybluemix.net";
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