Skip to content

Commit

Permalink
Remove PersistingBlockSimulator
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel-Trintinalia <[email protected]>
  • Loading branch information
Gabriel-Trintinalia committed Nov 27, 2024
1 parent 46cf544 commit 9661fed
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
import org.hyperledger.besu.datatypes.Transaction;
import org.hyperledger.besu.ethereum.chain.MutableBlockchain;
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.PersistingBlockSimulator;
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;
Expand All @@ -48,6 +49,14 @@ public BlockSimulatorServiceImpl(
this.worldStateArchive = worldStateArchive;
}

/**
* Simulate the creation of a block given 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,
Expand All @@ -60,18 +69,42 @@ public BlockSimulationResult simulate(
return response(result);
}

/**
* NOTE: This method is experimental and should be used with caution. It may result in database
* inconsistencies if not used properly. Use only in specific scenarios where its behavior is well
* understood.
*
* @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 simulateAndPersist(
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 = getWorldState(headerCore)) {
var result = blockSimulator.processWithMutableWorldState(headerCore, blockStateCall, ws);
ws.persist(result.getBlock().getHeader());
return response(result);
} catch (final Exception e) {
throw new RuntimeException("Error simulating block", e);
}
}

PersistingBlockSimulator persistingBlockSimulator =
new PersistingBlockSimulator(blockSimulator, worldStateArchive);
var result = persistingBlockSimulator.process(headerCore, blockStateCall);
return response(result);
private MutableWorldState getWorldState(
final org.hyperledger.besu.ethereum.core.BlockHeader header) {
return worldStateArchive
.getMutable(header, true)
.orElseThrow(
() ->
new IllegalArgumentException(
"Public world state not available for block " + header.toLogString()));
}

private BlockStateCall createBlockStateCall(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,6 @@ private TransactionProcessingResult createTransactionProcessingResult(
simulatorResult.result().getOutput(),
simulatorResult.result().getGasRemaining(),
null, // TODO ADD ERROR
null);// TODO ADD LOG
null); // TODO ADD LOG
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private List<BlockSimulationResult> processWithMutableWorldState(
return simulationResults;
}

protected BlockSimulationResult processWithMutableWorldState(
public BlockSimulationResult processWithMutableWorldState(
final BlockHeader header, final BlockStateCall blockStateCall, final MutableWorldState ws) {
WorldUpdater updater = ws.updater();
BlockOverrides blockOverrides = blockStateCall.getBlockOverrides();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,37 @@

import org.hyperledger.besu.datatypes.BlockOverrides;
import org.hyperledger.besu.datatypes.Transaction;
import org.hyperledger.besu.plugin.Unstable;
import org.hyperledger.besu.plugin.data.BlockHeader;
import org.hyperledger.besu.plugin.data.BlockSimulationResult;

import java.util.List;

public interface BlockSimulationService extends BesuService {
/**
* Simulate the creation of a block given a parent header, a list of transactions, and a
* timestamp.
* Simulate the creation of a block given header, a list of transactions, and blockOverrides.
*
* @param parentHeader the parent header
* @param header the header
* @param transactions the transactions to include in the block
* @param blockOverrides the blockSimulationOverride of the block
* @return the block context
*/
BlockSimulationResult simulate(
final BlockHeader parentHeader,
final BlockHeader header,
final List<? extends Transaction> transactions,
final BlockOverrides blockOverrides);

/**
* NOTE: This method is experimental and should be used with caution. It may result in database
* inconsistencies if not used properly. Use only in specific scenarios where its behavior is well
* understood.
*
* @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
BlockSimulationResult simulateAndPersist(
BlockHeader header, List<? extends Transaction> transactions, BlockOverrides blockOverrides);
}

0 comments on commit 9661fed

Please sign in to comment.