Search in sources :

Example 1 with FilterParameter

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

the class SubscriptionRequestMapper method mapPrivateSubscribeRequest.

public PrivateSubscribeRequest mapPrivateSubscribeRequest(final JsonRpcRequestContext jsonRpcRequestContext, final String privacyUserId) throws InvalidSubscriptionRequestException {
    try {
        final WebSocketRpcRequest webSocketRpcRequestBody = validateRequest(jsonRpcRequestContext);
        final String privacyGroupId = webSocketRpcRequestBody.getRequiredParameter(0, String.class);
        final SubscriptionType subscriptionType = webSocketRpcRequestBody.getRequiredParameter(1, SubscriptionType.class);
        switch(subscriptionType) {
            case LOGS:
                {
                    final FilterParameter filterParameter = jsonRpcRequestContext.getRequiredParameter(2, FilterParameter.class);
                    return new PrivateSubscribeRequest(SubscriptionType.LOGS, filterParameter, null, webSocketRpcRequestBody.getConnectionId(), privacyGroupId, privacyUserId);
                }
            default:
                throw new InvalidSubscriptionRequestException("Invalid subscribe request. Invalid private subscription type.");
        }
    } catch (final InvalidSubscriptionRequestException e) {
        throw e;
    } catch (final Exception e) {
        throw new InvalidSubscriptionRequestException("Error parsing subscribe request", e);
    }
}
Also used : WebSocketRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.methods.WebSocketRpcRequest) FilterParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.FilterParameter)

Example 2 with FilterParameter

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

the class EthGetLogs method response.

@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
    final FilterParameter filter = requestContext.getRequiredParameter(0, FilterParameter.class);
    if (!filter.isValid()) {
        return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.INVALID_PARAMS);
    }
    final List<LogWithMetadata> matchingLogs = filter.getBlockHash().map(blockHash -> blockchain.matchingLogs(blockHash, filter.getLogsQuery(), requestContext::isAlive)).orElseGet(() -> {
        final long fromBlockNumber = filter.getFromBlock().getNumber().orElse(0L);
        final long toBlockNumber = filter.getToBlock().getNumber().orElse(blockchain.headBlockNumber());
        return blockchain.matchingLogs(fromBlockNumber, toBlockNumber, filter.getLogsQuery(), requestContext::isAlive);
    });
    return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), new LogsResult(matchingLogs));
}
Also used : LogsResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.LogsResult) List(java.util.List) RpcMethod(org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod) JsonRpcError(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError) FilterParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.FilterParameter) JsonRpcResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse) BlockchainQueries(org.hyperledger.besu.ethereum.api.query.BlockchainQueries) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse) LogWithMetadata(org.hyperledger.besu.ethereum.core.LogWithMetadata) LogWithMetadata(org.hyperledger.besu.ethereum.core.LogWithMetadata) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) FilterParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.FilterParameter) LogsResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.LogsResult) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)

Example 3 with FilterParameter

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

the class EthNewFilter method response.

@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
    final FilterParameter filter = requestContext.getRequiredParameter(0, FilterParameter.class);
    if (!filter.isValid()) {
        return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.INVALID_PARAMS);
    }
    final String logFilterId = filterManager.installLogFilter(filter.getFromBlock(), filter.getToBlock(), filter.getLogsQuery());
    return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), logFilterId);
}
Also used : JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) FilterParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.FilterParameter) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)

Example 4 with FilterParameter

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

the class EthNewFilterTest method newFilterWithAddressOnlyParamInstallsExpectedLogFilter.

@Test
public void newFilterWithAddressOnlyParamInstallsExpectedLogFilter() {
    final Address address = Address.fromHexString("0x0");
    final FilterParameter filterParameter = filterParamWithAddressAndTopics(address, null);
    final JsonRpcRequestContext request = ethNewFilter(filterParameter);
    final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(request.getRequest().getId(), "0x1");
    final LogsQuery expectedLogsQuery = new LogsQuery.Builder().address(address).build();
    when(filterManager.installLogFilter(any(), any(), eq(expectedLogsQuery))).thenReturn("0x1");
    final JsonRpcResponse actualResponse = method.response(request);
    assertThat(actualResponse).usingRecursiveComparison().isEqualTo(expectedResponse);
    verify(filterManager).installLogFilter(refEq(BlockParameter.LATEST), refEq(BlockParameter.LATEST), eq(expectedLogsQuery));
}
Also used : JsonRpcResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse) Address(org.hyperledger.besu.datatypes.Address) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) LogsQuery(org.hyperledger.besu.ethereum.api.query.LogsQuery) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) FilterParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.FilterParameter) Test(org.junit.Test)

Example 5 with FilterParameter

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

the class TraceFilter method response.

@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
    final FilterParameter filterParameter = requestContext.getRequiredParameter(0, FilterParameter.class);
    final long fromBlock = resolveBlockNumber(filterParameter.getFromBlock());
    final long toBlock = resolveBlockNumber(filterParameter.getToBlock());
    LOG.trace("Received RPC rpcName={} fromBlock={} toBlock={}", getName(), fromBlock, toBlock);
    final ObjectMapper mapper = new ObjectMapper();
    final ArrayNodeWrapper resultArrayNode = new ArrayNodeWrapper(mapper.createArrayNode(), filterParameter.getAfter(), filterParameter.getCount());
    long currentBlockNumber = fromBlock;
    while (currentBlockNumber <= toBlock && !resultArrayNode.isFull()) {
        Optional<Block> blockByNumber = blockchainQueriesSupplier.get().getBlockchain().getBlockByNumber(currentBlockNumber);
        blockByNumber.ifPresent(block -> resultArrayNode.addAll(traceBlock(block, Optional.of(filterParameter))));
        currentBlockNumber++;
    }
    return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), resultArrayNode.getArrayNode());
}
Also used : ArrayNodeWrapper(org.hyperledger.besu.ethereum.api.util.ArrayNodeWrapper) Block(org.hyperledger.besu.ethereum.core.Block) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) FilterParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.FilterParameter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

FilterParameter (org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.FilterParameter)27 Test (org.junit.Test)21 JsonRpcRequestContext (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext)20 JsonRpcSuccessResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse)11 JsonRpcResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse)9 LogsQuery (org.hyperledger.besu.ethereum.api.query.LogsQuery)8 JsonRpcErrorResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)7 List (java.util.List)5 JsonRpcRequest (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest)5 Address (org.hyperledger.besu.datatypes.Address)4 LogsResult (org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.LogsResult)4 LogWithMetadata (org.hyperledger.besu.ethereum.core.LogWithMetadata)4 Hash (org.hyperledger.besu.datatypes.Hash)3 PrivateLogsSubscription (org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.logs.PrivateLogsSubscription)3 PrivateSubscribeRequest (org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.PrivateSubscribeRequest)3 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)3 RpcMethod (org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod)2 JsonRpcError (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError)2 NewBlockHeadersSubscription (org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.blockheaders.NewBlockHeadersSubscription)2 LogsSubscription (org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.logs.LogsSubscription)2