fix difficulty gauge metric in stratumserver, add simple test coverage (#2930)

Signed-off-by: garyschulte <garyschulte@gmail.com>
pull/2928/head
garyschulte 3 years ago committed by GitHub
parent d3c1110a4f
commit 4787cdf1cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      ethereum/stratum/build.gradle
  2. 17
      ethereum/stratum/src/main/java/org/hyperledger/besu/ethereum/stratum/StratumServer.java
  3. 67
      ethereum/stratum/src/test/java/org/hyperledger/besu/ethereum/stratum/StratumServerTest.java

@ -48,4 +48,5 @@ dependencies {
testImplementation 'junit:junit' testImplementation 'junit:junit'
testImplementation 'org.assertj:assertj-core' testImplementation 'org.assertj:assertj-core'
testImplementation 'org.mockito:mockito-core' testImplementation 'org.mockito:mockito-core'
testImplementation project(path: ':metrics:core', configuration: 'testSupportArtifacts')
} }

@ -18,12 +18,15 @@ import static org.apache.logging.log4j.LogManager.getLogger;
import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator; 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.EthHash;
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.metrics.BesuMetricCategory;
import org.hyperledger.besu.plugin.services.MetricsSystem; import org.hyperledger.besu.plugin.services.MetricsSystem;
import org.hyperledger.besu.plugin.services.metrics.Counter; import org.hyperledger.besu.plugin.services.metrics.Counter;
import java.math.BigInteger;
import java.util.Optional;
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.concurrent.atomic.AtomicLong;
@ -45,12 +48,11 @@ import org.apache.tuweni.units.bigints.UInt256;
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); protected final AtomicBoolean started = new AtomicBoolean(false);
private final AtomicLong numberOfMiners = new AtomicLong(0); private final AtomicLong numberOfMiners = new AtomicLong(0);
private final AtomicDouble currentDifficulty = new AtomicDouble(0.0); private final AtomicDouble currentDifficulty = new AtomicDouble(0.0);
private final StratumProtocol[] protocols; private final StratumProtocol[] protocols;
@ -159,7 +161,16 @@ 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);
// reverse the target calculation to get the difficulty
// and ensure we do not get divide by zero:
UInt256 difficulty =
Optional.of(poWSolverInputs.getTarget().toUnsignedBigInteger())
.filter(td -> td.compareTo(BigInteger.ONE) > 0)
.map(EthHash.TARGET_UPPER_BOUND::divide)
.map(UInt256::valueOf)
.orElse(UInt256.MAX_VALUE);
currentDifficulty.set(difficulty.toUnsignedBigInteger().doubleValue()); currentDifficulty.set(difficulty.toUnsignedBigInteger().doubleValue());
} }

@ -0,0 +1,67 @@
/*
* Copyright ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.stratum;
import static org.assertj.core.api.Assertions.assertThat;
import org.hyperledger.besu.ethereum.blockcreation.PoWMiningCoordinator;
import org.hyperledger.besu.ethereum.mainnet.EpochCalculator;
import org.hyperledger.besu.ethereum.mainnet.PoWSolverInputs;
import org.hyperledger.besu.metrics.ObservableMetricsSystem;
import org.hyperledger.besu.metrics.StubMetricsSystem;
import java.util.concurrent.CompletableFuture;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt256;
import org.junit.Test;
import org.mockito.Mockito;
public class StratumServerTest {
@Test
public void runJobSetDifficultyShouldSucceed() {
StubMetricsSystem metrics = new StubMetricsSystem();
stratumServerWithMocks(metrics).newJob(new PoWSolverInputs(UInt256.MAX_VALUE, Bytes.EMPTY, 1));
assertThat(metrics.getGaugeValue("difficulty")).isEqualTo(1d);
}
@Test
public void runJobSetZeroDifficultyShouldNotThrow() {
StubMetricsSystem metrics = new StubMetricsSystem();
stratumServerWithMocks(metrics).newJob(new PoWSolverInputs(UInt256.MIN_VALUE, Bytes.EMPTY, 1));
assertThat(metrics.getGaugeValue("difficulty"))
.isEqualTo(UInt256.MAX_VALUE.toUnsignedBigInteger().doubleValue());
}
private StratumServer stratumServerWithMocks(final ObservableMetricsSystem metrics) {
PoWMiningCoordinator mockPoW =
Mockito.when(Mockito.mock(PoWMiningCoordinator.class).getEpochCalculator())
.thenReturn(new EpochCalculator.DefaultEpochCalculator())
.getMock();
StratumServer ss =
new StratumServer(null, mockPoW, 0, "lo", "", metrics) {
@Override
public CompletableFuture<?> start() {
this.started.set(true);
return null;
}
};
ss.start();
return ss;
}
}
Loading…
Cancel
Save