diff --git a/besu/src/main/java/org/hyperledger/besu/chainimport/JsonBlockImporter.java b/besu/src/main/java/org/hyperledger/besu/chainimport/JsonBlockImporter.java index 455001681c0..6123cce2985 100644 --- a/besu/src/main/java/org/hyperledger/besu/chainimport/JsonBlockImporter.java +++ b/besu/src/main/java/org/hyperledger/besu/chainimport/JsonBlockImporter.java @@ -121,8 +121,8 @@ private void setOptionalFields( final GenesisConfigOptions genesisConfig) { // Some fields can only be configured for ethash if (genesisConfig.getPowAlgorithm() != PowAlgorithm.UNSUPPORTED) { - // For simplicity only set these for ethash. Other consensus algorithms use these fields for - // special purposes or ignore them + // For simplicity only set these for PoW consensus algorithms. + // Other consensus algorithms use these fields for special purposes or ignore them. miner.setCoinbase(blockData.getCoinbase().orElse(Address.ZERO)); miner.setExtraData(blockData.getExtraData().orElse(Bytes.EMPTY)); } else if (blockData.getCoinbase().isPresent() || blockData.getExtraData().isPresent()) { diff --git a/besu/src/main/java/org/hyperledger/besu/cli/config/EthNetworkConfig.java b/besu/src/main/java/org/hyperledger/besu/cli/config/EthNetworkConfig.java index 71d55c6ae4b..b103f7fcbd7 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/config/EthNetworkConfig.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/config/EthNetworkConfig.java @@ -57,7 +57,7 @@ public class EthNetworkConfig { private static final String RINKEBY_GENESIS = "/rinkeby.json"; private static final String GOERLI_GENESIS = "/goerli.json"; private static final String DEV_GENESIS = "/dev.json"; - private static final String ECIP1049_DEV_GENESIS = "/ecip1049_dev.json"; + private static final String DEV_ECIP1049_GENESIS = "/ecip1049_dev.json"; private static final String CLASSIC_GENESIS = "/classic.json"; private static final String KOTTI_GENESIS = "/kotti.json"; private static final String MORDOR_GENESIS = "/mordor.json"; @@ -158,7 +158,7 @@ public static EthNetworkConfig getNetworkConfig(final NetworkName networkName) { jsonConfig(CLASSIC_GENESIS), CLASSIC_NETWORK_ID, CLASSIC_BOOTSTRAP_NODES, null); case ECIP1049_DEV: return new EthNetworkConfig( - jsonConfig(ECIP1049_DEV_GENESIS), ECIP1049_DEV_NETWORK_ID, new ArrayList<>(), null); + jsonConfig(DEV_ECIP1049_GENESIS), ECIP1049_DEV_NETWORK_ID, new ArrayList<>(), null); case KOTTI: return new EthNetworkConfig( jsonConfig(KOTTI_GENESIS), KOTTI_NETWORK_ID, KOTTI_BOOTSTRAP_NODES, null); @@ -200,7 +200,7 @@ public static String jsonConfig(final NetworkName network) { case DEV: return jsonConfig(DEV_GENESIS); case ECIP1049_DEV: - return jsonConfig(ECIP1049_DEV_GENESIS); + return jsonConfig(DEV_ECIP1049_GENESIS); case CLASSIC: return jsonConfig(CLASSIC_GENESIS); case KOTTI: diff --git a/config/src/main/java/org/hyperledger/besu/config/PowAlgorithm.java b/config/src/main/java/org/hyperledger/besu/config/PowAlgorithm.java index 1949218a988..edcc7fc9eb0 100644 --- a/config/src/main/java/org/hyperledger/besu/config/PowAlgorithm.java +++ b/config/src/main/java/org/hyperledger/besu/config/PowAlgorithm.java @@ -16,8 +16,8 @@ /** An enumeration of supported Proof-of-work algorithms. */ public enum PowAlgorithm { - ETHASH, UNSUPPORTED, + ETHASH, KECCAK256; public static PowAlgorithm fromString(final String str) { diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetWork.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetWork.java index 6fa252f2237..5d2b6b3ea94 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetWork.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetWork.java @@ -62,7 +62,7 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { final byte[] dagSeed = DirectAcyclicGraphSeed.dagSeed(rawResult.getBlockNumber(), epochCalculator); final String[] result = { - "0x" + BaseEncoding.base16().lowerCase().encode(rawResult.getPrePowHash()), + rawResult.getPrePowHash().toHexString(), "0x" + BaseEncoding.base16().lowerCase().encode(dagSeed), rawResult.getTarget().toHexString(), Quantity.create(rawResult.getBlockNumber()) diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthSubmitWork.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthSubmitWork.java index 8c248d2e7b6..edf3a018b3c 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthSubmitWork.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthSubmitWork.java @@ -55,8 +55,7 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { Bytes.fromHexString(requestContext.getRequiredParameter(0, String.class)).getLong(0), requestContext.getRequiredParameter(2, Hash.class), null, - Bytes.fromHexString(requestContext.getRequiredParameter(1, String.class)) - .toArrayUnsafe()); + Bytes.fromHexString(requestContext.getRequiredParameter(1, String.class))); final boolean result = miner.submitWork(solution); return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), result); } else { diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetTransactionReceiptTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetTransactionReceiptTest.java index e2b19b76d25..658c1bafffb 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetTransactionReceiptTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetTransactionReceiptTest.java @@ -107,7 +107,7 @@ public class EthGetTransactionReceiptTest { Optional.empty(), TransactionGasBudgetCalculator.frontier(), null, - PoWHasher.ETHASH_LIGHT); + Optional.of(PoWHasher.ETHASH_LIGHT)); private final ProtocolSpec statusTransactionTypeSpec = new ProtocolSpec( "status", @@ -133,7 +133,7 @@ public class EthGetTransactionReceiptTest { Optional.empty(), TransactionGasBudgetCalculator.frontier(), null, - PoWHasher.ETHASH_LIGHT); + Optional.of(PoWHasher.ETHASH_LIGHT)); @SuppressWarnings("unchecked") private final ProtocolSchedule protocolSchedule = mock(ProtocolSchedule.class); diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetWorkTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetWorkTest.java index 32c6ab99737..67e71b55595 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetWorkTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetWorkTest.java @@ -31,6 +31,7 @@ import java.util.Optional; import com.google.common.io.BaseEncoding; +import org.apache.tuweni.bytes.Bytes; import org.apache.tuweni.units.bigints.UInt256; import org.junit.Before; import org.junit.Test; @@ -64,8 +65,7 @@ public void shouldReturnCorrectMethodName() { public void shouldReturnCorrectResultOnGenesisDAG() { final JsonRpcRequestContext request = requestWithParams(); final PoWSolverInputs values = - new PoWSolverInputs( - UInt256.fromHexString(hexValue), BaseEncoding.base16().lowerCase().decode(hexValue), 0); + new PoWSolverInputs(UInt256.fromHexString(hexValue), Bytes.fromHexString(hexValue), 0); final String[] expectedValue = { "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0x0000000000000000000000000000000000000000000000000000000000000000", @@ -84,10 +84,7 @@ public void shouldReturnCorrectResultOnGenesisDAG() { public void shouldReturnCorrectResultOnHighBlockSeed() { final JsonRpcRequestContext request = requestWithParams(); final PoWSolverInputs values = - new PoWSolverInputs( - UInt256.fromHexString(hexValue), - BaseEncoding.base16().lowerCase().decode(hexValue), - 30000); + new PoWSolverInputs(UInt256.fromHexString(hexValue), Bytes.fromHexString(hexValue), 30000); final String[] expectedValue = { "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", @@ -114,10 +111,7 @@ public void shouldReturnCorrectResultOnHighBlockSeedEcip1099() { method = new EthGetWork(miningCoordinator); final JsonRpcRequestContext request = requestWithParams(); final PoWSolverInputs values = - new PoWSolverInputs( - UInt256.fromHexString(hexValue), - BaseEncoding.base16().lowerCase().decode(hexValue), - 60000); + new PoWSolverInputs(UInt256.fromHexString(hexValue), Bytes.fromHexString(hexValue), 60000); final String[] expectedValue = { "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthSubmitWorkTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthSubmitWorkTest.java index 88658d17ac4..9a309aad1dc 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthSubmitWorkTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthSubmitWorkTest.java @@ -32,7 +32,6 @@ import java.util.Optional; -import com.google.common.io.BaseEncoding; import org.apache.tuweni.bytes.Bytes; import org.apache.tuweni.units.bigints.UInt256; import org.junit.Before; @@ -74,8 +73,7 @@ public void shouldFailIfNoMiningEnabled() { public void shouldFailIfMissingArguments() { final JsonRpcRequestContext request = requestWithParams(); final PoWSolverInputs values = - new PoWSolverInputs( - UInt256.fromHexString(hexValue), BaseEncoding.base16().lowerCase().decode(hexValue), 0); + new PoWSolverInputs(UInt256.fromHexString(hexValue), Bytes.fromHexString(hexValue), 0); when(miningCoordinator.getWorkDefinition()).thenReturn(Optional.of(values)); assertThatThrownBy( () -> method.response(request), "Missing required json rpc parameter at index 0") @@ -88,10 +86,11 @@ public void shouldReturnTrueIfGivenCorrectResult() { new PoWSolverInputs( UInt256.fromHexString( "0x0083126e978d4fdf3b645a1cac083126e978d4fdf3b645a1cac083126e978d4f"), - new byte[] { - 15, -114, -104, 87, -95, -36, -17, 120, 52, 1, 124, 61, -6, -66, 78, -27, -57, 118, - -18, -64, -103, -91, -74, -121, 42, 91, -14, -98, 101, 86, -43, -51 - }, + Bytes.wrap( + new byte[] { + 15, -114, -104, 87, -95, -36, -17, 120, 52, 1, 124, 61, -6, -66, 78, -27, -57, + 118, -18, -64, -103, -91, -74, -121, 42, 91, -14, -98, 101, 86, -43, -51 + }), 468); final PoWSolution expectedFirstOutput = diff --git a/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/PoWMinerExecutor.java b/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/PoWMinerExecutor.java index 7e73144516e..a342033c73a 100644 --- a/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/PoWMinerExecutor.java +++ b/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/PoWMinerExecutor.java @@ -75,7 +75,7 @@ public PoWBlockMiner createMiner( final PoWSolver solver = new PoWSolver( nonceGenerator, - protocolSchedule.getByBlockNumber(parentHeader.getNumber() + 1).getPoWHasher(), + protocolSchedule.getByBlockNumber(parentHeader.getNumber() + 1).getPoWHasher().get(), stratumMiningEnabled, ethHashObservers, epochCalculator); diff --git a/ethereum/blockcreation/src/test/java/org/hyperledger/besu/ethereum/blockcreation/PoWMiningCoordinatorTest.java b/ethereum/blockcreation/src/test/java/org/hyperledger/besu/ethereum/blockcreation/PoWMiningCoordinatorTest.java index 964db6ef739..a9e5198febb 100644 --- a/ethereum/blockcreation/src/test/java/org/hyperledger/besu/ethereum/blockcreation/PoWMiningCoordinatorTest.java +++ b/ethereum/blockcreation/src/test/java/org/hyperledger/besu/ethereum/blockcreation/PoWMiningCoordinatorTest.java @@ -53,7 +53,7 @@ public void miningCoordinatorIsCreatedDisabledWithNoReportableMiningStatistics() syncState, DEFAULT_REMOTE_SEALERS_LIMIT, DEFAULT_REMOTE_SEALERS_TTL); - final PoWSolution solution = new PoWSolution(1L, Hash.EMPTY, null, new byte[Bytes32.SIZE]); + final PoWSolution solution = new PoWSolution(1L, Hash.EMPTY, null, Bytes32.ZERO); assertThat(miningCoordinator.isMining()).isFalse(); assertThat(miningCoordinator.hashesPerSecond()).isEqualTo(Optional.empty()); diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/EthHash.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/EthHash.java index 648292e0bc0..c53d3bafa7a 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/EthHash.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/EthHash.java @@ -29,6 +29,7 @@ import com.google.common.primitives.Ints; import com.google.common.primitives.Longs; +import org.apache.tuweni.bytes.Bytes; import org.apache.tuweni.bytes.Bytes32; import org.bouncycastle.jcajce.provider.digest.Keccak; @@ -75,18 +76,18 @@ public final class EthHash { * bytes 32 to 63 */ public static PoWSolution hashimotoLight( - final long size, final int[] cache, final byte[] header, final long nonce) { + final long size, final int[] cache, final Bytes header, final long nonce) { return hashimoto(header, size, nonce, (target, ind) -> calcDatasetItem(target, cache, ind)); } public static PoWSolution hashimoto( - final byte[] header, + final Bytes header, final long size, final long nonce, final BiConsumer datasetLookup) { final int n = (int) Long.divideUnsigned(size, MIX_BYTES); final MessageDigest keccak512 = KECCAK_512.get(); - keccak512.update(header); + keccak512.update(header.toArrayUnsafe()); keccak512.update(Longs.toByteArray(Long.reverseBytes(nonce))); final byte[] seed = keccak512.digest(); final ByteBuffer mixBuffer = ByteBuffer.allocate(MIX_BYTES).order(ByteOrder.LITTLE_ENDIAN); @@ -171,7 +172,7 @@ public static void calcDatasetItem(final byte[] buffer, final int[] cache, final * @param header Block Header * @return Truncated BlockHeader hash */ - public static byte[] hashHeader(final SealableBlockHeader header) { + public static Bytes32 hashHeader(final SealableBlockHeader header) { final BytesValueRLPOutput out = new BytesValueRLPOutput(); out.startList(); out.writeBytes(header.getParentHash()); @@ -192,7 +193,8 @@ public static byte[] hashHeader(final SealableBlockHeader header) { out.writeLongScalar(header.getBaseFee().get()); } out.endList(); - return DirectAcyclicGraphSeed.KECCAK_256.get().digest(out.encoded().toArray()); + return Bytes32.wrap( + DirectAcyclicGraphSeed.KECCAK_256.get().digest(out.encoded().toArrayUnsafe())); } /** diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/KeccakHasher.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/KeccakHasher.java index 2be7f9168e1..2d73e4d8cbb 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/KeccakHasher.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/KeccakHasher.java @@ -44,10 +44,10 @@ public PoWSolution hash( final long nonce, final long number, final EpochCalculator epochCalc, - final byte[] prePowHash) { + final Bytes prePowHash) { MessageDigest digest = KECCAK_256.get(); - digest.update(prePowHash); + digest.update(prePowHash.toArrayUnsafe()); digest.update(Bytes.ofUnsignedLong(nonce).toArrayUnsafe()); Bytes32 solution = Bytes32.wrap(digest.digest()); Hash mixHash = Hash.wrap(solution); diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/PoWHasher.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/PoWHasher.java index 0bf7508b1b4..f578e0f8994 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/PoWHasher.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/PoWHasher.java @@ -14,6 +14,8 @@ */ package org.hyperledger.besu.ethereum.mainnet; +import org.apache.tuweni.bytes.Bytes; + public interface PoWHasher { PoWHasher ETHASH_LIGHT = new EthashLight(); @@ -28,7 +30,7 @@ public interface PoWHasher { * @param prePowHash Block Header (without mix digest and nonce) Hash * @return the PoW solution computed by the hashing function */ - PoWSolution hash(long nonce, long number, EpochCalculator epochCalc, byte[] prePowHash); + PoWSolution hash(long nonce, long number, EpochCalculator epochCalc, Bytes prePowHash); /** Implementation of Ethash Hashimoto Light Implementation. */ final class EthashLight implements PoWHasher { @@ -42,7 +44,7 @@ public PoWSolution hash( final long nonce, final long number, final EpochCalculator epochCalc, - final byte[] prePowHash) { + final Bytes prePowHash) { final EthHashCacheFactory.EthHashDescriptor cache = cacheFactory.ethHashCacheFor(number, epochCalc); final PoWSolution solution = @@ -61,7 +63,7 @@ public PoWSolution hash( final long nonce, final long number, final EpochCalculator epochCalc, - final byte[] prePowHash) { + final Bytes prePowHash) { throw new UnsupportedOperationException("Hashing is unsupported"); } } diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/PoWSolution.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/PoWSolution.java index 2353290d81b..fd4047fafce 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/PoWSolution.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/PoWSolution.java @@ -16,19 +16,18 @@ import org.hyperledger.besu.ethereum.core.Hash; -import java.util.Arrays; import java.util.Objects; -import org.apache.tuweni.bytes.Bytes32; +import org.apache.tuweni.bytes.Bytes; public class PoWSolution { private final long nonce; private final Hash mixHash; - private final byte[] powHash; - private final Bytes32 solution; + private final Bytes powHash; + private final Bytes solution; public PoWSolution( - final long nonce, final Hash mixHash, final Bytes32 solution, final byte[] powHash) { + final long nonce, final Hash mixHash, final Bytes solution, final Bytes powHash) { this.nonce = nonce; this.mixHash = mixHash; this.solution = solution; @@ -43,11 +42,11 @@ public Hash getMixHash() { return mixHash; } - public byte[] getPowHash() { + public Bytes getPowHash() { return powHash; } - public Bytes32 getSolution() { + public Bytes getSolution() { return solution; } @@ -59,13 +58,11 @@ public boolean equals(final Object o) { return nonce == that.nonce && Objects.equals(mixHash, that.mixHash) && Objects.equals(solution, that.solution) - && Arrays.equals(powHash, that.powHash); + && Objects.equals(powHash, that.powHash); } @Override public int hashCode() { - int result = Objects.hash(nonce, mixHash, solution); - result = 31 * result + Arrays.hashCode(powHash); - return result; + return Objects.hash(nonce, mixHash, solution, powHash); } } diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/PoWSolver.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/PoWSolver.java index 8750a590ba1..4e7b2bf08d2 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/PoWSolver.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/PoWSolver.java @@ -19,7 +19,6 @@ import org.hyperledger.besu.ethereum.chain.PoWObserver; import org.hyperledger.besu.util.Subscribers; -import java.util.Arrays; import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; @@ -161,7 +160,7 @@ public boolean submitSolution(final PoWSolution solution) { final PoWSolverJob job = jobSnapshot.get(); final PoWSolverInputs inputs = job.getInputs(); - if (!Arrays.equals(inputs.getPrePowHash(), solution.getPowHash())) { + if (!inputs.getPrePowHash().equals(solution.getPowHash())) { LOG.debug("Miner's solution does not match current job"); return false; } diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/PoWSolverInputs.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/PoWSolverInputs.java index 49845fe0ffe..fcb4297f055 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/PoWSolverInputs.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/PoWSolverInputs.java @@ -14,16 +14,15 @@ */ package org.hyperledger.besu.ethereum.mainnet; -import java.util.Arrays; - +import org.apache.tuweni.bytes.Bytes; import org.apache.tuweni.units.bigints.UInt256; public class PoWSolverInputs { private final UInt256 target; - private final byte[] prePowHash; + private final Bytes prePowHash; private final long blockNumber; - public PoWSolverInputs(final UInt256 target, final byte[] prePowHash, final long blockNumber) { + public PoWSolverInputs(final UInt256 target, final Bytes prePowHash, final long blockNumber) { this.target = target; this.prePowHash = prePowHash; this.blockNumber = blockNumber; @@ -33,7 +32,7 @@ public UInt256 getTarget() { return target; } - public byte[] getPrePowHash() { + public Bytes getPrePowHash() { return prePowHash; } @@ -43,11 +42,11 @@ public long getBlockNumber() { @Override public String toString() { - return "MiningSolverInputs{" + return "PoWSolverInputs{" + "target=" + target + ", prePowHash=" - + Arrays.toString(prePowHash) + + prePowHash + ", blockNumber=" + blockNumber + '}'; diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/ProtocolSpec.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/ProtocolSpec.java index 67b54e28748..0c0f0e44a92 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/ProtocolSpec.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/ProtocolSpec.java @@ -77,7 +77,7 @@ public class ProtocolSpec { private final BadBlockManager badBlockManager; - private final PoWHasher powHasher; + private final Optional powHasher; /** * Creates a new protocol specification instance. @@ -131,7 +131,7 @@ public ProtocolSpec( final Optional eip1559, final TransactionGasBudgetCalculator gasBudgetCalculator, final BadBlockManager badBlockManager, - final PoWHasher powHasher) { + final Optional powHasher) { this.name = name; this.evm = evm; this.transactionValidator = transactionValidator; @@ -361,7 +361,7 @@ public BadBlockManager getBadBlocksManager() { * * @return the Proof-of-Work hasher */ - public PoWHasher getPoWHasher() { + public Optional getPoWHasher() { return powHasher; } } diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/ProtocolSpecBuilder.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/ProtocolSpecBuilder.java index 4577b0894b9..57c0a0133e1 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/ProtocolSpecBuilder.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/ProtocolSpecBuilder.java @@ -270,7 +270,6 @@ public ProtocolSpec build(final ProtocolSchedule protocolSchedule) { checkNotNull(transactionPriceCalculator, "Missing transaction price calculator"); checkNotNull(eip1559, "Missing eip1559 optional wrapper"); checkNotNull(badBlockManager, "Missing bad blocks manager"); - checkNotNull(powHasher, "Missing PoW hasher"); final GasCalculator gasCalculator = gasCalculatorBuilder.get(); final EVM evm = evmBuilder.apply(gasCalculator); @@ -372,7 +371,7 @@ public ProtocolSpec build(final ProtocolSchedule protocolSchedule) { eip1559, gasBudgetCalculator, badBlockManager, - powHasher); + Optional.ofNullable(powHasher)); } public interface TransactionProcessorBuilder { diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/headervalidationrules/ProofOfWorkValidationRule.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/headervalidationrules/ProofOfWorkValidationRule.java index 26261649bb5..7a44de7cbcb 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/headervalidationrules/ProofOfWorkValidationRule.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/headervalidationrules/ProofOfWorkValidationRule.java @@ -61,7 +61,7 @@ public boolean validate(final BlockHeader header, final BlockHeader parent) { final Hash headerHash = hashHeader(header); PoWSolution solution = - hasher.hash(header.getNonce(), header.getNumber(), epochCalculator, headerHash.toArray()); + hasher.hash(header.getNonce(), header.getNumber(), epochCalculator, headerHash); if (header.getDifficulty().isZero()) { LOG.info("Invalid block header: difficulty is 0"); diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/EthHashTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/EthHashTest.java index 1c13a07f991..1eda369deb9 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/EthHashTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/EthHashTest.java @@ -63,7 +63,7 @@ public void hashimotoLightExample() { EthHash.hashimotoLight( 32 * 1024, cache, - Hex.decode("c9149cc0386e689d789a1c2f3d5d169a61a6218ed30e74414dc736e442ef3d1f"), + Bytes.fromHexString("c9149cc0386e689d789a1c2f3d5d169a61a6218ed30e74414dc736e442ef3d1f"), 0L); assertThat(solution.getSolution().toHexString()) diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/KeccakHasherTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/KeccakHasherTest.java index 7ab105e695a..d9aaa13a0a0 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/KeccakHasherTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/KeccakHasherTest.java @@ -71,8 +71,7 @@ public void testHasher() { 42L, new EpochCalculator.DefaultEpochCalculator(), Bytes.fromHexString( - "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef") - .toArrayUnsafe()); + "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef")); assertThat(solution.getMixHash()) .isEqualTo( diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/PoWSolverTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/PoWSolverTest.java index a2032299912..f306ad782cb 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/PoWSolverTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/PoWSolverTest.java @@ -62,7 +62,7 @@ public void hashRateIsProducedSuccessfully() throws InterruptedException, Execut invocation -> { final Object[] args = invocation.getArguments(); final long nonce = ((long) args[0]); - final byte[] prePow = ((byte[]) args[3]); + final Bytes prePow = (Bytes) args[3]; PoWSolution solution = new PoWSolution( nonce, @@ -83,7 +83,7 @@ public void hashRateIsProducedSuccessfully() throws InterruptedException, Execut new EpochCalculator.DefaultEpochCalculator()); final Stopwatch operationTimer = Stopwatch.createStarted(); - final PoWSolverInputs inputs = new PoWSolverInputs(UInt256.ONE, new byte[0], 5); + final PoWSolverInputs inputs = new PoWSolverInputs(UInt256.ONE, Bytes.EMPTY, 5); solver.solveFor(PoWSolver.PoWSolverJob.createFromInputs(inputs)); final double runtimeSeconds = operationTimer.elapsed(TimeUnit.NANOSECONDS) / 1e9; final long worstCaseHashesPerSecond = (long) (noncesToTry.size() / runtimeSeconds); @@ -104,10 +104,11 @@ public void ifInvokedTwiceProducesCorrectAnswerForSecondInvocation() new PoWSolverInputs( UInt256.fromHexString( "0x0083126e978d4fdf3b645a1cac083126e978d4fdf3b645a1cac083126e978d4f"), - new byte[] { - 15, -114, -104, 87, -95, -36, -17, 120, 52, 1, 124, 61, -6, -66, 78, -27, -57, 118, - -18, -64, -103, -91, -74, -121, 42, 91, -14, -98, 101, 86, -43, -51 - }, + Bytes.wrap( + new byte[] { + 15, -114, -104, 87, -95, -36, -17, 120, 52, 1, 124, 61, -6, -66, 78, -27, -57, + 118, -18, -64, -103, -91, -74, -121, 42, 91, -14, -98, 101, 86, -43, -51 + }), 468); final PoWSolution expectedFirstOutput = @@ -122,10 +123,11 @@ public void ifInvokedTwiceProducesCorrectAnswerForSecondInvocation() new PoWSolverInputs( UInt256.fromHexString( "0x0083126e978d4fdf3b645a1cac083126e978d4fdf3b645a1cac083126e978d4f"), - new byte[] { - -62, 121, -81, -31, 55, -38, -68, 102, -32, 95, -94, -83, -3, -48, -122, -68, 14, - -125, -83, 84, -55, -23, -123, -57, -34, 25, -89, 23, 64, -9, -114, -3, - }, + Bytes.wrap( + new byte[] { + -62, 121, -81, -31, 55, -38, -68, 102, -32, 95, -94, -83, -3, -48, -122, -68, 14, + -125, -83, 84, -55, -23, -123, -57, -34, 25, -89, 23, 64, -9, -114, -3, + }), 1); final PoWSolution expectedSecondOutput = diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/headervalidationrules/ProofOfWorkValidationRuleTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/headervalidationrules/ProofOfWorkValidationRuleTest.java index 9496f9a6ece..f89f36b5e6c 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/headervalidationrules/ProofOfWorkValidationRuleTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/headervalidationrules/ProofOfWorkValidationRuleTest.java @@ -98,7 +98,7 @@ public void passesBlockWithOneValuedDifficulty() { preHeader.getNonce(), preHeader.getNumber(), new EpochCalculator.DefaultEpochCalculator(), - headerHash.toArray()); + headerHash); final BlockHeader header = headerBuilder.mixHash(solution.getMixHash()).buildBlockHeader(); diff --git a/ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/NoRewardProtocolScheduleWrapper.java b/ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/NoRewardProtocolScheduleWrapper.java index 742c34f457c..27f2a4d89be 100644 --- a/ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/NoRewardProtocolScheduleWrapper.java +++ b/ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/NoRewardProtocolScheduleWrapper.java @@ -82,7 +82,7 @@ public ProtocolSpec getByBlockNumber(final long number) { original.getEip1559(), original.getGasBudgetCalculator(), original.getBadBlocksManager(), - null); + Optional.empty()); } @Override diff --git a/ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/RetestethContext.java b/ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/RetestethContext.java index ca7983b9397..f06a381c226 100644 --- a/ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/RetestethContext.java +++ b/ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/RetestethContext.java @@ -73,7 +73,7 @@ public class RetestethContext { private static final Logger LOG = LogManager.getLogger(); private static final PoWHasher NO_WORK_HASHER = - (final long nonce, final long number, EpochCalculator epochCalc, final byte[] headerHash) -> + (final long nonce, final long number, EpochCalculator epochCalc, final Bytes headerHash) -> null; private final ReentrantLock contextLock = new ReentrantLock(); diff --git a/ethereum/stratum/src/main/java/org/hyperledger/besu/ethereum/stratum/Stratum1EthProxyProtocol.java b/ethereum/stratum/src/main/java/org/hyperledger/besu/ethereum/stratum/Stratum1EthProxyProtocol.java index 3bb061d9aa7..dc757f2378b 100644 --- a/ethereum/stratum/src/main/java/org/hyperledger/besu/ethereum/stratum/Stratum1EthProxyProtocol.java +++ b/ethereum/stratum/src/main/java/org/hyperledger/besu/ethereum/stratum/Stratum1EthProxyProtocol.java @@ -29,7 +29,6 @@ import org.hyperledger.besu.ethereum.mainnet.PoWSolverInputs; import java.io.IOException; -import java.util.Arrays; import java.util.function.Function; import com.fasterxml.jackson.core.JsonProcessingException; @@ -92,7 +91,7 @@ public boolean canHandle(final String initialMessage, final StratumConnection co private void sendNewWork(final StratumConnection conn, final Object id) { byte[] dagSeed = DirectAcyclicGraphSeed.dagSeed(currentInput.getBlockNumber(), epochCalculator); final String[] result = { - "0x" + BaseEncoding.base16().lowerCase().encode(currentInput.getPrePowHash()), + currentInput.getPrePowHash().toHexString(), "0x" + BaseEncoding.base16().lowerCase().encode(dagSeed), currentInput.getTarget().toHexString() }; @@ -133,8 +132,8 @@ private void handleMiningSubmit(final StratumConnection conn, final JsonRpcReque Bytes.fromHexString(req.getRequiredParameter(0, String.class)).getLong(0), req.getRequiredParameter(2, Hash.class), null, - Bytes.fromHexString(req.getRequiredParameter(1, String.class)).toArrayUnsafe()); - if (Arrays.equals(currentInput.getPrePowHash(), solution.getPowHash())) { + Bytes.fromHexString(req.getRequiredParameter(1, String.class))); + if (currentInput.getPrePowHash().equals(solution.getPowHash())) { result = submitCallback.apply(solution); } diff --git a/ethereum/stratum/src/main/java/org/hyperledger/besu/ethereum/stratum/Stratum1Protocol.java b/ethereum/stratum/src/main/java/org/hyperledger/besu/ethereum/stratum/Stratum1Protocol.java index c509726362c..2f7aa5c9699 100644 --- a/ethereum/stratum/src/main/java/org/hyperledger/besu/ethereum/stratum/Stratum1Protocol.java +++ b/ethereum/stratum/src/main/java/org/hyperledger/besu/ethereum/stratum/Stratum1Protocol.java @@ -30,7 +30,6 @@ import java.io.IOException; import java.time.Instant; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Random; import java.util.function.Function; @@ -185,8 +184,8 @@ private void handleMiningSubmit(final StratumConnection conn, final JsonRpcReque Bytes.fromHexString(message.getRequiredParameter(2, String.class)).getLong(0), Hash.fromHexString(message.getRequiredParameter(4, String.class)), null, - Bytes.fromHexString(message.getRequiredParameter(3, String.class)).toArrayUnsafe()); - if (Arrays.equals(currentInput.getPrePowHash(), solution.getPowHash())) { + Bytes.fromHexString(message.getRequiredParameter(3, String.class))); + if (currentInput.getPrePowHash().equals(solution.getPowHash())) { result = submitCallback.apply(solution); } diff --git a/ethereum/stratum/src/test/java/org/hyperledger/besu/ethereum/stratum/StratumConnectionTest.java b/ethereum/stratum/src/test/java/org/hyperledger/besu/ethereum/stratum/StratumConnectionTest.java index 74b69ed2f18..b44b03efcea 100644 --- a/ethereum/stratum/src/test/java/org/hyperledger/besu/ethereum/stratum/StratumConnectionTest.java +++ b/ethereum/stratum/src/test/java/org/hyperledger/besu/ethereum/stratum/StratumConnectionTest.java @@ -128,8 +128,7 @@ public void testStratum1SendWork() { assertThat(called.get()).isFalse(); // now send work without waiting. protocol.setCurrentWorkTask( - new PoWSolverInputs( - UInt256.valueOf(3), Bytes.fromHexString("deadbeef").toArrayUnsafe(), 42)); + new PoWSolverInputs(UInt256.valueOf(3), Bytes.fromHexString("deadbeef"), 42)); assertThat(message.get()) .isEqualTo(