Skip to content
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

### Additions and Improvements
- Allow Ethstats connection url to specify ws:// or wss:// scheme. [#5494](https://github.com/hyperledger/besu/issues/5494)
-
- Add support for Shanghai changes to the GraphQL service [#5496](https://github.com/hyperledger/besu/pull/5496)

### Bug Fixes

### Download Links
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.Quantity;

import java.util.Locale;

import graphql.GraphQLContext;
import graphql.execution.CoercedVariables;
import graphql.language.IntValue;
import graphql.language.StringValue;
import graphql.language.Value;
import graphql.schema.Coercing;
import graphql.schema.CoercingParseLiteralException;
import graphql.schema.CoercingParseValueException;
Expand All @@ -35,19 +40,19 @@ private Scalars() {}
private static final Coercing<Address, String> ADDRESS_COERCING =
new Coercing<Address, String>() {
Address convertImpl(final Object input) {
if (input instanceof Address) {
return (Address) input;
} else if (input instanceof Bytes) {
if (input instanceof Address address) {
return address;
} else if (input instanceof Bytes bytes) {
if (((Bytes) input).size() <= 20) {
return Address.wrap((Bytes) input);
return Address.wrap(bytes);
} else {
return null;
}
} else if (input instanceof StringValue) {
return convertImpl(((StringValue) input).getValue());
} else if (input instanceof String) {
} else if (input instanceof StringValue stringValue) {
return convertImpl(stringValue.getValue());
} else if (input instanceof String string) {
try {
return Address.fromHexStringStrict((String) input);
return Address.fromHexStringStrict(string);
} catch (IllegalArgumentException iae) {
return null;
}
Expand All @@ -57,7 +62,9 @@ Address convertImpl(final Object input) {
}

@Override
public String serialize(final Object input) throws CoercingSerializeException {
public String serialize(
final Object input, final GraphQLContext graphQLContext, final Locale locale)
throws CoercingSerializeException {
Address result = convertImpl(input);
if (result != null) {
return result.toHexString();
Expand All @@ -67,7 +74,9 @@ public String serialize(final Object input) throws CoercingSerializeException {
}

@Override
public Address parseValue(final Object input) throws CoercingParseValueException {
public Address parseValue(
final Object input, final GraphQLContext graphQLContext, final Locale locale)
throws CoercingParseValueException {
Address result = convertImpl(input);
if (result != null) {
return result;
Expand All @@ -78,7 +87,12 @@ public Address parseValue(final Object input) throws CoercingParseValueException
}

@Override
public Address parseLiteral(final Object input) throws CoercingParseLiteralException {
public Address parseLiteral(
final Value<?> input,
final CoercedVariables variables,
final GraphQLContext graphQLContext,
final Locale locale)
throws CoercingParseLiteralException {
Address result = convertImpl(input);
if (result != null) {
return result;
Expand All @@ -92,25 +106,27 @@ public Address parseLiteral(final Object input) throws CoercingParseLiteralExcep
new Coercing<String, String>() {

String convertImpl(final Object input) {
if (input instanceof String) {
if (input instanceof String string) {
try {
return Bytes.fromHexStringLenient((String) input).toShortHexString();
return Bytes.fromHexStringLenient(string).toShortHexString();
} catch (IllegalArgumentException iae) {
return null;
}
} else if (input instanceof Bytes) {
return ((Bytes) input).toShortHexString();
} else if (input instanceof StringValue) {
return convertImpl(((StringValue) input).getValue());
} else if (input instanceof IntValue) {
return UInt256.valueOf(((IntValue) input).getValue()).toShortHexString();
} else if (input instanceof Bytes bytes) {
return bytes.toShortHexString();
} else if (input instanceof StringValue stringValue) {
return convertImpl(stringValue.getValue());
} else if (input instanceof IntValue intValue) {
return UInt256.valueOf(intValue.getValue()).toShortHexString();
} else {
return null;
}
}

@Override
public String serialize(final Object input) throws CoercingSerializeException {
public String serialize(
final Object input, final GraphQLContext graphQLContext, final Locale locale)
throws CoercingSerializeException {
var result = convertImpl(input);
if (result != null) {
return result;
Expand All @@ -120,7 +136,9 @@ public String serialize(final Object input) throws CoercingSerializeException {
}

@Override
public String parseValue(final Object input) throws CoercingParseValueException {
public String parseValue(
final Object input, final GraphQLContext graphQLContext, final Locale locale)
throws CoercingParseValueException {
var result = convertImpl(input);
if (result != null) {
return result;
Expand All @@ -131,7 +149,12 @@ public String parseValue(final Object input) throws CoercingParseValueException
}

@Override
public String parseLiteral(final Object input) throws CoercingParseLiteralException {
public String parseLiteral(
final Value<?> input,
final CoercedVariables variables,
final GraphQLContext graphQLContext,
final Locale locale)
throws CoercingParseLiteralException {
var result = convertImpl(input);
if (result != null) {
return result;
Expand All @@ -145,12 +168,12 @@ public String parseLiteral(final Object input) throws CoercingParseLiteralExcept
new Coercing<Bytes, String>() {

Bytes convertImpl(final Object input) {
if (input instanceof Bytes) {
return (Bytes) input;
} else if (input instanceof StringValue) {
return convertImpl(((StringValue) input).getValue());
} else if (input instanceof String) {
if (!Quantity.isValid((String) input)) {
if (input instanceof Bytes bytes) {
return bytes;
} else if (input instanceof StringValue stringValue) {
return convertImpl(stringValue.getValue());
} else if (input instanceof String string) {
if (!Quantity.isValid(string)) {
throw new CoercingParseLiteralException(
"Bytes value '" + input + "' is not prefixed with 0x");
}
Expand All @@ -165,7 +188,9 @@ Bytes convertImpl(final Object input) {
}

@Override
public String serialize(final Object input) throws CoercingSerializeException {
public String serialize(
final Object input, final GraphQLContext graphQLContext, final Locale locale)
throws CoercingSerializeException {
var result = convertImpl(input);
if (result != null) {
return result.toHexString();
Expand All @@ -175,7 +200,9 @@ public String serialize(final Object input) throws CoercingSerializeException {
}

@Override
public Bytes parseValue(final Object input) throws CoercingParseValueException {
public Bytes parseValue(
final Object input, final GraphQLContext graphQLContext, final Locale locale)
throws CoercingParseValueException {
var result = convertImpl(input);
if (result != null) {
return result;
Expand All @@ -186,7 +213,12 @@ public Bytes parseValue(final Object input) throws CoercingParseValueException {
}

@Override
public Bytes parseLiteral(final Object input) throws CoercingParseLiteralException {
public Bytes parseLiteral(
final Value<?> input,
final CoercedVariables variables,
final GraphQLContext graphQLContext,
final Locale locale)
throws CoercingParseLiteralException {
var result = convertImpl(input);
if (result != null) {
return result;
Expand All @@ -200,18 +232,18 @@ public Bytes parseLiteral(final Object input) throws CoercingParseLiteralExcepti
new Coercing<Bytes32, String>() {

Bytes32 convertImpl(final Object input) {
if (input instanceof Bytes32) {
return (Bytes32) input;
} else if (input instanceof Bytes) {
if (((Bytes) input).size() <= 32) {
if (input instanceof Bytes32 bytes32) {
return bytes32;
} else if (input instanceof Bytes bytes) {
if (bytes.size() <= 32) {
return Bytes32.leftPad((Bytes) input);
} else {
return null;
}
} else if (input instanceof StringValue) {
return convertImpl((((StringValue) input).getValue()));
} else if (input instanceof String) {
if (!Quantity.isValid((String) input)) {
} else if (input instanceof StringValue stringValue) {
return convertImpl(stringValue.getValue());
} else if (input instanceof String string) {
if (!Quantity.isValid(string)) {
throw new CoercingParseLiteralException(
"Bytes32 value '" + input + "' is not prefixed with 0x");
} else {
Expand All @@ -227,7 +259,9 @@ Bytes32 convertImpl(final Object input) {
}

@Override
public String serialize(final Object input) throws CoercingSerializeException {
public String serialize(
final Object input, final GraphQLContext graphQLContext, final Locale locale)
throws CoercingSerializeException {
var result = convertImpl(input);
if (result == null) {
throw new CoercingSerializeException("Unable to serialize " + input + " as an Bytes32");
Expand All @@ -237,7 +271,9 @@ public String serialize(final Object input) throws CoercingSerializeException {
}

@Override
public Bytes32 parseValue(final Object input) throws CoercingParseValueException {
public Bytes32 parseValue(
final Object input, final GraphQLContext graphQLContext, final Locale locale)
throws CoercingParseValueException {
var result = convertImpl(input);
if (result == null) {
throw new CoercingParseValueException(
Expand All @@ -248,7 +284,12 @@ public Bytes32 parseValue(final Object input) throws CoercingParseValueException
}

@Override
public Bytes32 parseLiteral(final Object input) throws CoercingParseLiteralException {
public Bytes32 parseLiteral(
final Value<?> input,
final CoercedVariables variables,
final GraphQLContext graphQLContext,
final Locale locale)
throws CoercingParseLiteralException {
var result = convertImpl(input);
if (result == null) {
throw new CoercingParseLiteralException("Value is not any Bytes32 : '" + input + "'");
Expand All @@ -259,13 +300,15 @@ public Bytes32 parseLiteral(final Object input) throws CoercingParseLiteralExcep
};

private static final Coercing<Number, Number> LONG_COERCING =
new Coercing<Number, Number>() {
new Coercing<>() {
@Override
public Number serialize(final Object input) throws CoercingSerializeException {
if (input instanceof Number) {
return (Number) input;
} else if (input instanceof String) {
final String value = ((String) input).toLowerCase();
public Number serialize(
final Object input, final GraphQLContext graphQLContext, final Locale locale)
throws CoercingSerializeException {
if (input instanceof Number number) {
return number;
} else if (input instanceof String string) {
final String value = string.toLowerCase();
if (value.startsWith("0x")) {
return Bytes.fromHexStringLenient(value).toLong();
} else {
Expand All @@ -276,11 +319,13 @@ public Number serialize(final Object input) throws CoercingSerializeException {
}

@Override
public Number parseValue(final Object input) throws CoercingParseValueException {
if (input instanceof Number) {
return (Number) input;
} else if (input instanceof String) {
final String value = ((String) input).toLowerCase();
public Number parseValue(
final Object input, final GraphQLContext graphQLContext, final Locale locale)
throws CoercingParseValueException {
if (input instanceof Number number) {
return number;
} else if (input instanceof String string) {
final String value = string.toLowerCase();
if (value.startsWith("0x")) {
return Bytes.fromHexStringLenient(value).toLong();
} else {
Expand All @@ -292,12 +337,17 @@ public Number parseValue(final Object input) throws CoercingParseValueException
}

@Override
public Number parseLiteral(final Object input) throws CoercingParseLiteralException {
public Number parseLiteral(
final Value<?> input,
final CoercedVariables variables,
final GraphQLContext graphQLContext,
final Locale locale)
throws CoercingParseLiteralException {
try {
if (input instanceof IntValue) {
return ((IntValue) input).getValue().longValue();
} else if (input instanceof StringValue) {
final String value = ((StringValue) input).getValue().toLowerCase();
if (input instanceof IntValue intValue) {
return intValue.getValue().longValue();
} else if (input instanceof StringValue stringValue) {
final String value = stringValue.getValue().toLowerCase();
if (value.startsWith("0x")) {
return Bytes.fromHexStringLenient(value).toLong();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,26 @@ public AccountAdapter(final Address address, final Account account) {
this.address = address;
}

public Optional<Address> getAddress() {
return Optional.of(address);
public Address getAddress() {
return address;
}

public Optional<Wei> getBalance() {
return account.map(AccountState::getBalance).or(() -> Optional.of(Wei.ZERO));
public Wei getBalance() {
return account.map(AccountState::getBalance).orElse(Wei.ZERO);
}

public Optional<Long> getTransactionCount() {
return account.map(AccountState::getNonce).or(() -> Optional.of(0L));
public Long getTransactionCount() {
return account.map(AccountState::getNonce).orElse(0L);
}

public Optional<Bytes> getCode() {
return account.map(AccountState::getCode);
public Bytes getCode() {
return account.map(AccountState::getCode).orElse(Bytes.EMPTY);
}

public Optional<Bytes32> getStorage(final DataFetchingEnvironment environment) {
public Bytes32 getStorage(final DataFetchingEnvironment environment) {
final Bytes32 slot = environment.getArgument("slot");
return account.map(account -> account.getStorageValue(UInt256.fromBytes(slot)));
return account
.map(a -> (Bytes32) a.getStorageValue(UInt256.fromBytes(slot)))
.orElse(Bytes32.ZERO);
}
}
Loading