You are on page 1of 72

Application Development in

Hyperledger Fabric
KC Tam (kctam@ledgertech.biz)
(November, 2019)
Agenda
Quick review of Hyperledger Fabric

Fabcar Application: How a blockchain application looks like in Hyperledger Fabric

Invoice Finance: A Trade Finance Use Case

Chaincode, and Deployment on a Fabric Network

Chaincode invoke and query using Client SDK

Closing: How Blockchain Application Brings Values in Consortium Environment

2
Quick Review of Hyperledger Fabric

3
Hyperledger Fabric
An open source project in Linux Foundation Hyperledger Greenhouse

An enterprise blockchain platform, coming with a blockchain infrastructure and


contract programmability for blockchain application (cf. permissionless blockchain
such as Ethereum)

Most popular among open-source enterprise blockchain platforms (per media


coverage, use case, etc.)

Release 1.0 was public around mid 2017. Currently it is on release 1.4, the first Long
Term Support (LTS) release (as of today)

4
Hyperledger Greenhouse

5
Source: Hyperledger Ledger web site (as of November, 2019)
6
Source: https://www.mas.gov.sg/-/media/MAS/ProjectUbin/Project-Ubin-Phase-2-Reimagining-RTGS.pdf
Layer Structure: Understanding Blockchain Platform

● interacting with blockchain (data and


Blockchain execution logic)
Application ● wallet
● tools including library, interfaces, etc.

● nodes
● inter-node communication
Blockchain
● data structure (ledger)
Infrastructure ● consensus mechanism
● execution environment

7
Blockchain Infrastructure

● interacting with blockchain (data and


Blockchain execution logic)
Application ● wallet
● tools including library, interfaces, etc.

● nodes
● inter-node communication
Blockchain
● data structure (ledger)
Infrastructure ● consensus mechanism
● execution environment

8
Fabric Sample: Basic Network

CA peer0

Org1

world state database in couchdb OSN

mychannel
Orderer Org
9
Fabric Sample: Basic Network

$ cd basic-network
$ ./start.sh
$ ./teardown.sh
10
Fabric Sample: First Network

CA peer0 peer0 CA

peer1 peer1
Org1 Org2

OSN

mychannel
Orderer Org
11
Fabric Sample: First network

$ cd first-network
$ ./byfn.sh up -n
$ ./byfn.sh down
12
More Complex Networks
In real life, we may need more complex networks, such as,

● multiple channels
● multiple physical nodes (over a cloud or multiple clouds)

I have tested some setup and documented in my articles.

13
Blockchain Application

Client Application
Blockchain
Application
SDK

Blockchain
Infrastructure Orderer Peer

Chaincode deployed in
fabric network
14
Fabcar: a Hyperledger Fabric
Application

15
Fabcar: Overview
A sample application comes with fabric-samples

Fabcar is simulating a car record database in a fabric network, with a data structure
of a car record and interactions on the records

Fabcar is composed of

● chaincode
● client applications

Fabcar can be deployed in any fabric network. In the sample it is deployed on First
Network.
16
A Typical Chaincode Structure (in Golang)
Package and Import
Data Structure
Function Init
Function Invoke

Function implementing various actions/commands


Function implementing various actions/commands

Function implementing various actions/commands

Function main()

17
Data Structure

18
Init() and Invoke()

19
Implementing "sub-functions"

20
Sample function: queryCar()

21
Sample Function: createCar()

22
Deploy Fabcar on First Network
Fabcar comes with the script fabcar/startScript.sh to for the overall process,
including

● creates crypto material and channel artifacts for First Network


● brings up components (containers) of First Network
● creates and joins peers to mychannel
● installs fabcar chaincode on each peer
● instantiates fabcar chaincode on mychannel
● invoke chaincode function initLedger()

23
World State Database (couchdb)

24
Use CLI to Invoke and Query

25
Client Application
Fabcar comes with client application written in JavaScript, using SDK to interact
with the fabric network and the deployed fabcar chaincode.

There are two steps

● enrol an identity (user) for client application


● interact with chaincode with the proper chaincode functions

26
Inside a Client Application
Load the required SDK

Configuration profile: tell the client application "where to connect"

Load wallet

Load fabric network and chaincode specific

Call chaincode function with the arguments required

27
Load SDK

Load connection profile

Load wallet "user1"

Load fabric network (channel) and


specific chaincode

Query chaincode function with


argument

fabcar/javascript/query.js 28
Load SDK

Load connection profile

Load wallet "user1"

Load fabric network (channel) and


specific chaincode

Invoke chaincode function with


argument

fabcar/javascript/invoke.js 29
Enrol user1 for Client Application

30
Execute Client Application invoke.js and query.js

31
APIs for Chaincode Functions

API (ExpressJS)
instantiated chaincode:
● queryallcars Fabcar
● query/carid
● newcar S
accessing ● changeowner/carid
mychannel
REST API D
K
connection profile First Network
wallet (user1)

anywhere API Server Node Fabric Node

32
Source: https://medium.com/@kctheservant/an-implementation-of-api-server-for-hyperledger-fabric-network-8764c79f1a87
Summary
Fabcar gives us a good example for blockchain application, both the chaincode
structure and client applications.

While chaincode does not have any fabric network information, we specify in the
client application the network (channel) and chaincode before client application can
invoke / query the ledger.

As permissioned blockchain, Hyperledger Fabric requires authorized user identity


before one can access the fabric network.

33
Invoice Financing

34
Source: https://medium.com/@kctheservant/a-hyperledger-fabric-use-case-invoice-financing-32142f195aea
Consortium Blockchain Application
Enterprise blockchain provides the best value in consortium, i.e., more than one
organizations (business entities).

Consortium can be formed by business partners and even business competitors.

Among many use cases, Trade Finance is one of them.

35
Trade Finance
Trade Finance signifies financing the trade. It
contains a number of services facilitating trade
locally and globally.

There are many consortium blockchain platforms


formed across the world on Trade Finance:
mainly across banks and financial institutes

Among the services, we take a look on invoicing


finance, and see how to handle double financing
and over-borrowing problem.

36
Source: http://fintechnews.sg/25029/blockchain/blockchain-trade-finance-super-connector/
A Simple Trade Flow
service

INVOICE
invbob001
Alice Software House Bob Shop
● $10,000
● 30 days

payment (term)

37
Invoice Financing
service

INVOICE
invbob001
Alice Software House Bob Shop
● $10,000
● 30 days

38
Double Financing
Bank Alpha Bank Beta

Alice Software House 39


Over-Borrowing
Bank Alpha Bank Beta

Alice Software House 40


Invoice Financing
Theoretically, one can get invoice financing from more than one bank.

Double financing is a risk to banks, as borrowers may over-borrow vs the invoiced


amount.

This happens because in current system there is no a single truth about the status
of an invoice that banks can trust and rely on.

Distributed Ledger Technology (blockchain) provides a good mechanism to reduce


risk of banks and financial institutions.

41
Build a Blockchain Application for Invoice Financing
Design the chaincode: data structure and functions

Design a Bank Network: here we use two banks as example. It can be extended to
more banks.

Simulating a company to make a loan on the invoice, and see how this blockchain
application can detect and reduce risk to the banks.

42
Chaincode and Bank Network

43
Bank Network
Bank Alpha Bank Beta
CA CA

peer0.alpha peer0.beta

peer1.alpha peer1.beta

44
Bank Network
Bank Alpha Bank Beta
CA CA

peer0.alpha peer0.beta

peer1.alpha peer1.beta

business logic (chaincode)


deployed in each peer node

45
Data Structure and Chaincode Functions
Data structure (key-value pair)
● key: company + invoice number
● value: invoiced amount and borrowed
amount

Do nothing during chaincode being instantiated


(no initial value needed in this example)

Three chaincode functions to be invoked /


queried
● initInv(company, invoice number, invoiced
amount)
● queryInv(invoice number)
● requestLoan(company, invoice number,
requestedAmount)

46
Check if number of arguments is
correct, expecting [company, invoice
number, invoice amount]

Check if invoice amount is an integer

Construct "key" of this record:


company-invoiceNumber

If this invoice is found (already recorded


in the ledger), return error message.

Set borrowedAmount to 0, as no loan is


made yet on this new invoice. Record
this in the ledger.

initInv() 47
Bank Network
Bank Alpha Bank Beta
CA CA

peer0.alpha peer0.beta

Alice-invbob001 10000 0 Alice-invbob001 10000 0

peer1.alpha peer1.beta

Alice-invbob001 10000 0 Alice-invbob001 10000 0

initInv(Alice, invbob001, 10000)

48
Check if number of arguments is
correct, expecting [company, invoice
number, requested amount]

Check if requested amount is an integer

Construct "key" of this record:


company-invoiceNumber

If this invoice is not initialized yet, return


an error.

See if the additional of requested


amount exceeds the invoice amount. If
so, return an error.

Update the borrowed amount in the


record, including the requested amount.

requestLoan() 49
Bank Network
Bank Alpha Bank Beta
CA CA

peer0.alpha peer0.beta

Alice-invbob001 10000 7000 Alice-invbob001 10000 7000

peer1.alpha peer1.beta

Alice-invbob001 10000 7000 Alice-invbob001 10000 7000

requestLoan(Alice, invbob001, 7000)

50
Check if number of arguments is
correct, expecting [company, invoice
number]

Construct "key" of this record:


company-invoiceNumber

If this invoice is not found, return an


error message.

Retrieve the record from ledger and


return.

queryInv() 51
Invoke Chaincode Functions using SDK

52
Client Applications
For demonstration purpose, we enroll two users, one user-alpha, one user-beta,
when invoking chaincode functions on behalf of Bank Alpha and Bank Beta,
respectively.

Two set of client applications are built for each bank, they are

● initInv-<bank>.js
● requestLoan-<bank>.js
● queryInv-<bank>.js

where <bank> is either alpha or beta.

53
Demo Flow
1. Alice, holding an invoice invbob001 of invoiced amount $10,000, applies for a
loan from Bank Alpha for $7,000.
2. Bank Alpha records this loan in the Bank Network
3. Alice, holding the same invoice invbob001, applies for a loan in Bank Beta.
4. Bank Beta finds this invoice in the Bank Network already.
Three possible scenarios
a. Bank Beta rejects this loan application to avoid double finance
b. Bank Beta allows double financing. Alice requests loan of $5,000, which exceeds the amount.
Bank Beta rejects this loan application
c. Bank Beta allows double financing. Alice requests loan of $2,000, which is still lower than the
invoice amount. Bank Beta approves this, and updates the ledger.

54
Bank Network

55
Bank Network
Bank Alpha Bank Beta
CA CA

peer0.alpha peer0.beta

Alice-invbob001 10000 0 Alice-invbob001 10000 0

peer1.alpha peer1.beta

Alice-invbob001 10000 0 Alice-invbob001 10000 0

initInv(Alice, invbob001, 10000)

authorized staff:
user-alpha 56
57
Bank Network
Bank Alpha Bank Beta
CA CA

peer0.alpha peer0.beta

Alice-invbob001 10000 7000 Alice-invbob001 10000 7000

peer1.alpha peer1.beta

Alice-invbob001 10000 7000 Alice-invbob001 10000 7000

requestLoan(Alice, invbob001, 7000)

authorized staff:
user-alpha 58
59
Bank Network
Bank Alpha Bank Beta
CA CA

peer0.alpha peer0.beta

Alice-invbob001 10000 7000 Alice-invbob001 10000 7000

peer1.alpha peer1.beta

Alice-invbob001 10000 7000 Alice-invbob001 10000 7000

initInv(Alice, invbob001, 10000) Record Found

authorized staff:
user-beta 60
61
Bank Network
Bank Alpha Bank Beta
CA CA

peer0.alpha peer0.beta

Alice-invbob001 10000 7000 Alice-invbob001 10000 7000

peer1.alpha peer1.beta

Alice-invbob001 10000 7000 Alice-invbob001 10000 7000

requestLoan(Alice, invbob001, 5000) Over Borrowing

authorized staff:
user-beta 62
63
Bank Network
Bank Alpha Bank Beta
CA CA

peer0.alpha peer0.beta

Alice-invbob001 10000 9000 Alice-invbob001 10000 9000

peer1.alpha peer1.beta

Alice-invbob001 10000 9000 Alice-invbob001 10000 9000

requestLoan(Alice, invbob001, 2000)

authorized staff:
user-beta 64
65
Closing: Blockchain Application in
Consortium

66
Fabcar
We have reviewed Hyperledger Fabric and examined the Fabcar application.

Fabcar is like a database recorded in a fabric network. It brings value on

● data tampering easily detected


● transaction traceable and auditable
● resilience

We also examined and demonstrated the chaincode and client applications


provided.

67
Fabcar: Do We Need a Blockchain?
If this application is deployed in multiple organizations, each of them will keep a
copy and in a sense the data is trustworthy.

If this application is wholly deployed and owned by one organization, the value is
less as it is no more than a database. And no trust challenge is in an organization.

Always ask: do we need a blockchain?

68
Invoice Financing
We also covered a Invoicing Finance use case, which brings unique value as it is a
consortium blockchain application.

Despite the fact of "being competitors", banks have incentive to build such
application as this reduces the risk in making a loan. And it turns out benefit to all
participants.

This is possible ONLY when participating banks trust the system, and believe the
system can enforce the business logic agreed upon.

69
General Guideline in Application Development
First: a sound business case makes things more possible. Always ask: do we need
a blockchain?

Once we confirm blockchain is needed, we can start planning both the network and
blockchain application.

Network part is straightforward, though need detail design.

Application part consists of chaincode (business logic) and client application (user
frontend).

70
General Guideline in Application Development (cont.)
Chaincode structure is more or less "fixed", what we need is to put the required
data structure and functions to interact the ledger.

Testing can be done on a simple network, rather than on a real production network.

71
KC's LinkedIn Profile

Thank You!
KC's Article Collection

72

You might also like