Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Do not return errors when no account is present, return a zero balanc…
…e instead

Signed-off-by: Antoine Toulme <antoine@lunar-ocean.com>
  • Loading branch information
atoulme committed Feb 26, 2021
commit d0f6a17680886b19527e3321ff6aab0d25cac901
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
}