Professional Documents
Culture Documents
Hyperledger Fabric
KC Tam (kctam@ledgertech.biz)
(November, 2019)
Agenda
Quick review of Hyperledger Fabric
2
Quick Review of Hyperledger Fabric
3
Hyperledger Fabric
An open source project in Linux Foundation Hyperledger Greenhouse
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
● nodes
● inter-node communication
Blockchain
● data structure (ledger)
Infrastructure ● consensus mechanism
● execution environment
7
Blockchain Infrastructure
● nodes
● inter-node communication
Blockchain
● data structure (ledger)
Infrastructure ● consensus mechanism
● execution environment
8
Fabric Sample: Basic Network
CA peer0
Org1
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)
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 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
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.
26
Inside a Client Application
Load the required SDK
Load wallet
27
Load SDK
fabcar/javascript/query.js 28
Load SDK
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)
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.
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).
35
Trade Finance
Trade Finance signifies financing the trade. It
contains a number of services facilitating trade
locally and globally.
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
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.
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
45
Data Structure and Chaincode Functions
Data structure (key-value pair)
● key: company + invoice number
● value: invoiced amount and borrowed
amount
46
Check if number of arguments is
correct, expecting [company, invoice
number, invoice amount]
initInv() 47
Bank Network
Bank Alpha Bank Beta
CA CA
peer0.alpha peer0.beta
peer1.alpha peer1.beta
48
Check if number of arguments is
correct, expecting [company, invoice
number, requested amount]
requestLoan() 49
Bank Network
Bank Alpha Bank Beta
CA CA
peer0.alpha peer0.beta
peer1.alpha peer1.beta
50
Check if number of arguments is
correct, expecting [company, invoice
number]
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
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
peer1.alpha peer1.beta
authorized staff:
user-alpha 56
57
Bank Network
Bank Alpha Bank Beta
CA CA
peer0.alpha peer0.beta
peer1.alpha peer1.beta
authorized staff:
user-alpha 58
59
Bank Network
Bank Alpha Bank Beta
CA CA
peer0.alpha peer0.beta
peer1.alpha peer1.beta
authorized staff:
user-beta 60
61
Bank Network
Bank Alpha Bank Beta
CA CA
peer0.alpha peer0.beta
peer1.alpha peer1.beta
authorized staff:
user-beta 62
63
Bank Network
Bank Alpha Bank Beta
CA CA
peer0.alpha peer0.beta
peer1.alpha peer1.beta
authorized staff:
user-beta 64
65
Closing: Blockchain Application in
Consortium
66
Fabcar
We have reviewed Hyperledger Fabric and examined the Fabcar application.
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.
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.
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