Skip to content

Commit

Permalink
Fix parsing gasLimit parameter when its value is > Long.MAX_VALUE (hy…
Browse files Browse the repository at this point in the history
…perledger#7116)

Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com>
  • Loading branch information
2 people authored and matthew1001 committed Jun 7, 2024
1 parent a0b4832 commit 9059b76
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<Long> {
public HexLongDeserializer() {
public class GasDeserializer extends StdDeserializer<Long> {
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();
}
}

0 comments on commit 9059b76

Please sign in to comment.