Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eth_simulateV1 - Add BlockSimulator feature #7941

Merged
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
4be39be
initial implementation of block simulator
Gabriel-Trintinalia Nov 22, 2024
668176e
Implement eth_simulateV1
Gabriel-Trintinalia Nov 23, 2024
924efbc
Implement simulate v1
Gabriel-Trintinalia Nov 23, 2024
36393c4
Remove blockchain
Gabriel-Trintinalia Nov 23, 2024
0de4e65
Add initial test for simulate v1
Gabriel-Trintinalia Nov 24, 2024
5778e0a
Simplify code
Gabriel-Trintinalia Nov 25, 2024
1964c3a
remove non necessary code
Gabriel-Trintinalia Nov 25, 2024
652f4a5
remove non necessary code
Gabriel-Trintinalia Nov 25, 2024
ac53d95
remove non necessary code
Gabriel-Trintinalia Nov 25, 2024
38bb8de
merge main
Gabriel-Trintinalia Nov 25, 2024
71dc684
Simplify code
Gabriel-Trintinalia Nov 25, 2024
572244a
Simplify code
Gabriel-Trintinalia Nov 25, 2024
91d9645
Simplify code
Gabriel-Trintinalia Nov 25, 2024
2615eb5
Simplify code
Gabriel-Trintinalia Nov 26, 2024
612ffc9
Refactor code
Gabriel-Trintinalia Nov 26, 2024
a826054
Add transaction processing result to output
Gabriel-Trintinalia Nov 26, 2024
aa0d2d9
Simplify code
Gabriel-Trintinalia Nov 26, 2024
c6ac81b
Simplify code
Gabriel-Trintinalia Nov 26, 2024
1026328
Expose rest of body
Gabriel-Trintinalia Nov 26, 2024
6382608
Simplify code
Gabriel-Trintinalia Nov 26, 2024
1d113b8
Merge branch 'main' into block-simulator-service
Gabriel-Trintinalia Nov 26, 2024
46cf544
Merge main into branch
Gabriel-Trintinalia Nov 26, 2024
9661fed
Remove PersistingBlockSimulator
Gabriel-Trintinalia Nov 27, 2024
dc9f802
Bug fixes
Gabriel-Trintinalia Dec 2, 2024
1df5f32
merge main into branch
Gabriel-Trintinalia Dec 3, 2024
7e482e1
Split PR into block simulator and eth_simulate v1
Gabriel-Trintinalia Dec 9, 2024
4bad506
Merge main
Gabriel-Trintinalia Dec 9, 2024
3ee61a4
Refactoring
Gabriel-Trintinalia Dec 9, 2024
9f0e610
Refactoring
Gabriel-Trintinalia Dec 9, 2024
a3c3a20
Add tests
Gabriel-Trintinalia Dec 10, 2024
40db421
Add tests
Gabriel-Trintinalia Dec 10, 2024
e1a1967
Remove unused files
Gabriel-Trintinalia Dec 10, 2024
8b97440
Fix javadoc
Gabriel-Trintinalia Dec 10, 2024
d026c0c
Update besu/src/main/java/org/hyperledger/besu/services/BlockSimulato…
Gabriel-Trintinalia Dec 13, 2024
7ab57c6
Update ethereum/core/src/main/java/org/hyperledger/besu/ethereum/tran…
Gabriel-Trintinalia Dec 13, 2024
a6b4597
Update ethereum/core/src/main/java/org/hyperledger/besu/ethereum/tran…
Gabriel-Trintinalia Dec 13, 2024
9de954d
Update ethereum/core/src/main/java/org/hyperledger/besu/ethereum/tran…
Gabriel-Trintinalia Dec 13, 2024
f516105
Fix missing rename
Gabriel-Trintinalia Dec 13, 2024
52a6d22
Fix wrong json properties
Gabriel-Trintinalia Dec 13, 2024
74a9fb0
Simplify simulator service impl
Gabriel-Trintinalia Dec 13, 2024
cc0bcca
Move BlockOverrides to plugin
Gabriel-Trintinalia Dec 13, 2024
6c6276f
Accept PR suggestions
Gabriel-Trintinalia Dec 17, 2024
394bec0
Merge branch 'main' into block-simulator-service
Gabriel-Trintinalia Dec 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
import org.hyperledger.besu.plugin.data.EnodeURL;
import org.hyperledger.besu.plugin.services.BesuConfiguration;
import org.hyperledger.besu.plugin.services.BesuEvents;
import org.hyperledger.besu.plugin.services.BlockSimulationService;
import org.hyperledger.besu.plugin.services.BlockchainService;
import org.hyperledger.besu.plugin.services.MetricsSystem;
import org.hyperledger.besu.plugin.services.PermissioningService;
Expand All @@ -178,6 +179,7 @@
import org.hyperledger.besu.services.BesuConfigurationImpl;
import org.hyperledger.besu.services.BesuEventsImpl;
import org.hyperledger.besu.services.BesuPluginContextImpl;
import org.hyperledger.besu.services.BlockSimulatorServiceImpl;
import org.hyperledger.besu.services.BlockchainServiceImpl;
import org.hyperledger.besu.services.MiningServiceImpl;
import org.hyperledger.besu.services.P2PServiceImpl;
Expand Down Expand Up @@ -1296,6 +1298,14 @@ private void startPlugins(final Runner runner) {
besuPluginContext.addService(
MiningService.class, new MiningServiceImpl(besuController.getMiningCoordinator()));

besuPluginContext.addService(
BlockSimulationService.class,
new BlockSimulatorServiceImpl(
besuController.getProtocolContext().getWorldStateArchive(),
miningParametersSupplier.get(),
besuController.getTransactionSimulator(),
besuController.getProtocolSchedule()));

besuController.getAdditionalPluginServices().appendPluginServices(besuPluginContext);
besuPluginContext.startPlugins();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright contributors to 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.services;

import org.hyperledger.besu.datatypes.AccountOverrideMap;
import org.hyperledger.besu.datatypes.BlockOverrides;
import org.hyperledger.besu.datatypes.Transaction;
import org.hyperledger.besu.ethereum.core.MiningConfiguration;
import org.hyperledger.besu.ethereum.core.MutableWorldState;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.transaction.BlockSimulator;
import org.hyperledger.besu.ethereum.transaction.BlockStateCall;
import org.hyperledger.besu.ethereum.transaction.CallParameter;
import org.hyperledger.besu.ethereum.transaction.TransactionSimulator;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;
import org.hyperledger.besu.plugin.Unstable;
import org.hyperledger.besu.plugin.data.BlockHeader;
import org.hyperledger.besu.plugin.data.BlockSimulationResult;
import org.hyperledger.besu.plugin.data.TransactionSimulationResult;
import org.hyperledger.besu.plugin.services.BlockSimulationService;

import java.util.List;

/** This class is a service that simulates the processing of a block */
public class BlockSimulatorServiceImpl implements BlockSimulationService {
private final BlockSimulator blockSimulator;
private final WorldStateArchive worldStateArchive;

/**
* This constructor creates a BlockSimulatorServiceImpl object
*
* @param worldStateArchive the world state archive
* @param miningConfiguration the mining configuration
* @param transactionSimulator the transaction simulator
* @param protocolSchedule the protocol schedule
*/
public BlockSimulatorServiceImpl(
final WorldStateArchive worldStateArchive,
final MiningConfiguration miningConfiguration,
final TransactionSimulator transactionSimulator,
final ProtocolSchedule protocolSchedule) {
blockSimulator =
new BlockSimulator(
worldStateArchive, protocolSchedule, transactionSimulator, miningConfiguration);
this.worldStateArchive = worldStateArchive;
}

/**
* Simulate the processing of a block given a header, a list of transactions, and blockOverrides.
*
* @param header the header
* @param transactions the transactions to include in the block
* @param blockOverrides the blockSimulationOverride of the block
* @return the block context
*/
@Override
public BlockSimulationResult simulate(
final BlockHeader header,
final List<? extends Transaction> transactions,
final BlockOverrides blockOverrides) {
BlockStateCall blockStateCall = createBlockStateCall(transactions, blockOverrides);
var headerCore = (org.hyperledger.besu.ethereum.core.BlockHeader) header;
Gabriel-Trintinalia marked this conversation as resolved.
Show resolved Hide resolved

var result = blockSimulator.process(headerCore, List.of(blockStateCall));
return response(result.getFirst());
}

/**
* This method is experimental and should be used with caution
Gabriel-Trintinalia marked this conversation as resolved.
Show resolved Hide resolved
*
* @param header the block header
* @param transactions the transactions to include in the block
* @param blockOverrides the blockSimulationOverride of the block
* @return the block context
*/
@Unstable
@Override
public BlockSimulationResult importBlockUnsafe(
final BlockHeader header,
final List<? extends Transaction> transactions,
final BlockOverrides blockOverrides) {

BlockStateCall blockStateCall = createBlockStateCall(transactions, blockOverrides);
var headerCore = (org.hyperledger.besu.ethereum.core.BlockHeader) header;
try (final MutableWorldState ws =
worldStateArchive
.getMutable(headerCore, true)
.orElseThrow(
() ->
new IllegalArgumentException(
"Public world state not available for block "
Gabriel-Trintinalia marked this conversation as resolved.
Show resolved Hide resolved
+ headerCore.toLogString()))) {
var results = blockSimulator.process(headerCore, List.of(blockStateCall), ws);
var result = results.getFirst();
ws.persist(result.getBlock().getHeader());
return response(result);
} catch (final Exception e) {
throw new RuntimeException("Error simulating block", e);
}
}

private BlockStateCall createBlockStateCall(
final List<? extends Transaction> transactions, final BlockOverrides blockOverrides) {
var callParameters = transactions.stream().map(CallParameter::fromTransaction).toList();
return new BlockStateCall(callParameters, blockOverrides, new AccountOverrideMap(), true);
}

private BlockSimulationResult response(
final org.hyperledger.besu.ethereum.transaction.BlockSimulationResult result) {
return new BlockSimulationResult(
result.getBlockHeader(),
result.getBlockBody(),
result.getReceipts(),
result.getTransactionSimulations().stream()
.map(
simulation ->
new TransactionSimulationResult(simulation.transaction(), simulation.result()))
.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public Optional<List<TransactionReceipt>> getReceiptsByBlockHash(final Hash bloc
public void storeBlock(
final BlockHeader blockHeader,
final BlockBody blockBody,
final List<TransactionReceipt> receipts) {
final List<? extends TransactionReceipt> receipts) {
final org.hyperledger.besu.ethereum.core.BlockHeader coreHeader =
(org.hyperledger.besu.ethereum.core.BlockHeader) blockHeader;
final org.hyperledger.besu.ethereum.core.BlockBody coreBody =
Expand Down
Loading
Loading