Search in sources :

Example 1 with JsonRpcError

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError in project besu by hyperledger.

the class JsonRpcHttpServiceTest method ethGetStorageAtInvalidParameterStorageIndex.

@Test
public void ethGetStorageAtInvalidParameterStorageIndex() throws Exception {
    // Setup mocks to return a block
    final BlockDataGenerator gen = new BlockDataGenerator();
    final Address address = gen.address();
    final String id = "88";
    final RequestBody body = RequestBody.create(JSON, "{\"jsonrpc\":\"2.0\",\"id\":" + Json.encode(id) + ",\"method\":\"eth_getStorageAt\", \"params\": [\"" + address + "\",\"" + "blah" + "\",\"latest\"]}");
    try (final Response resp = client.newCall(buildPostRequest(body)).execute()) {
        assertThat(resp.code()).isEqualTo(400);
        // Check general format of result
        final JsonObject json = new JsonObject(resp.body().string());
        final JsonRpcError expectedError = JsonRpcError.INVALID_PARAMS;
        testHelper.assertValidJsonRpcError(json, id, expectedError.getCode(), expectedError.getMessage());
    }
}
Also used : Response(okhttp3.Response) Address(org.hyperledger.besu.datatypes.Address) InetSocketAddress(java.net.InetSocketAddress) JsonObject(io.vertx.core.json.JsonObject) JsonRpcError(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError) BlockDataGenerator(org.hyperledger.besu.ethereum.core.BlockDataGenerator) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 2 with JsonRpcError

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError in project besu by hyperledger.

the class JsonRpcHttpServiceTest method getBlockByHashWithMissingBooleanParameter.

@Test
public void getBlockByHashWithMissingBooleanParameter() throws Exception {
    final String id = "123";
    final String blockHashString = "0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d15273321";
    final RequestBody body = RequestBody.create(JSON, "{\"jsonrpc\":\"2.0\",\"id\":" + Json.encode(id) + ",\"method\":\"eth_getBlockByHash\", \"params\": [\"" + blockHashString + "\"]}");
    try (final Response resp = client.newCall(buildPostRequest(body)).execute()) {
        assertThat(resp.code()).isEqualTo(400);
        // Check general format of result
        final JsonObject json = new JsonObject(resp.body().string());
        final JsonRpcError expectedError = JsonRpcError.INVALID_PARAMS;
        testHelper.assertValidJsonRpcError(json, id, expectedError.getCode(), expectedError.getMessage());
    }
}
Also used : Response(okhttp3.Response) JsonObject(io.vertx.core.json.JsonObject) JsonRpcError(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 3 with JsonRpcError

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError in project besu by hyperledger.

the class JsonRpcHttpServiceTest method getBlockByHashWithMissingHashParameter.

@Test
public void getBlockByHashWithMissingHashParameter() throws Exception {
    final String id = "123";
    final RequestBody body = RequestBody.create(JSON, "{\"jsonrpc\":\"2.0\",\"id\":" + Json.encode(id) + ",\"method\":\"eth_getBlockByHash\", \"params\": [true]}");
    // Setup mocks
    when(blockchainQueries.blockByHash(ArgumentMatchers.isA(Hash.class))).thenReturn(Optional.empty());
    try (final Response resp = client.newCall(buildPostRequest(body)).execute()) {
        assertThat(resp.code()).isEqualTo(400);
        // Check general format of result
        final JsonObject json = new JsonObject(resp.body().string());
        final JsonRpcError expectedError = JsonRpcError.INVALID_PARAMS;
        testHelper.assertValidJsonRpcError(json, id, expectedError.getCode(), expectedError.getMessage());
    }
}
Also used : Response(okhttp3.Response) JsonObject(io.vertx.core.json.JsonObject) JsonRpcError(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError) Hash(org.hyperledger.besu.datatypes.Hash) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 4 with JsonRpcError

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError in project besu by hyperledger.

the class JsonRpcHttpServiceTest method batchRequest.

@Test
public void batchRequest() throws Exception {
    final int clientVersionRequestId = 2;
    final int brokenRequestId = 3;
    final int netVersionRequestId = 4;
    final RequestBody body = RequestBody.create(JSON, "[{\"jsonrpc\":\"2.0\",\"id\":" + Json.encode(clientVersionRequestId) + ",\"method\":\"web3_clientVersion\"}," + "{\"jsonrpc\":\"2.0\",\"id\":" + Json.encode(brokenRequestId) + ",\"method\":\"bla\"}," + "{\"jsonrpc\":\"2.0\",\"id\":" + Json.encode(netVersionRequestId) + ",\"method\":\"net_version\"}]");
    try (final Response resp = client.newCall(buildPostRequest(body)).execute()) {
        assertThat(resp.code()).isEqualTo(200);
        // Check general format of result
        final JsonArray json = new JsonArray(resp.body().string());
        final int requestCount = 3;
        assertThat(json.size()).isEqualTo(requestCount);
        final Map<Integer, JsonObject> responses = new HashMap<>();
        for (int i = 0; i < requestCount; ++i) {
            final JsonObject response = json.getJsonObject(i);
            responses.put(response.getInteger("id"), response);
        }
        // Check result web3_clientVersion
        final JsonObject jsonClientVersion = responses.get(clientVersionRequestId);
        testHelper.assertValidJsonRpcResult(jsonClientVersion, clientVersionRequestId);
        assertThat(jsonClientVersion.getString("result")).isEqualTo(CLIENT_VERSION);
        // Check result unknown method
        final JsonObject jsonError = responses.get(brokenRequestId);
        final JsonRpcError expectedError = JsonRpcError.METHOD_NOT_FOUND;
        testHelper.assertValidJsonRpcError(jsonError, brokenRequestId, expectedError.getCode(), expectedError.getMessage());
        // Check result net_version
        final JsonObject jsonNetVersion = responses.get(netVersionRequestId);
        testHelper.assertValidJsonRpcResult(jsonNetVersion, netVersionRequestId);
        assertThat(jsonNetVersion.getString("result")).isEqualTo(String.valueOf(CHAIN_ID));
    }
}
Also used : Response(okhttp3.Response) JsonArray(io.vertx.core.json.JsonArray) BigInteger(java.math.BigInteger) HashMap(java.util.HashMap) JsonObject(io.vertx.core.json.JsonObject) JsonRpcError(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 5 with JsonRpcError

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError in project besu by hyperledger.

the class JsonRpcHttpServiceTest method missingMethodField.

@Test
public void missingMethodField() throws Exception {
    final Integer id = 2;
    final RequestBody body = RequestBody.create(JSON, "{\"jsonrpc\":\"2.0\",\"id\":" + Json.encode(id) + "}");
    try (final Response resp = client.newCall(buildPostRequest(body)).execute()) {
        assertThat(resp.code()).isEqualTo(400);
        final JsonObject json = new JsonObject(resp.body().string());
        final JsonRpcError expectedError = JsonRpcError.INVALID_REQUEST;
        testHelper.assertValidJsonRpcError(json, id, expectedError.getCode(), expectedError.getMessage());
    }
}
Also used : BigInteger(java.math.BigInteger) Response(okhttp3.Response) JsonObject(io.vertx.core.json.JsonObject) JsonRpcError(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Aggregations

JsonRpcError (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError)27 JsonObject (io.vertx.core.json.JsonObject)23 Test (org.junit.Test)23 RequestBody (okhttp3.RequestBody)22 Response (okhttp3.Response)22 JsonRpcMethod (org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod)4 JsonArray (io.vertx.core.json.JsonArray)3 BigInteger (java.math.BigInteger)3 JsonRpcRequest (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest)3 JsonRpcRequestContext (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext)3 Span (io.opentelemetry.api.trace.Span)2 HashMap (java.util.HashMap)2 JsonRpcRequestId (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestId)2 JsonRpcErrorResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)2 TransactionProcessingResult (org.hyperledger.besu.ethereum.processing.TransactionProcessingResult)2 TransactionInvalidReason (org.hyperledger.besu.ethereum.transaction.TransactionInvalidReason)2 InetSocketAddress (java.net.InetSocketAddress)1 Address (org.hyperledger.besu.datatypes.Address)1 Hash (org.hyperledger.besu.datatypes.Hash)1 EnclaveClientException (org.hyperledger.besu.enclave.EnclaveClientException)1