Hyperledger Fabric Application Development: Unit 06
Hyperledger Fabric Application Development: Unit 06
Hyperledger Fabric Application Development: Unit 06
Unit 06
Chaincode basics
Chaincode
structure, APIs,
and data modeling
Chaincode in action
Summary
What you should be able to do
3
Learning objectives
Chaincode basics
Chaincode
structure, APIs,
and data modeling
Chaincode in action
Summary
User and system chaincodes
Every chaincode program must implement the chaincode interface, which defines the methods that are run during the
chaincode’s lifecycle:
o The Init() method is called in response to an instantiate or upgrade transaction.
o The Invoke() method is called in response to an invoke transaction proposal.
5
Bootstrap chaincode (1/2): Install the chaincode
E0 E2
A
B O O A
B
O O
E1 Ordering service P3
A
B
At first, chaincode is installed onto the peers that must run it.
6
Bootstrap chaincode (2/2): Instantiate the chaincode
E0 E2
A
B O O A
B
O O
E1 Ordering Service P3
A
B
7
Chaincode deployment approaches
8
Learning objectives
Chaincode basics
Chaincode
structure, APIs,
and data modeling
Chaincode in action
Summary
Developing the client and chaincode
Fully
supported Go Go
Experimental
/ under
development
10
Chaincode structure
4. Implementation
3. CC interface GetState
Invoke() PutState
Init() GetFunctionAndParameters
2. shim.Start()
1. Import declarations
Chaincode APIs: Developing Go
• To develop chaincode in Go, you must import the shim package by running the following command:
import "github.com/hyperledger/fabric/core/chaincode/shim»
12
Chaincode APIs: Developing
• To develop chaincode in Node.js, you must import the fabric-shim module by running the following command:
const shim = require('fabric-shim’);
13
Chaincode implementation: Handling input and context
14
Chaincode implementation: Handling data
• The shim provides interface methods to interact with the data layer:
o getState(), putState(), delState(), getHistoryForKey(), GetQueryResult(), and so on.
o By default, the world state is stored in a key value store and uses a byte array as the
value.
o Data models eventually must be converted into a JSON string.
• Native JSON structure and rich query support depend on the state database.
• CouchDB:
CouchDB
o Requires indexes to:
Make JSON queries efficient.
Perform JSON queries with a sort.
Chaincode Peer
o Indexes must be defined and deployed.
SHIM
LevelDB
15
Handling the chain: Query System Chaincode
Query System Chaincode (QSCC) provides convenient APIs to retrieve information about the blockchain by providing the
following inquiry methods:
…
Block Hash: 57ec2fda71 Block Hash: 87ea2ffe94 Block Hash: 44bf2efe32
Transaction Transaction
Transaction
16
Client SDK and chaincode operations
Peer process
Client shim.Start()
Import
Learning objectives
Chaincode basics
Chaincode
structure, APIs,
and data modeling
Chaincode in action
Summary
Implementing Invoke()
Modify the ledger
state.
Arg(0)
key JSON
Invoke() Arg(x) string
Putstate
Value
Encoding/JSON
AssetName = string
GetStateByPartialCompositeKey
getQueryResult
Encoding/JSON
Key/Rich
Query
GetState
JSON
Query() string CouchDB
GetStateByRange
GetStateByPartialCompositeKey
LevelDB
Key getQueryResult
Encoding/JSON
Chaincode basics
Chaincode
structure, APIs,
and data modeling
Chaincode in action
Summary
Unit summary
This unit described chaincode basics for development, including:
24
Exercise objectives
During this exercise, you will apply Hyperledger Fabric chaincode development concepts to:
25
References
For more information about the topics that are covered in this unit, see the following resources:
• http://hyperledger-fabric.readthedocs.io/en/release-1.1
• https://developer.ibm.com/courses/all/ibm-blockchain-foundation-developer/
26
Thank you.
IBM Skills Academy
www.ibm.com/blockchain
developer.ibm.com/blockchain
www.hyperledger.org
© Copyright IBM Corporation 2017. All rights reserved. The information contained in these
materials is provided for informational purposes only, and is provided AS IS without warranty
of any kind, express or implied. Any statement of direction represents IBM's current intent, is
subject to change or withdrawal, and represents only goals and objectives. IBM, the IBM
logo, and other IBM products and services are trademarks of the International Business
Machines Corporation, in the United States, other countries or both. Other company, product,
or service names may be trademarks or service marks of others.
© Copyright IBM Corporation 2018. All rights reserved. The information contained in these
materials is provided for informational purposes only, and is provided AS IS without warranty
of any kind, express or implied. Any statement of direction represents IBM's current intent, is
subject to change or withdrawal, and represents only goals and objectives. IBM, the IBM
logo, and other IBM products and services are trademarks of the International Business
Machines Corporation, in the United States, other countries or both. Other company, product,
or service names may be trademarks or service marks of others.