Skip to content

ascatox/jLedgerClient

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 

Repository files navigation

jLedgerClient

Installation

Prerequisites

Java 8 and Maven installed in your environment.

  1. Add the JitPack repository to your build file
  <repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
  </repositories>
  1. Add the dependency
   <dependency>
	    <groupId>com.github.ascatox</groupId>
	    <artifactId>jLedgerClient</artifactId>
	    <version>1.2.0</version>
   </dependency>
  1. Copy under the folder resources the crypto-config dir coming from your Fabric installation and create a config-fabric-network.json in order to connect the client to your HLF installation.
    A file example is available in the test folder of the project.

Usage

HLFLedgerClient

In your project crate a class that extends HLFLedgerClient.
Instantiated a new HLFLedgerClient

HLFLedgerClient hlfledgerclient = new HFLedgerClient

Now you can view a 3 meeethod to working with chaincode:

  1. doInvoke(String fcn, List<String> args)
  2. doQuery(String fcn, List<String> args)
  3. doRegisterEvent(String eventName, ChaincodeEventListener chaincodeEventListener)

doInvoke

The Invoke method is invoked whenever the state of the blockchain is queried or modified.

public String doInvoke(String fcn, List<String> args) throws JLedgerClientException {
        final InvokeReturn invokeReturn = ledgerInteractionHelper.invokeChaincode(fcn, args);
        try {
            log.debug("BEFORE -> Store Completable Future at " + System.currentTimeMillis());
            invokeReturn.getCompletableFuture().get(configManager.getConfiguration().getTimeout(), TimeUnit.MILLISECONDS);
            log.debug("AFTER -> Store Completable Future at " + System.currentTimeMillis());
            final String payload = invokeReturn.getPayload();
            return payload;
        } catch (Exception e) {
            log.error(fcn.toUpperCase() + " " + e.getMessage());
            throw new JLedgerClientException(fcn + " " + e.getMessage());
        }
    }

This method needs two arguments, the first is the name of the function we want to invoke in the chaincode. The second is the list of args that we want to pass to the chaincode function.
doInvoke returns a string.
Example
final String payload = hlfledgerclient.doInvoke(fcn , args);

doQuery

A chaincode query is somewhat simpler to implement as it involves the entire network, but simply requires communication from client to peer.

public List<String> doQuery(String fcn, List<String> args) throws JLedgerClientException {
        List<String> data = new ArrayList<>();
        try {
            final List<QueryReturn> queryReturns = ledgerInteractionHelper.queryChainCode(fcn, args, null);
            for (QueryReturn queryReturn : queryReturns) {
                data.add(queryReturn.getPayload());
            }
            return data;
        } catch (Exception e) {
            log.error(fcn + " " + e.getMessage());
            throw new JLedgerClientException(fcn + " " + e.getMessage());
        }

It is the same seen just before.
Example
final String payload = hlfledgerclient.doQuery(fcn , args);

doRegisterEvent

 public String doRegisterEvent(String eventName, ChaincodeEventListener chaincodeEventListener) throws JLedgerClientException {
        return ledgerInteractionHelper.getEventHandler().register(eventName, chaincodeEventListener);
    }

To record an event sent by the chaincode you need to instantiate a ChaincodeEventListener.

               @Override
               public void received(String handle, BlockEvent blockEvent, ChaincodeEvent chaincodeEvent) {
                   String payload = new String(chaincodeEvent.getPayload());
                   System.out.println("Event from chaincode: " + chaincodeEvent.getEventName() + " " + payload);

               }
           };

Now you can call doRegisterEvent

Example

ledgerClient.doRegisterEvent("EVENT", chaincodeEventListener);

Watch out!!! The string eventName must be the same as the event created in the chaincode!!!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages