Skip to content

Commit

Permalink
add cli param to disable precompile caching
Browse files Browse the repository at this point in the history
Signed-off-by: garyschulte <[email protected]>
  • Loading branch information
garyschulte committed Jan 10, 2025
1 parent 64c45d7 commit 404b54b
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 27 deletions.
7 changes: 7 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 @@ -139,6 +139,7 @@
import org.hyperledger.besu.ethereum.worldstate.ImmutableDataStorageConfiguration;
import org.hyperledger.besu.ethereum.worldstate.ImmutableDiffBasedSubStorageConfiguration;
import org.hyperledger.besu.evm.precompile.AbstractAltBnPrecompiledContract;
import org.hyperledger.besu.evm.precompile.AbstractPrecompiledContract;
import org.hyperledger.besu.evm.precompile.BigIntegerModularExponentiationPrecompiledContract;
import org.hyperledger.besu.evm.precompile.KZGPointEvalPrecompiledContract;
import org.hyperledger.besu.metrics.BesuMetricCategory;
Expand Down Expand Up @@ -696,6 +697,11 @@ static class PrivacyOptionGroup {
description = "Specifies the number of last blocks to cache (default: ${DEFAULT-VALUE})")
private final Integer numberOfblocksToCache = 0;

@CommandLine.Option(
names = {"--cache-precompiles"},
description = "Specifies whether to cache precompile results (default: ${DEFAULT-VALUE})")
private final Boolean enablePrecompileCaching = true;

// Plugins Configuration Option Group
@CommandLine.ArgGroup(validate = false)
PluginsConfigurationOptions pluginsConfigurationOptions = new PluginsConfigurationOptions();
Expand Down Expand Up @@ -971,6 +977,7 @@ public void run() {
VersionMetadata.versionCompatibilityChecks(versionCompatibilityProtection, dataDir());

configureNativeLibs();
AbstractPrecompiledContract.setPrecompileCaching(enablePrecompileCaching);
besuController = buildController();

besuPluginContext.beforeExternalServices();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,10 @@ protected GasCalculator gasCalculator() {
public String getName() {
return name;
}

protected static Boolean enableResultCaching = Boolean.TRUE;

public static void setPrecompileCaching(final boolean enablePrecompileCaching) {
enableResultCaching = enablePrecompileCaching;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public class AltBN128AddPrecompiledContract extends AbstractAltBnPrecompiledCont
private static final int PARAMETER_LENGTH = 128;

private final long gasCost;

private static final Cache<Integer, PrecompileInputResultTuple> bnAddCache =
Caffeine.newBuilder().maximumSize(1000).build();

Expand Down Expand Up @@ -79,19 +78,24 @@ public long gasRequirement(final Bytes input) {
@Override
public PrecompileContractResult computePrecompile(
final Bytes input, @Nonnull final MessageFrame messageFrame) {
var cachedRes = bnAddCache.getIfPresent(input.hashCode());
if (cachedRes != null && cachedRes.cachedInput().equals(input)) {
return cachedRes.cachedResult();
}

PrecompileInputResultTuple calculatedRes;
PrecompileInputResultTuple res;

if (enableResultCaching) {
res = bnAddCache.getIfPresent(input.hashCode());
if (res != null && res.cachedInput().equals(input)) {
return res.cachedResult();
}
}
if (useNative) {
calculatedRes = new PrecompileInputResultTuple(input, computeNative(input, messageFrame));
res = new PrecompileInputResultTuple(input, computeNative(input, messageFrame));
} else {
calculatedRes = new PrecompileInputResultTuple(input, computeDefault(input));
res = new PrecompileInputResultTuple(input, computeDefault(input));
}
if (enableResultCaching) {
bnAddCache.put(input.hashCode(), res);
}
bnAddCache.put(input.hashCode(), calculatedRes);
return calculatedRes.cachedResult();
return res.cachedResult();
}

private static PrecompileContractResult computeDefault(final Bytes input) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,12 @@ public long gasRequirement(final Bytes input) {
public PrecompileContractResult computePrecompile(
final Bytes input, @Nonnull final MessageFrame messageFrame) {

var res = bnMulCache.getIfPresent(input.hashCode());
if (res != null && res.cachedInput().equals(input)) {
return res.cachedResult();
PrecompileInputResultTuple res;
if (enableResultCaching) {
res = bnMulCache.getIfPresent(input.hashCode());
if (res != null && res.cachedInput().equals(input)) {
return res.cachedResult();
}
}

if (input.size() >= 64 && input.slice(0, 64).equals(POINT_AT_INFINITY)) {
Expand All @@ -99,7 +102,9 @@ public PrecompileContractResult computePrecompile(
} else {
res = new PrecompileInputResultTuple(input, computeDefault(input));
}
bnMulCache.put(input.hashCode(), res);
if (enableResultCaching) {
bnMulCache.put(input.hashCode(), res);
}
return res.cachedResult();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ public class AltBN128PairingPrecompiledContract extends AbstractAltBnPrecompiled
private static final int FIELD_LENGTH = 32;
private static final int PARAMETER_LENGTH = 192;
private static final Cache<Integer, PrecompileInputResultTuple> bnPairingCache =
Caffeine.newBuilder().maximumWeight(16_000_000).weigher((k, v) -> ((Bytes) k).size()).build();
Caffeine.newBuilder()
.maximumWeight(16_000_000)
.weigher((k, v) -> ((PrecompileInputResultTuple) v).cachedInput().size())
.build();

/** The constant FALSE. */
static final Bytes FALSE =
Expand Down Expand Up @@ -103,16 +106,22 @@ public PrecompileContractResult computePrecompile(
return PrecompileContractResult.halt(
null, Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR));
}
var res = bnPairingCache.getIfPresent(input.hashCode());
if (res != null && res.cachedInput().equals(input)) {
return res.cachedResult();
PrecompileInputResultTuple res;
if (enableResultCaching) {
res = bnPairingCache.getIfPresent(input.hashCode());
if (res != null && res.cachedInput().equals(input)) {
return res.cachedResult();
}
}
if (useNative) {
res = new PrecompileInputResultTuple(input, computeNative(input, messageFrame));
} else {
res = new PrecompileInputResultTuple(input, computeDefault(input));
}
bnPairingCache.put(input.hashCode(), res);
if (enableResultCaching) {
bnPairingCache.put(input.hashCode(), res);
}

return res.cachedResult();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,22 @@ public PrecompileContractResult computePrecompile(
return PrecompileContractResult.halt(
null, Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR));
}
var res = blakeCache.getIfPresent(input.hashCode());
if (res != null && res.cachedInput().equals(input)) {
return res.cachedResult();
PrecompileInputResultTuple res = null;
if (enableResultCaching) {
res = blakeCache.getIfPresent(input.hashCode());
if (res != null && res.cachedInput().equals(input)) {
return res.cachedResult();
}
}

res =
new PrecompileInputResultTuple(
input, PrecompileContractResult.success(Hash.blake2bf(input)));
blakeCache.put(input.hashCode(), res);

if (enableResultCaching) {
blakeCache.put(input.hashCode(), res);
}

return res.cachedResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@ public PrecompileContractResult computePrecompile(
return PrecompileContractResult.success(Bytes.EMPTY);
}

var res = ecrecCache.getIfPresent(input.hashCode());
if (res != null && res.cachedInput().equals(input)) {
return res.cachedResult();
PrecompileInputResultTuple res;

if (enableResultCaching) {
res = ecrecCache.getIfPresent(input.hashCode());
if (res != null && res.cachedInput().equals(input)) {
return res.cachedResult();
}
}

final int recId = d.get(63) - V_BASE;
Expand Down Expand Up @@ -115,7 +119,9 @@ public PrecompileContractResult computePrecompile(
final MutableBytes32 result = MutableBytes32.create();
hashed.slice(12).copyTo(result, 12);
res = new PrecompileInputResultTuple(input, PrecompileContractResult.success(result));
ecrecCache.put(input.hashCode(), res);
if (enableResultCaching) {
ecrecCache.put(input.hashCode(), res);
}
return res.cachedResult();
} catch (final IllegalArgumentException e) {
return PrecompileContractResult.success(Bytes.EMPTY);
Expand Down

0 comments on commit 404b54b

Please sign in to comment.