Search in sources :

Example 1 with EthStatsRequest

use of org.hyperledger.besu.ethstats.request.EthStatsRequest in project besu by hyperledger.

the class EthStatsService method sendPing.

/**
 * Sends a ping request to the ethstats server
 */
private void sendPing() {
    // we store the timestamp when we sent the ping
    pingTimestamp = System.currentTimeMillis();
    sendMessage(webSocket, new EthStatsRequest(NODE_PING, ImmutablePingReport.of(enodeURL.getNodeId().toHexString(), String.valueOf(pingTimestamp))));
}
Also used : EthStatsRequest(org.hyperledger.besu.ethstats.request.EthStatsRequest)

Example 2 with EthStatsRequest

use of org.hyperledger.besu.ethstats.request.EthStatsRequest in project besu by hyperledger.

the class EthStatsService method sendNodeStatsReport.

/**
 * Sends information about the node (is mining, is syncing, etc.)
 */
private void sendNodeStatsReport() {
    final boolean isMiningEnabled;
    if (miningCoordinator instanceof CliqueMiningCoordinator) {
        isMiningEnabled = ((CliqueMiningCoordinator) miningCoordinator).isSigner();
    } else {
        isMiningEnabled = miningCoordinator.isMining();
    }
    final boolean isSyncing = syncState.isInSync();
    final long gasPrice = suggestGasPrice(blockchainQueries.getBlockchain().getChainHeadBlock());
    final long hashrate = miningCoordinator.hashesPerSecond().orElse(0L);
    final int peersNumber = protocolManager.ethContext().getEthPeers().peerCount();
    final NodeStatsReport nodeStatsReport = ImmutableNodeStatsReport.builder().id(enodeURL.getNodeId().toHexString()).stats(true, isMiningEnabled, hashrate, peersNumber, gasPrice, isSyncing, 100).build();
    sendMessage(webSocket, new EthStatsRequest(STATS, nodeStatsReport));
}
Also used : CliqueMiningCoordinator(org.hyperledger.besu.consensus.clique.blockcreation.CliqueMiningCoordinator) EthStatsRequest(org.hyperledger.besu.ethstats.request.EthStatsRequest) ImmutableNodeStatsReport(org.hyperledger.besu.ethstats.report.ImmutableNodeStatsReport) NodeStatsReport(org.hyperledger.besu.ethstats.report.NodeStatsReport)

Example 3 with EthStatsRequest

use of org.hyperledger.besu.ethstats.request.EthStatsRequest in project besu by hyperledger.

the class EthStatsService method sendHistoryReport.

/**
 * Sends a report concerning a set of blocks (range, list of blocks)
 */
private void sendHistoryReport(final List<Long> blocks) {
    final List<BlockResult> blockResults = new ArrayList<>();
    blocks.forEach(blockNumber -> blockchainQueries.blockByNumber(blockNumber).map(tx -> blockResultFactory.transactionComplete(tx, false)).ifPresent(blockResults::add));
    if (!blockResults.isEmpty()) {
        sendMessage(webSocket, new EthStatsRequest(HISTORY, ImmutableHistoryReport.of(enodeURL.getNodeId().toHexString(), blockResults)));
    }
}
Also used : BlockResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResult) EthStatsRequest(org.hyperledger.besu.ethstats.request.EthStatsRequest) ArrayList(java.util.ArrayList)

Example 4 with EthStatsRequest

use of org.hyperledger.besu.ethstats.request.EthStatsRequest in project besu by hyperledger.

the class EthStatsService method start.

public void start() {
    try {
        enodeURL = p2PNetwork.getLocalEnode().orElseThrow();
        vertx.createHttpClient(httpClientOptions).webSocket(webSocketConnectOptions, event -> {
            if (event.succeeded()) {
                webSocket = event.result();
                // reconnect if we lose the connection or if an error occurs
                webSocket.exceptionHandler(ex -> retryConnect());
                webSocket.closeHandler(handler -> retryConnect());
                // listen to the messages from the ethstats server in order to validate the
                // connection
                webSocket.textMessageHandler(ack -> {
                    EthStatsRequest ethStatsRequest = EthStatsRequest.fromResponse(ack);
                    if (ethStatsRequest.getType().equals(READY)) {
                        LOG.info("Connected to ethstats server");
                        // listen to messages from the ethstats server
                        startListeningEthstatsServer();
                        // send a full report after the connection
                        sendFullReport();
                    } else {
                        LOG.error("Failed to login to ethstats server {}", ack);
                    }
                });
                retryInProgress.set(false);
                // sending a hello to initiate the connection using the secret
                sendHello();
            } else {
                String errorMessage = "Failed to reach the ethstats server " + event.cause().getMessage();
                if (event.cause() instanceof SSLHandshakeException) {
                    webSocketConnectOptions.setSsl(false);
                    errorMessage += " (trying without ssl)";
                }
                LOG.error(errorMessage);
                retryInProgress.set(false);
                retryConnect();
            }
        });
    } catch (Exception e) {
        retryConnect();
    }
}
Also used : EthStatsRequest(org.hyperledger.besu.ethstats.request.EthStatsRequest) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) NoSuchElementException(java.util.NoSuchElementException)

Example 5 with EthStatsRequest

use of org.hyperledger.besu.ethstats.request.EthStatsRequest in project besu by hyperledger.

the class EthStatsService method sendHello.

/**
 * Sends a hello request to the ethstats server in order to log in.
 */
private void sendHello() {
    try {
        final Optional<Integer> port = enodeURL.getListeningPort();
        final Optional<BigInteger> chainId = genesisConfigOptions.getChainId();
        if (port.isPresent() && chainId.isPresent()) {
            final String os = PlatformDetector.getOSType();
            final String arch = PlatformDetector.getArch();
            final NodeInfo nodeInfo = ImmutableNodeInfo.of(netstatsUrl.getNodeName(), clientVersion, String.valueOf(port.get()), chainId.get().toString(), protocolManager.getSupportedCapabilities().toString(), "No", os, arch, "0.1.1", true, netstatsUrl.getContact());
            final EthStatsRequest hello = new EthStatsRequest(HELLO, ImmutableAuthenticationData.of(enodeURL.getNodeId().toHexString(), nodeInfo, netstatsUrl.getSecret()));
            sendMessage(webSocket, hello, isSucceeded -> {
                if (!isSucceeded) {
                    retryConnect();
                }
            });
        } else {
            throw new NoSuchElementException();
        }
    } catch (NoSuchElementException e) {
        LOG.error("Failed to find required parameters for ethstats request : {}", e.getMessage());
        retryConnect();
    }
}
Also used : BigInteger(java.math.BigInteger) EthStatsRequest(org.hyperledger.besu.ethstats.request.EthStatsRequest) ImmutableNodeInfo(org.hyperledger.besu.ethstats.authentication.ImmutableNodeInfo) NodeInfo(org.hyperledger.besu.ethstats.authentication.NodeInfo) BigInteger(java.math.BigInteger) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

EthStatsRequest (org.hyperledger.besu.ethstats.request.EthStatsRequest)6 NoSuchElementException (java.util.NoSuchElementException)2 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)1 CliqueMiningCoordinator (org.hyperledger.besu.consensus.clique.blockcreation.CliqueMiningCoordinator)1 BlockResult (org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResult)1 ImmutableNodeInfo (org.hyperledger.besu.ethstats.authentication.ImmutableNodeInfo)1 NodeInfo (org.hyperledger.besu.ethstats.authentication.NodeInfo)1 ImmutableNodeStatsReport (org.hyperledger.besu.ethstats.report.ImmutableNodeStatsReport)1 ImmutablePendingTransactionsReport (org.hyperledger.besu.ethstats.report.ImmutablePendingTransactionsReport)1 NodeStatsReport (org.hyperledger.besu.ethstats.report.NodeStatsReport)1 PendingTransactionsReport (org.hyperledger.besu.ethstats.report.PendingTransactionsReport)1