-
Notifications
You must be signed in to change notification settings - Fork 860
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add API to set and get the minGasPrice at runtime (#6097)
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
- Loading branch information
Showing
10 changed files
with
353 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/miner/MinerGetMinGasPrice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright Hyperledger Besu Contributors. | ||
* | ||
* 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.methods.miner; | ||
|
||
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.Quantity; | ||
import org.hyperledger.besu.ethereum.core.MiningParameters; | ||
|
||
public class MinerGetMinGasPrice implements JsonRpcMethod { | ||
private final MiningParameters miningParameters; | ||
|
||
public MinerGetMinGasPrice(final MiningParameters miningParameters) { | ||
this.miningParameters = miningParameters; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return RpcMethod.MINER_GET_MIN_GAS_PRICE.getMethodName(); | ||
} | ||
|
||
@Override | ||
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { | ||
return new JsonRpcSuccessResponse( | ||
requestContext.getRequest().getId(), | ||
Quantity.create(miningParameters.getMinTransactionGasPrice())); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/miner/MinerSetMinGasPrice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright Hyperledger Besu Contributors. | ||
* | ||
* 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.methods.miner; | ||
|
||
import org.hyperledger.besu.datatypes.Wei; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType; | ||
import org.hyperledger.besu.ethereum.core.MiningParameters; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class MinerSetMinGasPrice implements JsonRpcMethod { | ||
private static final Logger LOG = LoggerFactory.getLogger(MinerSetMinGasPrice.class); | ||
|
||
private final MiningParameters miningParameters; | ||
|
||
public MinerSetMinGasPrice(final MiningParameters miningParameters) { | ||
this.miningParameters = miningParameters; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return RpcMethod.MINER_SET_MIN_GAS_PRICE.getMethodName(); | ||
} | ||
|
||
@Override | ||
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { | ||
try { | ||
final Wei minGasPrice = | ||
Wei.fromHexString(requestContext.getRequiredParameter(0, String.class)); | ||
miningParameters.setMinTransactionGasPrice(minGasPrice); | ||
LOG.debug("min gas price changed to {}", minGasPrice.toHumanReadableString()); | ||
return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), true); | ||
} catch (final IllegalArgumentException invalidJsonRpcParameters) { | ||
return new JsonRpcErrorResponse( | ||
requestContext.getRequest().getId(), | ||
new JsonRpcError(RpcErrorType.INVALID_PARAMS, invalidJsonRpcParameters.getMessage())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...hyperledger/besu/ethereum/api/jsonrpc/internal/methods/miner/MinerGetMinGasPriceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright Hyperledger Besu Contributors. | ||
* | ||
* 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.methods.miner; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.hyperledger.besu.datatypes.Wei; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.Quantity; | ||
import org.hyperledger.besu.ethereum.core.ImmutableMiningParameters; | ||
import org.hyperledger.besu.ethereum.core.MiningParameters; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class MinerGetMinGasPriceTest { | ||
|
||
@Test | ||
public void shouldReturnDefaultMinGasPrice() { | ||
final MiningParameters miningParameters = ImmutableMiningParameters.newDefault(); | ||
final MinerGetMinGasPrice method = new MinerGetMinGasPrice(miningParameters); | ||
final JsonRpcRequestContext request = | ||
new JsonRpcRequestContext(new JsonRpcRequest("2.0", method.getName(), new Object[] {})); | ||
|
||
final JsonRpcResponse expected = | ||
new JsonRpcSuccessResponse( | ||
request.getRequest().getId(), | ||
Quantity.create(MiningParameters.MutableInitValues.DEFAULT_MIN_TRANSACTION_GAS_PRICE)); | ||
|
||
final JsonRpcResponse actual = method.response(request); | ||
assertThat(actual).usingRecursiveComparison().isEqualTo(expected); | ||
} | ||
|
||
@Test | ||
public void shouldReturnSetAtRuntimeMinGasPrice() { | ||
final MiningParameters miningParameters = ImmutableMiningParameters.newDefault(); | ||
final MinerGetMinGasPrice method = new MinerGetMinGasPrice(miningParameters); | ||
|
||
final Wei minGasPriceAtRuntime = Wei.of(2000); | ||
|
||
miningParameters.setMinTransactionGasPrice(minGasPriceAtRuntime); | ||
|
||
final JsonRpcRequestContext request = | ||
new JsonRpcRequestContext(new JsonRpcRequest("2.0", method.getName(), new Object[] {})); | ||
|
||
final JsonRpcResponse expected = | ||
new JsonRpcSuccessResponse( | ||
request.getRequest().getId(), Quantity.create(minGasPriceAtRuntime)); | ||
|
||
final JsonRpcResponse actual = method.response(request); | ||
assertThat(actual).usingRecursiveComparison().isEqualTo(expected); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...hyperledger/besu/ethereum/api/jsonrpc/internal/methods/miner/MinerSetMinGasPriceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright Hyperledger Besu Contributors. | ||
* | ||
* 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.methods.miner; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.hyperledger.besu.datatypes.Wei; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType; | ||
import org.hyperledger.besu.ethereum.core.MiningParameters; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class MinerSetMinGasPriceTest { | ||
MiningParameters miningParameters = MiningParameters.newDefault(); | ||
private MinerSetMinGasPrice method; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
method = new MinerSetMinGasPrice(miningParameters); | ||
} | ||
|
||
@Test | ||
public void shouldReturnFalseWhenParameterIsInvalid() { | ||
final String newMinGasPrice = "-1"; | ||
final var request = request(newMinGasPrice); | ||
|
||
method.response(request); | ||
final JsonRpcResponse expected = | ||
new JsonRpcErrorResponse( | ||
request.getRequest().getId(), | ||
new JsonRpcError( | ||
RpcErrorType.INVALID_PARAMS, | ||
"Illegal character '-' found at index 0 in hex binary representation")); | ||
|
||
final JsonRpcResponse actual = method.response(request); | ||
assertThat(actual).usingRecursiveComparison().isEqualTo(expected); | ||
} | ||
|
||
@Test | ||
public void shouldChangeMinPriorityFee() { | ||
final String newMinGasPrice = "0x10"; | ||
final var request = request(newMinGasPrice); | ||
method.response(request); | ||
final JsonRpcResponse expected = new JsonRpcSuccessResponse(request.getRequest().getId(), true); | ||
|
||
final JsonRpcResponse actual = method.response(request); | ||
assertThat(actual).usingRecursiveComparison().isEqualTo(expected); | ||
assertThat(miningParameters.getMinTransactionGasPrice()) | ||
.isEqualTo(Wei.fromHexString(newMinGasPrice)); | ||
} | ||
|
||
private JsonRpcRequestContext request(final String newMinGasPrice) { | ||
return new JsonRpcRequestContext( | ||
new JsonRpcRequest("2.0", method.getName(), new Object[] {newMinGasPrice})); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.