From 95b3b6c609db524fe3815d3048d15e0e27ea4f09 Mon Sep 17 00:00:00 2001 From: Justin Florentine Date: Sun, 15 Jan 2023 12:52:17 -0500 Subject: [PATCH] adds precompile error handling Signed-off-by: Justin Florentine --- .../KZGPointEvalPrecompiledContract.java | 29 ++++++++++++++----- .../KZGPointEvalPrecompileContractTest.java | 3 ++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/evm/src/main/java/org/hyperledger/besu/evm/precompile/KZGPointEvalPrecompiledContract.java b/evm/src/main/java/org/hyperledger/besu/evm/precompile/KZGPointEvalPrecompiledContract.java index a3502792d33..0a4d093c311 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/precompile/KZGPointEvalPrecompiledContract.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/precompile/KZGPointEvalPrecompiledContract.java @@ -16,6 +16,7 @@ import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; +import org.hyperledger.besu.evm.frame.ExceptionalHaltReason; import org.hyperledger.besu.evm.frame.MessageFrame; import java.io.BufferedReader; @@ -104,7 +105,10 @@ public PrecompileContractResult computePrecompile( if (input.size() != 192) { return new PrecompileContractResult( - Bytes.EMPTY, false, MessageFrame.State.COMPLETED_FAILED, Optional.empty()); + Bytes.EMPTY, + false, + MessageFrame.State.COMPLETED_FAILED, + Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR)); } // Bytes versionedHash = input.slice(0, 32); Bytes z = input.slice(32, 32); @@ -123,18 +127,27 @@ public PrecompileContractResult computePrecompile( // return Bytes(U256(FIELD_ELEMENTS_PER_BLOB).to_be_bytes32() + // U256(BLS_MODULUS).to_be_bytes32()) - result = - new PrecompileContractResult( - output, - false, - proved ? MessageFrame.State.COMPLETED_SUCCESS : MessageFrame.State.COMPLETED_FAILED, - Optional.empty()); + if (proved) { + result = + new PrecompileContractResult( + output, false, MessageFrame.State.COMPLETED_SUCCESS, Optional.empty()); + } else { + result = + new PrecompileContractResult( + output, + false, + MessageFrame.State.COMPLETED_FAILED, + Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR)); + } return result; } catch (RuntimeException kzgFailed) { System.out.println(kzgFailed.getMessage()); result = new PrecompileContractResult( - output, false, MessageFrame.State.COMPLETED_FAILED, Optional.empty()); + output, + false, + MessageFrame.State.COMPLETED_FAILED, + Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR)); } return result; } diff --git a/evm/src/test/java/org/hyperledger/besu/evm/precompile/KZGPointEvalPrecompileContractTest.java b/evm/src/test/java/org/hyperledger/besu/evm/precompile/KZGPointEvalPrecompileContractTest.java index d053bc8e1c2..21cfeb61c43 100644 --- a/evm/src/test/java/org/hyperledger/besu/evm/precompile/KZGPointEvalPrecompileContractTest.java +++ b/evm/src/test/java/org/hyperledger/besu/evm/precompile/KZGPointEvalPrecompileContractTest.java @@ -18,6 +18,7 @@ import static org.assertj.core.api.Assertions.fail; import static org.mockito.Mockito.mock; +import org.hyperledger.besu.evm.frame.ExceptionalHaltReason; import org.hyperledger.besu.evm.frame.MessageFrame; import java.io.IOException; @@ -74,6 +75,8 @@ public void sadPaths() { contract.computePrecompile(input, toRun); MessageFrame.State endState = result.getState(); assertThat(endState).isEqualTo(MessageFrame.State.COMPLETED_FAILED); + assertThat(result.getHaltReason()).isPresent(); + assertThat(result.getHaltReason().get()).isEqualTo(ExceptionalHaltReason.PRECOMPILE_ERROR); } } catch (IOException ioe) { fail("couldn't load test vectors", ioe);