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()));
}
}
Aggregations