Access Chaincode Functions

Posted By : Aman Sharma | 30-Nov-2022

Access Chaincode Functions


 

Objective: To create a class to access the chaincode functions in the project

Tools required: Eclipse IDE

Prerequisites: fabricjavaclient project, Hyperledger Fabric Test network


 

Steps to be followed:

  1. Setting up the Hyperledger Fabric Test network

  2. Creating a new Class with Name ClientApp

  3. Accessing chaincode functions of Carshowroom smart contract

 

Step 1: Setting up the Hyperledger Fabric Test network

  1. Navigate to the test-network folder by using the following command:

cd eclipse-workspace/fabric-samples/test-network

  1. Stop the previously running test network by running the following command:

sudo ./network.sh down

  1. Remove the unused docker images by running the following command:

sudo docker system prune

  1. Start the test network by executing the following command:

sudo ./network.sh up -ca -s couchdb

  1. To create a communication channel for the peers in the test network use the following command:

sudo ./network.sh createChannel -c samplechannel

  1. Check the list of the channels for the test network by executing the following commands:

sudo chmod 666 /var/run/docker.sock

docker exec peer0.org1.example.com peer channel list


 

Step 2: Creating a new Class with Name ClientApp

  1. Right-click on fabricjavaclient project and navigate to New > Class to create a new class with the name ClientApp

  2. Add the following code to import the libraries to ClientApp class:

import java.nio.file.Path;

import java.nio.file.Paths;

import org.hyperledger.fabric.gateway.Contract;

import org.hyperledger.fabric.gateway.Gateway;

import org.hyperledger.fabric.gateway.Network;

import org.hyperledger.fabric.gateway.Wallet;

import org.hyperledger.fabric.gateway.Wallets;


 

  1. Add the following code under the class ClientApp to set the property:

static {

System.setProperty("org.hyperledger.fabric.sdk.service_discovery.as_localhost", "true");

}


 

  1. Write the following code to create a gateway connect with the Carshowroom smart contract and access its chaincode functions:

public static void main(String[] args) throws Exception {

// Load a file system based wallet for managing identities.

Path walletPath = Paths.get("wallet");

Wallet wallet = Wallets.newFileSystemWallet(walletPath);

// load a CCP

Path networkConfigPath = Paths.get("..", "..", "test-network", "organizations", "peerOrganizations", "org1.example.com", "connection-org1.yaml");

 

Gateway.Builder builder = Gateway.createBuilder();

builder.identity(wallet, "appUser").networkConfig(networkConfigPath).discovery(true);

 

// create a gateway connection

try (Gateway gateway = builder.connect()) {

 

// get the network and contract

Network network = gateway.getNetwork("samplechannel");

Contract contract = network.getContract("CarShowroom");

 

byte[] result;

contract.submitTransaction("addNewCar", "1", "Ford", "Grey", "7890");

result = contract.evaluateTransaction("queryCarById", "1");

System.out.println(new String(result));

 

contract.submitTransaction("changeCarOwnership", "1", "Joe");

result = contract.evaluateTransaction("queryCarById", "1");

System.out.println(new String(result));

}

}


 

Note: Press Ctrl+S to save the file


 

Step 3: Accessing chaincode functions of Carshowroom chaincode

  1. Now install, approve, and commit Carshowroom chaincode

  2. In Eclipse, run the ClientApp file to access the chaincode functions

About Author

Author Image
Aman Sharma

. He has working knowledge and hands-on experience on python, javascript, odoo ERP., postgres (database)

Request for Proposal

Name is required

Comment is required

Sending message..