Skip to content

Commit

Permalink
Merge branch 'refs/heads/5098-branch-1-add-rpc-error-types' into 5098…
Browse files Browse the repository at this point in the history
…-branch-2-update-invalid-accounts-params
  • Loading branch information
Matilda-Clerke committed Jul 30, 2024
2 parents 5acb2d7 + 0d4a9b3 commit ad7d7b3
Show file tree
Hide file tree
Showing 30 changed files with 179 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
Expand All @@ -57,6 +58,8 @@ public class SetCodeTransactionAcceptanceTest extends AcceptanceTestBase {
public static final Bytes TRANSACTION_SPONSOR_PRIVATE_KEY =
Bytes.fromHexString("3a4ff6d22d7502ef2452368165422861c01a0f72f851793b372b87888dc3c453");

private final Account otherAccount = accounts.createAccount("otherAccount");

private BesuNode besuNode;
private PragueAcceptanceTestHelper testHelper;

Expand All @@ -68,11 +71,17 @@ void setUp() throws IOException {
testHelper = new PragueAcceptanceTestHelper(besuNode, ethTransactions);
}

@AfterEach
void tearDown() {
besuNode.close();
cluster.close();
}

/**
* At the beginning of the test both the authorizer and the transaction sponsor have a balance of
* 90000 ETH. The authorizer creates an authorization for a contract that send all its ETH to any
* given address. The transaction sponsor created a 7702 transaction with it and sends all the ETH
* from the authorizer to itself. The authorizer balance should be 0 and the transaction sponsor
* given address. The transaction sponsor sponsors the 7702 transaction and sends all the ETH from
* the authorizer to itself. The authorizer balance should be 0 and the transaction sponsor's
* balance should be 180000 ETH minus the transaction costs.
*/
@Test
Expand Down Expand Up @@ -122,4 +131,63 @@ public void shouldTransferAllEthOfAuthorizerToSponsor() throws IOException {
BigInteger expectedSponsorBalance = new BigInteger("180000000000000000000000").subtract(txCost);
cluster.verify(transactionSponsor.balanceEquals(Amount.wei(expectedSponsorBalance)));
}

/**
* The authorizer creates an authorization for a contract that sends all its ETH to any given
* address. But the nonce is 1 and the authorization list is processed before the nonce increase
* of the sender. Therefore, the authorization should be invalid and will be ignored. No balance
* change, except for a decrease for paying the transaction cost should occur.
*/
@Test
public void shouldCheckNonceBeforeNonceIncreaseOfSender() throws IOException {

cluster.verify(authorizer.balanceEquals(Amount.ether(90000)));

final org.hyperledger.besu.datatypes.SetCodeAuthorization authorization =
SetCodeAuthorization.builder()
.chainId(BigInteger.valueOf(20211))
.nonces(
Optional.of(
1L)) // nonce is 1, but because it is validated before the nonce increase, it
// should be 0
.address(SEND_ALL_ETH_CONTRACT_ADDRESS)
.signAndBuild(
secp256k1.createKeyPair(
secp256k1.createPrivateKey(AUTHORIZER_PRIVATE_KEY.toUnsignedBigInteger())));

final Transaction tx =
Transaction.builder()
.type(TransactionType.SET_CODE)
.chainId(BigInteger.valueOf(20211))
.nonce(0)
.maxPriorityFeePerGas(Wei.of(1000000000))
.maxFeePerGas(Wei.fromHexString("0x02540BE400"))
.gasLimit(1000000)
.to(Address.fromHexStringStrict(authorizer.getAddress()))
.value(Wei.ZERO)
.payload(Bytes32.leftPad(Bytes.fromHexString(otherAccount.getAddress())))
.accessList(List.of())
.setCodeTransactionPayloads(List.of(authorization))
.signAndBuild(
secp256k1.createKeyPair(
secp256k1.createPrivateKey(AUTHORIZER_PRIVATE_KEY.toUnsignedBigInteger())));

final String txHash =
besuNode.execute(ethTransactions.sendRawTransaction(tx.encoded().toHexString()));
testHelper.buildNewBlock();

Optional<TransactionReceipt> maybeTransactionReceipt =
besuNode.execute(ethTransactions.getTransactionReceipt(txHash));
assertThat(maybeTransactionReceipt).isPresent();

// verify that the balance of the other account has not changed
cluster.verify(otherAccount.balanceEquals(0));

final String gasPriceWithout0x =
maybeTransactionReceipt.get().getEffectiveGasPrice().substring(2);
final BigInteger txCost =
maybeTransactionReceipt.get().getGasUsed().multiply(new BigInteger(gasPriceWithout0x, 16));
BigInteger expectedSenderBalance = new BigInteger("90000000000000000000000").subtract(txCost);
cluster.verify(authorizer.balanceEquals(Amount.wei(expectedSenderBalance)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
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.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponseType;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.plugin.services.rpc.RpcResponseType;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -61,7 +61,7 @@ public void discardEmpty() {

final JsonRpcResponse response = discard.response(requestWithParams(a0));

assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
assertThat(response.getType()).isEqualTo(RpcResponseType.SUCCESS);
final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
assertThat(successResponse.getResult()).isEqualTo(true);
assertThat(validatorProvider.getVoteProviderAtHead().get().getProposals().get(a0)).isNull();
Expand All @@ -77,7 +77,7 @@ public void discardAuth() {
final JsonRpcResponse response = discard.response(requestWithParams(a0));

assertThat(validatorProvider.getVoteProviderAtHead().get().getProposals().get(a0)).isNull();
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
assertThat(response.getType()).isEqualTo(RpcResponseType.SUCCESS);
final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
assertThat(successResponse.getResult()).isEqualTo(true);
}
Expand All @@ -92,7 +92,7 @@ public void discardDrop() {
final JsonRpcResponse response = discard.response(requestWithParams(a0));

assertThat(validatorProvider.getVoteProviderAtHead().get().getProposals().get(a0)).isNull();
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
assertThat(response.getType()).isEqualTo(RpcResponseType.SUCCESS);
final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
assertThat(successResponse.getResult()).isEqualTo(true);
}
Expand All @@ -111,7 +111,7 @@ public void discardIsolation() {
assertThat(validatorProvider.getVoteProviderAtHead().get().getProposals().get(a0)).isNull();
assertThat(validatorProvider.getVoteProviderAtHead().get().getProposals().get(a1))
.isEqualTo(VoteType.ADD);
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
assertThat(response.getType()).isEqualTo(RpcResponseType.SUCCESS);
final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
assertThat(successResponse.getResult()).isEqualTo(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
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.JsonRpcResponseType;
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.chain.Blockchain;
import org.hyperledger.besu.plugin.services.rpc.RpcResponseType;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -59,7 +59,7 @@ public void testAuth() {

assertThat(validatorProvider.getVoteProviderAtHead().get().getProposals().get(a1))
.isEqualTo(VoteType.ADD);
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
assertThat(response.getType()).isEqualTo(RpcResponseType.SUCCESS);
final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
assertThat(successResponse.getResult()).isEqualTo(true);
}
Expand All @@ -72,7 +72,7 @@ public void testAuthWithAddressZeroResultsInError() {
final JsonRpcResponse response = propose.response(requestWithParams(a0, true));

assertThat(validatorProvider.getVoteProviderAtHead().get().getProposals().get(a0)).isNull();
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.ERROR);
assertThat(response.getType()).isEqualTo(RpcResponseType.ERROR);
final JsonRpcErrorResponse errorResponse = (JsonRpcErrorResponse) response;
assertThat(errorResponse.getErrorType()).isEqualTo(RpcErrorType.INVALID_REQUEST);
}
Expand All @@ -86,7 +86,7 @@ public void testDrop() {

assertThat(validatorProvider.getVoteProviderAtHead().get().getProposals().get(a1))
.isEqualTo(VoteType.DROP);
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
assertThat(response.getType()).isEqualTo(RpcResponseType.SUCCESS);
final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
assertThat(successResponse.getResult()).isEqualTo(true);
}
Expand All @@ -99,7 +99,7 @@ public void testDropWithAddressZeroResultsInError() {
final JsonRpcResponse response = propose.response(requestWithParams(a0, false));

assertThat(validatorProvider.getVoteProviderAtHead().get().getProposals().get(a0)).isNull();
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.ERROR);
assertThat(response.getType()).isEqualTo(RpcResponseType.ERROR);
final JsonRpcErrorResponse errorResponse = (JsonRpcErrorResponse) response;
assertThat(errorResponse.getErrorType()).isEqualTo(RpcErrorType.INVALID_REQUEST);
}
Expand All @@ -114,7 +114,7 @@ public void testRepeatAuth() {

assertThat(validatorProvider.getVoteProviderAtHead().get().getProposals().get(a1))
.isEqualTo(VoteType.ADD);
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
assertThat(response.getType()).isEqualTo(RpcResponseType.SUCCESS);
final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
assertThat(successResponse.getResult()).isEqualTo(true);
}
Expand All @@ -129,7 +129,7 @@ public void testRepeatDrop() {

assertThat(validatorProvider.getVoteProviderAtHead().get().getProposals().get(a1))
.isEqualTo(VoteType.DROP);
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
assertThat(response.getType()).isEqualTo(RpcResponseType.SUCCESS);
final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
assertThat(successResponse.getResult()).isEqualTo(true);
}
Expand All @@ -144,7 +144,7 @@ public void testChangeToAuth() {

assertThat(validatorProvider.getVoteProviderAtHead().get().getProposals().get(a1))
.isEqualTo(VoteType.ADD);
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
assertThat(response.getType()).isEqualTo(RpcResponseType.SUCCESS);
final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
assertThat(successResponse.getResult()).isEqualTo(true);
}
Expand All @@ -159,7 +159,7 @@ public void testChangeToDrop() {

assertThat(validatorProvider.getVoteProviderAtHead().get().getProposals().get(a0))
.isEqualTo(VoteType.DROP);
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
assertThat(response.getType()).isEqualTo(RpcResponseType.SUCCESS);
final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
assertThat(successResponse.getResult()).isEqualTo(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
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.JsonRpcResponseType;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.DebugTraceTransactionResult;
import org.hyperledger.besu.plugin.services.rpc.RpcResponseType;
import org.hyperledger.besu.testutil.BlockTestUtil;

import java.util.Map;
Expand Down Expand Up @@ -69,7 +69,7 @@ public void debugTraceTransactionSuccessTest() {
new JsonRpcRequestContext(new JsonRpcRequest("2.0", DEBUG_TRACE_TRANSACTION, params));

final JsonRpcResponse response = method.response(request);
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
assertThat(response.getType()).isEqualTo(RpcResponseType.SUCCESS);
DebugTraceTransactionResult debugTraceTransactionResult =
(DebugTraceTransactionResult) ((JsonRpcSuccessResponse) response).getResult();
assertThat(debugTraceTransactionResult.getGas()).isEqualTo(23705L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.execution.JsonRpcExecutor;
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.JsonRpcResponseType;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
import org.hyperledger.besu.plugin.services.rpc.RpcResponseType;

import java.io.IOException;

Expand Down Expand Up @@ -74,7 +74,7 @@ public void executeRpcRequestBatch(
generator.writeStartArray();
for (int i = 0; i < rpcRequestBatch.size(); i++) {
JsonRpcResponse response = processMaybeRequest(rpcRequestBatch.getValue(i));
if (response.getType() != JsonRpcResponseType.NONE) {
if (response.getType() != RpcResponseType.NONE) {
generator.writeObject(response);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.execution.JsonRpcExecutor;
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.JsonRpcResponseType;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
import org.hyperledger.besu.plugin.services.rpc.RpcResponseType;

import java.io.IOException;

Expand Down Expand Up @@ -70,7 +70,7 @@ private void handleJsonObjectResponse(
final RoutingContext ctx)
throws IOException {
response.setStatusCode(status(jsonRpcResponse).code());
if (jsonRpcResponse.getType() == JsonRpcResponseType.NONE) {
if (jsonRpcResponse.getType() == RpcResponseType.NONE) {
response.end();
} else {
try (final JsonResponseStreamer streamer =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
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.JsonRpcResponseType;
import org.hyperledger.besu.metrics.BesuMetricCategory;
import org.hyperledger.besu.plugin.services.MetricsSystem;
import org.hyperledger.besu.plugin.services.metrics.Counter;
import org.hyperledger.besu.plugin.services.metrics.LabelledMetric;
import org.hyperledger.besu.plugin.services.rpc.RpcResponseType;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.StatusCode;
Expand Down Expand Up @@ -52,7 +52,7 @@ public JsonRpcResponse process(
final Span metricSpan,
final JsonRpcRequestContext request) {
JsonRpcResponse jsonRpcResponse = rpcProcessor.process(id, method, metricSpan, request);
if (JsonRpcResponseType.ERROR == jsonRpcResponse.getType()) {
if (RpcResponseType.ERROR == jsonRpcResponse.getType()) {
JsonRpcErrorResponse errorResponse = (JsonRpcErrorResponse) jsonRpcResponse;
this.rpcErrorsCounter.labels(method.getName(), errorResponse.getErrorType().name()).inc();
switch (errorResponse.getErrorType()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.response;

import org.hyperledger.besu.ethereum.mainnet.ValidationResult;
import org.hyperledger.besu.plugin.services.rpc.RpcResponseType;

import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -72,8 +73,8 @@ public JsonRpcError getError() {

@Override
@JsonIgnore
public JsonRpcResponseType getType() {
return JsonRpcResponseType.ERROR;
public RpcResponseType getType() {
return RpcResponseType.ERROR;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.response;

import org.hyperledger.besu.plugin.services.rpc.RpcResponseType;

public class JsonRpcNoResponse implements JsonRpcResponse {

@Override
public JsonRpcResponseType getType() {
return JsonRpcResponseType.NONE;
public RpcResponseType getType() {
return RpcResponseType.NONE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.response;

import org.hyperledger.besu.plugin.services.rpc.RpcResponseType;

import com.fasterxml.jackson.annotation.JsonGetter;

public interface JsonRpcResponse {
Expand All @@ -23,5 +25,5 @@ default String getVersion() {
return "2.0";
}

JsonRpcResponseType getType();
RpcResponseType getType();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.response;

import org.hyperledger.besu.plugin.services.rpc.RpcResponseType;

import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonGetter;
Expand Down Expand Up @@ -50,8 +52,8 @@ public Object getResult() {

@Override
@JsonIgnore
public JsonRpcResponseType getType() {
return JsonRpcResponseType.SUCCESS;
public RpcResponseType getType() {
return RpcResponseType.SUCCESS;
}

@Override
Expand Down
Loading

0 comments on commit ad7d7b3

Please sign in to comment.