SlideShare une entreprise Scribd logo
1  sur  20
Chaincode Use Cases
Chaincode Use Cases
• Chaincode is a program, written in Go, node.js, or Java that implements a prescribed
interface. Chaincode runs in a secured Docker container isolated from the
endorsing peer process. Chaincode initializes and manages ledger state through
transactions submitted by applications.
• A chaincode typically handles business logic agreed to by members of the network,
so it may be considered as a “smart contract”. State created by a chaincode is
scoped exclusively to that chaincode and can’t be accessed directly by another
chaincode. However, within the same network, given the appropriate permission a
chaincode may invoke another chaincode to access its state.
use cases for Hyperledger Fabric
• Shared recordkeeping, smart contracts, supply chain management, shared
assets, provenance, and payments are particularly salient now with existing
blockchain tech to build apps around. Flexibility of blockchain solutions
supports many business use cases even sensitive one.
Two Personas
• We offer two different perspectives on chaincode. One, from the perspective
of an application developer developing a blockchain application/solution
entitled Chaincode for Developers, and the other, Chaincode for Operators
oriented to the blockchain network operator who is responsible for
managing a blockchain network, and who would leverage the Hyperledger
Fabric API to install, instantiate, and upgrade chaincode, but would likely not
be involved in the development of a chaincode application.
Chaincode for Developers
• Chaincode is a program, written in Go, node.js, or Java that implements a prescribed
interface. Chaincode runs in a secured Docker container isolated from the endorsing peer
process. Chaincode initializes and manages the ledger state through transactions submitted
by applications.
• A chaincode typically handles business logic agreed to by members of the network, so it
similar to a “smart contract”. A chaincode can be invoked to update or query the ledger in a
proposal transaction. Given the appropriate permission, a chaincode may invoke another
chaincode, either in the same channel or in different channels, to access its state. Note that,
if the called chaincode is on a different channel from the calling chaincode, only read query
is allowed. That is, the called chaincode on a different channel is only a Query, which does
not participate in state validation checks in subsequent commit phase.
Chaincode API
• Every chaincode program must implement the Chaincode interface:
1. Go
2. node.js
3. Java
• whose methods are called in response to received transactions. In particular the Init
method is called when a chaincode receives an instantiate or upgrade transaction so
that the chaincode may perform any necessary initialization, including initialization
of application state. The Invoke method is called in response to receiving an invoke
transaction to process transaction proposals.
Chaincode for Developers
• The other interface in the chaincode “shim” APIs is the
ChaincodeStubInterface:
1. Go
2. node.js
3. Java
• which is used to access and modify the ledger, and to make invocations
between chaincodes.
Choosing a Location for the Code
• If you haven’t been doing programming in Go, you may want to make sure
that you have Go Programming Language installed and your system properly
configured.
• Now, you will want to create a directory for your chaincode application as a
child directory of $GOPATH/src/.
Choosing a Location for the Code
• To keep things simple, let’s use the following command:
mkdir -p $GOPATH/src/sacc && cd $GOPATH/src/sacc
• Now, let’s create the source file that we’ll fill in with code:
touch sacc.go
Housekeeping
• First, let’s start with some housekeeping. As with every chaincode, it
implements the Chaincode interface in particular, Init and Invoke functions.
So, let’s add the Go import statements for the necessary dependencies for
our chaincode. We’ll import the chaincode shim package and the peer
protobuf package. Next, let’s add a struct SimpleAsset as a receiver for
Chaincode shim functions.
Housekeeping
• package main
• import (
• "fmt"
• "github.com/hyperledger/fabric/core/chaincode/shim"
• "github.com/hyperledger/fabric/protos/peer")
• // SimpleAsset implements a simple chaincode to manage an asset
• type SimpleAsset struct {
• }
Initializing the Chaincode
• Next, we’ll implement the Init function.
• // Init is called during chaincode instantiation to initialize any data.
• func (t *SimpleAsset) Init(stub shim.ChaincodeStubInterface) peer.Response
{
• }
Initializing the Chaincode
• Next, we’ll retrieve the arguments to the Init call using the
ChaincodeStubInterface.GetStringArgs function and check for validity. In
our case, we are expecting a key-value pair.
// Init is called during chaincode instantiation to initialize any
// data. Note that chaincode upgrade also calls this function to
reset
// or to migrate data, so be careful to avoid a scenario where you
// inadvertently clobber your ledger's data!
func (t *SimpleAsset) Init(stub shim.ChaincodeStubInterface)
peer.Response {
// Get the args from the transaction proposal
args := stub.GetStringArgs()
if len(args) != 2 {
return shim.Error("Incorrect arguments. Expecting a key and a
value")
}
}
Initializing the Chaincode
• Next, now that we have established that the call is valid, we’ll store the initial
state in the ledger. To do this, we will call ChaincodeStubInterface.PutState
with the key and value passed in as the arguments. Assuming all went well,
return a peer.Response object that indicates the initialization was a success.
Init is called during chaincode instantiation to initialize any
// data. Note that chaincode upgrade also calls this function to reset
// or to migrate data, so be careful to avoid a scenario where you
// inadvertently clobber your ledger's data!
func (t *SimpleAsset) Init(stub shim.ChaincodeStubInterface) peer.Response {
// Get the args from the transaction proposal
args := stub.GetStringArgs()
if len(args) != 2 {
return shim.Error("Incorrect arguments. Expecting a key and a value")
}
// Set up any variables or assets here by calling stub.PutState()
// We store the key and the value on the ledger
err := stub.PutState(args[0], []byte(args[1]))
if err != nil {
return shim.Error(fmt.Sprintf("Failed to create asset: %s", args[0]))
}
return shim.Success(nil)
}
Invoking the Chaincode
• First, let’s add the Invoke fun
• As with the Init function above, we need to extract the arguments from the
ChaincodeStubInterface. The Invoke function’s arguments will be the name
of the chaincode application function to invoke. In our case, our application
will simply have two functions: set and get, that allow the value of an asset to
be set or its current state to be retrieved. We first call
ChaincodeStubInterface.GetFunctionAndParameters to extract the function
name and the parameters to that chaincode application function.ction’s
signature.
Invoking the Chaincode
• Next, we’ll validate the function name as being either set or get, and invoke
those chaincode application functions, returning an appropriate response via
the shim.Success or shim.Error functions that will serialize the response into
a gRPC protobuf message.
Implementing the Chaincode Application
• As noted, our chaincode application implements two functions that can be
invoked via the Invoke function. Let’s implement those functions now. Note
that as we mentioned above, to access the ledger’s state, we will leverage the
ChaincodeStubInterface.PutState and ChaincodeStubInterface.GetState
functions of the chaincode shim API.
Pulling it All Together
• Finally, we need to add the main function, which will call the shim.Start
function. Here’s the whole chaincode program source.

Contenu connexe

Similaire à Hyperledger Fabric Chaincode Use Cases Guide

(Don't) Go Tracing Server Calls
(Don't) Go Tracing Server Calls(Don't) Go Tracing Server Calls
(Don't) Go Tracing Server CallsBrandon Hunter
 
Introduction to Hyperledger Composer
Introduction to Hyperledger ComposerIntroduction to Hyperledger Composer
Introduction to Hyperledger ComposerSimon Stone
 
Kogito: cloud native business automation
Kogito: cloud native business automationKogito: cloud native business automation
Kogito: cloud native business automationMario Fusco
 
Cosmos SDK Workshop: How to Build a Blockchain from Scratch
Cosmos SDK Workshop: How to Build a Blockchain from ScratchCosmos SDK Workshop: How to Build a Blockchain from Scratch
Cosmos SDK Workshop: How to Build a Blockchain from ScratchTendermint Inc
 
OGCE Project Overview
OGCE Project OverviewOGCE Project Overview
OGCE Project Overviewmarpierc
 
Javascripts hidden treasures BY - https://geekyants.com/
Javascripts hidden treasures            BY  -  https://geekyants.com/Javascripts hidden treasures            BY  -  https://geekyants.com/
Javascripts hidden treasures BY - https://geekyants.com/Geekyants
 
Build on Streakk Chain - Blockchain
Build on Streakk Chain - BlockchainBuild on Streakk Chain - Blockchain
Build on Streakk Chain - BlockchainEarn.World
 
OpenTelemetry Introduction
OpenTelemetry Introduction OpenTelemetry Introduction
OpenTelemetry Introduction DimitrisFinas1
 
[2019.1] 하이퍼레저 패브릭 v1.3, v1.4 새로운 기능
[2019.1] 하이퍼레저 패브릭 v1.3, v1.4 새로운 기능[2019.1] 하이퍼레저 패브릭 v1.3, v1.4 새로운 기능
[2019.1] 하이퍼레저 패브릭 v1.3, v1.4 새로운 기능Hyperledger Korea User Group
 
Razorpay Payment Gateway Integration In iOS Swift
Razorpay Payment Gateway Integration In iOS SwiftRazorpay Payment Gateway Integration In iOS Swift
Razorpay Payment Gateway Integration In iOS SwiftInnovationM
 
Integrate Ruby on Rails with Avectra's NetFORUM xWeb API.
Integrate Ruby on Rails with Avectra's NetFORUM xWeb API.Integrate Ruby on Rails with Avectra's NetFORUM xWeb API.
Integrate Ruby on Rails with Avectra's NetFORUM xWeb API.Thomas Vendetta
 
Operator SDK for K8s using Go
Operator SDK for K8s using GoOperator SDK for K8s using Go
Operator SDK for K8s using GoCloudOps2005
 
Hyperleger Composer Architecure Deep Dive
Hyperleger Composer Architecure Deep DiveHyperleger Composer Architecure Deep Dive
Hyperleger Composer Architecure Deep DiveDan Selman
 
Unit8 java
Unit8 javaUnit8 java
Unit8 javamrecedu
 
Dart for Java Developers
Dart for Java DevelopersDart for Java Developers
Dart for Java DevelopersYakov Fain
 
Dependency injection in Java, from naive to functional
Dependency injection in Java, from naive to functionalDependency injection in Java, from naive to functional
Dependency injection in Java, from naive to functionalMarian Wamsiedel
 
Deploying Cloud Native Red Team Infrastructure with Kubernetes, Istio and Envoy
Deploying Cloud Native Red Team Infrastructure with Kubernetes, Istio and Envoy Deploying Cloud Native Red Team Infrastructure with Kubernetes, Istio and Envoy
Deploying Cloud Native Red Team Infrastructure with Kubernetes, Istio and Envoy Jeffrey Holden
 
GWT Web Socket and data serialization
GWT Web Socket and data serializationGWT Web Socket and data serialization
GWT Web Socket and data serializationGWTcon
 
Blockchain architected
Blockchain architectedBlockchain architected
Blockchain architectedIBM Sverige
 

Similaire à Hyperledger Fabric Chaincode Use Cases Guide (20)

(Don't) Go Tracing Server Calls
(Don't) Go Tracing Server Calls(Don't) Go Tracing Server Calls
(Don't) Go Tracing Server Calls
 
Introduction to Hyperledger Composer
Introduction to Hyperledger ComposerIntroduction to Hyperledger Composer
Introduction to Hyperledger Composer
 
Kogito: cloud native business automation
Kogito: cloud native business automationKogito: cloud native business automation
Kogito: cloud native business automation
 
Cosmos SDK Workshop: How to Build a Blockchain from Scratch
Cosmos SDK Workshop: How to Build a Blockchain from ScratchCosmos SDK Workshop: How to Build a Blockchain from Scratch
Cosmos SDK Workshop: How to Build a Blockchain from Scratch
 
OGCE Project Overview
OGCE Project OverviewOGCE Project Overview
OGCE Project Overview
 
Javascripts hidden treasures BY - https://geekyants.com/
Javascripts hidden treasures            BY  -  https://geekyants.com/Javascripts hidden treasures            BY  -  https://geekyants.com/
Javascripts hidden treasures BY - https://geekyants.com/
 
Build on Streakk Chain - Blockchain
Build on Streakk Chain - BlockchainBuild on Streakk Chain - Blockchain
Build on Streakk Chain - Blockchain
 
OpenTelemetry Introduction
OpenTelemetry Introduction OpenTelemetry Introduction
OpenTelemetry Introduction
 
[2019.1] 하이퍼레저 패브릭 v1.3, v1.4 새로운 기능
[2019.1] 하이퍼레저 패브릭 v1.3, v1.4 새로운 기능[2019.1] 하이퍼레저 패브릭 v1.3, v1.4 새로운 기능
[2019.1] 하이퍼레저 패브릭 v1.3, v1.4 새로운 기능
 
Razorpay Payment Gateway Integration In iOS Swift
Razorpay Payment Gateway Integration In iOS SwiftRazorpay Payment Gateway Integration In iOS Swift
Razorpay Payment Gateway Integration In iOS Swift
 
Integrate Ruby on Rails with Avectra's NetFORUM xWeb API.
Integrate Ruby on Rails with Avectra's NetFORUM xWeb API.Integrate Ruby on Rails with Avectra's NetFORUM xWeb API.
Integrate Ruby on Rails with Avectra's NetFORUM xWeb API.
 
Operator SDK for K8s using Go
Operator SDK for K8s using GoOperator SDK for K8s using Go
Operator SDK for K8s using Go
 
Hyperleger Composer Architecure Deep Dive
Hyperleger Composer Architecure Deep DiveHyperleger Composer Architecure Deep Dive
Hyperleger Composer Architecure Deep Dive
 
Unit8 java
Unit8 javaUnit8 java
Unit8 java
 
Dart for Java Developers
Dart for Java DevelopersDart for Java Developers
Dart for Java Developers
 
Dependency injection in Java, from naive to functional
Dependency injection in Java, from naive to functionalDependency injection in Java, from naive to functional
Dependency injection in Java, from naive to functional
 
Deploying Cloud Native Red Team Infrastructure with Kubernetes, Istio and Envoy
Deploying Cloud Native Red Team Infrastructure with Kubernetes, Istio and Envoy Deploying Cloud Native Red Team Infrastructure with Kubernetes, Istio and Envoy
Deploying Cloud Native Red Team Infrastructure with Kubernetes, Istio and Envoy
 
GWT Web Socket and data serialization
GWT Web Socket and data serializationGWT Web Socket and data serialization
GWT Web Socket and data serialization
 
Hyperledger
HyperledgerHyperledger
Hyperledger
 
Blockchain architected
Blockchain architectedBlockchain architected
Blockchain architected
 

Dernier

DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????blackmambaettijean
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 

Dernier (20)

DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 

Hyperledger Fabric Chaincode Use Cases Guide

  • 2. Chaincode Use Cases • Chaincode is a program, written in Go, node.js, or Java that implements a prescribed interface. Chaincode runs in a secured Docker container isolated from the endorsing peer process. Chaincode initializes and manages ledger state through transactions submitted by applications. • A chaincode typically handles business logic agreed to by members of the network, so it may be considered as a “smart contract”. State created by a chaincode is scoped exclusively to that chaincode and can’t be accessed directly by another chaincode. However, within the same network, given the appropriate permission a chaincode may invoke another chaincode to access its state.
  • 3. use cases for Hyperledger Fabric • Shared recordkeeping, smart contracts, supply chain management, shared assets, provenance, and payments are particularly salient now with existing blockchain tech to build apps around. Flexibility of blockchain solutions supports many business use cases even sensitive one.
  • 4. Two Personas • We offer two different perspectives on chaincode. One, from the perspective of an application developer developing a blockchain application/solution entitled Chaincode for Developers, and the other, Chaincode for Operators oriented to the blockchain network operator who is responsible for managing a blockchain network, and who would leverage the Hyperledger Fabric API to install, instantiate, and upgrade chaincode, but would likely not be involved in the development of a chaincode application.
  • 5. Chaincode for Developers • Chaincode is a program, written in Go, node.js, or Java that implements a prescribed interface. Chaincode runs in a secured Docker container isolated from the endorsing peer process. Chaincode initializes and manages the ledger state through transactions submitted by applications. • A chaincode typically handles business logic agreed to by members of the network, so it similar to a “smart contract”. A chaincode can be invoked to update or query the ledger in a proposal transaction. Given the appropriate permission, a chaincode may invoke another chaincode, either in the same channel or in different channels, to access its state. Note that, if the called chaincode is on a different channel from the calling chaincode, only read query is allowed. That is, the called chaincode on a different channel is only a Query, which does not participate in state validation checks in subsequent commit phase.
  • 6. Chaincode API • Every chaincode program must implement the Chaincode interface: 1. Go 2. node.js 3. Java • whose methods are called in response to received transactions. In particular the Init method is called when a chaincode receives an instantiate or upgrade transaction so that the chaincode may perform any necessary initialization, including initialization of application state. The Invoke method is called in response to receiving an invoke transaction to process transaction proposals.
  • 7. Chaincode for Developers • The other interface in the chaincode “shim” APIs is the ChaincodeStubInterface: 1. Go 2. node.js 3. Java • which is used to access and modify the ledger, and to make invocations between chaincodes.
  • 8. Choosing a Location for the Code • If you haven’t been doing programming in Go, you may want to make sure that you have Go Programming Language installed and your system properly configured. • Now, you will want to create a directory for your chaincode application as a child directory of $GOPATH/src/.
  • 9. Choosing a Location for the Code • To keep things simple, let’s use the following command: mkdir -p $GOPATH/src/sacc && cd $GOPATH/src/sacc • Now, let’s create the source file that we’ll fill in with code: touch sacc.go
  • 10. Housekeeping • First, let’s start with some housekeeping. As with every chaincode, it implements the Chaincode interface in particular, Init and Invoke functions. So, let’s add the Go import statements for the necessary dependencies for our chaincode. We’ll import the chaincode shim package and the peer protobuf package. Next, let’s add a struct SimpleAsset as a receiver for Chaincode shim functions.
  • 11. Housekeeping • package main • import ( • "fmt" • "github.com/hyperledger/fabric/core/chaincode/shim" • "github.com/hyperledger/fabric/protos/peer") • // SimpleAsset implements a simple chaincode to manage an asset • type SimpleAsset struct { • }
  • 12. Initializing the Chaincode • Next, we’ll implement the Init function. • // Init is called during chaincode instantiation to initialize any data. • func (t *SimpleAsset) Init(stub shim.ChaincodeStubInterface) peer.Response { • }
  • 13. Initializing the Chaincode • Next, we’ll retrieve the arguments to the Init call using the ChaincodeStubInterface.GetStringArgs function and check for validity. In our case, we are expecting a key-value pair.
  • 14. // Init is called during chaincode instantiation to initialize any // data. Note that chaincode upgrade also calls this function to reset // or to migrate data, so be careful to avoid a scenario where you // inadvertently clobber your ledger's data! func (t *SimpleAsset) Init(stub shim.ChaincodeStubInterface) peer.Response { // Get the args from the transaction proposal args := stub.GetStringArgs() if len(args) != 2 { return shim.Error("Incorrect arguments. Expecting a key and a value") } }
  • 15. Initializing the Chaincode • Next, now that we have established that the call is valid, we’ll store the initial state in the ledger. To do this, we will call ChaincodeStubInterface.PutState with the key and value passed in as the arguments. Assuming all went well, return a peer.Response object that indicates the initialization was a success.
  • 16. Init is called during chaincode instantiation to initialize any // data. Note that chaincode upgrade also calls this function to reset // or to migrate data, so be careful to avoid a scenario where you // inadvertently clobber your ledger's data! func (t *SimpleAsset) Init(stub shim.ChaincodeStubInterface) peer.Response { // Get the args from the transaction proposal args := stub.GetStringArgs() if len(args) != 2 { return shim.Error("Incorrect arguments. Expecting a key and a value") } // Set up any variables or assets here by calling stub.PutState() // We store the key and the value on the ledger err := stub.PutState(args[0], []byte(args[1])) if err != nil { return shim.Error(fmt.Sprintf("Failed to create asset: %s", args[0])) } return shim.Success(nil) }
  • 17. Invoking the Chaincode • First, let’s add the Invoke fun • As with the Init function above, we need to extract the arguments from the ChaincodeStubInterface. The Invoke function’s arguments will be the name of the chaincode application function to invoke. In our case, our application will simply have two functions: set and get, that allow the value of an asset to be set or its current state to be retrieved. We first call ChaincodeStubInterface.GetFunctionAndParameters to extract the function name and the parameters to that chaincode application function.ction’s signature.
  • 18. Invoking the Chaincode • Next, we’ll validate the function name as being either set or get, and invoke those chaincode application functions, returning an appropriate response via the shim.Success or shim.Error functions that will serialize the response into a gRPC protobuf message.
  • 19. Implementing the Chaincode Application • As noted, our chaincode application implements two functions that can be invoked via the Invoke function. Let’s implement those functions now. Note that as we mentioned above, to access the ledger’s state, we will leverage the ChaincodeStubInterface.PutState and ChaincodeStubInterface.GetState functions of the chaincode shim API.
  • 20. Pulling it All Together • Finally, we need to add the main function, which will call the shim.Start function. Here’s the whole chaincode program source.