mirror of https://github.com/hyperledger/besu
NC-1244 Implement JSON-RPC method "eth_getWork" (#70)
* EthGetWork Added with Unit and acceptance tests * EthGetWork Added with Unit and acceptance tests * EthGetWork Added with Unit and acceptance tests * EthGetWork Added with Unit and acceptance tests Debugged * EthGetWork Added with Unit and acceptance tests Debugged Change Requests Actioned
parent
f20bd2716d
commit
1325a419c8
@ -0,0 +1,52 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2018 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. |
||||||
|
*/ |
||||||
|
package tech.pegasys.pantheon.tests.acceptance.jsonrpc; |
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||||
|
import static org.assertj.core.api.Assertions.catchThrowable; |
||||||
|
import static tech.pegasys.pantheon.tests.acceptance.dsl.node.PantheonNodeConfig.pantheonMinerNode; |
||||||
|
import static tech.pegasys.pantheon.tests.acceptance.dsl.node.PantheonNodeConfig.pantheonNode; |
||||||
|
|
||||||
|
import tech.pegasys.pantheon.tests.acceptance.dsl.AcceptanceTestBase; |
||||||
|
import tech.pegasys.pantheon.tests.acceptance.dsl.node.PantheonNode; |
||||||
|
|
||||||
|
import org.junit.Before; |
||||||
|
import org.junit.Test; |
||||||
|
import org.web3j.protocol.exceptions.ClientConnectionException; |
||||||
|
|
||||||
|
public class EthGetWorkAcceptanceTest extends AcceptanceTestBase { |
||||||
|
|
||||||
|
private PantheonNode minerNode; |
||||||
|
private PantheonNode fullNode; |
||||||
|
|
||||||
|
@Before |
||||||
|
public void setUp() throws Exception { |
||||||
|
minerNode = cluster.create(pantheonMinerNode("node1")); |
||||||
|
fullNode = cluster.create(pantheonNode("node2")); |
||||||
|
cluster.start(minerNode, fullNode); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void shouldReturnSuccessResponseWhenMining() throws Exception { |
||||||
|
String[] response = minerNode.eth().getWork(); |
||||||
|
assertThat(response).hasSize(3); |
||||||
|
assertThat(response).doesNotContainNull(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void shouldReturnErrorResponseWhenNotMining() { |
||||||
|
Throwable thrown = catchThrowable(() -> fullNode.eth().getWork()); |
||||||
|
assertThat(thrown).isInstanceOf(ClientConnectionException.class); |
||||||
|
assertThat(thrown.getMessage()).contains("No mining work available yet"); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,51 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2018 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. |
||||||
|
*/ |
||||||
|
package tech.pegasys.pantheon.ethereum.mainnet; |
||||||
|
|
||||||
|
import static tech.pegasys.pantheon.ethereum.mainnet.EthHash.EPOCH_LENGTH; |
||||||
|
|
||||||
|
import tech.pegasys.pantheon.crypto.BouncyCastleMessageDigestFactory; |
||||||
|
import tech.pegasys.pantheon.crypto.Hash; |
||||||
|
|
||||||
|
import java.security.DigestException; |
||||||
|
import java.security.MessageDigest; |
||||||
|
import java.security.NoSuchAlgorithmException; |
||||||
|
|
||||||
|
public class DirectAcyclicGraphSeed { |
||||||
|
|
||||||
|
public static final ThreadLocal<MessageDigest> KECCAK_256 = |
||||||
|
ThreadLocal.withInitial( |
||||||
|
() -> { |
||||||
|
try { |
||||||
|
return BouncyCastleMessageDigestFactory.create(Hash.KECCAK256_ALG); |
||||||
|
} catch (final NoSuchAlgorithmException ex) { |
||||||
|
throw new IllegalStateException(ex); |
||||||
|
} |
||||||
|
}); |
||||||
|
|
||||||
|
public static byte[] dagSeed(final long block) { |
||||||
|
final byte[] seed = new byte[32]; |
||||||
|
if (Long.compareUnsigned(block, EPOCH_LENGTH) >= 0) { |
||||||
|
final MessageDigest keccak256 = KECCAK_256.get(); |
||||||
|
for (int i = 0; i < Long.divideUnsigned(block, EPOCH_LENGTH); ++i) { |
||||||
|
keccak256.update(seed); |
||||||
|
try { |
||||||
|
keccak256.digest(seed, 0, seed.length); |
||||||
|
} catch (final DigestException ex) { |
||||||
|
throw new IllegalStateException(ex); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
return seed; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,62 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2018 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. |
||||||
|
*/ |
||||||
|
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods; |
||||||
|
|
||||||
|
import static org.apache.logging.log4j.LogManager.getLogger; |
||||||
|
|
||||||
|
import tech.pegasys.pantheon.ethereum.blockcreation.AbstractMiningCoordinator; |
||||||
|
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest; |
||||||
|
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcError; |
||||||
|
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcErrorResponse; |
||||||
|
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse; |
||||||
|
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse; |
||||||
|
import tech.pegasys.pantheon.ethereum.mainnet.DirectAcyclicGraphSeed; |
||||||
|
import tech.pegasys.pantheon.ethereum.mainnet.EthHashSolverInputs; |
||||||
|
|
||||||
|
import java.util.Optional; |
||||||
|
import javax.xml.bind.DatatypeConverter; |
||||||
|
|
||||||
|
import org.apache.logging.log4j.Logger; |
||||||
|
|
||||||
|
public class EthGetWork implements JsonRpcMethod { |
||||||
|
|
||||||
|
private final AbstractMiningCoordinator<?, ?> miner; |
||||||
|
private static final Logger LOG = getLogger(); |
||||||
|
|
||||||
|
public EthGetWork(final AbstractMiningCoordinator<?, ?> miner) { |
||||||
|
this.miner = miner; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String getName() { |
||||||
|
return "eth_getWork"; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public JsonRpcResponse response(final JsonRpcRequest req) { |
||||||
|
Optional<EthHashSolverInputs> solver = miner.getWorkDefinition(); |
||||||
|
if (solver.isPresent()) { |
||||||
|
EthHashSolverInputs rawResult = solver.get(); |
||||||
|
byte[] dagSeed = DirectAcyclicGraphSeed.dagSeed(rawResult.getBlockNumber()); |
||||||
|
String[] result = { |
||||||
|
"0x" + DatatypeConverter.printHexBinary(rawResult.getPrePowHash()).toLowerCase(), |
||||||
|
"0x" + DatatypeConverter.printHexBinary(dagSeed).toLowerCase(), |
||||||
|
rawResult.getTarget().toHexString() |
||||||
|
}; |
||||||
|
return new JsonRpcSuccessResponse(req.getId(), result); |
||||||
|
} else { |
||||||
|
LOG.trace("Mining is not operational, eth_getWork request cannot be processed"); |
||||||
|
return new JsonRpcErrorResponse(req.getId(), JsonRpcError.NO_MINING_WORK_FOUND); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,108 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2018 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. |
||||||
|
*/ |
||||||
|
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods; |
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||||
|
import static org.mockito.Mockito.when; |
||||||
|
|
||||||
|
import tech.pegasys.pantheon.ethereum.blockcreation.EthHashMiningCoordinator; |
||||||
|
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest; |
||||||
|
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcError; |
||||||
|
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcErrorResponse; |
||||||
|
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse; |
||||||
|
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse; |
||||||
|
import tech.pegasys.pantheon.ethereum.mainnet.DirectAcyclicGraphSeed; |
||||||
|
import tech.pegasys.pantheon.ethereum.mainnet.EthHashSolverInputs; |
||||||
|
import tech.pegasys.pantheon.util.uint.UInt256; |
||||||
|
|
||||||
|
import java.util.Optional; |
||||||
|
import javax.xml.bind.DatatypeConverter; |
||||||
|
|
||||||
|
import org.junit.Before; |
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
import org.mockito.Mock; |
||||||
|
import org.mockito.junit.MockitoJUnitRunner; |
||||||
|
|
||||||
|
@RunWith(MockitoJUnitRunner.class) |
||||||
|
public class EthGetWorkTest { |
||||||
|
|
||||||
|
private EthGetWork method; |
||||||
|
private final String ETH_METHOD = "eth_getWork"; |
||||||
|
private final String hexValue = |
||||||
|
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"; |
||||||
|
|
||||||
|
@Mock private EthHashMiningCoordinator miningCoordinator; |
||||||
|
|
||||||
|
@Before |
||||||
|
public void setUp() { |
||||||
|
method = new EthGetWork(miningCoordinator); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void shouldReturnCorrectMethodName() { |
||||||
|
assertThat(method.getName()).isEqualTo(ETH_METHOD); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void shouldReturnCorrectResultOnGenesisDAG() { |
||||||
|
final JsonRpcRequest request = requestWithParams(); |
||||||
|
final EthHashSolverInputs values = |
||||||
|
new EthHashSolverInputs( |
||||||
|
UInt256.fromHexString(hexValue), DatatypeConverter.parseHexBinary(hexValue), 0); |
||||||
|
final String[] expectedValue = { |
||||||
|
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", |
||||||
|
"0x0000000000000000000000000000000000000000000000000000000000000000", |
||||||
|
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" |
||||||
|
}; |
||||||
|
final JsonRpcResponse expectedResponse = |
||||||
|
new JsonRpcSuccessResponse(request.getId(), expectedValue); |
||||||
|
when(miningCoordinator.getWorkDefinition()).thenReturn(Optional.of(values)); |
||||||
|
|
||||||
|
JsonRpcResponse actualResponse = method.response(request); |
||||||
|
assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void shouldReturnCorrectResultOnHighBlockSeed() { |
||||||
|
final JsonRpcRequest request = requestWithParams(); |
||||||
|
final EthHashSolverInputs values = |
||||||
|
new EthHashSolverInputs( |
||||||
|
UInt256.fromHexString(hexValue), DatatypeConverter.parseHexBinary(hexValue), 30000); |
||||||
|
final String[] expectedValue = { |
||||||
|
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", |
||||||
|
"0x" + DatatypeConverter.printHexBinary(DirectAcyclicGraphSeed.dagSeed(30000)).toLowerCase(), |
||||||
|
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" |
||||||
|
}; |
||||||
|
final JsonRpcResponse expectedResponse = |
||||||
|
new JsonRpcSuccessResponse(request.getId(), expectedValue); |
||||||
|
when(miningCoordinator.getWorkDefinition()).thenReturn(Optional.of(values)); |
||||||
|
JsonRpcResponse actualResponse = method.response(request); |
||||||
|
assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void shouldReturnErrorOnNoneMiningNode() { |
||||||
|
final JsonRpcRequest request = requestWithParams(); |
||||||
|
final JsonRpcResponse expectedResponse = |
||||||
|
new JsonRpcErrorResponse(request.getId(), JsonRpcError.NO_MINING_WORK_FOUND); |
||||||
|
when(miningCoordinator.getWorkDefinition()).thenReturn(Optional.empty()); |
||||||
|
|
||||||
|
JsonRpcResponse actualResponse = method.response(request); |
||||||
|
assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); |
||||||
|
} |
||||||
|
|
||||||
|
private JsonRpcRequest requestWithParams(final Object... params) { |
||||||
|
return new JsonRpcRequest("2.0", ETH_METHOD, params); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue