Search in sources :

Example 1 with JsonRpcRequest

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

the class WebSocketRequestHandlerTest method handlerBatchRequestDeliversResponseSuccessfully.

@Test
public void handlerBatchRequestDeliversResponseSuccessfully(final TestContext context) {
    final Async async = context.async();
    final JsonObject requestJson = new JsonObject().put("id", 1).put("method", "eth_x");
    final JsonArray arrayJson = new JsonArray(List.of(requestJson, requestJson));
    final JsonRpcRequest requestBody = requestJson.mapTo(WebSocketRpcRequest.class);
    final JsonRpcRequestContext expectedRequest = new JsonRpcRequestContext(requestBody);
    final JsonRpcSuccessResponse expectedSingleResponse = new JsonRpcSuccessResponse(requestBody.getId(), null);
    final JsonArray expectedBatchResponse = new JsonArray(List.of(expectedSingleResponse, expectedSingleResponse));
    when(jsonRpcMethodMock.response(eq(expectedRequest))).thenReturn(expectedSingleResponse);
    when(websocketMock.writeFrame(argThat(this::isFinalFrame))).then(completeOnLastFrame(async));
    handler.handle(websocketMock, arrayJson.toString());
    async.awaitSuccess(WebSocketRequestHandlerTest.VERTX_AWAIT_TIMEOUT_MILLIS);
    // can verify only after async not before
    verify(websocketMock).writeFrame(argThat(isFrameWithText(Json.encode(expectedBatchResponse))));
    verify(websocketMock).writeFrame(argThat(this::isFinalFrame));
    verify(jsonRpcMethodMock, Mockito.times(2)).response(eq(expectedRequest));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) Async(io.vertx.ext.unit.Async) JsonObject(io.vertx.core.json.JsonObject) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) Test(org.junit.Test)

Example 2 with JsonRpcRequest

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

the class QbftGetValidatorsByBlockNumberTest method blockParameterIsParameter0.

@Test
public void blockParameterIsParameter0() {
    request = new JsonRpcRequestContext(new JsonRpcRequest("?", "ignore", new String[] { "0x1245" }));
    BlockParameter blockParameter = method.blockParameter(request);
    assertThat(blockParameter.getNumber()).isPresent();
    assertThat(blockParameter.getNumber().get()).isEqualTo(0x1245);
}
Also used : BlockParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameter) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) JsonRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest) Test(org.junit.Test)

Example 3 with JsonRpcRequest

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

the class CliqueGetSignersAtHashTest method failsWhenNoParam.

@Test
@SuppressWarnings("unchecked")
public void failsWhenNoParam() {
    final JsonRpcRequestContext request = new JsonRpcRequestContext(new JsonRpcRequest("2.0", "clique_getSignersAtHash", new Object[] {}));
    final Throwable thrown = AssertionsForClassTypes.catchThrowable(() -> method.response(request));
    assertThat(thrown).isInstanceOf(InvalidJsonRpcParameters.class).hasMessage("Missing required json rpc parameter at index 0");
}
Also used : JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) JsonRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest) InvalidJsonRpcParameters(org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters) Test(org.junit.Test)

Example 4 with JsonRpcRequest

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

the class CliqueGetSignersAtHashTest method returnsValidatorsForBlockHash.

@Test
@SuppressWarnings("unchecked")
public void returnsValidatorsForBlockHash() {
    final JsonRpcRequestContext request = new JsonRpcRequestContext(new JsonRpcRequest("2.0", "clique_getSignersAtHash", new Object[] { BLOCK_HASH }));
    when(blockchainQueries.blockByHash(Hash.fromHexString(BLOCK_HASH))).thenReturn(Optional.of(blockWithMetadata));
    when(blockWithMetadata.getHeader()).thenReturn(blockHeader);
    when(validatorProvider.getValidatorsAfterBlock(blockHeader)).thenReturn(validators);
    final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) method.response(request);
    assertThat(response.getResult()).isEqualTo(validatorsAsStrings);
}
Also used : JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) JsonRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) Test(org.junit.Test)

Example 5 with JsonRpcRequest

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

the class CliqueGetSignersTest method returnsValidatorsForBlockNumber.

@Test
@SuppressWarnings("unchecked")
public void returnsValidatorsForBlockNumber() {
    final JsonRpcRequestContext request = new JsonRpcRequestContext(new JsonRpcRequest("2.0", "clique_getSigners", new Object[] { "0x2EC88B" }));
    when(blockchainQueries.blockByNumber(3065995L)).thenReturn(Optional.of(blockWithMetadata));
    when(blockWithMetadata.getHeader()).thenReturn(blockHeader);
    when(validatorProvider.getValidatorsAfterBlock(blockHeader)).thenReturn(validators);
    final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) method.response(request);
    assertThat(response.getResult()).isEqualTo(validatorAsStrings);
}
Also used : JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) JsonRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) Test(org.junit.Test)

Aggregations

JsonRpcRequest (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest)196 JsonRpcRequestContext (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext)188 Test (org.junit.Test)159 JsonRpcSuccessResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse)84 JsonRpcResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse)74 JsonRpcErrorResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)46 InvalidJsonRpcParameters (org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters)29 JsonObject (io.vertx.core.json.JsonObject)27 Test (org.junit.jupiter.api.Test)16 HashMap (java.util.HashMap)13 Async (io.vertx.ext.unit.Async)9 Address (org.hyperledger.besu.datatypes.Address)9 Transaction (org.hyperledger.besu.ethereum.core.Transaction)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8 List (java.util.List)7 Hash (org.hyperledger.besu.datatypes.Hash)7 PrivacyGroup (org.hyperledger.besu.enclave.types.PrivacyGroup)7 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)6 MethodSource (org.junit.jupiter.params.provider.MethodSource)6 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6