From 5db020237b7af36fe7877ba87998ff0ffe8cbc49 Mon Sep 17 00:00:00 2001 From: Sally MacFarlane Date: Mon, 4 Nov 2019 11:14:20 +1000 Subject: [PATCH 01/42] edits to test names (#153) Signed-off-by: Sally MacFarlane Signed-off-by: SteveM --- .../java/org/hyperledger/besu/cli/PasswordSubCommandTest.java | 4 ++-- .../org/hyperledger/besu/cli/PublicKeySubCommandTest.java | 2 +- .../java/org/hyperledger/besu/cli/rlp/RLPSubCommandTest.java | 2 +- .../besu/cli/subcommands/blocks/BlocksSubCommandTest.java | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/besu/src/test/java/org/hyperledger/besu/cli/PasswordSubCommandTest.java b/besu/src/test/java/org/hyperledger/besu/cli/PasswordSubCommandTest.java index 27c6abf10ff..db2630815cd 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/PasswordSubCommandTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/PasswordSubCommandTest.java @@ -22,7 +22,7 @@ public class PasswordSubCommandTest extends CommandTestAbstract { @Test - public void passwordSubCommandExistAnbHaveSubCommands() { + public void passwordSubCommandExistsWithHashSubCommand() { final CommandSpec spec = parseCommand().getSpec(); assertThat(spec.subcommands()).containsKeys("password"); assertThat(spec.subcommands().get("password").getSubcommands()).containsKeys("hash"); @@ -39,7 +39,7 @@ public void passwordSubCommandExists() { } @Test - public void passwordHashSubCommandExist() { + public void passwordHashSubCommandExists() { parseCommand("password", "hash"); assertThat(commandOutput.toString()).isEmpty(); diff --git a/besu/src/test/java/org/hyperledger/besu/cli/PublicKeySubCommandTest.java b/besu/src/test/java/org/hyperledger/besu/cli/PublicKeySubCommandTest.java index f5bb2e78f51..3b7f8fef4a1 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/PublicKeySubCommandTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/PublicKeySubCommandTest.java @@ -79,7 +79,7 @@ public class PublicKeySubCommandTest extends CommandTestAbstract { // public-key sub-command @Test - public void publicKeySubCommandExistAnbHaveSubCommands() { + public void publicKeySubCommandExistsAndHasSubCommands() { CommandSpec spec = parseCommand().getSpec(); assertThat(spec.subcommands()).containsKeys(PUBLIC_KEY_SUBCOMMAND_NAME); assertThat(spec.subcommands().get(PUBLIC_KEY_SUBCOMMAND_NAME).getSubcommands()) diff --git a/besu/src/test/java/org/hyperledger/besu/cli/rlp/RLPSubCommandTest.java b/besu/src/test/java/org/hyperledger/besu/cli/rlp/RLPSubCommandTest.java index f40603cc3e9..27c3e62c030 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/rlp/RLPSubCommandTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/rlp/RLPSubCommandTest.java @@ -66,7 +66,7 @@ public class RLPSubCommandTest extends CommandTestAbstract { // RLP sub-command @Test - public void rlpSubCommandExistAnbHaveSubCommands() { + public void rlpSubCommandExistsAndHasSubCommands() { final CommandSpec spec = parseCommand().getSpec(); assertThat(spec.subcommands()).containsKeys(RLP_SUBCOMMAND_NAME); assertThat(spec.subcommands().get(RLP_SUBCOMMAND_NAME).getSubcommands()) diff --git a/besu/src/test/java/org/hyperledger/besu/cli/subcommands/blocks/BlocksSubCommandTest.java b/besu/src/test/java/org/hyperledger/besu/cli/subcommands/blocks/BlocksSubCommandTest.java index 1fda617e7bc..1fda465fc25 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/subcommands/blocks/BlocksSubCommandTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/subcommands/blocks/BlocksSubCommandTest.java @@ -103,7 +103,7 @@ public class BlocksSubCommandTest extends CommandTestAbstract { // Block sub-command @Test - public void blockSubCommandExistAnbHaveSubCommands() { + public void blockSubCommandExistsAndHasSubCommands() { CommandSpec spec = parseCommand().getSpec(); assertThat(spec.subcommands()).containsKeys(BLOCK_SUBCOMMAND_NAME); assertThat(spec.subcommands().get(BLOCK_SUBCOMMAND_NAME).getSubcommands()) From 3ac0ef58ecac46e70f65b33079850b604d4bc45e Mon Sep 17 00:00:00 2001 From: CJ Hare Date: Mon, 4 Nov 2019 17:21:07 +1000 Subject: [PATCH 02/42] Moving JsonRpcMethods classes into the same package, prior to refactor (#154) Signed-off-by: Christopher Hare Signed-off-by: SteveM --- besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java | 2 +- .../org/hyperledger/besu/controller/BesuController.java | 2 +- .../hyperledger/besu/controller/BesuControllerBuilder.java | 2 +- .../besu/controller/CliqueBesuControllerBuilder.java | 2 +- .../besu/controller/IbftBesuControllerBuilder.java | 2 +- .../clique/jsonrpc/CliqueJsonRpcMethodsFactory.java | 2 +- .../consensus/ibft/jsonrpc/IbftJsonRpcMethodsFactory.java | 2 +- .../ethereum/api/jsonrpc/JsonRpcTestMethodsFactory.java | 1 + .../jsonrpc/{methods => method}/EthCallIntegrationTest.java | 2 +- .../{methods => method}/EthEstimateGasIntegrationTest.java | 2 +- .../EthGetBlockByHashIntegrationTest.java | 2 +- .../EthGetBlockByNumberIntegrationTest.java | 2 +- .../EthGetFilterChangesIntegrationTest.java | 2 +- .../EthGetUncleByBlockHashAndIndexIntegrationTest.java | 2 +- .../EthGetUncleByBlockNumberAndIndexIntegrationTest.java | 2 +- .../PrivGetPrivateTransactionIntegrationTest.java | 2 +- .../{internal/methods => method}/JsonRpcMethodFactory.java | 3 ++- .../api/jsonrpc/{ => method}/JsonRpcMethodsFactory.java | 6 +++++- .../api/jsonrpc/AbstractJsonRpcHttpServiceTest.java | 1 + .../api/jsonrpc/JsonRpcHttpServiceHostWhitelistTest.java | 1 + .../ethereum/api/jsonrpc/JsonRpcHttpServiceLoginTest.java | 1 + .../ethereum/api/jsonrpc/JsonRpcHttpServiceRpcApisTest.java | 1 + .../besu/ethereum/api/jsonrpc/JsonRpcHttpServiceTest.java | 1 + .../besu/ethereum/retesteth/RetestethService.java | 2 +- 24 files changed, 29 insertions(+), 18 deletions(-) rename ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/{methods => method}/EthCallIntegrationTest.java (99%) rename ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/{methods => method}/EthEstimateGasIntegrationTest.java (99%) rename ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/{methods => method}/EthGetBlockByHashIntegrationTest.java (99%) rename ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/{methods => method}/EthGetBlockByNumberIntegrationTest.java (99%) rename ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/{methods => method}/EthGetFilterChangesIntegrationTest.java (99%) rename ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/{methods => method}/EthGetUncleByBlockHashAndIndexIntegrationTest.java (98%) rename ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/{methods => method}/EthGetUncleByBlockNumberAndIndexIntegrationTest.java (98%) rename ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/{methods => method}/PrivGetPrivateTransactionIntegrationTest.java (99%) rename ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/{internal/methods => method}/JsonRpcMethodFactory.java (86%) rename ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/{ => method}/JsonRpcMethodsFactory.java (98%) diff --git a/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java b/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java index 94fac0a9911..14d76a451cc 100644 --- a/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java @@ -28,7 +28,6 @@ import org.hyperledger.besu.ethereum.api.graphql.GraphQLProvider; import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcConfiguration; import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcHttpService; -import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcMethodsFactory; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi; import org.hyperledger.besu.ethereum.api.jsonrpc.health.HealthService; import org.hyperledger.besu.ethereum.api.jsonrpc.health.LivenessCheck; @@ -37,6 +36,7 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterManager; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterRepository; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodsFactory; import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration; import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketRequestHandler; import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketService; diff --git a/besu/src/main/java/org/hyperledger/besu/controller/BesuController.java b/besu/src/main/java/org/hyperledger/besu/controller/BesuController.java index b355fe2e200..ef6c31918f4 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/BesuController.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/BesuController.java @@ -21,7 +21,7 @@ import org.hyperledger.besu.ethereum.ProtocolContext; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethodFactory; +import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodFactory; import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator; import org.hyperledger.besu.ethereum.core.PrivacyParameters; import org.hyperledger.besu.ethereum.core.Synchronizer; diff --git a/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java b/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java index a635dd22f32..9d9288063a4 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java @@ -21,7 +21,7 @@ import org.hyperledger.besu.config.GenesisConfigFile; import org.hyperledger.besu.crypto.SECP256K1.KeyPair; import org.hyperledger.besu.ethereum.ProtocolContext; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethodFactory; +import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodFactory; import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator; import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.chain.GenesisState; diff --git a/besu/src/main/java/org/hyperledger/besu/controller/CliqueBesuControllerBuilder.java b/besu/src/main/java/org/hyperledger/besu/controller/CliqueBesuControllerBuilder.java index 9e8eda1a396..e64ac26e3ed 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/CliqueBesuControllerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/CliqueBesuControllerBuilder.java @@ -29,7 +29,7 @@ import org.hyperledger.besu.consensus.common.VoteTallyCache; import org.hyperledger.besu.consensus.common.VoteTallyUpdater; import org.hyperledger.besu.ethereum.ProtocolContext; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethodFactory; +import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodFactory; import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator; import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.core.Address; diff --git a/besu/src/main/java/org/hyperledger/besu/controller/IbftBesuControllerBuilder.java b/besu/src/main/java/org/hyperledger/besu/controller/IbftBesuControllerBuilder.java index fd23839e69b..e9a84123409 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/IbftBesuControllerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/IbftBesuControllerBuilder.java @@ -48,7 +48,7 @@ import org.hyperledger.besu.consensus.ibft.statemachine.IbftRoundFactory; import org.hyperledger.besu.consensus.ibft.validation.MessageValidatorFactory; import org.hyperledger.besu.ethereum.ProtocolContext; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethodFactory; +import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodFactory; import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator; import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.chain.MinedBlockObserver; diff --git a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/CliqueJsonRpcMethodsFactory.java b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/CliqueJsonRpcMethodsFactory.java index a1e87c355e2..b0217992ab6 100644 --- a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/CliqueJsonRpcMethodsFactory.java +++ b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/CliqueJsonRpcMethodsFactory.java @@ -29,8 +29,8 @@ import org.hyperledger.besu.ethereum.ProtocolContext; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethodFactory; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonRpcParameter; +import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodFactory; import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; import org.hyperledger.besu.ethereum.chain.MutableBlockchain; import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive; diff --git a/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/IbftJsonRpcMethodsFactory.java b/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/IbftJsonRpcMethodsFactory.java index 2eb7e3e3261..dcafe3c34de 100644 --- a/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/IbftJsonRpcMethodsFactory.java +++ b/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/IbftJsonRpcMethodsFactory.java @@ -27,8 +27,8 @@ import org.hyperledger.besu.ethereum.ProtocolContext; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethodFactory; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonRpcParameter; +import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodFactory; import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; import java.util.Collection; diff --git a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcTestMethodsFactory.java b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcTestMethodsFactory.java index c25b74ce359..fd827adec73 100644 --- a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcTestMethodsFactory.java +++ b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcTestMethodsFactory.java @@ -24,6 +24,7 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterManager; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterRepository; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodsFactory; import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration; import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; import org.hyperledger.besu.ethereum.blockcreation.EthHashMiningCoordinator; diff --git a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthCallIntegrationTest.java b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthCallIntegrationTest.java similarity index 99% rename from ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthCallIntegrationTest.java rename to ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthCallIntegrationTest.java index d8251a980cc..33d7a4240cc 100644 --- a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthCallIntegrationTest.java +++ b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthCallIntegrationTest.java @@ -12,7 +12,7 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.ethereum.api.jsonrpc.methods; +package org.hyperledger.besu.ethereum.api.jsonrpc.method; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.catchThrowable; diff --git a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthEstimateGasIntegrationTest.java b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthEstimateGasIntegrationTest.java similarity index 99% rename from ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthEstimateGasIntegrationTest.java rename to ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthEstimateGasIntegrationTest.java index e721b236f9f..37d838fefcb 100644 --- a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthEstimateGasIntegrationTest.java +++ b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthEstimateGasIntegrationTest.java @@ -12,7 +12,7 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.ethereum.api.jsonrpc.methods; +package org.hyperledger.besu.ethereum.api.jsonrpc.method; import static org.assertj.core.api.Assertions.assertThat; diff --git a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetBlockByHashIntegrationTest.java b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetBlockByHashIntegrationTest.java similarity index 99% rename from ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetBlockByHashIntegrationTest.java rename to ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetBlockByHashIntegrationTest.java index c827fc59cb6..6eae107548c 100644 --- a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetBlockByHashIntegrationTest.java +++ b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetBlockByHashIntegrationTest.java @@ -12,7 +12,7 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.ethereum.api.jsonrpc.methods; +package org.hyperledger.besu.ethereum.api.jsonrpc.method; import static org.assertj.core.api.Assertions.assertThat; diff --git a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetBlockByNumberIntegrationTest.java b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetBlockByNumberIntegrationTest.java similarity index 99% rename from ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetBlockByNumberIntegrationTest.java rename to ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetBlockByNumberIntegrationTest.java index e6440ad102d..7c83af67a85 100644 --- a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetBlockByNumberIntegrationTest.java +++ b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetBlockByNumberIntegrationTest.java @@ -12,7 +12,7 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.ethereum.api.jsonrpc.methods; +package org.hyperledger.besu.ethereum.api.jsonrpc.method; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.catchThrowable; diff --git a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetFilterChangesIntegrationTest.java b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetFilterChangesIntegrationTest.java similarity index 99% rename from ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetFilterChangesIntegrationTest.java rename to ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetFilterChangesIntegrationTest.java index ee85ca3781b..3d882d02463 100644 --- a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetFilterChangesIntegrationTest.java +++ b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetFilterChangesIntegrationTest.java @@ -12,7 +12,7 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.ethereum.api.jsonrpc.methods; +package org.hyperledger.besu.ethereum.api.jsonrpc.method; import static java.util.Arrays.asList; import static java.util.Collections.emptyList; diff --git a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetUncleByBlockHashAndIndexIntegrationTest.java b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetUncleByBlockHashAndIndexIntegrationTest.java similarity index 98% rename from ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetUncleByBlockHashAndIndexIntegrationTest.java rename to ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetUncleByBlockHashAndIndexIntegrationTest.java index 810fe93428c..d78d5741e12 100644 --- a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetUncleByBlockHashAndIndexIntegrationTest.java +++ b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetUncleByBlockHashAndIndexIntegrationTest.java @@ -12,7 +12,7 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.ethereum.api.jsonrpc.methods; +package org.hyperledger.besu.ethereum.api.jsonrpc.method; import static org.assertj.core.api.Assertions.assertThat; diff --git a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetUncleByBlockNumberAndIndexIntegrationTest.java b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetUncleByBlockNumberAndIndexIntegrationTest.java similarity index 98% rename from ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetUncleByBlockNumberAndIndexIntegrationTest.java rename to ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetUncleByBlockNumberAndIndexIntegrationTest.java index fa49bf7f9c2..b8a361938de 100644 --- a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetUncleByBlockNumberAndIndexIntegrationTest.java +++ b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetUncleByBlockNumberAndIndexIntegrationTest.java @@ -12,7 +12,7 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.ethereum.api.jsonrpc.methods; +package org.hyperledger.besu.ethereum.api.jsonrpc.method; import static org.assertj.core.api.Assertions.assertThat; diff --git a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivGetPrivateTransactionIntegrationTest.java b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/PrivGetPrivateTransactionIntegrationTest.java similarity index 99% rename from ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivGetPrivateTransactionIntegrationTest.java rename to ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/PrivGetPrivateTransactionIntegrationTest.java index ad44bfbe5fc..a90a5d32470 100644 --- a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivGetPrivateTransactionIntegrationTest.java +++ b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/PrivGetPrivateTransactionIntegrationTest.java @@ -12,7 +12,7 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.ethereum.api.jsonrpc.methods; +package org.hyperledger.besu.ethereum.api.jsonrpc.method; import static java.nio.charset.StandardCharsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/JsonRpcMethodFactory.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/JsonRpcMethodFactory.java similarity index 86% rename from ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/JsonRpcMethodFactory.java rename to ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/JsonRpcMethodFactory.java index a6ad31bdc18..2cdd2c55a6e 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/JsonRpcMethodFactory.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/JsonRpcMethodFactory.java @@ -12,9 +12,10 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods; +package org.hyperledger.besu.ethereum.api.jsonrpc.method; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; import java.util.Collection; import java.util.Map; diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcMethodsFactory.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/JsonRpcMethodsFactory.java similarity index 98% rename from ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcMethodsFactory.java rename to ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/JsonRpcMethodsFactory.java index cb11f445881..b5a39b3c08a 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcMethodsFactory.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/JsonRpcMethodsFactory.java @@ -12,10 +12,14 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.ethereum.api.jsonrpc; +package org.hyperledger.besu.ethereum.api.jsonrpc.method; import org.hyperledger.besu.config.GenesisConfigOptions; import org.hyperledger.besu.enclave.Enclave; +import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcConfiguration; +import org.hyperledger.besu.ethereum.api.jsonrpc.LatestNonceProvider; +import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi; +import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApis; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterManager; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.AdminAddPeer; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.AdminChangeLogLevel; diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/AbstractJsonRpcHttpServiceTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/AbstractJsonRpcHttpServiceTest.java index 8cd09584fbc..dee25966e5c 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/AbstractJsonRpcHttpServiceTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/AbstractJsonRpcHttpServiceTest.java @@ -26,6 +26,7 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterManager; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterRepository; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodsFactory; import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration; import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; import org.hyperledger.besu.ethereum.blockcreation.EthHashMiningCoordinator; diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceHostWhitelistTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceHostWhitelistTest.java index 513e6dd51d0..73b23950b17 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceHostWhitelistTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceHostWhitelistTest.java @@ -22,6 +22,7 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.health.HealthService; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterManager; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodsFactory; import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration; import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; import org.hyperledger.besu.ethereum.blockcreation.EthHashMiningCoordinator; diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceLoginTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceLoginTest.java index 6e775b16ad7..e4318c6b74d 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceLoginTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceLoginTest.java @@ -28,6 +28,7 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.NetVersion; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.Web3ClientVersion; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.Web3Sha3; +import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodsFactory; import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration; import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; import org.hyperledger.besu.ethereum.blockcreation.EthHashMiningCoordinator; diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceRpcApisTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceRpcApisTest.java index 15f147384ce..2a8e01b49d8 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceRpcApisTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceRpcApisTest.java @@ -25,6 +25,7 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterManager; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; +import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodsFactory; import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration; import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; import org.hyperledger.besu.ethereum.blockcreation.EthHashMiningCoordinator; diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceTest.java index 431999f3a3d..aaae48d35ee 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceTest.java @@ -29,6 +29,7 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterManager; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; +import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodsFactory; import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration; import org.hyperledger.besu.ethereum.api.query.BlockWithMetadata; import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; diff --git a/ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/RetestethService.java b/ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/RetestethService.java index d2d051c0426..06aec92198f 100644 --- a/ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/RetestethService.java +++ b/ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/RetestethService.java @@ -16,7 +16,6 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcConfiguration; import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcHttpService; -import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcMethodsFactory; import org.hyperledger.besu.ethereum.api.jsonrpc.health.HealthService; import org.hyperledger.besu.ethereum.api.jsonrpc.health.LivenessCheck; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.DebugAccountRange; @@ -31,6 +30,7 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.Web3ClientVersion; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonRpcParameter; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResultFactory; +import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodsFactory; import org.hyperledger.besu.ethereum.retesteth.methods.TestGetLogHash; import org.hyperledger.besu.ethereum.retesteth.methods.TestImportRawBlock; import org.hyperledger.besu.ethereum.retesteth.methods.TestMineBlocks; From a416b4e1e2add291659f017e4dcfb6ea6dc99557 Mon Sep 17 00:00:00 2001 From: Danno Ferrin Date: Mon, 4 Nov 2019 08:08:49 -0700 Subject: [PATCH 03/42] Add --identity flag for client identification in node browsers (#150) Support the "--identity" flag. This adds a fifth field to the normally four part clientId, with the identity in the second position. For example, if the CLI flag "--identity PegaSysEng" were passed in the clientID reported by ethernodes would read `besu/PegaSysEng/v1.3.2/linux-x86_64/oracle_openjdk-java-11` Whereas without the flag it would just read `besu/v1.3.2/linux-x86_64/oracle_openjdk-java-11` Signed-off-by: Danno Ferrin Signed-off-by: SteveM --- .../java/org/hyperledger/besu/BesuInfo.java | 23 +++++++++------- .../org/hyperledger/besu/RunnerBuilder.java | 10 +++++-- .../org/hyperledger/besu/cli/BesuCommand.java | 11 +++++++- .../org/hyperledger/besu/BesuInfoTest.java | 27 +++++++++++++++++++ .../hyperledger/besu/cli/BesuCommandTest.java | 10 +++++++ .../besu/cli/CommandTestAbstract.java | 1 + .../src/test/resources/everything_config.toml | 1 + 7 files changed, 70 insertions(+), 13 deletions(-) diff --git a/besu/src/main/java/org/hyperledger/besu/BesuInfo.java b/besu/src/main/java/org/hyperledger/besu/BesuInfo.java index aae241da000..00e458f5590 100644 --- a/besu/src/main/java/org/hyperledger/besu/BesuInfo.java +++ b/besu/src/main/java/org/hyperledger/besu/BesuInfo.java @@ -16,20 +16,23 @@ import org.hyperledger.besu.util.PlatformDetector; +import java.util.Optional; + public final class BesuInfo { - private static final String CLIENT_IDENTITY = "besu"; - private static final String VERSION = - CLIENT_IDENTITY - + "/v" - + BesuInfo.class.getPackage().getImplementationVersion() - + "/" - + PlatformDetector.getOS() - + "/" - + PlatformDetector.getVM(); + private static final String CLIENT = "besu"; + private static final String VERSION = BesuInfo.class.getPackage().getImplementationVersion(); + private static final String OS = PlatformDetector.getOS(); + private static final String VM = PlatformDetector.getVM(); private BesuInfo() {} public static String version() { - return VERSION; + return String.format("%s/v%s/%s/%s", CLIENT, VERSION, OS, VM); + } + + public static String nodeName(final Optional maybeIdentity) { + return maybeIdentity + .map(identity -> String.format("%s/%s/v%s/%s/%s", CLIENT, identity, VERSION, OS, VM)) + .orElse(version()); } } diff --git a/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java b/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java index 14d76a451cc..0dc79f66843 100644 --- a/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java @@ -131,6 +131,7 @@ public class RunnerBuilder { private ObservableMetricsSystem metricsSystem; private Optional permissioningConfiguration = Optional.empty(); private Collection staticNodes = Collections.emptyList(); + private Optional identityString = Optional.empty(); public RunnerBuilder vertx(final Vertx vertx) { this.vertx = vertx; @@ -247,6 +248,11 @@ public RunnerBuilder staticNodes(final Collection staticNodes) { return this; } + public RunnerBuilder identityString(final Optional identityString) { + this.identityString = identityString; + return this; + } + public Runner build() { Preconditions.checkNotNull(besuController); @@ -290,7 +296,7 @@ public Runner build() { .setBindPort(p2pListenPort) .setMaxPeers(maxPeers) .setSupportedProtocols(subProtocols) - .setClientId(BesuInfo.version()) + .setClientId(BesuInfo.nodeName(identityString)) .setLimitRemoteWireConnectionsEnabled(limitRemoteWireConnectionsEnabled) .setFractionRemoteWireConnectionsAllowed(fractionRemoteConnectionsAllowed); networkingConfiguration.setRlpx(rlpxConfiguration).setDiscovery(discoveryConfiguration); @@ -581,7 +587,7 @@ private Map jsonRpcMethods( final Map methods = new JsonRpcMethodsFactory() .methods( - BesuInfo.version(), + BesuInfo.nodeName(identityString), ethNetworkConfig.getNetworkId(), besuController.getGenesisConfigOptions(), network, diff --git a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java index 613ffe61348..bdc4c9f61df 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java @@ -207,6 +207,13 @@ protected KeyLoader getKeyLoader() { // CLI options defined by user at runtime. // Options parsing is done with CLI library Picocli https://picocli.info/ + @Option( + names = "--identity", + paramLabel = "", + description = "Identification for this node in the Client ID", + arity = "1") + private final Optional identityString = Optional.empty(); + // Completely disables P2P within Besu. @Option( names = {"--p2p-enabled"}, @@ -786,7 +793,7 @@ public void parse( public void run() { try { prepareLogging(); - logger.info("Starting Besu version: {}", BesuInfo.version()); + logger.info("Starting Besu version: {}", BesuInfo.nodeName(identityString)); validateOptions().configure().controller().startPlugins().startSynchronization(); } catch (final Exception e) { throw new ParameterException(this.commandLine, e.getMessage(), e); @@ -846,6 +853,7 @@ private BesuCommand registerConverters() { commandLine.registerConverter(Wei.class, (arg) -> Wei.of(Long.parseUnsignedLong(arg))); commandLine.registerConverter(PositiveNumber.class, PositiveNumber::fromString); commandLine.registerConverter(Hash.class, Hash::fromHexString); + commandLine.registerConverter(Optional.class, Optional::of); metricCategoryConverter.addCategories(BesuMetricCategory.class); metricCategoryConverter.addCategories(StandardMetricCategory.class); @@ -1413,6 +1421,7 @@ private void synchronize( .metricsSystem(metricsSystem) .metricsConfiguration(metricsConfiguration) .staticNodes(staticNodes) + .identityString(identityString) .build(); addShutdownHook(runner); diff --git a/besu/src/test/java/org/hyperledger/besu/BesuInfoTest.java b/besu/src/test/java/org/hyperledger/besu/BesuInfoTest.java index 0b6fcbf0479..8491702a644 100644 --- a/besu/src/test/java/org/hyperledger/besu/BesuInfoTest.java +++ b/besu/src/test/java/org/hyperledger/besu/BesuInfoTest.java @@ -16,6 +16,8 @@ import static org.assertj.core.api.Assertions.assertThat; +import java.util.Optional; + import org.junit.Test; public final class BesuInfoTest { @@ -30,4 +32,29 @@ public final class BesuInfoTest { public void versionStringIsEthstatsFriendly() { assertThat(BesuInfo.version()).matches("[^/]+/v(\\d+\\.\\d+\\.\\d+[^/]*|null)/[^/]+/[^/]+"); } + + /** + * Ethstats wants a version string like <foo>/v<bar>/<baz>/<bif>. Foo is the + * client identity (besu, Geth, Parity, etc). Bar is the version, in semantic version form + * (1.2.3-whatever), baz is OS and chip architecture, and bif is "compiler" - which we use as JVM + * info. + */ + @Test + public void noIdentityNodeNameIsEthstatsFriendly() { + assertThat(BesuInfo.nodeName(Optional.empty())) + .matches("[^/]+/v(\\d+\\.\\d+\\.\\d+[^/]*|null)/[^/]+/[^/]+"); + } + + /** + * Ethstats also accepts a version string like + * <foo>/%lt;qux>/v<bar>/<baz>/<bif>. Foo is the client identity (besu, + * Geth, Parity, etc). Qux is user identity (PegaSysEng, Yes-EIP-1679, etc) Bar is the version, in + * semantic version form (1.2.3-whatever), baz is OS and chip architecture, and bif is "compiler" + * - which we use as JVM info. + */ + @Test + public void userIdentityNodeNameIsEthstatsFriendly() { + assertThat(BesuInfo.nodeName(Optional.of("TestUserIdentity"))) + .matches("[^/]+/[^/]+/v(\\d+\\.\\d+\\.\\d+[^/]*|null)/[^/]+/[^/]+"); + } } diff --git a/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java b/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java index 4e71e52149a..5704da843e1 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java @@ -995,6 +995,16 @@ public void genesisPathDisabledUnderDocker() { assertThat(commandOutput.toString()).isEmpty(); } + @Test + public void identityValueTrueMustBeUsed() { + parseCommand("--identity", "test"); + + verify(mockRunnerBuilder.identityString(eq(Optional.of("test")))).build(); + + assertThat(commandOutput.toString()).isEmpty(); + assertThat(commandErrorOutput.toString()).isEmpty(); + } + @Test public void p2pEnabledOptionValueTrueMustBeUsed() { parseCommand("--p2p-enabled", "true"); diff --git a/besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java b/besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java index 3cc3809a6a2..f28246fd427 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java @@ -207,6 +207,7 @@ public void initMocks() throws Exception { when(mockRunnerBuilder.metricsSystem(any())).thenReturn(mockRunnerBuilder); when(mockRunnerBuilder.metricsConfiguration(any())).thenReturn(mockRunnerBuilder); when(mockRunnerBuilder.staticNodes(any())).thenReturn(mockRunnerBuilder); + when(mockRunnerBuilder.identityString(any())).thenReturn(mockRunnerBuilder); when(mockRunnerBuilder.build()).thenReturn(mockRunner); when(storageService.getByName("rocksdb")).thenReturn(Optional.of(rocksDBStorageFactory)); diff --git a/besu/src/test/resources/everything_config.toml b/besu/src/test/resources/everything_config.toml index d77d6bb25bd..dfa3c032859 100644 --- a/besu/src/test/resources/everything_config.toml +++ b/besu/src/test/resources/everything_config.toml @@ -14,6 +14,7 @@ logging="INFO" node-private-key-file="./path/to/privateKey" # P2P network +identity="PegaSysEng" p2p-enabled=true nat-method="NONE" discovery-enabled=false From a26c73e1b9821cef735e6d5b77f113cd03d735cc Mon Sep 17 00:00:00 2001 From: SteveM Date: Sun, 3 Nov 2019 16:11:11 -0800 Subject: [PATCH 04/42] [PAN-3202] add basic forkId (eip-2124) creation class and related test Signed-off-by: SteveM --- .../controller/BesuControllerBuilder.java | 15 +- .../eth/manager/EthProtocolManager.java | 45 ++- .../besu/ethereum/eth/manager/ForkId.java | 324 ++++++++++++++++++ .../besu/ethereum/eth/manager/ForkIdTest.java | 268 +++++++++++++++ 4 files changed, 650 insertions(+), 2 deletions(-) create mode 100644 ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkId.java create mode 100644 ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdTest.java diff --git a/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java b/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java index 9d9288063a4..57ff466d8ff 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java @@ -347,7 +347,8 @@ protected EthProtocolManager createEthProtocolManager( syncConfig.getComputationParallelism(), clock, metricsSystem, - ethereumWireProtocolConfiguration); + ethereumWireProtocolConfiguration, + gatherForks()); } private List createPeerValidators(final ProtocolSchedule protocolSchedule) { @@ -372,4 +373,16 @@ private List createPeerValidators(final ProtocolSchedule proto protected abstract PluginServiceFactory createAdditionalPluginServices( final Blockchain blockchain); + + private List gatherForks() { + // todo: may need to check what protocol version is being used. (i.e. if less than x return null to disable this check) + List listb = new ArrayList<>(); + Map values = genesisConfig.getConfigOptions(genesisConfigOverrides).asMap(); + values.forEach((x, y) -> { + if (y instanceof Long && !x.equals("chainId")) { + listb.add((Long) y); + } + }); + return listb; + } } diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java index 01243e12fa3..fd2973c3411 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java @@ -62,6 +62,7 @@ public class EthProtocolManager implements ProtocolManager, MinedBlockObserver { private final AtomicBoolean stopped = new AtomicBoolean(false); private final Hash genesisHash; + private final ForkId forkId; private final BigInteger networkId; private final EthPeers ethPeers; private final EthMessages ethMessages; @@ -91,6 +92,8 @@ public EthProtocolManager( this.shutdown = new CountDownLatch(1); genesisHash = blockchain.getBlockHashByNumber(0L).get(); + forkId = ForkId.buildCollection(genesisHash); + ethPeers = new EthPeers(getSupportedProtocol(), clock, metricsSystem); ethMessages = new EthMessages(); ethContext = new EthContext(ethPeers, ethMessages, scheduler); @@ -153,6 +156,46 @@ public EthProtocolManager( metricsSystem); } + public EthProtocolManager( + final Blockchain blockchain, + final WorldStateArchive worldStateArchive, + final BigInteger networkId, + final List peerValidators, + final boolean fastSyncEnabled, + final int syncWorkers, + final int txWorkers, + final int computationWorkers, + final Clock clock, + final MetricsSystem metricsSystem, + final EthProtocolConfiguration ethereumWireProtocolConfiguration, + final List forks) { + this.networkId = networkId; + this.peerValidators = peerValidators; + this.scheduler = new EthScheduler(syncWorkers, txWorkers, computationWorkers, metricsSystem); + this.blockchain = blockchain; + this.fastSyncEnabled = fastSyncEnabled; + + this.shutdown = new CountDownLatch(1); + genesisHash = blockchain.getBlockHashByNumber(0L).get(); + + // todo: check about modification of the above and related constructor + forkId = ForkId.buildCollection(genesisHash, forks, blockchain); + + ethPeers = new EthPeers(getSupportedProtocol(), clock, metricsSystem); + ethMessages = new EthMessages(); + ethContext = new EthContext(ethPeers, ethMessages, scheduler); + + this.blockBroadcaster = new BlockBroadcaster(ethContext); + + // Run validators + for (final PeerValidator peerValidator : this.peerValidators) { + PeerValidatorRunner.runValidator(ethContext, peerValidator); + } + + // Set up request handlers + new EthServer(blockchain, worldStateArchive, ethMessages, ethereumWireProtocolConfiguration); + } + public EthContext ethContext() { return ethContext; } @@ -284,7 +327,7 @@ private void handleStatusMessage(final EthPeer peer, final MessageData data) { if (!status.networkId().equals(networkId)) { LOG.debug("Disconnecting from peer with mismatched network id: {}", status.networkId()); peer.disconnect(DisconnectReason.SUBPROTOCOL_TRIGGERED); - } else if (!status.genesisHash().equals(genesisHash)) { + } else if (forkId.peerCheck(status.genesisHash())) { LOG.debug( "Disconnecting from peer with matching network id ({}), but non-matching genesis hash: {}", networkId, diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkId.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkId.java new file mode 100644 index 00000000000..20efa730ee3 --- /dev/null +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkId.java @@ -0,0 +1,324 @@ +package org.hyperledger.besu.ethereum.eth.manager; + +import org.hyperledger.besu.ethereum.chain.Blockchain; +import org.hyperledger.besu.ethereum.core.Hash; +import org.hyperledger.besu.ethereum.rlp.RLPInput; +import org.hyperledger.besu.ethereum.rlp.RLPOutput; +import org.hyperledger.besu.util.bytes.Bytes32; +import org.hyperledger.besu.util.bytes.BytesValues; + +import java.nio.charset.StandardCharsets; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import java.util.zip.CRC32; + +import static org.hyperledger.besu.util.bytes.BytesValue.wrap; + +public class ForkId { + + private Hash genesisHash; + private CRC32 crc = new CRC32(); + private Long currentHead; + private Long forkNext; + private Long highestKnownFork = 0L; + private ArrayDeque forkAndHashList; + + public ForkId(final Hash genesisHash, final Set forks, final Long currentHead) { + this.genesisHash = genesisHash; + this.currentHead = currentHead; + forkAndHashList = collectForksAndHashes(forks, currentHead); + }; + + public static ForkId buildCollection( + final Hash genesisHash, final List forks, final Blockchain blockchain) { + if (forks == null) { + return new ForkId(genesisHash, null, blockchain.getChainHeadBlockNumber()); + } else { + Set forkSet = new LinkedHashSet<>(forks); + return new ForkId(genesisHash, forkSet, blockchain.getChainHeadBlockNumber()); + } + }; + + public static ForkId buildCollection(final Hash genesisHash, final List forks) { + if (forks == null) { + return new ForkId(genesisHash, null, Long.MAX_VALUE); + } else { + Set forkSet = new LinkedHashSet<>(forks); + return new ForkId(genesisHash, forkSet, Long.MAX_VALUE); + } + }; + + public static ForkId buildCollection(final Hash genesisHash) { + return new ForkId(genesisHash, null, Long.MAX_VALUE); + }; + + public static ForkIdEntry readFrom(final RLPInput in) { + in.enterList(); + final String hash = in.readBytesValue(BytesValues::asString); + final long next = in.readLong(); + in.leaveList(); + return new ForkIdEntry(hash, next); + } + + // Non-RLP entry (for tests) + public static ForkIdEntry createIdEntry(final String hash, final long next) { + return new ForkIdEntry(hash, next); + } + + public ArrayDeque getForkAndHashList() { + return this.forkAndHashList; + } + + public boolean peerCheck(final String forkHash, final Long peerNext) { + // Run the fork checksum validation ruleset: + // 1. If local and remote FORK_CSUM matches, connect. + // The two nodes are in the same fork state currently. They might know + // of differing future forks, but that's not relevant until the fork + // triggers (might be postponed, nodes might be updated to match). + // 2. If the remote FORK_CSUM is a subset of the local past forks and the + // remote FORK_NEXT matches with the locally following fork block number, + // connect. + // Remote node is currently syncing. It might eventually diverge from + // us, but at this current point in time we don't have enough information. + // 3. If the remote FORK_CSUM is a superset of the local past forks and can + // be completed with locally known future forks, connect. + // Local node is currently syncing. It might eventually diverge from + // the remote, but at this current point in time we don't have enough + // information. + // 4. Reject in all other cases. + if (isHashKnown(forkHash)) { + if (currentHead < forkNext) { + return true; + } else { + if (isForkKnown(peerNext)) { + return isRemoteAwareOfPresent(forkHash, peerNext); + } else { + return false; + } + } + } else { + return false; + } + } + + /** + * Non EIP-2124 behaviour + * + * @param peerGenesisOrCheckSumHash + * @return boolean + */ + public boolean peerCheck(final Bytes32 peerGenesisOrCheckSumHash) { + return !peerGenesisOrCheckSumHash.equals(genesisHash); + } + + private boolean isHashKnown(final String forkHash) { + for (ForkIdEntry j : forkAndHashList) { + if (forkHash.equals(j.hash)) { + return true; + } + } + return false; + } + + private boolean isForkKnown(final Long nextFork) { + if (highestKnownFork < nextFork) { + return true; + } + for (ForkIdEntry j : forkAndHashList) { + if (nextFork.equals(j.next)) { + return true; + } + } + return false; + } + + private boolean isRemoteAwareOfPresent(final String forkHash, final Long nextFork) { + for (ForkIdEntry j : forkAndHashList) { + if (forkHash.equals(j.hash)) { + if (nextFork.equals(j.next)) { + return true; + } else if (j.next == 0L) { + return highestKnownFork <= nextFork; // Remote aware of future fork + } else { + return false; + } + } + } + return false; + } + + private ArrayDeque collectForksAndHashes( + final Set forks, final Long currentHead) { + boolean first = true; + ArrayDeque forkList = new ArrayDeque<>(); + Iterator iterator = forks.iterator(); + while (iterator.hasNext()) { + Long forkBlockNumber = iterator.next(); + if (highestKnownFork < forkBlockNumber) { + highestKnownFork = forkBlockNumber; + } + if (first) { + // first fork + first = false; + forkList.add( + new ForkIdEntry( + updateCrc(this.genesisHash.getHexString()), forkBlockNumber)); // Genesis + updateCrc(forkBlockNumber); + + } else if (!iterator.hasNext()) { + // most recent fork + forkList.add(new ForkIdEntry(getCurrentCrcHash(), forkBlockNumber)); + updateCrc(forkBlockNumber); + forkList.add(new ForkIdEntry(getCurrentCrcHash(), 0L)); + if (currentHead > forkBlockNumber) { + this.forkNext = 0L; + } else { + this.forkNext = forkBlockNumber; + } + + } else { + forkList.add(new ForkIdEntry(getCurrentCrcHash(), forkBlockNumber)); + updateCrc(forkBlockNumber); + } + } + return forkList; + } + + private String updateCrc(final Long block) { + byte[] byteRepresentationFork = longToBigEndian(block); + crc.update(byteRepresentationFork, 0, byteRepresentationFork.length); + return getCurrentCrcHash(); + } + + private String updateCrc(final String hash) { + byte[] byteRepresentation = hexStringToByteArray(hash); + crc.update(byteRepresentation, 0, byteRepresentation.length); + return getCurrentCrcHash(); + } + + public String getCurrentCrcHash() { + return "0x" + encodeHexString(BytesValues.ofUnsignedInt(crc.getValue()).getByteArray()); + } + + // TODO use Hash class instead of string for checksum. convert to or from string only when needed + public static class ForkIdEntry { + String hash; + long next; + + public ForkIdEntry(final String hash, final long next) { + this.hash = hash; + this.next = next; + } + + public void writeTo(final RLPOutput out) { + out.startList(); + out.writeBytesValue(wrap(hash.getBytes(StandardCharsets.US_ASCII))); + out.writeLong(next); + out.endList(); + } + + public List asByteList() { + ArrayList forRLP = new ArrayList(); + forRLP.add(hexStringToByteArray(hash)); + forRLP.add(longToBigEndian(next)); + return forRLP; + } + + public List asList() { + ArrayList forRLP = new ArrayList<>(); + forRLP.add(this); + return forRLP; + } + + @Override + public String toString() { + return "IdEntry(hash=" + this.hash + ", next=" + this.next + ")"; + } + + @Override + public boolean equals(final Object obj) { + if (obj instanceof ForkIdEntry) { + ForkIdEntry other = (ForkIdEntry) obj; + return other.hash.equals(this.hash) && other.next == this.next; + } + return false; + } + + @Override + public int hashCode() { + return super.hashCode(); + } + } + + // TODO: Ask / look to see if there is a helper for these below <---------- + private static byte[] hexStringToByteArray(final String s) { + String string = ""; + if (s.startsWith("0x")) { + string = s.replaceFirst("0x", ""); + } + string = (string.length() % 2 == 0 ? "" : "0") + string; + return decodeHexString(string); + } + + // next three methods adopted from: + // https://github.com/bcgit/bc-java/blob/master/core/src/main/java/org/bouncycastle/util/Pack.java + private static byte[] longToBigEndian(final long n) { + byte[] bs = new byte[8]; + intToBigEndian((int) (n >>> 32), bs, 0); + intToBigEndian((int) (n & 0xffffffffL), bs, 4); + return bs; + } + + @SuppressWarnings("MethodInputParametersMustBeFinal") + private static void intToBigEndian(final int n, final byte[] bs, int off) { + bs[off] = (byte) (n >>> 24); + bs[++off] = (byte) (n >>> 16); + bs[++off] = (byte) (n >>> 8); + bs[++off] = (byte) (n); + } + + private static String byteToHex(final byte num) { + char[] hexDigits = new char[2]; + hexDigits[0] = Character.forDigit((num >> 4) & 0xF, 16); + hexDigits[1] = Character.forDigit((num & 0xF), 16); + return new String(hexDigits); + } + + private static String encodeHexString(final byte[] byteArray) { + StringBuilder hexStringBuffer = new StringBuilder(); + for (int i = 0; i < byteArray.length; i++) { + hexStringBuffer.append(byteToHex(byteArray[i])); + } + return hexStringBuffer.toString(); + } + + private static byte[] decodeHexString(final String hexString) { + if (hexString.length() % 2 == 1) { + throw new IllegalArgumentException("Invalid hexadecimal String supplied."); + } + + byte[] bytes = new byte[hexString.length() / 2]; + for (int i = 0; i < hexString.length(); i += 2) { + bytes[i / 2] = hexToByte(hexString.substring(i, i + 2)); + } + return bytes; + } + + private static byte hexToByte(final String hexString) { + int firstDigit = toDigit(hexString.charAt(0)); + int secondDigit = toDigit(hexString.charAt(1)); + return (byte) ((firstDigit << 4) + secondDigit); + } + + private static int toDigit(final char hexChar) { + int digit = Character.digit(hexChar, 16); + if (digit == -1) { + throw new IllegalArgumentException("Invalid Hexadecimal Character: " + hexChar); + } + return digit; + } +} diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdTest.java new file mode 100644 index 00000000000..8983ee97d07 --- /dev/null +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdTest.java @@ -0,0 +1,268 @@ +package org.hyperledger.besu.ethereum.eth.manager; + +import org.hyperledger.besu.ethereum.core.Hash; +import org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput; +import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput; +import org.hyperledger.besu.util.bytes.BytesValue; +import org.junit.Test; + +import java.util.ArrayDeque; +import java.util.Arrays; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ForkIdTest { + private Long[] forksMainnet = {1150000L, 1920000L, 2463000L, 2675000L, 4370000L, 7280000L}; + private String mainnetGenHash = + "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"; + + @Test + public void checkCorrectMainnetForkIdHashesGenerated() { + ForkId.ForkIdEntry[] checkIds = { + ForkId.createIdEntry("0xfc64ec04", 1150000L), // Unsynced + ForkId.createIdEntry("0x97c2c34c", 1920000L), // First Homestead block + ForkId.createIdEntry("0x91d1f948", 2463000L), // First DAO block + ForkId.createIdEntry("0x7a64da13", 2675000L), // First Tangerine block + ForkId.createIdEntry("0x3edd5b10", 4370000L), // First Spurious block + ForkId.createIdEntry("0xa00bc324", 7280000L), // First Byzantium block + ForkId.createIdEntry("0x668db0af", 0L) // Today Petersburg block + }; + List list = Arrays.asList(forksMainnet); + ForkId forkId = ForkId.buildCollection(Hash.fromHexString(mainnetGenHash), list); + ArrayDeque entries = forkId.getForkAndHashList(); + + for (ForkId.ForkIdEntry id : checkIds) { + ForkId.ForkIdEntry testVal = entries.poll(); + if (testVal == null) { + break; + } + assertThat(testVal.equals(id)).isTrue(); + } + } + + @Test + public void checkCorrectRopstenForkIdHashesGenerated() { + Long[] forks = {10L, 1700000L, 4230000L, 4939394L}; + String genHash = "0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d"; + ForkId.ForkIdEntry[] checkIds = { + ForkId.createIdEntry( + "0x30c7ddbc", 10L), // Unsynced, last Frontier, Homestead and first Tangerine block + ForkId.createIdEntry("0x63760190", 1700000L), // First Spurious block + ForkId.createIdEntry("0x3ea159c7", 4230000L), // First Byzantium block + ForkId.createIdEntry("0x97b544f3", 4939394L), // First Constantinople block + ForkId.createIdEntry("0xd6e2149b", 0L) // Today Petersburg block + }; + List list = Arrays.asList(forks); + ForkId forkId = ForkId.buildCollection(Hash.fromHexString(genHash), list); + ArrayDeque entries = forkId.getForkAndHashList(); + + for (ForkId.ForkIdEntry id : checkIds) { + ForkId.ForkIdEntry testVal = entries.poll(); + if (testVal == null) { + break; + } + assertThat(testVal.equals(id)).isTrue(); + } + } + + @Test + public void checkCorrectRinkebyForkIdHashesGenerated() { + Long[] forks = {1L, 2L, 3L, 1035301L, 3660663L, 4321234L}; + String genHash = "0x6341fd3daf94b748c72ced5a5b26028f2474f5f00d824504e4fa37a75767e177"; + ForkId.ForkIdEntry[] checkIds = { + ForkId.createIdEntry( + "0x3b8e0691", 1L), // Unsynced, last Frontier, Homestead and first Tangerine block + ForkId.createIdEntry("0x60949295", 2L), // Last Tangerine block + ForkId.createIdEntry("0x8bde40dd", 3L), // First Spurious block + ForkId.createIdEntry("0xcb3a64bb", 1035301L), // First Byzantium block + ForkId.createIdEntry("0x8d748b57", 3660663L), // First Constantinople block + ForkId.createIdEntry("0xe49cab14", 4321234L), // First Petersburg block + ForkId.createIdEntry("0xafec6b27", 0L) // Today Petersburg block + }; + List list = Arrays.asList(forks); + ForkId forkId = ForkId.buildCollection(Hash.fromHexString(genHash), list); + ArrayDeque entries = forkId.getForkAndHashList(); + + for (ForkId.ForkIdEntry id : checkIds) { + ForkId.ForkIdEntry testVal = entries.poll(); + if (testVal == null) { + break; + } + assertThat(testVal.equals(id)).isTrue(); + } + } + + @Test + public void check1PetersburgWithRemoteAnnouncingTheSame() { + // 1 Local is mainnet Petersburg, remote announces the same. No future fork is announced. + // {7987396, ID{Hash: 0x668db0af, Next: 0}, nil}, + List list = Arrays.asList(forksMainnet); + Set forkSet = new LinkedHashSet<>(list); + ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + Boolean result = forkId.peerCheck("0x668db0af", 0L); + assertThat(result).isTrue(); + } + + @Test + public void check2PetersburgWithRemoteAnnouncingTheSameAndNextFork() { + // 2 Local is mainnet Petersburg, remote announces the same. Remote also announces a next fork + // at block 0xffffffff, but that is uncertain. + // {7987396, ID{Hash: 0x668db0af, Next: math.MaxUint64}, nil}, + List list = Arrays.asList(forksMainnet); + Set forkSet = new LinkedHashSet<>(list); + ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + Boolean result = forkId.peerCheck("0x668db0af", Long.MAX_VALUE); + assertThat(result).isTrue(); + } + + @Test + public void check3ByzantiumAwareOfPetersburgRemoteUnawareOfPetersburg() { + // 3 Local is mainnet currently in Byzantium only (so it's aware of Petersburg), remote + // announces also Byzantium, but it's not yet aware of Petersburg (e.g. non updated node before + // the fork). + // In this case we don't know if Petersburg passed yet or not. + // {7279999, ID{Hash: 0xa00bc324, Next: 0}, nil}, + List list = Arrays.asList(forksMainnet); + Set forkSet = new LinkedHashSet<>(list); + ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); + Boolean result = forkId.peerCheck("0xa00bc324", 0L); + assertThat(result).isTrue(); + } + + @Test + public void check4ByzantiumAwareOfPetersburgRemoteAwareOfPetersburg() { + // 4 Local is mainnet currently in Byzantium only (so it's aware of Petersburg), remote + // announces also Byzantium, and it's also aware of Petersburg (e.g. updated node before the + // fork). We don't know if Petersburg passed yet (will pass) or not. + // {7279999, ID{Hash: 0xa00bc324, Next: 7280000}, nil}, + List list = Arrays.asList(forksMainnet); + Set forkSet = new LinkedHashSet<>(list); + ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); + Boolean result = forkId.peerCheck("0xa00bc324", 7280000L); + assertThat(result).isTrue(); + } + + @Test + public void check5ByzantiumAwareOfPetersburgRemoteAnnouncingUnknownFork() { + // 5 Local is mainnet currently in Byzantium only (so it's aware of Petersburg), remote + // announces also Byzantium, and it's also aware of some random fork (e.g. misconfigured + // Petersburg). + // As neither forks passed at neither nodes, they may mismatch, but we still connect for now. + // {7279999, ID{Hash: 0xa00bc324, Next: math.MaxUint64}, nil}, + List list = Arrays.asList(forksMainnet); + Set forkSet = new LinkedHashSet<>(list); + ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); + Boolean result = forkId.peerCheck("0xa00bc324", Long.MAX_VALUE); + assertThat(result).isTrue(); + } + + @Test + public void check6PetersburgWithRemoteAnnouncingByzantiumAwareOfPetersburg() { + // 6 Local is mainnet Petersburg, remote announces Byzantium + knowledge about Petersburg. + // Remote is simply out of sync, accept. + // {7987396, ID{Hash: 0x668db0af, Next: 7280000}, nil}, + List list = Arrays.asList(forksMainnet); + Set forkSet = new LinkedHashSet<>(list); + ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + Boolean result = forkId.peerCheck("0x668db0af", 7280000L); + assertThat(result).isTrue(); + } + + @Test + public void check7PetersburgWithRemoteAnnouncingSpuriousAwareOfByzantiumRemoteMayNeedUpdate() { + // 7 Local is mainnet Petersburg, remote announces Spurious + knowledge about Byzantium. + // Remote is definitely out of sync. It may or may not need the Petersburg update, we don't know + // yet. + // {7987396, ID{Hash: 0x3edd5b10, Next: 4370000}, nil}, + List list = Arrays.asList(forksMainnet); + Set forkSet = new LinkedHashSet<>(list); + ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + Boolean result = forkId.peerCheck("0x3edd5b10", 4370000L); + assertThat(result).isTrue(); + } + + @Test + public void check8ByzantiumWithRemoteAnnouncingPetersburgLocalOutOfSync() { + // 8 Local is mainnet Byzantium, remote announces Petersburg. Local is out of sync, accept. + // {7279999, ID{Hash: 0x668db0af, Next: 0}, nil}, + List list = Arrays.asList(forksMainnet); + Set forkSet = new LinkedHashSet<>(list); + ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); + Boolean result = forkId.peerCheck("0x668db0af", 0L); + assertThat(result).isTrue(); + } + + @Test + public void check9SpuriousWithRemoteAnnouncingByzantiumRemoteUnawareOfPetersburg() { + // 9 Local is mainnet Spurious, remote announces Byzantium, but is not aware of Petersburg. + // Local out of sync. Local also knows about a future fork, but that is uncertain yet. + // {4369999, ID{Hash: 0xa00bc324, Next: 0}, nil}, + List list = Arrays.asList(forksMainnet); + Set forkSet = new LinkedHashSet<>(list); + ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 4369999L); + Boolean result = forkId.peerCheck("0xa00bc324", 0L); + assertThat(result).isTrue(); + } + + @Test + public void check10PetersburgWithRemoteAnnouncingByzantiumRemoteUnawareOfAdditionalForks() { + // 10 Local is mainnet Petersburg. remote announces Byzantium but is not aware of further forks. + // Remote needs software update. + // {7987396, ID{Hash: 0xa00bc324, Next: 0}, ErrRemoteStale}, + List list = Arrays.asList(forksMainnet); + Set forkSet = new LinkedHashSet<>(list); + ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + Boolean result = forkId.peerCheck("0xa00bc324", 0L); + assertThat(result).isFalse(); + } + + @Test + public void check11PetersburgWithRemoteAnnouncingPetersburgAndFutureForkLocalNeedsUpdate() { + // 11 Local is mainnet Petersburg, and isn't aware of more forks. Remote announces Petersburg + + // 0xffffffff. Local needs software update, reject. + // {7987396, ID{Hash: 0x5cddc0e1, Next: 0}, ErrLocalIncompatibleOrStale}, + List list = Arrays.asList(forksMainnet); + Set forkSet = new LinkedHashSet<>(list); + ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + Boolean result = forkId.peerCheck("0x5cddc0e1", 0L); + assertThat(result).isFalse(); + } + + @Test + public void check12ByzantiumWithRemoteAnnouncingPetersburgAndFutureForkLocalNeedsUpdate() { + // 12 Local is mainnet Byzantium, and is aware of Petersburg. Remote announces Petersburg + + // 0xffffffff. Local needs software update, reject. + // {7279999, ID{Hash: 0x5cddc0e1, Next: 0}, ErrLocalIncompatibleOrStale}, + List list = Arrays.asList(forksMainnet); + Set forkSet = new LinkedHashSet<>(list); + ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); + Boolean result = forkId.peerCheck("0x5cddc0e1", 0L); + assertThat(result).isFalse(); + } + + @Test + public void check13ByzantiumWithRemoteAnnouncingRinkebyPetersburg() { + // 13 Local is mainnet Petersburg, remote is Rinkeby Petersburg. + // {7987396, ID{Hash: 0xafec6b27, Next: 0}, ErrLocalIncompatibleOrStale}, + List list = Arrays.asList(forksMainnet); + Set forkSet = new LinkedHashSet<>(list); + ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + Boolean result = forkId.peerCheck("0xafec6b27", 0L); + assertThat(result).isFalse(); + } + + @Test + public void createAndDecodeRLP() { + ForkId.ForkIdEntry forkIdEntry = ForkId.createIdEntry("0xa00bc324", 7280000L); + BytesValueRLPOutput out = new BytesValueRLPOutput(); + out.writeList(forkIdEntry.asList(), ForkId.ForkIdEntry::writeTo); + BytesValue bytesValue = out.encoded(); + BytesValueRLPInput in = new BytesValueRLPInput(bytesValue, false); + List forkId = in.readList(ForkId::readFrom); + ForkId.ForkIdEntry decodedEntry = forkId.get(0); + assertThat(forkIdEntry.equals(decodedEntry)).isTrue(); + } +} From 7e38ff58f997d405fc7421f1cf35b606d515eb0d Mon Sep 17 00:00:00 2001 From: Ratan Rai Sur Date: Mon, 4 Nov 2019 16:55:58 -0500 Subject: [PATCH 05/42] [PAN-2798] Enable pruning by default for fast sync (#135) Signed-off-by: Ratan Rai Sur Signed-off-by: SteveM --- .../org/hyperledger/besu/cli/BesuCommand.java | 13 ++++--- .../controller/BesuControllerBuilder.java | 4 +-- .../hyperledger/besu/cli/BesuCommandTest.java | 35 ++++++++++++++++++- .../src/test/resources/everything_config.toml | 3 ++ 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java index bdc4c9f61df..50eca10ca02 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java @@ -587,10 +587,9 @@ void setBannedNodeIds(final List values) { @Option( names = {"--pruning-enabled"}, - hidden = true, description = - "Enable pruning of world state of blocks older than the retention period (default: ${DEFAULT-VALUE})") - private final Boolean isPruningEnabled = false; + "Enable pruning of world state of blocks older than the retention period (default: true if fast sync is enabled, false otherwise)") + private Boolean pruningOverride; @Option( names = {"--pruning-blocks-retained"}, @@ -1012,7 +1011,7 @@ private void issueOptionWarnings() { logger, commandLine, "--pruning-enabled", - !isPruningEnabled, + !isPruningEnabled(), asList("--pruning-block-confirmations", "--pruning-blocks-retained")); } @@ -1088,7 +1087,7 @@ public BesuControllerBuilder getControllerBuilder() { .clock(Clock.systemUTC()) .isRevertReasonEnabled(isRevertReasonEnabled) .storageProvider(keyStorageProvider(keyValueStorageName)) - .isPruningEnabled(isPruningEnabled) + .isPruningEnabled(isPruningEnabled()) .pruningConfiguration(buildPruningConfiguration()) .genesisConfigOverrides(genesisConfigOverrides) .targetGasLimit(targetGasLimit == null ? Optional.empty() : Optional.of(targetGasLimit)) @@ -1375,6 +1374,10 @@ private PruningConfiguration buildPruningConfiguration() { return new PruningConfiguration(pruningBlockConfirmations, pruningBlocksRetained); } + private boolean isPruningEnabled() { + return Optional.ofNullable(pruningOverride).orElse(syncMode == SyncMode.FAST); + } + // Blockchain synchronisation from peers. private void synchronize( final BesuController controller, diff --git a/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java b/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java index 57ff466d8ff..5e5c3630b62 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java @@ -159,8 +159,8 @@ public BesuControllerBuilder isRevertReasonEnabled(final boolean isRevertReas return this; } - public BesuControllerBuilder isPruningEnabled(final boolean pruningEnabled) { - this.isPruningEnabled = pruningEnabled; + public BesuControllerBuilder isPruningEnabled(final boolean isPruningEnabled) { + this.isPruningEnabled = isPruningEnabled; return this; } diff --git a/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java b/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java index 5704da843e1..bbc0f75fbaf 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java @@ -2327,7 +2327,29 @@ public void miningParametersAreCaptured() throws Exception { } @Test - public void pruningIsEnabledWhenSpecified() throws Exception { + public void pruningIsEnabledIfSyncModeIsFast() { + parseCommand("--sync-mode", "FAST"); + + verify(mockControllerBuilder).isPruningEnabled(true); + verify(mockControllerBuilder).build(); + + assertThat(commandOutput.toString()).isEmpty(); + assertThat(commandErrorOutput.toString()).isEmpty(); + } + + @Test + public void pruningIsDisabledIfSyncModeIsFull() { + parseCommand("--sync-mode", "FULL"); + + verify(mockControllerBuilder).isPruningEnabled(false); + verify(mockControllerBuilder).build(); + + assertThat(commandOutput.toString()).isEmpty(); + assertThat(commandErrorOutput.toString()).isEmpty(); + } + + @Test + public void pruningEnabledExplicitly() { parseCommand("--pruning-enabled"); verify(mockControllerBuilder).isPruningEnabled(true); @@ -2337,6 +2359,17 @@ public void pruningIsEnabledWhenSpecified() throws Exception { assertThat(commandErrorOutput.toString()).isEmpty(); } + @Test + public void pruningDisabledExplicitly() { + parseCommand("--pruning-enabled=false"); + + verify(mockControllerBuilder).isPruningEnabled(false); + verify(mockControllerBuilder).build(); + + assertThat(commandOutput.toString()).isEmpty(); + assertThat(commandErrorOutput.toString()).isEmpty(); + } + @Test public void pruningOptionsRequiresServiceToBeEnabled() { diff --git a/besu/src/test/resources/everything_config.toml b/besu/src/test/resources/everything_config.toml index dfa3c032859..b480d4f1253 100644 --- a/besu/src/test/resources/everything_config.toml +++ b/besu/src/test/resources/everything_config.toml @@ -83,6 +83,9 @@ miner-coinbase="0x0000000000000000000000000000000000000002" miner-extra-data="0x444F4E27542050414E4943202120484F444C2C20484F444C2C20484F444C2021" min-gas-price=1 +# Pruning +pruning-enabled=true + # Permissioning permissions-nodes-config-file-enabled=false permissions-nodes-config-file="./permissions_config.toml" From 5349e183930b4c9d0bdda53c9fa1805a2fdf8a1a Mon Sep 17 00:00:00 2001 From: Joshua Fernandes Date: Tue, 5 Nov 2019 11:38:20 +1000 Subject: [PATCH 06/42] commenting out circles publish on the release branch so we're ready for the release tomorrow and dont have jenkins & circle attempting to publish (#157) Signed-off-by: Joshua Fernandes Signed-off-by: SteveM --- .circleci/config.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index d37a548f8a5..6552dc52c30 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -53,6 +53,7 @@ commands: done - store_test_results: path: build/test-results + jobs: assemble: executor: besu_executor_xl @@ -202,7 +203,7 @@ workflows: branches: only: - master - - /^release-.*/ + #- /^release-.*/ requires: - integrationTests - unitTests @@ -214,7 +215,7 @@ workflows: branches: only: - master - - /^release-.*/ + #- /^release-.*/ requires: - integrationTests - unitTests From 46329a2937e0a2912f37f18a34b2bf75aea765b5 Mon Sep 17 00:00:00 2001 From: SteveM Date: Mon, 4 Nov 2019 22:12:49 -0800 Subject: [PATCH 07/42] fix style to properly conform Signed-off-by: SteveM --- .../controller/BesuControllerBuilder.java | 14 ++++--- .../eth/manager/EthProtocolManager.java | 26 ++++++------ .../besu/ethereum/eth/manager/ForkId.java | 41 +++++++++++++++++-- .../besu/ethereum/eth/manager/ForkIdTest.java | 25 ++++++++++- 4 files changed, 81 insertions(+), 25 deletions(-) diff --git a/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java b/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java index 5e5c3630b62..d6fa7045841 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java @@ -375,14 +375,16 @@ protected abstract PluginServiceFactory createAdditionalPluginServices( final Blockchain blockchain); private List gatherForks() { - // todo: may need to check what protocol version is being used. (i.e. if less than x return null to disable this check) + // todo: may need to check what protocol version is being used. (i.e. if less than x return null + // to disable this check) List listb = new ArrayList<>(); Map values = genesisConfig.getConfigOptions(genesisConfigOverrides).asMap(); - values.forEach((x, y) -> { - if (y instanceof Long && !x.equals("chainId")) { - listb.add((Long) y); - } - }); + values.forEach( + (x, y) -> { + if (y instanceof Long && !x.equals("chainId")) { + listb.add((Long) y); + } + }); return listb; } } diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java index fd2973c3411..dff5c2d6fc2 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java @@ -157,18 +157,18 @@ public EthProtocolManager( } public EthProtocolManager( - final Blockchain blockchain, - final WorldStateArchive worldStateArchive, - final BigInteger networkId, - final List peerValidators, - final boolean fastSyncEnabled, - final int syncWorkers, - final int txWorkers, - final int computationWorkers, - final Clock clock, - final MetricsSystem metricsSystem, - final EthProtocolConfiguration ethereumWireProtocolConfiguration, - final List forks) { + final Blockchain blockchain, + final WorldStateArchive worldStateArchive, + final BigInteger networkId, + final List peerValidators, + final boolean fastSyncEnabled, + final int syncWorkers, + final int txWorkers, + final int computationWorkers, + final Clock clock, + final MetricsSystem metricsSystem, + final EthProtocolConfiguration ethereumWireProtocolConfiguration, + final List forks) { this.networkId = networkId; this.peerValidators = peerValidators; this.scheduler = new EthScheduler(syncWorkers, txWorkers, computationWorkers, metricsSystem); @@ -290,7 +290,7 @@ public void handleNewConnection(final PeerConnection connection) { networkId, blockchain.getChainHead().getTotalDifficulty(), blockchain.getChainHeadHash(), - genesisHash); + forkId.getLatestForkId()); try { LOG.debug("Sending status message to {}.", peer); peer.send(status); diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkId.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkId.java index 20efa730ee3..573421701d0 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkId.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkId.java @@ -1,5 +1,21 @@ +/* + * 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.eth.manager; +import static org.hyperledger.besu.util.bytes.BytesValue.wrap; + import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.core.Hash; import org.hyperledger.besu.ethereum.rlp.RLPInput; @@ -16,8 +32,6 @@ import java.util.Set; import java.util.zip.CRC32; -import static org.hyperledger.besu.util.bytes.BytesValue.wrap; - public class ForkId { private Hash genesisHash; @@ -25,12 +39,20 @@ public class ForkId { private Long currentHead; private Long forkNext; private Long highestKnownFork = 0L; + private ForkIdEntry lastKnownEntry; + private boolean useForkId; private ArrayDeque forkAndHashList; public ForkId(final Hash genesisHash, final Set forks, final Long currentHead) { this.genesisHash = genesisHash; this.currentHead = currentHead; - forkAndHashList = collectForksAndHashes(forks, currentHead); + if(forks != null){ + useForkId = true; + forkAndHashList = collectForksAndHashes(forks, currentHead); + } else { + useForkId = false; + forkAndHashList = new ArrayDeque<>(); + } }; public static ForkId buildCollection( @@ -73,6 +95,15 @@ public ArrayDeque getForkAndHashList() { return this.forkAndHashList; } + public Hash getLatestForkId(){ + System.out.println("USING FORK ID: " + useForkId); // todo remove dev item + if(useForkId){ + return Hash.fromHexString(lastKnownEntry.hash); + } else { + return genesisHash; + } + } + public boolean peerCheck(final String forkHash, final Long peerNext) { // Run the fork checksum validation ruleset: // 1. If local and remote FORK_CSUM matches, connect. @@ -115,6 +146,7 @@ public boolean peerCheck(final Bytes32 peerGenesisOrCheckSumHash) { return !peerGenesisOrCheckSumHash.equals(genesisHash); } + private boolean isHashKnown(final String forkHash) { for (ForkIdEntry j : forkAndHashList) { if (forkHash.equals(j.hash)) { @@ -173,7 +205,8 @@ private ArrayDeque collectForksAndHashes( // most recent fork forkList.add(new ForkIdEntry(getCurrentCrcHash(), forkBlockNumber)); updateCrc(forkBlockNumber); - forkList.add(new ForkIdEntry(getCurrentCrcHash(), 0L)); + lastKnownEntry = new ForkIdEntry(getCurrentCrcHash(), 0L); + forkList.add(lastKnownEntry); if (currentHead > forkBlockNumber) { this.forkNext = 0L; } else { diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdTest.java index 8983ee97d07..206f6e79cbe 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdTest.java @@ -1,10 +1,25 @@ +/* + * 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.eth.manager; +import static org.assertj.core.api.Assertions.assertThat; + import org.hyperledger.besu.ethereum.core.Hash; import org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput; import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput; import org.hyperledger.besu.util.bytes.BytesValue; -import org.junit.Test; import java.util.ArrayDeque; import java.util.Arrays; @@ -12,13 +27,19 @@ import java.util.List; import java.util.Set; -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.Test; public class ForkIdTest { private Long[] forksMainnet = {1150000L, 1920000L, 2463000L, 2675000L, 4370000L, 7280000L}; private String mainnetGenHash = "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"; + @Test + public void checkItFunctionsWithPresentBehavior() { + ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), null, null); + assertThat(forkId.peerCheck(Hash.fromHexString(mainnetGenHash))).isFalse(); + } + @Test public void checkCorrectMainnetForkIdHashesGenerated() { ForkId.ForkIdEntry[] checkIds = { From 32ca6626be1a20693dca2946a5ac1486a7491d1c Mon Sep 17 00:00:00 2001 From: CJ Hare Date: Tue, 5 Nov 2019 16:27:24 +1000 Subject: [PATCH 08/42] Redesign of how JsonRpcMethods are created (#159) Signed-off-by: Christopher Hare Signed-off-by: Lucas Saldanha Signed-off-by: SteveM --- .../org/hyperledger/besu/RunnerBuilder.java | 2 +- .../besu/controller/BesuController.java | 8 +- .../controller/BesuControllerBuilder.java | 6 +- .../CliqueBesuControllerBuilder.java | 8 +- .../controller/IbftBesuControllerBuilder.java | 8 +- ...Factory.java => CliqueJsonRpcMethods.java} | 46 +- ...dsFactory.java => IbftJsonRpcMethods.java} | 50 +-- .../jsonrpc/JsonRpcTestMethodsFactory.java | 2 +- .../EthCallIntegrationTest.java | 2 +- .../EthEstimateGasIntegrationTest.java | 2 +- .../EthGetBlockByHashIntegrationTest.java | 2 +- .../EthGetBlockByNumberIntegrationTest.java | 2 +- .../EthGetFilterChangesIntegrationTest.java | 2 +- ...cleByBlockHashAndIndexIntegrationTest.java | 2 +- ...eByBlockNumberAndIndexIntegrationTest.java | 2 +- ...vGetPrivateTransactionIntegrationTest.java | 2 +- .../jsonrpc/method/JsonRpcMethodsFactory.java | 420 ------------------ .../jsonrpc/methods/AdminJsonRpcMethods.java | 70 +++ .../methods/ApiGroupJsonRpcMethods.java | 41 ++ .../jsonrpc/methods/DebugJsonRpcMethods.java | 80 ++++ .../jsonrpc/methods/EeaJsonRpcMethods.java | 58 +++ .../jsonrpc/methods/EthJsonRpcMethods.java | 163 +++++++ .../JsonRpcMethods.java} | 6 +- .../methods/JsonRpcMethodsFactory.java | 157 +++++++ .../jsonrpc/methods/MinerJsonRpcMethods.java | 52 +++ .../jsonrpc/methods/NetJsonRpcMethods.java | 69 +++ .../jsonrpc/methods/PermJsonRpcMethods.java | 63 +++ .../jsonrpc/methods/PrivJsonRpcMethods.java | 73 +++ .../PrivacyApiGroupJsonRpcMethods.java | 101 +++++ .../jsonrpc/methods/TraceJsonRpcMethods.java | 60 +++ .../jsonrpc/methods/TxPoolJsonRpcMethods.java | 45 ++ .../jsonrpc/methods/Web3JsonRpcMethods.java | 42 ++ .../AbstractJsonRpcHttpServiceTest.java | 2 +- .../JsonRpcHttpServiceHostWhitelistTest.java | 2 +- .../jsonrpc/JsonRpcHttpServiceLoginTest.java | 2 +- .../JsonRpcHttpServiceRpcApisTest.java | 2 +- .../api/jsonrpc/JsonRpcHttpServiceTest.java | 2 +- .../ethereum/retesteth/RetestethService.java | 62 +-- 38 files changed, 1177 insertions(+), 541 deletions(-) rename consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/{CliqueJsonRpcMethodsFactory.java => CliqueJsonRpcMethods.java} (68%) rename consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/{IbftJsonRpcMethodsFactory.java => IbftJsonRpcMethods.java} (55%) rename ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/{method => methods}/EthCallIntegrationTest.java (99%) rename ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/{method => methods}/EthEstimateGasIntegrationTest.java (99%) rename ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/{method => methods}/EthGetBlockByHashIntegrationTest.java (99%) rename ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/{method => methods}/EthGetBlockByNumberIntegrationTest.java (99%) rename ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/{method => methods}/EthGetFilterChangesIntegrationTest.java (99%) rename ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/{method => methods}/EthGetUncleByBlockHashAndIndexIntegrationTest.java (98%) rename ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/{method => methods}/EthGetUncleByBlockNumberAndIndexIntegrationTest.java (98%) rename ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/{method => methods}/PrivGetPrivateTransactionIntegrationTest.java (99%) delete mode 100644 ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/JsonRpcMethodsFactory.java create mode 100644 ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/AdminJsonRpcMethods.java create mode 100644 ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/ApiGroupJsonRpcMethods.java create mode 100644 ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/DebugJsonRpcMethods.java create mode 100644 ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EeaJsonRpcMethods.java create mode 100644 ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthJsonRpcMethods.java rename ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/{method/JsonRpcMethodFactory.java => methods/JsonRpcMethods.java} (81%) create mode 100644 ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/JsonRpcMethodsFactory.java create mode 100644 ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/MinerJsonRpcMethods.java create mode 100644 ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/NetJsonRpcMethods.java create mode 100644 ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PermJsonRpcMethods.java create mode 100644 ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivJsonRpcMethods.java create mode 100644 ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivacyApiGroupJsonRpcMethods.java create mode 100644 ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/TraceJsonRpcMethods.java create mode 100644 ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/TxPoolJsonRpcMethods.java create mode 100644 ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/Web3JsonRpcMethods.java diff --git a/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java b/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java index 0dc79f66843..6251f1cfbf8 100644 --- a/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java @@ -36,7 +36,7 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterManager; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterRepository; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; -import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodsFactory; +import org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethodsFactory; import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration; import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketRequestHandler; import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketService; diff --git a/besu/src/main/java/org/hyperledger/besu/controller/BesuController.java b/besu/src/main/java/org/hyperledger/besu/controller/BesuController.java index ef6c31918f4..452302ae48a 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/BesuController.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/BesuController.java @@ -21,7 +21,7 @@ import org.hyperledger.besu.ethereum.ProtocolContext; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; -import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodFactory; +import org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethods; import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator; import org.hyperledger.besu.ethereum.core.PrivacyParameters; import org.hyperledger.besu.ethereum.core.Synchronizer; @@ -52,7 +52,7 @@ public class BesuController implements java.io.Closeable { private final SubProtocolConfiguration subProtocolConfiguration; private final KeyPair keyPair; private final Synchronizer synchronizer; - private final JsonRpcMethodFactory additionalJsonRpcMethodsFactory; + private final JsonRpcMethods additionalJsonRpcMethodsFactory; private final TransactionPool transactionPool; private final MiningCoordinator miningCoordinator; @@ -72,7 +72,7 @@ public class BesuController implements java.io.Closeable { final TransactionPool transactionPool, final MiningCoordinator miningCoordinator, final PrivacyParameters privacyParameters, - final JsonRpcMethodFactory additionalJsonRpcMethodsFactory, + final JsonRpcMethods additionalJsonRpcMethodsFactory, final KeyPair keyPair, final List closeables, final PluginServiceFactory additionalPluginServices) { @@ -147,7 +147,7 @@ public PrivacyParameters getPrivacyParameters() { public Map getAdditionalJsonRpcMethods( final Collection enabledRpcApis) { - return additionalJsonRpcMethodsFactory.createJsonRpcMethods(enabledRpcApis); + return additionalJsonRpcMethodsFactory.create(enabledRpcApis); } public SyncState getSyncState() { diff --git a/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java b/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java index d6fa7045841..7e36484cb05 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java @@ -21,7 +21,7 @@ import org.hyperledger.besu.config.GenesisConfigFile; import org.hyperledger.besu.crypto.SECP256K1.KeyPair; import org.hyperledger.besu.ethereum.ProtocolContext; -import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodFactory; +import org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethods; import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator; import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.chain.GenesisState; @@ -279,7 +279,7 @@ public BesuController build() { final SubProtocolConfiguration subProtocolConfiguration = createSubProtocolConfiguration(ethProtocolManager); - final JsonRpcMethodFactory additionalJsonRpcMethodFactory = + final JsonRpcMethods additionalJsonRpcMethodFactory = createAdditionalJsonRpcMethodFactory(protocolContext); List closeables = new ArrayList<>(); @@ -307,7 +307,7 @@ public BesuController build() { protected void prepForBuild() {} - protected JsonRpcMethodFactory createAdditionalJsonRpcMethodFactory( + protected JsonRpcMethods createAdditionalJsonRpcMethodFactory( final ProtocolContext protocolContext) { return apis -> Collections.emptyMap(); } diff --git a/besu/src/main/java/org/hyperledger/besu/controller/CliqueBesuControllerBuilder.java b/besu/src/main/java/org/hyperledger/besu/controller/CliqueBesuControllerBuilder.java index e64ac26e3ed..ee70e2bc015 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/CliqueBesuControllerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/CliqueBesuControllerBuilder.java @@ -22,14 +22,14 @@ import org.hyperledger.besu.consensus.clique.blockcreation.CliqueBlockScheduler; import org.hyperledger.besu.consensus.clique.blockcreation.CliqueMinerExecutor; import org.hyperledger.besu.consensus.clique.blockcreation.CliqueMiningCoordinator; -import org.hyperledger.besu.consensus.clique.jsonrpc.CliqueJsonRpcMethodsFactory; +import org.hyperledger.besu.consensus.clique.jsonrpc.CliqueJsonRpcMethods; import org.hyperledger.besu.consensus.common.BlockInterface; import org.hyperledger.besu.consensus.common.EpochManager; import org.hyperledger.besu.consensus.common.VoteProposer; import org.hyperledger.besu.consensus.common.VoteTallyCache; import org.hyperledger.besu.consensus.common.VoteTallyUpdater; import org.hyperledger.besu.ethereum.ProtocolContext; -import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodFactory; +import org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethods; import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator; import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.core.Address; @@ -65,9 +65,9 @@ protected void prepForBuild() { } @Override - protected JsonRpcMethodFactory createAdditionalJsonRpcMethodFactory( + protected JsonRpcMethods createAdditionalJsonRpcMethodFactory( final ProtocolContext protocolContext) { - return new CliqueJsonRpcMethodsFactory(protocolContext); + return new CliqueJsonRpcMethods(protocolContext); } @Override diff --git a/besu/src/main/java/org/hyperledger/besu/controller/IbftBesuControllerBuilder.java b/besu/src/main/java/org/hyperledger/besu/controller/IbftBesuControllerBuilder.java index e9a84123409..69a02796758 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/IbftBesuControllerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/IbftBesuControllerBuilder.java @@ -36,7 +36,7 @@ import org.hyperledger.besu.consensus.ibft.blockcreation.IbftBlockCreatorFactory; import org.hyperledger.besu.consensus.ibft.blockcreation.IbftMiningCoordinator; import org.hyperledger.besu.consensus.ibft.blockcreation.ProposerSelector; -import org.hyperledger.besu.consensus.ibft.jsonrpc.IbftJsonRpcMethodsFactory; +import org.hyperledger.besu.consensus.ibft.jsonrpc.IbftJsonRpcMethods; import org.hyperledger.besu.consensus.ibft.network.ValidatorPeers; import org.hyperledger.besu.consensus.ibft.payload.MessageFactory; import org.hyperledger.besu.consensus.ibft.protocol.IbftProtocolManager; @@ -48,7 +48,7 @@ import org.hyperledger.besu.consensus.ibft.statemachine.IbftRoundFactory; import org.hyperledger.besu.consensus.ibft.validation.MessageValidatorFactory; import org.hyperledger.besu.ethereum.ProtocolContext; -import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodFactory; +import org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethods; import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator; import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.chain.MinedBlockObserver; @@ -82,9 +82,9 @@ protected void prepForBuild() { } @Override - protected JsonRpcMethodFactory createAdditionalJsonRpcMethodFactory( + protected JsonRpcMethods createAdditionalJsonRpcMethodFactory( final ProtocolContext protocolContext) { - return new IbftJsonRpcMethodsFactory(protocolContext); + return new IbftJsonRpcMethods(protocolContext); } @Override diff --git a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/CliqueJsonRpcMethodsFactory.java b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/CliqueJsonRpcMethods.java similarity index 68% rename from consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/CliqueJsonRpcMethodsFactory.java rename to consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/CliqueJsonRpcMethods.java index b0217992ab6..860af0e9369 100644 --- a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/CliqueJsonRpcMethodsFactory.java +++ b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/jsonrpc/CliqueJsonRpcMethods.java @@ -30,55 +30,45 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonRpcParameter; -import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodFactory; +import org.hyperledger.besu.ethereum.api.jsonrpc.methods.ApiGroupJsonRpcMethods; import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; import org.hyperledger.besu.ethereum.chain.MutableBlockchain; import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive; -import java.util.Collection; -import java.util.HashMap; import java.util.Map; -public class CliqueJsonRpcMethodsFactory implements JsonRpcMethodFactory { +public class CliqueJsonRpcMethods extends ApiGroupJsonRpcMethods { + private final JsonRpcParameter parameter = new JsonRpcParameter(); private final ProtocolContext context; - public CliqueJsonRpcMethodsFactory(final ProtocolContext context) { + public CliqueJsonRpcMethods(final ProtocolContext context) { this.context = context; } @Override - public Map createJsonRpcMethods(final Collection jsonRpcApis) { - final Map rpcMethods = new HashMap<>(); - if (!jsonRpcApis.contains(CliqueRpcApis.CLIQUE)) { - return rpcMethods; - } + protected RpcApi getApiGroup() { + return CliqueRpcApis.CLIQUE; + } + + @Override + protected Map create() { final MutableBlockchain blockchain = context.getBlockchain(); final WorldStateArchive worldStateArchive = context.getWorldStateArchive(); final BlockchainQueries blockchainQueries = new BlockchainQueries(blockchain, worldStateArchive); final VoteProposer voteProposer = context.getConsensusState().getVoteProposer(); - final JsonRpcParameter jsonRpcParameter = new JsonRpcParameter(); + // Must create our own voteTallyCache as using this would pollute the main voteTallyCache final VoteTallyCache voteTallyCache = createVoteTallyCache(context, blockchain); - final CliqueGetSigners cliqueGetSigners = - new CliqueGetSigners(blockchainQueries, voteTallyCache, jsonRpcParameter); - final CliqueGetSignersAtHash cliqueGetSignersAtHash = - new CliqueGetSignersAtHash(blockchainQueries, voteTallyCache, jsonRpcParameter); - final Propose proposeRpc = new Propose(voteProposer, jsonRpcParameter); - final Discard discardRpc = new Discard(voteProposer, jsonRpcParameter); - final CliqueProposals cliqueProposals = new CliqueProposals(voteProposer); - final CliqueGetSignerMetrics cliqueGetSignerMetrics = - new CliqueGetSignerMetrics(new CliqueBlockInterface(), blockchainQueries, jsonRpcParameter); - - rpcMethods.put(cliqueGetSigners.getName(), cliqueGetSigners); - rpcMethods.put(cliqueGetSignersAtHash.getName(), cliqueGetSignersAtHash); - rpcMethods.put(proposeRpc.getName(), proposeRpc); - rpcMethods.put(discardRpc.getName(), discardRpc); - rpcMethods.put(cliqueProposals.getName(), cliqueProposals); - rpcMethods.put(cliqueGetSignerMetrics.getName(), cliqueGetSignerMetrics); - return rpcMethods; + return mapOf( + new CliqueGetSigners(blockchainQueries, voteTallyCache, parameter), + new CliqueGetSignersAtHash(blockchainQueries, voteTallyCache, parameter), + new Propose(voteProposer, parameter), + new Discard(voteProposer, parameter), + new CliqueProposals(voteProposer), + new CliqueGetSignerMetrics(new CliqueBlockInterface(), blockchainQueries, parameter)); } private VoteTallyCache createVoteTallyCache( diff --git a/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/IbftJsonRpcMethodsFactory.java b/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/IbftJsonRpcMethods.java similarity index 55% rename from consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/IbftJsonRpcMethodsFactory.java rename to consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/IbftJsonRpcMethods.java index dcafe3c34de..a0480f7b577 100644 --- a/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/IbftJsonRpcMethodsFactory.java +++ b/consensus/ibft/src/main/java/org/hyperledger/besu/consensus/ibft/jsonrpc/IbftJsonRpcMethods.java @@ -28,50 +28,38 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonRpcParameter; -import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodFactory; +import org.hyperledger.besu.ethereum.api.jsonrpc.methods.ApiGroupJsonRpcMethods; import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; -import java.util.Collection; -import java.util.HashMap; import java.util.Map; -public class IbftJsonRpcMethodsFactory implements JsonRpcMethodFactory { +public class IbftJsonRpcMethods extends ApiGroupJsonRpcMethods { private final JsonRpcParameter jsonRpcParameter = new JsonRpcParameter(); private final ProtocolContext context; - public IbftJsonRpcMethodsFactory(final ProtocolContext context) { + public IbftJsonRpcMethods(final ProtocolContext context) { this.context = context; } @Override - public Map createJsonRpcMethods(final Collection jsonRpcApis) { - final Map rpcMethods = new HashMap<>(); - - if (jsonRpcApis.contains(IbftRpcApis.IBFT)) { - final BlockchainQueries blockchainQueries = - new BlockchainQueries(context.getBlockchain(), context.getWorldStateArchive()); - final VoteProposer voteProposer = context.getConsensusState().getVoteProposer(); - final BlockInterface blockInterface = new IbftBlockInterface(); - - addMethods( - rpcMethods, - new IbftProposeValidatorVote(voteProposer, jsonRpcParameter), - new IbftGetValidatorsByBlockNumber(blockchainQueries, blockInterface, jsonRpcParameter), - new IbftDiscardValidatorVote(voteProposer, jsonRpcParameter), - new IbftGetValidatorsByBlockHash( - context.getBlockchain(), blockInterface, jsonRpcParameter), - new IbftGetSignerMetrics(blockInterface, blockchainQueries, jsonRpcParameter), - new IbftGetPendingVotes(voteProposer)); - } - - return rpcMethods; + protected RpcApi getApiGroup() { + return IbftRpcApis.IBFT; } - private void addMethods( - final Map methods, final JsonRpcMethod... rpcMethods) { - for (final JsonRpcMethod rpcMethod : rpcMethods) { - methods.put(rpcMethod.getName(), rpcMethod); - } + @Override + protected Map create() { + final BlockchainQueries blockchainQueries = + new BlockchainQueries(context.getBlockchain(), context.getWorldStateArchive()); + final VoteProposer voteProposer = context.getConsensusState().getVoteProposer(); + final BlockInterface blockInterface = new IbftBlockInterface(); + + return mapOf( + new IbftProposeValidatorVote(voteProposer, jsonRpcParameter), + new IbftGetValidatorsByBlockNumber(blockchainQueries, blockInterface, jsonRpcParameter), + new IbftDiscardValidatorVote(voteProposer, jsonRpcParameter), + new IbftGetValidatorsByBlockHash(context.getBlockchain(), blockInterface, jsonRpcParameter), + new IbftGetSignerMetrics(blockInterface, blockchainQueries, jsonRpcParameter), + new IbftGetPendingVotes(voteProposer)); } } diff --git a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcTestMethodsFactory.java b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcTestMethodsFactory.java index fd827adec73..a9f8c40a6b0 100644 --- a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcTestMethodsFactory.java +++ b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcTestMethodsFactory.java @@ -24,7 +24,7 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterManager; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterRepository; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; -import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodsFactory; +import org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethodsFactory; import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration; import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; import org.hyperledger.besu.ethereum.blockcreation.EthHashMiningCoordinator; diff --git a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthCallIntegrationTest.java b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthCallIntegrationTest.java similarity index 99% rename from ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthCallIntegrationTest.java rename to ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthCallIntegrationTest.java index 33d7a4240cc..d8251a980cc 100644 --- a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthCallIntegrationTest.java +++ b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthCallIntegrationTest.java @@ -12,7 +12,7 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.ethereum.api.jsonrpc.method; +package org.hyperledger.besu.ethereum.api.jsonrpc.methods; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.catchThrowable; diff --git a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthEstimateGasIntegrationTest.java b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthEstimateGasIntegrationTest.java similarity index 99% rename from ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthEstimateGasIntegrationTest.java rename to ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthEstimateGasIntegrationTest.java index 37d838fefcb..e721b236f9f 100644 --- a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthEstimateGasIntegrationTest.java +++ b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthEstimateGasIntegrationTest.java @@ -12,7 +12,7 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.ethereum.api.jsonrpc.method; +package org.hyperledger.besu.ethereum.api.jsonrpc.methods; import static org.assertj.core.api.Assertions.assertThat; diff --git a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetBlockByHashIntegrationTest.java b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetBlockByHashIntegrationTest.java similarity index 99% rename from ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetBlockByHashIntegrationTest.java rename to ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetBlockByHashIntegrationTest.java index 6eae107548c..c827fc59cb6 100644 --- a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetBlockByHashIntegrationTest.java +++ b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetBlockByHashIntegrationTest.java @@ -12,7 +12,7 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.ethereum.api.jsonrpc.method; +package org.hyperledger.besu.ethereum.api.jsonrpc.methods; import static org.assertj.core.api.Assertions.assertThat; diff --git a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetBlockByNumberIntegrationTest.java b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetBlockByNumberIntegrationTest.java similarity index 99% rename from ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetBlockByNumberIntegrationTest.java rename to ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetBlockByNumberIntegrationTest.java index 7c83af67a85..e6440ad102d 100644 --- a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetBlockByNumberIntegrationTest.java +++ b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetBlockByNumberIntegrationTest.java @@ -12,7 +12,7 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.ethereum.api.jsonrpc.method; +package org.hyperledger.besu.ethereum.api.jsonrpc.methods; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.catchThrowable; diff --git a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetFilterChangesIntegrationTest.java b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetFilterChangesIntegrationTest.java similarity index 99% rename from ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetFilterChangesIntegrationTest.java rename to ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetFilterChangesIntegrationTest.java index 3d882d02463..ee85ca3781b 100644 --- a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetFilterChangesIntegrationTest.java +++ b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetFilterChangesIntegrationTest.java @@ -12,7 +12,7 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.ethereum.api.jsonrpc.method; +package org.hyperledger.besu.ethereum.api.jsonrpc.methods; import static java.util.Arrays.asList; import static java.util.Collections.emptyList; diff --git a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetUncleByBlockHashAndIndexIntegrationTest.java b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetUncleByBlockHashAndIndexIntegrationTest.java similarity index 98% rename from ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetUncleByBlockHashAndIndexIntegrationTest.java rename to ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetUncleByBlockHashAndIndexIntegrationTest.java index d78d5741e12..810fe93428c 100644 --- a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetUncleByBlockHashAndIndexIntegrationTest.java +++ b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetUncleByBlockHashAndIndexIntegrationTest.java @@ -12,7 +12,7 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.ethereum.api.jsonrpc.method; +package org.hyperledger.besu.ethereum.api.jsonrpc.methods; import static org.assertj.core.api.Assertions.assertThat; diff --git a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetUncleByBlockNumberAndIndexIntegrationTest.java b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetUncleByBlockNumberAndIndexIntegrationTest.java similarity index 98% rename from ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetUncleByBlockNumberAndIndexIntegrationTest.java rename to ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetUncleByBlockNumberAndIndexIntegrationTest.java index b8a361938de..fa49bf7f9c2 100644 --- a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/EthGetUncleByBlockNumberAndIndexIntegrationTest.java +++ b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetUncleByBlockNumberAndIndexIntegrationTest.java @@ -12,7 +12,7 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.ethereum.api.jsonrpc.method; +package org.hyperledger.besu.ethereum.api.jsonrpc.methods; import static org.assertj.core.api.Assertions.assertThat; diff --git a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/PrivGetPrivateTransactionIntegrationTest.java b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivGetPrivateTransactionIntegrationTest.java similarity index 99% rename from ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/PrivGetPrivateTransactionIntegrationTest.java rename to ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivGetPrivateTransactionIntegrationTest.java index a90a5d32470..ad44bfbe5fc 100644 --- a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/PrivGetPrivateTransactionIntegrationTest.java +++ b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivGetPrivateTransactionIntegrationTest.java @@ -12,7 +12,7 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.ethereum.api.jsonrpc.method; +package org.hyperledger.besu.ethereum.api.jsonrpc.methods; import static java.nio.charset.StandardCharsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/JsonRpcMethodsFactory.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/JsonRpcMethodsFactory.java deleted file mode 100644 index b5a39b3c08a..00000000000 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/JsonRpcMethodsFactory.java +++ /dev/null @@ -1,420 +0,0 @@ -/* - * 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.api.jsonrpc.method; - -import org.hyperledger.besu.config.GenesisConfigOptions; -import org.hyperledger.besu.enclave.Enclave; -import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcConfiguration; -import org.hyperledger.besu.ethereum.api.jsonrpc.LatestNonceProvider; -import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi; -import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApis; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterManager; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.AdminAddPeer; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.AdminChangeLogLevel; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.AdminNodeInfo; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.AdminPeers; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.AdminRemovePeer; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.DebugAccountRange; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.DebugMetrics; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.DebugStorageRangeAt; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.DebugTraceBlock; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.DebugTraceBlockByHash; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.DebugTraceBlockByNumber; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.DebugTraceTransaction; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthAccounts; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthBlockNumber; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthCall; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthChainId; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthCoinbase; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthEstimateGas; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGasPrice; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetBalance; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetBlockByHash; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetBlockByNumber; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetBlockTransactionCountByHash; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetBlockTransactionCountByNumber; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetCode; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetFilterChanges; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetFilterLogs; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetLogs; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetProof; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetStorageAt; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetTransactionByBlockHashAndIndex; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetTransactionByBlockNumberAndIndex; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetTransactionByHash; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetTransactionCount; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetTransactionReceipt; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetUncleByBlockHashAndIndex; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetUncleByBlockNumberAndIndex; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetUncleCountByBlockHash; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetUncleCountByBlockNumber; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetWork; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthHashrate; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthMining; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthNewBlockFilter; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthNewFilter; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthNewPendingTransactionFilter; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthProtocolVersion; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthSendRawTransaction; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthSendTransaction; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthSyncing; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthUninstallFilter; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.NetEnode; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.NetListening; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.NetPeerCount; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.NetServices; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.NetVersion; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.RpcModules; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.TxPoolBesuStatistics; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.TxPoolBesuTransactions; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.Web3ClientVersion; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.Web3Sha3; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.miner.MinerSetCoinbase; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.miner.MinerSetEtherbase; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.miner.MinerStart; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.miner.MinerStop; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.permissioning.PermAddAccountsToWhitelist; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.permissioning.PermAddNodesToWhitelist; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.permissioning.PermGetAccountsWhitelist; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.permissioning.PermGetNodesWhitelist; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.permissioning.PermReloadPermissionsFromFile; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.permissioning.PermRemoveAccountsFromWhitelist; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.permissioning.PermRemoveNodesFromWhitelist; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonRpcParameter; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.eea.EeaSendRawTransaction; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivCreatePrivacyGroup; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivDeletePrivacyGroup; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivDistributeRawTransaction; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivFindPrivacyGroup; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivGetEeaTransactionCount; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivGetPrivacyPrecompileAddress; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivGetPrivateTransaction; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivGetTransactionCount; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivGetTransactionReceipt; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivateEeaNonceProvider; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockReplay; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockTracer; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTracer; -import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResultFactory; -import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration; -import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; -import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator; -import org.hyperledger.besu.ethereum.chain.Blockchain; -import org.hyperledger.besu.ethereum.core.Address; -import org.hyperledger.besu.ethereum.core.PrivacyParameters; -import org.hyperledger.besu.ethereum.core.Synchronizer; -import org.hyperledger.besu.ethereum.eth.transactions.PendingTransactions; -import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool; -import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; -import org.hyperledger.besu.ethereum.mainnet.ScheduleBasedBlockHeaderFunctions; -import org.hyperledger.besu.ethereum.p2p.network.P2PNetwork; -import org.hyperledger.besu.ethereum.p2p.rlpx.wire.Capability; -import org.hyperledger.besu.ethereum.permissioning.AccountLocalConfigPermissioningController; -import org.hyperledger.besu.ethereum.permissioning.NodeLocalConfigPermissioningController; -import org.hyperledger.besu.ethereum.privacy.PrivateTransactionHandler; -import org.hyperledger.besu.ethereum.privacy.markertransaction.FixedKeySigningPrivateMarkerTransactionFactory; -import org.hyperledger.besu.ethereum.privacy.markertransaction.PrivateMarkerTransactionFactory; -import org.hyperledger.besu.ethereum.privacy.markertransaction.RandomSigningPrivateMarkerTransactionFactory; -import org.hyperledger.besu.ethereum.transaction.TransactionSimulator; -import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive; -import org.hyperledger.besu.metrics.ObservableMetricsSystem; -import org.hyperledger.besu.metrics.prometheus.MetricsConfiguration; - -import java.math.BigInteger; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; -import java.util.Set; - -public class JsonRpcMethodsFactory { - - private final BlockResultFactory blockResult = new BlockResultFactory(); - private final JsonRpcParameter parameter = new JsonRpcParameter(); - - public Map methods( - final String clientVersion, - final BigInteger networkId, - final GenesisConfigOptions genesisConfigOptions, - final P2PNetwork peerNetworkingService, - final Blockchain blockchain, - final WorldStateArchive worldStateArchive, - final Synchronizer synchronizer, - final TransactionPool transactionPool, - final ProtocolSchedule protocolSchedule, - final MiningCoordinator miningCoordinator, - final ObservableMetricsSystem metricsSystem, - final Set supportedCapabilities, - final Collection rpcApis, - final FilterManager filterManager, - final Optional accountsWhitelistController, - final Optional nodeWhitelistController, - final PrivacyParameters privacyParameters, - final JsonRpcConfiguration jsonRpcConfiguration, - final WebSocketConfiguration webSocketConfiguration, - final MetricsConfiguration metricsConfiguration) { - final BlockchainQueries blockchainQueries = - new BlockchainQueries(blockchain, worldStateArchive); - return methods( - clientVersion, - networkId, - genesisConfigOptions, - peerNetworkingService, - blockchainQueries, - synchronizer, - protocolSchedule, - filterManager, - transactionPool, - miningCoordinator, - metricsSystem, - supportedCapabilities, - accountsWhitelistController, - nodeWhitelistController, - rpcApis, - privacyParameters, - jsonRpcConfiguration, - webSocketConfiguration, - metricsConfiguration); - } - - public Map methods( - final String clientVersion, - final BigInteger networkId, - final GenesisConfigOptions genesisConfigOptions, - final P2PNetwork p2pNetwork, - final BlockchainQueries blockchainQueries, - final Synchronizer synchronizer, - final ProtocolSchedule protocolSchedule, - final FilterManager filterManager, - final TransactionPool transactionPool, - final MiningCoordinator miningCoordinator, - final ObservableMetricsSystem metricsSystem, - final Set supportedCapabilities, - final Optional accountsWhitelistController, - final Optional nodeWhitelistController, - final Collection rpcApis, - final PrivacyParameters privacyParameters, - final JsonRpcConfiguration jsonRpcConfiguration, - final WebSocketConfiguration webSocketConfiguration, - final MetricsConfiguration metricsConfiguration) { - final Map enabledMethods = new HashMap<>(); - if (!rpcApis.isEmpty()) { - addMethods(enabledMethods, new RpcModules(rpcApis)); - } - if (rpcApis.contains(RpcApis.ETH)) { - addMethods( - enabledMethods, - new EthAccounts(), - new EthBlockNumber(blockchainQueries), - new EthGetBalance(blockchainQueries, parameter), - new EthGetBlockByHash(blockchainQueries, blockResult, parameter), - new EthGetBlockByNumber(blockchainQueries, blockResult, parameter), - new EthGetBlockTransactionCountByNumber(blockchainQueries, parameter), - new EthGetBlockTransactionCountByHash(blockchainQueries, parameter), - new EthCall( - blockchainQueries, - new TransactionSimulator( - blockchainQueries.getBlockchain(), - blockchainQueries.getWorldStateArchive(), - protocolSchedule), - parameter), - new EthGetCode(blockchainQueries, parameter), - new EthGetLogs(blockchainQueries, parameter), - new EthGetProof(blockchainQueries, parameter), - new EthGetUncleCountByBlockHash(blockchainQueries, parameter), - new EthGetUncleCountByBlockNumber(blockchainQueries, parameter), - new EthGetUncleByBlockNumberAndIndex(blockchainQueries, parameter), - new EthGetUncleByBlockHashAndIndex(blockchainQueries, parameter), - new EthNewBlockFilter(filterManager), - new EthNewPendingTransactionFilter(filterManager), - new EthNewFilter(filterManager, parameter), - new EthGetTransactionByHash( - blockchainQueries, transactionPool.getPendingTransactions(), parameter), - new EthGetTransactionByBlockHashAndIndex(blockchainQueries, parameter), - new EthGetTransactionByBlockNumberAndIndex(blockchainQueries, parameter), - new EthGetTransactionCount( - blockchainQueries, transactionPool.getPendingTransactions(), parameter), - new EthGetTransactionReceipt(blockchainQueries, parameter), - new EthUninstallFilter(filterManager, parameter), - new EthGetFilterChanges(filterManager, parameter), - new EthGetFilterLogs(filterManager, parameter), - new EthSyncing(synchronizer), - new EthGetStorageAt(blockchainQueries, parameter), - new EthSendRawTransaction(transactionPool, parameter), - new EthSendTransaction(), - new EthEstimateGas( - blockchainQueries, - new TransactionSimulator( - blockchainQueries.getBlockchain(), - blockchainQueries.getWorldStateArchive(), - protocolSchedule), - parameter), - new EthMining(miningCoordinator), - new EthCoinbase(miningCoordinator), - new EthProtocolVersion(supportedCapabilities), - new EthGasPrice(miningCoordinator), - new EthGetWork(miningCoordinator), - new EthHashrate(miningCoordinator), - new EthChainId(protocolSchedule.getChainId())); - } - if (rpcApis.contains(RpcApis.DEBUG)) { - final BlockReplay blockReplay = - new BlockReplay( - protocolSchedule, - blockchainQueries.getBlockchain(), - blockchainQueries.getWorldStateArchive()); - addMethods( - enabledMethods, - new DebugTraceTransaction( - blockchainQueries, new TransactionTracer(blockReplay), parameter), - new DebugAccountRange(parameter, blockchainQueries), - new DebugStorageRangeAt(parameter, blockchainQueries, blockReplay), - new DebugMetrics(metricsSystem), - new DebugTraceBlock( - parameter, - new BlockTracer(blockReplay), - ScheduleBasedBlockHeaderFunctions.create(protocolSchedule), - blockchainQueries), - new DebugTraceBlockByNumber(parameter, new BlockTracer(blockReplay), blockchainQueries), - new DebugTraceBlockByHash(parameter, new BlockTracer(blockReplay))); - } - if (rpcApis.contains(RpcApis.NET)) { - addMethods( - enabledMethods, - new NetVersion(protocolSchedule.getChainId()), - new NetListening(p2pNetwork), - new NetPeerCount(p2pNetwork), - new NetEnode(p2pNetwork), - new NetServices( - jsonRpcConfiguration, webSocketConfiguration, p2pNetwork, metricsConfiguration)); - } - if (rpcApis.contains(RpcApis.WEB3)) { - addMethods(enabledMethods, new Web3ClientVersion(clientVersion), new Web3Sha3()); - } - if (rpcApis.contains(RpcApis.MINER)) { - final MinerSetCoinbase minerSetCoinbase = new MinerSetCoinbase(miningCoordinator, parameter); - addMethods( - enabledMethods, - new MinerStart(miningCoordinator), - new MinerStop(miningCoordinator), - minerSetCoinbase, - new MinerSetEtherbase(minerSetCoinbase)); - } - if (rpcApis.contains(RpcApis.TX_POOL)) { - addMethods( - enabledMethods, - new TxPoolBesuTransactions(transactionPool.getPendingTransactions()), - new TxPoolBesuStatistics(transactionPool.getPendingTransactions())); - } - if (rpcApis.contains(RpcApis.PERM)) { - addMethods( - enabledMethods, - new PermAddNodesToWhitelist(nodeWhitelistController, parameter), - new PermRemoveNodesFromWhitelist(nodeWhitelistController, parameter), - new PermGetNodesWhitelist(nodeWhitelistController), - new PermGetAccountsWhitelist(accountsWhitelistController), - new PermAddAccountsToWhitelist(accountsWhitelistController, parameter), - new PermRemoveAccountsFromWhitelist(accountsWhitelistController, parameter), - new PermReloadPermissionsFromFile(accountsWhitelistController, nodeWhitelistController)); - } - if (rpcApis.contains(RpcApis.ADMIN)) { - addMethods( - enabledMethods, - new AdminAddPeer(p2pNetwork, parameter), - new AdminRemovePeer(p2pNetwork, parameter), - new AdminNodeInfo( - clientVersion, networkId, genesisConfigOptions, p2pNetwork, blockchainQueries), - new AdminPeers(p2pNetwork), - new AdminChangeLogLevel(parameter)); - } - - // Disable TRACE functionality while under development - // if (rpcApis.contains(RpcApis.TRACE)) { - // addMethods( - // enabledMethods, - // new TraceReplayBlockTransactions( - // parameter, - // new BlockTracer( - // new BlockReplay( - // protocolSchedule, - // blockchainQueries.getBlockchain(), - // blockchainQueries.getWorldStateArchive())), - // blockchainQueries, - // protocolSchedule)); - // } - - final boolean eea = rpcApis.contains(RpcApis.EEA); - final boolean priv = rpcApis.contains(RpcApis.PRIV); - if (eea || priv) { - final PrivateMarkerTransactionFactory markerTransactionFactory = - createPrivateMarkerTransactionFactory( - privacyParameters, blockchainQueries, transactionPool.getPendingTransactions()); - - final PrivateTransactionHandler privateTransactionHandler = - new PrivateTransactionHandler( - privacyParameters, protocolSchedule.getChainId(), markerTransactionFactory); - final Enclave enclave = new Enclave(privacyParameters.getEnclaveUri()); - if (eea) { - addMethods( - enabledMethods, - new EeaSendRawTransaction(privateTransactionHandler, transactionPool, parameter), - new PrivGetEeaTransactionCount( - parameter, new PrivateEeaNonceProvider(enclave, privateTransactionHandler))); - } - if (priv) { - addMethods( - enabledMethods, - new PrivGetTransactionReceipt(blockchainQueries, enclave, parameter, privacyParameters), - new PrivCreatePrivacyGroup( - new Enclave(privacyParameters.getEnclaveUri()), privacyParameters, parameter), - new PrivDeletePrivacyGroup( - new Enclave(privacyParameters.getEnclaveUri()), privacyParameters, parameter), - new PrivFindPrivacyGroup(new Enclave(privacyParameters.getEnclaveUri()), parameter), - new PrivGetPrivacyPrecompileAddress(privacyParameters), - new PrivGetTransactionCount(parameter, privateTransactionHandler), - new PrivGetPrivateTransaction(blockchainQueries, enclave, parameter, privacyParameters), - new PrivDistributeRawTransaction( - privateTransactionHandler, transactionPool, parameter)); - } - } - - return enabledMethods; - } - - public static void addMethods( - final Map methods, final JsonRpcMethod... rpcMethods) { - for (final JsonRpcMethod rpcMethod : rpcMethods) { - methods.put(rpcMethod.getName(), rpcMethod); - } - } - - private PrivateMarkerTransactionFactory createPrivateMarkerTransactionFactory( - final PrivacyParameters privacyParameters, - final BlockchainQueries blockchainQueries, - final PendingTransactions pendingTransactions) { - - final Address privateContractAddress = - Address.privacyPrecompiled(privacyParameters.getPrivacyAddress()); - - if (privacyParameters.getSigningKeyPair().isPresent()) { - return new FixedKeySigningPrivateMarkerTransactionFactory( - privateContractAddress, - new LatestNonceProvider(blockchainQueries, pendingTransactions), - privacyParameters.getSigningKeyPair().get()); - } - return new RandomSigningPrivateMarkerTransactionFactory(privateContractAddress); - } -} diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/AdminJsonRpcMethods.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/AdminJsonRpcMethods.java new file mode 100644 index 00000000000..1c5b588db45 --- /dev/null +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/AdminJsonRpcMethods.java @@ -0,0 +1,70 @@ +/* + * 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.api.jsonrpc.methods; + +import org.hyperledger.besu.config.GenesisConfigOptions; +import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi; +import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApis; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.AdminAddPeer; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.AdminChangeLogLevel; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.AdminNodeInfo; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.AdminPeers; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.AdminRemovePeer; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonRpcParameter; +import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; +import org.hyperledger.besu.ethereum.p2p.network.P2PNetwork; + +import java.math.BigInteger; +import java.util.Map; + +public class AdminJsonRpcMethods extends ApiGroupJsonRpcMethods { + + private final JsonRpcParameter parameter = new JsonRpcParameter(); + private final String clientVersion; + private final BigInteger networkId; + private final GenesisConfigOptions genesisConfigOptions; + private final P2PNetwork p2pNetwork; + private final BlockchainQueries blockchainQueries; + + public AdminJsonRpcMethods( + final String clientVersion, + final BigInteger networkId, + final GenesisConfigOptions genesisConfigOptions, + final P2PNetwork p2pNetwork, + final BlockchainQueries blockchainQueries) { + this.clientVersion = clientVersion; + this.networkId = networkId; + this.genesisConfigOptions = genesisConfigOptions; + this.p2pNetwork = p2pNetwork; + this.blockchainQueries = blockchainQueries; + } + + @Override + protected RpcApi getApiGroup() { + return RpcApis.ADMIN; + } + + @Override + protected Map create() { + return mapOf( + new AdminAddPeer(p2pNetwork, parameter), + new AdminRemovePeer(p2pNetwork, parameter), + new AdminNodeInfo( + clientVersion, networkId, genesisConfigOptions, p2pNetwork, blockchainQueries), + new AdminPeers(p2pNetwork), + new AdminChangeLogLevel(parameter)); + } +} diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/ApiGroupJsonRpcMethods.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/ApiGroupJsonRpcMethods.java new file mode 100644 index 00000000000..eb1d04e17c3 --- /dev/null +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/ApiGroupJsonRpcMethods.java @@ -0,0 +1,41 @@ +/* + * 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.api.jsonrpc.methods; + +import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Map; +import java.util.stream.Collectors; + +public abstract class ApiGroupJsonRpcMethods implements JsonRpcMethods { + + @Override + public Map create(final Collection apis) { + return apis.contains(getApiGroup()) ? create() : Collections.emptyMap(); + } + + protected abstract RpcApi getApiGroup(); + + protected abstract Map create(); + + protected Map mapOf(final JsonRpcMethod... methods) { + return Arrays.stream(methods) + .collect(Collectors.toMap(JsonRpcMethod::getName, method -> method)); + } +} diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/DebugJsonRpcMethods.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/DebugJsonRpcMethods.java new file mode 100644 index 00000000000..e9acf87a4e6 --- /dev/null +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/DebugJsonRpcMethods.java @@ -0,0 +1,80 @@ +/* + * 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.api.jsonrpc.methods; + +import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi; +import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApis; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.DebugAccountRange; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.DebugMetrics; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.DebugStorageRangeAt; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.DebugTraceBlock; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.DebugTraceBlockByHash; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.DebugTraceBlockByNumber; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.DebugTraceTransaction; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonRpcParameter; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockReplay; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockTracer; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTracer; +import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; +import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; +import org.hyperledger.besu.ethereum.mainnet.ScheduleBasedBlockHeaderFunctions; +import org.hyperledger.besu.metrics.ObservableMetricsSystem; + +import java.util.Map; + +public class DebugJsonRpcMethods extends ApiGroupJsonRpcMethods { + + private final JsonRpcParameter parameter = new JsonRpcParameter(); + private final BlockchainQueries blockchainQueries; + private final ProtocolSchedule protocolSchedule; + private final ObservableMetricsSystem metricsSystem; + + public DebugJsonRpcMethods( + final BlockchainQueries blockchainQueries, + final ProtocolSchedule protocolSchedule, + final ObservableMetricsSystem metricsSystem) { + this.blockchainQueries = blockchainQueries; + this.protocolSchedule = protocolSchedule; + this.metricsSystem = metricsSystem; + } + + @Override + protected RpcApi getApiGroup() { + return RpcApis.DEBUG; + } + + @Override + protected Map create() { + final BlockReplay blockReplay = + new BlockReplay( + protocolSchedule, + blockchainQueries.getBlockchain(), + blockchainQueries.getWorldStateArchive()); + + return mapOf( + new DebugTraceTransaction(blockchainQueries, new TransactionTracer(blockReplay), parameter), + new DebugAccountRange(parameter, blockchainQueries), + new DebugStorageRangeAt(parameter, blockchainQueries, blockReplay), + new DebugMetrics(metricsSystem), + new DebugTraceBlock( + parameter, + new BlockTracer(blockReplay), + ScheduleBasedBlockHeaderFunctions.create(protocolSchedule), + blockchainQueries), + new DebugTraceBlockByNumber(parameter, new BlockTracer(blockReplay), blockchainQueries), + new DebugTraceBlockByHash(parameter, new BlockTracer(blockReplay))); + } +} diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EeaJsonRpcMethods.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EeaJsonRpcMethods.java new file mode 100644 index 00000000000..d8020069c29 --- /dev/null +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EeaJsonRpcMethods.java @@ -0,0 +1,58 @@ +/* + * 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.api.jsonrpc.methods; + +import org.hyperledger.besu.enclave.Enclave; +import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi; +import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApis; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonRpcParameter; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.eea.EeaSendRawTransaction; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivGetEeaTransactionCount; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivateEeaNonceProvider; +import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; +import org.hyperledger.besu.ethereum.core.PrivacyParameters; +import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool; +import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; +import org.hyperledger.besu.ethereum.privacy.PrivateTransactionHandler; + +import java.util.Map; + +public class EeaJsonRpcMethods extends PrivacyApiGroupJsonRpcMethods { + + private final JsonRpcParameter parameter = new JsonRpcParameter(); + + public EeaJsonRpcMethods( + final BlockchainQueries blockchainQueries, + final ProtocolSchedule protocolSchedule, + final TransactionPool transactionPool, + final PrivacyParameters privacyParameters) { + super(blockchainQueries, protocolSchedule, transactionPool, privacyParameters); + } + + @Override + protected RpcApi getApiGroup() { + return RpcApis.EEA; + } + + @Override + protected Map create( + final PrivateTransactionHandler privateTransactionHandler, final Enclave enclave) { + return mapOf( + new EeaSendRawTransaction(privateTransactionHandler, getTransactionPool(), parameter), + new PrivGetEeaTransactionCount( + parameter, new PrivateEeaNonceProvider(enclave, privateTransactionHandler))); + } +} diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthJsonRpcMethods.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthJsonRpcMethods.java new file mode 100644 index 00000000000..86197aa9f8d --- /dev/null +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthJsonRpcMethods.java @@ -0,0 +1,163 @@ +/* + * 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.api.jsonrpc.methods; + +import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi; +import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApis; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterManager; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthAccounts; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthBlockNumber; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthCall; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthChainId; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthCoinbase; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthEstimateGas; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGasPrice; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetBalance; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetBlockByHash; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetBlockByNumber; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetBlockTransactionCountByHash; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetBlockTransactionCountByNumber; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetCode; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetFilterChanges; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetFilterLogs; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetLogs; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetProof; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetStorageAt; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetTransactionByBlockHashAndIndex; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetTransactionByBlockNumberAndIndex; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetTransactionByHash; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetTransactionCount; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetTransactionReceipt; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetUncleByBlockHashAndIndex; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetUncleByBlockNumberAndIndex; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetUncleCountByBlockHash; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetUncleCountByBlockNumber; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetWork; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthHashrate; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthMining; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthNewBlockFilter; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthNewFilter; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthNewPendingTransactionFilter; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthProtocolVersion; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthSendRawTransaction; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthSendTransaction; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthSyncing; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthUninstallFilter; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonRpcParameter; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResultFactory; +import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; +import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator; +import org.hyperledger.besu.ethereum.core.Synchronizer; +import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool; +import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; +import org.hyperledger.besu.ethereum.p2p.rlpx.wire.Capability; +import org.hyperledger.besu.ethereum.transaction.TransactionSimulator; + +import java.util.Map; +import java.util.Set; + +public class EthJsonRpcMethods extends ApiGroupJsonRpcMethods { + + private final BlockResultFactory blockResult = new BlockResultFactory(); + private final JsonRpcParameter parameter = new JsonRpcParameter(); + + private final BlockchainQueries blockchainQueries; + private final Synchronizer synchronizer; + private final ProtocolSchedule protocolSchedule; + private final FilterManager filterManager; + private final TransactionPool transactionPool; + private final MiningCoordinator miningCoordinator; + private final Set supportedCapabilities; + + public EthJsonRpcMethods( + final BlockchainQueries blockchainQueries, + final Synchronizer synchronizer, + final ProtocolSchedule protocolSchedule, + final FilterManager filterManager, + final TransactionPool transactionPool, + final MiningCoordinator miningCoordinator, + final Set supportedCapabilities) { + this.blockchainQueries = blockchainQueries; + this.synchronizer = synchronizer; + this.protocolSchedule = protocolSchedule; + this.filterManager = filterManager; + this.transactionPool = transactionPool; + this.miningCoordinator = miningCoordinator; + this.supportedCapabilities = supportedCapabilities; + } + + @Override + protected RpcApi getApiGroup() { + return RpcApis.ETH; + } + + @Override + protected Map create() { + return mapOf( + new EthAccounts(), + new EthBlockNumber(blockchainQueries), + new EthGetBalance(blockchainQueries, parameter), + new EthGetBlockByHash(blockchainQueries, blockResult, parameter), + new EthGetBlockByNumber(blockchainQueries, blockResult, parameter), + new EthGetBlockTransactionCountByNumber(blockchainQueries, parameter), + new EthGetBlockTransactionCountByHash(blockchainQueries, parameter), + new EthCall( + blockchainQueries, + new TransactionSimulator( + blockchainQueries.getBlockchain(), + blockchainQueries.getWorldStateArchive(), + protocolSchedule), + parameter), + new EthGetCode(blockchainQueries, parameter), + new EthGetLogs(blockchainQueries, parameter), + new EthGetProof(blockchainQueries, parameter), + new EthGetUncleCountByBlockHash(blockchainQueries, parameter), + new EthGetUncleCountByBlockNumber(blockchainQueries, parameter), + new EthGetUncleByBlockNumberAndIndex(blockchainQueries, parameter), + new EthGetUncleByBlockHashAndIndex(blockchainQueries, parameter), + new EthNewBlockFilter(filterManager), + new EthNewPendingTransactionFilter(filterManager), + new EthNewFilter(filterManager, parameter), + new EthGetTransactionByHash( + blockchainQueries, transactionPool.getPendingTransactions(), parameter), + new EthGetTransactionByBlockHashAndIndex(blockchainQueries, parameter), + new EthGetTransactionByBlockNumberAndIndex(blockchainQueries, parameter), + new EthGetTransactionCount( + blockchainQueries, transactionPool.getPendingTransactions(), parameter), + new EthGetTransactionReceipt(blockchainQueries, parameter), + new EthUninstallFilter(filterManager, parameter), + new EthGetFilterChanges(filterManager, parameter), + new EthGetFilterLogs(filterManager, parameter), + new EthSyncing(synchronizer), + new EthGetStorageAt(blockchainQueries, parameter), + new EthSendRawTransaction(transactionPool, parameter), + new EthSendTransaction(), + new EthEstimateGas( + blockchainQueries, + new TransactionSimulator( + blockchainQueries.getBlockchain(), + blockchainQueries.getWorldStateArchive(), + protocolSchedule), + parameter), + new EthMining(miningCoordinator), + new EthCoinbase(miningCoordinator), + new EthProtocolVersion(supportedCapabilities), + new EthGasPrice(miningCoordinator), + new EthGetWork(miningCoordinator), + new EthHashrate(miningCoordinator), + new EthChainId(protocolSchedule.getChainId())); + } +} diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/JsonRpcMethodFactory.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/JsonRpcMethods.java similarity index 81% rename from ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/JsonRpcMethodFactory.java rename to ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/JsonRpcMethods.java index 2cdd2c55a6e..0b4ae464772 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/method/JsonRpcMethodFactory.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/JsonRpcMethods.java @@ -12,7 +12,7 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.ethereum.api.jsonrpc.method; +package org.hyperledger.besu.ethereum.api.jsonrpc.methods; import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; @@ -20,6 +20,6 @@ import java.util.Collection; import java.util.Map; -public interface JsonRpcMethodFactory { - Map createJsonRpcMethods(Collection enabledRpcApis); +public interface JsonRpcMethods { + Map create(Collection enabledRpcApis); } diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/JsonRpcMethodsFactory.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/JsonRpcMethodsFactory.java new file mode 100644 index 00000000000..5991ecc510d --- /dev/null +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/JsonRpcMethodsFactory.java @@ -0,0 +1,157 @@ +/* + * 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.api.jsonrpc.methods; + +import org.hyperledger.besu.config.GenesisConfigOptions; +import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcConfiguration; +import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterManager; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.RpcModules; +import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration; +import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; +import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator; +import org.hyperledger.besu.ethereum.chain.Blockchain; +import org.hyperledger.besu.ethereum.core.PrivacyParameters; +import org.hyperledger.besu.ethereum.core.Synchronizer; +import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool; +import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; +import org.hyperledger.besu.ethereum.p2p.network.P2PNetwork; +import org.hyperledger.besu.ethereum.p2p.rlpx.wire.Capability; +import org.hyperledger.besu.ethereum.permissioning.AccountLocalConfigPermissioningController; +import org.hyperledger.besu.ethereum.permissioning.NodeLocalConfigPermissioningController; +import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive; +import org.hyperledger.besu.metrics.ObservableMetricsSystem; +import org.hyperledger.besu.metrics.prometheus.MetricsConfiguration; + +import java.math.BigInteger; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; + +public class JsonRpcMethodsFactory { + + public Map methods( + final String clientVersion, + final BigInteger networkId, + final GenesisConfigOptions genesisConfigOptions, + final P2PNetwork peerNetworkingService, + final Blockchain blockchain, + final WorldStateArchive worldStateArchive, + final Synchronizer synchronizer, + final TransactionPool transactionPool, + final ProtocolSchedule protocolSchedule, + final MiningCoordinator miningCoordinator, + final ObservableMetricsSystem metricsSystem, + final Set supportedCapabilities, + final Collection rpcApis, + final FilterManager filterManager, + final Optional accountsWhitelistController, + final Optional nodeWhitelistController, + final PrivacyParameters privacyParameters, + final JsonRpcConfiguration jsonRpcConfiguration, + final WebSocketConfiguration webSocketConfiguration, + final MetricsConfiguration metricsConfiguration) { + final BlockchainQueries blockchainQueries = + new BlockchainQueries(blockchain, worldStateArchive); + return methods( + clientVersion, + networkId, + genesisConfigOptions, + peerNetworkingService, + blockchainQueries, + synchronizer, + protocolSchedule, + filterManager, + transactionPool, + miningCoordinator, + metricsSystem, + supportedCapabilities, + accountsWhitelistController, + nodeWhitelistController, + rpcApis, + privacyParameters, + jsonRpcConfiguration, + webSocketConfiguration, + metricsConfiguration); + } + + public Map methods( + final String clientVersion, + final BigInteger networkId, + final GenesisConfigOptions genesisConfigOptions, + final P2PNetwork p2pNetwork, + final BlockchainQueries blockchainQueries, + final Synchronizer synchronizer, + final ProtocolSchedule protocolSchedule, + final FilterManager filterManager, + final TransactionPool transactionPool, + final MiningCoordinator miningCoordinator, + final ObservableMetricsSystem metricsSystem, + final Set supportedCapabilities, + final Optional accountsWhitelistController, + final Optional nodeWhitelistController, + final Collection rpcApis, + final PrivacyParameters privacyParameters, + final JsonRpcConfiguration jsonRpcConfiguration, + final WebSocketConfiguration webSocketConfiguration, + final MetricsConfiguration metricsConfiguration) { + final Map enabled = new HashMap<>(); + + if (!rpcApis.isEmpty()) { + final JsonRpcMethod modules = new RpcModules(rpcApis); + enabled.put(modules.getName(), modules); + + final List availableApiGroups = + List.of( + new AdminJsonRpcMethods( + clientVersion, networkId, genesisConfigOptions, p2pNetwork, blockchainQueries), + new DebugJsonRpcMethods(blockchainQueries, protocolSchedule, metricsSystem), + new EeaJsonRpcMethods( + blockchainQueries, protocolSchedule, transactionPool, privacyParameters), + new EthJsonRpcMethods( + blockchainQueries, + synchronizer, + protocolSchedule, + filterManager, + transactionPool, + miningCoordinator, + supportedCapabilities), + new NetJsonRpcMethods( + p2pNetwork, + protocolSchedule, + jsonRpcConfiguration, + webSocketConfiguration, + metricsConfiguration), + new MinerJsonRpcMethods(miningCoordinator), + new PermJsonRpcMethods(accountsWhitelistController, nodeWhitelistController), + new PrivJsonRpcMethods( + blockchainQueries, protocolSchedule, transactionPool, privacyParameters), + new Web3JsonRpcMethods(clientVersion), + // TRACE Methods (Disabled while under development) + // new TraceJsonRpcMethods(blockchainQueries,protocolSchedule) + new TxPoolJsonRpcMethods(transactionPool)); + + for (final JsonRpcMethods apiGroup : availableApiGroups) { + enabled.putAll(apiGroup.create(rpcApis)); + } + } + + return enabled; + } +} diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/MinerJsonRpcMethods.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/MinerJsonRpcMethods.java new file mode 100644 index 00000000000..bed18cbaaea --- /dev/null +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/MinerJsonRpcMethods.java @@ -0,0 +1,52 @@ +/* + * 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.api.jsonrpc.methods; + +import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi; +import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApis; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.miner.MinerSetCoinbase; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.miner.MinerSetEtherbase; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.miner.MinerStart; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.miner.MinerStop; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonRpcParameter; +import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator; + +import java.util.Map; + +public class MinerJsonRpcMethods extends ApiGroupJsonRpcMethods { + + private final JsonRpcParameter parameter = new JsonRpcParameter(); + private final MiningCoordinator miningCoordinator; + + public MinerJsonRpcMethods(final MiningCoordinator miningCoordinator) { + this.miningCoordinator = miningCoordinator; + } + + @Override + protected RpcApi getApiGroup() { + return RpcApis.MINER; + } + + @Override + protected Map create() { + final MinerSetCoinbase minerSetCoinbase = new MinerSetCoinbase(miningCoordinator, parameter); + return mapOf( + new MinerStart(miningCoordinator), + new MinerStop(miningCoordinator), + minerSetCoinbase, + new MinerSetEtherbase(minerSetCoinbase)); + } +} diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/NetJsonRpcMethods.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/NetJsonRpcMethods.java new file mode 100644 index 00000000000..49a81953d64 --- /dev/null +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/NetJsonRpcMethods.java @@ -0,0 +1,69 @@ +/* + * 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.api.jsonrpc.methods; + +import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcConfiguration; +import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi; +import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApis; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.NetEnode; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.NetListening; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.NetPeerCount; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.NetServices; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.NetVersion; +import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration; +import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; +import org.hyperledger.besu.ethereum.p2p.network.P2PNetwork; +import org.hyperledger.besu.metrics.prometheus.MetricsConfiguration; + +import java.util.Map; + +public class NetJsonRpcMethods extends ApiGroupJsonRpcMethods { + + private final P2PNetwork p2pNetwork; + private final ProtocolSchedule protocolSchedule; + private final JsonRpcConfiguration jsonRpcConfiguration; + private final WebSocketConfiguration webSocketConfiguration; + private final MetricsConfiguration metricsConfiguration; + + public NetJsonRpcMethods( + final P2PNetwork p2pNetwork, + final ProtocolSchedule protocolSchedule, + final JsonRpcConfiguration jsonRpcConfiguration, + final WebSocketConfiguration webSocketConfiguration, + final MetricsConfiguration metricsConfiguration) { + this.p2pNetwork = p2pNetwork; + this.protocolSchedule = protocolSchedule; + this.jsonRpcConfiguration = jsonRpcConfiguration; + this.webSocketConfiguration = webSocketConfiguration; + this.metricsConfiguration = metricsConfiguration; + } + + @Override + protected RpcApi getApiGroup() { + return RpcApis.NET; + } + + @Override + protected Map create() { + return mapOf( + new NetVersion(protocolSchedule.getChainId()), + new NetListening(p2pNetwork), + new NetPeerCount(p2pNetwork), + new NetEnode(p2pNetwork), + new NetServices( + jsonRpcConfiguration, webSocketConfiguration, p2pNetwork, metricsConfiguration)); + } +} diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PermJsonRpcMethods.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PermJsonRpcMethods.java new file mode 100644 index 00000000000..6f667c2743e --- /dev/null +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PermJsonRpcMethods.java @@ -0,0 +1,63 @@ +/* + * 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.api.jsonrpc.methods; + +import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi; +import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApis; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.permissioning.PermAddAccountsToWhitelist; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.permissioning.PermAddNodesToWhitelist; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.permissioning.PermGetAccountsWhitelist; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.permissioning.PermGetNodesWhitelist; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.permissioning.PermReloadPermissionsFromFile; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.permissioning.PermRemoveAccountsFromWhitelist; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.permissioning.PermRemoveNodesFromWhitelist; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonRpcParameter; +import org.hyperledger.besu.ethereum.permissioning.AccountLocalConfigPermissioningController; +import org.hyperledger.besu.ethereum.permissioning.NodeLocalConfigPermissioningController; + +import java.util.Map; +import java.util.Optional; + +public class PermJsonRpcMethods extends ApiGroupJsonRpcMethods { + + private final JsonRpcParameter parameter = new JsonRpcParameter(); + private final Optional accountsWhitelistController; + private final Optional nodeWhitelistController; + + public PermJsonRpcMethods( + final Optional accountsWhitelistController, + final Optional nodeWhitelistController) { + this.accountsWhitelistController = accountsWhitelistController; + this.nodeWhitelistController = nodeWhitelistController; + } + + @Override + protected RpcApi getApiGroup() { + return RpcApis.PERM; + } + + @Override + protected Map create() { + return mapOf( + new PermAddNodesToWhitelist(nodeWhitelistController, parameter), + new PermRemoveNodesFromWhitelist(nodeWhitelistController, parameter), + new PermGetNodesWhitelist(nodeWhitelistController), + new PermGetAccountsWhitelist(accountsWhitelistController), + new PermAddAccountsToWhitelist(accountsWhitelistController, parameter), + new PermRemoveAccountsFromWhitelist(accountsWhitelistController, parameter), + new PermReloadPermissionsFromFile(accountsWhitelistController, nodeWhitelistController)); + } +} diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivJsonRpcMethods.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivJsonRpcMethods.java new file mode 100644 index 00000000000..6b79f8f59d5 --- /dev/null +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivJsonRpcMethods.java @@ -0,0 +1,73 @@ +/* + * 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.api.jsonrpc.methods; + +import org.hyperledger.besu.enclave.Enclave; +import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi; +import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApis; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonRpcParameter; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivCreatePrivacyGroup; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivDeletePrivacyGroup; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivDistributeRawTransaction; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivFindPrivacyGroup; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivGetPrivacyPrecompileAddress; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivGetPrivateTransaction; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivGetTransactionCount; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivGetTransactionReceipt; +import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; +import org.hyperledger.besu.ethereum.core.PrivacyParameters; +import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool; +import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; +import org.hyperledger.besu.ethereum.privacy.PrivateTransactionHandler; + +import java.util.Map; + +public class PrivJsonRpcMethods extends PrivacyApiGroupJsonRpcMethods { + + private final JsonRpcParameter parameter = new JsonRpcParameter(); + + public PrivJsonRpcMethods( + final BlockchainQueries blockchainQueries, + final ProtocolSchedule protocolSchedule, + final TransactionPool transactionPool, + final PrivacyParameters privacyParameters) { + super(blockchainQueries, protocolSchedule, transactionPool, privacyParameters); + } + + @Override + protected RpcApi getApiGroup() { + return RpcApis.PRIV; + } + + @Override + protected Map create( + final PrivateTransactionHandler privateTransactionHandler, final Enclave enclave) { + return mapOf( + new PrivGetTransactionReceipt( + getBlockchainQueries(), enclave, parameter, getPrivacyParameters()), + new PrivCreatePrivacyGroup( + new Enclave(getPrivacyParameters().getEnclaveUri()), getPrivacyParameters(), parameter), + new PrivDeletePrivacyGroup( + new Enclave(getPrivacyParameters().getEnclaveUri()), getPrivacyParameters(), parameter), + new PrivFindPrivacyGroup(new Enclave(getPrivacyParameters().getEnclaveUri()), parameter), + new PrivGetPrivacyPrecompileAddress(getPrivacyParameters()), + new PrivGetTransactionCount(parameter, privateTransactionHandler), + new PrivGetPrivateTransaction( + getBlockchainQueries(), enclave, parameter, getPrivacyParameters()), + new PrivDistributeRawTransaction( + privateTransactionHandler, getTransactionPool(), parameter)); + } +} diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivacyApiGroupJsonRpcMethods.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivacyApiGroupJsonRpcMethods.java new file mode 100644 index 00000000000..53881a8c015 --- /dev/null +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/PrivacyApiGroupJsonRpcMethods.java @@ -0,0 +1,101 @@ +/* + * 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.api.jsonrpc.methods; + +import org.hyperledger.besu.enclave.Enclave; +import org.hyperledger.besu.ethereum.api.jsonrpc.LatestNonceProvider; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; +import org.hyperledger.besu.ethereum.core.Address; +import org.hyperledger.besu.ethereum.core.PrivacyParameters; +import org.hyperledger.besu.ethereum.eth.transactions.PendingTransactions; +import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool; +import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; +import org.hyperledger.besu.ethereum.privacy.PrivateTransactionHandler; +import org.hyperledger.besu.ethereum.privacy.markertransaction.FixedKeySigningPrivateMarkerTransactionFactory; +import org.hyperledger.besu.ethereum.privacy.markertransaction.PrivateMarkerTransactionFactory; +import org.hyperledger.besu.ethereum.privacy.markertransaction.RandomSigningPrivateMarkerTransactionFactory; + +import java.util.Map; + +public abstract class PrivacyApiGroupJsonRpcMethods extends ApiGroupJsonRpcMethods { + + private final BlockchainQueries blockchainQueries; + private final ProtocolSchedule protocolSchedule; + private final TransactionPool transactionPool; + private final PrivacyParameters privacyParameters; + + public PrivacyApiGroupJsonRpcMethods( + final BlockchainQueries blockchainQueries, + final ProtocolSchedule protocolSchedule, + final TransactionPool transactionPool, + final PrivacyParameters privacyParameters) { + this.blockchainQueries = blockchainQueries; + this.protocolSchedule = protocolSchedule; + this.transactionPool = transactionPool; + this.privacyParameters = privacyParameters; + } + + public BlockchainQueries getBlockchainQueries() { + return blockchainQueries; + } + + public ProtocolSchedule getProtocolSchedule() { + return protocolSchedule; + } + + public TransactionPool getTransactionPool() { + return transactionPool; + } + + public PrivacyParameters getPrivacyParameters() { + return privacyParameters; + } + + @Override + protected Map create() { + final PrivateMarkerTransactionFactory markerTransactionFactory = + createPrivateMarkerTransactionFactory( + privacyParameters, blockchainQueries, transactionPool.getPendingTransactions()); + + final PrivateTransactionHandler privateTransactionHandler = + new PrivateTransactionHandler( + privacyParameters, protocolSchedule.getChainId(), markerTransactionFactory); + + final Enclave enclave = new Enclave(privacyParameters.getEnclaveUri()); + + return create(privateTransactionHandler, enclave); + } + + protected abstract Map create( + final PrivateTransactionHandler privateTransactionHandler, final Enclave enclave); + + private PrivateMarkerTransactionFactory createPrivateMarkerTransactionFactory( + final PrivacyParameters privacyParameters, + final BlockchainQueries blockchainQueries, + final PendingTransactions pendingTransactions) { + + final Address privateContractAddress = + Address.privacyPrecompiled(privacyParameters.getPrivacyAddress()); + + if (privacyParameters.getSigningKeyPair().isPresent()) { + return new FixedKeySigningPrivateMarkerTransactionFactory( + privateContractAddress, + new LatestNonceProvider(blockchainQueries, pendingTransactions), + privacyParameters.getSigningKeyPair().get()); + } + return new RandomSigningPrivateMarkerTransactionFactory(privateContractAddress); + } +} diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/TraceJsonRpcMethods.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/TraceJsonRpcMethods.java new file mode 100644 index 00000000000..eb0c8b46900 --- /dev/null +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/TraceJsonRpcMethods.java @@ -0,0 +1,60 @@ +/* + * 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.api.jsonrpc.methods; + +import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.TraceReplayBlockTransactions; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonRpcParameter; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockReplay; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockTracer; +import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; +import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; + +import java.util.Map; + +public class TraceJsonRpcMethods extends ApiGroupJsonRpcMethods { + + private final JsonRpcParameter parameter = new JsonRpcParameter(); + private final BlockchainQueries blockchainQueries; + private final ProtocolSchedule protocolSchedule; + + public TraceJsonRpcMethods( + final BlockchainQueries blockchainQueries, final ProtocolSchedule protocolSchedule) { + this.blockchainQueries = blockchainQueries; + this.protocolSchedule = protocolSchedule; + } + + @Override + protected RpcApi getApiGroup() { + // Disable TRACE functionality while under development + // return RpcApis.TRACE; + return null; + } + + @Override + protected Map create() { + return mapOf( + new TraceReplayBlockTransactions( + parameter, + new BlockTracer( + new BlockReplay( + protocolSchedule, + blockchainQueries.getBlockchain(), + blockchainQueries.getWorldStateArchive())), + blockchainQueries, + protocolSchedule)); + } +} diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/TxPoolJsonRpcMethods.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/TxPoolJsonRpcMethods.java new file mode 100644 index 00000000000..f1e3197d52f --- /dev/null +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/TxPoolJsonRpcMethods.java @@ -0,0 +1,45 @@ +/* + * 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.api.jsonrpc.methods; + +import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi; +import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApis; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.TxPoolBesuStatistics; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.TxPoolBesuTransactions; +import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool; + +import java.util.Map; + +public class TxPoolJsonRpcMethods extends ApiGroupJsonRpcMethods { + + private final TransactionPool transactionPool; + + public TxPoolJsonRpcMethods(final TransactionPool transactionPool) { + this.transactionPool = transactionPool; + } + + @Override + protected RpcApi getApiGroup() { + return RpcApis.TX_POOL; + } + + @Override + protected Map create() { + return mapOf( + new TxPoolBesuTransactions(transactionPool.getPendingTransactions()), + new TxPoolBesuStatistics(transactionPool.getPendingTransactions())); + } +} diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/Web3JsonRpcMethods.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/Web3JsonRpcMethods.java new file mode 100644 index 00000000000..ca9176aec21 --- /dev/null +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/Web3JsonRpcMethods.java @@ -0,0 +1,42 @@ +/* + * 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.api.jsonrpc.methods; + +import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi; +import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApis; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.Web3ClientVersion; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.Web3Sha3; + +import java.util.Map; + +public class Web3JsonRpcMethods extends ApiGroupJsonRpcMethods { + + private final String clientVersion; + + public Web3JsonRpcMethods(final String clientVersion) { + this.clientVersion = clientVersion; + } + + @Override + protected RpcApi getApiGroup() { + return RpcApis.WEB3; + } + + @Override + protected Map create() { + return mapOf(new Web3ClientVersion(clientVersion), new Web3Sha3()); + } +} diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/AbstractJsonRpcHttpServiceTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/AbstractJsonRpcHttpServiceTest.java index dee25966e5c..1af7864a72a 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/AbstractJsonRpcHttpServiceTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/AbstractJsonRpcHttpServiceTest.java @@ -26,7 +26,7 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterManager; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterRepository; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; -import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodsFactory; +import org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethodsFactory; import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration; import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; import org.hyperledger.besu.ethereum.blockcreation.EthHashMiningCoordinator; diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceHostWhitelistTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceHostWhitelistTest.java index 73b23950b17..40b4d1b10eb 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceHostWhitelistTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceHostWhitelistTest.java @@ -22,7 +22,7 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.health.HealthService; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterManager; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; -import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodsFactory; +import org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethodsFactory; import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration; import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; import org.hyperledger.besu.ethereum.blockcreation.EthHashMiningCoordinator; diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceLoginTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceLoginTest.java index e4318c6b74d..ca423147645 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceLoginTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceLoginTest.java @@ -28,7 +28,7 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.NetVersion; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.Web3ClientVersion; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.Web3Sha3; -import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodsFactory; +import org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethodsFactory; import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration; import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; import org.hyperledger.besu.ethereum.blockcreation.EthHashMiningCoordinator; diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceRpcApisTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceRpcApisTest.java index 2a8e01b49d8..881325c480b 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceRpcApisTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceRpcApisTest.java @@ -25,7 +25,7 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterManager; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; -import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodsFactory; +import org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethodsFactory; import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration; import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; import org.hyperledger.besu.ethereum.blockcreation.EthHashMiningCoordinator; diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceTest.java index aaae48d35ee..d9b9b527395 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcHttpServiceTest.java @@ -29,7 +29,7 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterManager; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; -import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodsFactory; +import org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethodsFactory; import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration; import org.hyperledger.besu.ethereum.api.query.BlockWithMetadata; import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; diff --git a/ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/RetestethService.java b/ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/RetestethService.java index 06aec92198f..37096c5eb6e 100644 --- a/ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/RetestethService.java +++ b/ethereum/retesteth/src/main/java/org/hyperledger/besu/ethereum/retesteth/RetestethService.java @@ -30,7 +30,6 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.Web3ClientVersion; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonRpcParameter; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResultFactory; -import org.hyperledger.besu.ethereum.api.jsonrpc.method.JsonRpcMethodsFactory; import org.hyperledger.besu.ethereum.retesteth.methods.TestGetLogHash; import org.hyperledger.besu.ethereum.retesteth.methods.TestImportRawBlock; import org.hyperledger.besu.ethereum.retesteth.methods.TestMineBlocks; @@ -39,9 +38,10 @@ import org.hyperledger.besu.ethereum.retesteth.methods.TestSetChainParams; import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem; -import java.util.HashMap; +import java.util.Arrays; import java.util.Map; import java.util.Optional; +import java.util.stream.Collectors; import io.vertx.core.Vertx; @@ -61,33 +61,32 @@ public RetestethService( final JsonRpcParameter parameters = new JsonRpcParameter(); final BlockResultFactory blockResult = new BlockResultFactory(); - final Map jsonRpcMethods = new HashMap<>(); - JsonRpcMethodsFactory.addMethods( - jsonRpcMethods, - new Web3ClientVersion(clientVersion), - new TestSetChainParams(retestethContext), - new TestImportRawBlock(retestethContext, parameters), - new EthBlockNumber(retestethContext::getBlockchainQueries, true), - new EthGetBlockByNumber( - retestethContext::getBlockchainQueries, blockResult, parameters, true), - new DebugAccountRange(parameters, retestethContext::getBlockchainQueries), - new EthGetBalance(retestethContext::getBlockchainQueries, parameters), - new EthGetCode(retestethContext::getBlockchainQueries, parameters), - new EthGetTransactionCount( - retestethContext::getBlockchainQueries, - retestethContext::getPendingTransactions, - parameters, - true), - new DebugStorageRangeAt( - parameters, - retestethContext::getBlockchainQueries, - retestethContext::getBlockReplay, - true), - new TestModifyTimestamp(retestethContext, parameters), - new EthSendRawTransaction(retestethContext::getTransactionPool, parameters, true), - new TestMineBlocks(retestethContext, parameters), - new TestGetLogHash(retestethContext, parameters), - new TestRewindToBlock(retestethContext, parameters)); + final Map jsonRpcMethods = + mapOf( + new Web3ClientVersion(clientVersion), + new TestSetChainParams(retestethContext), + new TestImportRawBlock(retestethContext, parameters), + new EthBlockNumber(retestethContext::getBlockchainQueries, true), + new EthGetBlockByNumber( + retestethContext::getBlockchainQueries, blockResult, parameters, true), + new DebugAccountRange(parameters, retestethContext::getBlockchainQueries), + new EthGetBalance(retestethContext::getBlockchainQueries, parameters), + new EthGetCode(retestethContext::getBlockchainQueries, parameters), + new EthGetTransactionCount( + retestethContext::getBlockchainQueries, + retestethContext::getPendingTransactions, + parameters, + true), + new DebugStorageRangeAt( + parameters, + retestethContext::getBlockchainQueries, + retestethContext::getBlockReplay, + true), + new TestModifyTimestamp(retestethContext, parameters), + new EthSendRawTransaction(retestethContext::getTransactionPool, parameters, true), + new TestMineBlocks(retestethContext, parameters), + new TestGetLogHash(retestethContext, parameters), + new TestRewindToBlock(retestethContext, parameters)); jsonRpcHttpService = new JsonRpcHttpService( @@ -112,4 +111,9 @@ public void close() { public void stop() { jsonRpcHttpService.stop(); } + + private static Map mapOf(final JsonRpcMethod... rpcMethods) { + return Arrays.stream(rpcMethods) + .collect(Collectors.toMap(JsonRpcMethod::getName, rpcMethod -> rpcMethod)); + } } From b89494f966f760f4199f42683b13ae7368fd797b Mon Sep 17 00:00:00 2001 From: SteveM Date: Mon, 4 Nov 2019 23:04:39 -0800 Subject: [PATCH 09/42] remove print statement from forkId Signed-off-by: SteveM --- .../besu/ethereum/eth/manager/ForkId.java | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkId.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkId.java index 573421701d0..3c95a9d944c 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkId.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkId.java @@ -40,17 +40,17 @@ public class ForkId { private Long forkNext; private Long highestKnownFork = 0L; private ForkIdEntry lastKnownEntry; - private boolean useForkId; +// private boolean useForkId; private ArrayDeque forkAndHashList; public ForkId(final Hash genesisHash, final Set forks, final Long currentHead) { this.genesisHash = genesisHash; this.currentHead = currentHead; - if(forks != null){ - useForkId = true; + if (forks != null) { +// useForkId = true; forkAndHashList = collectForksAndHashes(forks, currentHead); } else { - useForkId = false; +// useForkId = false; forkAndHashList = new ArrayDeque<>(); } }; @@ -95,13 +95,13 @@ public ArrayDeque getForkAndHashList() { return this.forkAndHashList; } - public Hash getLatestForkId(){ - System.out.println("USING FORK ID: " + useForkId); // todo remove dev item - if(useForkId){ - return Hash.fromHexString(lastKnownEntry.hash); - } else { + public Hash getLatestForkId() { + // TODO: implement handling for forkID in status message +// if (useForkId) { +// return Hash.fromHexString(lastKnownEntry.hash); +// } else { return genesisHash; - } +// } } public boolean peerCheck(final String forkHash, final Long peerNext) { @@ -146,7 +146,6 @@ public boolean peerCheck(final Bytes32 peerGenesisOrCheckSumHash) { return !peerGenesisOrCheckSumHash.equals(genesisHash); } - private boolean isHashKnown(final String forkHash) { for (ForkIdEntry j : forkAndHashList) { if (forkHash.equals(j.hash)) { From 138d03024e4bcc1bc6ae11632aac5e829ebf55cf Mon Sep 17 00:00:00 2001 From: SteveM Date: Mon, 4 Nov 2019 23:30:00 -0800 Subject: [PATCH 10/42] check protocol version and bypass at present until eth protocol with forkId announced Signed-off-by: SteveM --- .../besu/ethereum/eth/EthProtocol.java | 1 + .../eth/manager/EthProtocolManager.java | 4 +- .../besu/ethereum/eth/manager/ForkId.java | 52 +++++++++++++------ .../besu/ethereum/eth/manager/ForkIdTest.java | 28 +++++----- 4 files changed, 54 insertions(+), 31 deletions(-) diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/EthProtocol.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/EthProtocol.java index 913ef01e52e..6d0e77409fc 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/EthProtocol.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/EthProtocol.java @@ -120,5 +120,6 @@ public static EthProtocol get() { public static class EthVersion { public static final int V62 = 62; public static final int V63 = 63; + public static final int V64 = 64; } } diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java index dff5c2d6fc2..6dee6291254 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java @@ -179,7 +179,7 @@ public EthProtocolManager( genesisHash = blockchain.getBlockHashByNumber(0L).get(); // todo: check about modification of the above and related constructor - forkId = ForkId.buildCollection(genesisHash, forks, blockchain); + forkId = ForkId.buildCollection(genesisHash, forks, blockchain, getSupportedCapabilities()); ethPeers = new EthPeers(getSupportedProtocol(), clock, metricsSystem); ethMessages = new EthMessages(); @@ -290,7 +290,7 @@ public void handleNewConnection(final PeerConnection connection) { networkId, blockchain.getChainHead().getTotalDifficulty(), blockchain.getChainHeadHash(), - forkId.getLatestForkId()); + forkId.getLatestForkId(cap.getVersion())); try { LOG.debug("Sending status message to {}.", peer); peer.send(status); diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkId.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkId.java index 3c95a9d944c..f829db7b6bd 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkId.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkId.java @@ -18,9 +18,13 @@ import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.core.Hash; +import org.hyperledger.besu.ethereum.eth.EthProtocol; +import org.hyperledger.besu.ethereum.p2p.rlpx.wire.Capability; +import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput; import org.hyperledger.besu.ethereum.rlp.RLPInput; import org.hyperledger.besu.ethereum.rlp.RLPOutput; import org.hyperledger.besu.util.bytes.Bytes32; +import org.hyperledger.besu.util.bytes.BytesValue; import org.hyperledger.besu.util.bytes.BytesValues; import java.nio.charset.StandardCharsets; @@ -40,42 +44,52 @@ public class ForkId { private Long forkNext; private Long highestKnownFork = 0L; private ForkIdEntry lastKnownEntry; -// private boolean useForkId; + private boolean useForkId; private ArrayDeque forkAndHashList; - public ForkId(final Hash genesisHash, final Set forks, final Long currentHead) { + public ForkId( + final Hash genesisHash, + final Set forks, + final Long currentHead, + final boolean useForkId) { this.genesisHash = genesisHash; this.currentHead = currentHead; + this.useForkId = useForkId; if (forks != null) { -// useForkId = true; forkAndHashList = collectForksAndHashes(forks, currentHead); } else { -// useForkId = false; forkAndHashList = new ArrayDeque<>(); } }; public static ForkId buildCollection( - final Hash genesisHash, final List forks, final Blockchain blockchain) { + final Hash genesisHash, + final List forks, + final Blockchain blockchain, + final List caps) { if (forks == null) { - return new ForkId(genesisHash, null, blockchain.getChainHeadBlockNumber()); + return new ForkId(genesisHash, null, blockchain.getChainHeadBlockNumber(), false); } else { Set forkSet = new LinkedHashSet<>(forks); - return new ForkId(genesisHash, forkSet, blockchain.getChainHeadBlockNumber()); + return new ForkId( + genesisHash, + forkSet, + blockchain.getChainHeadBlockNumber(), + caps.contains(Capability.create(EthProtocol.NAME, EthProtocol.EthVersion.V64))); } }; public static ForkId buildCollection(final Hash genesisHash, final List forks) { if (forks == null) { - return new ForkId(genesisHash, null, Long.MAX_VALUE); + return new ForkId(genesisHash, null, Long.MAX_VALUE, false); } else { Set forkSet = new LinkedHashSet<>(forks); - return new ForkId(genesisHash, forkSet, Long.MAX_VALUE); + return new ForkId(genesisHash, forkSet, Long.MAX_VALUE, true); } }; public static ForkId buildCollection(final Hash genesisHash) { - return new ForkId(genesisHash, null, Long.MAX_VALUE); + return new ForkId(genesisHash, null, Long.MAX_VALUE, false); }; public static ForkIdEntry readFrom(final RLPInput in) { @@ -95,13 +109,13 @@ public ArrayDeque getForkAndHashList() { return this.forkAndHashList; } - public Hash getLatestForkId() { + public Hash getLatestForkId(final int protocolVersion) { // TODO: implement handling for forkID in status message -// if (useForkId) { -// return Hash.fromHexString(lastKnownEntry.hash); -// } else { + if (useForkId && protocolVersion > 63) { + return Hash.fromHexString(lastKnownEntry.hash); + } else { return genesisHash; -// } + } } public boolean peerCheck(final String forkHash, final Long peerNext) { @@ -240,10 +254,18 @@ public String getCurrentCrcHash() { public static class ForkIdEntry { String hash; long next; + BytesValue forkIdRLP; public ForkIdEntry(final String hash, final long next) { this.hash = hash; this.next = next; + createForkIdRLP(); + } + + public void createForkIdRLP() { + BytesValueRLPOutput out = new BytesValueRLPOutput(); + out.writeList(asList(), ForkId.ForkIdEntry::writeTo); + forkIdRLP = out.encoded(); } public void writeTo(final RLPOutput out) { diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdTest.java index 206f6e79cbe..60c481b4113 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdTest.java @@ -36,7 +36,7 @@ public class ForkIdTest { @Test public void checkItFunctionsWithPresentBehavior() { - ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), null, null); + ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), null, null, false); assertThat(forkId.peerCheck(Hash.fromHexString(mainnetGenHash))).isFalse(); } @@ -122,7 +122,7 @@ public void check1PetersburgWithRemoteAnnouncingTheSame() { // {7987396, ID{Hash: 0x668db0af, Next: 0}, nil}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L, true); Boolean result = forkId.peerCheck("0x668db0af", 0L); assertThat(result).isTrue(); } @@ -134,7 +134,7 @@ public void check2PetersburgWithRemoteAnnouncingTheSameAndNextFork() { // {7987396, ID{Hash: 0x668db0af, Next: math.MaxUint64}, nil}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L, true); Boolean result = forkId.peerCheck("0x668db0af", Long.MAX_VALUE); assertThat(result).isTrue(); } @@ -148,7 +148,7 @@ public void check3ByzantiumAwareOfPetersburgRemoteUnawareOfPetersburg() { // {7279999, ID{Hash: 0xa00bc324, Next: 0}, nil}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); + ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L, true); Boolean result = forkId.peerCheck("0xa00bc324", 0L); assertThat(result).isTrue(); } @@ -161,7 +161,7 @@ public void check4ByzantiumAwareOfPetersburgRemoteAwareOfPetersburg() { // {7279999, ID{Hash: 0xa00bc324, Next: 7280000}, nil}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); + ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L, true); Boolean result = forkId.peerCheck("0xa00bc324", 7280000L); assertThat(result).isTrue(); } @@ -175,7 +175,7 @@ public void check5ByzantiumAwareOfPetersburgRemoteAnnouncingUnknownFork() { // {7279999, ID{Hash: 0xa00bc324, Next: math.MaxUint64}, nil}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); + ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L, true); Boolean result = forkId.peerCheck("0xa00bc324", Long.MAX_VALUE); assertThat(result).isTrue(); } @@ -187,7 +187,7 @@ public void check6PetersburgWithRemoteAnnouncingByzantiumAwareOfPetersburg() { // {7987396, ID{Hash: 0x668db0af, Next: 7280000}, nil}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L, true); Boolean result = forkId.peerCheck("0x668db0af", 7280000L); assertThat(result).isTrue(); } @@ -200,7 +200,7 @@ public void check7PetersburgWithRemoteAnnouncingSpuriousAwareOfByzantiumRemoteMa // {7987396, ID{Hash: 0x3edd5b10, Next: 4370000}, nil}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L, true); Boolean result = forkId.peerCheck("0x3edd5b10", 4370000L); assertThat(result).isTrue(); } @@ -211,7 +211,7 @@ public void check8ByzantiumWithRemoteAnnouncingPetersburgLocalOutOfSync() { // {7279999, ID{Hash: 0x668db0af, Next: 0}, nil}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); + ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L, true); Boolean result = forkId.peerCheck("0x668db0af", 0L); assertThat(result).isTrue(); } @@ -223,7 +223,7 @@ public void check9SpuriousWithRemoteAnnouncingByzantiumRemoteUnawareOfPetersburg // {4369999, ID{Hash: 0xa00bc324, Next: 0}, nil}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 4369999L); + ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 4369999L, true); Boolean result = forkId.peerCheck("0xa00bc324", 0L); assertThat(result).isTrue(); } @@ -235,7 +235,7 @@ public void check10PetersburgWithRemoteAnnouncingByzantiumRemoteUnawareOfAdditio // {7987396, ID{Hash: 0xa00bc324, Next: 0}, ErrRemoteStale}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L, true); Boolean result = forkId.peerCheck("0xa00bc324", 0L); assertThat(result).isFalse(); } @@ -247,7 +247,7 @@ public void check11PetersburgWithRemoteAnnouncingPetersburgAndFutureForkLocalNee // {7987396, ID{Hash: 0x5cddc0e1, Next: 0}, ErrLocalIncompatibleOrStale}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L, true); Boolean result = forkId.peerCheck("0x5cddc0e1", 0L); assertThat(result).isFalse(); } @@ -259,7 +259,7 @@ public void check12ByzantiumWithRemoteAnnouncingPetersburgAndFutureForkLocalNeed // {7279999, ID{Hash: 0x5cddc0e1, Next: 0}, ErrLocalIncompatibleOrStale}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); + ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L, true); Boolean result = forkId.peerCheck("0x5cddc0e1", 0L); assertThat(result).isFalse(); } @@ -270,7 +270,7 @@ public void check13ByzantiumWithRemoteAnnouncingRinkebyPetersburg() { // {7987396, ID{Hash: 0xafec6b27, Next: 0}, ErrLocalIncompatibleOrStale}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L, true); Boolean result = forkId.peerCheck("0xafec6b27", 0L); assertThat(result).isFalse(); } From 935e1ec17749f5610a97eff53542eee9bc1f953f Mon Sep 17 00:00:00 2001 From: SteveM Date: Wed, 6 Nov 2019 22:11:58 -0800 Subject: [PATCH 11/42] commit suggestion to param description Signed-off-by: SteveM --- .../java/org/hyperledger/besu/ethereum/eth/manager/ForkId.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkId.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkId.java index f829db7b6bd..617fff8cc55 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkId.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkId.java @@ -153,7 +153,7 @@ public boolean peerCheck(final String forkHash, final Long peerNext) { /** * Non EIP-2124 behaviour * - * @param peerGenesisOrCheckSumHash + * @param peerGenesisOrCheckSumHash Hash or checksum to be validated. * @return boolean */ public boolean peerCheck(final Bytes32 peerGenesisOrCheckSumHash) { From 1882dcb79d9362bd387969320c45475254e58151 Mon Sep 17 00:00:00 2001 From: SteveM Date: Sat, 16 Nov 2019 17:56:37 -0800 Subject: [PATCH 12/42] change forkId to forkIdManager, idEntry to ForkId, modify statusMessage class to accept forkId, and use bytesValue instead of string within forkId Signed-off-by: SteveM --- .../eth/manager/EthProtocolManager.java | 58 +++++-- .../{ForkId.java => ForkIdManager.java} | 163 +++++++++--------- .../ethereum/eth/messages/StatusMessage.java | 57 +++++- ...ForkIdTest.java => ForkIdManagerTest.java} | 135 +++++++-------- .../eth/messages/StatusMessageTest.java | 21 +++ 5 files changed, 264 insertions(+), 170 deletions(-) rename ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/{ForkId.java => ForkIdManager.java} (68%) rename ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/{ForkIdTest.java => ForkIdManagerTest.java} (61%) diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java index 6dee6291254..be922e1ef3c 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java @@ -62,7 +62,7 @@ public class EthProtocolManager implements ProtocolManager, MinedBlockObserver { private final AtomicBoolean stopped = new AtomicBoolean(false); private final Hash genesisHash; - private final ForkId forkId; + private final ForkIdManager forkIdManager; private final BigInteger networkId; private final EthPeers ethPeers; private final EthMessages ethMessages; @@ -92,7 +92,7 @@ public EthProtocolManager( this.shutdown = new CountDownLatch(1); genesisHash = blockchain.getBlockHashByNumber(0L).get(); - forkId = ForkId.buildCollection(genesisHash); + forkIdManager = ForkIdManager.buildCollection(genesisHash); ethPeers = new EthPeers(getSupportedProtocol(), clock, metricsSystem); ethMessages = new EthMessages(); @@ -179,7 +179,7 @@ public EthProtocolManager( genesisHash = blockchain.getBlockHashByNumber(0L).get(); // todo: check about modification of the above and related constructor - forkId = ForkId.buildCollection(genesisHash, forks, blockchain, getSupportedCapabilities()); + forkIdManager = ForkIdManager.buildCollection(genesisHash, forks, blockchain, getSupportedCapabilities()); ethPeers = new EthPeers(getSupportedProtocol(), clock, metricsSystem); ethMessages = new EthMessages(); @@ -284,19 +284,37 @@ public void handleNewConnection(final PeerConnection connection) { } final Capability cap = connection.capability(getSupportedProtocol()); - final StatusMessage status = - StatusMessage.create( - cap.getVersion(), - networkId, - blockchain.getChainHead().getTotalDifficulty(), - blockchain.getChainHeadHash(), - forkId.getLatestForkId(cap.getVersion())); - try { - LOG.debug("Sending status message to {}.", peer); - peer.send(status); - peer.registerStatusSent(); - } catch (final PeerNotConnected peerNotConnected) { - // Nothing to do. + // TODO: look to consolidate code below if possible + if(cap.getVersion() > EthProtocol.EthVersion.V63){ + final StatusMessage status = + StatusMessage.create( + cap.getVersion(), + networkId, + blockchain.getChainHead().getTotalDifficulty(), + blockchain.getChainHeadHash(), + forkIdManager.getLatestForkId()); + try { + LOG.debug("Sending status message to {}.", peer); + peer.send(status); + peer.registerStatusSent(); + } catch (final PeerNotConnected peerNotConnected) { + // Nothing to do. + } + } else { + final StatusMessage status = + StatusMessage.create( + cap.getVersion(), + networkId, + blockchain.getChainHead().getTotalDifficulty(), + blockchain.getChainHeadHash(), + genesisHash); + try { + LOG.debug("Sending status message to {}.", peer); + peer.send(status); + peer.registerStatusSent(); + } catch (final PeerNotConnected peerNotConnected) { + // Nothing to do. + } } } @@ -327,7 +345,13 @@ private void handleStatusMessage(final EthPeer peer, final MessageData data) { if (!status.networkId().equals(networkId)) { LOG.debug("Disconnecting from peer with mismatched network id: {}", status.networkId()); peer.disconnect(DisconnectReason.SUBPROTOCOL_TRIGGERED); - } else if (forkId.peerCheck(status.genesisHash())) { + } else if (forkIdManager.peerCheck(status.forkId())) { + LOG.debug( + "Disconnecting from peer with matching network id ({}), but non-matching fork id: {}", + networkId, + status.forkId()); + peer.disconnect(DisconnectReason.SUBPROTOCOL_TRIGGERED); + } else if (forkIdManager.peerCheck(status.genesisHash())) { LOG.debug( "Disconnecting from peer with matching network id ({}), but non-matching genesis hash: {}", networkId, diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkId.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java similarity index 68% rename from ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkId.java rename to ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java index 617fff8cc55..75aa11c5564 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkId.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java @@ -19,6 +19,7 @@ import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.core.Hash; import org.hyperledger.besu.ethereum.eth.EthProtocol; +import org.hyperledger.besu.ethereum.eth.messages.StatusMessage; import org.hyperledger.besu.ethereum.p2p.rlpx.wire.Capability; import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput; import org.hyperledger.besu.ethereum.rlp.RLPInput; @@ -36,25 +37,19 @@ import java.util.Set; import java.util.zip.CRC32; -public class ForkId { +public class ForkIdManager { private Hash genesisHash; private CRC32 crc = new CRC32(); private Long currentHead; private Long forkNext; private Long highestKnownFork = 0L; - private ForkIdEntry lastKnownEntry; - private boolean useForkId; - private ArrayDeque forkAndHashList; + private ForkId lastKnownEntry; + private ArrayDeque forkAndHashList; - public ForkId( - final Hash genesisHash, - final Set forks, - final Long currentHead, - final boolean useForkId) { + public ForkIdManager(final Hash genesisHash, final Set forks, final Long currentHead) { this.genesisHash = genesisHash; this.currentHead = currentHead; - this.useForkId = useForkId; if (forks != null) { forkAndHashList = collectForksAndHashes(forks, currentHead); } else { @@ -62,63 +57,64 @@ public ForkId( } }; - public static ForkId buildCollection( + public static ForkIdManager buildCollection( final Hash genesisHash, final List forks, final Blockchain blockchain, final List caps) { if (forks == null) { - return new ForkId(genesisHash, null, blockchain.getChainHeadBlockNumber(), false); + return new ForkIdManager(genesisHash, null, blockchain.getChainHeadBlockNumber()); } else { Set forkSet = new LinkedHashSet<>(forks); - return new ForkId( - genesisHash, - forkSet, - blockchain.getChainHeadBlockNumber(), - caps.contains(Capability.create(EthProtocol.NAME, EthProtocol.EthVersion.V64))); + return new ForkIdManager(genesisHash, forkSet, blockchain.getChainHeadBlockNumber()); } }; - public static ForkId buildCollection(final Hash genesisHash, final List forks) { + public static ForkIdManager buildCollection(final Hash genesisHash, final List forks) { if (forks == null) { - return new ForkId(genesisHash, null, Long.MAX_VALUE, false); + return new ForkIdManager(genesisHash, null, Long.MAX_VALUE); } else { Set forkSet = new LinkedHashSet<>(forks); - return new ForkId(genesisHash, forkSet, Long.MAX_VALUE, true); + return new ForkIdManager(genesisHash, forkSet, Long.MAX_VALUE); } }; - public static ForkId buildCollection(final Hash genesisHash) { - return new ForkId(genesisHash, null, Long.MAX_VALUE, false); + public static ForkIdManager buildCollection(final Hash genesisHash) { + return new ForkIdManager(genesisHash, null, Long.MAX_VALUE); }; - public static ForkIdEntry readFrom(final RLPInput in) { + public static ForkId readFrom(final RLPInput in) { in.enterList(); - final String hash = in.readBytesValue(BytesValues::asString); + final BytesValue hash = in.readBytesValue(); final long next = in.readLong(); in.leaveList(); - return new ForkIdEntry(hash, next); + return new ForkId(hash, next); } // Non-RLP entry (for tests) - public static ForkIdEntry createIdEntry(final String hash, final long next) { - return new ForkIdEntry(hash, next); + public static ForkId createIdEntry(final String hash, final long next) { + return new ForkId(hash, next); } - public ArrayDeque getForkAndHashList() { + public ArrayDeque getForkAndHashList() { return this.forkAndHashList; } - public Hash getLatestForkId(final int protocolVersion) { + public ForkId getLatestForkId() { // TODO: implement handling for forkID in status message - if (useForkId && protocolVersion > 63) { - return Hash.fromHexString(lastKnownEntry.hash); - } else { - return genesisHash; - } + return lastKnownEntry; + } + + public boolean forkIdCapable() { + return forkAndHashList.size() != 0; } - public boolean peerCheck(final String forkHash, final Long peerNext) { + public boolean peerCheck(final ForkId forkId /* final String forkHash, final Long peerNext*/) { + System.out.println(forkId); // todo remove dev item + System.out.println(forkNext); // todo remove dev item + if (!forkIdCapable() || forkId == null) { + return false; + } // Run the fork checksum validation ruleset: // 1. If local and remote FORK_CSUM matches, connect. // The two nodes are in the same fork state currently. They might know @@ -135,12 +131,12 @@ public boolean peerCheck(final String forkHash, final Long peerNext) { // the remote, but at this current point in time we don't have enough // information. // 4. Reject in all other cases. - if (isHashKnown(forkHash)) { + if (isHashKnown(forkId.hash)) { if (currentHead < forkNext) { return true; } else { - if (isForkKnown(peerNext)) { - return isRemoteAwareOfPresent(forkHash, peerNext); + if (isForkKnown(forkId.next)) { + return isRemoteAwareOfPresent(forkId.hash, forkId.next); } else { return false; } @@ -157,11 +153,14 @@ public boolean peerCheck(final String forkHash, final Long peerNext) { * @return boolean */ public boolean peerCheck(final Bytes32 peerGenesisOrCheckSumHash) { + if (forkIdCapable()) { + return false; + } return !peerGenesisOrCheckSumHash.equals(genesisHash); } - private boolean isHashKnown(final String forkHash) { - for (ForkIdEntry j : forkAndHashList) { + private boolean isHashKnown(final BytesValue forkHash) { + for (ForkId j : forkAndHashList) { if (forkHash.equals(j.hash)) { return true; } @@ -173,7 +172,7 @@ private boolean isForkKnown(final Long nextFork) { if (highestKnownFork < nextFork) { return true; } - for (ForkIdEntry j : forkAndHashList) { + for (ForkId j : forkAndHashList) { if (nextFork.equals(j.next)) { return true; } @@ -181,8 +180,8 @@ private boolean isForkKnown(final Long nextFork) { return false; } - private boolean isRemoteAwareOfPresent(final String forkHash, final Long nextFork) { - for (ForkIdEntry j : forkAndHashList) { + private boolean isRemoteAwareOfPresent(final BytesValue forkHash, final Long nextFork) { + for (ForkId j : forkAndHashList) { if (forkHash.equals(j.hash)) { if (nextFork.equals(j.next)) { return true; @@ -196,10 +195,9 @@ private boolean isRemoteAwareOfPresent(final String forkHash, final Long nextFor return false; } - private ArrayDeque collectForksAndHashes( - final Set forks, final Long currentHead) { + private ArrayDeque collectForksAndHashes(final Set forks, final Long currentHead) { boolean first = true; - ArrayDeque forkList = new ArrayDeque<>(); + ArrayDeque forkList = new ArrayDeque<>(); Iterator iterator = forks.iterator(); while (iterator.hasNext()) { Long forkBlockNumber = iterator.next(); @@ -210,15 +208,14 @@ private ArrayDeque collectForksAndHashes( // first fork first = false; forkList.add( - new ForkIdEntry( - updateCrc(this.genesisHash.getHexString()), forkBlockNumber)); // Genesis + new ForkId(updateCrc(this.genesisHash.getHexString()), forkBlockNumber)); // Genesis updateCrc(forkBlockNumber); } else if (!iterator.hasNext()) { // most recent fork - forkList.add(new ForkIdEntry(getCurrentCrcHash(), forkBlockNumber)); + forkList.add(new ForkId(getCurrentCrcHash(), forkBlockNumber)); updateCrc(forkBlockNumber); - lastKnownEntry = new ForkIdEntry(getCurrentCrcHash(), 0L); + lastKnownEntry = new ForkId(getCurrentCrcHash(), 0L); forkList.add(lastKnownEntry); if (currentHead > forkBlockNumber) { this.forkNext = 0L; @@ -227,36 +224,47 @@ private ArrayDeque collectForksAndHashes( } } else { - forkList.add(new ForkIdEntry(getCurrentCrcHash(), forkBlockNumber)); + forkList.add(new ForkId(getCurrentCrcHash(), forkBlockNumber)); updateCrc(forkBlockNumber); } } return forkList; } - private String updateCrc(final Long block) { + private BytesValue updateCrc(final Long block) { byte[] byteRepresentationFork = longToBigEndian(block); crc.update(byteRepresentationFork, 0, byteRepresentationFork.length); return getCurrentCrcHash(); } - private String updateCrc(final String hash) { + private BytesValue updateCrc(final String hash) { byte[] byteRepresentation = hexStringToByteArray(hash); crc.update(byteRepresentation, 0, byteRepresentation.length); return getCurrentCrcHash(); } - public String getCurrentCrcHash() { - return "0x" + encodeHexString(BytesValues.ofUnsignedInt(crc.getValue()).getByteArray()); + public BytesValue getCurrentCrcHash() { + // return "0x" + encodeHexString(BytesValues.ofUnsignedInt(crc.getValue()).getByteArray()); + // System.out.println("0x" + + // encodeHexString(BytesValues.ofUnsignedInt(crc.getValue()).getByteArray())); // todo remove + // dev item + return BytesValues.ofUnsignedInt(crc.getValue()); } // TODO use Hash class instead of string for checksum. convert to or from string only when needed - public static class ForkIdEntry { - String hash; + // ^ the crc is not hashed/checksum-ed any further so the hash class is not suited for this case + public static class ForkId { + BytesValue hash; long next; BytesValue forkIdRLP; - public ForkIdEntry(final String hash, final long next) { + public ForkId(final String hash, final long next) { + this.hash = BytesValue.wrap(hexStringToByteArray(hash)); + this.next = next; + createForkIdRLP(); + } + + public ForkId(final BytesValue hash, final long next) { this.hash = hash; this.next = next; createForkIdRLP(); @@ -264,39 +272,47 @@ public ForkIdEntry(final String hash, final long next) { public void createForkIdRLP() { BytesValueRLPOutput out = new BytesValueRLPOutput(); - out.writeList(asList(), ForkId.ForkIdEntry::writeTo); + out.writeList(asList(), ForkId::writeTo); forkIdRLP = out.encoded(); } public void writeTo(final RLPOutput out) { out.startList(); - out.writeBytesValue(wrap(hash.getBytes(StandardCharsets.US_ASCII))); + out.writeBytesValue(hash); out.writeLong(next); out.endList(); } + public static ForkId readFrom(final RLPInput in) { + in.enterList(); + final BytesValue hash = in.readBytesValue(); + final long next = in.readLong(); + in.leaveList(); + return new ForkId(hash, next); + } + public List asByteList() { ArrayList forRLP = new ArrayList(); - forRLP.add(hexStringToByteArray(hash)); + forRLP.add(hash.getByteArray()); forRLP.add(longToBigEndian(next)); return forRLP; } - public List asList() { - ArrayList forRLP = new ArrayList<>(); + public List asList() { + ArrayList forRLP = new ArrayList<>(); forRLP.add(this); return forRLP; } @Override public String toString() { - return "IdEntry(hash=" + this.hash + ", next=" + this.next + ")"; + return "ForkId(hash=" + this.hash + ", next=" + this.next + ")"; } @Override public boolean equals(final Object obj) { - if (obj instanceof ForkIdEntry) { - ForkIdEntry other = (ForkIdEntry) obj; + if (obj instanceof ForkId) { + ForkId other = (ForkId) obj; return other.hash.equals(this.hash) && other.next == this.next; } return false; @@ -335,21 +351,6 @@ private static void intToBigEndian(final int n, final byte[] bs, int off) { bs[++off] = (byte) (n); } - private static String byteToHex(final byte num) { - char[] hexDigits = new char[2]; - hexDigits[0] = Character.forDigit((num >> 4) & 0xF, 16); - hexDigits[1] = Character.forDigit((num & 0xF), 16); - return new String(hexDigits); - } - - private static String encodeHexString(final byte[] byteArray) { - StringBuilder hexStringBuffer = new StringBuilder(); - for (int i = 0; i < byteArray.length; i++) { - hexStringBuffer.append(byteToHex(byteArray[i])); - } - return hexStringBuffer.toString(); - } - private static byte[] decodeHexString(final String hexString) { if (hexString.length() % 2 == 1) { throw new IllegalArgumentException("Invalid hexadecimal String supplied."); diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessage.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessage.java index b113945f05d..c6cf63639bf 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessage.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessage.java @@ -15,6 +15,7 @@ package org.hyperledger.besu.ethereum.eth.messages; import org.hyperledger.besu.ethereum.core.Hash; +import org.hyperledger.besu.ethereum.eth.manager.ForkIdManager; import org.hyperledger.besu.ethereum.p2p.rlpx.wire.AbstractMessageData; import org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData; import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput; @@ -49,6 +50,20 @@ public static StatusMessage create( return new StatusMessage(out.encoded()); } + public static StatusMessage create( + final int protocolVersion, + final BigInteger networkId, + final UInt256 totalDifficulty, + final Hash bestHash, + final ForkIdManager.ForkId forkId) { + final EthStatus status = + new EthStatus(protocolVersion, networkId, totalDifficulty, bestHash, forkId); + final BytesValueRLPOutput out = new BytesValueRLPOutput(); + status.writeTo(out); + + return new StatusMessage(out.encoded()); + } + public static StatusMessage readFrom(final MessageData message) { if (message instanceof StatusMessage) { return (StatusMessage) message; @@ -93,6 +108,13 @@ public Bytes32 genesisHash() { return status().genesisHash; } + /** + * @return The fork id of the network the associated node is participating in. + */ + public ForkIdManager.ForkId forkId() { + return status().forkId; + } + private EthStatus status() { if (status == null) { final RLPInput input = RLP.input(data); @@ -107,6 +129,7 @@ private static class EthStatus { private final UInt256 totalDifficulty; private final Hash bestHash; private final Hash genesisHash; + private final ForkIdManager.ForkId forkId; EthStatus( final int protocolVersion, @@ -119,6 +142,21 @@ private static class EthStatus { this.totalDifficulty = totalDifficulty; this.bestHash = bestHash; this.genesisHash = genesisHash; + this.forkId = null; + } + + EthStatus( + final int protocolVersion, + final BigInteger networkId, + final UInt256 totalDifficulty, + final Hash bestHash, + final ForkIdManager.ForkId forkHash) { + this.protocolVersion = protocolVersion; + this.networkId = networkId; + this.totalDifficulty = totalDifficulty; + this.bestHash = bestHash; + this.forkId = forkHash; + this.genesisHash = null; } public void writeTo(final RLPOutput out) { @@ -128,7 +166,11 @@ public void writeTo(final RLPOutput out) { out.writeBigIntegerScalar(networkId); out.writeUInt256Scalar(totalDifficulty); out.writeBytesValue(bestHash); - out.writeBytesValue(genesisHash); + if (forkId != null) { + forkId.writeTo(out); + } else { + out.writeBytesValue(genesisHash); + } out.endList(); } @@ -140,11 +182,18 @@ public static EthStatus readFrom(final RLPInput in) { final BigInteger networkId = in.readBigIntegerScalar(); final UInt256 totalDifficulty = in.readUInt256Scalar(); final Hash bestHash = Hash.wrap(in.readBytes32()); - final Hash genesisHash = Hash.wrap(in.readBytes32()); + if (in.nextIsList()) { + final ForkIdManager.ForkId forkId = ForkIdManager.ForkId.readFrom(in); + in.leaveList(); + + return new EthStatus(protocolVersion, networkId, totalDifficulty, bestHash, forkId); + } else { + final Hash genesisHash = Hash.wrap(in.readBytes32()); - in.leaveList(); + in.leaveList(); - return new EthStatus(protocolVersion, networkId, totalDifficulty, bestHash, genesisHash); + return new EthStatus(protocolVersion, networkId, totalDifficulty, bestHash, genesisHash); + } } } } diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java similarity index 61% rename from ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdTest.java rename to ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java index 60c481b4113..33c42c49823 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java @@ -29,34 +29,33 @@ import org.junit.Test; -public class ForkIdTest { +public class ForkIdManagerTest { private Long[] forksMainnet = {1150000L, 1920000L, 2463000L, 2675000L, 4370000L, 7280000L}; private String mainnetGenHash = "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"; @Test public void checkItFunctionsWithPresentBehavior() { - ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), null, null, false); - assertThat(forkId.peerCheck(Hash.fromHexString(mainnetGenHash))).isFalse(); + ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), null, null); + assertThat(forkIdManager.peerCheck(Hash.fromHexString(mainnetGenHash))).isFalse(); } @Test public void checkCorrectMainnetForkIdHashesGenerated() { - ForkId.ForkIdEntry[] checkIds = { - ForkId.createIdEntry("0xfc64ec04", 1150000L), // Unsynced - ForkId.createIdEntry("0x97c2c34c", 1920000L), // First Homestead block - ForkId.createIdEntry("0x91d1f948", 2463000L), // First DAO block - ForkId.createIdEntry("0x7a64da13", 2675000L), // First Tangerine block - ForkId.createIdEntry("0x3edd5b10", 4370000L), // First Spurious block - ForkId.createIdEntry("0xa00bc324", 7280000L), // First Byzantium block - ForkId.createIdEntry("0x668db0af", 0L) // Today Petersburg block + ForkIdManager.ForkId[] checkIds = { + ForkIdManager.createIdEntry("0xfc64ec04", 1150000L), // Unsynced + ForkIdManager.createIdEntry("0x97c2c34c", 1920000L), // First Homestead block + ForkIdManager.createIdEntry("0x91d1f948", 2463000L), // First DAO block + ForkIdManager.createIdEntry("0x7a64da13", 2675000L), // First Tangerine block + ForkIdManager.createIdEntry("0x3edd5b10", 4370000L), // First Spurious block + ForkIdManager.createIdEntry("0xa00bc324", 7280000L), // First Byzantium block + ForkIdManager.createIdEntry("0x668db0af", 0L) // Today Petersburg block }; List list = Arrays.asList(forksMainnet); - ForkId forkId = ForkId.buildCollection(Hash.fromHexString(mainnetGenHash), list); - ArrayDeque entries = forkId.getForkAndHashList(); - - for (ForkId.ForkIdEntry id : checkIds) { - ForkId.ForkIdEntry testVal = entries.poll(); + ForkIdManager forkIdManager = ForkIdManager.buildCollection(Hash.fromHexString(mainnetGenHash), list); + ArrayDeque entries = forkIdManager.getForkAndHashList(); + for (ForkIdManager.ForkId id : checkIds) { + ForkIdManager.ForkId testVal = entries.poll(); if (testVal == null) { break; } @@ -68,20 +67,20 @@ public void checkCorrectMainnetForkIdHashesGenerated() { public void checkCorrectRopstenForkIdHashesGenerated() { Long[] forks = {10L, 1700000L, 4230000L, 4939394L}; String genHash = "0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d"; - ForkId.ForkIdEntry[] checkIds = { - ForkId.createIdEntry( + ForkIdManager.ForkId[] checkIds = { + ForkIdManager.createIdEntry( "0x30c7ddbc", 10L), // Unsynced, last Frontier, Homestead and first Tangerine block - ForkId.createIdEntry("0x63760190", 1700000L), // First Spurious block - ForkId.createIdEntry("0x3ea159c7", 4230000L), // First Byzantium block - ForkId.createIdEntry("0x97b544f3", 4939394L), // First Constantinople block - ForkId.createIdEntry("0xd6e2149b", 0L) // Today Petersburg block + ForkIdManager.createIdEntry("0x63760190", 1700000L), // First Spurious block + ForkIdManager.createIdEntry("0x3ea159c7", 4230000L), // First Byzantium block + ForkIdManager.createIdEntry("0x97b544f3", 4939394L), // First Constantinople block + ForkIdManager.createIdEntry("0xd6e2149b", 0L) // Today Petersburg block }; List list = Arrays.asList(forks); - ForkId forkId = ForkId.buildCollection(Hash.fromHexString(genHash), list); - ArrayDeque entries = forkId.getForkAndHashList(); + ForkIdManager forkIdManager = ForkIdManager.buildCollection(Hash.fromHexString(genHash), list); + ArrayDeque entries = forkIdManager.getForkAndHashList(); - for (ForkId.ForkIdEntry id : checkIds) { - ForkId.ForkIdEntry testVal = entries.poll(); + for (ForkIdManager.ForkId id : checkIds) { + ForkIdManager.ForkId testVal = entries.poll(); if (testVal == null) { break; } @@ -93,22 +92,22 @@ public void checkCorrectRopstenForkIdHashesGenerated() { public void checkCorrectRinkebyForkIdHashesGenerated() { Long[] forks = {1L, 2L, 3L, 1035301L, 3660663L, 4321234L}; String genHash = "0x6341fd3daf94b748c72ced5a5b26028f2474f5f00d824504e4fa37a75767e177"; - ForkId.ForkIdEntry[] checkIds = { - ForkId.createIdEntry( + ForkIdManager.ForkId[] checkIds = { + ForkIdManager.createIdEntry( "0x3b8e0691", 1L), // Unsynced, last Frontier, Homestead and first Tangerine block - ForkId.createIdEntry("0x60949295", 2L), // Last Tangerine block - ForkId.createIdEntry("0x8bde40dd", 3L), // First Spurious block - ForkId.createIdEntry("0xcb3a64bb", 1035301L), // First Byzantium block - ForkId.createIdEntry("0x8d748b57", 3660663L), // First Constantinople block - ForkId.createIdEntry("0xe49cab14", 4321234L), // First Petersburg block - ForkId.createIdEntry("0xafec6b27", 0L) // Today Petersburg block + ForkIdManager.createIdEntry("0x60949295", 2L), // Last Tangerine block + ForkIdManager.createIdEntry("0x8bde40dd", 3L), // First Spurious block + ForkIdManager.createIdEntry("0xcb3a64bb", 1035301L), // First Byzantium block + ForkIdManager.createIdEntry("0x8d748b57", 3660663L), // First Constantinople block + ForkIdManager.createIdEntry("0xe49cab14", 4321234L), // First Petersburg block + ForkIdManager.createIdEntry("0xafec6b27", 0L) // Today Petersburg block }; List list = Arrays.asList(forks); - ForkId forkId = ForkId.buildCollection(Hash.fromHexString(genHash), list); - ArrayDeque entries = forkId.getForkAndHashList(); + ForkIdManager forkIdManager = ForkIdManager.buildCollection(Hash.fromHexString(genHash), list); + ArrayDeque entries = forkIdManager.getForkAndHashList(); - for (ForkId.ForkIdEntry id : checkIds) { - ForkId.ForkIdEntry testVal = entries.poll(); + for (ForkIdManager.ForkId id : checkIds) { + ForkIdManager.ForkId testVal = entries.poll(); if (testVal == null) { break; } @@ -122,8 +121,8 @@ public void check1PetersburgWithRemoteAnnouncingTheSame() { // {7987396, ID{Hash: 0x668db0af, Next: 0}, nil}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L, true); - Boolean result = forkId.peerCheck("0x668db0af", 0L); + ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0x668db0af", 0L)); assertThat(result).isTrue(); } @@ -134,8 +133,8 @@ public void check2PetersburgWithRemoteAnnouncingTheSameAndNextFork() { // {7987396, ID{Hash: 0x668db0af, Next: math.MaxUint64}, nil}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L, true); - Boolean result = forkId.peerCheck("0x668db0af", Long.MAX_VALUE); + ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0x668db0af", Long.MAX_VALUE)); assertThat(result).isTrue(); } @@ -148,8 +147,8 @@ public void check3ByzantiumAwareOfPetersburgRemoteUnawareOfPetersburg() { // {7279999, ID{Hash: 0xa00bc324, Next: 0}, nil}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L, true); - Boolean result = forkId.peerCheck("0xa00bc324", 0L); + ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); + Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0xa00bc324", 0L)); assertThat(result).isTrue(); } @@ -161,8 +160,8 @@ public void check4ByzantiumAwareOfPetersburgRemoteAwareOfPetersburg() { // {7279999, ID{Hash: 0xa00bc324, Next: 7280000}, nil}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L, true); - Boolean result = forkId.peerCheck("0xa00bc324", 7280000L); + ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); + Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0xa00bc324", 7280000L)); assertThat(result).isTrue(); } @@ -175,8 +174,8 @@ public void check5ByzantiumAwareOfPetersburgRemoteAnnouncingUnknownFork() { // {7279999, ID{Hash: 0xa00bc324, Next: math.MaxUint64}, nil}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L, true); - Boolean result = forkId.peerCheck("0xa00bc324", Long.MAX_VALUE); + ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); + Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0xa00bc324", Long.MAX_VALUE)); assertThat(result).isTrue(); } @@ -187,8 +186,8 @@ public void check6PetersburgWithRemoteAnnouncingByzantiumAwareOfPetersburg() { // {7987396, ID{Hash: 0x668db0af, Next: 7280000}, nil}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L, true); - Boolean result = forkId.peerCheck("0x668db0af", 7280000L); + ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0x668db0af", 7280000L)); assertThat(result).isTrue(); } @@ -200,8 +199,8 @@ public void check7PetersburgWithRemoteAnnouncingSpuriousAwareOfByzantiumRemoteMa // {7987396, ID{Hash: 0x3edd5b10, Next: 4370000}, nil}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L, true); - Boolean result = forkId.peerCheck("0x3edd5b10", 4370000L); + ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0x3edd5b10", 4370000L)); assertThat(result).isTrue(); } @@ -211,8 +210,8 @@ public void check8ByzantiumWithRemoteAnnouncingPetersburgLocalOutOfSync() { // {7279999, ID{Hash: 0x668db0af, Next: 0}, nil}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L, true); - Boolean result = forkId.peerCheck("0x668db0af", 0L); + ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); + Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0x668db0af", 0L)); assertThat(result).isTrue(); } @@ -223,8 +222,8 @@ public void check9SpuriousWithRemoteAnnouncingByzantiumRemoteUnawareOfPetersburg // {4369999, ID{Hash: 0xa00bc324, Next: 0}, nil}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 4369999L, true); - Boolean result = forkId.peerCheck("0xa00bc324", 0L); + ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 4369999L); + Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0xa00bc324", 0L)); assertThat(result).isTrue(); } @@ -235,8 +234,8 @@ public void check10PetersburgWithRemoteAnnouncingByzantiumRemoteUnawareOfAdditio // {7987396, ID{Hash: 0xa00bc324, Next: 0}, ErrRemoteStale}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L, true); - Boolean result = forkId.peerCheck("0xa00bc324", 0L); + ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0xa00bc324", 0L)); assertThat(result).isFalse(); } @@ -247,8 +246,8 @@ public void check11PetersburgWithRemoteAnnouncingPetersburgAndFutureForkLocalNee // {7987396, ID{Hash: 0x5cddc0e1, Next: 0}, ErrLocalIncompatibleOrStale}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L, true); - Boolean result = forkId.peerCheck("0x5cddc0e1", 0L); + ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0x5cddc0e1", 0L)); assertThat(result).isFalse(); } @@ -259,8 +258,8 @@ public void check12ByzantiumWithRemoteAnnouncingPetersburgAndFutureForkLocalNeed // {7279999, ID{Hash: 0x5cddc0e1, Next: 0}, ErrLocalIncompatibleOrStale}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L, true); - Boolean result = forkId.peerCheck("0x5cddc0e1", 0L); + ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); + Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0x5cddc0e1", 0L)); assertThat(result).isFalse(); } @@ -270,20 +269,20 @@ public void check13ByzantiumWithRemoteAnnouncingRinkebyPetersburg() { // {7987396, ID{Hash: 0xafec6b27, Next: 0}, ErrLocalIncompatibleOrStale}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkId forkId = new ForkId(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L, true); - Boolean result = forkId.peerCheck("0xafec6b27", 0L); + ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0xafec6b27", 0L)); assertThat(result).isFalse(); } @Test public void createAndDecodeRLP() { - ForkId.ForkIdEntry forkIdEntry = ForkId.createIdEntry("0xa00bc324", 7280000L); + ForkIdManager.ForkId forkIdEntry = ForkIdManager.createIdEntry("0xa00bc324", 7280000L); BytesValueRLPOutput out = new BytesValueRLPOutput(); - out.writeList(forkIdEntry.asList(), ForkId.ForkIdEntry::writeTo); + out.writeList(forkIdEntry.asList(), ForkIdManager.ForkId::writeTo); BytesValue bytesValue = out.encoded(); BytesValueRLPInput in = new BytesValueRLPInput(bytesValue, false); - List forkId = in.readList(ForkId::readFrom); - ForkId.ForkIdEntry decodedEntry = forkId.get(0); + List forkId = in.readList(ForkIdManager::readFrom); + ForkIdManager.ForkId decodedEntry = forkId.get(0); assertThat(forkIdEntry.equals(decodedEntry)).isTrue(); } } diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessageTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessageTest.java index b243712385a..36f4c4f0f8b 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessageTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessageTest.java @@ -18,6 +18,7 @@ import org.hyperledger.besu.ethereum.core.Hash; import org.hyperledger.besu.ethereum.eth.EthProtocol; +import org.hyperledger.besu.ethereum.eth.manager.ForkIdManager; import org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData; import org.hyperledger.besu.util.bytes.Bytes32; import org.hyperledger.besu.util.uint.UInt256; @@ -66,6 +67,26 @@ public void serializeDeserialize() { assertThat(copy.genesisHash()).isEqualTo(genesisHash); } + @Test + public void serializeDeserializeWithForkId() { + final int version = EthProtocol.EthVersion.V64; + final BigInteger networkId = BigInteger.ONE; + final UInt256 td = UInt256.of(1000L); + final Hash bestHash = randHash(1L); + final ForkIdManager.ForkId forkId = ForkIdManager.createIdEntry("0xa00bc334", 0L); + + final MessageData msg = StatusMessage.create(version, networkId, td, bestHash, forkId); + + // Make a message copy from serialized data and check deserialized results + final StatusMessage copy = new StatusMessage(msg.getData()); + + assertThat(copy.protocolVersion()).isEqualTo(version); + assertThat(copy.networkId()).isEqualTo(networkId); + assertThat(copy.totalDifficulty()).isEqualTo(td); + assertThat(copy.bestHash()).isEqualTo(bestHash); + assertThat(copy.forkId()).isEqualTo(forkId); + } + private Hash randHash(final long seed) { final Random random = new Random(seed); final byte[] bytes = new byte[32]; From 27c42ffdeefb16c3a8c7d90229703707e029f6df Mon Sep 17 00:00:00 2001 From: SteveM Date: Thu, 28 Nov 2019 18:45:08 -0800 Subject: [PATCH 13/42] include forkId in statusMessage class, move fork collection to genesisConfigFile class, rename forkId to forkIdManager and inner class to forkId Signed-off-by: SteveM --- .../controller/BesuControllerBuilder.java | 15 +-------------- .../besu/config/GenesisConfigFile.java | 19 +++++++++++++++++++ .../besu/config/GenesisConfigFileTest.java | 13 +++++++++++++ .../eth/manager/EthProtocolManager.java | 1 + .../ethereum/eth/manager/ForkIdManager.java | 2 ++ 5 files changed, 36 insertions(+), 14 deletions(-) diff --git a/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java b/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java index d9e02208f38..90b72a183cc 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java @@ -356,7 +356,7 @@ protected EthProtocolManager createEthProtocolManager( clock, metricsSystem, ethereumWireProtocolConfiguration, - gatherForks()); + genesisConfig.getForks()); } private List createPeerValidators(final ProtocolSchedule protocolSchedule) { @@ -390,17 +390,4 @@ private List createPeerValidators(final ProtocolSchedule proto protected abstract PluginServiceFactory createAdditionalPluginServices( final Blockchain blockchain); - private List gatherForks() { - // todo: may need to check what protocol version is being used. (i.e. if less than x return null - // to disable this check) - List listb = new ArrayList<>(); - Map values = genesisConfig.getConfigOptions(genesisConfigOverrides).asMap(); - values.forEach( - (x, y) -> { - if (y instanceof Long && !x.equals("chainId")) { - listb.add((Long) y); - } - }); - return listb; - } } diff --git a/config/src/main/java/org/hyperledger/besu/config/GenesisConfigFile.java b/config/src/main/java/org/hyperledger/besu/config/GenesisConfigFile.java index 61d5734c37e..089b850385d 100644 --- a/config/src/main/java/org/hyperledger/besu/config/GenesisConfigFile.java +++ b/config/src/main/java/org/hyperledger/besu/config/GenesisConfigFile.java @@ -15,14 +15,20 @@ package org.hyperledger.besu.config; import static java.nio.charset.StandardCharsets.UTF_8; +import static java.util.stream.Collectors.toList; +import static java.util.stream.Collectors.toSet; import static org.hyperledger.besu.config.JsonUtil.normalizeKeys; import java.io.IOException; +import java.util.ArrayList; import java.util.Collections; +import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Optional; import java.util.stream.Stream; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ObjectNode; import com.google.common.collect.Streams; import com.google.common.io.Resources; @@ -131,6 +137,19 @@ public long getTimestamp() { return parseLong("timestamp", JsonUtil.getValueAsString(configRoot, "timestamp", "0x0")); } + // TODO look into how to handle invalid or exceptional (i.e. null or "") fork values + public List getForks() { + return JsonUtil.getObjectNode(configRoot, "config").stream() + .flatMap(node -> + Streams.stream(node.fieldNames()) + .filter(name -> !name.toLowerCase().equals("chainid")) + .filter(name -> node.get(name).canConvertToLong()) + .filter(name -> name.contains("block")) + .map(name -> node.get(name).asLong()) + ) + .collect(toList()); + } + private String getRequiredString(final String key) { if (!configRoot.has(key)) { throw new IllegalArgumentException( diff --git a/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java b/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java index 28fce6b02e1..e93906db2ed 100644 --- a/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java +++ b/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java @@ -16,6 +16,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.InstanceOfAssertFactories.LIST; import java.math.BigInteger; import java.util.HashMap; @@ -228,6 +229,7 @@ public void testOverridePresent() { override.put("chainId", bigBlockString); override.put("contractSizeLimit", bigBlockString); + assertThat(config.getForks().size()).isPositive(); assertThat(config.getConfigOptions(override).getIstanbulBlockNumber()).hasValue(bigBlock); assertThat(config.getConfigOptions(override).getChainId()) .hasValue(BigInteger.valueOf(bigBlock)); @@ -242,6 +244,7 @@ public void testOverrideNull() { override.put("chainId", null); override.put("contractSizeLimit", null); + assertThat(config.getForks().size()).isPositive(); assertThat(config.getConfigOptions(override).getIstanbulBlockNumber()).isNotPresent(); assertThat(config.getConfigOptions(override).getChainId()).isNotPresent(); assertThat(config.getConfigOptions(override).getContractSizeLimit()).isNotPresent(); @@ -290,6 +293,14 @@ public void testNoOverride() { assertThat(config.getConfigOptions().getEvmStackSize()).isNotPresent(); } + @Test + public void shouldLoadMainnetForks() { + final GenesisConfigFile config = GenesisConfigFile.mainnet(); + + assertThat(config.getForks()).contains(1150000L, 1920000L, 2463000L, 2675000L, 2675000L, 4370000L, 7280000L, 9069000L); + assertThat(config.getConfigOptions().getChainId()).hasValue(MAINNET_CHAIN_ID); + } + private GenesisConfigFile configWithProperty(final String key, final String value) { return GenesisConfigFile.fromConfig("{\"" + key + "\":\"" + value + "\"}"); } @@ -299,4 +310,6 @@ private void assertInvalidConfiguration(final ThrowingCallable getter) { .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("Invalid genesis block configuration"); } + + } diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java index be922e1ef3c..c295b19dc7d 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java @@ -285,6 +285,7 @@ public void handleNewConnection(final PeerConnection connection) { final Capability cap = connection.capability(getSupportedProtocol()); // TODO: look to consolidate code below if possible + // making status non-final and implementing it above would be one way. if(cap.getVersion() > EthProtocol.EthVersion.V63){ final StatusMessage status = StatusMessage.create( diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java index 75aa11c5564..08cb233cc97 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java @@ -50,11 +50,13 @@ public class ForkIdManager { public ForkIdManager(final Hash genesisHash, final Set forks, final Long currentHead) { this.genesisHash = genesisHash; this.currentHead = currentHead; + System.out.println(currentHead); // todo remove dev item if (forks != null) { forkAndHashList = collectForksAndHashes(forks, currentHead); } else { forkAndHashList = new ArrayDeque<>(); } + System.out.println(forkAndHashList); // todo remove dev item }; public static ForkIdManager buildCollection( From fb9cde1958632537491ed9cbe5419e76c4d9eaf3 Mon Sep 17 00:00:00 2001 From: SteveM Date: Thu, 28 Nov 2019 23:01:24 -0800 Subject: [PATCH 14/42] clean up unused imports and remove some comments related to forkId Signed-off-by: SteveM --- .../controller/BesuControllerBuilder.java | 1 - .../besu/config/GenesisConfigFileTest.java | 3 +- .../eth/manager/EthProtocolManager.java | 53 ++++++++++--------- .../ethereum/eth/manager/ForkIdManager.java | 26 +++++---- .../eth/messages/StatusMessageTest.java | 1 - 5 files changed, 40 insertions(+), 44 deletions(-) diff --git a/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java b/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java index 90b72a183cc..1d561c6e31d 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java @@ -389,5 +389,4 @@ private List createPeerValidators(final ProtocolSchedule proto protected abstract PluginServiceFactory createAdditionalPluginServices( final Blockchain blockchain); - } diff --git a/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java b/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java index e93906db2ed..d8f07769549 100644 --- a/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java +++ b/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java @@ -16,7 +16,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.assertj.core.api.InstanceOfAssertFactories.LIST; import java.math.BigInteger; import java.util.HashMap; @@ -297,7 +296,7 @@ public void testNoOverride() { public void shouldLoadMainnetForks() { final GenesisConfigFile config = GenesisConfigFile.mainnet(); - assertThat(config.getForks()).contains(1150000L, 1920000L, 2463000L, 2675000L, 2675000L, 4370000L, 7280000L, 9069000L); + assertThat(config.getForks()).contains(1150000L, 1920000L, 2463000L, 2675000L, 2675000L, 4370000L, 7280000L); assertThat(config.getConfigOptions().getChainId()).hasValue(MAINNET_CHAIN_ID); } diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java index c295b19dc7d..acd9ea8b932 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java @@ -178,8 +178,8 @@ public EthProtocolManager( this.shutdown = new CountDownLatch(1); genesisHash = blockchain.getBlockHashByNumber(0L).get(); - // todo: check about modification of the above and related constructor - forkIdManager = ForkIdManager.buildCollection(genesisHash, forks, blockchain, getSupportedCapabilities()); + forkIdManager = + ForkIdManager.buildCollection(genesisHash, forks, blockchain, getSupportedCapabilities()); ethPeers = new EthPeers(getSupportedProtocol(), clock, metricsSystem); ethMessages = new EthMessages(); @@ -286,14 +286,14 @@ public void handleNewConnection(final PeerConnection connection) { final Capability cap = connection.capability(getSupportedProtocol()); // TODO: look to consolidate code below if possible // making status non-final and implementing it above would be one way. - if(cap.getVersion() > EthProtocol.EthVersion.V63){ + if (cap.getVersion() > EthProtocol.EthVersion.V63) { final StatusMessage status = - StatusMessage.create( - cap.getVersion(), - networkId, - blockchain.getChainHead().getTotalDifficulty(), - blockchain.getChainHeadHash(), - forkIdManager.getLatestForkId()); + StatusMessage.create( + cap.getVersion(), + networkId, + blockchain.getChainHead().getTotalDifficulty(), + blockchain.getChainHeadHash(), + forkIdManager.getLatestForkId()); try { LOG.debug("Sending status message to {}.", peer); peer.send(status); @@ -303,12 +303,12 @@ public void handleNewConnection(final PeerConnection connection) { } } else { final StatusMessage status = - StatusMessage.create( - cap.getVersion(), - networkId, - blockchain.getChainHead().getTotalDifficulty(), - blockchain.getChainHeadHash(), - genesisHash); + StatusMessage.create( + cap.getVersion(), + networkId, + blockchain.getChainHead().getTotalDifficulty(), + blockchain.getChainHeadHash(), + genesisHash); try { LOG.debug("Sending status message to {}.", peer); peer.send(status); @@ -346,17 +346,18 @@ private void handleStatusMessage(final EthPeer peer, final MessageData data) { if (!status.networkId().equals(networkId)) { LOG.debug("Disconnecting from peer with mismatched network id: {}", status.networkId()); peer.disconnect(DisconnectReason.SUBPROTOCOL_TRIGGERED); - } else if (forkIdManager.peerCheck(status.forkId())) { - LOG.debug( - "Disconnecting from peer with matching network id ({}), but non-matching fork id: {}", - networkId, - status.forkId()); - peer.disconnect(DisconnectReason.SUBPROTOCOL_TRIGGERED); - } else if (forkIdManager.peerCheck(status.genesisHash())) { - LOG.debug( - "Disconnecting from peer with matching network id ({}), but non-matching genesis hash: {}", - networkId, - status.genesisHash()); + } else if (forkIdManager.peerCheck(status)) { + if (status.genesisHash() != null) { + LOG.debug( + "Disconnecting from peer with matching network id ({}), but non-matching genesis hash: {}", + networkId, + status.genesisHash()); + } else { + LOG.debug( + "Disconnecting from peer with matching network id ({}), but non-matching fork id: {}", + networkId, + status.forkId()); + } peer.disconnect(DisconnectReason.SUBPROTOCOL_TRIGGERED); } else { LOG.debug("Received status message from {}: {}", peer, status); diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java index 08cb233cc97..47773188bdf 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java @@ -14,11 +14,8 @@ */ package org.hyperledger.besu.ethereum.eth.manager; -import static org.hyperledger.besu.util.bytes.BytesValue.wrap; - import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.core.Hash; -import org.hyperledger.besu.ethereum.eth.EthProtocol; import org.hyperledger.besu.ethereum.eth.messages.StatusMessage; import org.hyperledger.besu.ethereum.p2p.rlpx.wire.Capability; import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput; @@ -28,7 +25,6 @@ import org.hyperledger.besu.util.bytes.BytesValue; import org.hyperledger.besu.util.bytes.BytesValues; -import java.nio.charset.StandardCharsets; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Iterator; @@ -50,13 +46,11 @@ public class ForkIdManager { public ForkIdManager(final Hash genesisHash, final Set forks, final Long currentHead) { this.genesisHash = genesisHash; this.currentHead = currentHead; - System.out.println(currentHead); // todo remove dev item if (forks != null) { forkAndHashList = collectForksAndHashes(forks, currentHead); } else { forkAndHashList = new ArrayDeque<>(); } - System.out.println(forkAndHashList); // todo remove dev item }; public static ForkIdManager buildCollection( @@ -103,7 +97,6 @@ public ArrayDeque getForkAndHashList() { } public ForkId getLatestForkId() { - // TODO: implement handling for forkID in status message return lastKnownEntry; } @@ -111,10 +104,18 @@ public boolean forkIdCapable() { return forkAndHashList.size() != 0; } - public boolean peerCheck(final ForkId forkId /* final String forkHash, final Long peerNext*/) { - System.out.println(forkId); // todo remove dev item - System.out.println(forkNext); // todo remove dev item - if (!forkIdCapable() || forkId == null) { + public boolean peerCheck(final StatusMessage status) { + if (status.forkId() == null && status.genesisHash() != null) { + return peerCheck(status.genesisHash()); + } else if (status.forkId() != null) { + return peerCheck(status.forkId()); + } else { + return false; + } + } + + public boolean peerCheck(final ForkId forkId) { + if (forkId == null) { return false; } // Run the fork checksum validation ruleset: @@ -155,9 +156,6 @@ public boolean peerCheck(final ForkId forkId /* final String forkHash, final Lon * @return boolean */ public boolean peerCheck(final Bytes32 peerGenesisOrCheckSumHash) { - if (forkIdCapable()) { - return false; - } return !peerGenesisOrCheckSumHash.equals(genesisHash); } diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessageTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessageTest.java index 36f4c4f0f8b..93b24722c64 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessageTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessageTest.java @@ -77,7 +77,6 @@ public void serializeDeserializeWithForkId() { final MessageData msg = StatusMessage.create(version, networkId, td, bestHash, forkId); - // Make a message copy from serialized data and check deserialized results final StatusMessage copy = new StatusMessage(msg.getData()); assertThat(copy.protocolVersion()).isEqualTo(version); From cbef3e940bc827aa25e4474e8237cc73095d0360 Mon Sep 17 00:00:00 2001 From: SteveM Date: Fri, 29 Nov 2019 21:42:55 -0800 Subject: [PATCH 15/42] eip-2124 RLP encoding checks pass, store next block number as bytesValue Signed-off-by: SteveM --- .../eth/manager/EthProtocolManager.java | 67 +++----- .../ethereum/eth/manager/ForkIdManager.java | 160 ++++++++++++++---- .../ethereum/eth/messages/StatusMessage.java | 27 ++- .../eth/manager/ForkIdManagerTest.java | 118 ++++++++++--- .../eth/messages/StatusMessageTest.java | 4 +- 5 files changed, 267 insertions(+), 109 deletions(-) diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java index acd9ea8b932..c0eeeb0160c 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java @@ -286,36 +286,20 @@ public void handleNewConnection(final PeerConnection connection) { final Capability cap = connection.capability(getSupportedProtocol()); // TODO: look to consolidate code below if possible // making status non-final and implementing it above would be one way. - if (cap.getVersion() > EthProtocol.EthVersion.V63) { - final StatusMessage status = - StatusMessage.create( - cap.getVersion(), - networkId, - blockchain.getChainHead().getTotalDifficulty(), - blockchain.getChainHeadHash(), - forkIdManager.getLatestForkId()); - try { - LOG.debug("Sending status message to {}.", peer); - peer.send(status); - peer.registerStatusSent(); - } catch (final PeerNotConnected peerNotConnected) { - // Nothing to do. - } - } else { - final StatusMessage status = - StatusMessage.create( - cap.getVersion(), - networkId, - blockchain.getChainHead().getTotalDifficulty(), - blockchain.getChainHeadHash(), - genesisHash); - try { - LOG.debug("Sending status message to {}.", peer); - peer.send(status); - peer.registerStatusSent(); - } catch (final PeerNotConnected peerNotConnected) { - // Nothing to do. - } + final StatusMessage status = + StatusMessage.create( + cap.getVersion(), + networkId, + blockchain.getChainHead().getTotalDifficulty(), + blockchain.getChainHeadHash(), + genesisHash, + forkIdManager.getLatestForkId()); + try { + LOG.debug("Sending status message to {}.", peer); + peer.send(status); + peer.registerStatusSent(); + } catch (final PeerNotConnected peerNotConnected) { + // Nothing to do. } } @@ -346,18 +330,17 @@ private void handleStatusMessage(final EthPeer peer, final MessageData data) { if (!status.networkId().equals(networkId)) { LOG.debug("Disconnecting from peer with mismatched network id: {}", status.networkId()); peer.disconnect(DisconnectReason.SUBPROTOCOL_TRIGGERED); - } else if (forkIdManager.peerCheck(status)) { - if (status.genesisHash() != null) { - LOG.debug( - "Disconnecting from peer with matching network id ({}), but non-matching genesis hash: {}", - networkId, - status.genesisHash()); - } else { - LOG.debug( - "Disconnecting from peer with matching network id ({}), but non-matching fork id: {}", - networkId, - status.forkId()); - } + } else if (forkIdManager.peerCheck(status.forkId())) { + LOG.debug( + "Disconnecting from peer with matching network id ({}), but non-matching fork id: {}", + networkId, + status.forkId()); + peer.disconnect(DisconnectReason.SUBPROTOCOL_TRIGGERED); + } else if (forkIdManager.peerCheck(status.genesisHash())) { + LOG.debug( + "Disconnecting from peer with matching network id ({}), but non-matching genesis hash: {}", + networkId, + status.genesisHash()); peer.disconnect(DisconnectReason.SUBPROTOCOL_TRIGGERED); } else { LOG.debug("Received status message from {}: {}", peer, status); diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java index 47773188bdf..b10da36c45f 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java @@ -16,7 +16,6 @@ import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.core.Hash; -import org.hyperledger.besu.ethereum.eth.messages.StatusMessage; import org.hyperledger.besu.ethereum.p2p.rlpx.wire.Capability; import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput; import org.hyperledger.besu.ethereum.rlp.RLPInput; @@ -33,6 +32,8 @@ import java.util.Set; import java.util.zip.CRC32; +import static java.nio.charset.StandardCharsets.UTF_8; + public class ForkIdManager { private Hash genesisHash; @@ -40,7 +41,7 @@ public class ForkIdManager { private Long currentHead; private Long forkNext; private Long highestKnownFork = 0L; - private ForkId lastKnownEntry; + private ForkId lastKnownEntry = null; private ArrayDeque forkAndHashList; public ForkIdManager(final Hash genesisHash, final Set forks, final Long currentHead) { @@ -82,14 +83,71 @@ public static ForkIdManager buildCollection(final Hash genesisHash) { public static ForkId readFrom(final RLPInput in) { in.enterList(); final BytesValue hash = in.readBytesValue(); - final long next = in.readLong(); + final BytesValue next = in.readBytesValue(); +// final long next = in.readLong(); in.leaveList(); return new ForkId(hash, next); } +// public static ForkId readFrom(final RLPInput in) { +// in.enterList(); +// final BytesValue hash = in.readBytesValue(); +// final long next = in.readLong(); +// in.leaveList(); +// return new ForkId(hash, next); +// } + // Non-RLP entry (for tests) public static ForkId createIdEntry(final String hash, final long next) { - return new ForkId(hash, next); + return new ForkId(BytesValue.wrap(hexStringToByteArray(hash)), BytesValue.wrap(longToBigEndian(next))); + } + + public static ForkId createIdEntry(final String hash, final String next) { + BytesValue bHash; + System.out.print("String hash: "); // todo remove dev item + System.out.print(hash); // todo remove dev item + System.out.print(", String next: "); // todo remove dev item + System.out.println(next); // todo remove dev item + bHash = BytesValue.wrap(hexStringToByteArray(hash)); + if(bHash.size() < 4){ + bHash = padToEightBytes(bHash); + } + if(next.equals("") || next.equals("0x")){ + System.out.println("1"); // todo remove dev item + return new ForkId(bHash, BytesValue.wrap(hexStringToByteArray("0x"))); + } else if (next.startsWith("0x")) { + System.out.println("2"); // todo remove dev item + long asLong = Long.parseLong(next.replaceFirst("0x", ""), 16); + return new ForkId(bHash, BytesValues.trimLeadingZeros(BytesValue.wrap(longToBigEndian(asLong)))); + } else { + System.out.println("3"); // todo remove dev item + return new ForkId(bHash, BytesValue.wrap(longToBigEndian(Long.parseLong(next)))); + } + } + + public static ForkId createIdEntry(final BytesValue hash, final String next) { + if (next.startsWith("0x")) { + String temp = next.replaceFirst("0x", ""); + BytesValue temp2 = BytesValue.wrap(longToBigEndian(Long.parseLong(temp, 16))); + return new ForkId(hash, BytesValues.trimLeadingZeros(temp2)); + } else if(next.equals("")){ + return new ForkId(hash, BytesValue.wrap(hexStringToByteArray("0x"))); + } else { + return new ForkId(hash, BytesValue.wrap(longToBigEndian(Long.parseLong(next)))); + } + } + + public static ForkId createIdEntry(final BytesValue hash, final long next) { + return new ForkId(hash, BytesValue.wrap(longToBigEndian(next))); + } + + private static BytesValue padToEightBytes(final BytesValue hash){ + if(hash.size() < 4){ + BytesValue padded = BytesValues.concatenate(hash, BytesValue.wrap(hexStringToByteArray("0x00"))); + return padToEightBytes(padded); + } else { + return hash; + } } public ArrayDeque getForkAndHashList() { @@ -104,17 +162,14 @@ public boolean forkIdCapable() { return forkAndHashList.size() != 0; } - public boolean peerCheck(final StatusMessage status) { - if (status.forkId() == null && status.genesisHash() != null) { - return peerCheck(status.genesisHash()); - } else if (status.forkId() != null) { - return peerCheck(status.forkId()); - } else { - return false; - } - } - + /** + * EIP-2124 behaviour + * + * @param forkId to be validated. + * @return boolean + */ public boolean peerCheck(final ForkId forkId) { + System.out.println(getForkAndHashList()); // todo remove dev item if (forkId == null) { return false; } @@ -138,8 +193,8 @@ public boolean peerCheck(final ForkId forkId) { if (currentHead < forkNext) { return true; } else { - if (isForkKnown(forkId.next)) { - return isRemoteAwareOfPresent(forkId.hash, forkId.next); + if (isForkKnown(forkId.getNextAsLong())) { + return isRemoteAwareOfPresent(forkId.hash, forkId.getNextAsLong()); } else { return false; } @@ -160,6 +215,7 @@ public boolean peerCheck(final Bytes32 peerGenesisOrCheckSumHash) { } private boolean isHashKnown(final BytesValue forkHash) { + System.out.println(forkHash); // todo remove dev item for (ForkId j : forkAndHashList) { if (forkHash.equals(j.hash)) { return true; @@ -173,7 +229,7 @@ private boolean isForkKnown(final Long nextFork) { return true; } for (ForkId j : forkAndHashList) { - if (nextFork.equals(j.next)) { + if (nextFork.equals(j.getNextAsLong())) { return true; } } @@ -183,9 +239,9 @@ private boolean isForkKnown(final Long nextFork) { private boolean isRemoteAwareOfPresent(final BytesValue forkHash, final Long nextFork) { for (ForkId j : forkAndHashList) { if (forkHash.equals(j.hash)) { - if (nextFork.equals(j.next)) { + if (nextFork.equals(j.getNextAsLong())) { return true; - } else if (j.next == 0L) { + } else if (j.getNextAsLong() == 0L) { return highestKnownFork <= nextFork; // Remote aware of future fork } else { return false; @@ -194,9 +250,10 @@ private boolean isRemoteAwareOfPresent(final BytesValue forkHash, final Long nex } return false; } - +// TODO: sort these when the list of forks is first gathered private ArrayDeque collectForksAndHashes(final Set forks, final Long currentHead) { boolean first = true; + System.out.println(forks); // todo remove dev item ArrayDeque forkList = new ArrayDeque<>(); Iterator iterator = forks.iterator(); while (iterator.hasNext()) { @@ -244,10 +301,6 @@ private BytesValue updateCrc(final String hash) { } public BytesValue getCurrentCrcHash() { - // return "0x" + encodeHexString(BytesValues.ofUnsignedInt(crc.getValue()).getByteArray()); - // System.out.println("0x" + - // encodeHexString(BytesValues.ofUnsignedInt(crc.getValue()).getByteArray())); // todo remove - // dev item return BytesValues.ofUnsignedInt(crc.getValue()); } @@ -255,34 +308,78 @@ public BytesValue getCurrentCrcHash() { // ^ the crc is not hashed/checksum-ed any further so the hash class is not suited for this case public static class ForkId { BytesValue hash; - long next; + BytesValue next; BytesValue forkIdRLP; + public ForkId(final BytesValue hash, final BytesValue next) { + this.hash = hash; + this.next = next; + createForkIdRLP(); + } + + public ForkId(final String hash, final String next) { + this.hash = BytesValue.wrap(hexStringToByteArray(hash)); + if(this.hash.size() < 4){ + this.hash = padToEightBytes(this.hash); + } + if(next.equals("") || next.equals("0x")){ + this.next = BytesValue.wrap(hexStringToByteArray("0x")); + } else if (next.startsWith("0x")) { + long asLong = Long.parseLong(next.replaceFirst("0x", ""), 16); + this.next = BytesValues.trimLeadingZeros(BytesValue.wrap(longToBigEndian(asLong))); + } else { + this.next = BytesValue.wrap(longToBigEndian(Long.parseLong(next))); + } + createForkIdRLP(); + } + public ForkId(final String hash, final long next) { this.hash = BytesValue.wrap(hexStringToByteArray(hash)); - this.next = next; + this.next = BytesValue.wrap(longToBigEndian(next)); + createForkIdRLP(); + } + + public ForkId(final BytesValue hash, final String next) { + this.hash = hash; + this.next = BytesValue.wrap(longToBigEndian(Long.parseLong(next))); createForkIdRLP(); } public ForkId(final BytesValue hash, final long next) { this.hash = hash; - this.next = next; + this.next = BytesValue.wrap(longToBigEndian(next)); createForkIdRLP(); } + public long getNextAsLong(){ + return BytesValues.extractLong(next); + } + + public void createForkIdRLP() { BytesValueRLPOutput out = new BytesValueRLPOutput(); - out.writeList(asList(), ForkId::writeTo); +// out.writeList(asList(), ForkId::writeTo); + writeTo(out); forkIdRLP = out.encoded(); } public void writeTo(final RLPOutput out) { out.startList(); out.writeBytesValue(hash); - out.writeLong(next); + out.writeBytesValue(next); out.endList(); } + // Non-RLP entry (for tests) + public BytesValue createNotAsListForkIdRLP() { + BytesValueRLPOutput outPlain = new BytesValueRLPOutput(); + outPlain.startList(); + outPlain.writeBytesValue(hash); + outPlain.writeBytesValue(next); + outPlain.endList(); + return outPlain.encoded(); + } + public static ForkId readFrom(final RLPInput in) { in.enterList(); final BytesValue hash = in.readBytesValue(); @@ -294,7 +391,7 @@ public static ForkId readFrom(final RLPInput in) { public List asByteList() { ArrayList forRLP = new ArrayList(); forRLP.add(hash.getByteArray()); - forRLP.add(longToBigEndian(next)); + forRLP.add(next.getByteArray()); return forRLP; } @@ -313,7 +410,8 @@ public String toString() { public boolean equals(final Object obj) { if (obj instanceof ForkId) { ForkId other = (ForkId) obj; - return other.hash.equals(this.hash) && other.next == this.next; + return other.hash.equals(this.hash) && other.next.equals(this.next); +// return other.hash.equals(this.hash) && other.next == this.next; } return false; } diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessage.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessage.java index c6cf63639bf..75ca7a41cad 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessage.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessage.java @@ -55,9 +55,10 @@ public static StatusMessage create( final BigInteger networkId, final UInt256 totalDifficulty, final Hash bestHash, + final Hash genesisHash, final ForkIdManager.ForkId forkId) { final EthStatus status = - new EthStatus(protocolVersion, networkId, totalDifficulty, bestHash, forkId); + new EthStatus(protocolVersion, networkId, totalDifficulty, bestHash, genesisHash, forkId); final BytesValueRLPOutput out = new BytesValueRLPOutput(); status.writeTo(out); @@ -108,9 +109,7 @@ public Bytes32 genesisHash() { return status().genesisHash; } - /** - * @return The fork id of the network the associated node is participating in. - */ + /** @return The fork id of the network the associated node is participating in. */ public ForkIdManager.ForkId forkId() { return status().forkId; } @@ -150,13 +149,14 @@ private static class EthStatus { final BigInteger networkId, final UInt256 totalDifficulty, final Hash bestHash, + final Hash genesisHash, final ForkIdManager.ForkId forkHash) { this.protocolVersion = protocolVersion; this.networkId = networkId; this.totalDifficulty = totalDifficulty; this.bestHash = bestHash; + this.genesisHash = genesisHash; this.forkId = forkHash; - this.genesisHash = null; } public void writeTo(final RLPOutput out) { @@ -166,10 +166,9 @@ public void writeTo(final RLPOutput out) { out.writeBigIntegerScalar(networkId); out.writeUInt256Scalar(totalDifficulty); out.writeBytesValue(bestHash); + out.writeBytesValue(genesisHash); if (forkId != null) { forkId.writeTo(out); - } else { - out.writeBytesValue(genesisHash); } out.endList(); @@ -182,18 +181,16 @@ public static EthStatus readFrom(final RLPInput in) { final BigInteger networkId = in.readBigIntegerScalar(); final UInt256 totalDifficulty = in.readUInt256Scalar(); final Hash bestHash = Hash.wrap(in.readBytes32()); + final Hash genesisHash = Hash.wrap(in.readBytes32()); if (in.nextIsList()) { final ForkIdManager.ForkId forkId = ForkIdManager.ForkId.readFrom(in); in.leaveList(); - - return new EthStatus(protocolVersion, networkId, totalDifficulty, bestHash, forkId); - } else { - final Hash genesisHash = Hash.wrap(in.readBytes32()); - - in.leaveList(); - - return new EthStatus(protocolVersion, networkId, totalDifficulty, bestHash, genesisHash); + return new EthStatus( + protocolVersion, networkId, totalDifficulty, bestHash, genesisHash, forkId); } + in.leaveList(); + + return new EthStatus(protocolVersion, networkId, totalDifficulty, bestHash, genesisHash); } } } diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java index 33c42c49823..521533291d7 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java @@ -19,14 +19,19 @@ import org.hyperledger.besu.ethereum.core.Hash; import org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput; import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput; +// import org.hyperledger.besu.ethereum.rlp.RLP; +//import org.hyperledger.besu.ethereum.rlp.RLPException; +// import org.hyperledger.besu.ethereum.rlp.RLPInput; import org.hyperledger.besu.util.bytes.BytesValue; import java.util.ArrayDeque; +// import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; +//import org.hyperledger.besu.util.bytes.BytesValues; import org.junit.Test; public class ForkIdManagerTest { @@ -50,9 +55,11 @@ public void checkCorrectMainnetForkIdHashesGenerated() { ForkIdManager.createIdEntry("0x3edd5b10", 4370000L), // First Spurious block ForkIdManager.createIdEntry("0xa00bc324", 7280000L), // First Byzantium block ForkIdManager.createIdEntry("0x668db0af", 0L) // Today Petersburg block + }; List list = Arrays.asList(forksMainnet); - ForkIdManager forkIdManager = ForkIdManager.buildCollection(Hash.fromHexString(mainnetGenHash), list); + ForkIdManager forkIdManager = + ForkIdManager.buildCollection(Hash.fromHexString(mainnetGenHash), list); ArrayDeque entries = forkIdManager.getForkAndHashList(); for (ForkIdManager.ForkId id : checkIds) { ForkIdManager.ForkId testVal = entries.poll(); @@ -121,7 +128,8 @@ public void check1PetersburgWithRemoteAnnouncingTheSame() { // {7987396, ID{Hash: 0x668db0af, Next: 0}, nil}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + ForkIdManager forkIdManager = + new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0x668db0af", 0L)); assertThat(result).isTrue(); } @@ -133,8 +141,10 @@ public void check2PetersburgWithRemoteAnnouncingTheSameAndNextFork() { // {7987396, ID{Hash: 0x668db0af, Next: math.MaxUint64}, nil}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); - Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0x668db0af", Long.MAX_VALUE)); + ForkIdManager forkIdManager = + new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + Boolean result = + forkIdManager.peerCheck(ForkIdManager.createIdEntry("0x668db0af", Long.MAX_VALUE)); assertThat(result).isTrue(); } @@ -147,7 +157,8 @@ public void check3ByzantiumAwareOfPetersburgRemoteUnawareOfPetersburg() { // {7279999, ID{Hash: 0xa00bc324, Next: 0}, nil}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); + ForkIdManager forkIdManager = + new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0xa00bc324", 0L)); assertThat(result).isTrue(); } @@ -160,7 +171,8 @@ public void check4ByzantiumAwareOfPetersburgRemoteAwareOfPetersburg() { // {7279999, ID{Hash: 0xa00bc324, Next: 7280000}, nil}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); + ForkIdManager forkIdManager = + new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0xa00bc324", 7280000L)); assertThat(result).isTrue(); } @@ -174,8 +186,10 @@ public void check5ByzantiumAwareOfPetersburgRemoteAnnouncingUnknownFork() { // {7279999, ID{Hash: 0xa00bc324, Next: math.MaxUint64}, nil}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); - Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0xa00bc324", Long.MAX_VALUE)); + ForkIdManager forkIdManager = + new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); + Boolean result = + forkIdManager.peerCheck(ForkIdManager.createIdEntry("0xa00bc324", Long.MAX_VALUE)); assertThat(result).isTrue(); } @@ -186,7 +200,8 @@ public void check6PetersburgWithRemoteAnnouncingByzantiumAwareOfPetersburg() { // {7987396, ID{Hash: 0x668db0af, Next: 7280000}, nil}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + ForkIdManager forkIdManager = + new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0x668db0af", 7280000L)); assertThat(result).isTrue(); } @@ -199,7 +214,8 @@ public void check7PetersburgWithRemoteAnnouncingSpuriousAwareOfByzantiumRemoteMa // {7987396, ID{Hash: 0x3edd5b10, Next: 4370000}, nil}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + ForkIdManager forkIdManager = + new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0x3edd5b10", 4370000L)); assertThat(result).isTrue(); } @@ -210,7 +226,8 @@ public void check8ByzantiumWithRemoteAnnouncingPetersburgLocalOutOfSync() { // {7279999, ID{Hash: 0x668db0af, Next: 0}, nil}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); + ForkIdManager forkIdManager = + new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0x668db0af", 0L)); assertThat(result).isTrue(); } @@ -222,7 +239,8 @@ public void check9SpuriousWithRemoteAnnouncingByzantiumRemoteUnawareOfPetersburg // {4369999, ID{Hash: 0xa00bc324, Next: 0}, nil}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 4369999L); + ForkIdManager forkIdManager = + new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 4369999L); Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0xa00bc324", 0L)); assertThat(result).isTrue(); } @@ -234,7 +252,8 @@ public void check10PetersburgWithRemoteAnnouncingByzantiumRemoteUnawareOfAdditio // {7987396, ID{Hash: 0xa00bc324, Next: 0}, ErrRemoteStale}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + ForkIdManager forkIdManager = + new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0xa00bc324", 0L)); assertThat(result).isFalse(); } @@ -246,7 +265,8 @@ public void check11PetersburgWithRemoteAnnouncingPetersburgAndFutureForkLocalNee // {7987396, ID{Hash: 0x5cddc0e1, Next: 0}, ErrLocalIncompatibleOrStale}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + ForkIdManager forkIdManager = + new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0x5cddc0e1", 0L)); assertThat(result).isFalse(); } @@ -258,7 +278,8 @@ public void check12ByzantiumWithRemoteAnnouncingPetersburgAndFutureForkLocalNeed // {7279999, ID{Hash: 0x5cddc0e1, Next: 0}, ErrLocalIncompatibleOrStale}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); + ForkIdManager forkIdManager = + new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0x5cddc0e1", 0L)); assertThat(result).isFalse(); } @@ -269,7 +290,8 @@ public void check13ByzantiumWithRemoteAnnouncingRinkebyPetersburg() { // {7987396, ID{Hash: 0xafec6b27, Next: 0}, ErrLocalIncompatibleOrStale}, List list = Arrays.asList(forksMainnet); Set forkSet = new LinkedHashSet<>(list); - ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + ForkIdManager forkIdManager = + new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0xafec6b27", 0L)); assertThat(result).isFalse(); } @@ -278,11 +300,67 @@ public void check13ByzantiumWithRemoteAnnouncingRinkebyPetersburg() { public void createAndDecodeRLP() { ForkIdManager.ForkId forkIdEntry = ForkIdManager.createIdEntry("0xa00bc324", 7280000L); BytesValueRLPOutput out = new BytesValueRLPOutput(); - out.writeList(forkIdEntry.asList(), ForkIdManager.ForkId::writeTo); + forkIdEntry.writeTo(out); + BytesValue bytesValue = out.encoded(); + BytesValueRLPInput in = new BytesValueRLPInput(bytesValue, false); + ForkIdManager.ForkId decodedEntry = ForkIdManager.readFrom(in); + assertThat(forkIdEntry.equals(decodedEntry)).isTrue(); + } + + // OLD VERSION + // @Test + // public void createAndDecodeRLP() { + // ForkIdManager.ForkId forkIdEntry = ForkIdManager.createIdEntry("0xa00bc324", 7280000L); + // BytesValueRLPOutput out = new BytesValueRLPOutput(); + // out.writeList(forkIdEntry.asList(), ForkIdManager.ForkId::writeTo); + // BytesValue bytesValue = out.encoded(); + // BytesValueRLPInput in = new BytesValueRLPInput(bytesValue, false); + // List forkId = in.readList(ForkIdManager::readFrom); + // ForkIdManager.ForkId decodedEntry = forkId.get(0); + // assertThat(forkIdEntry.equals(decodedEntry)).isTrue(); + // } + + @Test + public void check1ZeroZeroProperRLPEncoding() { + ForkIdManager.ForkId forkIdEntry = ForkIdManager.createIdEntry("0", "0x"); + System.out.println(forkIdEntry); // todo remove dev item + BytesValueRLPOutput out = new BytesValueRLPOutput(); + forkIdEntry.writeTo(out); + String str1 = "0xc6840000000080"; + BytesValue bytesValue = out.encoded(); + System.out.println(bytesValue); // todo remove dev item + assertThat(str1.equals(bytesValue.toString())).isTrue(); + BytesValueRLPInput in = new BytesValueRLPInput(bytesValue, false); + ForkIdManager.ForkId decodedEntry = ForkIdManager.readFrom(in); + assertThat(forkIdEntry.equals(decodedEntry)).isTrue(); + } + + @Test + public void check2ArbitraryProperRLPEncoding() { + ForkIdManager.ForkId forkIdEntry = ForkIdManager.createIdEntry("0xdeadbeef", "0xBADDCAFE"); + BytesValueRLPOutput out = new BytesValueRLPOutput(); + forkIdEntry.writeTo(out); + String str1 = "0xca84deadbeef84baddcafe"; + BytesValue bytesValue = out.encoded(); + assertThat(str1.equals(bytesValue.toString())).isTrue(); + BytesValueRLPInput in = new BytesValueRLPInput(bytesValue, false); + ForkIdManager.ForkId decodedEntry = ForkIdManager.readFrom(in); + assertThat(forkIdEntry.equals(decodedEntry)).isTrue(); + } + + @Test + public void check3MaximumsProperRLPEncoding() { + ForkIdManager.ForkId forkIdEntry = ForkIdManager.createIdEntry("0xffffffff", Long.MAX_VALUE); + BytesValueRLPOutput out = new BytesValueRLPOutput(); + forkIdEntry.writeTo(out); + // ce84ffffffff88ffffffffffffffff; // Check value supplied in EIP-2124 spec via GO lang + // math.MaxUint64 + String str1 = + "0xce84ffffffff887fffffffffffffff"; // Long.MAX_VALUE is smaller than GO lang math.MaxUint64 BytesValue bytesValue = out.encoded(); + assertThat(str1.equals(bytesValue.toString())).isTrue(); BytesValueRLPInput in = new BytesValueRLPInput(bytesValue, false); - List forkId = in.readList(ForkIdManager::readFrom); - ForkIdManager.ForkId decodedEntry = forkId.get(0); + ForkIdManager.ForkId decodedEntry = ForkIdManager.readFrom(in); assertThat(forkIdEntry.equals(decodedEntry)).isTrue(); } -} + } diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessageTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessageTest.java index 93b24722c64..1e5ccbfd430 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessageTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessageTest.java @@ -73,9 +73,10 @@ public void serializeDeserializeWithForkId() { final BigInteger networkId = BigInteger.ONE; final UInt256 td = UInt256.of(1000L); final Hash bestHash = randHash(1L); + final Hash genesisHash = randHash(2L); final ForkIdManager.ForkId forkId = ForkIdManager.createIdEntry("0xa00bc334", 0L); - final MessageData msg = StatusMessage.create(version, networkId, td, bestHash, forkId); + final MessageData msg = StatusMessage.create(version, networkId, td, bestHash, genesisHash, forkId); final StatusMessage copy = new StatusMessage(msg.getData()); @@ -83,6 +84,7 @@ public void serializeDeserializeWithForkId() { assertThat(copy.networkId()).isEqualTo(networkId); assertThat(copy.totalDifficulty()).isEqualTo(td); assertThat(copy.bestHash()).isEqualTo(bestHash); + assertThat(copy.genesisHash()).isEqualTo(genesisHash); assertThat(copy.forkId()).isEqualTo(forkId); } From 9c946a8aeedcd5f85d5e6db167ef71c10710fc8a Mon Sep 17 00:00:00 2001 From: SteveM Date: Sat, 30 Nov 2019 00:19:45 -0800 Subject: [PATCH 16/42] eip-2124 RLP encoding checks pass, store next block number as bytesValue, include backward compatibility of eip-2364 Signed-off-by: SteveM --- .../besu/ethereum/eth/EthProtocol.java | 1 + .../eth/manager/EthProtocolManager.java | 4 +- .../ethereum/eth/manager/ForkIdManager.java | 176 ++++++------------ .../eth/manager/ForkIdManagerTest.java | 15 -- 4 files changed, 64 insertions(+), 132 deletions(-) diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/EthProtocol.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/EthProtocol.java index 6d0e77409fc..775aafca359 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/EthProtocol.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/EthProtocol.java @@ -31,6 +31,7 @@ public class EthProtocol implements SubProtocol { public static final String NAME = "eth"; public static final Capability ETH62 = Capability.create(NAME, EthVersion.V62); public static final Capability ETH63 = Capability.create(NAME, EthVersion.V63); + public static final Capability ETH64 = Capability.create(NAME, EthVersion.V64); private static final EthProtocol INSTANCE = new EthProtocol(); private static final List eth62Messages = diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java index c0eeeb0160c..610abc618c8 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java @@ -179,7 +179,7 @@ public EthProtocolManager( genesisHash = blockchain.getBlockHashByNumber(0L).get(); forkIdManager = - ForkIdManager.buildCollection(genesisHash, forks, blockchain, getSupportedCapabilities()); + ForkIdManager.buildCollection(genesisHash, forks, blockchain); ethPeers = new EthPeers(getSupportedProtocol(), clock, metricsSystem); ethMessages = new EthMessages(); @@ -330,7 +330,7 @@ private void handleStatusMessage(final EthPeer peer, final MessageData data) { if (!status.networkId().equals(networkId)) { LOG.debug("Disconnecting from peer with mismatched network id: {}", status.networkId()); peer.disconnect(DisconnectReason.SUBPROTOCOL_TRIGGERED); - } else if (forkIdManager.peerCheck(status.forkId())) { + } else if (!forkIdManager.peerCheck(status.forkId()) && status.protocolVersion() > 63) { LOG.debug( "Disconnecting from peer with matching network id ({}), but non-matching fork id: {}", networkId, diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java index b10da36c45f..7e93a9addf4 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java @@ -16,7 +16,6 @@ import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.core.Hash; -import org.hyperledger.besu.ethereum.p2p.rlpx.wire.Capability; import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput; import org.hyperledger.besu.ethereum.rlp.RLPInput; import org.hyperledger.besu.ethereum.rlp.RLPOutput; @@ -32,8 +31,6 @@ import java.util.Set; import java.util.zip.CRC32; -import static java.nio.charset.StandardCharsets.UTF_8; - public class ForkIdManager { private Hash genesisHash; @@ -54,11 +51,8 @@ public ForkIdManager(final Hash genesisHash, final Set forks, final Long c } }; - public static ForkIdManager buildCollection( - final Hash genesisHash, - final List forks, - final Blockchain blockchain, - final List caps) { + static ForkIdManager buildCollection( + final Hash genesisHash, final List forks, final Blockchain blockchain) { if (forks == null) { return new ForkIdManager(genesisHash, null, blockchain.getChainHeadBlockNumber()); } else { @@ -76,102 +70,47 @@ public static ForkIdManager buildCollection(final Hash genesisHash, final List getForkAndHashList() { return this.forkAndHashList; } - public ForkId getLatestForkId() { + ForkId getLatestForkId() { return lastKnownEntry; } - public boolean forkIdCapable() { - return forkAndHashList.size() != 0; + public static ForkId readFrom(final RLPInput in) { + in.enterList(); + final BytesValue hash = in.readBytesValue(); + final BytesValue next = in.readBytesValue(); + in.leaveList(); + return new ForkId(hash, next); } + + /** * EIP-2124 behaviour * * @param forkId to be validated. - * @return boolean + * @return boolean (peer valid (true) or invalid (false)) */ public boolean peerCheck(final ForkId forkId) { - System.out.println(getForkAndHashList()); // todo remove dev item if (forkId == null) { - return false; + return true; // Another method must be used to validate (i.e. genesis hash) } // Run the fork checksum validation ruleset: // 1. If local and remote FORK_CSUM matches, connect. @@ -189,12 +128,12 @@ public boolean peerCheck(final ForkId forkId) { // the remote, but at this current point in time we don't have enough // information. // 4. Reject in all other cases. - if (isHashKnown(forkId.hash)) { + if (isHashKnown(forkId.getHash())) { if (currentHead < forkNext) { return true; } else { - if (isForkKnown(forkId.getNextAsLong())) { - return isRemoteAwareOfPresent(forkId.hash, forkId.getNextAsLong()); + if (isForkKnown(forkId.getNext())) { + return isRemoteAwareOfPresent(forkId.getHash(), forkId.getNext()); } else { return false; } @@ -215,9 +154,8 @@ public boolean peerCheck(final Bytes32 peerGenesisOrCheckSumHash) { } private boolean isHashKnown(final BytesValue forkHash) { - System.out.println(forkHash); // todo remove dev item for (ForkId j : forkAndHashList) { - if (forkHash.equals(j.hash)) { + if (forkHash.equals(j.getHash())) { return true; } } @@ -229,7 +167,7 @@ private boolean isForkKnown(final Long nextFork) { return true; } for (ForkId j : forkAndHashList) { - if (nextFork.equals(j.getNextAsLong())) { + if (nextFork.equals(j.getNext())) { return true; } } @@ -238,11 +176,11 @@ private boolean isForkKnown(final Long nextFork) { private boolean isRemoteAwareOfPresent(final BytesValue forkHash, final Long nextFork) { for (ForkId j : forkAndHashList) { - if (forkHash.equals(j.hash)) { - if (nextFork.equals(j.getNextAsLong())) { + if (forkHash.equals(j.getHash())) { + if (nextFork.equals(j.getNext())) { return true; - } else if (j.getNextAsLong() == 0L) { - return highestKnownFork <= nextFork; // Remote aware of future fork + } else if (j.getNext() == 0L) { + return highestKnownFork <= nextFork; // Remote aware of an additional future fork } else { return false; } @@ -250,10 +188,10 @@ private boolean isRemoteAwareOfPresent(final BytesValue forkHash, final Long nex } return false; } -// TODO: sort these when the list of forks is first gathered + + // TODO: sort these when the list of forks is first gathered private ArrayDeque collectForksAndHashes(final Set forks, final Long currentHead) { boolean first = true; - System.out.println(forks); // todo remove dev item ArrayDeque forkList = new ArrayDeque<>(); Iterator iterator = forks.iterator(); while (iterator.hasNext()) { @@ -272,11 +210,14 @@ private ArrayDeque collectForksAndHashes(final Set forks, final Lo // most recent fork forkList.add(new ForkId(getCurrentCrcHash(), forkBlockNumber)); updateCrc(forkBlockNumber); - lastKnownEntry = new ForkId(getCurrentCrcHash(), 0L); - forkList.add(lastKnownEntry); + // next fork or no future fork known if (currentHead > forkBlockNumber) { + lastKnownEntry = new ForkId(getCurrentCrcHash(), 0L); + forkList.add(lastKnownEntry); this.forkNext = 0L; } else { + lastKnownEntry = new ForkId(getCurrentCrcHash(), forkBlockNumber); + forkList.add(lastKnownEntry); this.forkNext = forkBlockNumber; } @@ -288,10 +229,9 @@ private ArrayDeque collectForksAndHashes(final Set forks, final Lo return forkList; } - private BytesValue updateCrc(final Long block) { + private void updateCrc(final Long block) { byte[] byteRepresentationFork = longToBigEndian(block); crc.update(byteRepresentationFork, 0, byteRepresentationFork.length); - return getCurrentCrcHash(); } private BytesValue updateCrc(final String hash) { @@ -300,7 +240,7 @@ private BytesValue updateCrc(final String hash) { return getCurrentCrcHash(); } - public BytesValue getCurrentCrcHash() { + private BytesValue getCurrentCrcHash() { return BytesValues.ofUnsignedInt(crc.getValue()); } @@ -311,54 +251,50 @@ public static class ForkId { BytesValue next; BytesValue forkIdRLP; - public ForkId(final BytesValue hash, final BytesValue next) { + ForkId(final BytesValue hash, final BytesValue next) { this.hash = hash; this.next = next; createForkIdRLP(); } - public ForkId(final String hash, final String next) { + ForkId(final String hash, final String next) { this.hash = BytesValue.wrap(hexStringToByteArray(hash)); - if(this.hash.size() < 4){ + if (this.hash.size() < 4) { this.hash = padToEightBytes(this.hash); } - if(next.equals("") || next.equals("0x")){ + if (next.equals("") || next.equals("0x")) { this.next = BytesValue.wrap(hexStringToByteArray("0x")); } else if (next.startsWith("0x")) { long asLong = Long.parseLong(next.replaceFirst("0x", ""), 16); this.next = BytesValues.trimLeadingZeros(BytesValue.wrap(longToBigEndian(asLong))); - } else { + } else { this.next = BytesValue.wrap(longToBigEndian(Long.parseLong(next))); } createForkIdRLP(); } - public ForkId(final String hash, final long next) { + ForkId(final String hash, final long next) { this.hash = BytesValue.wrap(hexStringToByteArray(hash)); this.next = BytesValue.wrap(longToBigEndian(next)); createForkIdRLP(); } - public ForkId(final BytesValue hash, final String next) { - this.hash = hash; - this.next = BytesValue.wrap(longToBigEndian(Long.parseLong(next))); - createForkIdRLP(); - } - - public ForkId(final BytesValue hash, final long next) { + ForkId(final BytesValue hash, final long next) { this.hash = hash; this.next = BytesValue.wrap(longToBigEndian(next)); createForkIdRLP(); } - public long getNextAsLong(){ + public long getNext() { return BytesValues.extractLong(next); } + public BytesValue getHash() { + return hash; + } - public void createForkIdRLP() { + void createForkIdRLP() { BytesValueRLPOutput out = new BytesValueRLPOutput(); -// out.writeList(asList(), ForkId::writeTo); writeTo(out); forkIdRLP = out.encoded(); } @@ -401,17 +337,27 @@ public List asList() { return forRLP; } + private static BytesValue padToEightBytes(final BytesValue hash) { + if (hash.size() < 4) { + BytesValue padded = + BytesValues.concatenate(hash, BytesValue.wrap(hexStringToByteArray("0x00"))); + return padToEightBytes(padded); + } else { + return hash; + } + } + @Override public String toString() { - return "ForkId(hash=" + this.hash + ", next=" + this.next + ")"; + return "ForkId(hash=" + this.hash + ", next=" + BytesValues.extractLong(next) + ")"; } @Override public boolean equals(final Object obj) { if (obj instanceof ForkId) { ForkId other = (ForkId) obj; - return other.hash.equals(this.hash) && other.next.equals(this.next); -// return other.hash.equals(this.hash) && other.next == this.next; + Long thisNext = BytesValues.extractLong(next); + return other.getHash().equals(this.hash) && thisNext.equals(other.getNext()); } return false; } diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java index 521533291d7..ebd695fa3d2 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java @@ -307,28 +307,13 @@ public void createAndDecodeRLP() { assertThat(forkIdEntry.equals(decodedEntry)).isTrue(); } - // OLD VERSION - // @Test - // public void createAndDecodeRLP() { - // ForkIdManager.ForkId forkIdEntry = ForkIdManager.createIdEntry("0xa00bc324", 7280000L); - // BytesValueRLPOutput out = new BytesValueRLPOutput(); - // out.writeList(forkIdEntry.asList(), ForkIdManager.ForkId::writeTo); - // BytesValue bytesValue = out.encoded(); - // BytesValueRLPInput in = new BytesValueRLPInput(bytesValue, false); - // List forkId = in.readList(ForkIdManager::readFrom); - // ForkIdManager.ForkId decodedEntry = forkId.get(0); - // assertThat(forkIdEntry.equals(decodedEntry)).isTrue(); - // } - @Test public void check1ZeroZeroProperRLPEncoding() { ForkIdManager.ForkId forkIdEntry = ForkIdManager.createIdEntry("0", "0x"); - System.out.println(forkIdEntry); // todo remove dev item BytesValueRLPOutput out = new BytesValueRLPOutput(); forkIdEntry.writeTo(out); String str1 = "0xc6840000000080"; BytesValue bytesValue = out.encoded(); - System.out.println(bytesValue); // todo remove dev item assertThat(str1.equals(bytesValue.toString())).isTrue(); BytesValueRLPInput in = new BytesValueRLPInput(bytesValue, false); ForkIdManager.ForkId decodedEntry = ForkIdManager.readFrom(in); From 35f012f7ace33d62aec1f2c0b221558a5b9f2cec Mon Sep 17 00:00:00 2001 From: SteveM Date: Sat, 30 Nov 2019 01:13:03 -0800 Subject: [PATCH 17/42] remove redundant methods in forkId and modify fork collection to use a list throughout Signed-off-by: SteveM --- .../besu/config/GenesisConfigFile.java | 22 +++--- .../ethereum/eth/manager/ForkIdManager.java | 73 ++++--------------- .../eth/manager/ForkIdManagerTest.java | 72 +++++++----------- 3 files changed, 50 insertions(+), 117 deletions(-) diff --git a/config/src/main/java/org/hyperledger/besu/config/GenesisConfigFile.java b/config/src/main/java/org/hyperledger/besu/config/GenesisConfigFile.java index 089b850385d..7149e8e357b 100644 --- a/config/src/main/java/org/hyperledger/besu/config/GenesisConfigFile.java +++ b/config/src/main/java/org/hyperledger/besu/config/GenesisConfigFile.java @@ -16,19 +16,15 @@ import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.stream.Collectors.toList; -import static java.util.stream.Collectors.toSet; import static org.hyperledger.besu.config.JsonUtil.normalizeKeys; import java.io.IOException; -import java.util.ArrayList; import java.util.Collections; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.stream.Stream; -import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ObjectNode; import com.google.common.collect.Streams; import com.google.common.io.Resources; @@ -137,17 +133,17 @@ public long getTimestamp() { return parseLong("timestamp", JsonUtil.getValueAsString(configRoot, "timestamp", "0x0")); } - // TODO look into how to handle invalid or exceptional (i.e. null or "") fork values public List getForks() { return JsonUtil.getObjectNode(configRoot, "config").stream() - .flatMap(node -> - Streams.stream(node.fieldNames()) - .filter(name -> !name.toLowerCase().equals("chainid")) - .filter(name -> node.get(name).canConvertToLong()) - .filter(name -> name.contains("block")) - .map(name -> node.get(name).asLong()) - ) - .collect(toList()); + .flatMap( + node -> + Streams.stream(node.fieldNames()) + .filter(name -> !name.toLowerCase().equals("chainid")) + .filter(name -> node.get(name).canConvertToLong()) + .filter(name -> name.contains("block")) + .map(name -> node.get(name).asLong())) + .distinct() + .collect(toList()); } private String getRequiredString(final String key) { diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java index 7e93a9addf4..8ad8341aa0c 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java @@ -26,9 +26,7 @@ import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Iterator; -import java.util.LinkedHashSet; import java.util.List; -import java.util.Set; import java.util.zip.CRC32; public class ForkIdManager { @@ -41,7 +39,7 @@ public class ForkIdManager { private ForkId lastKnownEntry = null; private ArrayDeque forkAndHashList; - public ForkIdManager(final Hash genesisHash, final Set forks, final Long currentHead) { + public ForkIdManager(final Hash genesisHash, final List forks, final Long currentHead) { this.genesisHash = genesisHash; this.currentHead = currentHead; if (forks != null) { @@ -56,8 +54,7 @@ static ForkIdManager buildCollection( if (forks == null) { return new ForkIdManager(genesisHash, null, blockchain.getChainHeadBlockNumber()); } else { - Set forkSet = new LinkedHashSet<>(forks); - return new ForkIdManager(genesisHash, forkSet, blockchain.getChainHeadBlockNumber()); + return new ForkIdManager(genesisHash, forks, blockchain.getChainHeadBlockNumber()); } }; @@ -65,8 +62,7 @@ public static ForkIdManager buildCollection(final Hash genesisHash, final List forkSet = new LinkedHashSet<>(forks); - return new ForkIdManager(genesisHash, forkSet, Long.MAX_VALUE); + return new ForkIdManager(genesisHash, forks, Long.MAX_VALUE); } }; @@ -100,8 +96,6 @@ public static ForkId readFrom(final RLPInput in) { return new ForkId(hash, next); } - - /** * EIP-2124 behaviour * @@ -146,11 +140,11 @@ public boolean peerCheck(final ForkId forkId) { /** * Non EIP-2124 behaviour * - * @param peerGenesisOrCheckSumHash Hash or checksum to be validated. + * @param peerGenesisHash Hash to be validated. * @return boolean */ - public boolean peerCheck(final Bytes32 peerGenesisOrCheckSumHash) { - return !peerGenesisOrCheckSumHash.equals(genesisHash); + public boolean peerCheck(final Bytes32 peerGenesisHash) { + return !peerGenesisHash.equals(genesisHash); } private boolean isHashKnown(final BytesValue forkHash) { @@ -189,8 +183,8 @@ private boolean isRemoteAwareOfPresent(final BytesValue forkHash, final Long nex return false; } - // TODO: sort these when the list of forks is first gathered - private ArrayDeque collectForksAndHashes(final Set forks, final Long currentHead) { + // TODO: should sort these when first gathering the list of forks to ensure order + private ArrayDeque collectForksAndHashes(final List forks, final Long currentHead) { boolean first = true; ArrayDeque forkList = new ArrayDeque<>(); Iterator iterator = forks.iterator(); @@ -235,7 +229,8 @@ private void updateCrc(final Long block) { } private BytesValue updateCrc(final String hash) { - byte[] byteRepresentation = hexStringToByteArray(hash); + BytesValue bv = BytesValue.fromHexString(hash); + byte[] byteRepresentation = bv.extractArray(); crc.update(byteRepresentation, 0, byteRepresentation.length); return getCurrentCrcHash(); } @@ -244,8 +239,6 @@ private BytesValue getCurrentCrcHash() { return BytesValues.ofUnsignedInt(crc.getValue()); } - // TODO use Hash class instead of string for checksum. convert to or from string only when needed - // ^ the crc is not hashed/checksum-ed any further so the hash class is not suited for this case public static class ForkId { BytesValue hash; BytesValue next; @@ -258,12 +251,12 @@ public static class ForkId { } ForkId(final String hash, final String next) { - this.hash = BytesValue.wrap(hexStringToByteArray(hash)); + this.hash = BytesValue.fromHexString((hash.length() % 2 == 0 ? "" : "0") + hash); if (this.hash.size() < 4) { this.hash = padToEightBytes(this.hash); } if (next.equals("") || next.equals("0x")) { - this.next = BytesValue.wrap(hexStringToByteArray("0x")); + this.next = BytesValue.EMPTY; } else if (next.startsWith("0x")) { long asLong = Long.parseLong(next.replaceFirst("0x", ""), 16); this.next = BytesValues.trimLeadingZeros(BytesValue.wrap(longToBigEndian(asLong))); @@ -274,7 +267,7 @@ public static class ForkId { } ForkId(final String hash, final long next) { - this.hash = BytesValue.wrap(hexStringToByteArray(hash)); + this.hash = BytesValue.fromHexString(hash); this.next = BytesValue.wrap(longToBigEndian(next)); createForkIdRLP(); } @@ -340,7 +333,7 @@ public List asList() { private static BytesValue padToEightBytes(final BytesValue hash) { if (hash.size() < 4) { BytesValue padded = - BytesValues.concatenate(hash, BytesValue.wrap(hexStringToByteArray("0x00"))); + BytesValues.concatenate(hash, BytesValue.fromHexString("0x00")); return padToEightBytes(padded); } else { return hash; @@ -368,17 +361,7 @@ public int hashCode() { } } - // TODO: Ask / look to see if there is a helper for these below <---------- - private static byte[] hexStringToByteArray(final String s) { - String string = ""; - if (s.startsWith("0x")) { - string = s.replaceFirst("0x", ""); - } - string = (string.length() % 2 == 0 ? "" : "0") + string; - return decodeHexString(string); - } - - // next three methods adopted from: + // next two methods adopted from: // https://github.com/bcgit/bc-java/blob/master/core/src/main/java/org/bouncycastle/util/Pack.java private static byte[] longToBigEndian(final long n) { byte[] bs = new byte[8]; @@ -394,30 +377,4 @@ private static void intToBigEndian(final int n, final byte[] bs, int off) { bs[++off] = (byte) (n >>> 8); bs[++off] = (byte) (n); } - - private static byte[] decodeHexString(final String hexString) { - if (hexString.length() % 2 == 1) { - throw new IllegalArgumentException("Invalid hexadecimal String supplied."); - } - - byte[] bytes = new byte[hexString.length() / 2]; - for (int i = 0; i < hexString.length(); i += 2) { - bytes[i / 2] = hexToByte(hexString.substring(i, i + 2)); - } - return bytes; - } - - private static byte hexToByte(final String hexString) { - int firstDigit = toDigit(hexString.charAt(0)); - int secondDigit = toDigit(hexString.charAt(1)); - return (byte) ((firstDigit << 4) + secondDigit); - } - - private static int toDigit(final char hexChar) { - int digit = Character.digit(hexChar, 16); - if (digit == -1) { - throw new IllegalArgumentException("Invalid Hexadecimal Character: " + hexChar); - } - return digit; - } } diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java index ebd695fa3d2..4d5ebeb5eb6 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java @@ -19,19 +19,12 @@ import org.hyperledger.besu.ethereum.core.Hash; import org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput; import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput; -// import org.hyperledger.besu.ethereum.rlp.RLP; -//import org.hyperledger.besu.ethereum.rlp.RLPException; -// import org.hyperledger.besu.ethereum.rlp.RLPInput; import org.hyperledger.besu.util.bytes.BytesValue; import java.util.ArrayDeque; -// import java.util.ArrayList; import java.util.Arrays; -import java.util.LinkedHashSet; import java.util.List; -import java.util.Set; -//import org.hyperledger.besu.util.bytes.BytesValues; import org.junit.Test; public class ForkIdManagerTest { @@ -126,10 +119,9 @@ public void checkCorrectRinkebyForkIdHashesGenerated() { public void check1PetersburgWithRemoteAnnouncingTheSame() { // 1 Local is mainnet Petersburg, remote announces the same. No future fork is announced. // {7987396, ID{Hash: 0x668db0af, Next: 0}, nil}, - List list = Arrays.asList(forksMainnet); - Set forkSet = new LinkedHashSet<>(list); + List forkList = Arrays.asList(forksMainnet); ForkIdManager forkIdManager = - new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkList, 7987396L); Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0x668db0af", 0L)); assertThat(result).isTrue(); } @@ -139,10 +131,9 @@ public void check2PetersburgWithRemoteAnnouncingTheSameAndNextFork() { // 2 Local is mainnet Petersburg, remote announces the same. Remote also announces a next fork // at block 0xffffffff, but that is uncertain. // {7987396, ID{Hash: 0x668db0af, Next: math.MaxUint64}, nil}, - List list = Arrays.asList(forksMainnet); - Set forkSet = new LinkedHashSet<>(list); + List forkList = Arrays.asList(forksMainnet); ForkIdManager forkIdManager = - new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkList, 7987396L); Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0x668db0af", Long.MAX_VALUE)); assertThat(result).isTrue(); @@ -155,10 +146,9 @@ public void check3ByzantiumAwareOfPetersburgRemoteUnawareOfPetersburg() { // the fork). // In this case we don't know if Petersburg passed yet or not. // {7279999, ID{Hash: 0xa00bc324, Next: 0}, nil}, - List list = Arrays.asList(forksMainnet); - Set forkSet = new LinkedHashSet<>(list); + List forkList = Arrays.asList(forksMainnet); ForkIdManager forkIdManager = - new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); + new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkList, 7279999L); Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0xa00bc324", 0L)); assertThat(result).isTrue(); } @@ -169,10 +159,9 @@ public void check4ByzantiumAwareOfPetersburgRemoteAwareOfPetersburg() { // announces also Byzantium, and it's also aware of Petersburg (e.g. updated node before the // fork). We don't know if Petersburg passed yet (will pass) or not. // {7279999, ID{Hash: 0xa00bc324, Next: 7280000}, nil}, - List list = Arrays.asList(forksMainnet); - Set forkSet = new LinkedHashSet<>(list); + List forkList = Arrays.asList(forksMainnet); ForkIdManager forkIdManager = - new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); + new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkList, 7279999L); Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0xa00bc324", 7280000L)); assertThat(result).isTrue(); } @@ -184,10 +173,9 @@ public void check5ByzantiumAwareOfPetersburgRemoteAnnouncingUnknownFork() { // Petersburg). // As neither forks passed at neither nodes, they may mismatch, but we still connect for now. // {7279999, ID{Hash: 0xa00bc324, Next: math.MaxUint64}, nil}, - List list = Arrays.asList(forksMainnet); - Set forkSet = new LinkedHashSet<>(list); + List forkList = Arrays.asList(forksMainnet); ForkIdManager forkIdManager = - new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); + new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkList, 7279999L); Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0xa00bc324", Long.MAX_VALUE)); assertThat(result).isTrue(); @@ -198,10 +186,9 @@ public void check6PetersburgWithRemoteAnnouncingByzantiumAwareOfPetersburg() { // 6 Local is mainnet Petersburg, remote announces Byzantium + knowledge about Petersburg. // Remote is simply out of sync, accept. // {7987396, ID{Hash: 0x668db0af, Next: 7280000}, nil}, - List list = Arrays.asList(forksMainnet); - Set forkSet = new LinkedHashSet<>(list); + List forkList = Arrays.asList(forksMainnet); ForkIdManager forkIdManager = - new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkList, 7987396L); Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0x668db0af", 7280000L)); assertThat(result).isTrue(); } @@ -212,10 +199,9 @@ public void check7PetersburgWithRemoteAnnouncingSpuriousAwareOfByzantiumRemoteMa // Remote is definitely out of sync. It may or may not need the Petersburg update, we don't know // yet. // {7987396, ID{Hash: 0x3edd5b10, Next: 4370000}, nil}, - List list = Arrays.asList(forksMainnet); - Set forkSet = new LinkedHashSet<>(list); + List forkList = Arrays.asList(forksMainnet); ForkIdManager forkIdManager = - new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkList, 7987396L); Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0x3edd5b10", 4370000L)); assertThat(result).isTrue(); } @@ -224,10 +210,9 @@ public void check7PetersburgWithRemoteAnnouncingSpuriousAwareOfByzantiumRemoteMa public void check8ByzantiumWithRemoteAnnouncingPetersburgLocalOutOfSync() { // 8 Local is mainnet Byzantium, remote announces Petersburg. Local is out of sync, accept. // {7279999, ID{Hash: 0x668db0af, Next: 0}, nil}, - List list = Arrays.asList(forksMainnet); - Set forkSet = new LinkedHashSet<>(list); + List forkList = Arrays.asList(forksMainnet); ForkIdManager forkIdManager = - new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); + new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkList, 7279999L); Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0x668db0af", 0L)); assertThat(result).isTrue(); } @@ -237,10 +222,9 @@ public void check9SpuriousWithRemoteAnnouncingByzantiumRemoteUnawareOfPetersburg // 9 Local is mainnet Spurious, remote announces Byzantium, but is not aware of Petersburg. // Local out of sync. Local also knows about a future fork, but that is uncertain yet. // {4369999, ID{Hash: 0xa00bc324, Next: 0}, nil}, - List list = Arrays.asList(forksMainnet); - Set forkSet = new LinkedHashSet<>(list); + List forkList = Arrays.asList(forksMainnet); ForkIdManager forkIdManager = - new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 4369999L); + new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkList, 4369999L); Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0xa00bc324", 0L)); assertThat(result).isTrue(); } @@ -250,10 +234,9 @@ public void check10PetersburgWithRemoteAnnouncingByzantiumRemoteUnawareOfAdditio // 10 Local is mainnet Petersburg. remote announces Byzantium but is not aware of further forks. // Remote needs software update. // {7987396, ID{Hash: 0xa00bc324, Next: 0}, ErrRemoteStale}, - List list = Arrays.asList(forksMainnet); - Set forkSet = new LinkedHashSet<>(list); + List forkList = Arrays.asList(forksMainnet); ForkIdManager forkIdManager = - new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkList, 7987396L); Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0xa00bc324", 0L)); assertThat(result).isFalse(); } @@ -263,10 +246,9 @@ public void check11PetersburgWithRemoteAnnouncingPetersburgAndFutureForkLocalNee // 11 Local is mainnet Petersburg, and isn't aware of more forks. Remote announces Petersburg + // 0xffffffff. Local needs software update, reject. // {7987396, ID{Hash: 0x5cddc0e1, Next: 0}, ErrLocalIncompatibleOrStale}, - List list = Arrays.asList(forksMainnet); - Set forkSet = new LinkedHashSet<>(list); + List forkList = Arrays.asList(forksMainnet); ForkIdManager forkIdManager = - new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkList, 7987396L); Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0x5cddc0e1", 0L)); assertThat(result).isFalse(); } @@ -276,10 +258,9 @@ public void check12ByzantiumWithRemoteAnnouncingPetersburgAndFutureForkLocalNeed // 12 Local is mainnet Byzantium, and is aware of Petersburg. Remote announces Petersburg + // 0xffffffff. Local needs software update, reject. // {7279999, ID{Hash: 0x5cddc0e1, Next: 0}, ErrLocalIncompatibleOrStale}, - List list = Arrays.asList(forksMainnet); - Set forkSet = new LinkedHashSet<>(list); + List forkList = Arrays.asList(forksMainnet); ForkIdManager forkIdManager = - new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7279999L); + new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkList, 7279999L); Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0x5cddc0e1", 0L)); assertThat(result).isFalse(); } @@ -288,10 +269,9 @@ public void check12ByzantiumWithRemoteAnnouncingPetersburgAndFutureForkLocalNeed public void check13ByzantiumWithRemoteAnnouncingRinkebyPetersburg() { // 13 Local is mainnet Petersburg, remote is Rinkeby Petersburg. // {7987396, ID{Hash: 0xafec6b27, Next: 0}, ErrLocalIncompatibleOrStale}, - List list = Arrays.asList(forksMainnet); - Set forkSet = new LinkedHashSet<>(list); + List forkList = Arrays.asList(forksMainnet); ForkIdManager forkIdManager = - new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkSet, 7987396L); + new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkList, 7987396L); Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0xafec6b27", 0L)); assertThat(result).isFalse(); } From 2597ac9d9c0dc62b4fb264b16f6d740f22e573f6 Mon Sep 17 00:00:00 2001 From: SteveM Date: Sat, 30 Nov 2019 02:35:44 -0800 Subject: [PATCH 18/42] needed to lint prior commit Signed-off-by: SteveM --- .../besu/config/GenesisConfigFileTest.java | 5 ++-- .../eth/manager/EthProtocolManager.java | 23 +++++++++---------- .../ethereum/eth/manager/ForkIdManager.java | 5 ++-- .../eth/manager/ForkIdManagerTest.java | 3 +-- .../eth/messages/StatusMessageTest.java | 3 ++- 5 files changed, 18 insertions(+), 21 deletions(-) diff --git a/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java b/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java index d8f07769549..882ede49d9d 100644 --- a/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java +++ b/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java @@ -296,7 +296,8 @@ public void testNoOverride() { public void shouldLoadMainnetForks() { final GenesisConfigFile config = GenesisConfigFile.mainnet(); - assertThat(config.getForks()).contains(1150000L, 1920000L, 2463000L, 2675000L, 2675000L, 4370000L, 7280000L); + assertThat(config.getForks()) + .contains(1150000L, 1920000L, 2463000L, 2675000L, 2675000L, 4370000L, 7280000L); assertThat(config.getConfigOptions().getChainId()).hasValue(MAINNET_CHAIN_ID); } @@ -309,6 +310,4 @@ private void assertInvalidConfiguration(final ThrowingCallable getter) { .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("Invalid genesis block configuration"); } - - } diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java index 610abc618c8..ba3bea1c02b 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java @@ -178,8 +178,7 @@ public EthProtocolManager( this.shutdown = new CountDownLatch(1); genesisHash = blockchain.getBlockHashByNumber(0L).get(); - forkIdManager = - ForkIdManager.buildCollection(genesisHash, forks, blockchain); + forkIdManager = ForkIdManager.buildCollection(genesisHash, forks, blockchain); ethPeers = new EthPeers(getSupportedProtocol(), clock, metricsSystem); ethMessages = new EthMessages(); @@ -287,13 +286,13 @@ public void handleNewConnection(final PeerConnection connection) { // TODO: look to consolidate code below if possible // making status non-final and implementing it above would be one way. final StatusMessage status = - StatusMessage.create( - cap.getVersion(), - networkId, - blockchain.getChainHead().getTotalDifficulty(), - blockchain.getChainHeadHash(), - genesisHash, - forkIdManager.getLatestForkId()); + StatusMessage.create( + cap.getVersion(), + networkId, + blockchain.getChainHead().getTotalDifficulty(), + blockchain.getChainHeadHash(), + genesisHash, + forkIdManager.getLatestForkId()); try { LOG.debug("Sending status message to {}.", peer); peer.send(status); @@ -332,9 +331,9 @@ private void handleStatusMessage(final EthPeer peer, final MessageData data) { peer.disconnect(DisconnectReason.SUBPROTOCOL_TRIGGERED); } else if (!forkIdManager.peerCheck(status.forkId()) && status.protocolVersion() > 63) { LOG.debug( - "Disconnecting from peer with matching network id ({}), but non-matching fork id: {}", - networkId, - status.forkId()); + "Disconnecting from peer with matching network id ({}), but non-matching fork id: {}", + networkId, + status.forkId()); peer.disconnect(DisconnectReason.SUBPROTOCOL_TRIGGERED); } else if (forkIdManager.peerCheck(status.genesisHash())) { LOG.debug( diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java index 8ad8341aa0c..2d14a3dd3c4 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java @@ -50,7 +50,7 @@ public ForkIdManager(final Hash genesisHash, final List forks, final Long }; static ForkIdManager buildCollection( - final Hash genesisHash, final List forks, final Blockchain blockchain) { + final Hash genesisHash, final List forks, final Blockchain blockchain) { if (forks == null) { return new ForkIdManager(genesisHash, null, blockchain.getChainHeadBlockNumber()); } else { @@ -332,8 +332,7 @@ public List asList() { private static BytesValue padToEightBytes(final BytesValue hash) { if (hash.size() < 4) { - BytesValue padded = - BytesValues.concatenate(hash, BytesValue.fromHexString("0x00")); + BytesValue padded = BytesValues.concatenate(hash, BytesValue.fromHexString("0x00")); return padToEightBytes(padded); } else { return hash; diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java index 4d5ebeb5eb6..daf6d538dae 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java @@ -48,7 +48,6 @@ public void checkCorrectMainnetForkIdHashesGenerated() { ForkIdManager.createIdEntry("0x3edd5b10", 4370000L), // First Spurious block ForkIdManager.createIdEntry("0xa00bc324", 7280000L), // First Byzantium block ForkIdManager.createIdEntry("0x668db0af", 0L) // Today Petersburg block - }; List list = Arrays.asList(forksMainnet); ForkIdManager forkIdManager = @@ -328,4 +327,4 @@ public void check3MaximumsProperRLPEncoding() { ForkIdManager.ForkId decodedEntry = ForkIdManager.readFrom(in); assertThat(forkIdEntry.equals(decodedEntry)).isTrue(); } - } +} diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessageTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessageTest.java index 1e5ccbfd430..785c0223eb9 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessageTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessageTest.java @@ -76,7 +76,8 @@ public void serializeDeserializeWithForkId() { final Hash genesisHash = randHash(2L); final ForkIdManager.ForkId forkId = ForkIdManager.createIdEntry("0xa00bc334", 0L); - final MessageData msg = StatusMessage.create(version, networkId, td, bestHash, genesisHash, forkId); + final MessageData msg = + StatusMessage.create(version, networkId, td, bestHash, genesisHash, forkId); final StatusMessage copy = new StatusMessage(msg.getData()); From 5b5cac697fc096987520d0315f2fb8b90e57c77d Mon Sep 17 00:00:00 2001 From: SteveM Date: Sat, 30 Nov 2019 14:24:01 -0800 Subject: [PATCH 19/42] correct forkId test Signed-off-by: SteveM --- .../besu/ethereum/eth/manager/ForkIdManagerTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java index daf6d538dae..2eb8a2bb52b 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java @@ -314,14 +314,14 @@ public void check2ArbitraryProperRLPEncoding() { @Test public void check3MaximumsProperRLPEncoding() { - ForkIdManager.ForkId forkIdEntry = ForkIdManager.createIdEntry("0xffffffff", Long.MAX_VALUE); + ForkIdManager.ForkId forkIdEntry = + ForkIdManager.createIdEntry("0xffffffff", Long.parseUnsignedLong("ffffffffffffffff", 16)); BytesValueRLPOutput out = new BytesValueRLPOutput(); forkIdEntry.writeTo(out); - // ce84ffffffff88ffffffffffffffff; // Check value supplied in EIP-2124 spec via GO lang - // math.MaxUint64 String str1 = - "0xce84ffffffff887fffffffffffffff"; // Long.MAX_VALUE is smaller than GO lang math.MaxUint64 + "0xce84ffffffff88ffffffffffffffff"; // Check value supplied in EIP-2124 spec via GO lang BytesValue bytesValue = out.encoded(); + System.out.println(bytesValue); // todo remove dev item assertThat(str1.equals(bytesValue.toString())).isTrue(); BytesValueRLPInput in = new BytesValueRLPInput(bytesValue, false); ForkIdManager.ForkId decodedEntry = ForkIdManager.readFrom(in); From 14e56ba2116af78c0753ac607f5f36dafcc637f5 Mon Sep 17 00:00:00 2001 From: SteveM Date: Tue, 3 Dec 2019 00:17:19 -0800 Subject: [PATCH 20/42] reduce duplication, and check getForks order Signed-off-by: SteveM --- .../besu/config/GenesisConfigFileTest.java | 3 +- .../eth/manager/EthProtocolManager.java | 118 ++++++++++++------ .../eth/manager/EthProtocolManagerTest.java | 3 +- .../manager/EthProtocolManagerTestUtil.java | 3 +- 4 files changed, 83 insertions(+), 44 deletions(-) diff --git a/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java b/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java index 882ede49d9d..ac56a7479e3 100644 --- a/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java +++ b/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java @@ -296,8 +296,7 @@ public void testNoOverride() { public void shouldLoadMainnetForks() { final GenesisConfigFile config = GenesisConfigFile.mainnet(); - assertThat(config.getForks()) - .contains(1150000L, 1920000L, 2463000L, 2675000L, 2675000L, 4370000L, 7280000L); + assertThat(config.getForks()).containsSequence(1150000L, 1920000L, 2463000L, 2675000L, 4370000L, 7280000L); assertThat(config.getConfigOptions().getChainId()).hasValue(MAINNET_CHAIN_ID); } diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java index ba3bea1c02b..efda9efb88d 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java @@ -44,6 +44,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Optional; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicBoolean; @@ -82,7 +83,8 @@ public EthProtocolManager( final EthScheduler scheduler, final EthProtocolConfiguration ethereumWireProtocolConfiguration, final Clock clock, - final MetricsSystem metricsSystem) { + final MetricsSystem metricsSystem, + final ForkIdManager forkIdManager) { this.networkId = networkId; this.peerValidators = peerValidators; this.scheduler = scheduler; @@ -92,7 +94,15 @@ public EthProtocolManager( this.shutdown = new CountDownLatch(1); genesisHash = blockchain.getBlockHashByNumber(0L).get(); - forkIdManager = ForkIdManager.buildCollection(genesisHash); + this.forkIdManager = ForkIdManager.buildCollection(genesisHash); + // if(forks != null){ +// forkIdManager = ForkIdManager.buildCollection(genesisHash, forks, blockchain); +// } else { +// forkIdManager = ForkIdManager.buildCollection(genesisHash); +// } + + + ethPeers = new EthPeers(getSupportedProtocol(), clock, metricsSystem); ethMessages = new EthMessages(); @@ -129,7 +139,8 @@ public EthProtocolManager( new EthScheduler(syncWorkers, txWorkers, computationWorkers, metricsSystem), EthProtocolConfiguration.defaultConfig(), clock, - metricsSystem); + metricsSystem, + ForkIdManager.buildCollection(blockchain.getBlockHashByNumber(0L).get())); } public EthProtocolManager( @@ -153,48 +164,75 @@ public EthProtocolManager( new EthScheduler(syncWorkers, txWorkers, computationWorkers, metricsSystem), ethereumWireProtocolConfiguration, clock, - metricsSystem); + metricsSystem, + ForkIdManager.buildCollection(blockchain.getBlockHashByNumber(0L).get())); } public EthProtocolManager( - final Blockchain blockchain, - final WorldStateArchive worldStateArchive, - final BigInteger networkId, - final List peerValidators, - final boolean fastSyncEnabled, - final int syncWorkers, - final int txWorkers, - final int computationWorkers, - final Clock clock, - final MetricsSystem metricsSystem, - final EthProtocolConfiguration ethereumWireProtocolConfiguration, - final List forks) { - this.networkId = networkId; - this.peerValidators = peerValidators; - this.scheduler = new EthScheduler(syncWorkers, txWorkers, computationWorkers, metricsSystem); - this.blockchain = blockchain; - this.fastSyncEnabled = fastSyncEnabled; - - this.shutdown = new CountDownLatch(1); - genesisHash = blockchain.getBlockHashByNumber(0L).get(); - - forkIdManager = ForkIdManager.buildCollection(genesisHash, forks, blockchain); - - ethPeers = new EthPeers(getSupportedProtocol(), clock, metricsSystem); - ethMessages = new EthMessages(); - ethContext = new EthContext(ethPeers, ethMessages, scheduler); - - this.blockBroadcaster = new BlockBroadcaster(ethContext); - - // Run validators - for (final PeerValidator peerValidator : this.peerValidators) { - PeerValidatorRunner.runValidator(ethContext, peerValidator); - } - - // Set up request handlers - new EthServer(blockchain, worldStateArchive, ethMessages, ethereumWireProtocolConfiguration); + final Blockchain blockchain, + final WorldStateArchive worldStateArchive, + final BigInteger networkId, + final List peerValidators, + final boolean fastSyncEnabled, + final int syncWorkers, + final int txWorkers, + final int computationWorkers, + final Clock clock, + final MetricsSystem metricsSystem, + final EthProtocolConfiguration ethereumWireProtocolConfiguration, + final List forks) { + this( + blockchain, + worldStateArchive, + networkId, + peerValidators, + fastSyncEnabled, + new EthScheduler(syncWorkers, txWorkers, computationWorkers, metricsSystem), + ethereumWireProtocolConfiguration, + clock, + metricsSystem, + ForkIdManager.buildCollection(blockchain.getBlockHashByNumber(0L).get(), forks, blockchain)); } +// public EthProtocolManager( +// final Blockchain blockchain, +// final WorldStateArchive worldStateArchive, +// final BigInteger networkId, +// final List peerValidators, +// final boolean fastSyncEnabled, +// final int syncWorkers, +// final int txWorkers, +// final int computationWorkers, +// final Clock clock, +// final MetricsSystem metricsSystem, +// final EthProtocolConfiguration ethereumWireProtocolConfiguration, +// final List forks) { +// this.networkId = networkId; +// this.peerValidators = peerValidators; +// this.scheduler = new EthScheduler(syncWorkers, txWorkers, computationWorkers, metricsSystem); +// this.blockchain = blockchain; +// this.fastSyncEnabled = fastSyncEnabled; +// +// this.shutdown = new CountDownLatch(1); +// genesisHash = blockchain.getBlockHashByNumber(0L).get(); +// +// forkIdManager = ForkIdManager.buildCollection(genesisHash, forks, blockchain); +// +// ethPeers = new EthPeers(getSupportedProtocol(), clock, metricsSystem); +// ethMessages = new EthMessages(); +// ethContext = new EthContext(ethPeers, ethMessages, scheduler); +// +// this.blockBroadcaster = new BlockBroadcaster(ethContext); +// +// // Run validators +// for (final PeerValidator peerValidator : this.peerValidators) { +// PeerValidatorRunner.runValidator(ethContext, peerValidator); +// } +// +// // Set up request handlers +// new EthServer(blockchain, worldStateArchive, ethMessages, ethereumWireProtocolConfiguration); +// } + public EthContext ethContext() { return ethContext; } diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManagerTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManagerTest.java index 515ae46cbb9..fc5f6e39307 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManagerTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManagerTest.java @@ -1093,7 +1093,8 @@ public void transactionMessagesGoToTheCorrectExecutor() { ethScheduler, EthProtocolConfiguration.defaultConfig(), TestClock.fixed(), - metricsSystem)) { + metricsSystem, + ForkIdManager.buildCollection(blockchain.getBlockHashByNumber(0L).get()))) { // Create a transaction pool. This has a side effect of registering a listener for the // transactions message. diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManagerTestUtil.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManagerTestUtil.java index 325773668d9..3874223447c 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManagerTestUtil.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManagerTestUtil.java @@ -62,7 +62,8 @@ public static EthProtocolManager create( ethScheduler, EthProtocolConfiguration.defaultConfig(), TestClock.fixed(), - new NoOpMetricsSystem()); + new NoOpMetricsSystem(), + ForkIdManager.buildCollection(blockchain.getBlockHashByNumber(0L).get())); } public static EthProtocolManager create( From 36cbe981978c0c4b3b03c717571f5677394dd71c Mon Sep 17 00:00:00 2001 From: SteveM Date: Wed, 4 Dec 2019 09:16:52 -0800 Subject: [PATCH 21/42] replace ArrayDeque usage with ArrayList Signed-off-by: SteveM --- .../ethereum/eth/manager/ForkIdManager.java | 15 ++++++----- .../eth/manager/ForkIdManagerTest.java | 25 ++++++++----------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java index 2d14a3dd3c4..8878c238edb 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java @@ -23,7 +23,6 @@ import org.hyperledger.besu.util.bytes.BytesValue; import org.hyperledger.besu.util.bytes.BytesValues; -import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -37,7 +36,7 @@ public class ForkIdManager { private Long forkNext; private Long highestKnownFork = 0L; private ForkId lastKnownEntry = null; - private ArrayDeque forkAndHashList; + private List forkAndHashList; public ForkIdManager(final Hash genesisHash, final List forks, final Long currentHead) { this.genesisHash = genesisHash; @@ -45,7 +44,7 @@ public ForkIdManager(final Hash genesisHash, final List forks, final Long if (forks != null) { forkAndHashList = collectForksAndHashes(forks, currentHead); } else { - forkAndHashList = new ArrayDeque<>(); + forkAndHashList = new ArrayList<>(); } }; @@ -80,7 +79,7 @@ public static ForkId createIdEntry(final String hash, final String next) { return new ForkId(hash, next); } - public ArrayDeque getForkAndHashList() { + public List getForkAndHashList() { return this.forkAndHashList; } @@ -184,9 +183,9 @@ private boolean isRemoteAwareOfPresent(final BytesValue forkHash, final Long nex } // TODO: should sort these when first gathering the list of forks to ensure order - private ArrayDeque collectForksAndHashes(final List forks, final Long currentHead) { + private List collectForksAndHashes(final List forks, final Long currentHead) { boolean first = true; - ArrayDeque forkList = new ArrayDeque<>(); + ArrayList forkList = new ArrayList<>(); Iterator iterator = forks.iterator(); while (iterator.hasNext()) { Long forkBlockNumber = iterator.next(); @@ -348,8 +347,8 @@ public String toString() { public boolean equals(final Object obj) { if (obj instanceof ForkId) { ForkId other = (ForkId) obj; - Long thisNext = BytesValues.extractLong(next); - return other.getHash().equals(this.hash) && thisNext.equals(other.getNext()); + long thisNext = BytesValues.extractLong(next); + return other.getHash().equals(this.hash) && thisNext == other.getNext(); } return false; } diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java index 2eb8a2bb52b..af0ac306dff 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java @@ -22,7 +22,9 @@ import org.hyperledger.besu.util.bytes.BytesValue; import java.util.ArrayDeque; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Iterator; import java.util.List; import org.junit.Test; @@ -52,13 +54,12 @@ public void checkCorrectMainnetForkIdHashesGenerated() { List list = Arrays.asList(forksMainnet); ForkIdManager forkIdManager = ForkIdManager.buildCollection(Hash.fromHexString(mainnetGenHash), list); - ArrayDeque entries = forkIdManager.getForkAndHashList(); + Iterator entries = new ArrayList<>(forkIdManager.getForkAndHashList()).iterator(); for (ForkIdManager.ForkId id : checkIds) { - ForkIdManager.ForkId testVal = entries.poll(); - if (testVal == null) { + if (!entries.hasNext()) { break; } - assertThat(testVal.equals(id)).isTrue(); + assertThat(entries.next().equals(id)).isTrue(); } } @@ -76,14 +77,12 @@ public void checkCorrectRopstenForkIdHashesGenerated() { }; List list = Arrays.asList(forks); ForkIdManager forkIdManager = ForkIdManager.buildCollection(Hash.fromHexString(genHash), list); - ArrayDeque entries = forkIdManager.getForkAndHashList(); - + Iterator entries = new ArrayList<>(forkIdManager.getForkAndHashList()).iterator(); for (ForkIdManager.ForkId id : checkIds) { - ForkIdManager.ForkId testVal = entries.poll(); - if (testVal == null) { + if (!entries.hasNext()) { break; } - assertThat(testVal.equals(id)).isTrue(); + assertThat(entries.next().equals(id)).isTrue(); } } @@ -103,14 +102,12 @@ public void checkCorrectRinkebyForkIdHashesGenerated() { }; List list = Arrays.asList(forks); ForkIdManager forkIdManager = ForkIdManager.buildCollection(Hash.fromHexString(genHash), list); - ArrayDeque entries = forkIdManager.getForkAndHashList(); - + Iterator entries = new ArrayList<>(forkIdManager.getForkAndHashList()).iterator(); for (ForkIdManager.ForkId id : checkIds) { - ForkIdManager.ForkId testVal = entries.poll(); - if (testVal == null) { + if (!entries.hasNext()) { break; } - assertThat(testVal.equals(id)).isTrue(); + assertThat(entries.next().equals(id)).isTrue(); } } From ea69e62b5548bf1f8e9d3c41acdd220b3bc1619b Mon Sep 17 00:00:00 2001 From: SteveM Date: Wed, 4 Dec 2019 09:41:58 -0800 Subject: [PATCH 22/42] lint via spotlessApply Signed-off-by: SteveM --- .../besu/config/GenesisConfigFileTest.java | 3 +- .../eth/manager/EthProtocolManager.java | 139 +++++++++--------- .../manager/EthProtocolManagerTestUtil.java | 2 +- .../eth/manager/ForkIdManagerTest.java | 10 +- 4 files changed, 78 insertions(+), 76 deletions(-) diff --git a/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java b/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java index ac56a7479e3..08022895d95 100644 --- a/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java +++ b/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java @@ -296,7 +296,8 @@ public void testNoOverride() { public void shouldLoadMainnetForks() { final GenesisConfigFile config = GenesisConfigFile.mainnet(); - assertThat(config.getForks()).containsSequence(1150000L, 1920000L, 2463000L, 2675000L, 4370000L, 7280000L); + assertThat(config.getForks()) + .containsSequence(1150000L, 1920000L, 2463000L, 2675000L, 4370000L, 7280000L); assertThat(config.getConfigOptions().getChainId()).hasValue(MAINNET_CHAIN_ID); } diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java index efda9efb88d..ab557a39e22 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java @@ -44,7 +44,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.Optional; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicBoolean; @@ -96,13 +95,10 @@ public EthProtocolManager( this.forkIdManager = ForkIdManager.buildCollection(genesisHash); // if(forks != null){ -// forkIdManager = ForkIdManager.buildCollection(genesisHash, forks, blockchain); -// } else { -// forkIdManager = ForkIdManager.buildCollection(genesisHash); -// } - - - + // forkIdManager = ForkIdManager.buildCollection(genesisHash, forks, blockchain); + // } else { + // forkIdManager = ForkIdManager.buildCollection(genesisHash); + // } ethPeers = new EthPeers(getSupportedProtocol(), clock, metricsSystem); ethMessages = new EthMessages(); @@ -140,7 +136,7 @@ public EthProtocolManager( EthProtocolConfiguration.defaultConfig(), clock, metricsSystem, - ForkIdManager.buildCollection(blockchain.getBlockHashByNumber(0L).get())); + ForkIdManager.buildCollection(blockchain.getBlockHashByNumber(0L).get())); } public EthProtocolManager( @@ -165,73 +161,76 @@ public EthProtocolManager( ethereumWireProtocolConfiguration, clock, metricsSystem, - ForkIdManager.buildCollection(blockchain.getBlockHashByNumber(0L).get())); + ForkIdManager.buildCollection(blockchain.getBlockHashByNumber(0L).get())); } public EthProtocolManager( - final Blockchain blockchain, - final WorldStateArchive worldStateArchive, - final BigInteger networkId, - final List peerValidators, - final boolean fastSyncEnabled, - final int syncWorkers, - final int txWorkers, - final int computationWorkers, - final Clock clock, - final MetricsSystem metricsSystem, - final EthProtocolConfiguration ethereumWireProtocolConfiguration, - final List forks) { + final Blockchain blockchain, + final WorldStateArchive worldStateArchive, + final BigInteger networkId, + final List peerValidators, + final boolean fastSyncEnabled, + final int syncWorkers, + final int txWorkers, + final int computationWorkers, + final Clock clock, + final MetricsSystem metricsSystem, + final EthProtocolConfiguration ethereumWireProtocolConfiguration, + final List forks) { this( - blockchain, - worldStateArchive, - networkId, - peerValidators, - fastSyncEnabled, - new EthScheduler(syncWorkers, txWorkers, computationWorkers, metricsSystem), - ethereumWireProtocolConfiguration, - clock, - metricsSystem, - ForkIdManager.buildCollection(blockchain.getBlockHashByNumber(0L).get(), forks, blockchain)); + blockchain, + worldStateArchive, + networkId, + peerValidators, + fastSyncEnabled, + new EthScheduler(syncWorkers, txWorkers, computationWorkers, metricsSystem), + ethereumWireProtocolConfiguration, + clock, + metricsSystem, + ForkIdManager.buildCollection( + blockchain.getBlockHashByNumber(0L).get(), forks, blockchain)); } -// public EthProtocolManager( -// final Blockchain blockchain, -// final WorldStateArchive worldStateArchive, -// final BigInteger networkId, -// final List peerValidators, -// final boolean fastSyncEnabled, -// final int syncWorkers, -// final int txWorkers, -// final int computationWorkers, -// final Clock clock, -// final MetricsSystem metricsSystem, -// final EthProtocolConfiguration ethereumWireProtocolConfiguration, -// final List forks) { -// this.networkId = networkId; -// this.peerValidators = peerValidators; -// this.scheduler = new EthScheduler(syncWorkers, txWorkers, computationWorkers, metricsSystem); -// this.blockchain = blockchain; -// this.fastSyncEnabled = fastSyncEnabled; -// -// this.shutdown = new CountDownLatch(1); -// genesisHash = blockchain.getBlockHashByNumber(0L).get(); -// -// forkIdManager = ForkIdManager.buildCollection(genesisHash, forks, blockchain); -// -// ethPeers = new EthPeers(getSupportedProtocol(), clock, metricsSystem); -// ethMessages = new EthMessages(); -// ethContext = new EthContext(ethPeers, ethMessages, scheduler); -// -// this.blockBroadcaster = new BlockBroadcaster(ethContext); -// -// // Run validators -// for (final PeerValidator peerValidator : this.peerValidators) { -// PeerValidatorRunner.runValidator(ethContext, peerValidator); -// } -// -// // Set up request handlers -// new EthServer(blockchain, worldStateArchive, ethMessages, ethereumWireProtocolConfiguration); -// } + // public EthProtocolManager( + // final Blockchain blockchain, + // final WorldStateArchive worldStateArchive, + // final BigInteger networkId, + // final List peerValidators, + // final boolean fastSyncEnabled, + // final int syncWorkers, + // final int txWorkers, + // final int computationWorkers, + // final Clock clock, + // final MetricsSystem metricsSystem, + // final EthProtocolConfiguration ethereumWireProtocolConfiguration, + // final List forks) { + // this.networkId = networkId; + // this.peerValidators = peerValidators; + // this.scheduler = new EthScheduler(syncWorkers, txWorkers, computationWorkers, + // metricsSystem); + // this.blockchain = blockchain; + // this.fastSyncEnabled = fastSyncEnabled; + // + // this.shutdown = new CountDownLatch(1); + // genesisHash = blockchain.getBlockHashByNumber(0L).get(); + // + // forkIdManager = ForkIdManager.buildCollection(genesisHash, forks, blockchain); + // + // ethPeers = new EthPeers(getSupportedProtocol(), clock, metricsSystem); + // ethMessages = new EthMessages(); + // ethContext = new EthContext(ethPeers, ethMessages, scheduler); + // + // this.blockBroadcaster = new BlockBroadcaster(ethContext); + // + // // Run validators + // for (final PeerValidator peerValidator : this.peerValidators) { + // PeerValidatorRunner.runValidator(ethContext, peerValidator); + // } + // + // // Set up request handlers + // new EthServer(blockchain, worldStateArchive, ethMessages, + // ethereumWireProtocolConfiguration); + // } public EthContext ethContext() { return ethContext; diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManagerTestUtil.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManagerTestUtil.java index 3874223447c..c0f6188af9d 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManagerTestUtil.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManagerTestUtil.java @@ -63,7 +63,7 @@ public static EthProtocolManager create( EthProtocolConfiguration.defaultConfig(), TestClock.fixed(), new NoOpMetricsSystem(), - ForkIdManager.buildCollection(blockchain.getBlockHashByNumber(0L).get())); + ForkIdManager.buildCollection(blockchain.getBlockHashByNumber(0L).get())); } public static EthProtocolManager create( diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java index af0ac306dff..cc61361e630 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java @@ -21,7 +21,6 @@ import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput; import org.hyperledger.besu.util.bytes.BytesValue; -import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; @@ -54,7 +53,8 @@ public void checkCorrectMainnetForkIdHashesGenerated() { List list = Arrays.asList(forksMainnet); ForkIdManager forkIdManager = ForkIdManager.buildCollection(Hash.fromHexString(mainnetGenHash), list); - Iterator entries = new ArrayList<>(forkIdManager.getForkAndHashList()).iterator(); + Iterator entries = + new ArrayList<>(forkIdManager.getForkAndHashList()).iterator(); for (ForkIdManager.ForkId id : checkIds) { if (!entries.hasNext()) { break; @@ -77,7 +77,8 @@ public void checkCorrectRopstenForkIdHashesGenerated() { }; List list = Arrays.asList(forks); ForkIdManager forkIdManager = ForkIdManager.buildCollection(Hash.fromHexString(genHash), list); - Iterator entries = new ArrayList<>(forkIdManager.getForkAndHashList()).iterator(); + Iterator entries = + new ArrayList<>(forkIdManager.getForkAndHashList()).iterator(); for (ForkIdManager.ForkId id : checkIds) { if (!entries.hasNext()) { break; @@ -102,7 +103,8 @@ public void checkCorrectRinkebyForkIdHashesGenerated() { }; List list = Arrays.asList(forks); ForkIdManager forkIdManager = ForkIdManager.buildCollection(Hash.fromHexString(genHash), list); - Iterator entries = new ArrayList<>(forkIdManager.getForkAndHashList()).iterator(); + Iterator entries = + new ArrayList<>(forkIdManager.getForkAndHashList()).iterator(); for (ForkIdManager.ForkId id : checkIds) { if (!entries.hasNext()) { break; From 6ef95485cf6bba3e6ea616424108d5dfe3d683ab Mon Sep 17 00:00:00 2001 From: Ratan Rai Sur Date: Tue, 3 Dec 2019 14:20:05 -0500 Subject: [PATCH 23/42] enforce exhaustiveness of fork sequence in test Signed-off-by: Ratan Rai Sur --- .../java/org/hyperledger/besu/config/GenesisConfigFileTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java b/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java index 08022895d95..ab2a737dddd 100644 --- a/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java +++ b/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java @@ -297,7 +297,7 @@ public void shouldLoadMainnetForks() { final GenesisConfigFile config = GenesisConfigFile.mainnet(); assertThat(config.getForks()) - .containsSequence(1150000L, 1920000L, 2463000L, 2675000L, 4370000L, 7280000L); + .containsExactly(1150000L, 1920000L, 2463000L, 2675000L, 4370000L, 7280000L); assertThat(config.getConfigOptions().getChainId()).hasValue(MAINNET_CHAIN_ID); } From b3601da0afe36a7b9c857750d9dd346fc5dca217 Mon Sep 17 00:00:00 2001 From: Ratan Rai Sur Date: Tue, 3 Dec 2019 14:20:14 -0500 Subject: [PATCH 24/42] remove commented code Signed-off-by: Ratan Rai Sur --- .../eth/manager/EthProtocolManager.java | 46 ------------------- 1 file changed, 46 deletions(-) diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java index ab557a39e22..db5f067db6c 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java @@ -94,11 +94,6 @@ public EthProtocolManager( genesisHash = blockchain.getBlockHashByNumber(0L).get(); this.forkIdManager = ForkIdManager.buildCollection(genesisHash); - // if(forks != null){ - // forkIdManager = ForkIdManager.buildCollection(genesisHash, forks, blockchain); - // } else { - // forkIdManager = ForkIdManager.buildCollection(genesisHash); - // } ethPeers = new EthPeers(getSupportedProtocol(), clock, metricsSystem); ethMessages = new EthMessages(); @@ -191,47 +186,6 @@ public EthProtocolManager( blockchain.getBlockHashByNumber(0L).get(), forks, blockchain)); } - // public EthProtocolManager( - // final Blockchain blockchain, - // final WorldStateArchive worldStateArchive, - // final BigInteger networkId, - // final List peerValidators, - // final boolean fastSyncEnabled, - // final int syncWorkers, - // final int txWorkers, - // final int computationWorkers, - // final Clock clock, - // final MetricsSystem metricsSystem, - // final EthProtocolConfiguration ethereumWireProtocolConfiguration, - // final List forks) { - // this.networkId = networkId; - // this.peerValidators = peerValidators; - // this.scheduler = new EthScheduler(syncWorkers, txWorkers, computationWorkers, - // metricsSystem); - // this.blockchain = blockchain; - // this.fastSyncEnabled = fastSyncEnabled; - // - // this.shutdown = new CountDownLatch(1); - // genesisHash = blockchain.getBlockHashByNumber(0L).get(); - // - // forkIdManager = ForkIdManager.buildCollection(genesisHash, forks, blockchain); - // - // ethPeers = new EthPeers(getSupportedProtocol(), clock, metricsSystem); - // ethMessages = new EthMessages(); - // ethContext = new EthContext(ethPeers, ethMessages, scheduler); - // - // this.blockBroadcaster = new BlockBroadcaster(ethContext); - // - // // Run validators - // for (final PeerValidator peerValidator : this.peerValidators) { - // PeerValidatorRunner.runValidator(ethContext, peerValidator); - // } - // - // // Set up request handlers - // new EthServer(blockchain, worldStateArchive, ethMessages, - // ethereumWireProtocolConfiguration); - // } - public EthContext ethContext() { return ethContext; } From a1fb990bcb53bfcf600eca485121cd9a0f1f5918 Mon Sep 17 00:00:00 2001 From: Ratan Rai Sur Date: Tue, 3 Dec 2019 14:23:32 -0500 Subject: [PATCH 25/42] asserts Signed-off-by: Ratan Rai Sur --- .../org/hyperledger/besu/config/GenesisConfigFileTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java b/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java index ab2a737dddd..cbc4cc51b19 100644 --- a/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java +++ b/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java @@ -228,7 +228,7 @@ public void testOverridePresent() { override.put("chainId", bigBlockString); override.put("contractSizeLimit", bigBlockString); - assertThat(config.getForks().size()).isPositive(); + assertThat(config.getForks()).isNotEmpty(); assertThat(config.getConfigOptions(override).getIstanbulBlockNumber()).hasValue(bigBlock); assertThat(config.getConfigOptions(override).getChainId()) .hasValue(BigInteger.valueOf(bigBlock)); @@ -243,7 +243,7 @@ public void testOverrideNull() { override.put("chainId", null); override.put("contractSizeLimit", null); - assertThat(config.getForks().size()).isPositive(); + assertThat(config.getForks()).isNotEmpty(); assertThat(config.getConfigOptions(override).getIstanbulBlockNumber()).isNotPresent(); assertThat(config.getConfigOptions(override).getChainId()).isNotPresent(); assertThat(config.getConfigOptions(override).getContractSizeLimit()).isNotPresent(); From c226b3a614a76985a0ec7d744a90b2a8a95a657b Mon Sep 17 00:00:00 2001 From: Ratan Rai Sur Date: Tue, 3 Dec 2019 14:29:33 -0500 Subject: [PATCH 26/42] immutable list Signed-off-by: Ratan Rai Sur --- .../java/org/hyperledger/besu/config/GenesisConfigFile.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/src/main/java/org/hyperledger/besu/config/GenesisConfigFile.java b/config/src/main/java/org/hyperledger/besu/config/GenesisConfigFile.java index 7149e8e357b..4739926da27 100644 --- a/config/src/main/java/org/hyperledger/besu/config/GenesisConfigFile.java +++ b/config/src/main/java/org/hyperledger/besu/config/GenesisConfigFile.java @@ -16,6 +16,7 @@ import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.stream.Collectors.toList; +import static java.util.stream.Collectors.toUnmodifiableList; import static org.hyperledger.besu.config.JsonUtil.normalizeKeys; import java.io.IOException; @@ -143,7 +144,7 @@ public List getForks() { .filter(name -> name.contains("block")) .map(name -> node.get(name).asLong())) .distinct() - .collect(toList()); + .collect(toUnmodifiableList()); } private String getRequiredString(final String key) { From 1dd5486dd1c7df2f2d0ed5c160866af5f10a1821 Mon Sep 17 00:00:00 2001 From: Ratan Rai Sur Date: Tue, 3 Dec 2019 14:39:38 -0500 Subject: [PATCH 27/42] functional style Signed-off-by: Ratan Rai Sur --- .../besu/config/GenesisConfigFile.java | 1 - .../ethereum/eth/manager/ForkIdManager.java | 24 ++++--------- .../eth/manager/ForkIdManagerTest.java | 34 +++++-------------- 3 files changed, 15 insertions(+), 44 deletions(-) diff --git a/config/src/main/java/org/hyperledger/besu/config/GenesisConfigFile.java b/config/src/main/java/org/hyperledger/besu/config/GenesisConfigFile.java index 4739926da27..b7defb77d40 100644 --- a/config/src/main/java/org/hyperledger/besu/config/GenesisConfigFile.java +++ b/config/src/main/java/org/hyperledger/besu/config/GenesisConfigFile.java @@ -15,7 +15,6 @@ package org.hyperledger.besu.config; import static java.nio.charset.StandardCharsets.UTF_8; -import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toUnmodifiableList; import static org.hyperledger.besu.config.JsonUtil.normalizeKeys; diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java index 8878c238edb..abb9b8aae75 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java @@ -14,6 +14,8 @@ */ package org.hyperledger.besu.ethereum.eth.manager; +import static java.util.Collections.emptyList; + import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.core.Hash; import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput; @@ -44,7 +46,7 @@ public ForkIdManager(final Hash genesisHash, final List forks, final Long if (forks != null) { forkAndHashList = collectForksAndHashes(forks, currentHead); } else { - forkAndHashList = new ArrayList<>(); + forkAndHashList = emptyList(); } }; @@ -147,24 +149,12 @@ public boolean peerCheck(final Bytes32 peerGenesisHash) { } private boolean isHashKnown(final BytesValue forkHash) { - for (ForkId j : forkAndHashList) { - if (forkHash.equals(j.getHash())) { - return true; - } - } - return false; + return forkAndHashList.stream().map(ForkId::getHash).anyMatch(hash -> hash.equals(forkHash)); } private boolean isForkKnown(final Long nextFork) { - if (highestKnownFork < nextFork) { - return true; - } - for (ForkId j : forkAndHashList) { - if (nextFork.equals(j.getNext())) { - return true; - } - } - return false; + return highestKnownFork < nextFork + || forkAndHashList.stream().map(ForkId::getNext).anyMatch(fork -> fork.equals(nextFork)); } private boolean isRemoteAwareOfPresent(final BytesValue forkHash, final Long nextFork) { @@ -185,7 +175,7 @@ private boolean isRemoteAwareOfPresent(final BytesValue forkHash, final Long nex // TODO: should sort these when first gathering the list of forks to ensure order private List collectForksAndHashes(final List forks, final Long currentHead) { boolean first = true; - ArrayList forkList = new ArrayList<>(); + List forkList = new ArrayList<>(); Iterator iterator = forks.iterator(); while (iterator.hasNext()) { Long forkBlockNumber = iterator.next(); diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java index cc61361e630..4aed19b6486 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java @@ -21,9 +21,7 @@ import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput; import org.hyperledger.besu.util.bytes.BytesValue; -import java.util.ArrayList; import java.util.Arrays; -import java.util.Iterator; import java.util.List; import org.junit.Test; @@ -53,14 +51,8 @@ public void checkCorrectMainnetForkIdHashesGenerated() { List list = Arrays.asList(forksMainnet); ForkIdManager forkIdManager = ForkIdManager.buildCollection(Hash.fromHexString(mainnetGenHash), list); - Iterator entries = - new ArrayList<>(forkIdManager.getForkAndHashList()).iterator(); - for (ForkIdManager.ForkId id : checkIds) { - if (!entries.hasNext()) { - break; - } - assertThat(entries.next().equals(id)).isTrue(); - } + List entries = forkIdManager.getForkAndHashList(); + assertThat(entries).containsExactly(checkIds); } @Test @@ -77,14 +69,9 @@ public void checkCorrectRopstenForkIdHashesGenerated() { }; List list = Arrays.asList(forks); ForkIdManager forkIdManager = ForkIdManager.buildCollection(Hash.fromHexString(genHash), list); - Iterator entries = - new ArrayList<>(forkIdManager.getForkAndHashList()).iterator(); - for (ForkIdManager.ForkId id : checkIds) { - if (!entries.hasNext()) { - break; - } - assertThat(entries.next().equals(id)).isTrue(); - } + List entries = forkIdManager.getForkAndHashList(); + + assertThat(entries).containsExactly(checkIds); } @Test @@ -103,14 +90,9 @@ public void checkCorrectRinkebyForkIdHashesGenerated() { }; List list = Arrays.asList(forks); ForkIdManager forkIdManager = ForkIdManager.buildCollection(Hash.fromHexString(genHash), list); - Iterator entries = - new ArrayList<>(forkIdManager.getForkAndHashList()).iterator(); - for (ForkIdManager.ForkId id : checkIds) { - if (!entries.hasNext()) { - break; - } - assertThat(entries.next().equals(id)).isTrue(); - } + List entries = forkIdManager.getForkAndHashList(); + + assertThat(entries).containsExactly(checkIds); } @Test From 2280d86b8959bc4ee915d72232438fba962e940a Mon Sep 17 00:00:00 2001 From: Ratan Rai Sur Date: Wed, 4 Dec 2019 11:47:21 -0500 Subject: [PATCH 28/42] Math.max Signed-off-by: Ratan Rai Sur --- .../hyperledger/besu/ethereum/eth/manager/ForkIdManager.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java index abb9b8aae75..5818cb19ee4 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java @@ -179,9 +179,7 @@ private List collectForksAndHashes(final List forks, final Long cu Iterator iterator = forks.iterator(); while (iterator.hasNext()) { Long forkBlockNumber = iterator.next(); - if (highestKnownFork < forkBlockNumber) { - highestKnownFork = forkBlockNumber; - } + highestKnownFork = Math.max(highestKnownFork, forkBlockNumber); if (first) { // first fork first = false; From 1bdeb0bac13ea0e52825fd582f3a04b0d5146f53 Mon Sep 17 00:00:00 2001 From: Ratan Rai Sur Date: Wed, 4 Dec 2019 13:39:53 -0500 Subject: [PATCH 29/42] add final Signed-off-by: Ratan Rai Sur --- .../besu/ethereum/eth/manager/ForkIdManager.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java index 5818cb19ee4..35cf9521f62 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java @@ -227,8 +227,8 @@ private BytesValue getCurrentCrcHash() { } public static class ForkId { - BytesValue hash; - BytesValue next; + final BytesValue hash; + final BytesValue next; BytesValue forkIdRLP; ForkId(final BytesValue hash, final BytesValue next) { @@ -238,10 +238,8 @@ public static class ForkId { } ForkId(final String hash, final String next) { - this.hash = BytesValue.fromHexString((hash.length() % 2 == 0 ? "" : "0") + hash); - if (this.hash.size() < 4) { - this.hash = padToEightBytes(this.hash); - } + this.hash = + padToEightBytes(BytesValue.fromHexString((hash.length() % 2 == 0 ? "" : "0") + hash)); if (next.equals("") || next.equals("0x")) { this.next = BytesValue.EMPTY; } else if (next.startsWith("0x")) { From 1130695a23e2f2bc5bd027a4a1e16adc0694a7f8 Mon Sep 17 00:00:00 2001 From: Ratan Rai Sur Date: Wed, 4 Dec 2019 14:06:48 -0500 Subject: [PATCH 30/42] rename Signed-off-by: Ratan Rai Sur --- .../hyperledger/besu/ethereum/eth/manager/ForkIdManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java index 35cf9521f62..f36ffe3f988 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java @@ -44,7 +44,7 @@ public ForkIdManager(final Hash genesisHash, final List forks, final Long this.genesisHash = genesisHash; this.currentHead = currentHead; if (forks != null) { - forkAndHashList = collectForksAndHashes(forks, currentHead); + forkAndHashList = createForkIds(forks, currentHead); } else { forkAndHashList = emptyList(); } @@ -173,7 +173,7 @@ private boolean isRemoteAwareOfPresent(final BytesValue forkHash, final Long nex } // TODO: should sort these when first gathering the list of forks to ensure order - private List collectForksAndHashes(final List forks, final Long currentHead) { + private List createForkIds(final List forks, final Long currentHead) { boolean first = true; List forkList = new ArrayList<>(); Iterator iterator = forks.iterator(); From 4b7a09013d0e386acd1575c9714f692fb94dadef Mon Sep 17 00:00:00 2001 From: Ratan Rai Sur Date: Sun, 8 Dec 2019 10:00:11 -0500 Subject: [PATCH 31/42] cleanup fork generation Signed-off-by: Ratan Rai Sur --- .../ethereum/eth/manager/ForkIdManager.java | 88 ++++++------------- .../eth/manager/ForkIdManagerTest.java | 4 +- 2 files changed, 31 insertions(+), 61 deletions(-) diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java index f36ffe3f988..5fd2e4da076 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java @@ -24,6 +24,7 @@ import org.hyperledger.besu.util.bytes.Bytes32; import org.hyperledger.besu.util.bytes.BytesValue; import org.hyperledger.besu.util.bytes.BytesValues; +import org.openjdk.jmh.annotations.Fork; import java.util.ArrayList; import java.util.Iterator; @@ -33,7 +34,6 @@ public class ForkIdManager { private Hash genesisHash; - private CRC32 crc = new CRC32(); private Long currentHead; private Long forkNext; private Long highestKnownFork = 0L; @@ -43,32 +43,20 @@ public class ForkIdManager { public ForkIdManager(final Hash genesisHash, final List forks, final Long currentHead) { this.genesisHash = genesisHash; this.currentHead = currentHead; - if (forks != null) { - forkAndHashList = createForkIds(forks, currentHead); - } else { - forkAndHashList = emptyList(); - } + this.forkAndHashList = createForkIds(forks); }; static ForkIdManager buildCollection( final Hash genesisHash, final List forks, final Blockchain blockchain) { - if (forks == null) { - return new ForkIdManager(genesisHash, null, blockchain.getChainHeadBlockNumber()); - } else { - return new ForkIdManager(genesisHash, forks, blockchain.getChainHeadBlockNumber()); - } + return new ForkIdManager(genesisHash, forks, blockchain.getChainHeadBlockNumber()); }; public static ForkIdManager buildCollection(final Hash genesisHash, final List forks) { - if (forks == null) { - return new ForkIdManager(genesisHash, null, Long.MAX_VALUE); - } else { - return new ForkIdManager(genesisHash, forks, Long.MAX_VALUE); - } + return new ForkIdManager(genesisHash, forks, Long.MAX_VALUE); }; static ForkIdManager buildCollection(final Hash genesisHash) { - return new ForkIdManager(genesisHash, null, Long.MAX_VALUE); + return new ForkIdManager(genesisHash, emptyList(), Long.MAX_VALUE); }; // Non-generated entry (for tests) @@ -173,56 +161,36 @@ private boolean isRemoteAwareOfPresent(final BytesValue forkHash, final Long nex } // TODO: should sort these when first gathering the list of forks to ensure order - private List createForkIds(final List forks, final Long currentHead) { - boolean first = true; - List forkList = new ArrayList<>(); - Iterator iterator = forks.iterator(); - while (iterator.hasNext()) { - Long forkBlockNumber = iterator.next(); - highestKnownFork = Math.max(highestKnownFork, forkBlockNumber); - if (first) { - // first fork - first = false; - forkList.add( - new ForkId(updateCrc(this.genesisHash.getHexString()), forkBlockNumber)); // Genesis - updateCrc(forkBlockNumber); - - } else if (!iterator.hasNext()) { - // most recent fork - forkList.add(new ForkId(getCurrentCrcHash(), forkBlockNumber)); - updateCrc(forkBlockNumber); - // next fork or no future fork known - if (currentHead > forkBlockNumber) { - lastKnownEntry = new ForkId(getCurrentCrcHash(), 0L); - forkList.add(lastKnownEntry); - this.forkNext = 0L; - } else { - lastKnownEntry = new ForkId(getCurrentCrcHash(), forkBlockNumber); - forkList.add(lastKnownEntry); - this.forkNext = forkBlockNumber; - } - - } else { - forkList.add(new ForkId(getCurrentCrcHash(), forkBlockNumber)); - updateCrc(forkBlockNumber); - } + private List createForkIds(final List forks) { + final CRC32 crc = new CRC32(); + crc.update(this.genesisHash.getByteArray()); + final List forkHashes = new ArrayList<>(List.of(getCurrentCrcHash(crc))); + for (final Long fork : forks) { + updateCrc(crc, fork); + forkHashes.add(getCurrentCrcHash(crc)); + } + final List forkIds = new ArrayList<>(); + // This loop is for all the fork hashes that have an associated "next fork" + for (int i = 0; i < forks.size(); i++) { + forkIds.add(new ForkId(forkHashes.get(i), forks.get(i))); } - return forkList; + if (!forks.isEmpty()) { + this.forkNext = forkIds.get(forkIds.size() - 1).getNext(); + forkIds.add( + new ForkId( + forkHashes.get(forkHashes.size() - 1), + currentHead > forkNext ? 0 : forkNext // Use 0 if there are no known next forks + )); + } + return forkIds; } - private void updateCrc(final Long block) { + private void updateCrc(final CRC32 crc, final Long block) { byte[] byteRepresentationFork = longToBigEndian(block); crc.update(byteRepresentationFork, 0, byteRepresentationFork.length); } - private BytesValue updateCrc(final String hash) { - BytesValue bv = BytesValue.fromHexString(hash); - byte[] byteRepresentation = bv.extractArray(); - crc.update(byteRepresentation, 0, byteRepresentation.length); - return getCurrentCrcHash(); - } - - private BytesValue getCurrentCrcHash() { + private BytesValue getCurrentCrcHash(final CRC32 crc) { return BytesValues.ofUnsignedInt(crc.getValue()); } diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java index 4aed19b6486..6685044d927 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java @@ -22,6 +22,7 @@ import org.hyperledger.besu.util.bytes.BytesValue; import java.util.Arrays; +import java.util.Collections; import java.util.List; import org.junit.Test; @@ -33,7 +34,8 @@ public class ForkIdManagerTest { @Test public void checkItFunctionsWithPresentBehavior() { - ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), null, null); + ForkIdManager forkIdManager = + new ForkIdManager(Hash.fromHexString(mainnetGenHash), Collections.emptyList(), null); assertThat(forkIdManager.peerCheck(Hash.fromHexString(mainnetGenHash))).isFalse(); } From 5839ecd12e4f9d6d7529c0ec79c5270ea684e010 Mon Sep 17 00:00:00 2001 From: Ratan Rai Sur Date: Mon, 9 Dec 2019 09:54:11 -0500 Subject: [PATCH 32/42] visible for testing Signed-off-by: Ratan Rai Sur --- .../hyperledger/besu/ethereum/eth/manager/ForkIdManager.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java index 5fd2e4da076..e05e9dd2cb7 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java @@ -16,6 +16,7 @@ import static java.util.Collections.emptyList; +import com.google.common.annotations.VisibleForTesting; import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.core.Hash; import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput; @@ -51,6 +52,7 @@ static ForkIdManager buildCollection( return new ForkIdManager(genesisHash, forks, blockchain.getChainHeadBlockNumber()); }; + @VisibleForTesting public static ForkIdManager buildCollection(final Hash genesisHash, final List forks) { return new ForkIdManager(genesisHash, forks, Long.MAX_VALUE); }; From b68a84dd8afb4920d85bc905b6682e361143dcea Mon Sep 17 00:00:00 2001 From: Ratan Rai Sur Date: Mon, 9 Dec 2019 09:59:42 -0500 Subject: [PATCH 33/42] spotless Signed-off-by: Ratan Rai Sur --- .../hyperledger/besu/ethereum/eth/manager/ForkIdManager.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java index e05e9dd2cb7..855da1512a7 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java @@ -16,7 +16,6 @@ import static java.util.Collections.emptyList; -import com.google.common.annotations.VisibleForTesting; import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.core.Hash; import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput; @@ -25,13 +24,13 @@ import org.hyperledger.besu.util.bytes.Bytes32; import org.hyperledger.besu.util.bytes.BytesValue; import org.hyperledger.besu.util.bytes.BytesValues; -import org.openjdk.jmh.annotations.Fork; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; import java.util.zip.CRC32; +import com.google.common.annotations.VisibleForTesting; + public class ForkIdManager { private Hash genesisHash; From 28d507e5b1da0415bd10d672a908d7cb9a05216f Mon Sep 17 00:00:00 2001 From: Ratan Rai Sur Date: Mon, 9 Dec 2019 15:50:54 -0500 Subject: [PATCH 34/42] add ordering to get forks test Signed-off-by: Ratan Rai Sur --- .../besu/config/GenesisConfigFile.java | 5 ++- config/src/main/resources/mainnet.json | 2 +- .../besu/config/GenesisConfigFileTest.java | 42 +++++++++++++------ .../valid_config_with_custom_forks.json | 2 +- .../ethereum/eth/manager/ForkIdManager.java | 8 +++- 5 files changed, 41 insertions(+), 18 deletions(-) diff --git a/config/src/main/java/org/hyperledger/besu/config/GenesisConfigFile.java b/config/src/main/java/org/hyperledger/besu/config/GenesisConfigFile.java index b7defb77d40..c2351e899a0 100644 --- a/config/src/main/java/org/hyperledger/besu/config/GenesisConfigFile.java +++ b/config/src/main/java/org/hyperledger/besu/config/GenesisConfigFile.java @@ -138,11 +138,12 @@ public List getForks() { .flatMap( node -> Streams.stream(node.fieldNames()) - .filter(name -> !name.toLowerCase().equals("chainid")) + .map(String::toLowerCase) + .filter(name -> !name.equals("chainid")) .filter(name -> node.get(name).canConvertToLong()) .filter(name -> name.contains("block")) .map(name -> node.get(name).asLong())) - .distinct() + .sorted() .collect(toUnmodifiableList()); } diff --git a/config/src/main/resources/mainnet.json b/config/src/main/resources/mainnet.json index f23d41cd3d8..a397fec74b2 100644 --- a/config/src/main/resources/mainnet.json +++ b/config/src/main/resources/mainnet.json @@ -1,8 +1,8 @@ { "config": { "chainId": 1, - "homesteadBlock": 1150000, "daoForkBlock": 1920000, + "homesteadBlock": 1150000, "daoForkSupport": true, "eip150Block": 2463000, "eip150Hash": "0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0", diff --git a/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java b/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java index cbc4cc51b19..9c7fd8beaa6 100644 --- a/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java +++ b/config/src/test/java/org/hyperledger/besu/config/GenesisConfigFileTest.java @@ -16,8 +16,11 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.hyperledger.besu.config.GenesisConfigFile.fromConfig; +import java.io.IOException; import java.math.BigInteger; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; @@ -25,6 +28,9 @@ import java.util.stream.Collectors; import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.google.common.io.Resources; import org.assertj.core.api.ThrowableAssert.ThrowingCallable; import org.junit.Test; @@ -32,7 +38,7 @@ public class GenesisConfigFileTest { private static final BigInteger MAINNET_CHAIN_ID = BigInteger.ONE; private static final BigInteger DEVELOPMENT_CHAIN_ID = BigInteger.valueOf(2018); - private static final GenesisConfigFile EMPTY_CONFIG = GenesisConfigFile.fromConfig("{}"); + private static final GenesisConfigFile EMPTY_CONFIG = fromConfig("{}"); @Test public void shouldLoadMainnetConfigFile() { @@ -143,7 +149,7 @@ public void shouldDefaultTimestampToZero() { @Test public void shouldGetAllocations() { final GenesisConfigFile config = - GenesisConfigFile.fromConfig( + fromConfig( "{" + " \"alloc\": {" + " \"fe3b557e8fb62b89f4916b721be55ceb828dbd73\": {" @@ -193,14 +199,14 @@ public void shouldGetAllocations() { @Test public void shouldGetEmptyAllocationsWhenAllocNotPresent() { - final GenesisConfigFile config = GenesisConfigFile.fromConfig("{}"); + final GenesisConfigFile config = fromConfig("{}"); assertThat(config.streamAllocations()).isEmpty(); } @Test public void shouldGetLargeChainId() { final GenesisConfigFile config = - GenesisConfigFile.fromConfig( + fromConfig( "{\"config\": { \"chainId\": 31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095 }}"); assertThat(config.getConfigOptions().getChainId()) .contains( @@ -212,7 +218,7 @@ public void shouldGetLargeChainId() { public void mustNotAcceptComments() { assertThatThrownBy( () -> - GenesisConfigFile.fromConfig( + fromConfig( "{\"config\": { \"chainId\": 2017 }\n/* C comment }*/\n//C++ comment }\n}")) .hasCauseInstanceOf(JsonParseException.class) .hasMessageContaining("Unexpected character ('/'"); @@ -293,16 +299,28 @@ public void testNoOverride() { } @Test - public void shouldLoadMainnetForks() { - final GenesisConfigFile config = GenesisConfigFile.mainnet(); - - assertThat(config.getForks()) - .containsExactly(1150000L, 1920000L, 2463000L, 2675000L, 4370000L, 7280000L); - assertThat(config.getConfigOptions().getChainId()).hasValue(MAINNET_CHAIN_ID); + public void shouldLoadForksInSortedOrder() throws IOException { + final ObjectNode configNode = + new ObjectMapper() + .createObjectNode() + .set( + "config", + JsonUtil.objectNodeFromString( + Resources.toString( + Resources.getResource( + // If you inspect this config, you should see that fork block 2 is + // declared before 1 + "valid_config_with_custom_forks.json"), + StandardCharsets.UTF_8))); + + final GenesisConfigFile config = fromConfig(configNode); + + assertThat(config.getForks()).containsExactly(1L, 2L, 3L, 3L, 1035301L); + assertThat(config.getConfigOptions().getChainId()).hasValue(BigInteger.valueOf(4)); } private GenesisConfigFile configWithProperty(final String key, final String value) { - return GenesisConfigFile.fromConfig("{\"" + key + "\":\"" + value + "\"}"); + return fromConfig("{\"" + key + "\":\"" + value + "\"}"); } private void assertInvalidConfiguration(final ThrowingCallable getter) { diff --git a/config/src/test/resources/valid_config_with_custom_forks.json b/config/src/test/resources/valid_config_with_custom_forks.json index 6fa2c49861f..22e7b12767f 100644 --- a/config/src/test/resources/valid_config_with_custom_forks.json +++ b/config/src/test/resources/valid_config_with_custom_forks.json @@ -1,7 +1,7 @@ { "chainId": 4, - "homesteadBlock": 1, "eip150Block": 2, + "homesteadBlock": 1, "eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000", "eip155Block": 3, "eip158Block": 3, diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java index 855da1512a7..b7938f158d5 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java @@ -27,6 +27,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; import java.util.zip.CRC32; import com.google.common.annotations.VisibleForTesting; @@ -43,7 +44,11 @@ public class ForkIdManager { public ForkIdManager(final Hash genesisHash, final List forks, final Long currentHead) { this.genesisHash = genesisHash; this.currentHead = currentHead; - this.forkAndHashList = createForkIds(forks); + this.forkAndHashList = + createForkIds( + // If there are two forks at the same block height, we only want to add it once to the + // crc checksum + forks.stream().distinct().collect(Collectors.toUnmodifiableList())); }; static ForkIdManager buildCollection( @@ -161,7 +166,6 @@ private boolean isRemoteAwareOfPresent(final BytesValue forkHash, final Long nex return false; } - // TODO: should sort these when first gathering the list of forks to ensure order private List createForkIds(final List forks) { final CRC32 crc = new CRC32(); crc.update(this.genesisHash.getByteArray()); From 4d4b187f2c9c5710f5b5c21a48bc8241b57ee2e5 Mon Sep 17 00:00:00 2001 From: Ratan Rai Sur Date: Tue, 10 Dec 2019 12:15:38 -0500 Subject: [PATCH 35/42] remove unused methods Signed-off-by: Ratan Rai Sur --- .../ethereum/eth/manager/ForkIdManager.java | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java index b7938f158d5..8e35d9afcb3 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java @@ -257,16 +257,6 @@ public void writeTo(final RLPOutput out) { out.endList(); } - // Non-RLP entry (for tests) - public BytesValue createNotAsListForkIdRLP() { - BytesValueRLPOutput outPlain = new BytesValueRLPOutput(); - outPlain.startList(); - outPlain.writeBytesValue(hash); - outPlain.writeBytesValue(next); - outPlain.endList(); - return outPlain.encoded(); - } - public static ForkId readFrom(final RLPInput in) { in.enterList(); final BytesValue hash = in.readBytesValue(); @@ -275,13 +265,6 @@ public static ForkId readFrom(final RLPInput in) { return new ForkId(hash, next); } - public List asByteList() { - ArrayList forRLP = new ArrayList(); - forRLP.add(hash.getByteArray()); - forRLP.add(next.getByteArray()); - return forRLP; - } - public List asList() { ArrayList forRLP = new ArrayList<>(); forRLP.add(this); From f72f0e4489078715d43b70577d99d6b1058a9f59 Mon Sep 17 00:00:00 2001 From: SteveM Date: Mon, 30 Dec 2019 17:16:25 -0800 Subject: [PATCH 36/42] update to use tuweni bytes Signed-off-by: SteveM --- .../ethereum/eth/manager/ForkIdManager.java | 61 +++++++++---------- .../ethereum/eth/messages/StatusMessage.java | 12 ++-- .../eth/manager/ForkIdManagerTest.java | 11 ++-- .../eth/messages/StatusMessageTest.java | 2 +- 4 files changed, 41 insertions(+), 45 deletions(-) diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java index 8e35d9afcb3..f19f5823cf6 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java @@ -21,9 +21,8 @@ import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput; import org.hyperledger.besu.ethereum.rlp.RLPInput; import org.hyperledger.besu.ethereum.rlp.RLPOutput; -import org.hyperledger.besu.util.bytes.Bytes32; -import org.hyperledger.besu.util.bytes.BytesValue; -import org.hyperledger.besu.util.bytes.BytesValues; +import org.apache.tuweni.bytes.Bytes; +import org.apache.tuweni.bytes.Bytes32; import java.util.ArrayList; import java.util.List; @@ -85,8 +84,8 @@ ForkId getLatestForkId() { public static ForkId readFrom(final RLPInput in) { in.enterList(); - final BytesValue hash = in.readBytesValue(); - final BytesValue next = in.readBytesValue(); + final Bytes hash = in.readBytes(); + final Bytes next = in.readBytes(); in.leaveList(); return new ForkId(hash, next); } @@ -142,7 +141,7 @@ public boolean peerCheck(final Bytes32 peerGenesisHash) { return !peerGenesisHash.equals(genesisHash); } - private boolean isHashKnown(final BytesValue forkHash) { + private boolean isHashKnown(final Bytes forkHash) { return forkAndHashList.stream().map(ForkId::getHash).anyMatch(hash -> hash.equals(forkHash)); } @@ -151,7 +150,7 @@ private boolean isForkKnown(final Long nextFork) { || forkAndHashList.stream().map(ForkId::getNext).anyMatch(fork -> fork.equals(nextFork)); } - private boolean isRemoteAwareOfPresent(final BytesValue forkHash, final Long nextFork) { + private boolean isRemoteAwareOfPresent(final Bytes forkHash, final Long nextFork) { for (ForkId j : forkAndHashList) { if (forkHash.equals(j.getHash())) { if (nextFork.equals(j.getNext())) { @@ -169,7 +168,7 @@ private boolean isRemoteAwareOfPresent(final BytesValue forkHash, final Long nex private List createForkIds(final List forks) { final CRC32 crc = new CRC32(); crc.update(this.genesisHash.getByteArray()); - final List forkHashes = new ArrayList<>(List.of(getCurrentCrcHash(crc))); + final List forkHashes = new ArrayList<>(List.of(getCurrentCrcHash(crc))); for (final Long fork : forks) { updateCrc(crc, fork); forkHashes.add(getCurrentCrcHash(crc)); @@ -195,16 +194,16 @@ private void updateCrc(final CRC32 crc, final Long block) { crc.update(byteRepresentationFork, 0, byteRepresentationFork.length); } - private BytesValue getCurrentCrcHash(final CRC32 crc) { - return BytesValues.ofUnsignedInt(crc.getValue()); + private Bytes getCurrentCrcHash(final CRC32 crc) { + return Bytes.ofUnsignedInt(crc.getValue()); } public static class ForkId { - final BytesValue hash; - final BytesValue next; - BytesValue forkIdRLP; + final Bytes hash; + final Bytes next; + Bytes forkIdRLP; - ForkId(final BytesValue hash, final BytesValue next) { + ForkId(final Bytes hash, final Bytes next) { this.hash = hash; this.next = next; createForkIdRLP(); @@ -212,35 +211,35 @@ public static class ForkId { ForkId(final String hash, final String next) { this.hash = - padToEightBytes(BytesValue.fromHexString((hash.length() % 2 == 0 ? "" : "0") + hash)); + padToEightBytes(Bytes.fromHexString((hash.length() % 2 == 0 ? "" : "0") + hash)); if (next.equals("") || next.equals("0x")) { - this.next = BytesValue.EMPTY; + this.next = Bytes.EMPTY; } else if (next.startsWith("0x")) { long asLong = Long.parseLong(next.replaceFirst("0x", ""), 16); - this.next = BytesValues.trimLeadingZeros(BytesValue.wrap(longToBigEndian(asLong))); + this.next = Bytes.wrap(longToBigEndian(asLong)).trimLeadingZeros(); } else { - this.next = BytesValue.wrap(longToBigEndian(Long.parseLong(next))); + this.next = Bytes.wrap(longToBigEndian(Long.parseLong(next))); } createForkIdRLP(); } ForkId(final String hash, final long next) { - this.hash = BytesValue.fromHexString(hash); - this.next = BytesValue.wrap(longToBigEndian(next)); + this.hash = Bytes.fromHexString(hash); + this.next = Bytes.wrap(longToBigEndian(next)); createForkIdRLP(); } - ForkId(final BytesValue hash, final long next) { + ForkId(final Bytes hash, final long next) { this.hash = hash; - this.next = BytesValue.wrap(longToBigEndian(next)); + this.next = Bytes.wrap(longToBigEndian(next)); createForkIdRLP(); } public long getNext() { - return BytesValues.extractLong(next); + return next.toLong(); } - public BytesValue getHash() { + public Bytes getHash() { return hash; } @@ -252,14 +251,14 @@ void createForkIdRLP() { public void writeTo(final RLPOutput out) { out.startList(); - out.writeBytesValue(hash); - out.writeBytesValue(next); + out.writeBytes(hash); + out.writeBytes(next); out.endList(); } public static ForkId readFrom(final RLPInput in) { in.enterList(); - final BytesValue hash = in.readBytesValue(); + final Bytes hash = in.readBytes(); final long next = in.readLong(); in.leaveList(); return new ForkId(hash, next); @@ -271,9 +270,9 @@ public List asList() { return forRLP; } - private static BytesValue padToEightBytes(final BytesValue hash) { + private static Bytes padToEightBytes(final Bytes hash) { if (hash.size() < 4) { - BytesValue padded = BytesValues.concatenate(hash, BytesValue.fromHexString("0x00")); + Bytes padded = Bytes.concatenate(hash, Bytes.fromHexString("0x00")); return padToEightBytes(padded); } else { return hash; @@ -282,14 +281,14 @@ private static BytesValue padToEightBytes(final BytesValue hash) { @Override public String toString() { - return "ForkId(hash=" + this.hash + ", next=" + BytesValues.extractLong(next) + ")"; + return "ForkId(hash=" + this.hash + ", next=" + next.toLong() + ")"; } @Override public boolean equals(final Object obj) { if (obj instanceof ForkId) { ForkId other = (ForkId) obj; - long thisNext = BytesValues.extractLong(next); + long thisNext = next.toLong(); return other.getHash().equals(this.hash) && thisNext == other.getNext(); } return false; diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessage.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessage.java index 476e2c3c53c..e55ce22b681 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessage.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessage.java @@ -54,7 +54,7 @@ public static StatusMessage create( public static StatusMessage create( final int protocolVersion, final BigInteger networkId, - final UInt256 totalDifficulty, + final Difficulty totalDifficulty, final Hash bestHash, final Hash genesisHash, final ForkIdManager.ForkId forkId) { @@ -148,7 +148,7 @@ private static class EthStatus { EthStatus( final int protocolVersion, final BigInteger networkId, - final UInt256 totalDifficulty, + final Difficulty totalDifficulty, final Hash bestHash, final Hash genesisHash, final ForkIdManager.ForkId forkHash) { @@ -166,11 +166,9 @@ public void writeTo(final RLPOutput out) { out.writeIntScalar(protocolVersion); out.writeBigIntegerScalar(networkId); out.writeUInt256Scalar(totalDifficulty); - out.writeBytesValue(bestHash); - out.writeBytesValue(genesisHash); - if (forkId != null) { - forkId.writeTo(out); - } + out.writeBytes(bestHash); + out.writeBytes(genesisHash); + out.endList(); } diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java index 6685044d927..2520ccbf7e8 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java @@ -16,10 +16,10 @@ import static org.assertj.core.api.Assertions.assertThat; +import org.apache.tuweni.bytes.Bytes; import org.hyperledger.besu.ethereum.core.Hash; import org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput; import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput; -import org.hyperledger.besu.util.bytes.BytesValue; import java.util.Arrays; import java.util.Collections; @@ -263,7 +263,7 @@ public void createAndDecodeRLP() { ForkIdManager.ForkId forkIdEntry = ForkIdManager.createIdEntry("0xa00bc324", 7280000L); BytesValueRLPOutput out = new BytesValueRLPOutput(); forkIdEntry.writeTo(out); - BytesValue bytesValue = out.encoded(); + Bytes bytesValue = out.encoded(); BytesValueRLPInput in = new BytesValueRLPInput(bytesValue, false); ForkIdManager.ForkId decodedEntry = ForkIdManager.readFrom(in); assertThat(forkIdEntry.equals(decodedEntry)).isTrue(); @@ -275,7 +275,7 @@ public void check1ZeroZeroProperRLPEncoding() { BytesValueRLPOutput out = new BytesValueRLPOutput(); forkIdEntry.writeTo(out); String str1 = "0xc6840000000080"; - BytesValue bytesValue = out.encoded(); + Bytes bytesValue = out.encoded(); assertThat(str1.equals(bytesValue.toString())).isTrue(); BytesValueRLPInput in = new BytesValueRLPInput(bytesValue, false); ForkIdManager.ForkId decodedEntry = ForkIdManager.readFrom(in); @@ -288,7 +288,7 @@ public void check2ArbitraryProperRLPEncoding() { BytesValueRLPOutput out = new BytesValueRLPOutput(); forkIdEntry.writeTo(out); String str1 = "0xca84deadbeef84baddcafe"; - BytesValue bytesValue = out.encoded(); + Bytes bytesValue = out.encoded(); assertThat(str1.equals(bytesValue.toString())).isTrue(); BytesValueRLPInput in = new BytesValueRLPInput(bytesValue, false); ForkIdManager.ForkId decodedEntry = ForkIdManager.readFrom(in); @@ -303,8 +303,7 @@ public void check3MaximumsProperRLPEncoding() { forkIdEntry.writeTo(out); String str1 = "0xce84ffffffff88ffffffffffffffff"; // Check value supplied in EIP-2124 spec via GO lang - BytesValue bytesValue = out.encoded(); - System.out.println(bytesValue); // todo remove dev item + Bytes bytesValue = out.encoded(); assertThat(str1.equals(bytesValue.toString())).isTrue(); BytesValueRLPInput in = new BytesValueRLPInput(bytesValue, false); ForkIdManager.ForkId decodedEntry = ForkIdManager.readFrom(in); diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessageTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessageTest.java index 715d7158e10..ce6ad7d4da3 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessageTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessageTest.java @@ -71,7 +71,7 @@ public void serializeDeserialize() { public void serializeDeserializeWithForkId() { final int version = EthProtocol.EthVersion.V64; final BigInteger networkId = BigInteger.ONE; - final UInt256 td = UInt256.of(1000L); + final Difficulty td = Difficulty.of(1000L); final Hash bestHash = randHash(1L); final Hash genesisHash = randHash(2L); final ForkIdManager.ForkId forkId = ForkIdManager.createIdEntry("0xa00bc334", 0L); From fcd87db89af5f9852f908a7568e5efdc64fe39ff Mon Sep 17 00:00:00 2001 From: SteveM Date: Mon, 30 Dec 2019 20:51:51 -0800 Subject: [PATCH 37/42] ran spotlessJava Signed-off-by: SteveM --- .../besu/ethereum/eth/manager/ForkIdManager.java | 7 +++---- .../besu/ethereum/eth/manager/ForkIdManagerTest.java | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java index f19f5823cf6..04fd47a85e7 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java @@ -21,8 +21,6 @@ import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput; import org.hyperledger.besu.ethereum.rlp.RLPInput; import org.hyperledger.besu.ethereum.rlp.RLPOutput; -import org.apache.tuweni.bytes.Bytes; -import org.apache.tuweni.bytes.Bytes32; import java.util.ArrayList; import java.util.List; @@ -30,6 +28,8 @@ import java.util.zip.CRC32; import com.google.common.annotations.VisibleForTesting; +import org.apache.tuweni.bytes.Bytes; +import org.apache.tuweni.bytes.Bytes32; public class ForkIdManager { @@ -210,8 +210,7 @@ public static class ForkId { } ForkId(final String hash, final String next) { - this.hash = - padToEightBytes(Bytes.fromHexString((hash.length() % 2 == 0 ? "" : "0") + hash)); + this.hash = padToEightBytes(Bytes.fromHexString((hash.length() % 2 == 0 ? "" : "0") + hash)); if (next.equals("") || next.equals("0x")) { this.next = Bytes.EMPTY; } else if (next.startsWith("0x")) { diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java index 2520ccbf7e8..a90e95a2285 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java @@ -16,7 +16,6 @@ import static org.assertj.core.api.Assertions.assertThat; -import org.apache.tuweni.bytes.Bytes; import org.hyperledger.besu.ethereum.core.Hash; import org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput; import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput; @@ -25,6 +24,7 @@ import java.util.Collections; import java.util.List; +import org.apache.tuweni.bytes.Bytes; import org.junit.Test; public class ForkIdManagerTest { From 4463ca4b2ff4b4ad1609d892b98e08c696eb5e9b Mon Sep 17 00:00:00 2001 From: SteveM Date: Mon, 30 Dec 2019 21:57:36 -0800 Subject: [PATCH 38/42] add missing item to StatusMessage.java to fix failing test Signed-off-by: SteveM --- .../hyperledger/besu/ethereum/eth/messages/StatusMessage.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessage.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessage.java index e55ce22b681..80050ecd921 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessage.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/messages/StatusMessage.java @@ -168,7 +168,9 @@ public void writeTo(final RLPOutput out) { out.writeUInt256Scalar(totalDifficulty); out.writeBytes(bestHash); out.writeBytes(genesisHash); - + if (forkId != null) { + forkId.writeTo(out); + } out.endList(); } From 992a156d398dd9e7fc74ab2398d2d5150793140e Mon Sep 17 00:00:00 2001 From: SteveM Date: Wed, 8 Jan 2020 12:16:36 -0800 Subject: [PATCH 39/42] fix ci failure from forkId Signed-off-by: SteveM --- .../ethereum/eth/manager/ForkIdManager.java | 23 ++++++++++--------- .../eth/manager/ForkIdManagerTest.java | 8 +++---- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java index 04fd47a85e7..a2c882dc8a9 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java @@ -33,22 +33,23 @@ public class ForkIdManager { - private Hash genesisHash; - private Long currentHead; + private final Hash genesisHash; + private final Long currentHead; private Long forkNext; - private Long highestKnownFork = 0L; - private ForkId lastKnownEntry = null; - private List forkAndHashList; + private final Long highestKnownFork = 0L; + private final List forkAndHashList; public ForkIdManager(final Hash genesisHash, final List forks, final Long currentHead) { this.genesisHash = genesisHash; this.currentHead = currentHead; this.forkAndHashList = - createForkIds( - // If there are two forks at the same block height, we only want to add it once to the - // crc checksum - forks.stream().distinct().collect(Collectors.toUnmodifiableList())); - }; + createForkIds( + // If there are two forks at the same block height, we only want to add it once to the + // crc checksum + forks.stream().distinct().collect(Collectors.toUnmodifiableList())); + } + + ; static ForkIdManager buildCollection( final Hash genesisHash, final List forks, final Blockchain blockchain) { @@ -79,7 +80,7 @@ public List getForkAndHashList() { } ForkId getLatestForkId() { - return lastKnownEntry; + return forkAndHashList.get(forkAndHashList.size() - 1); } public static ForkId readFrom(final RLPInput in) { diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java index a90e95a2285..b038bc155cf 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java @@ -28,14 +28,14 @@ import org.junit.Test; public class ForkIdManagerTest { - private Long[] forksMainnet = {1150000L, 1920000L, 2463000L, 2675000L, 4370000L, 7280000L}; - private String mainnetGenHash = - "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"; + private final Long[] forksMainnet = {1150000L, 1920000L, 2463000L, 2675000L, 4370000L, 7280000L}; + private final String mainnetGenHash = + "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"; @Test public void checkItFunctionsWithPresentBehavior() { ForkIdManager forkIdManager = - new ForkIdManager(Hash.fromHexString(mainnetGenHash), Collections.emptyList(), null); + new ForkIdManager(Hash.fromHexString(mainnetGenHash), Collections.emptyList(), null); assertThat(forkIdManager.peerCheck(Hash.fromHexString(mainnetGenHash))).isFalse(); } From 4268b8e55ee893a01d3754209e4df3df8117f1ff Mon Sep 17 00:00:00 2001 From: SteveM Date: Wed, 8 Jan 2020 12:56:27 -0800 Subject: [PATCH 40/42] re-fix ci failure from forkId Signed-off-by: SteveM --- .../besu/ethereum/eth/manager/ForkIdManager.java | 12 +++++------- .../besu/ethereum/eth/manager/ForkIdManagerTest.java | 4 ++-- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java index a2c882dc8a9..a1c983b0405 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java @@ -43,13 +43,11 @@ public ForkIdManager(final Hash genesisHash, final List forks, final Long this.genesisHash = genesisHash; this.currentHead = currentHead; this.forkAndHashList = - createForkIds( - // If there are two forks at the same block height, we only want to add it once to the - // crc checksum - forks.stream().distinct().collect(Collectors.toUnmodifiableList())); - } - - ; + createForkIds( + // If there are two forks at the same block height, we only want to add it once to the + // crc checksum + forks.stream().distinct().collect(Collectors.toUnmodifiableList())); + }; static ForkIdManager buildCollection( final Hash genesisHash, final List forks, final Blockchain blockchain) { diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java index b038bc155cf..eb9ffa0d6d0 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java @@ -30,12 +30,12 @@ public class ForkIdManagerTest { private final Long[] forksMainnet = {1150000L, 1920000L, 2463000L, 2675000L, 4370000L, 7280000L}; private final String mainnetGenHash = - "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"; + "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"; @Test public void checkItFunctionsWithPresentBehavior() { ForkIdManager forkIdManager = - new ForkIdManager(Hash.fromHexString(mainnetGenHash), Collections.emptyList(), null); + new ForkIdManager(Hash.fromHexString(mainnetGenHash), Collections.emptyList(), null); assertThat(forkIdManager.peerCheck(Hash.fromHexString(mainnetGenHash))).isFalse(); } From c93769cc4031bc02e2a5a97f51274572044700db Mon Sep 17 00:00:00 2001 From: SteveM Date: Wed, 8 Jan 2020 13:41:22 -0800 Subject: [PATCH 41/42] fix getLatestForkId out of bounds error Signed-off-by: SteveM --- .../besu/ethereum/eth/manager/ForkIdManager.java | 5 ++++- .../besu/ethereum/eth/manager/ForkIdManagerTest.java | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java index a1c983b0405..9fead26c0dc 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java @@ -78,7 +78,10 @@ public List getForkAndHashList() { } ForkId getLatestForkId() { - return forkAndHashList.get(forkAndHashList.size() - 1); + if (forkAndHashList.size() > 0) { + return forkAndHashList.get(forkAndHashList.size() - 1); + } + return null; } public static ForkId readFrom(final RLPInput in) { diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java index eb9ffa0d6d0..13ad8e1c7c9 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManagerTest.java @@ -37,6 +37,7 @@ public void checkItFunctionsWithPresentBehavior() { ForkIdManager forkIdManager = new ForkIdManager(Hash.fromHexString(mainnetGenHash), Collections.emptyList(), null); assertThat(forkIdManager.peerCheck(Hash.fromHexString(mainnetGenHash))).isFalse(); + assertThat(forkIdManager.getLatestForkId()).isNull(); } @Test @@ -55,6 +56,8 @@ public void checkCorrectMainnetForkIdHashesGenerated() { ForkIdManager.buildCollection(Hash.fromHexString(mainnetGenHash), list); List entries = forkIdManager.getForkAndHashList(); assertThat(entries).containsExactly(checkIds); + assertThat(forkIdManager.getLatestForkId()).isNotNull(); + assertThat(forkIdManager.getLatestForkId().equals(checkIds[6])).isTrue(); } @Test @@ -74,6 +77,8 @@ public void checkCorrectRopstenForkIdHashesGenerated() { List entries = forkIdManager.getForkAndHashList(); assertThat(entries).containsExactly(checkIds); + assertThat(forkIdManager.getLatestForkId()).isNotNull(); + assertThat(forkIdManager.getLatestForkId().equals(checkIds[4])).isTrue(); } @Test @@ -95,6 +100,8 @@ public void checkCorrectRinkebyForkIdHashesGenerated() { List entries = forkIdManager.getForkAndHashList(); assertThat(entries).containsExactly(checkIds); + assertThat(forkIdManager.getLatestForkId()).isNotNull(); + assertThat(forkIdManager.getLatestForkId().equals(checkIds[6])).isTrue(); } @Test @@ -106,6 +113,7 @@ public void check1PetersburgWithRemoteAnnouncingTheSame() { new ForkIdManager(Hash.fromHexString(mainnetGenHash), forkList, 7987396L); Boolean result = forkIdManager.peerCheck(ForkIdManager.createIdEntry("0x668db0af", 0L)); assertThat(result).isTrue(); + assertThat(forkIdManager.getLatestForkId()).isNotNull(); } @Test From 154742740df11730778ca195672455c241bae15f Mon Sep 17 00:00:00 2001 From: SteveM Date: Wed, 8 Jan 2020 16:16:21 -0800 Subject: [PATCH 42/42] change method used to get byte array Signed-off-by: SteveM --- .../hyperledger/besu/ethereum/eth/manager/ForkIdManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java index 9fead26c0dc..b1b7b73ff3b 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java @@ -169,7 +169,7 @@ private boolean isRemoteAwareOfPresent(final Bytes forkHash, final Long nextFork private List createForkIds(final List forks) { final CRC32 crc = new CRC32(); - crc.update(this.genesisHash.getByteArray()); + crc.update(this.genesisHash.toArray()); final List forkHashes = new ArrayList<>(List.of(getCurrentCrcHash(crc))); for (final Long fork : forks) { updateCrc(crc, fork);