PAN-2711 - Don't require 'to' in 'blocks' queries (#1464)

{blocks(from:7777777) {number}} should work, instead it is requiring a to: argument when it should be implied as latest
Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/2/head
Danno Ferrin 6 years ago committed by GitHub
parent 2a64902bd2
commit 11f28ac7bd
  1. 15
      ethereum/graphqlrpc/src/main/java/tech/pegasys/pantheon/ethereum/graphqlrpc/GraphQLDataFetchers.java
  2. 7
      ethereum/graphqlrpc/src/test/java/tech/pegasys/pantheon/ethereum/graphqlrpc/EthGraphQLRpcHttpBySpecTest.java
  3. 19
      ethereum/graphqlrpc/src/test/resources/tech/pegasys/pantheon/ethereum/graphqlrpc/graphql_blocks_noTo.json

@ -115,19 +115,24 @@ public class GraphQLDataFetchers {
DataFetcher<List<NormalBlockAdapter>> getRangeBlockDataFetcher() {
return dataFetchingEnvironment -> {
final BlockchainQuery blockchainQuery =
((GraphQLDataFetcherContext) dataFetchingEnvironment.getContext()).getBlockchainQuery();
final long from = dataFetchingEnvironment.getArgument("from");
final long to = dataFetchingEnvironment.getArgument("to");
final long to;
if (dataFetchingEnvironment.containsArgument("to")) {
to = dataFetchingEnvironment.getArgument("to");
} else {
to = blockchainQuery.latestBlock().map(block -> block.getHeader().getNumber()).orElse(0L);
}
if (from > to) {
throw new GraphQLRpcException(GraphQLRpcError.INVALID_PARAMS);
}
final BlockchainQuery blockchain =
((GraphQLDataFetcherContext) dataFetchingEnvironment.getContext()).getBlockchainQuery();
final List<NormalBlockAdapter> results = new ArrayList<>();
for (long i = from; i <= to; i++) {
final Optional<BlockWithMetadata<TransactionWithMetadata, Hash>> block =
blockchain.blockByNumber(i);
blockchainQuery.blockByNumber(i);
block.ifPresent(e -> results.add(new NormalBlockAdapter(e)));
}
return results;

@ -87,6 +87,7 @@ public class EthGraphQLRpcHttpBySpecTest extends AbstractEthGraphQLRpcHttpServic
specs.add("eth_getLogs_matchTopic");
specs.add("graphql_blocks_noTo");
specs.add("graphql_pending");
return specs;
}
@ -108,14 +109,14 @@ public class EthGraphQLRpcHttpBySpecTest extends AbstractEthGraphQLRpcHttpServic
importBlocks(1, BLOCKS.size());
try (final Response resp = client.newCall(request).execute()) {
final int expectedStatusCode = spec.getInteger("statusCode");
assertThat(resp.code()).isEqualTo(expectedStatusCode);
final JsonObject expectedRespBody = spec.getJsonObject("response");
final String resultStr = resp.body().string();
final JsonObject result = new JsonObject(resultStr);
assertThat(result).isEqualTo(expectedRespBody);
final int expectedStatusCode = spec.getInteger("statusCode");
assertThat(resp.code()).isEqualTo(expectedStatusCode);
}
}

@ -0,0 +1,19 @@
{
"request": "{ blocks(from:30) { number } }",
"response": {
"data": {
"blocks": [
{
"number": 30
},
{
"number": 31
},
{
"number": 32
}
]
}
},
"statusCode": 200
}
Loading…
Cancel
Save