Hyperledger tutorial

Hyperledger is an Opensource platform to create blockchain applications in a business network of Business character.
To do this, the philosophy pursues is that network participants must be known, the networks must be permitted, you must have a transactions rate for second very high and must save the deprived and Confidentiality of transactions and data.

Among the use cases that Hyperledger are applying we can find Supply Chain, Provence, Property Rights, Fintech, Healthcare, among others.

Hyperledger consists of several sub-projects or frameworks (Burrow, Fabric, Iroha, Sawtooth) and several tools (Caliper, Cello, Composer, Explorer, Quilt).
Hyperledger is governed by Linux Foundation and has a great support by IBM.
The best known of Hyperledger is Fabricand Composer. Fabric is the most active of the current 10 projects. This tutorial is focused on Hyperledger Fabric.

Hyperledger Iroha

Initially, he had contributions from Soramitsu, Hitachi, NTT Data and Colu. It is focused on the simplicity of implementation using C ++, emphasizing the development of mobile apps and using the YAC consensus algorithm.

By the way, if you want to get free cryptocurrencies when learning, you can take a look at Coinbase that is also an excellent exchange.

Hyperledger Indy

This framework is focused on the digital identity on the Internet. Use DID (decentralized identifiers) and verifiable credentials to add a layer of identity on the Internet.

Hyperledger Sawtooth

It has been one of the first projects to join Linux Foundation and is surrounded by Intel. It allows permitted or non -permitted use. Use the proof of elapsed time consensus algorithm

Hyperledger Fabric

It has been one of the first projects to mature, under the hand of IBM. Use Peers, an Ordering Service and a membership server. It has the ability to use Smart Contracts (called Chainode) and allows the use of several channels.

Fabric characteristics

Introduction

FabricIt is a private DLT permitted with channel support. You don't need one cryptocurrency to function.
ComposerIt is a set of tools for modeling and building business networks in blockchain. Built with JavaScript, it uses several tools well known as Node.js, NPM and Yeoman.
Fabric can use plug and play -type consensus models, to adapt to each useful case the most efficient.
For example, if used in a single company, a type consensus algorithm can be used Crash Byzantine Fault TolerantWhile if used among several companies could use an algorithm of Type consensus Byzantine failures tolerant
It is permitted, that is, it is administered who can read or write in the blockchain.
Save the status of digital assets in a "key-value" database.
Has Smart Contracts called "Chainode", which can be implemented in go or Java or JavaScript.
You can also use an identities management of plug and play, such as LDAP or Openid, to adjust to the needs of each company or use case.

Fabric design works with several coms coms are the ASSETs, Chainode, Ledger, Privacy, the membership services and consensus.

ASSETS

Assets allow to model the exchange of anything that has value through the business network, From food, cars or currencies.
Even intangible things as contracts or rights of intellectual property.
Basically they are stored as a collection of "key-value" pairs with registered state changes such as Transactions in the Ledger.

Chainode

Chainode allows these ASSETs to be modified, through business logic the rules to read or Alter these "key-value" pairs or other information from the "State" database.

Ledger

In the Ledger all the changes of states are kept, sequentially and resistant to the manipulation of These data.
These state changes occur from the invocations of the chainode in the form of transactions that Several business network participants broadcast. Thus the Ledger is composed of a blockchain to store the sequence of changes and a base of States data.

Privacy

Through the use of channels, the privacy of network transactions is maintained.

Membership services

Since all business network participants have known identities within a organization, pki with certificates x509 is used to generate cryptographic certificates to Organizations, participants or users of applications.
There are also the membership service providers that define within the organization the role of Each peer.

Consensus

The consensus algorithm is the fundamental piece when ordering transactions in a block, from The proposal and the "endorsement" to the ordination, validation and writing.

Business Network (Business Network)

A business network consists of several nodes that are communicated by the network. There are 3 types of nodes: customers that send transactions to the peers,
Peers who record the transitations, maintain a copy of the LED and the State and the Ordering Service that order, validate transactions, put them into blocks and send them to the Peers. The certified authority service is used to generate certificates and keys to manage and Configure the identities of the business network.
Ordering Service orders time transactions and prepares them to include them in a block.
Peers that belong to organizations, store the LEDger and the Smart Contracts.
Transactions are recorded in the blockchain, while the state of the ASSETs is stored in a database.
Both the state of the ASSETs and the records are shared between the various peers of the system using The consensus algorithm.

To define a business network, at least one Ordering Service is needed. This is configured and managed by an administrator of a certain organization, giving only permits to that organization. Subsequently, at least one peer is added that stores a copy of the LEDger. This peer communicates with the Ordering Service using a channel.

Call sequence

Below is how applications interact with the Peers to access the LEDger. The consultation transactions are returned immediately,
while transactions containing updates require interactions between applications, Peers and Ordering Services.

  • The app connects to a peer
  • The app invokes chainode (proposal)
  • The peer invokes his chaincode (proposal)
  • The chainode generates a (proposal) of Query or Update in the Ledger
  • The peer responds to the app with the (proposal) - in the case of a Query ends here
  • The app asks the Ordering Service to insert the (proposal) - in the case of Updates
  • Ordering Service sends the transaction in a block to peer
  • The peer updates the Ledger
  • The peer sends an event to the app

Components of a business network

ASSET

An ASSET is a digital asset modeling within the Hyperledger system. It can be anything from a car, a food, etc ... have an identifier and several properties, and may be related to Other ASSET or with participants.

Participant

A participant is an actor of the network who can be the owner of an ASSET and can send transactions. To the Like an ASSET, they consist of an identifier and several properties.

Identities

They are defined by a digital certificate with their private key, they are assigned to the participants of the application. These identities are kept in the "cards".

Transactions

They define the business logic of the application. In other words, participants are related to ASSETs.

Query

Define consultations about the state of the Blockchain Assets.

Events

They are defined in modeling file and allow the world off-chain to realize the on-chain changes. The applications can subscribe to several events and thus show specific alerts of what happens in ASSETS

ACL (Access Control List)

Allow to define the permits of the participants when accessing the various ASSETs

The Hyperledger Composer

It is a set of tools to develop and expedite the creation of blockchain applications. HE communicates with manufacture in a very easy way and allows to design concept tests and deploy in a matter of a few weeks.
Composer packages all files on a BNA file (Business Network Archive).

A bna file contains:

  • he modelthat defines the application (ASSETs, participants and transactions) within Files .cto
  • the Business logicof the model (chainode), that is the logic of transactions Inside .js files
  • the Access Rulesto the models within files .acl
  • the ConsultationsInside .qry
  • something else like a readme.MD or a package.json

All this is packaged in a BNA and allows you to display a blockchain application easily in several environments, whether local or in the cloud.

To execute Hyperledger Composer, it can be done on the local computer or in the cloud (Bluemix of IBM). It is used what is called a "playground", which is a test environment where we can edit all the previous files and deploy them in Fabric de transparent way. It allows to model and test a blockchain application easily and quickly.

The playground is already preloaded with several examples, so it is highly advisable to use it to Get an idea of how to model a blockchain application within the Hyperledger ecosystem.

Hyperledger installation

When choosing an environment to install Hyperledger, it is advisable to use a Linux version, in General is recommended an ubuntu 16.04 or a macOS.

Requirements

A whole series of programs that are the following are needed:

  • Docker 17.03 or higher and docker-compose 1.8 or higher
  • Node.js 8.9 or higher
  • NPM 5.X
  • git 2.9.x or higher
  • Python 2.7.x

There is a github script that manages the requirements. We can install it with curl. If we don't have installed curl we will do it with:

sudo apt install curl

The script is then downloaded and executed:

curl -O https://hyperledger.github.io/composer/latest/prereqs-ubuntu.sh
  chmod u+x prereqs-ubuntu.sh
  prereqs-ubuntu.sh            

Composer

To install the composer you have to use NPM to install several tools:

  • Cli Tools:
    npm install -g composer-cli
  • API REST server:
    npm install -g composer-rest-server
  • utility to generate ASSETS:
    npm install -g generator-hyperledger-composer
  • Yeoman to generate apps:
    npm install -g yo
  • The playground:
    npm install -g composer-playground

Fabric

There is another script that contains the Fabric Installation script. We create a directory, we download the script with curl and unpack it:

mkdir ~/fabric-dev-servers && cd ~/fabric-dev-servers
  curl -O https://raw.githubusercontent.com/hyperledger/composer-tools/master/packages/fabric-dev-servers/fabric-dev-servers.tar.gz
  tar -xvf fabric-dev-servers.tar.gz
  cd ~/fabric-dev-servers
  ./downloadFabric.sh  

To start manufacture we do:

cd ~/fabric-dev-servers
and
./startFabric.sh

To stop Fabric we do:
./stopFabric.sh

And to start the playground:
composer-playground

Once this we can access the playground from the browser in the URL:

http: // localhost: 8080

It can be installed KitematicTo handle Docker containers visually and InsomniaTo handle the requests to the Rest APIs if desired.

Application development with Hyperledger

The first step is to create a definition of the business network. There the data model is defined, the logic of transactions and access rules.
Yeoman can be used to generate a skeleton:

yo hyperledger-composer:businessnetwork

Once we have developed the models and business logic we can pack it with:

composer archive create -t dir -n .

This will create a type file mired@0.0.1.bna

Here are the credentials of the business network administrator:

createPeerAdminCard.sh

With that we are created a "card" called "peeradmin@hlfv1"

Then you have to install the BNA in Fabric:

composer network install --card PeerAdmin@hlfv1 --archiveFile mired@0.0.1.bna

Then start the network:
composer network start --networkName mired --networkAdmin admin --networkAdminEnrollSecret adminpw
  --card PeerAdmin@hlfv1 --file miredadmin.card --networkVersion 0.0.1 

Import card:
composer card import --file miredadmin.card

We can make a ping to the business network:
composer network ping --card admin@mired

And even publish an API Rest to communicate with the blockchain:
composer-rest-server

There is an option to generate an angular application skeleton using Yeoman:

yo hyperledger-composer

This application can be started with:
npm start

To update a BNA, the following steps are required:
Change the "version" of the package.json from 0.0.1 to 0.0.2
Create the package this time with the name: mired@0.0.2.bna
Install the new network in Fabric:

composer network install --card PeerAdmin@hlfv1 --archiveFile mired@0.0.2.bna

Upgrade the network to the new version:
composer network upgrade -c PeerAdmin@hlfv1 -n mired -V 0.0.2

Make a ping to the network:
composer network ping --card admin@mired

Regenerate the API Rest:
composer-rest-server

Composer modeling language

Models use object -oriented language, similar to a Java language. A name space is used to Group resources:

namespace org.tutorial.basic

Then these can be imported within other files.
import org.tutorial.basic

The primitive types are: String(UTF8), Double(64bit decimal number), Integer(whole number with sign 32 bit), Long(whole number with 64bit sign), Datetime(Date and time with Timezone) and Boolean(A value true or false).

Default values ​​with the keyword can be assigned:

default="texto"
or optional values With the word:
optional

Arrays can be defined with brackets [] and enums:

enum Estado {
          o PENDIENTE
            o FINALIZADO
        } 

Then you can define Assets with the word

asset
that can inherit from other ASSETs with the word
extends
The properties of an ASSET are defined with an "or" the type of data and the name of the property. And if We want to include a relationship is defined with "->"

Next, participants can be defined with the word

participant
that can inherit from Other participants with the word:
extends
The properties of a participant are also defined with an "or" type of data and the name of the property. And if we want to include a relationship it is defined with "->"

The same applies to transactions and events, they can inherit and include properties and relationships.

There is an abstract class type that does not represent Assets, participants or transactions. They cannot be Referenced. They serve to group properties:

abstract concepto Direccion {
  o String calle
    o String ciudad
    o String pais
}

Chainode

To define the execution logic, JavaScript files are used. Use comments and metadata to Define these functions.
They are described as

async nombreTransaccion()
Because they are oriented to events like Node.
They are atomic and we can launch an exception with
throw new Error("error")
which causes a rollback and all changes are lost.

Within the code you can use several bookstores: Common, Runtime, Admin, and Client.
These allow to manage assets, issue events or manage the network.

Queries

Business network consultations are defined in files .qry. The consultations are preceded by the word Query and contain a "description" and a "statement" that is the database consultation proper.

query seleccionaMiAsset {
description: "Selecciona todos los assets de tipo MiAsset"
               statement: SELECT org.tutorial.MiAsset
} 

Playground

It is possible and recommended to define all this complete code in the playground and test it directly.
You can even export all the full code to a .bna file for its deployment in Fabric operating in our local network.

Frequent questions

This website uses cookies to obtain statistical data on the navigation of its users. If you continue browsing we consider that you accept its use. Cookies policy