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