From d821ac1ee6766514489cd05a054f8a69663c5773 Mon Sep 17 00:00:00 2001 From: Fabio Di Fabio Date: Thu, 16 May 2024 14:26:30 +0300 Subject: [PATCH] Fix parsing gasLimit parameter when its value is > Long.MAX_VALUE Signed-off-by: Fabio Di Fabio --- CHANGELOG.md | 2 +- .../parameters/JsonCallParameter.java | 4 +- .../parameters/JsonCallParameterTest.java | 40 +++++++++++++++++++ ...Deserializer.java => GasDeserializer.java} | 18 ++++++--- 4 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/parameters/JsonCallParameterTest.java rename ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/json/{HexLongDeserializer.java => GasDeserializer.java} (61%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 29a4611989f..c5348456dd0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ - Disconnect peers that have multiple discovery ports since they give us bad neighbours [#7089](https://github.com/hyperledger/besu/pull/7089) ### Bug fixes - +- Fix parsing `gasLimit` parameter when its value is > `Long.MAX_VALUE` [#7116](https://github.com/hyperledger/besu/pull/7116) ## 24.5.1 diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/parameters/JsonCallParameter.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/parameters/JsonCallParameter.java index b71083a4c38..b084ebc9135 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/parameters/JsonCallParameter.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/parameters/JsonCallParameter.java @@ -18,7 +18,7 @@ import org.hyperledger.besu.datatypes.Address; import org.hyperledger.besu.datatypes.VersionedHash; import org.hyperledger.besu.datatypes.Wei; -import org.hyperledger.besu.ethereum.core.json.HexLongDeserializer; +import org.hyperledger.besu.ethereum.core.json.GasDeserializer; import org.hyperledger.besu.ethereum.core.json.HexStringDeserializer; import org.hyperledger.besu.ethereum.transaction.CallParameter; @@ -45,7 +45,7 @@ public class JsonCallParameter extends CallParameter { public JsonCallParameter( @JsonProperty("from") final Address from, @JsonProperty("to") final Address to, - @JsonDeserialize(using = HexLongDeserializer.class) @JsonProperty("gas") final Long gasLimit, + @JsonDeserialize(using = GasDeserializer.class) @JsonProperty("gas") final Long gasLimit, @JsonProperty("gasPrice") final Wei gasPrice, @JsonProperty("maxPriorityFeePerGas") final Wei maxPriorityFeePerGas, @JsonProperty("maxFeePerGas") final Wei maxFeePerGas, diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/parameters/JsonCallParameterTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/parameters/JsonCallParameterTest.java new file mode 100644 index 00000000000..b74c68f97df --- /dev/null +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/parameters/JsonCallParameterTest.java @@ -0,0 +1,40 @@ +/* + * Copyright contributors to Hyperledger Besu. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Test; + +public class JsonCallParameterTest { + + private final ObjectMapper objectMapper = new ObjectMapper(); + + @Test + public void acceptsAndCapMaxValueForGasLimit() throws JsonProcessingException { + final String json = + """ + { + "gas": "0xffffffffffffffff" + } + """; + + final JsonCallParameter callParameter = objectMapper.readValue(json, JsonCallParameter.class); + + assertThat(callParameter.getGasLimit()).isEqualTo(Long.MAX_VALUE); + } +} diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/json/HexLongDeserializer.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/json/GasDeserializer.java similarity index 61% rename from ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/json/HexLongDeserializer.java rename to ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/json/GasDeserializer.java index 79d81905059..dd0b8a3f6ca 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/json/HexLongDeserializer.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/json/GasDeserializer.java @@ -1,5 +1,5 @@ /* - * Copyright ConsenSys AG. + * Copyright contributors to Hyperledger Besu. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at @@ -19,19 +19,27 @@ import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.deser.std.StdDeserializer; +import org.apache.tuweni.units.bigints.UInt64; +import org.apache.tuweni.units.bigints.UInt64s; -public class HexLongDeserializer extends StdDeserializer { - public HexLongDeserializer() { +public class GasDeserializer extends StdDeserializer { + private static final UInt64 GAS_MAX_VALUE = UInt64.valueOf(Long.MAX_VALUE); + + public GasDeserializer() { this(null); } - public HexLongDeserializer(final Class vc) { + public GasDeserializer(final Class vc) { super(vc); } @Override public Long deserialize(final JsonParser jsonparser, final DeserializationContext context) throws IOException { - return Long.decode(jsonparser.getCodec().readValue(jsonparser, String.class)); + final var uint64 = + UInt64.fromHexString(jsonparser.getCodec().readValue(jsonparser, String.class)); + // we can safely cap the value to Long.MAX_VALUE since gas is not expected to reach these value + // anytime soon if ever + return UInt64s.min(uint64, GAS_MAX_VALUE).toLong(); } }