Network Functionality

This page will take you through the API calls for querying the following information:

  • syncing status
  • current peer count
  • network listening status

The APIs at present do not support querying for the network identifier similar to net_version.

The final subsection contains code illustrating all of the above interactions.

Retrieve Syncing Status

The examples below show how to query the APIs for the kernel's sync status.
The functionality is compatible with eth_syncing.
In each code snippet, the sync status is retrieved from the API and printed to the standard output.

// get sync status
SyncInfo status = api.getNet().syncInfo().getObject();

// print status
System.out.format("{ currentBlock: %d,%n  highestBlock: %d,%n  startingBlock: %d }%n",
                  status.getChainBestBlock(),
                  status.getNetworkBestBlock(),
                  status.getStartingBlock());
// get sync status
let status = web3.eth.syncing;

// print status
console.log(status);

Sample output:

{ currentBlock: 306442,
  highestBlock: 306442,
  startingBlock: 306413 }
{ currentBlock: 306442,
  highestBlock: 306442,
  startingBlock: 306413 }

Retrieve Current Number of Peers

The examples below show how to query the APIs for the number of active peers.
The functionality is compatible with net_peerCount.
In each code snippet, the number of active peers is retrieved from the API and printed to the standard output.

// get peer count
int peerCount = api.getNet().getPeerCount().getObject();

// print count
System.out.format("number of active peers = %d%n", peerCount);
// get peer information
List<Node> peers = api.getNet().getActiveNodes().getObject();

// print count
System.out.format("number of active peers = %d%n", peers.size());
// get peer count
let peers = web3.net.peerCount;

// print count
console.log("number of active peers = " + peers);

Sample output:

number of active peers = 6
number of active peers = 6

Retrieve Network Listening Status

The examples below show how to query the APIs to determine if the kernel is actively listening for network connections.
The functionality is compatible with net_listening.
In each code snippet, the listening status is retrieved from the API and printed to the standard output.

// get listening status
boolean listening = api.getNet().isListening().getObject();

// print status
System.out.format("%slistening for connections%n", (listening ? "" : "not "));
// get listening status
let listening = web3.net.listening;

// print status
console.log((listening ? "" : "not ") + "listening for connections",);

Sample output:

listening for connections
listening for connections

Complete Examples

Each code example below retrieves and prints to the standard output the following:

  1. the syncing status,
  2. the number of active peers, and
  3. if the client is listening for connections.
package org.aion.tutorials;

import org.aion.api.IAionAPI;
import org.aion.api.type.ApiMsg;
import org.aion.api.type.Node;
import org.aion.api.type.SyncInfo;

import java.util.List;

public class NetworkExample {

    public static void main(String[] args) {

        // connect to Java API
        IAionAPI api = IAionAPI.init();
        ApiMsg apiMsg = api.connect(IAionAPI.LOCALHOST_URL);

        // failed connection
        if (apiMsg.isError()) {
            System.out.format("Could not connect due to <%s>%n", apiMsg.getErrString());
            System.exit(-1);
        }

        // 1. eth_syncing

        // get sync status
        SyncInfo status = api.getNet().syncInfo().getObject();

        // print status
        System.out.format("{ currentBlock: %d,%n  highestBlock: %d,%n  startingBlock: %d }%n",
                          status.getChainBestBlock(),
                          status.getNetworkBestBlock(),
                          status.getStartingBlock());

        // 2.a) net_peerCount

        // get peer count
        int peerCount = api.getNet().getPeerCount().getObject();

        // print count
        System.out.format("%nnumber of active peers = %d%n", peerCount);

        // 2.b) net_peerCount

        // get peer information
        List<Node> peers = api.getNet().getActiveNodes().getObject();

        // print count
        System.out.format("%nnumber of active peers = %d%n", peers.size());

        // 3. net_listening

        // get listening status
        boolean listening = api.getNet().isListening().getObject();

        // print status
        System.out.format("%n%slistening for connections%n", (listening ? "" : "not "));

        // disconnect from api
        api.destroyApi();

        System.exit(0);
    }
}
const Web3 = require('/path/to/aion/web3');
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

// 1. eth_syncing

// get sync status
let status = web3.eth.syncing;

// print status
console.log(status);

// 2. net_peerCount

// get peer count
let peers = web3.net.peerCount;

// print count
console.log("\nnumber of active peers = " + peers);

// 3. net_listening

// get listening status
let listening = web3.net.listening;

// print status
console.log("\n" +  (listening ? "" : "not ") + "listening for connections",);

Sample output:

{ currentBlock: 306442,
  highestBlock: 306442,
  startingBlock: 306413 }

number of active peers = 6

number of active peers = 6

listening for connections
{ currentBlock: 306442,
  highestBlock: 306442,
  startingBlock: 306413 }

number of active peers = 6

listening for connections{ currentBlock