Skip to content

LennartFr/Blockchain-at-Galvanize

Repository files navigation

Workshop: Introduction to writing Blockchain applications

The workshop materials are all out on: ibm.biz/blockchaingalvanize

Housekeeping info:

Wifi at Galavanize SSID/pass = g|Events / machinelearning

Your instructor: Lennart [email protected], Developer Advocate at IBM, focusing on Blockchain, Fintech and Watson.

Marek Sadowski, Syed Zaidi. Maya Reyes, Juanita Dion.

Assumption: you have brought your laptop and you know enough about Blockchain to want to write Blockchain apps.

Agenda

6:00: Sign-in, mingle, food, welcome, form teams 
6:20: Lennart: Introduction to IBM Blockchain and Hyperledger  
6:40: Coding starts.
      Lab 1: Let's run our first application in Hyperledger Fabric
      Lab 2: Let's write an app with the Hyperledger Composer
      Lab 3: A full app, Decentralized Energy with Hyperledger Composer
8:15: Coding ends.
      Where do we go from here? 
      Developer Journeys for Blockchain
      Q1 2018: Advanced Blockchain, a Hands-on Workshop.
8:30: Event ends   

Let's get started, introduction and documentation

How did it all start?

October 2008 It all started with Satoshi Nakamoto and his paper Bitcoin: A Peer-to-Peer Electronic Cash System which addressed a key problem in electronic commerce:

Double Spending

In this workshop we will use the Hyperledger implementation of Blockchain : http://hyperledger.org/

Hyperledger, an open source collaborative effort to advance cross-industry blockchain technologies, is hosted by The Linux Foundation®.

Deployed in Docker images.

  1. A blockchain emulates a “trusted” computing service through a distributed protocol, run by nodes connected over the Internet.

  2. The service represents or creates an asset, in which all nodes have some stake.

  3. The nodes share the common goal of running the service but do not necessarily trust each other for more.

....

  1. On the other hand, blockchains in the “permissioned” model control who participates in validation and in the protocol; these nodes typically have established identities and form a consortium

Consensus protocol is pluggable, currently an implementation of Byzantine fault-tolerant con- sensus using the PBFT protocol.

Source: https://www.zurich.ibm.com/dccl/papers/cachin_dccl.pdf

Hyperledger Services

Hyperledger Composer source code on GitHub https://github.com/hyperledger/composer

Hyperledger Composer is an application development framework which simplifies and expedites the creation of Hyperledger fabric blockchain applications.

Hyperledger Fabric source code on Github https://github.com/hyperledger/fabric

Hyperledger Fabric is a platform for distributed ledger solutions, underpinned by a modular architecture delivering high degrees of confidentiality, resiliency, flexibility and scalability. It is designed to support pluggable implementations of different components, and accommodate the complexity and intricacies that exist across the economic ecosystem.

A Business Network Archive (BNA) file is exported by the Hyperledger Composer with the extension of .bna, and contains the definitions all the definitions for a Business Network. It is deployed in a Hyper Ledger Fabric.

General information:

Hyperledger Consensus

blockchain-banner2

Prerequisites:

http://hyperledger-fabric.readthedocs.io/en/latest/prereqs.html

Please note that it is crucial that you go thru the link above to verify that you have met the requirements for running Hyperledger on your laptop. Especially if the labs do'nt work.

We will be developing on our laptops. MacOS, Ubuntu or Windows.

Lab 1: Let's run our first application in Hyperledger Fabric

Original instructions: http://hyperledger-fabric.readthedocs.io/en/latest/write_first_app.html

Architecture:

Hyperledger helloworld

Step 1 First, remove any docker containers, next, delete the chaincode image if it exists:

1 docker rm -f $(docker ps -aq)
2 docker rmi dev-peer0.org1.example.com-fabcar-1.0  //delete chaincode image

Step 2 Navigate to a directory where you want the samples downloaded to, and issue these commands:

1. Source file: http://hyperledger-fabric.readthedocs.io/en/release/write_first_app.html
2. Open terminal window on laptop, do: git clone https://github.com/hyperledger/fabric-samples.git
3. cd fabric-samples/fabcar
4. See what's inside the directory: enrollAdmin.js invoke.js package.json query.js registerUser.js  startFabric.sh
5. docker rm -f $(docker ps -aq)
6. Start the fabric: ./startFabric.sh
7. Install the SDK Node modules: npm install
8. node enrollAdmin.js
9. node registerUser.js
  1. node query.js

Output:

Creating network "net_basic" with the default driver Creating couchdb ...

Creating ca.example.com ...

  CA: Certificate Authority https://hyperledger-fabric-ca.readthedocs.io/en/latest/ 
     Comment: It provides features such as:
     1. Issues certificates to other network participants to enroll in the network,
       or connects to LDAP as the user registry
     2. issuance of Enrollment Certificates (ECerts)
     3. certificate renewal and revocation
     
Creating orderer.example.com ...

    Comment: The Hyperledger fabric ordering service is intended to provide an atomic broadcast 
    ordering service for consumption by the peers. This means that many clients may 
submit messages for ordering, and all clients are delivered the same series of 
ordered batches in response.

Creating ca.example.com

Creating orderer.example.com

Creating couchdb ... done
   Comment: Transactions are collected into blocks/batches on the Ordering Service.
   The  blocks are stored locally to disk on every Ordering Service node 
   along with a LevelDB or CouchDB index to these blocks by number. 
   The blocks are delivered via RPC to committing peers. 
   The peers store them locally, and maintain a LevelDB (or CouchDB)based block index. 
   http://bit.ly/2zORhrC

Creating peer0.org1.example.com ...
   Comment: A Peer is a node that validates and commits transactions and maintains the state and a copy 
   of the ledger. 

   Every peer stores the data of the blockchain in its container. 
   In a production envirement this container has a volume (mounted folder) to a real hard drive, NAS, SAN 
   or any other storage that you may use. Otherwise if the container is stopped all the data that was in the 
   container will be gone. 
   
   Every peer stores exactly the same data. So if a blockchain is 10Gb and you have 5 peers 
   then 50Gb will be needed to store 5 separate files from every peer and every file is 10Gb.

   Creating peer0.org1.example.com ... 
   done
hyperledger architecture
11 node query.js

output; Arnes-MBP:fabcar arnelennartfrantzell $ node query.js Create a client and set the wallet location Set wallet path, and associate user PeerAdmin with application Check user is enrolled, and set a query URL in the network Make query Assigning transaction_id: 9367267e0e9c66cba821eb79500ffee6f004ced6f53bc01ddf280b00e2a8cb10 returned from query Query result count = 1 Response is
[{"Key":"CAR0", "Record":{"colour":"blue","make":"Toyota","model":"Prius","owner":"Tomoko"}},

{"Key":"CAR1", "Record":{"colour":"red","make":"Ford","model":"Mustang","owner":"Brad"}},

{"Key":"CAR2", "Record":{"colour":"green","make":"Hyundai","model":"Tucson","owner":"Jin Soo"}},

{"Key":"CAR3", "Record":{"colour":"yellow","make":"Volkswagen","model":"Passat","owner":"Max"}},

{"Key":"CAR4", "Record":{"colour":"black","make":"Tesla","model":"S","owner":"Adriana"}},

{"Key":"CAR5", "Record":{"colour":"purple","make":"Peugeot","model":"205","owner":"Michel"}},

{"Key":"CAR6", "Record":{"colour":"white","make":"Chery","model":"S22L","owner":"Aarav"}},

{"Key":"CAR7", "Record":{"colour":"violet","make":"Fiat","model":"Punto","owner":"Pari"}},

{"Key":"CAR8", "Record":{"colour":"indigo","make":"Tata","model":"Nano","owner":"Valeria"}},

{"Key":"CAR9", "Record":{"colour":"brown","make":"Holden","model":"Barina","owner":"Shotaro"}}]

Step 3 Now let's add a new car and update the Blockchain.

1 Open the Invoke.js program, locate the request function and change the args section like this, using your own name and args:

var request = {
        targets: targets,
        chaincodeId: options.chaincode_id,
        fcn: 'createCar',
        args: ['CAR10', 'Chevy', 'Volt', 'Red', 'Lennart'],
        chainId: options.channel_id,
        txId: tx_id

2 Save the file and invoke it like this: node invoke.js. 
3 Followed by: node query.js
You should see the new car appear, stored in the blockchain.

Step 4 Now let's change car ownership

1 Change the Invoke.js program as shown below:

// createCar - requires 5 args, ex: args: ['CAR11', 'Honda', 'Accord', 'Black', 'Tom'],
    // changeCarOwner - requires 2 args , ex: args: ['CAR10', 'Barry'],
    // send proposal to endorser
    var request = {
        targets: targets,
        chaincodeId: options.chaincode_id,
     // fcn: 'createCar',
        fcn: 'changeCarOwner',
     // args: ['CAR10', 'Chevy', 'Volt', 'Red', 'Lennart'],
        args: ['CAR10', 'Syed'],
        chainId: options.channel_id,
        txId: tx_id
    };

2 Again, enter node query.js.

You will see that the new owner is now Syed.

Hyperledger helloworld 2

Read more about this app:

This concludes Lab 1.

Lab 2: Let's write an app with the Hyperledger Composer!

Composer Playground

Playground Tutorial

Run Hyperledger Composer in your browser https://hyperledger.github.io/composer/

[Instructions (open in parallel window): Composer Playground and the Perishable Goods Network]

(https://github.com/LennartFr/Blockchain-at-Galvanize/blob/master/Hyperledger%20Composer%20Perishable%20Food%20Network%203.pdf)

This concludes the second lab centered arund the Composer Playground.

Lab 3: Decentralized Energy with Hyperledger composer

This third lab, a Developer Journey written by Raheel Zubairy of IBM, describes a complete blockchain application with a decentralized energy network with a working user interface.

https://developer.ibm.com/code/journey/decentralized-energy-hyperledger-composer/

Lab Instrructions: https://github.com/IBM/Decentralized-Energy-Composer?cm_sp=IBMCode-_-decentralized-energy-hyperledger-composer-_-Get-the-Code

This concludes the lab portion of this workshop

blueband

Overview of the Hyperledger architecture

The flow with one SDK

Hyperledger Architecture

The flow with two and more SDKs

Hyper Ivan 2

The Blockchain and State database.

Ordering service

Blockchain Ledger

Ivan Blockchain

Hyperledger Fabric and its docker images

hyper docker

blueband

Where do we go from here?

IBMBlockchain1

Blockchain for Dummies

IBM has a series of Developer Journeys covering various aspects of Blockchain, with more being added regularly.

** Initiate a new blockchain network including setting democratic network policies and inviting new members to join. ** Join a network, as a new member, based on an invite from the network initiator.

  • Use Issues to log suggestions to this workshop. #1

Appendix

Bonus Lab : Let's bring up the Hyperledger Fabric!

Instructions below from thess URLs:

Step 1

Only if necessary, execute the following three commands to remove existing Docker instances.

  • docker kill $(docker ps -q)
  • docker rm $(docker ps -aq)
  • docker rmi $(docker images dev-* -q)

Step 2

Instructions below come from the following URL: http://hyperledger-fabric.readthedocs.io/en/latest/samples.html  
From a terminal window on your laptop, execute the following commands:

Step 3

Bring up the Hyperledger Fabric:

  • cd first-network
  • ./byfn.sh -m generate
  • ./byfn.sh -m up. //See full output from command on this link http://bit.ly/2yyTeIj
  • After checking the output, remember to run ./byfn.sh -m down.

Step 4 (optional) Configurate your Hyperledger Fabric instance.

http://hyperledger-fabric.readthedocs.io/en/latest/build_network.html

Deploy bna file on hyperledger fabric

https://hyperledger.github.io/composer/business-network/bnd-deploy.html ``~ Arnes-MBP:bnafile arnelennartfrantzell$ composer network deploy -p hlfv1 -a my-basic-sample.bna -i PeerAdmin -s randomString -A admin -S Deploying business network from archive: my-basic-sample.bna Business network definition: Identifier: [email protected] Description: The Composer basic sample network

✔ Deploying business network definition. This may take a minute...

Command succeeded


## ping application on hyperledger composer

Arnes-MBP:bnafile arnelennartfrantzell$ composer network ping -n my-basic-sample -p hlfv1 -i admin -s adminpw
The connection to the network was successfully tested: my-basic-sample
	version: 0.14.1
	participant: org.hyperledger.composer.system.NetworkAdmin#admin

Command succeeded