Skip to content

Commit

Permalink
Fix and test
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio Di Fabio <[email protected]>
  • Loading branch information
fab-10 committed Jun 12, 2024
1 parent 668f135 commit 91c822a
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
* Copyright Consensys Software Inc.
*
* 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 linea.plugin.acc.test.rpc.linea;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;

import linea.plugin.acc.test.LineaPluginTestBase;
import linea.plugin.acc.test.TestCommandLineOptionsBuilder;
import net.consensys.linea.config.LineaProfitabilityCliOptions;
import net.consensys.linea.config.LineaProfitabilityConfiguration;
import org.apache.tuweni.bytes.Bytes32;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.NodeRequests;
import org.hyperledger.besu.tests.acceptance.dsl.transaction.Transaction;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.web3j.protocol.core.Request;
import org.web3j.protocol.http.HttpService;

public class SetExtraDataTest extends LineaPluginTestBase {
protected static final int FIXED_GAS_COST_WEI = 0;
protected static final int VARIABLE_GAS_COST_WEI = 1_000_000_000;
protected static final double MIN_MARGIN = 1.0;
protected static final double ESTIMATE_GAS_MIN_MARGIN = 1.0;
protected static final Wei MIN_GAS_PRICE = Wei.of(1_000_000_000);
protected static final int MAX_TRANSACTION_GAS_LIMIT = 30_000_000;
protected LineaProfitabilityConfiguration profitabilityConf;

@Override
public List<String> getTestCliOptions() {
return getTestCommandLineOptionsBuilder().build();
}

protected TestCommandLineOptionsBuilder getTestCommandLineOptionsBuilder() {
return new TestCommandLineOptionsBuilder()
.set("--plugin-linea-fixed-gas-cost-wei=", String.valueOf(FIXED_GAS_COST_WEI))
.set("--plugin-linea-variable-gas-cost-wei=", String.valueOf(VARIABLE_GAS_COST_WEI))
.set("--plugin-linea-min-margin=", String.valueOf(MIN_MARGIN))
.set("--plugin-linea-estimate-gas-min-margin=", String.valueOf(ESTIMATE_GAS_MIN_MARGIN))
.set("--plugin-linea-max-tx-gas-limit=", String.valueOf(MAX_TRANSACTION_GAS_LIMIT));
}

@BeforeEach
public void setMinGasPrice() {
minerNode.getMiningParameters().setMinTransactionGasPrice(MIN_GAS_PRICE);
}

@BeforeEach
public void createDefaultConfigurations() {
profitabilityConf =
LineaProfitabilityCliOptions.create().toDomainObject().toBuilder()
.fixedCostWei(FIXED_GAS_COST_WEI)
.variableCostWei(VARIABLE_GAS_COST_WEI)
.minMargin(MIN_MARGIN)
.estimateGasMinMargin(ESTIMATE_GAS_MIN_MARGIN)
.build();
}

@Test
public void setUnsupportedExtraDataReturnsError() {
final var unsupportedExtraData = Bytes32.ZERO;

final var reqLinea = new FailingLineaSetExtraDataRequest(unsupportedExtraData);
final var respLinea = reqLinea.execute(minerNode.nodeRequests());
assertThat(respLinea.getMessage())
.isEqualTo(
"Unsupported extra data field 0x0000000000000000000000000000000000000000000000000000000000000000");
}

@Test
public void successfulSetExtraData() {
final var extraData =
Bytes32.fromHexString("0x0100000000000000000000000000000000000000000000000000000000000000");

final var reqLinea = new LineaSetExtraDataRequest(extraData);
final var respLinea = reqLinea.execute(minerNode.nodeRequests());
assertThat(respLinea).isTrue();
}

@Test
public void parseErrorLineaEstimateGasRequestReturnErrorResponse()
throws IOException, InterruptedException {
final var httpService = (HttpService) minerNode.nodeRequests().getWeb3jService();
final var httpClient = HttpClient.newHttpClient();
final var badJsonRequest =
HttpRequest.newBuilder(URI.create(httpService.getUrl()))
.headers("Content-Type", "application/json")
.POST(
HttpRequest.BodyPublishers.ofString(
"""
{"jsonrpc":"2.0","method":"linea_setExtraData","params":[malformed json],"id":53}
"""))
.build();
final var errorResponse = httpClient.send(badJsonRequest, HttpResponse.BodyHandlers.ofString());
assertThat(errorResponse.body())
.isEqualTo(
"""
{"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"Parse error"}}""");
}

static class LineaSetExtraDataRequest implements Transaction<Boolean> {
private final Bytes32 extraData;

public LineaSetExtraDataRequest(final Bytes32 extraData) {
this.extraData = extraData;
}

@Override
public Boolean execute(final NodeRequests nodeRequests) {
try {
return new Request<>(
"linea_setExtraData",
List.of(extraData.toHexString()),
nodeRequests.getWeb3jService(),
LineaSetExtraDataResponse.class)
.send()
.getResult();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

static class FailingLineaSetExtraDataRequest
implements Transaction<org.web3j.protocol.core.Response.Error> {
private final Bytes32 extraData;

public FailingLineaSetExtraDataRequest(final Bytes32 extraData) {
this.extraData = extraData;
}

@Override
public org.web3j.protocol.core.Response.Error execute(final NodeRequests nodeRequests) {
try {
return new Request<>(
"linea_setExtraData",
List.of(extraData.toHexString()),
nodeRequests.getWeb3jService(),
LineaSetExtraDataResponse.class)
.send()
.getError();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

static class LineaSetExtraDataResponse extends org.web3j.protocol.core.Response<Boolean> {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,19 @@

public class LineaExtraDataException extends RuntimeException {
public enum ErrorType {
INVALID_ARGUMENT,
FAILED_CALLING_SET_MIN_GAS_PRICE,
FAILED_CALLING_SET_EXTRA_DATA
INVALID_ARGUMENT(-32602),
FAILED_CALLING_SET_MIN_GAS_PRICE(-32000),
FAILED_CALLING_SET_EXTRA_DATA(-32000);

private final int code;

ErrorType(int code) {
this.code = code;
}

public int getCode() {
return code;
}
}

private final ErrorType errorType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,11 @@ public void doRegister(final BesuContext context) {
lineaEstimateGasMethod.getName(),
lineaEstimateGasMethod::execute);

lineaSetExtraDataMethod =
new LineaSetExtraData(
rpcEndpointService,
new LineaExtraDataHandler(rpcEndpointService, profitabilityConfiguration));
lineaSetExtraDataMethod = new LineaSetExtraData(rpcEndpointService);
rpcEndpointService.registerRPCEndpoint(
lineaSetExtraDataMethod.getNamespace(),
lineaSetExtraDataMethod.getName(),
lineaEstimateGasMethod::execute);
lineaSetExtraDataMethod::execute);
}

@Override
Expand All @@ -105,5 +102,7 @@ public void beforeExternalServices() {
profitabilityConfiguration,
createLimitModules(tracerConfiguration),
l1L2BridgeConfiguration);
lineaSetExtraDataMethod.init(
new LineaExtraDataHandler(rpcEndpointService, profitabilityConfiguration));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public LineaEstimateGas(
}

public void init(
LineaRpcConfiguration rpcConfiguration,
final LineaRpcConfiguration rpcConfiguration,
final LineaTransactionPoolValidatorConfiguration transactionValidatorConfiguration,
final LineaProfitabilityConfiguration profitabilityConf,
final Map<String, Integer> limitsMap,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ public class LineaSetExtraData {

private static final AtomicInteger LOG_SEQUENCE = new AtomicInteger();
private final JsonRpcParameter parameterParser = new JsonRpcParameter();
private final LineaExtraDataHandler extraDataHandler;
private final RpcEndpointService rpcEndpointService;
private LineaExtraDataHandler extraDataHandler;

public LineaSetExtraData(
final RpcEndpointService rpcEndpointService, final LineaExtraDataHandler extraDataHandler) {
public LineaSetExtraData(final RpcEndpointService rpcEndpointService) {
this.rpcEndpointService = rpcEndpointService;
}

public void init(final LineaExtraDataHandler extraDataHandler) {
this.extraDataHandler = extraDataHandler;
}

Expand Down Expand Up @@ -68,8 +70,8 @@ public Boolean execute(final PluginRpcRequest request) {
updateStandardExtraData(extraData);

return Boolean.TRUE;
} catch (final Exception e) {
throw new PluginRpcEndpointException(new ExtraDataPricingError(e.toString()));
} catch (final LineaExtraDataException lede) {
throw new PluginRpcEndpointException(new ExtraDataPricingError(lede));
}
}

Expand Down Expand Up @@ -108,15 +110,15 @@ public record Response(
@JsonProperty String baseFeePerGas,
@JsonProperty String priorityFeePerGas) {}

private record ExtraDataPricingError(String errorReason) implements RpcMethodError {
private record ExtraDataPricingError(LineaExtraDataException ex) implements RpcMethodError {
@Override
public int getCode() {
return -32000;
return ex.getErrorType().getCode();
}

@Override
public String getMessage() {
return errorReason;
return ex.getMessage();
}
}
}

0 comments on commit 91c822a

Please sign in to comment.