From fc6b2ec13ccd35f4088b98cd111ee7d4b6b9f436 Mon Sep 17 00:00:00 2001 From: Danno Ferrin Date: Wed, 6 Apr 2022 09:35:50 -0600 Subject: [PATCH] Fix precompile deprecation warnings (#3690) Migrate all the precompiles to the non-deprecated method, making sonar analysis happy. Signed-off-by: Danno Ferrin --- .../FlexiblePrivacyPrecompiledContract.java | 12 ++++++---- .../PrivacyPluginPrecompiledContract.java | 4 +++- .../privacy/PrivacyPrecompiledContract.java | 4 +++- .../besu/evm/frame/ExceptionalHaltReason.java | 6 +++-- .../AbstractAltBnPrecompiledContract.java | 15 ++++++++---- .../AbstractBLS12PrecompiledContract.java | 11 ++++++--- .../AltBN128AddPrecompiledContract.java | 13 ++++++---- .../AltBN128MulPrecompiledContract.java | 14 +++++++---- .../AltBN128PairingPrecompiledContract.java | 24 ++++++++++++------- .../BLAKE2BFPrecompileContract.java | 14 +++++++---- ...ularExponentiationPrecompiledContract.java | 9 ++++--- .../precompile/ECRECPrecompiledContract.java | 16 +++++++------ .../evm/precompile/IDPrecompiledContract.java | 6 +++-- .../evm/precompile/PrecompiledContract.java | 3 ++- .../RIPEMD160PrecompiledContract.java | 6 +++-- .../precompile/SHA256PrecompiledContract.java | 7 ++++-- 16 files changed, 111 insertions(+), 53 deletions(-) diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/precompiles/privacy/FlexiblePrivacyPrecompiledContract.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/precompiles/privacy/FlexiblePrivacyPrecompiledContract.java index cc652f8d76..ec8121949c 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/precompiles/privacy/FlexiblePrivacyPrecompiledContract.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/precompiles/privacy/FlexiblePrivacyPrecompiledContract.java @@ -51,6 +51,7 @@ import java.util.Base64; import java.util.Collections; import java.util.List; import java.util.Optional; +import javax.annotation.Nonnull; import org.apache.tuweni.bytes.Bytes; import org.apache.tuweni.bytes.Bytes32; @@ -98,9 +99,10 @@ public class FlexiblePrivacyPrecompiledContract extends PrivacyPrecompiledContra return privateTransactionEventObservers.unsubscribe(observerId); } + @Nonnull @Override public PrecompileContractResult computePrecompile( - final Bytes input, final MessageFrame messageFrame) { + final Bytes input, @Nonnull final MessageFrame messageFrame) { if (skipContractExecution(messageFrame)) { return NO_RESULT; } @@ -248,7 +250,7 @@ public class FlexiblePrivacyPrecompiledContract extends PrivacyPrecompiledContra return false; } - if (isContractLocked && !isTargettingFlexiblePrivacyProxy(privateTransaction)) { + if (isContractLocked && !isTargetingFlexiblePrivacyProxy(privateTransaction)) { LOG.debug( "Privacy Group {} is locked while trying to execute transaction with commitment {}", privacyGroupId.toHexString(), @@ -310,13 +312,13 @@ public class FlexiblePrivacyPrecompiledContract extends PrivacyPrecompiledContra return input.slice(4).toBase64String(); } - private boolean isTargettingFlexiblePrivacyProxy(final PrivateTransaction privateTransaction) { + private boolean isTargetingFlexiblePrivacyProxy(final PrivateTransaction privateTransaction) { return privateTransaction.getTo().isPresent() && privateTransaction.getTo().get().equals(FLEXIBLE_PRIVACY_PROXY); } private boolean isAddingParticipant(final PrivateTransaction privateTransaction) { - return isTargettingFlexiblePrivacyProxy(privateTransaction) + return isTargetingFlexiblePrivacyProxy(privateTransaction) && privateTransaction .getPayload() .toHexString() @@ -324,7 +326,7 @@ public class FlexiblePrivacyPrecompiledContract extends PrivacyPrecompiledContra } private boolean isRemovingParticipant(final PrivateTransaction privateTransaction) { - return isTargettingFlexiblePrivacyProxy(privateTransaction) + return isTargetingFlexiblePrivacyProxy(privateTransaction) && privateTransaction .getPayload() .toHexString() diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/precompiles/privacy/PrivacyPluginPrecompiledContract.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/precompiles/privacy/PrivacyPluginPrecompiledContract.java index 1740733092..723af2efa4 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/precompiles/privacy/PrivacyPluginPrecompiledContract.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/precompiles/privacy/PrivacyPluginPrecompiledContract.java @@ -27,6 +27,7 @@ import org.hyperledger.besu.evm.gascalculator.GasCalculator; import org.hyperledger.besu.evm.worldstate.WorldUpdater; import java.util.Optional; +import javax.annotation.Nonnull; import org.apache.tuweni.bytes.Bytes; import org.apache.tuweni.bytes.Bytes32; @@ -43,9 +44,10 @@ public class PrivacyPluginPrecompiledContract extends PrivacyPrecompiledContract this.privacyParameters = privacyParameters; } + @Nonnull @Override public PrecompileContractResult computePrecompile( - final Bytes input, final MessageFrame messageFrame) { + final Bytes input, @Nonnull final MessageFrame messageFrame) { if (skipContractExecution(messageFrame)) { return NO_RESULT; } diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/precompiles/privacy/PrivacyPrecompiledContract.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/precompiles/privacy/PrivacyPrecompiledContract.java index 6856ec0a03..948551a60b 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/precompiles/privacy/PrivacyPrecompiledContract.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/precompiles/privacy/PrivacyPrecompiledContract.java @@ -50,6 +50,7 @@ import org.hyperledger.besu.plugin.data.Hash; import java.util.Base64; import java.util.Optional; +import javax.annotation.Nonnull; import org.apache.tuweni.bytes.Bytes; import org.apache.tuweni.bytes.Bytes32; @@ -106,9 +107,10 @@ public class PrivacyPrecompiledContract extends AbstractPrecompiledContract { return 0L; } + @Nonnull @Override public PrecompileContractResult computePrecompile( - final Bytes input, final MessageFrame messageFrame) { + final Bytes input, @Nonnull final MessageFrame messageFrame) { if (skipContractExecution(messageFrame)) { return NO_RESULT; diff --git a/evm/src/main/java/org/hyperledger/besu/evm/frame/ExceptionalHaltReason.java b/evm/src/main/java/org/hyperledger/besu/evm/frame/ExceptionalHaltReason.java index 9037636bbc..5f91d081d3 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/frame/ExceptionalHaltReason.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/frame/ExceptionalHaltReason.java @@ -30,6 +30,7 @@ public interface ExceptionalHaltReason { ExceptionalHaltReason OUT_OF_BOUNDS = DefaultExceptionalHaltReason.OUT_OF_BOUNDS; ExceptionalHaltReason CODE_TOO_LARGE = DefaultExceptionalHaltReason.CODE_TOO_LARGE; ExceptionalHaltReason INVALID_CODE = DefaultExceptionalHaltReason.INVALID_CODE; + ExceptionalHaltReason PRECOMPILE_ERROR = DefaultExceptionalHaltReason.PRECOMPILE_ERROR; String name(); @@ -46,9 +47,10 @@ public interface ExceptionalHaltReason { ILLEGAL_STATE_CHANGE("Illegal state change"), OUT_OF_BOUNDS("Out of bounds"), CODE_TOO_LARGE("Code is too large"), - INVALID_CODE("Code is invalid"); + INVALID_CODE("Code is invalid"), + PRECOMPILE_ERROR("Precompile error"); - String description; + final String description; DefaultExceptionalHaltReason(final String description) { this.description = description; diff --git a/evm/src/main/java/org/hyperledger/besu/evm/precompile/AbstractAltBnPrecompiledContract.java b/evm/src/main/java/org/hyperledger/besu/evm/precompile/AbstractAltBnPrecompiledContract.java index 20ae8a8e37..79c38cf0df 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/precompile/AbstractAltBnPrecompiledContract.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/precompile/AbstractAltBnPrecompiledContract.java @@ -17,10 +17,14 @@ package org.hyperledger.besu.evm.precompile; import static java.nio.charset.StandardCharsets.UTF_8; +import org.hyperledger.besu.evm.frame.ExceptionalHaltReason; import org.hyperledger.besu.evm.frame.MessageFrame; import org.hyperledger.besu.evm.gascalculator.GasCalculator; import org.hyperledger.besu.nativelib.bls12_381.LibEthPairings; +import java.util.Optional; +import javax.annotation.Nonnull; + import com.sun.jna.ptr.IntByReference; import org.apache.tuweni.bytes.Bytes; import org.slf4j.Logger; @@ -58,7 +62,9 @@ public abstract class AbstractAltBnPrecompiledContract extends AbstractPrecompil } } - public Bytes computeNative(final Bytes input, final MessageFrame messageFrame) { + @Nonnull + public PrecompileContractResult computeNative( + final @Nonnull Bytes input, final MessageFrame messageFrame) { final byte[] result = new byte[LibEthPairings.EIP196_PREALLOCATE_FOR_RESULT_BYTES]; final byte[] error = new byte[LibEthPairings.EIP2537_PREALLOCATE_FOR_ERROR_BYTES]; @@ -66,7 +72,7 @@ public abstract class AbstractAltBnPrecompiledContract extends AbstractPrecompil new IntByReference(LibEthPairings.EIP196_PREALLOCATE_FOR_RESULT_BYTES); final IntByReference err_len = new IntByReference(LibEthPairings.EIP2537_PREALLOCATE_FOR_ERROR_BYTES); - int inputSize = Math.min(inputLen, input.size()); + final int inputSize = Math.min(inputLen, input.size()); final int errorNo = LibEthPairings.eip196_perform_operation( operationId, @@ -77,12 +83,13 @@ public abstract class AbstractAltBnPrecompiledContract extends AbstractPrecompil error, err_len); if (errorNo == 0) { - return Bytes.wrap(result, 0, o_len.getValue()); + return PrecompileContractResult.success(Bytes.wrap(result, 0, o_len.getValue())); } else { final String errorString = new String(error, 0, err_len.getValue(), UTF_8); messageFrame.setRevertReason(Bytes.wrap(error, 0, err_len.getValue())); LOG.trace("Error executing precompiled contract {}: '{}'", getName(), errorString); - return null; + return PrecompileContractResult.halt( + null, Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR)); } } } diff --git a/evm/src/main/java/org/hyperledger/besu/evm/precompile/AbstractBLS12PrecompiledContract.java b/evm/src/main/java/org/hyperledger/besu/evm/precompile/AbstractBLS12PrecompiledContract.java index 3869ff88cf..161162bc9e 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/precompile/AbstractBLS12PrecompiledContract.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/precompile/AbstractBLS12PrecompiledContract.java @@ -17,9 +17,11 @@ package org.hyperledger.besu.evm.precompile; import static java.nio.charset.StandardCharsets.UTF_8; +import org.hyperledger.besu.evm.frame.ExceptionalHaltReason; import org.hyperledger.besu.evm.frame.MessageFrame; import org.hyperledger.besu.nativelib.bls12_381.LibEthPairings; +import java.util.Optional; import javax.annotation.Nonnull; import com.sun.jna.ptr.IntByReference; @@ -61,8 +63,10 @@ public abstract class AbstractBLS12PrecompiledContract implements PrecompiledCon return name; } + @Nonnull @Override - public Bytes compute(final Bytes input, @Nonnull final MessageFrame messageFrame) { + public PrecompileContractResult computePrecompile( + final Bytes input, @Nonnull final MessageFrame messageFrame) { final byte[] result = new byte[LibEthPairings.EIP2537_PREALLOCATE_FOR_RESULT_BYTES]; final byte[] error = new byte[LibEthPairings.EIP2537_PREALLOCATE_FOR_ERROR_BYTES]; @@ -81,12 +85,13 @@ public abstract class AbstractBLS12PrecompiledContract implements PrecompiledCon error, err_len); if (errorNo == 0) { - return Bytes.wrap(result, 0, o_len.getValue()); + return PrecompileContractResult.success(Bytes.wrap(result, 0, o_len.getValue())); } else { final String errorMessage = new String(error, 0, err_len.getValue(), UTF_8); messageFrame.setRevertReason(Bytes.wrap(error, 0, err_len.getValue())); LOG.trace("Error executing precompiled contract {}: '{}'", name, errorMessage); - return null; + return PrecompileContractResult.halt( + null, Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR)); } } diff --git a/evm/src/main/java/org/hyperledger/besu/evm/precompile/AltBN128AddPrecompiledContract.java b/evm/src/main/java/org/hyperledger/besu/evm/precompile/AltBN128AddPrecompiledContract.java index 9773e76060..070a26e348 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/precompile/AltBN128AddPrecompiledContract.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/precompile/AltBN128AddPrecompiledContract.java @@ -16,12 +16,14 @@ package org.hyperledger.besu.evm.precompile; import org.hyperledger.besu.crypto.altbn128.AltBn128Point; import org.hyperledger.besu.crypto.altbn128.Fq; +import org.hyperledger.besu.evm.frame.ExceptionalHaltReason; import org.hyperledger.besu.evm.frame.MessageFrame; import org.hyperledger.besu.evm.gascalculator.GasCalculator; import org.hyperledger.besu.nativelib.bls12_381.LibEthPairings; import java.math.BigInteger; import java.util.Arrays; +import java.util.Optional; import javax.annotation.Nonnull; import org.apache.tuweni.bytes.Bytes; @@ -55,8 +57,10 @@ public class AltBN128AddPrecompiledContract extends AbstractAltBnPrecompiledCont return gasCost; } + @Nonnull @Override - public Bytes compute(final Bytes input, @Nonnull final MessageFrame messageFrame) { + public PrecompileContractResult computePrecompile( + final Bytes input, @Nonnull final MessageFrame messageFrame) { if (useNative) { return computeNative(input, messageFrame); } else { @@ -64,7 +68,7 @@ public class AltBN128AddPrecompiledContract extends AbstractAltBnPrecompiledCont } } - private static Bytes computeDefault(final Bytes input) { + private static PrecompileContractResult computeDefault(final Bytes input) { final BigInteger x1 = extractParameter(input, 0, 32); final BigInteger y1 = extractParameter(input, 32, 32); final BigInteger x2 = extractParameter(input, 64, 32); @@ -73,7 +77,8 @@ public class AltBN128AddPrecompiledContract extends AbstractAltBnPrecompiledCont final AltBn128Point p1 = new AltBn128Point(Fq.create(x1), Fq.create(y1)); final AltBn128Point p2 = new AltBn128Point(Fq.create(x2), Fq.create(y2)); if (!p1.isOnCurve() || !p2.isOnCurve()) { - return null; + return PrecompileContractResult.halt( + null, Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR)); } final AltBn128Point sum = p1.add(p2); final Bytes x = sum.getX().toBytes(); @@ -82,7 +87,7 @@ public class AltBN128AddPrecompiledContract extends AbstractAltBnPrecompiledCont x.copyTo(result, 32 - x.size()); y.copyTo(result, 64 - y.size()); - return result.copy(); + return PrecompileContractResult.success(result.copy()); } private static BigInteger extractParameter( diff --git a/evm/src/main/java/org/hyperledger/besu/evm/precompile/AltBN128MulPrecompiledContract.java b/evm/src/main/java/org/hyperledger/besu/evm/precompile/AltBN128MulPrecompiledContract.java index ff04625bcc..c3adf0d120 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/precompile/AltBN128MulPrecompiledContract.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/precompile/AltBN128MulPrecompiledContract.java @@ -16,12 +16,14 @@ package org.hyperledger.besu.evm.precompile; import org.hyperledger.besu.crypto.altbn128.AltBn128Point; import org.hyperledger.besu.crypto.altbn128.Fq; +import org.hyperledger.besu.evm.frame.ExceptionalHaltReason; import org.hyperledger.besu.evm.frame.MessageFrame; import org.hyperledger.besu.evm.gascalculator.GasCalculator; import org.hyperledger.besu.nativelib.bls12_381.LibEthPairings; import java.math.BigInteger; import java.util.Arrays; +import java.util.Optional; import javax.annotation.Nonnull; import org.apache.tuweni.bytes.Bytes; @@ -59,8 +61,10 @@ public class AltBN128MulPrecompiledContract extends AbstractAltBnPrecompiledCont return gasCost; } + @Nonnull @Override - public Bytes compute(final Bytes input, @Nonnull final MessageFrame messageFrame) { + public PrecompileContractResult computePrecompile( + final Bytes input, @Nonnull final MessageFrame messageFrame) { if (useNative) { return computeNative(input, messageFrame); } else { @@ -68,14 +72,16 @@ public class AltBN128MulPrecompiledContract extends AbstractAltBnPrecompiledCont } } - private static Bytes computeDefault(final Bytes input) { + @Nonnull + private static PrecompileContractResult computeDefault(final Bytes input) { final BigInteger x = extractParameter(input, 0, 32); final BigInteger y = extractParameter(input, 32, 32); final BigInteger n = extractParameter(input, 64, 32); final AltBn128Point p = new AltBn128Point(Fq.create(x), Fq.create(y)); if (!p.isOnCurve() || n.compareTo(MAX_N) > 0) { - return null; + return PrecompileContractResult.halt( + null, Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR)); } final AltBn128Point product = p.multiply(n); @@ -85,7 +91,7 @@ public class AltBN128MulPrecompiledContract extends AbstractAltBnPrecompiledCont xResult.copyTo(result, 32 - xResult.size()); yResult.copyTo(result, 64 - yResult.size()); - return result; + return PrecompileContractResult.success(result); } private static BigInteger extractParameter( diff --git a/evm/src/main/java/org/hyperledger/besu/evm/precompile/AltBN128PairingPrecompiledContract.java b/evm/src/main/java/org/hyperledger/besu/evm/precompile/AltBN128PairingPrecompiledContract.java index 32a0ed4492..a8afb74c8a 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/precompile/AltBN128PairingPrecompiledContract.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/precompile/AltBN128PairingPrecompiledContract.java @@ -20,6 +20,7 @@ import org.hyperledger.besu.crypto.altbn128.AltBn128Point; import org.hyperledger.besu.crypto.altbn128.Fq; import org.hyperledger.besu.crypto.altbn128.Fq12; import org.hyperledger.besu.crypto.altbn128.Fq2; +import org.hyperledger.besu.evm.frame.ExceptionalHaltReason; import org.hyperledger.besu.evm.frame.MessageFrame; import org.hyperledger.besu.evm.gascalculator.GasCalculator; import org.hyperledger.besu.nativelib.bls12_381.LibEthPairings; @@ -28,6 +29,7 @@ import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Optional; import javax.annotation.Nonnull; import org.apache.tuweni.bytes.Bytes; @@ -70,13 +72,16 @@ public class AltBN128PairingPrecompiledContract extends AbstractAltBnPrecompiled return (pairingGasCost * parameters) + baseGasCost; } + @Nonnull @Override - public Bytes compute(final Bytes input, @Nonnull final MessageFrame messageFrame) { + public PrecompileContractResult computePrecompile( + final Bytes input, @Nonnull final MessageFrame messageFrame) { if (input.isEmpty()) { - return TRUE; + return PrecompileContractResult.success(TRUE); } if (input.size() % PARAMETER_LENGTH != 0) { - return null; + return PrecompileContractResult.halt( + null, Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR)); } if (useNative) { return computeNative(input, messageFrame); @@ -85,7 +90,8 @@ public class AltBN128PairingPrecompiledContract extends AbstractAltBnPrecompiled } } - private static Bytes computeDefault(final Bytes input) { + @Nonnull + private static PrecompileContractResult computeDefault(final Bytes input) { final int parameters = input.size() / PARAMETER_LENGTH; final List a = new ArrayList<>(); final List b = new ArrayList<>(); @@ -94,7 +100,8 @@ public class AltBN128PairingPrecompiledContract extends AbstractAltBnPrecompiled final BigInteger p1_y = extractParameter(input, i * PARAMETER_LENGTH + 32, FIELD_LENGTH); final AltBn128Point p1 = new AltBn128Point(Fq.create(p1_x), Fq.create(p1_y)); if (!p1.isOnCurve()) { - return null; + return PrecompileContractResult.halt( + null, Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR)); } a.add(p1); @@ -106,7 +113,8 @@ public class AltBN128PairingPrecompiledContract extends AbstractAltBnPrecompiled final Fq2 p2_y = Fq2.create(p2_yReal, p2_yImag); final AltBn128Fq2Point p2 = new AltBn128Fq2Point(p2_x, p2_y); if (!p2.isOnCurve() || !p2.isInGroup()) { - return null; + return PrecompileContractResult.halt( + null, Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR)); } b.add(p2); } @@ -117,9 +125,9 @@ public class AltBN128PairingPrecompiledContract extends AbstractAltBnPrecompiled } if (AltBn128Fq12Pairer.finalize(exponent).equals(Fq12.one())) { - return TRUE; + return PrecompileContractResult.success(TRUE); } else { - return FALSE; + return PrecompileContractResult.success(FALSE); } } diff --git a/evm/src/main/java/org/hyperledger/besu/evm/precompile/BLAKE2BFPrecompileContract.java b/evm/src/main/java/org/hyperledger/besu/evm/precompile/BLAKE2BFPrecompileContract.java index 5c4130a095..56db6eb947 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/precompile/BLAKE2BFPrecompileContract.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/precompile/BLAKE2BFPrecompileContract.java @@ -18,10 +18,12 @@ import static java.util.Arrays.copyOfRange; import static org.hyperledger.besu.crypto.Blake2bfMessageDigest.Blake2bfDigest.MESSAGE_LENGTH_BYTES; import org.hyperledger.besu.crypto.Hash; +import org.hyperledger.besu.evm.frame.ExceptionalHaltReason; import org.hyperledger.besu.evm.frame.MessageFrame; import org.hyperledger.besu.evm.gascalculator.GasCalculator; import java.math.BigInteger; +import java.util.Optional; import javax.annotation.Nonnull; import org.apache.tuweni.bytes.Bytes; @@ -54,17 +56,21 @@ public class BLAKE2BFPrecompileContract extends AbstractPrecompiledContract { return rounds.longValueExact(); } + @Nonnull @Override - public Bytes compute(final Bytes input, @Nonnull final MessageFrame messageFrame) { + public PrecompileContractResult computePrecompile( + final Bytes input, @Nonnull final MessageFrame messageFrame) { if (input.size() != MESSAGE_LENGTH_BYTES) { LOG.trace( "Incorrect input length. Expected {} and got {}", MESSAGE_LENGTH_BYTES, input.size()); - return null; + return PrecompileContractResult.halt( + null, Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR)); } if ((input.get(212) & 0xFE) != 0) { LOG.trace("Incorrect finalization flag, expected 0 or 1 and got {}", input.get(212)); - return null; + return PrecompileContractResult.halt( + null, Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR)); } - return Hash.blake2bf(input); + return PrecompileContractResult.success(Hash.blake2bf(input)); } } diff --git a/evm/src/main/java/org/hyperledger/besu/evm/precompile/BigIntegerModularExponentiationPrecompiledContract.java b/evm/src/main/java/org/hyperledger/besu/evm/precompile/BigIntegerModularExponentiationPrecompiledContract.java index f7ec63a0f7..6d00c926a5 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/precompile/BigIntegerModularExponentiationPrecompiledContract.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/precompile/BigIntegerModularExponentiationPrecompiledContract.java @@ -23,6 +23,7 @@ import javax.annotation.Nonnull; import org.apache.tuweni.bytes.Bytes; import org.apache.tuweni.bytes.MutableBytes; +import org.jetbrains.annotations.NotNull; // The big integer modular exponentiation precompiled contract defined in EIP-198. public class BigIntegerModularExponentiationPrecompiledContract @@ -52,8 +53,10 @@ public class BigIntegerModularExponentiationPrecompiledContract return gasCalculator().modExpGasCost(input); } + @NotNull @Override - public Bytes compute(final Bytes input, @Nonnull final MessageFrame messageFrame) { + public PrecompileContractResult computePrecompile( + final Bytes input, @Nonnull final MessageFrame messageFrame) { final BigInteger baseLength = baseLength(input); final BigInteger exponentLength = exponentLength(input); final BigInteger modulusLength = modulusLength(input); @@ -61,7 +64,7 @@ public class BigIntegerModularExponentiationPrecompiledContract // we could have a massively overflowing exp because it wouldn't have been filtered out at the // gas cost phase if (baseLength.equals(BigInteger.ZERO) && modulusLength.equals(BigInteger.ZERO)) { - return Bytes.EMPTY; + return PrecompileContractResult.success(Bytes.EMPTY); } final BigInteger exponentOffset = BASE_OFFSET.add(baseLength); final BigInteger modulusOffset = exponentOffset.add(exponentLength); @@ -81,7 +84,7 @@ public class BigIntegerModularExponentiationPrecompiledContract } modExp.copyTo(result, result.size() - modExp.size()); - return result; + return PrecompileContractResult.success(result); } // Equation to estimate the multiplication complexity. diff --git a/evm/src/main/java/org/hyperledger/besu/evm/precompile/ECRECPrecompiledContract.java b/evm/src/main/java/org/hyperledger/besu/evm/precompile/ECRECPrecompiledContract.java index 488f61e9df..86f37ae15d 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/precompile/ECRECPrecompiledContract.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/precompile/ECRECPrecompiledContract.java @@ -24,12 +24,12 @@ import org.hyperledger.besu.evm.gascalculator.GasCalculator; import java.math.BigInteger; import java.util.Optional; +import javax.annotation.Nonnull; import org.apache.tuweni.bytes.Bytes; import org.apache.tuweni.bytes.Bytes32; import org.apache.tuweni.bytes.MutableBytes; import org.apache.tuweni.bytes.MutableBytes32; -import org.jetbrains.annotations.NotNull; public class ECRECPrecompiledContract extends AbstractPrecompiledContract { @@ -44,8 +44,10 @@ public class ECRECPrecompiledContract extends AbstractPrecompiledContract { return gasCalculator().getEcrecPrecompiledContractGasCost(); } + @Nonnull @Override - public Bytes compute(final Bytes input, @NotNull final MessageFrame messageFrame) { + public PrecompileContractResult computePrecompile( + final Bytes input, @Nonnull final MessageFrame messageFrame) { final int size = input.size(); final Bytes d = size >= 128 ? input : Bytes.wrap(input, MutableBytes.create(128 - size)); final Bytes32 h = Bytes32.wrap(d, 0); @@ -54,7 +56,7 @@ public class ECRECPrecompiledContract extends AbstractPrecompiledContract { // it is simply the last byte of those 32 bytes that needs to be used. It does appear we need // to check the rest of the bytes are zero though. if (!d.slice(32, 31).isZero()) { - return Bytes.EMPTY; + return PrecompileContractResult.success(Bytes.EMPTY); } final int recId = d.get(63) - V_BASE; @@ -66,7 +68,7 @@ public class ECRECPrecompiledContract extends AbstractPrecompiledContract { try { signature = signatureAlgorithm.createSignature(r, s, (byte) recId); } catch (final IllegalArgumentException e) { - return Bytes.EMPTY; + return PrecompileContractResult.success(Bytes.EMPTY); } // SECP256K1#PublicKey#recoverFromSignature throws an Illegal argument exception @@ -77,15 +79,15 @@ public class ECRECPrecompiledContract extends AbstractPrecompiledContract { final Optional recovered = signatureAlgorithm.recoverPublicKeyFromSignature(h, signature); if (recovered.isEmpty()) { - return Bytes.EMPTY; + return PrecompileContractResult.success(Bytes.EMPTY); } final Bytes32 hashed = Hash.keccak256(recovered.get().getEncodedBytes()); final MutableBytes32 result = MutableBytes32.create(); hashed.slice(12).copyTo(result, 12); - return result; + return PrecompileContractResult.success(result); } catch (final IllegalArgumentException e) { - return Bytes.EMPTY; + return PrecompileContractResult.success(Bytes.EMPTY); } } } diff --git a/evm/src/main/java/org/hyperledger/besu/evm/precompile/IDPrecompiledContract.java b/evm/src/main/java/org/hyperledger/besu/evm/precompile/IDPrecompiledContract.java index d7ecf160fe..6d672eb3e1 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/precompile/IDPrecompiledContract.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/precompile/IDPrecompiledContract.java @@ -32,8 +32,10 @@ public class IDPrecompiledContract extends AbstractPrecompiledContract { return gasCalculator().idPrecompiledContractGasCost(input); } + @Nonnull @Override - public Bytes compute(final Bytes input, @Nonnull final MessageFrame messageFrame) { - return input.copy(); + public PrecompileContractResult computePrecompile( + final Bytes input, @Nonnull final MessageFrame messageFrame) { + return PrecompileContractResult.success(input.copy()); } } diff --git a/evm/src/main/java/org/hyperledger/besu/evm/precompile/PrecompiledContract.java b/evm/src/main/java/org/hyperledger/besu/evm/precompile/PrecompiledContract.java index 7d6162e790..c687a04459 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/precompile/PrecompiledContract.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/precompile/PrecompiledContract.java @@ -54,6 +54,7 @@ public interface PrecompiledContract { * @return the output of the pre-compiled contract. */ @SuppressWarnings("deprecation") + @Nonnull default PrecompileContractResult computePrecompile( final Bytes input, @Nonnull final MessageFrame messageFrame) { final Bytes result = compute(input, messageFrame); @@ -86,7 +87,7 @@ public interface PrecompiledContract { /** * Encapsulated result of precompiled contract. * - * @param output output if successfull + * @param output output if successful * @param refundGas Should we charge the gasRequirement? * @param state state of the EVM after execution (for format errors this would be * ExceptionalHalt) diff --git a/evm/src/main/java/org/hyperledger/besu/evm/precompile/RIPEMD160PrecompiledContract.java b/evm/src/main/java/org/hyperledger/besu/evm/precompile/RIPEMD160PrecompiledContract.java index 6d061f4f2a..e0fb1b9156 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/precompile/RIPEMD160PrecompiledContract.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/precompile/RIPEMD160PrecompiledContract.java @@ -34,8 +34,10 @@ public class RIPEMD160PrecompiledContract extends AbstractPrecompiledContract { return gasCalculator().ripemd160PrecompiledContractGasCost(input); } + @Nonnull @Override - public Bytes compute(final Bytes input, @Nonnull final MessageFrame messageFrame) { - return Bytes32.leftPad(Hash.ripemd160(input)); + public PrecompileContractResult computePrecompile( + final Bytes input, @Nonnull final MessageFrame messageFrame) { + return PrecompileContractResult.success(Bytes32.leftPad(Hash.ripemd160(input))); } } diff --git a/evm/src/main/java/org/hyperledger/besu/evm/precompile/SHA256PrecompiledContract.java b/evm/src/main/java/org/hyperledger/besu/evm/precompile/SHA256PrecompiledContract.java index 20225aee94..384031f239 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/precompile/SHA256PrecompiledContract.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/precompile/SHA256PrecompiledContract.java @@ -21,6 +21,7 @@ import org.hyperledger.besu.evm.gascalculator.GasCalculator; import javax.annotation.Nonnull; import org.apache.tuweni.bytes.Bytes; +import org.jetbrains.annotations.NotNull; public class SHA256PrecompiledContract extends AbstractPrecompiledContract { @@ -33,8 +34,10 @@ public class SHA256PrecompiledContract extends AbstractPrecompiledContract { return gasCalculator().sha256PrecompiledContractGasCost(input); } + @NotNull @Override - public Bytes compute(final Bytes input, @Nonnull final MessageFrame messageFrame) { - return Hash.sha256(input); + public PrecompileContractResult computePrecompile( + final Bytes input, @Nonnull final MessageFrame messageFrame) { + return PrecompileContractResult.success(Hash.sha256(input)); } }