Skip to content

Commit

Permalink
Introduce a block level and a tx level world state updater
Browse files Browse the repository at this point in the history
to avoid that a long running tx processing invalidates the already
computed world state

Signed-off-by: Fabio Di Fabio <[email protected]>
  • Loading branch information
fab-10 committed Oct 18, 2023
1 parent 06a7145 commit 3286bf2
Showing 1 changed file with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public class BlockTransactionSelector {
private final OperationTracer pluginOperationTracer;
private final EthScheduler ethScheduler;
private final AtomicBoolean isTimeout = new AtomicBoolean(false);
private final WorldUpdater blockWorldStateUpdater;

public BlockTransactionSelector(
final MiningParameters miningParameters,
Expand Down Expand Up @@ -136,6 +137,7 @@ public BlockTransactionSelector(
.map(PluginTransactionSelectorFactory::create)
.orElse(AllAcceptingTransactionSelector.INSTANCE);
pluginOperationTracer = pluginTransactionSelector.getOperationTracer();
blockWorldStateUpdater = worldState.updater();
}

private List<AbstractTransactionSelector> createTransactionSelectors(
Expand Down Expand Up @@ -232,9 +234,9 @@ private TransactionSelectionResult evaluateTransaction(
return handleTransactionNotSelected(pendingTransaction, selectionResult);
}

final WorldUpdater worldStateUpdater = worldState.updater();
final WorldUpdater txWorldStateUpdater = blockWorldStateUpdater.updater();
final TransactionProcessingResult processingResult =
processTransaction(pendingTransaction, worldStateUpdater);
processTransaction(pendingTransaction, txWorldStateUpdater);

var postProcessingSelectionResult =
evaluatePostProcessing(pendingTransaction, processingResult);
Expand All @@ -245,14 +247,15 @@ private TransactionSelectionResult evaluateTransaction(
// production, and do not rely on the presence of this result, since by the time it is
// written,
// the code consuming the selection results, could already have been by another thread
return handleTransactionNotSelected(pendingTransaction, BLOCK_SELECTION_TIMEOUT, true);
return asyncHandleTransactionNotSelected(
pendingTransaction, BLOCK_SELECTION_TIMEOUT, txWorldStateUpdater);
}

if (postProcessingSelectionResult.selected()) {
return handleTransactionSelected(pendingTransaction, processingResult, worldStateUpdater);
return handleTransactionSelected(pendingTransaction, processingResult, txWorldStateUpdater);
}

return handleTransactionNotSelected(pendingTransaction, postProcessingSelectionResult);
return handleTransactionNotSelected(
pendingTransaction, postProcessingSelectionResult, txWorldStateUpdater);
}
}

Expand Down Expand Up @@ -336,14 +339,15 @@ private TransactionProcessingResult processTransaction(
*
* @param pendingTransaction The pending transaction.
* @param processingResult The result of the transaction processing.
* @param worldStateUpdater The world state updater.
* @param txWorldStateUpdater The world state updater.
* @return The result of the transaction selection process.
*/
private TransactionSelectionResult handleTransactionSelected(
final PendingTransaction pendingTransaction,
final TransactionProcessingResult processingResult,
final WorldUpdater worldStateUpdater) {
worldStateUpdater.commit();
final WorldUpdater txWorldStateUpdater) {
txWorldStateUpdater.commit();
blockWorldStateUpdater.commit();
final Transaction transaction = pendingTransaction.getTransaction();

final long gasUsedByTransaction =
Expand Down Expand Up @@ -374,13 +378,17 @@ private TransactionSelectionResult handleTransactionSelected(
*
* @param pendingTransaction The unselected pending transaction.
* @param selectionResult The result of the transaction selection process.
* @param maybeTxWorldStateUpdater The optional world state updater for the pending transaction.
* @param notifyAsync If the plugin should be notified asynchronously.
* @return The result of the transaction selection process.
*/
private TransactionSelectionResult handleTransactionNotSelected(
final PendingTransaction pendingTransaction,
final TransactionSelectionResult selectionResult,
final Optional<WorldUpdater> maybeTxWorldStateUpdater,
final boolean notifyAsync) {
maybeTxWorldStateUpdater.ifPresent(WorldUpdater::revert);

transactionSelectionResults.updateNotSelected(
pendingTransaction.getTransaction(), selectionResult);
final Runnable notifyPlugin =
Expand All @@ -394,10 +402,27 @@ private TransactionSelectionResult handleTransactionNotSelected(
return selectionResult;
}

private TransactionSelectionResult handleTransactionNotSelected(
final PendingTransaction pendingTransaction,
final TransactionSelectionResult selectionResult,
final WorldUpdater txWorldStateUpdater) {
return handleTransactionNotSelected(
pendingTransaction, selectionResult, Optional.of(txWorldStateUpdater), false);
}

private TransactionSelectionResult asyncHandleTransactionNotSelected(
final PendingTransaction pendingTransaction,
final TransactionSelectionResult selectionResult,
final WorldUpdater txWorldStateUpdater) {
return handleTransactionNotSelected(
pendingTransaction, selectionResult, Optional.of(txWorldStateUpdater), true);
}

private TransactionSelectionResult handleTransactionNotSelected(
final PendingTransaction pendingTransaction,
final TransactionSelectionResult selectionResult) {
return handleTransactionNotSelected(pendingTransaction, selectionResult, false);
return handleTransactionNotSelected(
pendingTransaction, selectionResult, Optional.empty(), false);
}

private void checkCancellation() {
Expand Down

0 comments on commit 3286bf2

Please sign in to comment.