Skip to content

Commit 7acdd87

Browse files
setMinPriorityFee - Return Invalid Param when invalid and use hexadecimal instead of Long (#6099)
Signed-off-by: Gabriel-Trintinalia <gabriel.trintinalia@consensys.net>
1 parent 094c841 commit 7acdd87

File tree

4 files changed

+51
-18
lines changed

4 files changed

+51
-18
lines changed

ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/miner/MinerGetMinPriorityFee.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
2020
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
2121
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
22+
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.Quantity;
2223
import org.hyperledger.besu.ethereum.core.MiningParameters;
2324

2425
public class MinerGetMinPriorityFee implements JsonRpcMethod {
@@ -36,6 +37,7 @@ public String getName() {
3637
@Override
3738
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
3839
return new JsonRpcSuccessResponse(
39-
requestContext.getRequest().getId(), miningParameters.getMinPriorityFeePerGas().getValue());
40+
requestContext.getRequest().getId(),
41+
Quantity.create(miningParameters.getMinPriorityFeePerGas()));
4042
}
4143
}

ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/miner/MinerSetMinPriorityFee.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
1919
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
2020
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
21+
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError;
22+
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
2123
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
2224
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
25+
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
2326
import org.hyperledger.besu.ethereum.core.MiningParameters;
2427

2528
import org.slf4j.Logger;
@@ -42,12 +45,16 @@ public String getName() {
4245
@Override
4346
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
4447
try {
45-
final Wei minPriorityFeePerGas = Wei.of(requestContext.getRequiredParameter(0, Long.class));
48+
final Wei minPriorityFeePerGas =
49+
Wei.fromHexString(requestContext.getRequiredParameter(0, String.class));
4650
miningParameters.setMinPriorityFeePerGas(minPriorityFeePerGas);
47-
LOG.debug("min priority fee per gas changed to {}", minPriorityFeePerGas);
51+
LOG.debug(
52+
"min priority fee per gas changed to {}", minPriorityFeePerGas.toHumanReadableString());
4853
return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), true);
4954
} catch (final IllegalArgumentException invalidJsonRpcParameters) {
50-
return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), false);
55+
return new JsonRpcErrorResponse(
56+
requestContext.getRequest().getId(),
57+
new JsonRpcError(RpcErrorType.INVALID_PARAMS, invalidJsonRpcParameters.getMessage()));
5158
}
5259
}
5360
}

ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/miner/MinerGetMinPriorityFeeTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@ public void setUp() {
3939

4040
@Test
4141
public void shouldReturnMinPriorityFee() {
42-
Wei minPriorityFee = Wei.of(70);
42+
String minPriorityFee = "0x46";
43+
4344
final JsonRpcRequestContext request =
4445
new JsonRpcRequestContext(new JsonRpcRequest("2.0", method.getName(), new Object[] {}));
4546

46-
when(miningParameters.getMinPriorityFeePerGas()).thenReturn(minPriorityFee);
47+
when(miningParameters.getMinPriorityFeePerGas()).thenReturn(Wei.fromHexString(minPriorityFee));
4748

4849
final JsonRpcResponse expected =
49-
new JsonRpcSuccessResponse(request.getRequest().getId(), minPriorityFee.getValue());
50+
new JsonRpcSuccessResponse(request.getRequest().getId(), minPriorityFee);
5051

5152
final JsonRpcResponse actual = method.response(request);
5253
assertThat(actual).usingRecursiveComparison().isEqualTo(expected);

ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/miner/MinerSetMinPriorityFeeTest.java

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919
import org.hyperledger.besu.datatypes.Wei;
2020
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest;
2121
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
22+
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError;
23+
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
2224
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
2325
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
26+
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
2427
import org.hyperledger.besu.ethereum.core.MiningParameters;
2528

2629
import org.junit.jupiter.api.BeforeEach;
@@ -36,32 +39,52 @@ public void setUp() {
3639
}
3740

3841
@Test
39-
public void shouldReturnFalseWhenParameterIsInvalid() {
40-
final long newMinPriorityFee = -1;
41-
final var request = request(newMinPriorityFee);
42-
42+
public void shouldReturnInvalidParamsWhenParameterIsInvalid() {
43+
final String invalidMinPriorityFee =
44+
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
45+
final var request = request(invalidMinPriorityFee);
4346
method.response(request);
47+
4448
final JsonRpcResponse expected =
45-
new JsonRpcSuccessResponse(request.getRequest().getId(), false);
49+
new JsonRpcErrorResponse(
50+
request.getRequest().getId(),
51+
new JsonRpcError(
52+
RpcErrorType.INVALID_PARAMS,
53+
"Hex value is too large: expected at most 32 bytes but got 33"));
54+
55+
final JsonRpcResponse actual = method.response(request);
56+
assertThat(actual).usingRecursiveComparison().isEqualTo(expected);
57+
}
4658

59+
@Test
60+
public void shouldReturnInvalidParamsWhenParameterIsMissing() {
61+
final var request =
62+
new JsonRpcRequestContext(new JsonRpcRequest("2.0", method.getName(), new Object[] {}));
63+
method.response(request);
64+
final JsonRpcResponse expected =
65+
new JsonRpcErrorResponse(
66+
request.getRequest().getId(),
67+
new JsonRpcError(
68+
RpcErrorType.INVALID_PARAMS, "Missing required json rpc parameter at index 0"));
4769
final JsonRpcResponse actual = method.response(request);
4870
assertThat(actual).usingRecursiveComparison().isEqualTo(expected);
4971
}
5072

5173
@Test
52-
public void shouldChangeMinPriorityFee() {
53-
final long newMinPriorityFee = 10;
74+
public void shouldReturnTrueWhenChangeMinPriorityFee() {
75+
final String newMinPriorityFee = "0x10";
5476
final var request = request(newMinPriorityFee);
5577
method.response(request);
56-
final JsonRpcResponse expected = new JsonRpcSuccessResponse(request.getRequest().getId(), true);
5778

79+
final JsonRpcResponse expected = new JsonRpcSuccessResponse(request.getRequest().getId(), true);
5880
final JsonRpcResponse actual = method.response(request);
5981
assertThat(actual).usingRecursiveComparison().isEqualTo(expected);
60-
assertThat(miningParameters.getMinPriorityFeePerGas()).isEqualTo(Wei.of(newMinPriorityFee));
82+
assertThat(miningParameters.getMinPriorityFeePerGas())
83+
.isEqualTo(Wei.fromHexString(newMinPriorityFee));
6184
}
6285

63-
private JsonRpcRequestContext request(final long longParam) {
86+
private JsonRpcRequestContext request(final String newMinPriorityFee) {
6487
return new JsonRpcRequestContext(
65-
new JsonRpcRequest("2.0", method.getName(), new Object[] {String.valueOf(longParam)}));
88+
new JsonRpcRequest("2.0", method.getName(), new Object[] {newMinPriorityFee}));
6689
}
6790
}

0 commit comments

Comments
 (0)