Search in sources :

Example 1 with DebugAccountRangeAtResult

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.DebugAccountRangeAtResult 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

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