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

Use gasPrice when this field is present (eth_estimateGas) #1636

Merged
merged 10 commits into from
Dec 7, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonCallParameter;
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.core.Address;
import org.hyperledger.besu.ethereum.core.Gas;
import org.hyperledger.besu.ethereum.core.Wei;
import org.hyperledger.besu.ethereum.transaction.CallParameter;
import org.hyperledger.besu.testutil.BlockTestUtil;

import java.util.Map;
Expand Down Expand Up @@ -66,16 +66,17 @@ public void setUp() {

@Test
public void shouldReturnExpectedResultForCallAtLatestBlock() {
final CallParameter callParameter =
new CallParameter(
final JsonCallParameter callParameter =
new JsonCallParameter(
Address.fromHexString("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b"),
Address.fromHexString("0x6295ee1b4f6dd65047762f924ecd367c17eabf8f"),
null,
null,
null,
null,
null,
Bytes.fromHexString("0x12a7b914"));
Bytes.fromHexString("0x12a7b914"),
null);
final JsonRpcRequestContext request = requestWithParams(callParameter, "latest");
final JsonRpcResponse expectedResponse =
new JsonRpcSuccessResponse(
Expand All @@ -88,16 +89,17 @@ public void shouldReturnExpectedResultForCallAtLatestBlock() {

@Test
public void shouldReturnExpectedResultForCallAtSpecificBlock() {
final CallParameter callParameter =
new CallParameter(
final JsonCallParameter callParameter =
new JsonCallParameter(
Address.fromHexString("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b"),
Address.fromHexString("0x6295ee1b4f6dd65047762f924ecd367c17eabf8f"),
null,
null,
null,
null,
null,
Bytes.fromHexString("0x12a7b914"));
Bytes.fromHexString("0x12a7b914"),
null);
final JsonRpcRequestContext request = requestWithParams(callParameter, "0x8");
final JsonRpcResponse expectedResponse =
new JsonRpcSuccessResponse(
Expand All @@ -110,16 +112,17 @@ public void shouldReturnExpectedResultForCallAtSpecificBlock() {

@Test
public void shouldReturnInvalidRequestWhenMissingToField() {
final CallParameter callParameter =
new CallParameter(
final JsonCallParameter callParameter =
new JsonCallParameter(
Address.fromHexString("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b"),
null,
null,
null,
null,
null,
null,
Bytes.fromHexString("0x12a7b914"));
Bytes.fromHexString("0x12a7b914"),
null);
final JsonRpcRequestContext request = requestWithParams(callParameter, "latest");

final Throwable thrown = catchThrowable(() -> method.response(request));
Expand All @@ -132,16 +135,17 @@ public void shouldReturnInvalidRequestWhenMissingToField() {

@Test
public void shouldReturnErrorWithGasLimitTooLow() {
final CallParameter callParameter =
new CallParameter(
final JsonCallParameter callParameter =
new JsonCallParameter(
Address.fromHexString("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b"),
Address.fromHexString("0x6295ee1b4f6dd65047762f924ecd367c17eabf8f"),
Gas.ZERO,
null,
null,
null,
null,
Bytes.fromHexString("0x12a7b914"));
Bytes.fromHexString("0x12a7b914"),
null);
final JsonRpcRequestContext request = requestWithParams(callParameter, "latest");
final JsonRpcResponse expectedResponse =
new JsonRpcErrorResponse(null, JsonRpcError.INTRINSIC_GAS_EXCEEDS_LIMIT);
Expand All @@ -153,16 +157,17 @@ public void shouldReturnErrorWithGasLimitTooLow() {

@Test
public void shouldReturnErrorWithGasPriceTooHigh() {
final CallParameter callParameter =
new CallParameter(
final JsonCallParameter callParameter =
new JsonCallParameter(
Address.fromHexString("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b"),
Address.fromHexString("0x6295ee1b4f6dd65047762f924ecd367c17eabf8f"),
null,
Wei.fromHexString("0x10000000000000"),
null,
null,
null,
Bytes.fromHexString("0x12a7b914"));
Bytes.fromHexString("0x12a7b914"),
null);
final JsonRpcRequestContext request = requestWithParams(callParameter, "latest");
final JsonRpcResponse expectedResponse =
new JsonRpcErrorResponse(null, JsonRpcError.TRANSACTION_UPFRONT_COST_EXCEEDS_BALANCE);
Expand All @@ -174,15 +179,16 @@ public void shouldReturnErrorWithGasPriceTooHigh() {

@Test
public void shouldReturnEmptyHashResultForCallWithOnlyToField() {
final CallParameter callParameter =
new CallParameter(
final JsonCallParameter callParameter =
new JsonCallParameter(
null,
Address.fromHexString("0x6295ee1b4f6dd65047762f924ecd367c17eabf8f"),
null,
null,
null,
null,
null,
null,
null);
final JsonRpcRequestContext request = requestWithParams(callParameter, "latest");
final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(null, "0x");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
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.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonCallParameter;
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.core.Address;
import org.hyperledger.besu.ethereum.core.Gas;
import org.hyperledger.besu.ethereum.core.Wei;
import org.hyperledger.besu.ethereum.transaction.CallParameter;
import org.hyperledger.besu.testutil.BlockTestUtil;

import java.util.Map;
Expand Down Expand Up @@ -62,8 +64,8 @@ public void setUp() {

@Test
public void shouldReturnExpectedValueForEmptyCallParameter() {
final CallParameter callParameter =
new CallParameter(null, null, null, null, null, null, null, null);
final JsonCallParameter callParameter =
new JsonCallParameter(null, null, null, null, null, null, null, null, null);
final JsonRpcRequestContext request = requestWithParams(callParameter);
final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(null, "0x5208");

Expand All @@ -74,15 +76,16 @@ public void shouldReturnExpectedValueForEmptyCallParameter() {

@Test
public void shouldReturnExpectedValueForTransfer() {
final CallParameter callParameter =
new CallParameter(
final JsonCallParameter callParameter =
new JsonCallParameter(
Address.fromHexString("0x6295ee1b4f6dd65047762f924ecd367c17eabf8f"),
Address.fromHexString("0x8888f1f195afa192cfee860698584c030f4c9db1"),
null,
null,
null,
null,
Wei.ONE,
null,
null);
final JsonRpcRequestContext request = requestWithParams(callParameter);
final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(null, "0x5208");
Expand All @@ -94,8 +97,8 @@ public void shouldReturnExpectedValueForTransfer() {

@Test
public void shouldReturnExpectedValueForContractDeploy() {
final CallParameter callParameter =
new CallParameter(
final JsonCallParameter callParameter =
new JsonCallParameter(
Address.fromHexString("0x6295ee1b4f6dd65047762f924ecd367c17eabf8f"),
null,
null,
Expand All @@ -104,7 +107,8 @@ public void shouldReturnExpectedValueForContractDeploy() {
null,
null,
Bytes.fromHexString(
"0x608060405234801561001057600080fd5b50610157806100206000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633bdab8bf146100515780639ae97baa14610068575b600080fd5b34801561005d57600080fd5b5061006661007f565b005b34801561007457600080fd5b5061007d6100b9565b005b7fa53887c1eed04528e23301f55ad49a91634ef5021aa83a97d07fd16ed71c039a60016040518082815260200191505060405180910390a1565b7fa53887c1eed04528e23301f55ad49a91634ef5021aa83a97d07fd16ed71c039a60026040518082815260200191505060405180910390a17fa53887c1eed04528e23301f55ad49a91634ef5021aa83a97d07fd16ed71c039a60036040518082815260200191505060405180910390a15600a165627a7a7230582010ddaa52e73a98c06dbcd22b234b97206c1d7ed64a7c048e10c2043a3d2309cb0029"));
"0x608060405234801561001057600080fd5b50610157806100206000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633bdab8bf146100515780639ae97baa14610068575b600080fd5b34801561005d57600080fd5b5061006661007f565b005b34801561007457600080fd5b5061007d6100b9565b005b7fa53887c1eed04528e23301f55ad49a91634ef5021aa83a97d07fd16ed71c039a60016040518082815260200191505060405180910390a1565b7fa53887c1eed04528e23301f55ad49a91634ef5021aa83a97d07fd16ed71c039a60026040518082815260200191505060405180910390a17fa53887c1eed04528e23301f55ad49a91634ef5021aa83a97d07fd16ed71c039a60036040518082815260200191505060405180910390a15600a165627a7a7230582010ddaa52e73a98c06dbcd22b234b97206c1d7ed64a7c048e10c2043a3d2309cb0029"),
null);
final JsonRpcRequestContext request = requestWithParams(callParameter);
final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(null, "0x1b551");

Expand All @@ -114,9 +118,9 @@ public void shouldReturnExpectedValueForContractDeploy() {
}

@Test
public void shouldIgnoreGasLimitAndGasPriceAndReturnExpectedValue() {
final CallParameter callParameter =
new CallParameter(
public void shouldIgnoreSenderBalanceAccountWhenStrictModeDisabledAndReturnExpectedValue() {
final JsonCallParameter callParameter =
new JsonCallParameter(
Address.fromHexString("0x6295ee1b4f6dd65047762f924ecd367c17eabf8f"),
null,
Gas.of(1),
Expand All @@ -125,7 +129,8 @@ public void shouldIgnoreGasLimitAndGasPriceAndReturnExpectedValue() {
null,
null,
Bytes.fromHexString(
"0x608060405234801561001057600080fd5b50610157806100206000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633bdab8bf146100515780639ae97baa14610068575b600080fd5b34801561005d57600080fd5b5061006661007f565b005b34801561007457600080fd5b5061007d6100b9565b005b7fa53887c1eed04528e23301f55ad49a91634ef5021aa83a97d07fd16ed71c039a60016040518082815260200191505060405180910390a1565b7fa53887c1eed04528e23301f55ad49a91634ef5021aa83a97d07fd16ed71c039a60026040518082815260200191505060405180910390a17fa53887c1eed04528e23301f55ad49a91634ef5021aa83a97d07fd16ed71c039a60036040518082815260200191505060405180910390a15600a165627a7a7230582010ddaa52e73a98c06dbcd22b234b97206c1d7ed64a7c048e10c2043a3d2309cb0029"));
"0x608060405234801561001057600080fd5b50610157806100206000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633bdab8bf146100515780639ae97baa14610068575b600080fd5b34801561005d57600080fd5b5061006661007f565b005b34801561007457600080fd5b5061007d6100b9565b005b7fa53887c1eed04528e23301f55ad49a91634ef5021aa83a97d07fd16ed71c039a60016040518082815260200191505060405180910390a1565b7fa53887c1eed04528e23301f55ad49a91634ef5021aa83a97d07fd16ed71c039a60026040518082815260200191505060405180910390a17fa53887c1eed04528e23301f55ad49a91634ef5021aa83a97d07fd16ed71c039a60036040518082815260200191505060405180910390a15600a165627a7a7230582010ddaa52e73a98c06dbcd22b234b97206c1d7ed64a7c048e10c2043a3d2309cb0029"),
false);
final JsonRpcRequestContext request = requestWithParams(callParameter);
final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(null, "0x1b551");

Expand All @@ -134,10 +139,32 @@ public void shouldIgnoreGasLimitAndGasPriceAndReturnExpectedValue() {
assertThat(response).isEqualToComparingFieldByField(expectedResponse);
}

@Test
public void shouldNotIgnoreSenderBalanceAccountWhenStrictModeDisabledAndThrowError() {
final JsonCallParameter callParameter =
new JsonCallParameter(
Address.fromHexString("0x6295ee1b4f6dd65047762f924ecd367c17eabf8f"),
null,
Gas.of(1),
Wei.fromHexString("0x9999999999"),
null,
null,
null,
Bytes.fromHexString(
"0x608060405234801561001057600080fd5b50610157806100206000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633bdab8bf146100515780639ae97baa14610068575b600080fd5b34801561005d57600080fd5b5061006661007f565b005b34801561007457600080fd5b5061007d6100b9565b005b7fa53887c1eed04528e23301f55ad49a91634ef5021aa83a97d07fd16ed71c039a60016040518082815260200191505060405180910390a1565b7fa53887c1eed04528e23301f55ad49a91634ef5021aa83a97d07fd16ed71c039a60026040518082815260200191505060405180910390a17fa53887c1eed04528e23301f55ad49a91634ef5021aa83a97d07fd16ed71c039a60036040518082815260200191505060405180910390a15600a165627a7a7230582010ddaa52e73a98c06dbcd22b234b97206c1d7ed64a7c048e10c2043a3d2309cb0029"),
true);
final JsonRpcRequestContext request = requestWithParams(callParameter);
final JsonRpcResponse expectedResponse =
new JsonRpcErrorResponse(null, JsonRpcError.TRANSACTION_UPFRONT_COST_EXCEEDS_BALANCE);

final JsonRpcResponse response = method.response(request);
assertThat(response).isEqualToComparingFieldByField(expectedResponse);
}

@Test
public void shouldReturnExpectedValueForInsufficientGas() {
final CallParameter callParameter =
new CallParameter(null, null, Gas.of(1), null, null, null, null, null);
final JsonCallParameter callParameter =
new JsonCallParameter(null, null, Gas.of(1), null, null, null, null, null, null);
final JsonRpcRequestContext request = requestWithParams(callParameter);
final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(null, "0x5208");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
import org.hyperledger.besu.ethereum.core.Wei;
import org.hyperledger.besu.ethereum.core.WorldState;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.TransactionValidationParams;
import org.hyperledger.besu.ethereum.transaction.CallParameter;
import org.hyperledger.besu.ethereum.transaction.TransactionSimulator;
import org.hyperledger.besu.ethereum.transaction.TransactionSimulatorResult;
import org.hyperledger.besu.ethereum.vm.OperationTracer;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -216,7 +218,12 @@ private Optional<CallResult> executeCall(final DataFetchingEnvironment environme
final CallParameter param =
new CallParameter(from, to, gasParam, gasPriceParam, valueParam, data);

final Optional<TransactionSimulatorResult> opt = transactionSimulator.process(param, bn);
final Optional<TransactionSimulatorResult> opt =
transactionSimulator.process(
param,
TransactionValidationParams.transactionSimulator(),
OperationTracer.NO_TRACING,
bn);
if (opt.isPresent()) {
final TransactionSimulatorResult result = opt.get();
long status = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonCallParameter;
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.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.transaction.CallParameter;
import org.hyperledger.besu.ethereum.mainnet.TransactionValidationParams;
import org.hyperledger.besu.ethereum.transaction.TransactionSimulator;
import org.hyperledger.besu.ethereum.vm.OperationTracer;

public class EthCall extends AbstractBlockParameterMethod {

Expand All @@ -49,10 +51,14 @@ protected BlockParameter blockParameter(final JsonRpcRequestContext request) {
@Override
protected Object resultByBlockNumber(
final JsonRpcRequestContext request, final long blockNumber) {
final CallParameter callParams = validateAndGetCallParams(request);
final JsonCallParameter callParams = validateAndGetCallParams(request);

return transactionSimulator
.process(callParams, blockNumber)
.process(
callParams,
TransactionValidationParams.transactionSimulator(),
OperationTracer.NO_TRACING,
blockNumber)
.map(
result ->
result
Expand All @@ -77,11 +83,15 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
return (JsonRpcResponse) findResultByParamType(requestContext);
}

private CallParameter validateAndGetCallParams(final JsonRpcRequestContext request) {
final CallParameter callParams = request.getRequiredParameter(0, CallParameter.class);
private JsonCallParameter validateAndGetCallParams(final JsonRpcRequestContext request) {
final JsonCallParameter callParams = request.getRequiredParameter(0, JsonCallParameter.class);
if (callParams.getTo() == null) {
throw new InvalidJsonRpcParameters("Missing \"to\" field in call arguments");
}
if (callParams.getGasPrice() != null
&& (callParams.getFeeCap().isPresent() || callParams.getGasPremium().isPresent())) {
throw new InvalidJsonRpcParameters("gasPrice cannot be used with baseFee or feeCap");
}
return callParams;
}
}
Loading