Skip to content

Commit

Permalink
Tests and optimizations
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 Dec 2, 2024
1 parent 591edb5 commit e489386
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.Transaction;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.mainnet.ImmutableTransactionValidationParams;
import org.hyperledger.besu.ethereum.mainnet.TransactionValidationParams;
import org.hyperledger.besu.ethereum.mainnet.ValidationResult;
import org.hyperledger.besu.ethereum.processing.TransactionProcessingResult;
Expand All @@ -35,11 +34,6 @@
/** TransactionSimulationServiceImpl */
@Unstable
public class TransactionSimulationServiceImpl implements TransactionSimulationService {
private static final TransactionValidationParams SIMULATOR_ALLOWING_EXCEEDING_BALANCE =
ImmutableTransactionValidationParams.builder()
.from(TransactionValidationParams.transactionSimulator())
.isAllowExceedingBalance(true)
.build();
private Blockchain blockchain;
private TransactionSimulator transactionSimulator;

Expand Down Expand Up @@ -85,7 +79,7 @@ public Optional<TransactionSimulationResult> simulate(
.process(
callParameter,
isAllowExceedingBalance
? SIMULATOR_ALLOWING_EXCEEDING_BALANCE
? TransactionValidationParams.transactionSimulatorAllowExceedingBalance()
: TransactionValidationParams.transactionSimulator(),
operationTracer,
maybeBlockHeader.get())
Expand All @@ -97,7 +91,7 @@ public Optional<TransactionSimulationResult> simulate(
callParameter,
maybeAccountOverrides,
isAllowExceedingBalance
? SIMULATOR_ALLOWING_EXCEEDING_BALANCE
? TransactionValidationParams.transactionSimulatorAllowExceedingBalance()
: TransactionValidationParams.transactionSimulator(),
operationTracer,
transactionSimulator.simulatePendingBlockHeader())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package org.hyperledger.besu.consensus.qbft.validator;

import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.ethereum.mainnet.ImmutableTransactionValidationParams;
import org.hyperledger.besu.ethereum.mainnet.TransactionValidationParams;
import org.hyperledger.besu.ethereum.transaction.CallParameter;
import org.hyperledger.besu.ethereum.transaction.TransactionSimulator;
Expand Down Expand Up @@ -94,10 +93,7 @@ private Optional<TransactionSimulatorResult> callFunction(
final CallParameter callParams =
new CallParameter(null, contractAddress, -1, null, null, payload);
final TransactionValidationParams transactionValidationParams =
ImmutableTransactionValidationParams.builder()
.from(TransactionValidationParams.transactionSimulator())
.isAllowExceedingBalance(true)
.build();
TransactionValidationParams.transactionSimulatorAllowExceedingBalance();
return transactionSimulator.process(
callParams, transactionValidationParams, OperationTracer.NO_TRACING, blockNumber);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.Difficulty;
import org.hyperledger.besu.ethereum.core.LogWithMetadata;
import org.hyperledger.besu.ethereum.mainnet.ImmutableTransactionValidationParams;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.TransactionValidationParams;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput;
Expand Down Expand Up @@ -359,14 +358,9 @@ private Optional<CallResult> executeCall(final DataFetchingEnvironment environme
data,
Optional.empty());

ImmutableTransactionValidationParams.Builder transactionValidationParams =
ImmutableTransactionValidationParams.builder()
.from(TransactionValidationParams.transactionSimulator());
transactionValidationParams.isAllowExceedingBalance(true);

return transactionSimulator.process(
param,
transactionValidationParams.build(),
TransactionValidationParams.transactionSimulatorAllowExceedingBalance(),
OperationTracer.NO_TRACING,
(mutableWorldState, transactionSimulatorResult) ->
transactionSimulatorResult.map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.mainnet.ImmutableTransactionValidationParams;
import org.hyperledger.besu.ethereum.mainnet.TransactionValidationParams;
import org.hyperledger.besu.ethereum.mainnet.ValidationResult;
import org.hyperledger.besu.ethereum.processing.TransactionProcessingResult;
Expand All @@ -46,11 +45,6 @@
import com.google.common.annotations.VisibleForTesting;

public abstract class AbstractEstimateGas extends AbstractBlockParameterMethod {
protected static final TransactionValidationParams ALLOW_EXCEEDING_BALANCE_VALIDATION_PARAMETERS =
ImmutableTransactionValidationParams.builder()
.from(TransactionValidationParams.transactionSimulator())
.isAllowExceedingBalance(true)
.build();

private static final double SUB_CALL_REMAINING_GAS_RATIO = 65D / 64D;

Expand Down Expand Up @@ -214,10 +208,9 @@ protected static TransactionValidationParams getTransactionValidationParams(
final JsonCallParameter callParams) {
final boolean isAllowExceedingBalance = !callParams.isMaybeStrict().orElse(Boolean.FALSE);

if (isAllowExceedingBalance) {
return ALLOW_EXCEEDING_BALANCE_VALIDATION_PARAMETERS;
}
return TransactionValidationParams.transactionSimulator();
return isAllowExceedingBalance
? TransactionValidationParams.transactionSimulatorAllowExceedingBalance()
: TransactionValidationParams.transactionSimulator();
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.debug.TraceOptions;
import org.hyperledger.besu.ethereum.mainnet.ImmutableTransactionValidationParams;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.TransactionValidationParams;
import org.hyperledger.besu.ethereum.transaction.TransactionSimulator;
Expand Down Expand Up @@ -117,9 +116,7 @@ protocolSchedule, transactionTrace, block, new AtomicInteger(), false)
}

protected TransactionValidationParams buildTransactionValidationParams() {
return ImmutableTransactionValidationParams.builder()
.from(TransactionValidationParams.transactionSimulator())
.build();
return TransactionValidationParams.transactionSimulator();
}

protected TraceOptions buildTraceOptions(final Set<TraceTypeParameter.TraceType> traceTypes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.mainnet.ImmutableTransactionValidationParams;
import org.hyperledger.besu.ethereum.mainnet.TransactionValidationParams;
import org.hyperledger.besu.ethereum.mainnet.ValidationResult;
import org.hyperledger.besu.ethereum.processing.TransactionProcessingResult;
Expand Down Expand Up @@ -169,20 +168,18 @@ private JsonRpcErrorResponse errorResponse(
private TransactionValidationParams buildTransactionValidationParams(
final BlockHeader header, final JsonCallParameter callParams) {

ImmutableTransactionValidationParams.Builder transactionValidationParams =
ImmutableTransactionValidationParams.builder()
.from(TransactionValidationParams.transactionSimulator());

final boolean isAllowExceedingBalance;
// if it is not set explicitly whether we want a strict check of the balance or not. this will
// be decided according to the provided parameters
if (callParams.isMaybeStrict().isEmpty()) {
transactionValidationParams.isAllowExceedingBalance(
isAllowExceedingBalanceAutoSelection(header, callParams));
isAllowExceedingBalance = isAllowExceedingBalanceAutoSelection(header, callParams);

} else {
transactionValidationParams.isAllowExceedingBalance(
!callParams.isMaybeStrict().orElse(Boolean.FALSE));
isAllowExceedingBalance = !callParams.isMaybeStrict().orElse(Boolean.FALSE);
}
return transactionValidationParams.build();
return isAllowExceedingBalance
? TransactionValidationParams.transactionSimulatorAllowExceedingBalance()
: TransactionValidationParams.transactionSimulator();
}

private boolean isAllowExceedingBalanceAutoSelection(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.mainnet.ImmutableTransactionValidationParams;
import org.hyperledger.besu.ethereum.mainnet.TransactionValidationParams;
import org.hyperledger.besu.ethereum.mainnet.ValidationResult;
import org.hyperledger.besu.ethereum.processing.TransactionProcessingResult;
Expand Down Expand Up @@ -376,11 +375,7 @@ public void shouldIgnoreSenderBalanceAccountWhenStrictModeDisabled() {
.process(
eq(modifiedLegacyTransactionCallParameter(Wei.ZERO)),
eq(Optional.empty()), // no account overrides
eq(
ImmutableTransactionValidationParams.builder()
.from(TransactionValidationParams.transactionSimulator())
.isAllowExceedingBalance(true)
.build()),
eq(TransactionValidationParams.transactionSimulatorAllowExceedingBalance()),
any(OperationTracer.class),
eq(latestBlockHeader));
}
Expand All @@ -397,11 +392,7 @@ public void shouldNotIgnoreSenderBalanceAccountWhenStrictModeEnabled() {
.process(
eq(modifiedLegacyTransactionCallParameter(Wei.ZERO)),
eq(Optional.empty()), // no account overrides
eq(
ImmutableTransactionValidationParams.builder()
.from(TransactionValidationParams.transactionSimulator())
.isAllowExceedingBalance(false)
.build()),
eq(TransactionValidationParams.transactionSimulator()),
any(OperationTracer.class),
eq(latestBlockHeader));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public interface TransactionValidationParams {
TransactionValidationParams transactionSimulatorParams =
ImmutableTransactionValidationParams.of(false, false, false, false, false, true);

TransactionValidationParams transactionSimulatorAllowExceedingBalanceParams =
ImmutableTransactionValidationParams.of(false, true, false, false, false, true);

@Value.Default
default boolean isAllowFutureNonce() {
return false;
Expand Down Expand Up @@ -69,6 +72,10 @@ static TransactionValidationParams transactionSimulator() {
return transactionSimulatorParams;
}

static TransactionValidationParams transactionSimulatorAllowExceedingBalance() {
return transactionSimulatorAllowExceedingBalanceParams;
}

static TransactionValidationParams processingBlock() {
return processingBlockParams;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.hyperledger.besu.ethereum.core.MutableWorldState;
import org.hyperledger.besu.ethereum.core.ProcessableBlockHeader;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.mainnet.ImmutableTransactionValidationParams;
import org.hyperledger.besu.ethereum.mainnet.MainnetTransactionProcessor;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpec;
Expand Down Expand Up @@ -216,10 +215,7 @@ public Optional<TransactionSimulatorResult> processAtHead(final CallParameter ca
final var chainHeadHash = blockchain.getChainHeadHash();
return process(
callParams,
ImmutableTransactionValidationParams.builder()
.from(TransactionValidationParams.transactionSimulator())
.isAllowExceedingBalance(true)
.build(),
TransactionValidationParams.transactionSimulatorAllowExceedingBalance(),
OperationTracer.NO_TRACING,
(mutableWorldState, transactionSimulatorResult) -> transactionSimulatorResult,
blockchain
Expand Down
Loading

0 comments on commit e489386

Please sign in to comment.