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

PAN-2715 - return block not found reasons in error #1485

Merged
merged 5 commits into from
May 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ DataFetcher<Optional<Bytes32>> getSendRawTransactionDataFetcher() {
transactionPool.addLocalTransaction(transaction);
if (validationResult.isValid()) {
return Optional.of(transaction.hash());
} else {
throw new GraphQLException(GraphQLError.of(validationResult.getInvalidReason()));
}
} catch (final IllegalArgumentException | RLPException e) {
throw new GraphQLException(GraphQLError.INVALID_PARAMS);
}
throw new GraphQLException(GraphQLError.INVALID_PARAMS);
};
}

Expand Down Expand Up @@ -153,8 +154,10 @@ public DataFetcher<Optional<NormalBlockAdapter>> getBlockDataFetcher() {
final Optional<BlockWithMetadata<TransactionWithMetadata, Hash>> block;
if (number != null) {
block = blockchain.blockByNumber(number);
checkArgument(block.isPresent(), "Block number %s was not found", number);
} else if (hash != null) {
block = blockchain.blockByHash(Hash.wrap(hash));
checkArgument(block.isPresent(), "Block hash %s was not found", hash);
} else {
block = blockchain.latestBlock();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package tech.pegasys.pantheon.ethereum.graphql.internal.response;

import tech.pegasys.pantheon.ethereum.mainnet.TransactionValidator.TransactionInvalidReason;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonGetter;

Expand All @@ -21,7 +23,19 @@ public enum GraphQLError {
INVALID_PARAMS(-32602, "Invalid params"),
INTERNAL_ERROR(-32603, "Internal error"),

CHAIN_HEAD_WORLD_STATE_NOT_AVAILABLE(-32008, "Initial sync is still in progress");
// Transaction validation failures
NONCE_TOO_LOW(-32001, "Nonce too low"),
INVALID_TRANSACTION_SIGNATURE(-32002, "Invalid signature"),
INTRINSIC_GAS_EXCEEDS_LIMIT(-32003, "Intrinsic gas exceeds gas limit"),
TRANSACTION_UPFRONT_COST_EXCEEDS_BALANCE(-32004, "Upfront cost exceeds account balance"),
EXCEEDS_BLOCK_GAS_LIMIT(-32005, "Transaction gas limit exceeds block gas limit"),
INCORRECT_NONCE(-32006, "Incorrect nonce"),
TX_SENDER_NOT_AUTHORIZED(-32007, "Sender account not authorized to send transactions"),
CHAIN_HEAD_WORLD_STATE_NOT_AVAILABLE(-32008, "Initial sync is still in progress"),
WRONG_CHAIN_ID(-32000, "Wrong Chain ID in transaction signature"),
REPLAY_PROTECTED_SIGNATURES_NOT_SUPPORTED(
-32000, "Signatures with replay protection are not supported"),
PRIVATE_TRANSACTION_FAILED(-32000, "Private transaction failed");

private final int code;
private final String message;
Expand All @@ -40,4 +54,33 @@ public int getCode() {
public String getMessage() {
return message;
}

public static GraphQLError of(final TransactionInvalidReason transactionInvalidReason) {
switch (transactionInvalidReason) {
case WRONG_CHAIN_ID:
return WRONG_CHAIN_ID;
case REPLAY_PROTECTED_SIGNATURES_NOT_SUPPORTED:
return REPLAY_PROTECTED_SIGNATURES_NOT_SUPPORTED;
case INVALID_SIGNATURE:
return INVALID_TRANSACTION_SIGNATURE;
case UPFRONT_COST_EXCEEDS_BALANCE:
return TRANSACTION_UPFRONT_COST_EXCEEDS_BALANCE;
case NONCE_TOO_LOW:
return NONCE_TOO_LOW;
case INCORRECT_NONCE:
return INCORRECT_NONCE;
case INTRINSIC_GAS_EXCEEDS_GAS_LIMIT:
return INTRINSIC_GAS_EXCEEDS_LIMIT;
case EXCEEDS_BLOCK_GAS_LIMIT:
return EXCEEDS_BLOCK_GAS_LIMIT;
case TX_SENDER_NOT_AUTHORIZED:
return TX_SENDER_NOT_AUTHORIZED;
case CHAIN_HEAD_WORLD_STATE_NOT_AVAILABLE:
return CHAIN_HEAD_WORLD_STATE_NOT_AVAILABLE;
case PRIVATE_TRANSACTION_FAILED:
return PRIVATE_TRANSACTION_FAILED;
default:
return INTERNAL_ERROR;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@
*/
package tech.pegasys.pantheon.ethereum.graphql;

import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

import tech.pegasys.pantheon.ethereum.core.Hash;
import tech.pegasys.pantheon.ethereum.graphql.internal.BlockWithMetadata;
import tech.pegasys.pantheon.util.bytes.BytesValue;

import java.util.Optional;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
Expand All @@ -43,6 +47,8 @@ public void onlyNumber() throws Exception {

when(environment.getContext()).thenReturn(context);
when(context.getBlockchainQuery()).thenReturn(query);
when(query.blockByNumber(anyLong()))
.thenReturn(Optional.of(new BlockWithMetadata<>(null, null, null, null, 0)));

fetcher.get(environment);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ public EthGraphQLHttpBySpecErrorCaseTest(final String specFileName) {
@Parameters(name = "{index}: {0}")
public static Collection<String> specs() {
final List<String> specs = new ArrayList<>();
specs.add("eth_getBlockWrongParams");
specs.add("eth_getBlocksByWrongRange");
specs.add("eth_getBalance_toobig_bn");
specs.add("eth_getBalance_without_addr");

return specs;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,51 +44,63 @@ public static Collection<String> specs() {
final List<String> specs = new ArrayList<>();

specs.add("eth_blockNumber");
specs.add("eth_getTransactionByHash");
specs.add("eth_getTransactionByHashNull");
specs.add("eth_getBlockByHash");
specs.add("eth_getBlockByNumber");
specs.add("eth_getBlockTransactionCountByHash");
specs.add("eth_getBlockTransactionCountByNumber");
specs.add("eth_getTransactionByBlockHashAndIndex");
specs.add("eth_getTransactionByBlockNumberAndIndex");

specs.add("eth_call_Block8");
specs.add("eth_call_BlockLatest");

specs.add("eth_estimateGas_transfer");
specs.add("eth_estimateGas_noParams");
specs.add("eth_estimateGas_contractDeploy");

specs.add("eth_gasPrice");

specs.add("eth_getBalance_0x19");
specs.add("eth_getBalance_invalidAccountBlockNumber");
specs.add("eth_getBalance_invalidAccountLatest");
specs.add("eth_getBalance_latest");
specs.add("eth_getBalance_toobig_bn");
specs.add("eth_getBalance_without_addr");

specs.add("eth_getBlock_byHash");
specs.add("eth_getBlock_byHashInvalid");
specs.add("eth_getBlock_byNumber");
specs.add("eth_getBlock_byNumberInvalid");
specs.add("eth_getBlock_wrongParams");

specs.add("eth_getBlockTransactionCount_byHash");
specs.add("eth_getBlockTransactionCount_byNumber");

specs.add("eth_getCode");
specs.add("eth_getCode_noCode");

specs.add("eth_getLogs_matchTopic");

specs.add("eth_getStorageAt");
specs.add("eth_getStorageAt_illegalRangeGreaterThan");

specs.add("eth_getTransactionCount");

specs.add("eth_getTransactionByBlockNumberAndInvalidIndex");
specs.add("eth_getTransaction_byBlockHashAndIndex");
specs.add("eth_getTransaction_byBlockNumberAndIndex");
specs.add("eth_getTransaction_byBlockNumberAndInvalidIndex");
specs.add("eth_getTransaction_byHash");
specs.add("eth_getTransaction_byHashNull");

specs.add("eth_getBlocksByRange");
specs.add("eth_call_Block8");
specs.add("eth_call_BlockLatest");
specs.add("eth_getBalance_latest");
specs.add("eth_getBalance_0x19");
specs.add("eth_getBalance_invalidAccountBlockNumber");
specs.add("eth_getBalance_invalidAccountLatest");
specs.add("eth_gasPrice");
specs.add("eth_getTransactionCount");

specs.add("eth_getTransactionReceipt");

specs.add("eth_syncing");
specs.add("eth_sendRawTransaction_contractCreation");

specs.add("eth_sendRawTransaction_messageCall");
specs.add("eth_sendRawTransaction_transferEther");
specs.add("eth_sendRawTransaction_unsignedTransaction");

specs.add("eth_getLogs_matchTopic");
specs.add("eth_syncing");

specs.add("graphql_blocks_byFrom");
specs.add("graphql_blocks_byRange");
specs.add("graphql_blocks_byWrongRange");

specs.add("graphql_blocks_noTo");
specs.add("graphql_pending");

return specs;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
{
"request": "{account(address: \"0x8895ee1b4f6dd65047762f924ecd367c17eabf8f\") { balance } }",
"request": "{account { balance } }",
"response": {
"errors": [
{
"message": "Validation error of type MissingFieldArgument: Missing field argument address @ 'account'",
"locations": [
{
"line": 1,
"column": 2
}
]
}
]
},
"statusCode": 400
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"request":
"request":

"{block (hash : \"0xc8df1f061abb4d0c107b2b1a794ade8780b3120e681f723fe55a7be586d95ba6\") {number transactions{hash} timestamp difficulty totalDifficulty gasUsed gasLimit hash nonce ommerCount logsBloom mixHash ommerHash extraData stateRoot receiptsRoot transactionCount transactionsRoot}} ",


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"request": "{block (hash : \"0x123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0\") {number } }",
"response": {
"data": null,
"errors": [
{
"message": "Exception while fetching data (/block) : Block hash 0x123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0 was not found",
"locations": [
{
"line": 1,
"column": 2
}
],
"path": [
"block"
]
}
]
},
"statusCode": 400
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"request": "{block (number: 88888888) {number }} ",
"response": {
"data": null,
"errors": [
{
"message": "Exception while fetching data (/block) : Block number 88888888 was not found",
"locations": [
{
"line": 1,
"column": 2
}
],
"path": [
"block"
]
}
]
},
"statusCode": 400
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"request": "{block (number: \"0x03\", hash : \"0xc8df1f061abb4d0c107b2b1a794ade8780b3120e681f723fe55a7be586d95ba6\") {number transactions{hash} timestamp difficulty totalDifficulty gasUsed gasLimit hash nonce ommerCount logsBloom mixHash ommerHash extraData stateRoot receiptsRoot transactionCount transactionsRoot}} ",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how you can add a comment in there but I wonder what the invalid param is exactly in this specific request.
Also would there be a way to indicate it in the error message ?

"response": {
"data": null,
"errors": [
{
"message": "Exception while fetching data (/block) : Invalid params",
"locations": [
{
"line": 1,
"column": 2
}
],
"path": [
"block"
],
"extensions": {
"errorCode": -32602,
"errorMessage": "Invalid params"
}
}
]
},
"statusCode": 400
}