Skip to content

Commit

Permalink
Refactor OperationResult for PrecompiledContracts - no need for outpu…
Browse files Browse the repository at this point in the history
…t in halt cases

Signed-off-by: Luis Pinto <[email protected]>
  • Loading branch information
lu-pinto committed Nov 27, 2024
1 parent e84e6d6 commit 06547a5
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,7 @@ public PrecompileContractResult computePrecompile(
pmtHash, privacyGroupId, disposablePrivateState, privateMetadataUpdater, result);
}

return new PrecompileContractResult(
result.getOutput(), true, MessageFrame.State.CODE_EXECUTING, Optional.empty());
return PrecompileContractResult.executing(result.getOutput());
}

private void sendParticipantRemovedEvent(final PrivateTransaction privateTransaction) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ public PrecompileContractResult computePrecompile(
pmtHash, privacyGroupId, disposablePrivateState, privateMetadataUpdater, result);
}

return new PrecompileContractResult(
result.getOutput(), true, MessageFrame.State.CODE_EXECUTING, Optional.empty());
return PrecompileContractResult.executing(result.getOutput());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import org.hyperledger.besu.evm.worldstate.WorldUpdater;

import java.util.Base64;
import java.util.Optional;
import javax.annotation.Nonnull;

import org.apache.tuweni.bytes.Bytes;
Expand All @@ -68,9 +67,7 @@ public class PrivacyPrecompiledContract extends AbstractPrecompiledContract {

private static final Logger LOG = LoggerFactory.getLogger(PrivacyPrecompiledContract.class);

static final PrecompileContractResult NO_RESULT =
new PrecompileContractResult(
Bytes.EMPTY, true, MessageFrame.State.CODE_EXECUTING, Optional.empty());
static final PrecompileContractResult NO_RESULT = PrecompileContractResult.executing(Bytes.EMPTY);

public PrivacyPrecompiledContract(
final GasCalculator gasCalculator,
Expand Down Expand Up @@ -219,8 +216,7 @@ public PrecompileContractResult computePrecompile(
pmtHash, privacyGroupId, disposablePrivateState, privateMetadataUpdater, result);
}

return new PrecompileContractResult(
result.getOutput(), true, MessageFrame.State.CODE_EXECUTING, Optional.empty());
return PrecompileContractResult.executing(result.getOutput());
}

protected void maybeApplyGenesisToPrivateWorldState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.hyperledger.besu.nativelib.gnark.LibGnarkEIP196;

import java.util.Optional;
import javax.annotation.Nonnull;

import com.sun.jna.ptr.IntByReference;
Expand Down Expand Up @@ -130,8 +129,7 @@ public PrecompileContractResult computeNative(
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 PrecompileContractResult.halt(
null, Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR));
return PrecompileContractResult.halt(ExceptionalHaltReason.PRECOMPILE_ERROR);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.nativelib.gnark.LibGnarkEIP2537;

import java.util.Optional;
import javax.annotation.Nonnull;

import com.sun.jna.ptr.IntByReference;
Expand Down Expand Up @@ -115,8 +114,7 @@ public PrecompileContractResult computePrecompile(
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 PrecompileContractResult.halt(
null, Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR));
return PrecompileContractResult.halt(ExceptionalHaltReason.PRECOMPILE_ERROR);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Optional;
import javax.annotation.Nonnull;

import org.apache.tuweni.bytes.Bytes;
Expand Down Expand Up @@ -90,8 +89,7 @@ private static PrecompileContractResult computeDefault(final Bytes input) {
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 PrecompileContractResult.halt(
null, Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR));
return PrecompileContractResult.halt(ExceptionalHaltReason.PRECOMPILE_ERROR);
}
final AltBn128Point sum = p1.add(p2);
final Bytes x = sum.getX().toBytes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Optional;
import javax.annotation.Nonnull;

import org.apache.tuweni.bytes.Bytes;
Expand Down Expand Up @@ -81,8 +80,7 @@ public PrecompileContractResult computePrecompile(
final Bytes input, @Nonnull final MessageFrame messageFrame) {

if (input.size() >= 64 && input.slice(0, 64).equals(POINT_AT_INFINITY)) {
return new PrecompileContractResult(
POINT_AT_INFINITY, false, MessageFrame.State.COMPLETED_SUCCESS, Optional.empty());
return PrecompileContractResult.success(POINT_AT_INFINITY);
}

if (useNative) {
Expand All @@ -100,8 +98,7 @@ private static PrecompileContractResult computeDefault(final Bytes input) {

final AltBn128Point p = new AltBn128Point(Fq.create(x), Fq.create(y));
if (!p.isOnCurve() || n.compareTo(MAX_N) > 0) {
return PrecompileContractResult.halt(
null, Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR));
return PrecompileContractResult.halt(ExceptionalHaltReason.PRECOMPILE_ERROR);
}
final AltBn128Point product = p.multiply(n);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
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;
Expand Down Expand Up @@ -96,8 +95,7 @@ public PrecompileContractResult computePrecompile(
return PrecompileContractResult.success(TRUE);
}
if (input.size() % PARAMETER_LENGTH != 0) {
return PrecompileContractResult.halt(
null, Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR));
return PrecompileContractResult.halt(ExceptionalHaltReason.PRECOMPILE_ERROR);
}
if (useNative) {
return computeNative(input, messageFrame);
Expand All @@ -116,8 +114,7 @@ private static PrecompileContractResult computeDefault(final Bytes input) {
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 PrecompileContractResult.halt(
null, Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR));
return PrecompileContractResult.halt(ExceptionalHaltReason.PRECOMPILE_ERROR);
}
a.add(p1);

Expand All @@ -129,8 +126,7 @@ private static PrecompileContractResult computeDefault(final Bytes input) {
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 PrecompileContractResult.halt(
null, Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR));
return PrecompileContractResult.halt(ExceptionalHaltReason.PRECOMPILE_ERROR);
}
b.add(p2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
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;
Expand Down Expand Up @@ -69,13 +68,11 @@ public PrecompileContractResult computePrecompile(
if (input.size() != MESSAGE_LENGTH_BYTES) {
LOG.trace(
"Incorrect input length. Expected {} and got {}", MESSAGE_LENGTH_BYTES, input.size());
return PrecompileContractResult.halt(
null, Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR));
return PrecompileContractResult.halt(ExceptionalHaltReason.PRECOMPILE_ERROR);
}
if ((input.get(212) & 0xFE) != 0) {
LOG.trace("Incorrect finalization flag, expected 0 or 1 and got {}", input.get(212));
return PrecompileContractResult.halt(
null, Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR));
return PrecompileContractResult.halt(ExceptionalHaltReason.PRECOMPILE_ERROR);
}
return PrecompileContractResult.success(Hash.blake2bf(input));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.hyperledger.besu.nativelib.arithmetic.LibArithmetic;

import java.math.BigInteger;
import java.util.Optional;
import javax.annotation.Nonnull;

import com.sun.jna.ptr.IntByReference;
Expand Down Expand Up @@ -258,8 +257,7 @@ public PrecompileContractResult computeNative(final @Nonnull Bytes input) {
return PrecompileContractResult.success(Bytes.wrap(result, 0, o_len.getValue()));
} else {
LOG.trace("Error executing precompiled contract {}: {}", getName(), errorNo);
return PrecompileContractResult.halt(
null, Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR));
return PrecompileContractResult.halt(ExceptionalHaltReason.PRECOMPILE_ERROR);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.hyperledger.besu.evm.internal.Words;

import java.nio.file.Path;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.annotation.Nonnull;

Expand Down Expand Up @@ -109,23 +108,20 @@ public PrecompileContractResult computePrecompile(
final Bytes input, @Nonnull final MessageFrame messageFrame) {

if (input.size() != 192) {
return PrecompileContractResult.halt(
null, Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR));
return PrecompileContractResult.halt(ExceptionalHaltReason.PRECOMPILE_ERROR);
}
Bytes32 versionedHash = Bytes32.wrap(input.slice(0, 32));
Bytes z = input.slice(32, 32);
Bytes y = input.slice(64, 32);
Bytes commitment = input.slice(96, 48);
Bytes proof = input.slice(144, 48);
if (versionedHash.get(0) != 0x01) { // unsupported hash version
return PrecompileContractResult.halt(
null, Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR));
return PrecompileContractResult.halt(ExceptionalHaltReason.PRECOMPILE_ERROR);
} else {
byte[] hash = Hash.sha256(commitment).toArrayUnsafe();
hash[0] = 0x01;
if (!versionedHash.equals(Bytes32.wrap(hash))) {
return PrecompileContractResult.halt(
null, Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR));
return PrecompileContractResult.halt(ExceptionalHaltReason.PRECOMPILE_ERROR);
}
}
try {
Expand All @@ -136,14 +132,12 @@ public PrecompileContractResult computePrecompile(
if (proved) {
return PrecompileContractResult.success(successResult);
} else {
return PrecompileContractResult.halt(
null, Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR));
return PrecompileContractResult.halt(ExceptionalHaltReason.PRECOMPILE_ERROR);
}
} catch (RuntimeException kzgFailed) {
LOG.debug("Native KZG failed", kzgFailed);

return PrecompileContractResult.halt(
null, Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR));
return PrecompileContractResult.halt(ExceptionalHaltReason.PRECOMPILE_ERROR);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package org.hyperledger.besu.evm.precompile;

import static com.google.common.base.Preconditions.checkNotNull;

import org.hyperledger.besu.evm.frame.ExceptionalHaltReason;
import org.hyperledger.besu.evm.frame.MessageFrame;

Expand Down Expand Up @@ -59,7 +61,7 @@ default PrecompileContractResult computePrecompile(
final Bytes input, @Nonnull final MessageFrame messageFrame) {
final Bytes result = compute(input, messageFrame);
if (result == null) {
return PrecompileContractResult.halt(null, Optional.of(ExceptionalHaltReason.NONE));
return PrecompileContractResult.halt(ExceptionalHaltReason.NONE);
} else {
return PrecompileContractResult.success(result);
}
Expand Down Expand Up @@ -94,8 +96,7 @@ class PrecompileContractResult {
* ExceptionalHalt)
* @param haltReason the exceptional halt reason
*/
// TOO JDK17 use a record
public PrecompileContractResult(
private PrecompileContractResult(
final Bytes output,
final boolean refundGas,
final MessageFrame.State state,
Expand All @@ -117,6 +118,17 @@ public static PrecompileContractResult success(final Bytes output) {
output, false, MessageFrame.State.COMPLETED_SUCCESS, Optional.empty());
}

/**
* precompile contract result with code executing state.
*
* @param output the output
* @return the precompile contract result
*/
public static PrecompileContractResult executing(final Bytes output) {
return new PrecompileContractResult(
output, true, MessageFrame.State.CODE_EXECUTING, Optional.empty());
}

/**
* precompile contract result with revert state.
*
Expand All @@ -131,17 +143,13 @@ public static PrecompileContractResult revert(final Bytes output) {
/**
* precompile contract result with Halt state.
*
* @param output the output
* @param haltReason the halt reason
* @return the precompile contract result
*/
public static PrecompileContractResult halt(
final Bytes output, final Optional<ExceptionalHaltReason> haltReason) {
if (haltReason.isEmpty()) {
throw new IllegalArgumentException("Halt reason cannot be empty");
}
public static PrecompileContractResult halt(final ExceptionalHaltReason haltReason) {
checkNotNull(haltReason, "haltReason is null");
return new PrecompileContractResult(
output, false, MessageFrame.State.EXCEPTIONAL_HALT, haltReason);
null, false, MessageFrame.State.EXCEPTIONAL_HALT, Optional.of(haltReason));
}

/**
Expand Down

0 comments on commit 06547a5

Please sign in to comment.