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 <[email protected]>
  • Loading branch information
fab-10 committed Nov 26, 2024
1 parent 0e908d2 commit 2ef5f7f
Show file tree
Hide file tree
Showing 23 changed files with 145 additions and 112 deletions.
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 @@ -1088,7 +1086,8 @@ public Runner build() {
natService,
besuPluginContext.getNamedPlugins(),
dataDir,
rpcEndpointServiceImpl);
rpcEndpointServiceImpl,
transactionSimulator);

jsonRpcIpcService =
Optional.of(
Expand Down Expand Up @@ -1127,7 +1126,8 @@ public Runner build() {
natService,
besuPluginContext.getNamedPlugins(),
dataDir,
rpcEndpointServiceImpl);
rpcEndpointServiceImpl,
transactionSimulator);
} else {
inProcessRpcMethods = Map.of();
}
Expand Down Expand Up @@ -1289,7 +1289,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 @@ -1326,7 +1327,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 @@ -1453,7 +1449,6 @@ private void validateOptions() {
validateTransactionPoolOptions();
validateDataStorageOptions();
validateGraphQlOptions();
validateApiOptions();
validateConsensusSyncCompatibilityOptions();
validatePluginOptions();
p2pTLSConfigOptions.checkP2PTLSOptionsDependencies(logger, commandLine);
Expand Down Expand Up @@ -1709,7 +1704,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 @@ -1827,6 +1821,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 @@ -2152,6 +2147,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 @@ -796,7 +820,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 @@ -45,6 +45,7 @@
import org.hyperledger.besu.ethereum.p2p.network.P2PNetwork;
import org.hyperledger.besu.ethereum.permissioning.AccountLocalConfigPermissioningController;
import org.hyperledger.besu.ethereum.permissioning.NodeLocalConfigPermissioningController;
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.noop.NoOpMetricsSystem;
Expand Down Expand Up @@ -77,6 +78,7 @@ public class JsonRpcTestMethodsFactory {
private final BlockchainQueries blockchainQueries;
private final Synchronizer synchronizer;
private final ProtocolSchedule protocolSchedule;
private final TransactionSimulator transactionSimulator;

public JsonRpcTestMethodsFactory(final BlockchainImporter importer) {
this.importer = importer;
Expand All @@ -98,6 +100,9 @@ public JsonRpcTestMethodsFactory(final BlockchainImporter importer) {
this.blockchainQueries =
new BlockchainQueries(
protocolSchedule, blockchain, stateArchive, MiningConfiguration.newDefault());

this.transactionSimulator =
new TransactionSimulator(blockchain, stateArchive, protocolSchedule, 0L);
}

public JsonRpcTestMethodsFactory(
Expand All @@ -117,6 +122,8 @@ public JsonRpcTestMethodsFactory(
stateArchive,
MiningConfiguration.newDefault());
this.synchronizer = mock(Synchronizer.class);
this.transactionSimulator =
new TransactionSimulator(blockchain, stateArchive, protocolSchedule, 0L);
}

public JsonRpcTestMethodsFactory(
Expand All @@ -137,6 +144,8 @@ public JsonRpcTestMethodsFactory(
blockchain,
stateArchive,
MiningConfiguration.newDefault());
this.transactionSimulator =
new TransactionSimulator(blockchain, stateArchive, protocolSchedule, 0L);
}

public BlockchainQueries getBlockchainQueries() {
Expand Down Expand Up @@ -219,6 +228,7 @@ public Map<String, JsonRpcMethod> methods() {
ethPeers,
Vertx.vertx(new VertxOptions().setWorkerPoolSize(1)),
ImmutableApiConfiguration.builder().build(),
Optional.empty());
Optional.empty(),
transactionSimulator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ public enum GraphQLContextType {
/** Represents chain ID context. */
CHAIN_ID,

/** Represents gas cap context. */
GAS_CAP
/** Represents the transaction simulator. */
TRANSACTION_SIMULATOR
}
Loading

0 comments on commit 2ef5f7f

Please sign in to comment.