Do not return errors when no account is present, return a zero balance instead (#1951)

Signed-off-by: Antoine Toulme <antoine@lunar-ocean.com>
pull/1985/head
Antoine Toulme 4 years ago committed by GitHub
parent 9947692322
commit eddd35f3a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      CHANGELOG.md
  2. 20
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/graphql/GraphQLDataFetchers.java
  3. 9
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/graphql/internal/pojoadapter/EmptyAccountAdapter.java
  4. 23
      ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/graphql/eth_getBalance_invalidAccountBlockNumber.json
  5. 23
      ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/graphql/eth_getBalance_invalidAccountLatest.json

@ -9,6 +9,7 @@
### Bug Fixes
* Fixed incorrect `groupId` in published maven pom files.
* Fixed GraphQL response for missing account, return empty account instead [\#1946](https://github.com/hyperledger/besu/issues/1946)
### Early Access Features

@ -17,6 +17,7 @@ package org.hyperledger.besu.ethereum.api.graphql;
import static com.google.common.base.Preconditions.checkArgument;
import org.hyperledger.besu.ethereum.api.graphql.internal.pojoadapter.AccountAdapter;
import org.hyperledger.besu.ethereum.api.graphql.internal.pojoadapter.EmptyAccountAdapter;
import org.hyperledger.besu.ethereum.api.graphql.internal.pojoadapter.LogAdapter;
import org.hyperledger.besu.ethereum.api.graphql.internal.pojoadapter.NormalBlockAdapter;
import org.hyperledger.besu.ethereum.api.graphql.internal.pojoadapter.PendingStateAdapter;
@ -186,8 +187,9 @@ public class GraphQLDataFetchers {
final Optional<WorldState> ws = blockchainQuery.getWorldState(bn);
if (ws.isPresent()) {
final Account account = ws.get().get(addr);
Preconditions.checkArgument(
account != null, "Account with address %s does not exist", addr);
if (account == null) {
return Optional.of(new EmptyAccountAdapter(addr));
}
return Optional.of(new AccountAdapter(account));
} else if (bn > blockchainQuery.getBlockchain().getChainHeadBlockNumber()) {
// block is past chainhead
@ -201,13 +203,13 @@ public class GraphQLDataFetchers {
final long latestBn = blockchainQuery.latestBlock().get().getHeader().getNumber();
final Optional<WorldState> ows = blockchainQuery.getWorldState(latestBn);
return ows.flatMap(
ws -> {
Account account = ws.get(addr);
Preconditions.checkArgument(
account != null, "Account with address %s does not exist", addr);
return Optional.ofNullable(account);
})
.map(AccountAdapter::new);
ws -> {
Account account = ws.get(addr);
if (account == null) {
return Optional.of(new EmptyAccountAdapter(addr));
}
return Optional.of(new AccountAdapter(account));
});
}
};
}

@ -23,30 +23,35 @@ import graphql.schema.DataFetchingEnvironment;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
@SuppressWarnings("unused") // reflected by GraphQL
public class EmptyAccountAdapter extends AdapterBase {
public class EmptyAccountAdapter extends AccountAdapter {
private final Address address;
public EmptyAccountAdapter(final Address address) {
super(null);
this.address = address;
}
@Override
public Optional<Address> getAddress() {
return Optional.of(address);
}
@Override
public Optional<Wei> getBalance() {
return Optional.of(Wei.ZERO);
}
@Override
public Optional<Long> getTransactionCount() {
return Optional.of(0L);
}
@Override
public Optional<Bytes> getCode() {
return Optional.of(Bytes.EMPTY);
}
@Override
public Optional<Bytes32> getStorage(final DataFetchingEnvironment environment) {
return Optional.of(Bytes32.ZERO);
}

@ -1,25 +1,12 @@
{
"request": "{account(blockNumber:\"0x19\", address: \"0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef\") { balance } }",
"response": {
"errors": [
{
"message": "Exception while fetching data (/account) : Account with address 0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef does not exist",
"locations": [
{
"line": 1,
"column": 2
}
],
"path": [
"account"
],
"extensions": {
"classification": "DataFetchingException"
}
"data": {
"account": {
"balance": "0x"
}
],
"data": null
}
},
"statusCode": 400
"statusCode": 200
}

@ -1,25 +1,12 @@
{
"request": "{account(address: \"0xdeaff00ddeaff00ddeaff00ddeaff00ddeaff00d\") { balance } }",
"response": {
"errors": [
{
"message": "Exception while fetching data (/account) : Account with address 0xdeaff00ddeaff00ddeaff00ddeaff00ddeaff00d does not exist",
"locations": [
{
"line": 1,
"column": 2
}
],
"path": [
"account"
],
"extensions": {
"classification": "DataFetchingException"
}
"data" : {
"account" : {
"balance" : "0x"
}
],
"data": null
}
},
"statusCode": 400
"statusCode": 200
}

Loading…
Cancel
Save