Add stratum metrics (#2583)

* Add stratum metrics

Signed-off-by: Antoine Toulme <antoine@lunar-ocean.com>

* Use a more descriptive name for the target difficulty divisor
Use a double gauge so all difficulty values can be represented

Signed-off-by: Antoine Toulme <antoine@lunar-ocean.com>

* rename field

Signed-off-by: Antoine Toulme <antoine@lunar-ocean.com>
pull/2593/head
Antoine Toulme 3 years ago committed by GitHub
parent d54c89b40c
commit 670a19e7d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java
  2. 1
      ethereum/stratum/build.gradle
  3. 38
      ethereum/stratum/src/main/java/org/hyperledger/besu/ethereum/stratum/StratumServer.java
  4. 3
      metrics/core/src/main/java/org/hyperledger/besu/metrics/BesuMetricCategory.java

@ -521,7 +521,8 @@ public class RunnerBuilder {
miningCoordinator, miningCoordinator,
miningParameters.getStratumPort(), miningParameters.getStratumPort(),
miningParameters.getStratumNetworkInterface(), miningParameters.getStratumNetworkInterface(),
miningParameters.getStratumExtranonce())); miningParameters.getStratumExtranonce(),
metricsSystem));
miningCoordinator.addEthHashObserver(stratumServer.get()); miningCoordinator.addEthHashObserver(stratumServer.get());
} }

@ -34,6 +34,7 @@ dependencies {
implementation project(':ethereum:api') implementation project(':ethereum:api')
implementation project(':ethereum:core') implementation project(':ethereum:core')
implementation project(':ethereum:blockcreation') implementation project(':ethereum:blockcreation')
implementation project(':metrics:core')
implementation 'com.google.guava:guava' implementation 'com.google.guava:guava'
implementation 'io.vertx:vertx-core' implementation 'io.vertx:vertx-core'

@ -20,17 +20,23 @@ import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator;
import org.hyperledger.besu.ethereum.chain.PoWObserver; import org.hyperledger.besu.ethereum.chain.PoWObserver;
import org.hyperledger.besu.ethereum.mainnet.PoWSolution; import org.hyperledger.besu.ethereum.mainnet.PoWSolution;
import org.hyperledger.besu.ethereum.mainnet.PoWSolverInputs; import org.hyperledger.besu.ethereum.mainnet.PoWSolverInputs;
import org.hyperledger.besu.metrics.BesuMetricCategory;
import org.hyperledger.besu.plugin.services.MetricsSystem;
import org.hyperledger.besu.plugin.services.metrics.Counter;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function; import java.util.function.Function;
import com.google.common.util.concurrent.AtomicDouble;
import io.vertx.core.Vertx; import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer; import io.vertx.core.buffer.Buffer;
import io.vertx.core.net.NetServer; import io.vertx.core.net.NetServer;
import io.vertx.core.net.NetServerOptions; import io.vertx.core.net.NetServerOptions;
import io.vertx.core.net.NetSocket; import io.vertx.core.net.NetSocket;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.apache.tuweni.units.bigints.UInt256;
/** /**
* TCP server allowing miners to connect to the client over persistent TCP connections, using the * TCP server allowing miners to connect to the client over persistent TCP connections, using the
@ -39,12 +45,17 @@ import org.apache.logging.log4j.Logger;
public class StratumServer implements PoWObserver { public class StratumServer implements PoWObserver {
private static final Logger logger = getLogger(); private static final Logger logger = getLogger();
private static final UInt256 DIFFICULTY_1_TARGET = UInt256.valueOf(2).pow(256);
private final Vertx vertx; private final Vertx vertx;
private final int port; private final int port;
private final String networkInterface; private final String networkInterface;
private final AtomicBoolean started = new AtomicBoolean(false); private final AtomicBoolean started = new AtomicBoolean(false);
private final AtomicLong numberOfMiners = new AtomicLong(0);
private final AtomicDouble currentDifficulty = new AtomicDouble(0.0);
private final StratumProtocol[] protocols; private final StratumProtocol[] protocols;
private final Counter connectionsCount;
private final Counter disconnectionsCount;
private NetServer server; private NetServer server;
public StratumServer( public StratumServer(
@ -52,7 +63,8 @@ public class StratumServer implements PoWObserver {
final MiningCoordinator miningCoordinator, final MiningCoordinator miningCoordinator,
final int port, final int port,
final String networkInterface, final String networkInterface,
final String extraNonce) { final String extraNonce,
final MetricsSystem metricsSystem) {
this.vertx = vertx; this.vertx = vertx;
this.port = port; this.port = port;
this.networkInterface = networkInterface; this.networkInterface = networkInterface;
@ -61,6 +73,19 @@ public class StratumServer implements PoWObserver {
new Stratum1Protocol(extraNonce, miningCoordinator), new Stratum1Protocol(extraNonce, miningCoordinator),
new Stratum1EthProxyProtocol(miningCoordinator) new Stratum1EthProxyProtocol(miningCoordinator)
}; };
metricsSystem.createLongGauge(
BesuMetricCategory.STRATUM, "miners", "Number of miners connected", numberOfMiners::get);
metricsSystem.createGauge(
BesuMetricCategory.STRATUM,
"difficulty",
"Current mining difficulty",
currentDifficulty::get);
this.connectionsCount =
metricsSystem.createCounter(
BesuMetricCategory.STRATUM, "connections", "Number of connections over time");
this.disconnectionsCount =
metricsSystem.createCounter(
BesuMetricCategory.STRATUM, "disconnections", "Number of disconnections over time");
} }
public CompletableFuture<?> start() { public CompletableFuture<?> start() {
@ -89,11 +114,18 @@ public class StratumServer implements PoWObserver {
} }
private void handle(final NetSocket socket) { private void handle(final NetSocket socket) {
connectionsCount.inc();
numberOfMiners.incrementAndGet();
StratumConnection conn = StratumConnection conn =
new StratumConnection( new StratumConnection(
protocols, socket::close, bytes -> socket.write(Buffer.buffer(bytes))); protocols, socket::close, bytes -> socket.write(Buffer.buffer(bytes)));
socket.handler(conn::handleBuffer); socket.handler(conn::handleBuffer);
socket.closeHandler(conn::close); socket.closeHandler(
(aVoid) -> {
conn.close(aVoid);
numberOfMiners.decrementAndGet();
disconnectionsCount.inc();
});
} }
public CompletableFuture<?> stop() { public CompletableFuture<?> stop() {
@ -126,6 +158,8 @@ public class StratumServer implements PoWObserver {
for (StratumProtocol protocol : protocols) { for (StratumProtocol protocol : protocols) {
protocol.setCurrentWorkTask(poWSolverInputs); protocol.setCurrentWorkTask(poWSolverInputs);
} }
UInt256 difficulty = poWSolverInputs.getTarget().divide(DIFFICULTY_1_TARGET);
currentDifficulty.set(difficulty.toUnsignedBigInteger().doubleValue());
} }
@Override @Override

@ -36,7 +36,8 @@ public enum BesuMetricCategory implements MetricCategory {
PRUNER("pruner"), PRUNER("pruner"),
RPC("rpc"), RPC("rpc"),
SYNCHRONIZER("synchronizer"), SYNCHRONIZER("synchronizer"),
TRANSACTION_POOL("transaction_pool"); TRANSACTION_POOL("transaction_pool"),
STRATUM("stratum");
private static final Optional<String> BESU_PREFIX = Optional.of("besu_"); private static final Optional<String> BESU_PREFIX = Optional.of("besu_");
public static final Set<MetricCategory> DEFAULT_METRIC_CATEGORIES; public static final Set<MetricCategory> DEFAULT_METRIC_CATEGORIES;

Loading…
Cancel
Save