Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing gasLimit parameter when its value is > Long.MAX_VALUE #7116

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
Loading