Skip to content

Commit

Permalink
Do not return errors when no account is present, return a zero balanc…
Browse files Browse the repository at this point in the history
…e instead (#1951)

Signed-off-by: Antoine Toulme <[email protected]>
  • Loading branch information
atoulme authored Mar 4, 2021
1 parent 9947692 commit eddd35f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 47 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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;
Expand Down Expand Up @@ -186,8 +187,9 @@ DataFetcher<Optional<AccountAdapter>> getAccountDataFetcher() {
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
Expand All @@ -201,13 +203,13 @@ DataFetcher<Optional<AccountAdapter>> getAccountDataFetcher() {
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));
});
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,35 @@
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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}

Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit eddd35f

Please sign in to comment.