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))));
}
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));
}
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)));
}
}
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();
}
}
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();
}
}
Aggregations