Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel-Trintinalia <[email protected]>
  • Loading branch information
Gabriel-Trintinalia committed Dec 9, 2024
1 parent 3ee61a4 commit 9f0e610
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ public BlockSimulator(
this.transactionSimulator = transactionSimulator;
}

/**
* Processes a list of BlockStateCalls sequentially, collecting the results.
*
* @param header The block header for all simulations.
* @param blockStateCalls The list of BlockStateCalls to process.
* @return A list of BlockSimulationResult objects from processing each BlockStateCall.
*/
public List<BlockSimulationResult> process(
final BlockHeader header, final List<? extends BlockStateCall> blockStateCalls) {
try (final MutableWorldState ws =
Expand Down Expand Up @@ -110,6 +117,14 @@ public List<BlockSimulationResult> processWithMutableWorldState(
return simulationResults;
}

/**
* Processes a single BlockStateCall, simulating the block execution.
*
* @param header The block header for the simulation.
* @param blockStateCall The BlockStateCall to process.
* @param ws The MutableWorldState to use for the simulation.
* @return A BlockSimulationResult from processing the BlockStateCall.
*/
private BlockSimulationResult processWithMutableWorldState(
final BlockHeader header, final BlockStateCall blockStateCall, final MutableWorldState ws) {
BlockOverrides blockOverrides = blockStateCall.getBlockOverrides();
Expand All @@ -121,14 +136,7 @@ private BlockSimulationResult processWithMutableWorldState(
BlockHeader blockHeader = applyBlockHeaderOverrides(header, newProtocolSpec, blockOverrides);

// Apply state overrides
blockStateCall
.getAccountOverrides()
.ifPresent(
overrides -> {
var updater = ws.updater();
applyStateOverrides(overrides, updater);
updater.commit();
});
blockStateCall.getAccountOverrides().ifPresent(overrides -> applyStateOverrides(overrides, ws));

long currentGasUsed = 0;
final var transactionReceiptFactory = newProtocolSpec.getTransactionReceiptFactory();
Expand All @@ -145,7 +153,7 @@ private BlockSimulationResult processWithMutableWorldState(
final Optional<TransactionSimulatorResult> transactionSimulatorResult =
transactionSimulator.processWithWorldUpdater(
callParameter,
Optional.empty(),
Optional.empty(), // We have already applied state overrides on block level
buildTransactionValidationParams(blockStateCall.isValidate()),
OperationTracer.NO_TRACING,
blockHeader,
Expand Down Expand Up @@ -191,9 +199,17 @@ private BlockSimulationResult processWithMutableWorldState(
return new BlockSimulationResult(block, receipts, transactionSimulations);
}

/**
* Applies state overrides to the world state.
*
* @param accountOverrideMap The AccountOverrideMap containing the state overrides.
* @param ws The MutableWorldState to apply the overrides to.
*/
private void applyStateOverrides(
final AccountOverrideMap accountOverrideMap, final WorldUpdater updater) {
final AccountOverrideMap accountOverrideMap, final MutableWorldState ws) {
var updater = ws.updater();
for (Address accountToOverride : accountOverrideMap.keySet()) {

final AccountOverride override = accountOverrideMap.get(accountToOverride);
MutableAccount account = updater.getOrCreate(accountToOverride);
override.getNonce().ifPresent(account::setNonce);
Expand All @@ -210,8 +226,17 @@ private void applyStateOverrides(
account.setStorageValue(
UInt256.fromHexString(key), UInt256.fromHexString(value))));
}
updater.commit();
}

/**
* Applies block header overrides to the block header.
*
* @param header The original block header.
* @param newProtocolSpec The ProtocolSpec for the block.
* @param blockOverrides The BlockOverrides to apply.
* @return The modified block header.
*/
private BlockHeader applyBlockHeaderOverrides(
final BlockHeader header,
final ProtocolSpec newProtocolSpec,
Expand Down Expand Up @@ -245,6 +270,17 @@ private BlockHeader applyBlockHeaderOverrides(
.buildBlockHeader();
}

/**
* Creates the final block header after applying state changes and transaction processing.
*
* @param blockHeader The original block header.
* @param ws The MutableWorldState after applying state overrides.
* @param transactions The list of transactions in the block.
* @param blockOverrides The BlockOverrides to apply.
* @param receipts The list of transaction receipts.
* @param currentGasUsed The total gas used in the block.
* @return The final block header.
*/
private BlockHeader createFinalBlockHeader(
final BlockHeader blockHeader,
final MutableWorldState ws,
Expand All @@ -269,6 +305,12 @@ private BlockHeader createFinalBlockHeader(
.buildBlockHeader();
}

/**
* Builds the TransactionValidationParams for the block simulation.
*
* @param shouldValidate Whether to validate transactions.
* @return The TransactionValidationParams for the block simulation.
*/
private ImmutableTransactionValidationParams buildTransactionValidationParams(
final boolean shouldValidate) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,31 +341,6 @@ private MutableWorldState getWorldState(final BlockHeader header) {
"Public world state not available for block " + header.toLogString()));
}

@Nonnull
public Optional<TransactionSimulatorResult> processWithWorldUpdater(
final CallParameter callParams,
final Optional<AccountOverrideMap> maybeStateOverrides,
final TransactionValidationParams transactionValidationParams,
final OperationTracer operationTracer,
final BlockHeader header,
final WorldUpdater updater) {

Address miningBeneficiary =
protocolSchedule
.getByBlockHeader(header)
.getMiningBeneficiaryCalculator()
.calculateBeneficiary(header);

return processWithWorldUpdater(
callParams,
maybeStateOverrides,
transactionValidationParams,
operationTracer,
header,
updater,
miningBeneficiary);
}

@Nonnull
public Optional<TransactionSimulatorResult> processWithWorldUpdater(
final CallParameter callParams,
Expand Down

0 comments on commit 9f0e610

Please sign in to comment.