This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PAN-2946] - changes in core JSON-RPC method to support ReTestEth (#1815
) * [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
Showing
34 changed files
with
893 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
.../main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/DebugAccountRange.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.