Skip to content

Commit

Permalink
TransactionSimulator can be a singleton
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
  • Loading branch information
fab-10 committed Nov 27, 2024
1 parent 5b2da5a commit 07ff952
Show file tree
Hide file tree
Showing 35 changed files with 180 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,8 @@ public BesuController provideBesuController(
final BesuControllerBuilder builder,
final MetricsSystem metricsSystem,
final KeyValueStorageProvider storageProvider,
final MiningConfiguration miningConfiguration) {
final MiningConfiguration miningConfiguration,
final ApiConfiguration apiConfiguration) {

builder
.synchronizerConfiguration(synchronizerConfiguration)
Expand All @@ -474,6 +475,7 @@ public BesuController provideBesuController(
.maxRemotelyInitiatedPeers(15)
.miningParameters(miningConfiguration)
.randomPeerPriority(false)
.apiConfiguration(apiConfiguration)
.besuComponent(null);
return builder.build();
}
Expand Down
30 changes: 16 additions & 14 deletions besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -698,12 +698,7 @@ public Runner build() {

final Synchronizer synchronizer = besuController.getSynchronizer();

final TransactionSimulator transactionSimulator =
new TransactionSimulator(
context.getBlockchain(),
context.getWorldStateArchive(),
protocolSchedule,
apiConfiguration.getGasCap());
final TransactionSimulator transactionSimulator = besuController.getTransactionSimulator();

final Bytes localNodeId = nodeKey.getPublicKey().getEncodedBytes();
final Optional<NodePermissioningController> nodePermissioningController =
Expand Down Expand Up @@ -867,7 +862,8 @@ public Runner build() {
natService,
besuPluginContext.getNamedPlugins(),
dataDir,
rpcEndpointServiceImpl);
rpcEndpointServiceImpl,
transactionSimulator);

jsonRpcHttpService =
Optional.of(
Expand Down Expand Up @@ -912,7 +908,8 @@ public Runner build() {
natService,
besuPluginContext.getNamedPlugins(),
dataDir,
rpcEndpointServiceImpl);
rpcEndpointServiceImpl,
transactionSimulator);

final Optional<AuthenticationService> authToUse =
engineJsonRpcConfiguration.get().isAuthenticationEnabled()
Expand Down Expand Up @@ -959,7 +956,7 @@ public Runner build() {
graphQlContextMap.putIfAbsent(GraphQLContextType.SYNCHRONIZER, synchronizer);
graphQlContextMap.putIfAbsent(
GraphQLContextType.CHAIN_ID, protocolSchedule.getChainId().map(UInt256::valueOf));
graphQlContextMap.putIfAbsent(GraphQLContextType.GAS_CAP, apiConfiguration.getGasCap());
graphQlContextMap.putIfAbsent(GraphQLContextType.TRANSACTION_SIMULATOR, transactionSimulator);
final GraphQL graphQL;
try {
graphQL = GraphQLProvider.buildGraphQL(fetchers);
Expand Down Expand Up @@ -1007,7 +1004,8 @@ public Runner build() {
natService,
besuPluginContext.getNamedPlugins(),
dataDir,
rpcEndpointServiceImpl);
rpcEndpointServiceImpl,
transactionSimulator);

createLogsSubscriptionService(
context.getBlockchain(), subscriptionManager, privacyParameters, blockchainQueries);
Expand Down Expand Up @@ -1087,7 +1085,8 @@ public Runner build() {
natService,
besuPluginContext.getNamedPlugins(),
dataDir,
rpcEndpointServiceImpl);
rpcEndpointServiceImpl,
transactionSimulator);

jsonRpcIpcService =
Optional.of(
Expand Down Expand Up @@ -1126,7 +1125,8 @@ public Runner build() {
natService,
besuPluginContext.getNamedPlugins(),
dataDir,
rpcEndpointServiceImpl);
rpcEndpointServiceImpl,
transactionSimulator);
} else {
inProcessRpcMethods = Map.of();
}
Expand Down Expand Up @@ -1288,7 +1288,8 @@ private Map<String, JsonRpcMethod> jsonRpcMethods(
final NatService natService,
final Map<String, BesuPlugin> namedPlugins,
final Path dataDir,
final RpcEndpointServiceImpl rpcEndpointServiceImpl) {
final RpcEndpointServiceImpl rpcEndpointServiceImpl,
final TransactionSimulator transactionSimulator) {
// sync vertx for engine consensus API, to process requests in FIFO order;
final Vertx consensusEngineServer = Vertx.vertx(new VertxOptions().setWorkerPoolSize(1));

Expand Down Expand Up @@ -1325,7 +1326,8 @@ private Map<String, JsonRpcMethod> jsonRpcMethods(
besuController.getProtocolManager().ethContext().getEthPeers(),
consensusEngineServer,
apiConfiguration,
enodeDnsConfiguration);
enodeDnsConfiguration,
transactionSimulator);
methods.putAll(besuController.getAdditionalJsonRpcMethods(jsonRpcApis));

final var pluginMethods =
Expand Down
20 changes: 10 additions & 10 deletions besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@
import org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueSegmentIdentifier;
import org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueStorageProvider;
import org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueStorageProviderBuilder;
import org.hyperledger.besu.ethereum.transaction.TransactionSimulator;
import org.hyperledger.besu.ethereum.worldstate.DataStorageConfiguration;
import org.hyperledger.besu.ethereum.worldstate.DiffBasedSubStorageConfiguration;
import org.hyperledger.besu.ethereum.worldstate.ImmutableDataStorageConfiguration;
Expand Down Expand Up @@ -342,6 +341,8 @@ public class BesuCommand implements DefaultCommandValues, Runnable {
Suppliers.memoize(this::readGenesisConfigOptions);
private final Supplier<MiningConfiguration> miningParametersSupplier =
Suppliers.memoize(this::getMiningParameters);
private final Supplier<ApiConfiguration> apiConfigurationSupplier =
Suppliers.memoize(this::getApiConfiguration);

private RocksDBPlugin rocksDBPlugin;

Expand Down Expand Up @@ -717,7 +718,6 @@ static class PrivacyOptionGroup {
private WebSocketConfiguration webSocketConfiguration;
private JsonRpcIpcConfiguration jsonRpcIpcConfiguration;
private InProcessRpcConfiguration inProcessRpcConfiguration;
private ApiConfiguration apiConfiguration;
private MetricsConfiguration metricsConfiguration;
private Optional<PermissioningConfiguration> permissioningConfiguration;
private Optional<TLSConfiguration> p2pTLSConfiguration;
Expand Down Expand Up @@ -1244,7 +1244,7 @@ private Runner buildRunner() {
webSocketConfiguration,
jsonRpcIpcConfiguration,
inProcessRpcConfiguration,
apiConfiguration,
apiConfigurationSupplier.get(),
metricsConfiguration,
permissioningConfiguration,
staticNodes,
Expand All @@ -1256,11 +1256,7 @@ private void startPlugins(final Runner runner) {
besuController.getProtocolContext().getBlockchain(), besuController.getProtocolSchedule());
transactionSimulationServiceImpl.init(
besuController.getProtocolContext().getBlockchain(),
new TransactionSimulator(
besuController.getProtocolContext().getBlockchain(),
besuController.getProtocolContext().getWorldStateArchive(),
besuController.getProtocolSchedule(),
apiConfiguration.getGasCap()));
besuController.getTransactionSimulator());
rpcEndpointServiceImpl.init(runner.getInProcessRpcMethods());

besuPluginContext.addService(
Expand Down Expand Up @@ -1454,7 +1450,6 @@ private void validateOptions() {
validateTransactionPoolOptions();
validateDataStorageOptions();
validateGraphQlOptions();
validateApiOptions();
validateConsensusSyncCompatibilityOptions();
validatePluginOptions();
p2pTLSConfigOptions.checkP2PTLSOptionsDependencies(logger, commandLine);
Expand Down Expand Up @@ -1710,7 +1705,6 @@ private void configure() throws Exception {
unstableIpcOptions.getIpcPath(),
unstableIpcOptions.getRpcIpcApis());
inProcessRpcConfiguration = inProcessRpcOptions.toDomainObject();
apiConfiguration = apiConfigurationOptions.apiConfiguration();
dataStorageConfiguration = getDataStorageConfiguration();
// hostsWhitelist is a hidden option. If it is specified, add the list to hostAllowlist
if (!hostsWhitelist.isEmpty()) {
Expand Down Expand Up @@ -1828,6 +1822,7 @@ public BesuControllerBuilder setupControllerBuilder() {
.chainPruningConfiguration(unstableChainPruningOptions.toDomainObject())
.cacheLastBlocks(numberOfblocksToCache)
.genesisStateHashCacheEnabled(genesisStateHashCacheEnabled)
.apiConfiguration(apiConfigurationSupplier.get())
.besuComponent(besuComponent);
if (DataStorageFormat.BONSAI.equals(getDataStorageConfiguration().getDataStorageFormat())) {
final DiffBasedSubStorageConfiguration subStorageConfiguration =
Expand Down Expand Up @@ -2153,6 +2148,11 @@ private MiningConfiguration getMiningParameters() {
return miningParameters;
}

private ApiConfiguration getApiConfiguration() {
validateApiOptions();
return apiConfigurationOptions.apiConfiguration();
}

/**
* Get the data storage configuration
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.p2p.config.SubProtocolConfiguration;
import org.hyperledger.besu.ethereum.storage.StorageProvider;
import org.hyperledger.besu.ethereum.transaction.TransactionSimulator;
import org.hyperledger.besu.ethereum.worldstate.DataStorageConfiguration;

import java.io.Closeable;
Expand Down Expand Up @@ -66,7 +67,6 @@ public class BesuController implements java.io.Closeable {
private final NodeKey nodeKey;
private final Synchronizer synchronizer;
private final JsonRpcMethods additionalJsonRpcMethodsFactory;

private final TransactionPool transactionPool;
private final MiningCoordinator miningCoordinator;
private final PrivacyParameters privacyParameters;
Expand All @@ -77,6 +77,7 @@ public class BesuController implements java.io.Closeable {
private final EthPeers ethPeers;
private final StorageProvider storageProvider;
private final DataStorageConfiguration dataStorageConfiguration;
private final TransactionSimulator transactionSimulator;

/**
* Instantiates a new Besu controller.
Expand All @@ -99,6 +100,7 @@ public class BesuController implements java.io.Closeable {
* @param ethPeers the eth peers
* @param storageProvider the storage provider
* @param dataStorageConfiguration the data storage configuration
* @param transactionSimulator the transaction simulator
*/
BesuController(
final ProtocolSchedule protocolSchedule,
Expand All @@ -118,7 +120,8 @@ public class BesuController implements java.io.Closeable {
final PluginServiceFactory additionalPluginServices,
final EthPeers ethPeers,
final StorageProvider storageProvider,
final DataStorageConfiguration dataStorageConfiguration) {
final DataStorageConfiguration dataStorageConfiguration,
final TransactionSimulator transactionSimulator) {
this.protocolSchedule = protocolSchedule;
this.protocolContext = protocolContext;
this.ethProtocolManager = ethProtocolManager;
Expand All @@ -137,6 +140,7 @@ public class BesuController implements java.io.Closeable {
this.ethPeers = ethPeers;
this.storageProvider = storageProvider;
this.dataStorageConfiguration = dataStorageConfiguration;
this.transactionSimulator = transactionSimulator;
}

/**
Expand Down Expand Up @@ -307,6 +311,15 @@ public DataStorageConfiguration getDataStorageConfiguration() {
return dataStorageConfiguration;
}

/**
* Gets the transaction simulator
*
* @return the transaction simulator
*/
public TransactionSimulator getTransactionSimulator() {
return transactionSimulator;
}

/** The type Builder. */
public static class Builder {
/** Instantiates a new Builder. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.hyperledger.besu.ethereum.ConsensusContext;
import org.hyperledger.besu.ethereum.GasLimitCalculator;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.api.ApiConfiguration;
import org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethods;
import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator;
import org.hyperledger.besu.ethereum.chain.BadBlockManager;
Expand Down Expand Up @@ -84,6 +85,7 @@
import org.hyperledger.besu.ethereum.p2p.config.SubProtocolConfiguration;
import org.hyperledger.besu.ethereum.storage.StorageProvider;
import org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueSegmentIdentifier;
import org.hyperledger.besu.ethereum.transaction.TransactionSimulator;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.BonsaiWorldStateProvider;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.cache.BonsaiCachedMerkleTrieLoader;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.storage.BonsaiWorldStateKeyValueStorage;
Expand Down Expand Up @@ -211,6 +213,12 @@ public abstract class BesuControllerBuilder implements MiningParameterOverrides
/** whether parallel transaction processing is enabled or not */
protected boolean isParallelTxProcessingEnabled;

/** The API configuration */
protected ApiConfiguration apiConfiguration;

/** The transaction simulator */
protected TransactionSimulator transactionSimulator;

/** Instantiates a new Besu controller builder. */
protected BesuControllerBuilder() {}

Expand Down Expand Up @@ -272,6 +280,17 @@ public BesuControllerBuilder synchronizerConfiguration(
return this;
}

/**
* API configuration besu controller builder.
*
* @param apiConfiguration the API config
* @return the besu controller builder
*/
public BesuControllerBuilder apiConfiguration(final ApiConfiguration apiConfiguration) {
this.apiConfiguration = apiConfiguration;
return this;
}

/**
* Eth protocol configuration besu controller builder.
*
Expand Down Expand Up @@ -556,6 +575,7 @@ public BesuController build() {
checkNotNull(gasLimitCalculator, "Missing gas limit calculator");
checkNotNull(evmConfiguration, "Missing evm config");
checkNotNull(networkingConfiguration, "Missing network configuration");
checkNotNull(apiConfiguration, "Missing API configuration");
checkNotNull(dataStorageConfiguration, "Missing data storage configuration");
checkNotNull(besuComponent, "Must supply a BesuComponent");
prepForBuild();
Expand Down Expand Up @@ -604,6 +624,10 @@ public BesuController build() {
genesisState.writeStateTo(worldStateArchive.getMutable());
}

transactionSimulator =
new TransactionSimulator(
blockchain, worldStateArchive, protocolSchedule, apiConfiguration.getGasCap());

final var consensusContext =
createConsensusContext(blockchain, worldStateArchive, protocolSchedule);

Expand Down Expand Up @@ -795,7 +819,8 @@ public BesuController build() {
additionalPluginServices,
ethPeers,
storageProvider,
dataStorageConfiguration);
dataStorageConfiguration,
transactionSimulator);
}

private GenesisState getGenesisState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.p2p.config.SubProtocolConfiguration;
import org.hyperledger.besu.ethereum.transaction.TransactionSimulator;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;
import org.hyperledger.besu.plugin.services.BesuEvents;
import org.hyperledger.besu.util.Subscribers;
Expand Down Expand Up @@ -399,8 +398,6 @@ protected BftContext createConsensusContext(
BlockValidatorProvider.forkingValidatorProvider(
blockchain, epochManager, bftBlockInterface().get(), validatorOverrides);

final TransactionSimulator transactionSimulator =
new TransactionSimulator(blockchain, worldStateArchive, protocolSchedule, 0L);
transactionValidatorProvider =
new TransactionValidatorProvider(
blockchain, new ValidatorContractController(transactionSimulator), qbftForksSchedule);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.enclave.EnclaveFactory;
import org.hyperledger.besu.ethereum.GasLimitCalculator;
import org.hyperledger.besu.ethereum.api.ImmutableApiConfiguration;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture;
import org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider;
Expand Down Expand Up @@ -162,6 +163,7 @@ BesuController provideBesuController(
.evmConfiguration(EvmConfiguration.DEFAULT)
.networkConfiguration(NetworkingConfiguration.create())
.besuComponent(context)
.apiConfiguration(ImmutableApiConfiguration.builder().build())
.build();
}
}
Expand Down
2 changes: 2 additions & 0 deletions besu/src/test/java/org/hyperledger/besu/PrivacyReorgTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.hyperledger.besu.enclave.types.ReceiveResponse;
import org.hyperledger.besu.ethereum.GasLimitCalculator;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.api.ImmutableApiConfiguration;
import org.hyperledger.besu.ethereum.chain.DefaultBlockchain;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockDataGenerator;
Expand Down Expand Up @@ -558,6 +559,7 @@ BesuController provideBesuController(
.evmConfiguration(EvmConfiguration.DEFAULT)
.networkConfiguration(NetworkingConfiguration.create())
.besuComponent(context)
.apiConfiguration(ImmutableApiConfiguration.builder().build())
.build();
return retval;
}
Expand Down
Loading

0 comments on commit 07ff952

Please sign in to comment.