Search in sources :

Example 1 with BlockParameterOrBlockHash

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

the class DebugStorageRangeAt method response.

@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
    final BlockParameterOrBlockHash blockParameterOrBlockHash = requestContext.getRequiredParameter(0, BlockParameterOrBlockHash.class);
    final int transactionIndex = requestContext.getRequiredParameter(1, Integer.class);
    final Address accountAddress = requestContext.getRequiredParameter(2, Address.class);
    final Hash startKey = Hash.fromHexStringLenient(requestContext.getRequiredParameter(3, String.class));
    final int limit = requestContext.getRequiredParameter(4, Integer.class);
    final Optional<Hash> blockHashOptional = hashFromParameter(blockParameterOrBlockHash);
    if (blockHashOptional.isEmpty()) {
        return emptyResponse(requestContext);
    }
    final Hash blockHash = blockHashOptional.get();
    final Optional<BlockHeader> blockHeaderOptional = blockchainQueries.get().blockByHash(blockHash).map(BlockWithMetadata::getHeader);
    if (blockHeaderOptional.isEmpty()) {
        return emptyResponse(requestContext);
    }
    final Optional<TransactionWithMetadata> optional = blockchainQueries.get().transactionByBlockHashAndIndex(blockHash, transactionIndex);
    return optional.map(transactionWithMetadata -> (blockReplay.get().afterTransactionInBlock(blockHash, transactionWithMetadata.getTransaction().getHash(), (transaction, blockHeader, blockchain, worldState, transactionProcessor) -> extractStorageAt(requestContext, accountAddress, startKey, limit, worldState)).orElseGet(() -> emptyResponse(requestContext)))).orElseGet(() -> blockchainQueries.get().getWorldState(blockHeaderOptional.get().getNumber()).map(worldState -> extractStorageAt(requestContext, accountAddress, startKey, limit, worldState)).orElseGet(() -> emptyResponse(requestContext)));
}
Also used : WorldState(org.hyperledger.besu.evm.worldstate.WorldState) Account(org.hyperledger.besu.evm.account.Account) AccountStorageEntry(org.hyperledger.besu.evm.account.AccountStorageEntry) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) JsonRpcResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse) BlockchainQueries(org.hyperledger.besu.ethereum.api.query.BlockchainQueries) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) NavigableMap(java.util.NavigableMap) Address(org.hyperledger.besu.datatypes.Address) Supplier(java.util.function.Supplier) RpcMethod(org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod) BlockWithMetadata(org.hyperledger.besu.ethereum.api.query.BlockWithMetadata) TransactionWithMetadata(org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) BlockParameterOrBlockHash(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameterOrBlockHash) Optional(java.util.Optional) Suppliers(com.google.common.base.Suppliers) DebugStorageRangeAtResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.DebugStorageRangeAtResult) Collections(java.util.Collections) Bytes32(org.apache.tuweni.bytes.Bytes32) Hash(org.hyperledger.besu.datatypes.Hash) BlockReplay(org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockReplay) Address(org.hyperledger.besu.datatypes.Address) TransactionWithMetadata(org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata) BlockParameterOrBlockHash(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameterOrBlockHash) BlockWithMetadata(org.hyperledger.besu.ethereum.api.query.BlockWithMetadata) BlockParameterOrBlockHash(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameterOrBlockHash) Hash(org.hyperledger.besu.datatypes.Hash) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader)

Example 2 with BlockParameterOrBlockHash

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

the class AbstractBlockParameterOrBlockHashMethod method handleParamTypes.

protected Object handleParamTypes(final JsonRpcRequestContext requestContext) {
    final BlockParameterOrBlockHash blockParameterOrBlockHash = blockParameterOrBlockHash(requestContext);
    final Object result;
    if (blockParameterOrBlockHash.isLatest()) {
        result = latestResult(requestContext);
    } else if (blockParameterOrBlockHash.isPending()) {
        result = pendingResult(requestContext);
    } else if (blockParameterOrBlockHash.isNumeric() || blockParameterOrBlockHash.isEarliest()) {
        final OptionalLong blockNumber = blockParameterOrBlockHash.getNumber();
        if (blockNumber.isEmpty() || blockNumber.getAsLong() < 0) {
            return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.INVALID_PARAMS);
        } else if (blockNumber.getAsLong() > getBlockchainQueries().headBlockNumber()) {
            return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.BLOCK_NOT_FOUND);
        }
        result = resultByBlockHash(requestContext, blockchainQueries.get().getBlockHashByNumber(blockNumber.getAsLong()).orElse(Hash.EMPTY));
    } else {
        Optional<Hash> blockHash = blockParameterOrBlockHash.getHash();
        if (blockHash.isEmpty()) {
            return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.INVALID_PARAMS);
        }
        if (Boolean.TRUE.equals(blockParameterOrBlockHash.getRequireCanonical()) && !getBlockchainQueries().blockIsOnCanonicalChain(blockHash.get())) {
            return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.JSON_RPC_NOT_CANONICAL_ERROR);
        }
        result = resultByBlockHash(requestContext, blockHash.get());
    }
    return result;
}
Also used : OptionalLong(java.util.OptionalLong) BlockParameterOrBlockHash(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameterOrBlockHash) BlockParameterOrBlockHash(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameterOrBlockHash) Hash(org.hyperledger.besu.datatypes.Hash) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)

Example 3 with BlockParameterOrBlockHash

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

the class DebugAccountRange method response.

@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
    final BlockParameterOrBlockHash blockParameterOrBlockHash = requestContext.getRequiredParameter(0, BlockParameterOrBlockHash.class);
    final String addressHash = requestContext.getRequiredParameter(2, String.class);
    final int maxResults = requestContext.getRequiredParameter(3, Integer.TYPE);
    final Optional<Hash> blockHashOptional = hashFromParameter(blockParameterOrBlockHash);
    if (blockHashOptional.isEmpty()) {
        return emptyResponse(requestContext);
    }
    final Hash blockHash = blockHashOptional.get();
    final Optional<BlockHeader> blockHeaderOptional = blockchainQueries.get().blockByHash(blockHash).map(BlockWithMetadata::getHeader);
    if (blockHeaderOptional.isEmpty()) {
        return emptyResponse(requestContext);
    }
    // TODO deal with mid-block locations
    final Optional<WorldState> state = blockchainQueries.get().getWorldState(blockHeaderOptional.get().getNumber());
    if (state.isEmpty()) {
        return emptyResponse(requestContext);
    } else {
        final List<StreamableAccount> accounts = state.get().streamAccounts(Bytes32.fromHexStringLenient(addressHash), maxResults + 1).collect(Collectors.toList());
        Bytes32 nextKey = Bytes32.ZERO;
        if (accounts.size() == maxResults + 1) {
            nextKey = accounts.get(maxResults).getAddressHash();
            accounts.remove(maxResults);
        }
        return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), new DebugAccountRangeAtResult(accounts.stream().collect(Collectors.toMap(account -> account.getAddressHash().toString(), account -> account.getAddress().orElse(Address.ZERO).toString())), nextKey.toString()));
    }
}
Also used : WorldState(org.hyperledger.besu.evm.worldstate.WorldState) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) JsonRpcResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse) BlockchainQueries(org.hyperledger.besu.ethereum.api.query.BlockchainQueries) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) Address(org.hyperledger.besu.datatypes.Address) DebugAccountRangeAtResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.DebugAccountRangeAtResult) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) StreamableAccount(org.hyperledger.besu.evm.worldstate.WorldState.StreamableAccount) List(java.util.List) BlockWithMetadata(org.hyperledger.besu.ethereum.api.query.BlockWithMetadata) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) BlockParameterOrBlockHash(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameterOrBlockHash) Optional(java.util.Optional) Suppliers(com.google.common.base.Suppliers) Collections(java.util.Collections) Bytes32(org.apache.tuweni.bytes.Bytes32) Hash(org.hyperledger.besu.datatypes.Hash) StreamableAccount(org.hyperledger.besu.evm.worldstate.WorldState.StreamableAccount) WorldState(org.hyperledger.besu.evm.worldstate.WorldState) BlockWithMetadata(org.hyperledger.besu.ethereum.api.query.BlockWithMetadata) BlockParameterOrBlockHash(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameterOrBlockHash) Hash(org.hyperledger.besu.datatypes.Hash) Bytes32(org.apache.tuweni.bytes.Bytes32) DebugAccountRangeAtResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.DebugAccountRangeAtResult) BlockParameterOrBlockHash(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameterOrBlockHash) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse)

Aggregations

Hash (org.hyperledger.besu.datatypes.Hash)3 BlockParameterOrBlockHash (org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameterOrBlockHash)3 Suppliers (com.google.common.base.Suppliers)2 Collections (java.util.Collections)2 Optional (java.util.Optional)2 Supplier (java.util.function.Supplier)2 Bytes32 (org.apache.tuweni.bytes.Bytes32)2 Address (org.hyperledger.besu.datatypes.Address)2 JsonRpcRequestContext (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext)2 JsonRpcResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse)2 JsonRpcSuccessResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse)2 BlockWithMetadata (org.hyperledger.besu.ethereum.api.query.BlockWithMetadata)2 BlockchainQueries (org.hyperledger.besu.ethereum.api.query.BlockchainQueries)2 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)2 WorldState (org.hyperledger.besu.evm.worldstate.WorldState)2 List (java.util.List)1 NavigableMap (java.util.NavigableMap)1 OptionalLong (java.util.OptionalLong)1 Collectors (java.util.stream.Collectors)1 RpcMethod (org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod)1