Skip to content

Commit

Permalink
Add stratum metrics (hyperledger#2583)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
atoulme authored Jul 28, 2021
1 parent d54c89b commit 670a19e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
3 changes: 2 additions & 1 deletion besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,8 @@ public Runner build() {
miningCoordinator,
miningParameters.getStratumPort(),
miningParameters.getStratumNetworkInterface(),
miningParameters.getStratumExtranonce()));
miningParameters.getStratumExtranonce(),
metricsSystem));
miningCoordinator.addEthHashObserver(stratumServer.get());
}

Expand Down
1 change: 1 addition & 0 deletions ethereum/stratum/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies {
implementation project(':ethereum:api')
implementation project(':ethereum:core')
implementation project(':ethereum:blockcreation')
implementation project(':metrics:core')

implementation 'com.google.guava:guava'
implementation 'io.vertx:vertx-core'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,23 @@
import org.hyperledger.besu.ethereum.chain.PoWObserver;
import org.hyperledger.besu.ethereum.mainnet.PoWSolution;
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.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function;

import com.google.common.util.concurrent.AtomicDouble;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.net.NetServer;
import io.vertx.core.net.NetServerOptions;
import io.vertx.core.net.NetSocket;
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
Expand All @@ -39,20 +45,26 @@
public class StratumServer implements PoWObserver {

private static final Logger logger = getLogger();
private static final UInt256 DIFFICULTY_1_TARGET = UInt256.valueOf(2).pow(256);

private final Vertx vertx;
private final int port;
private final String networkInterface;
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 Counter connectionsCount;
private final Counter disconnectionsCount;
private NetServer server;

public StratumServer(
final Vertx vertx,
final MiningCoordinator miningCoordinator,
final int port,
final String networkInterface,
final String extraNonce) {
final String extraNonce,
final MetricsSystem metricsSystem) {
this.vertx = vertx;
this.port = port;
this.networkInterface = networkInterface;
Expand All @@ -61,6 +73,19 @@ public StratumServer(
new Stratum1Protocol(extraNonce, 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() {
Expand Down Expand Up @@ -89,11 +114,18 @@ public CompletableFuture<?> start() {
}

private void handle(final NetSocket socket) {
connectionsCount.inc();
numberOfMiners.incrementAndGet();
StratumConnection conn =
new StratumConnection(
protocols, socket::close, bytes -> socket.write(Buffer.buffer(bytes)));
socket.handler(conn::handleBuffer);
socket.closeHandler(conn::close);
socket.closeHandler(
(aVoid) -> {
conn.close(aVoid);
numberOfMiners.decrementAndGet();
disconnectionsCount.inc();
});
}

public CompletableFuture<?> stop() {
Expand Down Expand Up @@ -126,6 +158,8 @@ public void newJob(final PoWSolverInputs poWSolverInputs) {
for (StratumProtocol protocol : protocols) {
protocol.setCurrentWorkTask(poWSolverInputs);
}
UInt256 difficulty = poWSolverInputs.getTarget().divide(DIFFICULTY_1_TARGET);
currentDifficulty.set(difficulty.toUnsignedBigInteger().doubleValue());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public enum BesuMetricCategory implements MetricCategory {
PRUNER("pruner"),
RPC("rpc"),
SYNCHRONIZER("synchronizer"),
TRANSACTION_POOL("transaction_pool");
TRANSACTION_POOL("transaction_pool"),
STRATUM("stratum");

private static final Optional<String> BESU_PREFIX = Optional.of("besu_");
public static final Set<MetricCategory> DEFAULT_METRIC_CATEGORIES;
Expand Down

0 comments on commit 670a19e

Please sign in to comment.