Skip to content

Commit

Permalink
Merge with zkbesu
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio Di Fabio <[email protected]>
  • Loading branch information
fab-10 committed Jul 31, 2024
1 parent 5622666 commit a127045
Show file tree
Hide file tree
Showing 57 changed files with 1,157 additions and 1,285 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
- Allow configuration of Withdrawal Request Contract Address via genesis configuration [#7356](https://github.com/hyperledger/besu/pull/7356)

### Breaking Changes
- Remove long-deprecated `perm*whitelist*` methods [#7401](https://github.com/hyperledger/besu/pull/7401)

### Additions and Improvements
- Expose set finalized/safe block in plugin api BlockchainService. These method can be used by plugins to set finalized/safe block for a PoA network (such as QBFT, IBFT and Clique).[#7382](https://github.com/hyperledger/besu/pull/7382)
- In process RPC service [#7395](https://github.com/hyperledger/besu/pull/7395)

### Bug fixes

Expand Down Expand Up @@ -42,7 +45,7 @@
- Force bonsai-limit-trie-logs-enabled=false when sync-mode=FULL instead of startup error [#7357](https://github.com/hyperledger/besu/pull/7357)
- `--Xbonsai-parallel-tx-processing-enabled` option enables executing transactions in parallel during block processing for Bonsai nodes
- Reduce default trie log pruning window size from 30,000 to 5,000 [#7365](https://github.com/hyperledger/besu/pull/7365)
- Add option `--poa-discovery-retry-bootnodes` for PoA networks to always use bootnodes during peer refresh, not just on first start [#7314](https://github.com/hyperledger/besu/pull/7314)
- Add option `--poa-discovery-retry-bootnodes` for PoA networks to always use bootnodes during peer refresh, not just on first start [#7314](https://github.com/hyperledger/besu/pull/7314)

### Bug fixes
- Fix `eth_call` deserialization to correctly ignore unknown fields in the transaction object. [#7323](https://github.com/hyperledger/besu/pull/7323)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ private BesuPluginContextImpl buildPluginContext(
final TransactionPoolValidatorServiceImpl transactionPoolValidatorServiceImpl,
final BlockchainServiceImpl blockchainServiceImpl,
final RpcEndpointServiceImpl rpcEndpointServiceImpl,
final BesuEventsImpl besuEventsImpl,
final BesuConfiguration commonPluginConfiguration,
final PermissioningServiceImpl permissioningService) {
final CommandLine commandLine = new CommandLine(CommandSpec.create());
Expand All @@ -123,7 +122,6 @@ private BesuPluginContextImpl buildPluginContext(
TransactionSimulationService.class, transactionSimulationServiceImpl);
besuPluginContext.addService(BlockchainService.class, blockchainServiceImpl);
besuPluginContext.addService(BesuConfiguration.class, commonPluginConfiguration);
besuPluginContext.addService(BesuEvents.class, besuEventsImpl);

final Path pluginsPath;
final String pluginDir = System.getProperty("besu.plugins.dir");
Expand Down Expand Up @@ -177,7 +175,6 @@ public void startNode(final BesuNode node) {
final Path dataDir = node.homeDirectory();
final BesuConfigurationImpl commonPluginConfiguration = new BesuConfigurationImpl();
final PermissioningServiceImpl permissioningService = new PermissioningServiceImpl();
final BesuEventsImpl besuEventsImpl = new BesuEventsImpl();

final var miningParameters =
ImmutableMiningParameters.builder()
Expand All @@ -201,7 +198,6 @@ public void startNode(final BesuNode node) {
transactionPoolValidatorServiceImpl,
blockchainServiceImpl,
rpcEndpointServiceImpl,
besuEventsImpl,
commonPluginConfiguration,
permissioningService));

Expand Down Expand Up @@ -313,12 +309,14 @@ public void startNode(final BesuNode node) {

runner.startExternalServices();

besuEventsImpl.init(
besuController.getProtocolContext().getBlockchain(),
besuController.getProtocolManager().getBlockBroadcaster(),
besuController.getTransactionPool(),
besuController.getSyncState(),
besuController.getProtocolContext().getBadBlockManager());
besuPluginContext.addService(
BesuEvents.class,
new BesuEventsImpl(
besuController.getProtocolContext().getBlockchain(),
besuController.getProtocolManager().getBlockBroadcaster(),
besuController.getTransactionPool(),
besuController.getSyncState(),
besuController.getProtocolContext().getBadBlockManager()));

rpcEndpointServiceImpl.init(runner.getInProcessRpcMethods());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
import static org.hyperledger.besu.ethereum.api.jsonrpc.RpcApis.ADMIN;
import static org.hyperledger.besu.ethereum.api.jsonrpc.RpcApis.IBFT;

import org.hyperledger.besu.crypto.KeyPair;
import org.hyperledger.besu.datatypes.Wei;
Expand Down Expand Up @@ -43,6 +45,7 @@
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -331,12 +334,20 @@ public BesuNode createArchiveNodeWithRpcDisabled(final String name) throws IOExc
}

public BesuNode createPluginsNode(
final String name, final List<String> plugins, final List<String> extraCLIOptions)
final String name,
final List<String> plugins,
final List<String> extraCLIOptions,
final String... extraRpcApis)
throws IOException {

final List<String> enableRpcApis = new ArrayList<>(Arrays.asList(extraRpcApis));
enableRpcApis.addAll(List.of(IBFT.name(), ADMIN.name()));

return create(
new BesuNodeConfigurationBuilder()
.name(name)
.jsonRpcConfiguration(node.createJsonRpcWithIbft2AdminEnabledConfig())
.jsonRpcConfiguration(
node.createJsonRpcWithRpcApiEnabledConfig(enableRpcApis.toArray(String[]::new)))
.webSocketConfiguration(node.createWebSocketEnabledConfig())
.plugins(plugins)
.extraCLIOptions(extraCLIOptions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public PrivacyNode(
besuConfig.getEngineRpcConfiguration(),
besuConfig.getWebSocketConfiguration(),
besuConfig.getJsonRpcIpcConfiguration(),
config.getInProcessRpcConfiguration(),
besuConfig.getInProcessRpcConfiguration(),
besuConfig.getMetricsConfiguration(),
besuConfig.getPermissioningConfiguration(),
besuConfig.getApiConfiguration(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright contributors to Hyperledger Besu.
*
* 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.tests.acceptance.dsl.transaction.miner;

import static org.assertj.core.api.Assertions.assertThat;

import org.hyperledger.besu.tests.acceptance.dsl.transaction.NodeRequests;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.Transaction;

import java.io.IOException;
import java.math.BigInteger;

import org.web3j.protocol.core.methods.response.EthGasPrice;

public class MinerGetMinGasPriceTransaction implements Transaction<BigInteger> {

@Override
public BigInteger execute(final NodeRequests node) {
try {
final EthGasPrice result = node.miner().minerGetMinGasPrice().send();
assertThat(result).isNotNull();
assertThat(result.hasError()).isFalse();

return result.getGasPrice();

} catch (final IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.web3j.protocol.Web3jService;
import org.web3j.protocol.core.Request;
import org.web3j.protocol.core.methods.response.EthGasPrice;

public class MinerRequestFactory {

Expand All @@ -40,4 +41,8 @@ Request<?, org.web3j.protocol.core.methods.response.VoidResponse> minerStop() {
web3jService,
org.web3j.protocol.core.methods.response.VoidResponse.class);
}

Request<?, EthGasPrice> minerGetMinGasPrice() {
return new Request<>("miner_getMinGasPrice", null, web3jService, EthGasPrice.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ public MinerStartTransaction minerStart() {
public MinerStopTransaction minerStop() {
return new MinerStopTransaction();
}

public MinerGetMinGasPriceTransaction minerGetMinGasPrice() {
return new MinerGetMinGasPriceTransaction();
}
}
1 change: 1 addition & 0 deletions acceptance-tests/test-plugins/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencies {
implementation project(':datatypes')
implementation project(':ethereum:core')
implementation project(':ethereum:rlp')
implementation project(':ethereum:api')
implementation project(':plugin-api')
implementation 'com.google.auto.service:auto-service'
implementation 'info.picocli:picocli'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright contributors to Hyperledger Besu.
*
* 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.tests.acceptance.plugins;

import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonRpcParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
import org.hyperledger.besu.plugin.BesuContext;
import org.hyperledger.besu.plugin.BesuPlugin;
import org.hyperledger.besu.plugin.data.BlockContext;
import org.hyperledger.besu.plugin.services.BlockchainService;
import org.hyperledger.besu.plugin.services.RpcEndpointService;
import org.hyperledger.besu.plugin.services.exception.PluginRpcEndpointException;
import org.hyperledger.besu.plugin.services.rpc.PluginRpcRequest;

import java.util.Optional;

import com.google.auto.service.AutoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@AutoService(BesuPlugin.class)
public class TestBlockchainServiceFinalizedPlugin implements BesuPlugin {
private static final Logger LOG =
LoggerFactory.getLogger(TestBlockchainServiceFinalizedPlugin.class);
private static final String RPC_NAMESPACE = "updater";
private static final String RPC_METHOD_FINALIZED_BLOCK = "updateFinalizedBlockV1";
private static final String RPC_METHOD_SAFE_BLOCK = "updateSafeBlockV1";

@Override
public void register(final BesuContext besuContext) {
LOG.trace("Registering plugin ...");

final RpcEndpointService rpcEndpointService =
besuContext
.getService(RpcEndpointService.class)
.orElseThrow(
() ->
new RuntimeException(
"Failed to obtain RpcEndpointService from the BesuContext."));

final BlockchainService blockchainService =
besuContext
.getService(BlockchainService.class)
.orElseThrow(
() ->
new RuntimeException(
"Failed to obtain BlockchainService from the BesuContext."));

final FinalizationUpdaterRpcMethod rpcMethod =
new FinalizationUpdaterRpcMethod(blockchainService);
rpcEndpointService.registerRPCEndpoint(
RPC_NAMESPACE, RPC_METHOD_FINALIZED_BLOCK, rpcMethod::setFinalizedBlock);
rpcEndpointService.registerRPCEndpoint(
RPC_NAMESPACE, RPC_METHOD_SAFE_BLOCK, rpcMethod::setSafeBlock);
}

@Override
public void start() {
LOG.trace("Starting plugin ...");
}

@Override
public void stop() {
LOG.trace("Stopping plugin ...");
}

static class FinalizationUpdaterRpcMethod {
private final BlockchainService blockchainService;
private final JsonRpcParameter parameterParser = new JsonRpcParameter();

FinalizationUpdaterRpcMethod(final BlockchainService blockchainService) {
this.blockchainService = blockchainService;
}

Boolean setFinalizedBlock(final PluginRpcRequest request) {
return setFinalizedOrSafeBlock(request, true);
}

Boolean setSafeBlock(final PluginRpcRequest request) {
return setFinalizedOrSafeBlock(request, false);
}

private Boolean setFinalizedOrSafeBlock(
final PluginRpcRequest request, final boolean isFinalized) {
final Long blockNumberToSet = parseResult(request);

// lookup finalized block by number in local chain
final Optional<BlockContext> finalizedBlock =
blockchainService.getBlockByNumber(blockNumberToSet);
if (finalizedBlock.isEmpty()) {
throw new PluginRpcEndpointException(
RpcErrorType.BLOCK_NOT_FOUND,
"Block not found in the local chain: " + blockNumberToSet);
}

try {
final Hash blockHash = finalizedBlock.get().getBlockHeader().getBlockHash();
if (isFinalized) {
blockchainService.setFinalizedBlock(blockHash);
} else {
blockchainService.setSafeBlock(blockHash);
}
} catch (final IllegalArgumentException e) {
throw new PluginRpcEndpointException(
RpcErrorType.BLOCK_NOT_FOUND,
"Block not found in the local chain: " + blockNumberToSet);
} catch (final UnsupportedOperationException e) {
throw new PluginRpcEndpointException(
RpcErrorType.METHOD_NOT_ENABLED,
"Method not enabled for PoS network: setFinalizedBlock");
} catch (final Exception e) {
throw new PluginRpcEndpointException(
RpcErrorType.INTERNAL_ERROR, "Error setting finalized block: " + blockNumberToSet);
}

return Boolean.TRUE;
}

private Long parseResult(final PluginRpcRequest request) {
Long blockNumber;
try {
final Object[] params = request.getParams();
blockNumber = parameterParser.required(params, 0, Long.class);
} catch (final Exception e) {
throw new PluginRpcEndpointException(RpcErrorType.INVALID_PARAMS, e.getMessage());
}

if (blockNumber <= 0) {
throw new PluginRpcEndpointException(
RpcErrorType.INVALID_PARAMS, "Block number must be greater than 0");
}

return blockNumber;
}
}
}
Loading

0 comments on commit a127045

Please sign in to comment.