[SOLVED] fabric-sdk-java How to implement the Network.addBlockListener method in my App.java

Issue

This Content is from Stack Overflow. Question asked by Víctor Rosillo

I am new to Hyperlegder-Fabric and already manage to connect to a network through the org.hyperledger.fabric.gateway package.

This is my App.java class:


//Fabric Imports
import org.hyperledger.fabric.gateway.*;
import org.hyperledger.fabric.sdk.BlockEvent;
import org.hyperledger.fabric.sdk.ChaincodeEventListener;

//Other Imports
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.InvalidKeyException;
import java.security.PrivateKey;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;


public class App{

public static void main(String[] args) throws IOException {
        // Load an existing wallet holding identities used to access the network.
        Path walletDirectory = Paths.get("wallet");
        Wallet wallet = Wallets.newFileSystemWallet(walletDirectory);

        // Path to a common connection profile describing the network.
        Path networkConfigFile = Paths.get("connection.json");

        // Configure the gateway connection used to access the network.
        Gateway.Builder builder = Gateway.createBuilder()
                .identity(wallet, "user1")
                .networkConfig(networkConfigFile);

        // Create a gateway connection
        try (Gateway gateway = builder.connect()) {

            // Obtain a smart contract deployed on the network.
            Network network = gateway.getNetwork("mychannel");
            Contract contract = network.getContract("fabcar");

            // Submit transactions that store state to the ledger.
            byte[] createCarResult = contract.createTransaction("createCar")
                    .submit("CAR10", "VW", "Polo", "Grey", "Mary");
            System.out.println(new String(createCarResult, StandardCharsets.UTF_8));

            // Evaluate transactions that query state from the ledger.
            byte[] queryAllCarsResult = contract.evaluateTransaction("queryAllCars");
            System.out.println(new String(queryAllCarsResult, StandardCharsets.UTF_8));

        } catch (ContractException | TimeoutException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Now I need to listen for block events, so I see in the documentation that the Network package contains the addBlockListener method (https://hyperledger.github.io/fabric-gateway-java/release-2.2/org/hyperledger/fabric/gateway/Network.html)

My doubt is how i can implement this method in the above App.java file so i can get the block number, etc. I am not a java developer i am struggling a lot on this since.

Appreciate any help.



Solution

The listener you attach is an implementation of the Consumer<BlockEvent> type, where Consumer is a standard Java functional interface:

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/function/Consumer.html

To define your listener you could:

  1. write your own listener class that implements that interface; or
  2. define an anonymous class in-line that implements that interface; or
  3. use a lambda expression as a short-hand for the anonymous class implementation to minimise boiler-plate code.

Using a lambda expression, your code might look like this:

network.addBlockListener(blockEvent -> {
    long blockNumber = blockEvent.getBlockNumber();
    System.out.println("Received block number " + blockNumber);
});

If you add the listener to the network before submitting your transaction, you should see the block event created when your submitted transaction is recorded on the ledger.


This Question was asked in StackOverflow by Víctor Rosillo and Answered by bestbeforetoday It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?