BCT EXPERIMENT 5
AIM: To Deploy Chain-code on a Permissioned Blockchain.
THEORY:
A permissioned blockchain restricts the participants who can access the network,
unlike public blockchains. Hyperledger Fabric is a commonly used framework for
developing permissioned blockchain applications. The chain code, or smart
contract in Hyperledger, governs the business logic on the blockchain. It defines
the interactions with the ledger and is executed by the nodes within the network.
Deploying chain code involves several steps:
1. Network Setup: Setting up the network of peers, orderers, and channels.
2. Writing Chain code: Writing the business logic to manage the ledger state
(typically in Go, Java, or Node.js).
3. Packaging Chain code: Packaging the chain code as a binary package for
deployment.
4. Installing Chain code: Installing the packaged chain code on the peers.
5. Approving and Committing: Approving the chain code on the channel and
committing it to the ledger.
6. Invoking Chain code: Interacting with the chain code by invoking transactions.
Hyperledger Fabric uses a modular architecture to provide flexibility in deploying
and managing the network, consensus mechanisms, and smart contract operations.
The Fabric SDK is often used to interact with the blockchain and deploy chain
code.
CODE:
This example assumes you're using Hyperledger Fabric with chain code written in
Go. The chain code manages simple asset creation and transfer on the blockchain.
Chain code (Go) -
`asset_transfer.go` go
package main
import (
"encoding/json"
"fmt"
"github.com/hyperledger/fabric-contract-api-go/contractapi"
)
type SmartContract struct {
contractapi.Contract
}
type Asset struct {
ID string `json:"id"`
Owner string `json:"owner"`
Value int `json:"value"`
}
func (s *SmartContract) CreateAsset(ctx contractapi.TransactionContextInterface,
id string, owner string, value int) error {
asset := Asset{
ID: id,
Owner: owner,
Value: value,
}
assetJSON, err :=
json.Marshal(asset) if err != nil
{
return err
}
return ctx.GetStub().PutState(id, assetJSON)
}
func (s *SmartContract) TransferAsset(ctx
contractapi.TransactionContextInterface, id string, newOwner string) error {
assetJSON, err :=
ctx.GetStub().GetState(id) if err != nil
{
return err
}
if assetJSON == nil {
return fmt.Errorf("Asset %s does not exist", id)
}
var asset Asset
err = json.Unmarshal(assetJSON,
&asset) if err != nil {
return err
}
asset.Owner = newOwner
updatedAssetJSON, err :=
json.Marshal(asset) if err != nil {
return err
}
return ctx.GetStub().PutState(id, updatedAssetJSON)
}
func main() {
chaincode, err :=
contractapi.NewChaincode(&SmartContract{}) if err
!= nil {
fmt.Printf("Error creating chaincode: %s",
err.Error()) return
}
if err := chaincode.Start(); err != nil {
fmt.Printf("Error starting chaincode: %s", err.Error())
}
}
Steps to Deploy Chain code
1. Package the Chain code: bash
peer lifecycle chaincode package asset_transfer.tar.gz --path ./chaincode --lang
golang --label asset_transfer_v1
2. Install Chain code on Peers:
bash
peer lifecycle chaincode install asset_transfer.tar.gz
3. Query Installed Chain code:
bash
peer lifecycle chaincode queryinstalled
4. Approve the Chain code: bash peer lifecycle chaincode approveformyorg --
channelID mychannel --name asset_transfer --version
1.0 --package-id <PACKAGE_ID> --sequence 1 --tls --cafile $ORDERER_CA
5. Commit the Chain code: bash peer lifecycle chaincode commit -o
orderer.example.com:7050 --channelID mychannel --name asset_transfer --
version 1.0 --sequence 1 --tls --cafile $ORDERER_CA
6. Invoke the Chain code:
bash
peer chaincode invoke -o orderer.example.com:7050 --channelID mychannel --
name asset_transfer --tls --cafile $ORDERER_CA -c
'{"Args":["CreateAsset","asset1","owner1","100"]}'
7. Query the Asset: bash
peer chaincode query -C mychannel -n asset_transfer -c
'{"Args":["ReadAsset","asset1"]}'
OUTPUT:
After successful deployment and invocation of the chaincode, you'll see outputs
confirming the asset creation and transfer.
Example of successful invoke response:
json
{
"status": 200,
"message": "Asset created successfully"
}
Querying the asset:
json
{
"id": "asset1",
"owner": "owner1",
"value": 100
}
Transferring the asset:
json
{
"status": 200,
"message": "Asset transferred successfully"
}
CONCLUSION: Hence, we have successfully implemented chain code
deployment on a permissioned blockchain.