Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
[PAN-2946] - changes in core JSON-RPC method to support ReTestEth (#1815
Browse files Browse the repository at this point in the history
)

* [PAN-2946] - changes in core JSON-RPC method to support ReTestEth

Some of the methods need to have a changeable reference to stuff like
blockchainqueries.  For the impacted methods the solution is to wrap them
in a Supplier<> interface.  This includes new constructors for re-used methods.

Also include the debug_accountRangeAt method as it's namespaced as debug.

Some features needed for retesteth are flag controlled to preserve current behaviors.
  • Loading branch information
Danno Ferrin authored Aug 2, 2019
1 parent b3e352a commit 7eac896
Show file tree
Hide file tree
Showing 34 changed files with 893 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ public IbftGetValidatorsByBlockNumber(

@Override
protected BlockParameter blockParameter(final JsonRpcRequest request) {
return parameters().required(request.getParams(), 0, BlockParameter.class);
return getParameters().required(request.getParams(), 0, BlockParameter.class);
}

@Override
protected Object resultByBlockNumber(final JsonRpcRequest request, final long blockNumber) {
final Optional<BlockHeader> blockHeader =
blockchainQueries().getBlockHeaderByNumber(blockNumber);
getBlockchainQueries().getBlockHeaderByNumber(blockNumber);
LOG.trace("Received RPC rpcName={} block={}", getName(), blockNumber);
return blockHeader
.map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ private JsonRpcResponse process(final JsonObject requestJson, final Optional<Use
try (final TimingContext ignored = requestTimer.labels(request.getMethod()).startTimer()) {
return method.response(request);
} catch (final InvalidJsonRpcParameters e) {
LOG.debug(e);
LOG.debug("Invalid Params", e);
return errorResponse(id, JsonRpcError.INVALID_PARAMS);
} catch (final RuntimeException e) {
LOG.error("Error processing JSON-RPC request", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.AdminNodeInfo;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.AdminPeers;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.AdminRemovePeer;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.DebugAccountRange;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.DebugMetrics;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.DebugStorageRangeAt;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.DebugTraceBlock;
Expand Down Expand Up @@ -262,6 +263,7 @@ public Map<String, JsonRpcMethod> methods(
enabledMethods,
new DebugTraceTransaction(
blockchainQueries, new TransactionTracer(blockReplay), parameter),
new DebugAccountRange(parameter, blockchainQueries),
new DebugStorageRangeAt(parameter, blockchainQueries, blockReplay),
new DebugMetrics(metricsSystem),
new DebugTraceBlock(
Expand Down Expand Up @@ -352,7 +354,7 @@ blockchainQueries, new TransactionTracer(blockReplay), parameter),
return enabledMethods;
}

private void addMethods(
public static void addMethods(
final Map<String, JsonRpcMethod> methods, final JsonRpcMethod... rpcMethods) {
for (final JsonRpcMethod rpcMethod : rpcMethods) {
methods.put(rpcMethod.getName(), rpcMethod);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,22 @@
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse;

import java.util.OptionalLong;
import java.util.function.Supplier;

import com.google.common.base.Suppliers;

public abstract class AbstractBlockParameterMethod implements JsonRpcMethod {

private final BlockchainQueries blockchainQueries;
private final Supplier<BlockchainQueries> blockchainQueries;
private final JsonRpcParameter parameters;

protected AbstractBlockParameterMethod(
final BlockchainQueries blockchainQueries, final JsonRpcParameter parameters) {
this(Suppliers.ofInstance(blockchainQueries), parameters);
}

protected AbstractBlockParameterMethod(
final Supplier<BlockchainQueries> blockchainQueries, final JsonRpcParameter parameters) {
this.blockchainQueries = blockchainQueries;
this.parameters = parameters;
}
Expand All @@ -36,11 +44,11 @@ protected AbstractBlockParameterMethod(

protected abstract Object resultByBlockNumber(JsonRpcRequest request, long blockNumber);

protected BlockchainQueries blockchainQueries() {
return blockchainQueries;
protected BlockchainQueries getBlockchainQueries() {
return blockchainQueries.get();
}

protected JsonRpcParameter parameters() {
protected JsonRpcParameter getParameters() {
return parameters;
}

Expand All @@ -51,7 +59,7 @@ protected Object pendingResult(final JsonRpcRequest request) {
}

protected Object latestResult(final JsonRpcRequest request) {
return resultByBlockNumber(request, blockchainQueries.headBlockNumber());
return resultByBlockNumber(request, blockchainQueries.get().headBlockNumber());
}

protected Object findResultByParamType(final JsonRpcRequest request) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Copyright 2019 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods;

import tech.pegasys.pantheon.ethereum.core.Account;
import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.ethereum.core.Hash;
import tech.pegasys.pantheon.ethereum.core.MutableWorldState;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.BlockParameterOrBlockHash;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.queries.BlockWithMetadata;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.queries.BlockchainQueries;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.results.DebugAccountRangeAtResult;
import tech.pegasys.pantheon.util.bytes.Bytes32;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import com.google.common.base.Suppliers;

public class DebugAccountRange implements JsonRpcMethod {

private final JsonRpcParameter parameters;
private final Supplier<BlockchainQueries> blockchainQueries;

public DebugAccountRange(
final JsonRpcParameter parameters, final BlockchainQueries blockchainQueries) {
this(parameters, Suppliers.ofInstance(blockchainQueries));
}

public DebugAccountRange(
final JsonRpcParameter parameters, final Supplier<BlockchainQueries> blockchainQueries) {
this.parameters = parameters;
this.blockchainQueries = blockchainQueries;
}

@Override
public String getName() {
// TODO(shemnon) 5229b899 is the last stable commit of retesteth, after this they rename the
// method to just "debug_accountRange". Once the tool is stable we will support the new name.
return "debug_accountRangeAt";
}

@Override
public JsonRpcResponse response(final JsonRpcRequest request) {
final Object[] params = request.getParams();
final BlockParameterOrBlockHash blockParameterOrBlockHash =
parameters.required(params, 0, BlockParameterOrBlockHash.class);
final String addressHash = parameters.required(params, 2, String.class);
final int maxResults = parameters.required(params, 3, Integer.TYPE);

final Optional<Hash> blockHashOptional = hashFromParameter(blockParameterOrBlockHash);
if (blockHashOptional.isEmpty()) {
return emptyResponse(request);
}
final Hash blockHash = blockHashOptional.get();
final Optional<BlockHeader> blockHeaderOptional =
blockchainQueries.get().blockByHash(blockHash).map(BlockWithMetadata::getHeader);
if (blockHeaderOptional.isEmpty()) {
return emptyResponse(request);
}

// TODO deal with mid-block locations

final Optional<MutableWorldState> state =
blockchainQueries.get().getWorldState(blockHeaderOptional.get().getNumber());

if (state.isEmpty()) {
return emptyResponse(request);
} else {
final List<Account> 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(
request.getId(),
new DebugAccountRangeAtResult(
accounts.stream()
.collect(
Collectors.toMap(
account -> account.getAddressHash().toString(),
account -> account.getAddress().toString())),
nextKey.toString()));
}
}

private Optional<Hash> hashFromParameter(final BlockParameterOrBlockHash blockParameter) {
if (blockParameter.isEarliest()) {
return blockchainQueries.get().getBlockHashByNumber(0);
} else if (blockParameter.isLatest() || blockParameter.isPending()) {
return blockchainQueries
.get()
.latestBlockWithTxHashes()
.map(block -> block.getHeader().getHash());
} else if (blockParameter.isNumeric()) {
return blockchainQueries.get().getBlockHashByNumber(blockParameter.getNumber().getAsLong());
} else {
return blockParameter.getHash();
}
}

private JsonRpcSuccessResponse emptyResponse(final JsonRpcRequest request) {
return new JsonRpcSuccessResponse(
request.getId(), new DebugAccountRangeAtResult(Collections.emptyNavigableMap(), null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,56 @@
import tech.pegasys.pantheon.ethereum.core.Account;
import tech.pegasys.pantheon.ethereum.core.AccountStorageEntry;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.ethereum.core.Hash;
import tech.pegasys.pantheon.ethereum.core.MutableWorldState;
import tech.pegasys.pantheon.ethereum.jsonrpc.RpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.BlockParameterOrBlockHash;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.processor.BlockReplay;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.queries.BlockWithMetadata;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.queries.BlockchainQueries;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.queries.TransactionWithMetadata;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.results.DebugStorageRangeAtResult;
import tech.pegasys.pantheon.util.bytes.Bytes32;

import java.util.Collections;
import java.util.NavigableMap;
import java.util.Optional;
import java.util.function.Supplier;

import com.google.common.base.Suppliers;

public class DebugStorageRangeAt implements JsonRpcMethod {

private final JsonRpcParameter parameters;
private final BlockchainQueries blockchainQueries;
private final BlockReplay blockReplay;
private final Supplier<BlockchainQueries> blockchainQueries;
private final Supplier<BlockReplay> blockReplay;
private final boolean shortValues;

public DebugStorageRangeAt(
final JsonRpcParameter parameters,
final BlockchainQueries blockchainQueries,
final BlockReplay blockReplay) {
this(
parameters,
Suppliers.ofInstance(blockchainQueries),
Suppliers.ofInstance(blockReplay),
false);
}

public DebugStorageRangeAt(
final JsonRpcParameter parameters,
final Supplier<BlockchainQueries> blockchainQueries,
final Supplier<BlockReplay> blockReplay,
final boolean shortValues) {
this.parameters = parameters;
this.blockchainQueries = blockchainQueries;
this.blockReplay = blockReplay;
this.shortValues = shortValues;
}

@Override
Expand All @@ -53,25 +74,63 @@ public String getName() {

@Override
public JsonRpcResponse response(final JsonRpcRequest request) {
final Hash blockHash = parameters.required(request.getParams(), 0, Hash.class);
final BlockParameterOrBlockHash blockParameterOrBlockHash =
parameters.required(request.getParams(), 0, BlockParameterOrBlockHash.class);
final int transactionIndex = parameters.required(request.getParams(), 1, Integer.class);
final Address accountAddress = parameters.required(request.getParams(), 2, Address.class);
final Hash startKey = parameters.required(request.getParams(), 3, Hash.class);
final Hash startKey =
Hash.fromHexStringLenient(parameters.required(request.getParams(), 3, String.class));
final int limit = parameters.required(request.getParams(), 4, Integer.class);

final Optional<Hash> blockHashOptional = hashFromParameter(blockParameterOrBlockHash);
if (blockHashOptional.isEmpty()) {
return emptyResponse(request);
}
final Hash blockHash = blockHashOptional.get();
final Optional<BlockHeader> blockHeaderOptional =
blockchainQueries.get().blockByHash(blockHash).map(BlockWithMetadata::getHeader);
if (blockHeaderOptional.isEmpty()) {
return emptyResponse(request);
}

final Optional<TransactionWithMetadata> optional =
blockchainQueries.transactionByBlockHashAndIndex(blockHash, transactionIndex);
blockchainQueries.get().transactionByBlockHashAndIndex(blockHash, transactionIndex);

return optional
.map(
transactionWithMetadata ->
(blockReplay
.get()
.afterTransactionInBlock(
blockHash,
transactionWithMetadata.getTransaction().hash(),
(transaction, blockHeader, blockchain, worldState, transactionProcessor) ->
extractStorageAt(request, accountAddress, startKey, limit, worldState))
.orElseGet(() -> new JsonRpcSuccessResponse(request.getId(), null))))
.orElseGet(() -> new JsonRpcSuccessResponse(request.getId(), null));
.orElseGet(() -> emptyResponse(request))))
.orElseGet(
() ->
blockchainQueries
.get()
.getWorldState(blockHeaderOptional.get().getNumber())
.map(
worldState ->
extractStorageAt(request, accountAddress, startKey, limit, worldState))
.orElseGet(() -> emptyResponse(request)));
}

private Optional<Hash> hashFromParameter(final BlockParameterOrBlockHash blockParameter) {
if (blockParameter.isEarliest()) {
return blockchainQueries.get().getBlockHashByNumber(0);
} else if (blockParameter.isLatest() || blockParameter.isPending()) {
return blockchainQueries
.get()
.latestBlockWithTxHashes()
.map(block -> block.getHeader().getHash());
} else if (blockParameter.isNumeric()) {
return blockchainQueries.get().getBlockHashByNumber(blockParameter.getNumber().getAsLong());
} else {
return blockParameter.getHash();
}
}

private JsonRpcSuccessResponse extractStorageAt(
Expand All @@ -90,6 +149,12 @@ private JsonRpcSuccessResponse extractStorageAt(
entries.remove(nextKey);
}
return new JsonRpcSuccessResponse(
request.getId(), new DebugStorageRangeAtResult(entries, nextKey));
request.getId(), new DebugStorageRangeAtResult(entries, nextKey, shortValues));
}

private JsonRpcSuccessResponse emptyResponse(final JsonRpcRequest request) {
return new JsonRpcSuccessResponse(
request.getId(),
new DebugStorageRangeAtResult(Collections.emptyNavigableMap(), null, shortValues));
}
}
Loading

0 comments on commit 7eac896

Please sign in to comment.