Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nkramer44 committed Oct 18, 2024
1 parent 787cea8 commit 9f96789
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public String encodeForSigningClaim(String json) throws JsonProcessingException
}
UnsignedByteArray channel = UnsignedByteArray.fromHex(node.get(CHANNEL_FIELD_NAME).asText());
UnsignedByteArray amount = UnsignedByteArray.of(
new UInt64Type(UnsignedLong.valueOf(node.get(AMOUNT_FIELD_NAME).asText()), 16).toBytes()
new UInt64Type(UnsignedLong.valueOf(node.get(AMOUNT_FIELD_NAME).asText())).toBytes()
);

UnsignedByteArray byteArray = UnsignedByteArray.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public <T extends SerializedType<T>> T readType(Class<T> type) {
* @return The type associated with the given field.
*/
public SerializedType typeForField(FieldInstance field) {
return SerializedType.getTypeByName(field.type(), field.name());
return SerializedType.getTypeByName(field.type());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,8 @@ public void writeFieldAndValue(final FieldInstance field, final SerializedType v
public void writeFieldAndValue(final FieldInstance field, final JsonNode value) throws JsonProcessingException {
Objects.requireNonNull(field);
Objects.requireNonNull(value);
SerializedType typedValue;
if (field.name().equals("BaseFee")) {
typedValue = SerializedType.getTypeByName(field.type(), field.name()).fromHex(value.asText());
} else {
typedValue = SerializedType.getTypeByName(field.type(), field.name()).fromJson(value);
}
SerializedType typedValue = SerializedType.getTypeByName(field.type()).fromJson(value, field);

writeFieldAndValue(field, typedValue);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import org.xrpl.xrpl4j.codec.addresses.UnsignedByteArray;
import org.xrpl.xrpl4j.codec.binary.BinaryCodecObjectMapperFactory;
import org.xrpl.xrpl4j.codec.binary.definitions.FieldInstance;
import org.xrpl.xrpl4j.codec.binary.serdes.BinaryParser;

import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;

/**
* Defines an abstract type serialization parent-class for all XRPL serialized type definitions.
Expand All @@ -41,36 +41,28 @@
*/
public abstract class SerializedType<T extends SerializedType<T>> {

private static final Set<String> BASE_10_UINT64_FIELD_NAMES = Sets.newHashSet(
"MaximumAmount", "OutstandingAmount", "MPTAmount"
);

@SuppressWarnings("all")
private static final Map<String, Function<String, SerializedType<?>>> typeMap =
new ImmutableMap.Builder<String, Function<String, SerializedType<?>>>()
.put("AccountID", fieldName -> new AccountIdType())
.put("Amount", fieldName -> new AmountType())
.put("Blob", fieldName -> new BlobType())
.put("Currency", fieldName -> new CurrencyType())
.put("Hash128", fieldName -> new Hash128Type())
.put("Hash160", fieldName -> new Hash160Type())
.put("Hash192", fieldName -> new Hash192Type())
.put("Hash256", fieldName -> new Hash256Type())
.put("PathSet", fieldName -> new PathSetType())
.put("STArray", fieldName -> new STArrayType())
.put("STObject", fieldName -> new STObjectType())
.put("UInt8", fieldName -> new UInt8Type())
.put("UInt16", fieldName -> new UInt16Type())
.put("UInt32", fieldName -> new UInt32Type())
.put("UInt64", fieldName -> {
if (BASE_10_UINT64_FIELD_NAMES.contains(fieldName)) {
return new UInt64Type(10);
} else {
return new UInt64Type(16);
}
})
.put("Vector256", fieldName -> new Vector256Type())
.put("Issue", fieldName -> new IssueType())
.put("XChainBridge", fieldName -> new XChainBridgeType())
private static final Map<String, Supplier<SerializedType<?>>> typeMap =
new ImmutableMap.Builder<String, Supplier<SerializedType<?>>>()
.put("AccountID", () -> new AccountIdType())
.put("Amount", () -> new AmountType())
.put("Blob", () -> new BlobType())
.put("Currency", () -> new CurrencyType())
.put("Hash128", () -> new Hash128Type())
.put("Hash160", () -> new Hash160Type())
.put("Hash192", () -> new Hash192Type())
.put("Hash256", () -> new Hash256Type())
.put("PathSet", () -> new PathSetType())
.put("STArray", () -> new STArrayType())
.put("STObject", () -> new STObjectType())
.put("UInt8", () -> new UInt8Type())
.put("UInt16", () -> new UInt16Type())
.put("UInt32", () -> new UInt32Type())
.put("UInt64", () -> new UInt64Type())
.put("Vector256", () -> new Vector256Type())
.put("Issue", () -> new IssueType())
.put("XChainBridge", () -> new XChainBridgeType())
.build();
private final UnsignedByteArray bytes;

Expand All @@ -81,13 +73,12 @@ public SerializedType(UnsignedByteArray bytes) {
/**
* Get the {@link SerializedType} for the supplied {@code name}.
*
* @param name A {@link String} representing the name of a {@link SerializedType}.
* @param fieldName The name of the field that is being serialized.
* @param name A {@link String} representing the name of a {@link SerializedType}.
*
* @return A {@link SerializedType} for the supplied {@code name}.
*/
public static SerializedType<?> getTypeByName(String name, String fieldName) {
return typeMap.get(name).apply(fieldName);
public static SerializedType<?> getTypeByName(String name) {
return typeMap.get(name).get();
}

/**
Expand All @@ -102,7 +93,7 @@ public static String getNameByType(SerializedType<?> type) {
.stream()
// We only care about the class name, and the String passed to .apply is only used to figure
// out the radix of the JSON string for UInt64Types. Plus this method is only used in a test.
.filter(entry -> entry.getValue().apply("").getClass().equals(type.getClass()))
.filter(entry -> entry.getValue().get().getClass().equals(type.getClass()))
.map(Map.Entry::getKey)
.findAny()
.orElse(null);
Expand Down Expand Up @@ -141,6 +132,10 @@ public T fromParser(BinaryParser parser, int lengthHint) {
*/
public abstract T fromJson(JsonNode node) throws JsonProcessingException;

public T fromJson(JsonNode node, FieldInstance fieldInstance) throws JsonProcessingException {
return fromJson(node);
}

/**
* Construct a concrete instance of {@link SerializedType} from the supplied {@code json}.
*
Expand Down Expand Up @@ -212,6 +207,10 @@ public JsonNode toJson() {
return new TextNode(toHex());
}

public JsonNode toJson(FieldInstance fieldInstance) {
return toJson();
}

/**
* Convert this {@link SerializedType} to a hex-encoded {@link String}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,65 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.google.common.collect.Sets;
import com.google.common.primitives.UnsignedLong;
import org.xrpl.xrpl4j.codec.binary.definitions.FieldInstance;
import org.xrpl.xrpl4j.codec.binary.serdes.BinaryParser;

import java.util.Set;

/**
* Codec for XRPL UInt64 type.
*/
public class UInt64Type extends UIntType<UInt64Type> {

private final int radix;
private static final Set<String> BASE_10_UINT64_FIELD_NAMES = Sets.newHashSet(
"MaximumAmount", "OutstandingAmount", "MPTAmount"
);

public UInt64Type(int radix) {
this(UnsignedLong.ZERO, radix);
public UInt64Type() {
this(UnsignedLong.ZERO);
}

public UInt64Type(UnsignedLong value, int radix) {
public UInt64Type(UnsignedLong value) {
super(value, 64);
this.radix = radix;
}

@Override
public UInt64Type fromParser(BinaryParser parser) {
return new UInt64Type(parser.readUInt64(), radix);
return new UInt64Type(parser.readUInt64());
}

@Override
public UInt64Type fromJson(JsonNode value) {
// STUInt64s are represented as hex-encoded Strings in JSON.
return new UInt64Type(UnsignedLong.valueOf(value.asText(), radix), radix);
throw new UnsupportedOperationException("Cannot construct UInt64Type from JSON without a FieldInstance. Call " +
"the overload of this method that accepts a FieldInstance instead.");
}

@Override
public JsonNode toJson() {
throw new UnsupportedOperationException("Cannot convert UInt64Type to JSON without a FieldInstance. Call " +
"the overload of this method that accepts a FieldInstance instead.");
}

@Override
public UInt64Type fromJson(JsonNode value, FieldInstance fieldInstance) {
int radix = getRadix(fieldInstance);
// STUInt64s are represented as hex-encoded Strings in JSON.
return new UInt64Type(UnsignedLong.valueOf(value.asText(), radix));
}

@Override
public JsonNode toJson(FieldInstance fieldInstance) {
int radix = getRadix(fieldInstance);
return new TextNode(UnsignedLong.valueOf(toHex(), 16).toString(radix).toUpperCase());
}

private static int getRadix(FieldInstance fieldInstance) {
int radix = 16;
if (BASE_10_UINT64_FIELD_NAMES.contains(fieldInstance.name())) {
radix = 10;
}
return radix;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.common.primitives.UnsignedLong;
import org.xrpl.xrpl4j.codec.addresses.ByteUtils;
import org.xrpl.xrpl4j.codec.addresses.UnsignedByteArray;
import org.xrpl.xrpl4j.codec.binary.definitions.FieldInstance;

/**
* Base codec for XRPL UInt types.
Expand All @@ -50,4 +51,5 @@ UnsignedLong valueOf() {
public JsonNode toJson() {
return new TextNode(UnsignedLong.valueOf(toHex(), 16).toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
import org.junit.jupiter.params.provider.ValueSource;

public class UInt64TypeUnitTest {
private final UInt64Type base16Type = new UInt64Type(16);
private final UInt64Type base10Type = new UInt64Type(10);
private final UInt64Type base16Type = new UInt64Type();
private static UnsignedLong maxUint64 = UnsignedLong.valueOf("FFFFFFFFFFFFFFFF", 16);

@Test
Expand All @@ -50,28 +49,6 @@ void encodeBase16() {
assertThat(base16Type.fromJson("\"FFFFFFFFFFFFFFFF\"").toHex()).isEqualTo("FFFFFFFFFFFFFFFF");
}

@Test
void decodeBase10() {
assertThat(base10Type.fromHex("0000000000000000").valueOf()).isEqualTo(UnsignedLong.valueOf(0));
assertThat(base10Type.fromHex("000000000000000F").valueOf()).isEqualTo(UnsignedLong.valueOf(15));
assertThat(base10Type.fromHex("00000000FFFFFFFF").valueOf()).isEqualTo(UnsignedLong.valueOf(4294967295L));
assertThat(base10Type.fromHex("FFFFFFFFFFFFFFFF").valueOf()).isEqualTo(maxUint64);
}

@Test
void encodeBase10() {
assertThat(base10Type.fromJson("\"0\"").toHex()).isEqualTo("0000000000000000");
assertThat(base10Type.fromJson("\"15\"").toHex()).isEqualTo("000000000000000F");
assertThat(base10Type.fromJson("\"65535\"").toHex()).isEqualTo("000000000000FFFF");
assertThat(base10Type.fromJson("\"4294967295\"").toHex()).isEqualTo("00000000FFFFFFFF");
assertThat(base10Type.fromJson("\"18446744073709551615\"").toHex()).isEqualTo("FFFFFFFFFFFFFFFF");
}

@ParameterizedTest
@ValueSource(strings = {"\"0\"", "\"15\"", "\"65535\"", "\"4294967295\"", "\"18446744073709551615\""})
void toFromJsonBase10(String json) {
assertThat(base10Type.fromJson(json).toJson().toString()).isEqualTo(json);
}

@ParameterizedTest
@ValueSource(strings = {"\"0\"", "\"F\"", "\"FFFF\"", "\"FFFFFFFF\"", "\"FFFFFFFFFFFFFFFF\""})
Expand Down

0 comments on commit 9f96789

Please sign in to comment.