diff --git a/src/main/java/com/casper/sdk/helper/CasperDeployHelper.java b/src/main/java/com/casper/sdk/helper/CasperDeployHelper.java index e75a59e3c..9d34a9fa2 100644 --- a/src/main/java/com/casper/sdk/helper/CasperDeployHelper.java +++ b/src/main/java/com/casper/sdk/helper/CasperDeployHelper.java @@ -39,9 +39,13 @@ @NoArgsConstructor(access = AccessLevel.PRIVATE) public class CasperDeployHelper { - public static DeployHeader buildDeployHeader(PublicKey fromPublicKey, String chainName, - Long gasPrice, Ttl ttl, Date date, - List dependencies, byte[] bodyHash) { + public static DeployHeader buildDeployHeader(final PublicKey fromPublicKey, + final String chainName, + final Long gasPrice, + final Ttl ttl, + final Date date, + final List dependencies, + final byte[] bodyHash) { return DeployHeader .builder() .account(fromPublicKey) @@ -54,28 +58,29 @@ public static DeployHeader buildDeployHeader(PublicKey fromPublicKey, String cha .build(); } - public static HashAndSignature signDeployHeader(AbstractPrivateKey privateKey, DeployHeader deployHeader) + public static HashAndSignature signDeployHeader(final AbstractPrivateKey privateKey, final DeployHeader deployHeader) throws GeneralSecurityException, NoSuchTypeException, ValueSerializationException { - SerializerBuffer serializerBuffer = new SerializerBuffer(); - + final SerializerBuffer serializerBuffer = new SerializerBuffer(); deployHeader.serialize(serializerBuffer, Target.BYTE); - byte[] headerHash = Blake2b.digest(serializerBuffer.toByteArray(), 32); - Signature signature = Signature.sign(privateKey, headerHash); + final byte[] headerHash = Blake2b.digest(serializerBuffer.toByteArray(), 32); + final Signature signature = Signature.sign(privateKey, headerHash); return new HashAndSignature(headerHash, signature); } - public static byte[] getDeployItemAndModuleBytesHash(ExecutableDeployItem deployItem, ModuleBytes moduleBytes) + public static byte[] getDeployItemAndModuleBytesHash(final ExecutableDeployItem deployItem, final ModuleBytes moduleBytes) throws NoSuchTypeException, ValueSerializationException { - SerializerBuffer ser = new SerializerBuffer(); + final SerializerBuffer ser = new SerializerBuffer(); moduleBytes.serialize(ser, Target.BYTE); deployItem.serialize(ser, Target.BYTE); return Blake2b.digest(ser.toByteArray(), 32); } - public static ModuleBytes getPaymentModuleBytes(BigInteger paymentAmount) throws ValueSerializationException { - List> paymentArgs = new LinkedList<>(); - NamedArg paymentArg = new NamedArg<>("amount", - new CLValueU512(paymentAmount)); + public static ModuleBytes getPaymentModuleBytes(final BigInteger paymentAmount) throws ValueSerializationException { + final List> paymentArgs = new LinkedList<>(); + final NamedArg paymentArg = new NamedArg<>( + "amount", + new CLValueU512(paymentAmount) + ); paymentArgs.add(paymentArg); return ModuleBytes .builder() @@ -96,26 +101,38 @@ public static ModuleBytes getPaymentModuleBytes(BigInteger paymentAmount) throws * ms (30 minutes)) * @param date deploy date * @param dependencies list of digest dependencies - * @return + * @return the built deploy * @throws NoSuchTypeException * @throws GeneralSecurityException * @throws ValueSerializationException */ - public static Deploy buildDeploy(AbstractPrivateKey fromPrivateKey, String chainName, - ExecutableDeployItem session, ModuleBytes payment, - Long gasPrice, Ttl ttl, Date date, List dependencies) + public static Deploy buildDeploy(final AbstractPrivateKey fromPrivateKey, + final String chainName, + final ExecutableDeployItem session, + final ModuleBytes payment, + final Long gasPrice, + final Ttl ttl, + final Date date, + final List dependencies) throws NoSuchTypeException, GeneralSecurityException, ValueSerializationException { - byte[] sessionAnPaymentHash = getDeployItemAndModuleBytesHash(session, payment); + final byte[] sessionAnPaymentHash = getDeployItemAndModuleBytesHash(session, payment); - PublicKey fromPublicKey = PublicKey.fromAbstractPublicKey(fromPrivateKey.derivePublicKey()); + final PublicKey fromPublicKey = PublicKey.fromAbstractPublicKey(fromPrivateKey.derivePublicKey()); - DeployHeader deployHeader = buildDeployHeader(fromPublicKey, chainName, gasPrice, ttl, - date, dependencies, sessionAnPaymentHash); + final DeployHeader deployHeader = buildDeployHeader( + fromPublicKey, + chainName, + gasPrice, + ttl, + date, + dependencies, + sessionAnPaymentHash + ); - HashAndSignature hashAndSignature = signDeployHeader(fromPrivateKey, deployHeader); + final HashAndSignature hashAndSignature = signDeployHeader(fromPrivateKey, deployHeader); - List approvals = new LinkedList<>(); + final List approvals = new LinkedList<>(); approvals.add(Approval.builder() .signer(PublicKey.fromAbstractPublicKey(fromPrivateKey.derivePublicKey())) .signature(hashAndSignature.getSignature()) diff --git a/src/main/java/com/casper/sdk/model/clvalue/AbstractCLValue.java b/src/main/java/com/casper/sdk/model/clvalue/AbstractCLValue.java index ad2ea93a3..1acfb0531 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/AbstractCLValue.java +++ b/src/main/java/com/casper/sdk/model/clvalue/AbstractCLValue.java @@ -4,6 +4,7 @@ import com.casper.sdk.exception.NoSuchTypeException; import com.casper.sdk.jackson.resolver.CLValueResolver; import com.casper.sdk.model.clvalue.cltype.AbstractCLType; +import com.casper.sdk.model.clvalue.cltype.AbstractCLTypeWithChildren; import com.casper.sdk.model.clvalue.cltype.CLTypeData; import com.casper.sdk.model.clvalue.serde.CasperDeserializableObject; import com.casper.sdk.model.clvalue.serde.CasperSerializableObject; @@ -58,8 +59,15 @@ public void setValue(final T value) throws ValueSerializationException { final int length = deser.readI32(); final byte[] bytes = deser.readByteArray(length); final byte clType = deser.readU8(); + try { - final AbstractCLValue clValue = CLTypeData.getTypeBySerializationTag(clType).getClazz().getDeclaredConstructor().newInstance(); + CLTypeData clTypeData = CLTypeData.getTypeBySerializationTag(clType); + final AbstractCLValue clValue = clTypeData.getClazz().getDeclaredConstructor().newInstance(); + if (clValue instanceof AbstractCLValueWithChildren) { + // We have only obtained the parent type from the buffer now we need to read the child types + ((AbstractCLTypeWithChildren) clValue.getClType()).deserializeChildTypes(deser); + } + clValue.deserializeCustom(new DeserializerBuffer(Hex.encode(bytes))); return clValue; } catch (Exception e) { @@ -67,6 +75,11 @@ public void setValue(final T value) throws ValueSerializationException { } } + @Override + protected Object clone() throws CloneNotSupportedException { + return super.clone(); + } + @SneakyThrows({ValueSerializationException.class, NoSuchTypeException.class}) @JsonGetter(value = "bytes") @ExcludeFromJacocoGeneratedReport @@ -84,10 +97,7 @@ protected String getJsonBytes() { @ExcludeFromJacocoGeneratedReport protected void setJsonBytes(final String bytes) { this.bytes = bytes; - - final DeserializerBuffer deser = new DeserializerBuffer(this.bytes); - - this.deserialize(deser); + this.deserialize(new DeserializerBuffer(this.bytes)); } @JsonIgnore @@ -124,7 +134,7 @@ public void serialize(final SerializerBuffer ser, final Target target) throws Va serializeValue(ser); if (Target.BYTE.equals(target)) { - this.encodeType(ser); + getClType().serialize(ser); } } @@ -140,9 +150,4 @@ public void deserialize(final DeserializerBuffer deserializerBuffer) throws Valu throw new ValueDeserializationException("Error deserializing value", e); } } - - protected void encodeType(final SerializerBuffer ser) throws NoSuchTypeException { - final byte typeTag = (getClType().getClTypeData().getSerializationTag()); - ser.writeU8(typeTag); - } } diff --git a/src/main/java/com/casper/sdk/model/clvalue/AbstractCLValueWithChildren.java b/src/main/java/com/casper/sdk/model/clvalue/AbstractCLValueWithChildren.java index 0c1eeaebf..5cd53ae4a 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/AbstractCLValueWithChildren.java +++ b/src/main/java/com/casper/sdk/model/clvalue/AbstractCLValueWithChildren.java @@ -1,12 +1,9 @@ package com.casper.sdk.model.clvalue; import com.casper.sdk.annotation.ExcludeFromJacocoGeneratedReport; -import com.casper.sdk.exception.NoSuchTypeException; import com.casper.sdk.model.clvalue.cltype.AbstractCLTypeWithChildren; -import com.casper.sdk.model.clvalue.cltype.CLTypeData; import com.fasterxml.jackson.annotation.JsonSetter; import dev.oak3.sbs4j.DeserializerBuffer; -import dev.oak3.sbs4j.SerializerBuffer; import dev.oak3.sbs4j.exception.ValueDeserializationException; import lombok.EqualsAndHashCode; import lombok.SneakyThrows; @@ -32,8 +29,7 @@ public abstract class AbstractCLValueWithChildren child, - final CLTypeData childDataType) throws NoSuchTypeException { - if (child instanceof AbstractCLValueWithChildren) { - child.encodeType(ser); - } else { - // If there are no AbstractCLValueWithChildren as children we just need a simple tag - byte element0TypeTag = childDataType.getSerializationTag(); - ser.writeU8(element0TypeTag); + this.deserialize(new DeserializerBuffer(this.getBytes())); } } } diff --git a/src/main/java/com/casper/sdk/model/clvalue/CLValueByteArray.java b/src/main/java/com/casper/sdk/model/clvalue/CLValueByteArray.java index 1d960d98e..c5743323b 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/CLValueByteArray.java +++ b/src/main/java/com/casper/sdk/model/clvalue/CLValueByteArray.java @@ -1,7 +1,6 @@ package com.casper.sdk.model.clvalue; import com.casper.sdk.annotation.ExcludeFromJacocoGeneratedReport; -import com.casper.sdk.exception.NoSuchTypeException; import com.casper.sdk.model.clvalue.cltype.CLTypeByteArray; import com.fasterxml.jackson.annotation.JsonProperty; import dev.oak3.sbs4j.DeserializerBuffer; @@ -42,12 +41,6 @@ protected void serializeValue(final SerializerBuffer ser) throws ValueSerializat this.setBytes(Hex.toHexString(getValue())); } - @Override - protected void encodeType(final SerializerBuffer ser) throws NoSuchTypeException { - super.encodeType(ser); - ser.writeI32(this.getClType().getLength()); - } - @Override public void deserializeCustom(final DeserializerBuffer deser) throws Exception { this.setValue(deser.readByteArray(this.getClType().getLength())); diff --git a/src/main/java/com/casper/sdk/model/clvalue/CLValueList.java b/src/main/java/com/casper/sdk/model/clvalue/CLValueList.java index 1a67ac1e2..8200d59da 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/CLValueList.java +++ b/src/main/java/com/casper/sdk/model/clvalue/CLValueList.java @@ -1,6 +1,5 @@ package com.casper.sdk.model.clvalue; -import com.casper.sdk.exception.NoSuchTypeException; import com.casper.sdk.model.clvalue.cltype.AbstractCLTypeWithChildren; import com.casper.sdk.model.clvalue.cltype.CLTypeData; import com.casper.sdk.model.clvalue.cltype.CLTypeList; @@ -67,11 +66,6 @@ protected void serializeValue(final SerializerBuffer ser) throws ValueSerializat this.setBytes(Hex.toHexString(bytes)); } - @Override - protected void encodeChildTypes(final SerializerBuffer ser) throws NoSuchTypeException { - final byte val = (getClType().getListType().getClTypeData().getSerializationTag()); - ser.writeU8(val); - } @Override public void deserializeCustom(final DeserializerBuffer deser) throws Exception { diff --git a/src/main/java/com/casper/sdk/model/clvalue/CLValueMap.java b/src/main/java/com/casper/sdk/model/clvalue/CLValueMap.java index e7ea4d410..e5d81c638 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/CLValueMap.java +++ b/src/main/java/com/casper/sdk/model/clvalue/CLValueMap.java @@ -1,6 +1,5 @@ package com.casper.sdk.model.clvalue; -import com.casper.sdk.exception.NoSuchTypeException; import com.casper.sdk.model.clvalue.cltype.AbstractCLTypeWithChildren; import com.casper.sdk.model.clvalue.cltype.CLTypeData; import com.casper.sdk.model.clvalue.cltype.CLTypeMap; @@ -70,18 +69,13 @@ protected void serializeValue(final SerializerBuffer ser) throws ValueSerializat this.setBytes(Hex.toHexString(bytes)); } - @Override - protected void encodeChildTypes(final SerializerBuffer ser) throws NoSuchTypeException { - - final byte keyTypeTag = (getClType().getKeyValueTypes().getKeyType().getClTypeData().getSerializationTag()); - ser.writeU8(keyTypeTag); - - final byte valueTypeTag = (getClType().getKeyValueTypes().getValueType().getClTypeData().getSerializationTag()); - ser.writeU8(valueTypeTag); - } @Override public void deserializeCustom(final DeserializerBuffer deser) throws Exception { + if (this.clType.getChildTypes().isEmpty()) { + this.clType.deserializeChildTypes(deser); + } + final CLTypeData keyType = clType.getKeyValueTypes().getKeyType().getClTypeData(); final CLTypeData valType = clType.getKeyValueTypes().getValueType().getClTypeData(); @@ -116,6 +110,7 @@ public void deserializeCustom(final DeserializerBuffer deser) throws Exception { setValue(map); } + @Override @JsonIgnore protected void setChildTypes(final Map, ? extends AbstractCLValue> value) { @@ -127,9 +122,14 @@ protected void setChildTypes(final Map, ? extend } } - // This needed to be customized to ensure equality is being checked correctly. - // The java Map equals method tries to get the "other" map entry's value by using "this" key object, - // which then fails to find the object since they are "different" and returns always null. + /** + * This needed to be customized to ensure equality is being checked correctly. + * The java Map equals method tries to get the "other" map entry's value by using "this" key object, + * which then fails to find the object since they are "different" and returns always null. + * + * @param o the object to compare + * @return true if the objects are equal, false otherwise + */ @Override public boolean equals(final Object o) { if (o == this) return true; @@ -180,6 +180,15 @@ public int hashCode() { @Override public String toString() { - return getValue() != null ? getValue().keySet().stream().map(key -> key.getValue().toString() + "=" + key.getValue().toString()).collect(Collectors.joining(", ")) : null; + if (getValue() == null) { + return null; + } else { + return getValue() + .entrySet() + .stream() + .map(entry -> + entry.getKey().toString() + "=" + entry.getValue().toString()).collect(Collectors.joining(", ") + ); + } } } diff --git a/src/main/java/com/casper/sdk/model/clvalue/CLValueOption.java b/src/main/java/com/casper/sdk/model/clvalue/CLValueOption.java index 38e5ab2f5..ded0ed933 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/CLValueOption.java +++ b/src/main/java/com/casper/sdk/model/clvalue/CLValueOption.java @@ -1,6 +1,5 @@ package com.casper.sdk.model.clvalue; -import com.casper.sdk.exception.NoSuchTypeException; import com.casper.sdk.model.clvalue.cltype.*; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonSetter; @@ -92,14 +91,6 @@ public void deserializeCustom(final DeserializerBuffer deser) throws Exception { setValue(Optional.of(child)); } - @Override - protected void encodeChildTypes(final SerializerBuffer ser) throws NoSuchTypeException { - final Optional> child = getValue(); - if (child.isPresent()) { - encodeChildType(ser, child.get(), this.getClType().getOptionType().getClTypeData()); - } - } - @Override protected void setChildTypes(final Optional> value) { if (value.isPresent()) { diff --git a/src/main/java/com/casper/sdk/model/clvalue/CLValueTuple1.java b/src/main/java/com/casper/sdk/model/clvalue/CLValueTuple1.java index 710a1d1f3..3d7be7f85 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/CLValueTuple1.java +++ b/src/main/java/com/casper/sdk/model/clvalue/CLValueTuple1.java @@ -1,6 +1,5 @@ package com.casper.sdk.model.clvalue; -import com.casper.sdk.exception.NoSuchTypeException; import com.casper.sdk.model.clvalue.cltype.AbstractCLTypeWithChildren; import com.casper.sdk.model.clvalue.cltype.CLTypeData; import com.casper.sdk.model.clvalue.cltype.CLTypeTuple1; @@ -56,11 +55,6 @@ protected void serializeValue(final SerializerBuffer ser) throws ValueSerializat this.setBytes(Hex.toHexString(bytes)); } - @Override - protected void encodeChildTypes(final SerializerBuffer ser) throws NoSuchTypeException { - encodeChildType(ser, this.getValue().getValue0(), getClType().getChildClTypeData(0)); - } - @Override public void deserializeCustom(final DeserializerBuffer deser) throws Exception { CLTypeData childTypeData1 = clType.getChildClTypeData(0); diff --git a/src/main/java/com/casper/sdk/model/clvalue/CLValueTuple2.java b/src/main/java/com/casper/sdk/model/clvalue/CLValueTuple2.java index 3e02df674..1fcf0fd8f 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/CLValueTuple2.java +++ b/src/main/java/com/casper/sdk/model/clvalue/CLValueTuple2.java @@ -1,6 +1,5 @@ package com.casper.sdk.model.clvalue; -import com.casper.sdk.exception.NoSuchTypeException; import com.casper.sdk.model.clvalue.cltype.AbstractCLTypeWithChildren; import com.casper.sdk.model.clvalue.cltype.CLTypeData; import com.casper.sdk.model.clvalue.cltype.CLTypeTuple2; @@ -59,13 +58,6 @@ protected void serializeValue(final SerializerBuffer ser) throws ValueSerializat } - @Override - protected void encodeChildTypes(final SerializerBuffer ser) throws NoSuchTypeException { - encodeChildType(ser, this.getValue().getValue0(), getClType().getChildClTypeData(0)); - encodeChildType(ser, this.getValue().getValue1(), getClType().getChildClTypeData(1)); - } - - @Override public void deserializeCustom(final DeserializerBuffer deser) throws Exception { final CLTypeData childTypeData1 = clType.getChildClTypeData(0); diff --git a/src/main/java/com/casper/sdk/model/clvalue/CLValueTuple3.java b/src/main/java/com/casper/sdk/model/clvalue/CLValueTuple3.java index d52a314c4..e48e1674d 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/CLValueTuple3.java +++ b/src/main/java/com/casper/sdk/model/clvalue/CLValueTuple3.java @@ -1,6 +1,5 @@ package com.casper.sdk.model.clvalue; -import com.casper.sdk.exception.NoSuchTypeException; import com.casper.sdk.model.clvalue.cltype.AbstractCLTypeWithChildren; import com.casper.sdk.model.clvalue.cltype.CLTypeData; import com.casper.sdk.model.clvalue.cltype.CLTypeTuple3; @@ -59,13 +58,6 @@ protected void serializeValue(final SerializerBuffer ser) throws ValueSerializat this.setBytes(Hex.toHexString(bytes)); } - @Override - protected void encodeChildTypes(SerializerBuffer ser) throws NoSuchTypeException { - encodeChildType(ser, this.getValue().getValue0(), getClType().getChildClTypeData(0)); - encodeChildType(ser, this.getValue().getValue1(), getClType().getChildClTypeData(1)); - encodeChildType(ser, this.getValue().getValue2(), getClType().getChildClTypeData(2)); - } - @Override public void deserializeCustom(final DeserializerBuffer deser) throws Exception { final CLTypeData childTypeData1 = clType.getChildClTypeData(0); diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/AbstractCLType.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/AbstractCLType.java index f3d4974eb..8b9f89a80 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/AbstractCLType.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/AbstractCLType.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.databind.annotation.JsonTypeResolver; +import dev.oak3.sbs4j.SerializerBuffer; import lombok.Getter; import lombok.Setter; @@ -67,7 +68,7 @@ public CLTypeData getClTypeData() throws NoSuchTypeException { } /** - * Indicates if the CLType does not contains and 'Any', or other un-deserializable child type from bytes. The reason + * Indicates if the CLType does not contain an 'Any', or other un-deserializable child type from bytes. The reason * for this is the 'Any' type does not provide a length for its bytes. This type information is obtained from the * JSON metadata. * @@ -76,4 +77,15 @@ public CLTypeData getClTypeData() throws NoSuchTypeException { */ @JsonIgnore public abstract boolean isDeserializable(); + + + /** + * Serializes the type to the provided buffer + * + * @param ser the buffer to serialize to + * @throws NoSuchTypeException if the type is not supported + */ + public void serialize(final SerializerBuffer ser) throws NoSuchTypeException { + ser.writeU8(getClTypeData().getSerializationTag()); + } } diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/AbstractCLTypeBasic.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/AbstractCLTypeBasic.java index 0a3104222..64a68421a 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/AbstractCLTypeBasic.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/AbstractCLTypeBasic.java @@ -11,7 +11,7 @@ */ @NoArgsConstructor public abstract class AbstractCLTypeBasic extends AbstractCLType { - protected AbstractCLTypeBasic(String typeName) { + protected AbstractCLTypeBasic(final String typeName) { if (!this.getTypeName().equals(typeName)) { throw new IllegalArgumentException( String.format("%s is an invalid type for %s", getClass().getSimpleName(), typeName)); diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/AbstractCLTypeWithChildren.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/AbstractCLTypeWithChildren.java index 438ec3740..3729412b8 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/AbstractCLTypeWithChildren.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/AbstractCLTypeWithChildren.java @@ -1,7 +1,11 @@ package com.casper.sdk.model.clvalue.cltype; +import com.casper.sdk.exception.DynamicInstanceException; import com.casper.sdk.exception.NoSuchTypeException; import com.fasterxml.jackson.annotation.JsonIgnore; +import dev.oak3.sbs4j.DeserializerBuffer; +import dev.oak3.sbs4j.SerializerBuffer; +import dev.oak3.sbs4j.exception.ValueDeserializationException; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; @@ -27,10 +31,9 @@ public abstract class AbstractCLTypeWithChildren extends AbstractCLType { @JsonIgnore private List childTypes = new ArrayList<>(); - private List childTypeObjects; - protected void setChildTypeObjects(List childTypeObjects) + protected void setChildTypeObjects(final List childTypeObjects) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, NoSuchTypeException { this.childTypeObjects = childTypeObjects; @@ -55,7 +58,7 @@ protected List getChildTypeObjects() { } @JsonIgnore - public CLTypeData getChildClTypeData(int index) throws NoSuchTypeException { + public CLTypeData getChildClTypeData(final int index) throws NoSuchTypeException { return CLTypeData.getTypeByName(getChildTypes().get(index).getTypeName()); } @@ -64,7 +67,7 @@ public boolean isDeserializable() { return getChildTypes().stream().allMatch(AbstractCLType::isDeserializable); } - protected void loadCLTypes(List childTypeObjects) + protected void loadCLTypes(final List childTypeObjects) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, NoSuchTypeException { childTypes.clear(); @@ -76,7 +79,7 @@ protected void loadCLTypes(List childTypeObjects) } } - private void addChildType(Object childTypeObject, List parent) + private void addChildType(final Object childTypeObject, final List parent) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, NoSuchTypeException { if (childTypeObject instanceof String) { @@ -97,4 +100,38 @@ private void addChildType(Object childTypeObject, List parent) } } } + + @Override + public void serialize(final SerializerBuffer ser) throws NoSuchTypeException { + super.serialize(ser); + serializeChildTypes(ser); + } + + /** + * Updates the child types from the deserializer buffer. + * + * @param deser the deserializer buffer + */ + public abstract void deserializeChildTypes(final DeserializerBuffer deser) + throws ValueDeserializationException, NoSuchTypeException, DynamicInstanceException; + + /** + * Writes the child types to the serialization buffer. + * + * @param ser the serialization buffer + */ + protected abstract void serializeChildTypes(final SerializerBuffer ser) throws NoSuchTypeException; + + protected AbstractCLType deserializeChildType(final DeserializerBuffer deser) + throws ValueDeserializationException, NoSuchTypeException, DynamicInstanceException { + final int childTypeTag = deser.readU8(); + final CLTypeData childType = CLTypeData.getTypeBySerializationTag((byte) childTypeTag); + final AbstractCLType clChildType = CLTypeData.createCLTypeFromCLTypeName(childType.getClTypeName()); + + if (clChildType instanceof AbstractCLTypeWithChildren) { + ((AbstractCLTypeWithChildren) clChildType).deserializeChildTypes(deser); + } + + return clChildType; + } } diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeAny.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeAny.java index c32c2151d..dedb0fbc0 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeAny.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeAny.java @@ -20,7 +20,7 @@ public class CLTypeAny extends AbstractCLTypeBasic { private final String typeName = AbstractCLType.ANY; @JsonCreator - protected CLTypeAny(String typeName) { + protected CLTypeAny(final String typeName) { super(typeName); } } diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeBool.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeBool.java index 9ccb7b54a..d882e53a6 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeBool.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeBool.java @@ -20,7 +20,7 @@ public class CLTypeBool extends AbstractCLTypeBasic { private final String typeName = AbstractCLType.BOOL; @JsonCreator - protected CLTypeBool(String typeName) { + protected CLTypeBool(final String typeName) { super(typeName); } } diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeByteArray.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeByteArray.java index a0bab9f67..c3ea42fe8 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeByteArray.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeByteArray.java @@ -1,6 +1,8 @@ package com.casper.sdk.model.clvalue.cltype; +import com.casper.sdk.exception.NoSuchTypeException; import com.fasterxml.jackson.annotation.JsonProperty; +import dev.oak3.sbs4j.SerializerBuffer; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; @@ -26,4 +28,10 @@ public class CLTypeByteArray extends AbstractCLType { public boolean isDeserializable() { return true; } + + @Override + public void serialize(final SerializerBuffer ser) throws NoSuchTypeException { + super.serialize(ser); + ser.writeI32(getLength()); + } } diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeData.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeData.java index 5336098ef..b2547421a 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeData.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeData.java @@ -59,7 +59,7 @@ public enum CLTypeData { * @return the requested {@link CLTypeData} * @throws NoSuchTypeException raised when the clType is not valid/found */ - public static CLTypeData getTypeBySerializationTag(byte serializationTag) throws NoSuchTypeException { + public static CLTypeData getTypeBySerializationTag(final byte serializationTag) throws NoSuchTypeException { for (CLTypeData clType : values()) { if (clType.serializationTag == serializationTag) { return clType; diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeI32.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeI32.java index 68024e797..a38537cf7 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeI32.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeI32.java @@ -20,7 +20,7 @@ public class CLTypeI32 extends AbstractCLTypeBasic { private final String typeName = I32; @JsonCreator - protected CLTypeI32(String typeName) { + protected CLTypeI32(final String typeName) { super(typeName); } } diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeI64.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeI64.java index 386a07927..a000c9542 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeI64.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeI64.java @@ -20,7 +20,7 @@ public class CLTypeI64 extends AbstractCLTypeBasic { private final String typeName = I64; @JsonCreator - protected CLTypeI64(String typeName) { + protected CLTypeI64(final String typeName) { super(typeName); } } diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeKey.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeKey.java index 0bd24f622..d84517d15 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeKey.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeKey.java @@ -20,7 +20,7 @@ public class CLTypeKey extends AbstractCLTypeBasic { private final String typeName = AbstractCLType.KEY; @JsonCreator - protected CLTypeKey(String typeName) { + protected CLTypeKey(final String typeName) { super(typeName); } } diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeList.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeList.java index 018a21bf7..dbbea1761 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeList.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeList.java @@ -1,9 +1,14 @@ package com.casper.sdk.model.clvalue.cltype; import com.casper.sdk.annotation.ExcludeFromJacocoGeneratedReport; +import com.casper.sdk.exception.DynamicInstanceException; +import com.casper.sdk.exception.NoSuchTypeException; import com.fasterxml.jackson.annotation.JsonGetter; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonSetter; +import dev.oak3.sbs4j.DeserializerBuffer; +import dev.oak3.sbs4j.SerializerBuffer; +import dev.oak3.sbs4j.exception.ValueDeserializationException; import lombok.EqualsAndHashCode; import lombok.Getter; @@ -32,7 +37,7 @@ protected Object getJsonValue() { @JsonSetter(AbstractCLType.LIST) @ExcludeFromJacocoGeneratedReport - protected void setJsonValue(AbstractCLType clType) { + protected void setJsonValue(final AbstractCLType clType) { getChildTypes().add(clType); } @@ -46,7 +51,7 @@ public AbstractCLType getListType() { } @JsonIgnore - public void setListType(AbstractCLType listType) { + public void setListType(final AbstractCLType listType) { getChildTypes().clear(); getChildTypes().add(listType); } @@ -62,4 +67,17 @@ public boolean isDeserializable() { } }); } + + @Override + public void serializeChildTypes(final SerializerBuffer ser) throws NoSuchTypeException { + if (!this.getChildTypes().isEmpty()) { + this.getChildTypes().get(0).serialize(ser); + } + } + + @Override + public void deserializeChildTypes(final DeserializerBuffer deser) + throws ValueDeserializationException, NoSuchTypeException, DynamicInstanceException { + setListType(deserializeChildType(deser)); + } } diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeMap.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeMap.java index 01c158e43..276445161 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeMap.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeMap.java @@ -1,10 +1,15 @@ package com.casper.sdk.model.clvalue.cltype; import com.casper.sdk.annotation.ExcludeFromJacocoGeneratedReport; +import com.casper.sdk.exception.DynamicInstanceException; +import com.casper.sdk.exception.NoSuchTypeException; import com.fasterxml.jackson.annotation.JsonGetter; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonSetter; +import dev.oak3.sbs4j.DeserializerBuffer; +import dev.oak3.sbs4j.SerializerBuffer; +import dev.oak3.sbs4j.exception.ValueDeserializationException; import lombok.*; /** @@ -21,7 +26,7 @@ public class CLTypeMap extends AbstractCLTypeWithChildren { private final String typeName = AbstractCLType.MAP; @JsonProperty(MAP) - public void setKeyValueTypes(CLTypeMapEntryType keyValueTypes) { + public void setKeyValueTypes(final CLTypeMapEntryType keyValueTypes) { this.keyValueTypes = keyValueTypes; getChildTypes().add(this.keyValueTypes.getKeyType()); getChildTypes().add(this.keyValueTypes.getValueType()); @@ -36,7 +41,7 @@ public boolean isDeserializable() { // The map contains an 'Any' type therefore cannot be deserialized return false; } else if (getKeyValueTypes().valueType instanceof AbstractCLTypeWithChildren) { - return getChildTypes().stream().allMatch(childType -> { + return getChildTypes().stream().allMatch(childType -> { if (childType instanceof CLTypeAny) { return false; } else { @@ -69,7 +74,7 @@ public static class CLTypeMapEntryType { @JsonSetter("key") @ExcludeFromJacocoGeneratedReport - protected void setJsonKey(AbstractCLType clType) { + protected void setJsonKey(final AbstractCLType clType) { this.keyType = clType; } @@ -85,7 +90,7 @@ protected Object getJsonKey() { @JsonSetter("value") @ExcludeFromJacocoGeneratedReport - protected void setJsonValue(AbstractCLType clType) { + protected void setJsonValue(final AbstractCLType clType) { this.valueType = clType; } @@ -99,4 +104,19 @@ protected Object getJsonValue() { } } } + + @Override + public void serializeChildTypes(final SerializerBuffer ser) throws NoSuchTypeException { + getKeyValueTypes().getKeyType().serialize(ser); + getKeyValueTypes().getValueType().serialize(ser); + } + + @Override + public void deserializeChildTypes(final DeserializerBuffer deser) + throws ValueDeserializationException, NoSuchTypeException, DynamicInstanceException { + // read child types + final AbstractCLType clTypeKey = deserializeChildType(deser); + final AbstractCLType clTypeValue = deserializeChildType(deser); + setKeyValueTypes(new CLTypeMap.CLTypeMapEntryType(clTypeKey, clTypeValue)); + } } diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeOption.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeOption.java index 58d395ae1..9c9744264 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeOption.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeOption.java @@ -1,9 +1,14 @@ package com.casper.sdk.model.clvalue.cltype; import com.casper.sdk.annotation.ExcludeFromJacocoGeneratedReport; +import com.casper.sdk.exception.DynamicInstanceException; +import com.casper.sdk.exception.NoSuchTypeException; import com.fasterxml.jackson.annotation.JsonGetter; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonSetter; +import dev.oak3.sbs4j.DeserializerBuffer; +import dev.oak3.sbs4j.SerializerBuffer; +import dev.oak3.sbs4j.exception.ValueDeserializationException; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NoArgsConstructor; @@ -34,7 +39,7 @@ protected Object getJsonClType() { @JsonSetter(OPTION) @ExcludeFromJacocoGeneratedReport - protected void setJsonClType(AbstractCLType clType) { + protected void setJsonClType(final AbstractCLType clType) { getChildTypes().add(clType); } @@ -48,7 +53,7 @@ public AbstractCLType getOptionType() { } @JsonIgnore - public void setOptionType(AbstractCLType listType) { + public void setOptionType(final AbstractCLType listType) { getChildTypes().clear(); getChildTypes().add(listType); } @@ -57,4 +62,14 @@ public void setOptionType(AbstractCLType listType) { public boolean isDeserializable() { return getOptionType().isDeserializable(); } + + @Override + public void serializeChildTypes(final SerializerBuffer ser) throws NoSuchTypeException { + getOptionType().serialize(ser); + } + + @Override + public void deserializeChildTypes(final DeserializerBuffer deser) throws ValueDeserializationException, NoSuchTypeException, DynamicInstanceException { + setOptionType(deserializeChildType(deser)); + } } diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypePublicKey.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypePublicKey.java index 7645300b3..a226c4f56 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypePublicKey.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypePublicKey.java @@ -20,7 +20,7 @@ public class CLTypePublicKey extends AbstractCLTypeBasic { private final String typeName = PUBLIC_KEY; @JsonCreator - protected CLTypePublicKey(String typeName) { + protected CLTypePublicKey(final String typeName) { super(typeName); } } diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeResult.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeResult.java index ec3c26a60..9b89fae77 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeResult.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeResult.java @@ -46,7 +46,7 @@ public static class CLTypeResultOkErrTypes { @JsonSetter("ok") @ExcludeFromJacocoGeneratedReport - protected void setJsonKey(AbstractCLType clType) { + protected void setJsonKey(final AbstractCLType clType) { this.okClType = clType; } @@ -62,7 +62,7 @@ protected Object getJsonKey() { @JsonSetter("err") @ExcludeFromJacocoGeneratedReport - protected void setJsonValue(AbstractCLType clType) { + protected void setJsonValue(final AbstractCLType clType) { this.errClType = clType; } diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeString.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeString.java index 04425eb44..c39c483b7 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeString.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeString.java @@ -20,7 +20,7 @@ public class CLTypeString extends AbstractCLTypeBasic { private final String typeName = STRING; @JsonCreator - protected CLTypeString(String typeName) { + protected CLTypeString(final String typeName) { super(typeName); } } diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeTuple1.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeTuple1.java index 3cb5ac403..43e9e9cf5 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeTuple1.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeTuple1.java @@ -1,7 +1,11 @@ package com.casper.sdk.model.clvalue.cltype; +import com.casper.sdk.exception.DynamicInstanceException; import com.casper.sdk.exception.NoSuchTypeException; import com.fasterxml.jackson.annotation.JsonProperty; +import dev.oak3.sbs4j.DeserializerBuffer; +import dev.oak3.sbs4j.SerializerBuffer; +import dev.oak3.sbs4j.exception.ValueDeserializationException; import lombok.EqualsAndHashCode; import lombok.Getter; @@ -23,7 +27,7 @@ public class CLTypeTuple1 extends AbstractCLTypeWithChildren { @Override @JsonProperty(TUPLE1) - protected void setChildTypeObjects(List childTypeObjects) + protected void setChildTypeObjects(final List childTypeObjects) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, NoSuchTypeException { super.setChildTypeObjects(childTypeObjects); @@ -34,4 +38,17 @@ protected void setChildTypeObjects(List childTypeObjects) protected List getChildTypeObjects() { return super.getChildTypeObjects(); } + + @Override + public void serializeChildTypes(final SerializerBuffer ser) throws NoSuchTypeException { + if (!getChildTypes().isEmpty()) { + getChildTypes().get(0).serialize(ser); + } + } + + @Override + public void deserializeChildTypes(final DeserializerBuffer deser) + throws ValueDeserializationException, NoSuchTypeException, DynamicInstanceException { + getChildTypes().add(deserializeChildType(deser)); + } } diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeTuple2.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeTuple2.java index 372d0a358..bcf76fa06 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeTuple2.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeTuple2.java @@ -1,7 +1,11 @@ package com.casper.sdk.model.clvalue.cltype; +import com.casper.sdk.exception.DynamicInstanceException; import com.casper.sdk.exception.NoSuchTypeException; import com.fasterxml.jackson.annotation.JsonProperty; +import dev.oak3.sbs4j.DeserializerBuffer; +import dev.oak3.sbs4j.SerializerBuffer; +import dev.oak3.sbs4j.exception.ValueDeserializationException; import lombok.EqualsAndHashCode; import lombok.Getter; @@ -19,11 +23,12 @@ @Getter @EqualsAndHashCode(callSuper = true, of = {"typeName"}) public class CLTypeTuple2 extends AbstractCLTypeWithChildren { + private final String typeName = AbstractCLType.TUPLE2; @Override @JsonProperty(AbstractCLType.TUPLE2) - protected void setChildTypeObjects(List childTypeObjects) + protected void setChildTypeObjects(final List childTypeObjects) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, NoSuchTypeException { super.setChildTypeObjects(childTypeObjects); @@ -34,4 +39,20 @@ protected void setChildTypeObjects(List childTypeObjects) protected List getChildTypeObjects() { return super.getChildTypeObjects(); } + + @Override + public void serializeChildTypes(final SerializerBuffer ser) throws NoSuchTypeException { + + if (getChildTypes().size() >= 2) { + getChildTypes().get(0).serialize(ser); + getChildTypes().get(1).serialize(ser); + } + } + + @Override + public void deserializeChildTypes(final DeserializerBuffer deser) + throws ValueDeserializationException, NoSuchTypeException, DynamicInstanceException { + getChildTypes().add(deserializeChildType(deser)); + getChildTypes().add(deserializeChildType(deser)); + } } diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeTuple3.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeTuple3.java index abe4fad7a..f8177bc69 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeTuple3.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeTuple3.java @@ -1,7 +1,11 @@ package com.casper.sdk.model.clvalue.cltype; +import com.casper.sdk.exception.DynamicInstanceException; import com.casper.sdk.exception.NoSuchTypeException; import com.fasterxml.jackson.annotation.JsonProperty; +import dev.oak3.sbs4j.DeserializerBuffer; +import dev.oak3.sbs4j.SerializerBuffer; +import dev.oak3.sbs4j.exception.ValueDeserializationException; import lombok.EqualsAndHashCode; import lombok.Getter; @@ -23,7 +27,7 @@ public class CLTypeTuple3 extends AbstractCLTypeWithChildren { @Override @JsonProperty(AbstractCLType.TUPLE3) - protected void setChildTypeObjects(List childTypeObjects) + protected void setChildTypeObjects(final List childTypeObjects) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, NoSuchTypeException { super.setChildTypeObjects(childTypeObjects); @@ -34,4 +38,21 @@ protected void setChildTypeObjects(List childTypeObjects) protected List getChildTypeObjects() { return super.getChildTypeObjects(); } + + public void serializeChildTypes(SerializerBuffer ser) throws NoSuchTypeException { + + if (getChildTypes().size() >= 3) { + getChildTypes().get(0).serialize(ser); + getChildTypes().get(1).serialize(ser); + getChildTypes().get(2).serialize(ser); + } + } + + @Override + public void deserializeChildTypes(final DeserializerBuffer deser) + throws ValueDeserializationException, NoSuchTypeException, DynamicInstanceException { + getChildTypes().add(deserializeChildType(deser)); + getChildTypes().add(deserializeChildType(deser)); + getChildTypes().add(deserializeChildType(deser)); + } } diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeU128.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeU128.java index 7a0b03075..40852c4ca 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeU128.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeU128.java @@ -21,7 +21,7 @@ public class CLTypeU128 extends AbstractCLTypeBasic { private final String typeName = U128; @JsonCreator - protected CLTypeU128(String typeName) { + protected CLTypeU128(final String typeName) { super(typeName); } } diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeU256.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeU256.java index 2ff4c34b8..f645fb252 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeU256.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeU256.java @@ -21,7 +21,7 @@ public class CLTypeU256 extends AbstractCLTypeBasic { private final String typeName = U256; @JsonCreator - protected CLTypeU256(String typeName) { + protected CLTypeU256(final String typeName) { super(typeName); } } diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeU32.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeU32.java index 9e80e26e5..831a6a8e9 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeU32.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeU32.java @@ -20,7 +20,7 @@ public class CLTypeU32 extends AbstractCLTypeBasic { private final String typeName = U32; @JsonCreator - protected CLTypeU32(String typeName) { + protected CLTypeU32(final String typeName) { super(typeName); } } diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeU512.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeU512.java index cd515a81c..8b12cd99b 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeU512.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeU512.java @@ -21,7 +21,7 @@ public class CLTypeU512 extends AbstractCLTypeBasic { private final String typeName = AbstractCLType.U512; @JsonCreator - protected CLTypeU512(String typeName) { + protected CLTypeU512(final String typeName) { super(typeName); } } diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeU64.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeU64.java index a07e72b97..4e1e4cc66 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeU64.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeU64.java @@ -21,7 +21,7 @@ public class CLTypeU64 extends AbstractCLTypeBasic { private final String typeName = U64; @JsonCreator - protected CLTypeU64(String typeName) { + protected CLTypeU64(final String typeName) { super(typeName); } } diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeU8.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeU8.java index 52d9fa5ad..66015e45e 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeU8.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeU8.java @@ -20,7 +20,7 @@ public class CLTypeU8 extends AbstractCLTypeBasic { private final String typeName = AbstractCLType.U8; @JsonCreator - protected CLTypeU8(String typeName) { + protected CLTypeU8(final String typeName) { super(typeName); } } diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeURef.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeURef.java index 8cf7129b8..06a0270e7 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeURef.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeURef.java @@ -21,7 +21,7 @@ public class CLTypeURef extends AbstractCLTypeBasic { private final String typeName = AbstractCLType.UREF; @JsonCreator - protected CLTypeURef(String typeName) { + protected CLTypeURef(final String typeName) { super(typeName); } } diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeUnit.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeUnit.java index 847e3457d..9b9c57d27 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeUnit.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeUnit.java @@ -21,7 +21,7 @@ public class CLTypeUnit extends AbstractCLTypeBasic { private final String typeName = UNIT; @JsonCreator - protected CLTypeUnit(String typeName) { + protected CLTypeUnit(final String typeName) { super(typeName); } } diff --git a/src/test/java/com/casper/sdk/model/clvalue/CLValueTests.java b/src/test/java/com/casper/sdk/model/clvalue/CLValueTests.java index 2de521603..7d5562ac9 100644 --- a/src/test/java/com/casper/sdk/model/clvalue/CLValueTests.java +++ b/src/test/java/com/casper/sdk/model/clvalue/CLValueTests.java @@ -2,10 +2,7 @@ import com.casper.sdk.exception.DynamicInstanceException; import com.casper.sdk.exception.NoSuchTypeException; -import com.casper.sdk.model.clvalue.cltype.CLTypeAny; -import com.casper.sdk.model.clvalue.cltype.CLTypeData; -import com.casper.sdk.model.clvalue.cltype.CLTypeMap; -import com.casper.sdk.model.clvalue.cltype.CLTypeTuple1; +import com.casper.sdk.model.clvalue.cltype.*; import com.casper.sdk.model.clvalue.serde.Target; import com.casper.sdk.model.deploy.NamedArg; import com.syntifi.crypto.key.encdec.Hex; @@ -16,10 +13,11 @@ import org.javatuples.Unit; import org.junit.jupiter.api.Test; -import java.util.HashMap; -import java.util.Map; +import java.math.BigInteger; +import java.util.*; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.core.Is.is; import static org.junit.jupiter.api.Assertions.*; @@ -168,6 +166,9 @@ void nestedTuple1Serialization() throws Exception { final byte[] expected = {4, 0, 0, 0, 1, 0, 0, 0, 18, 18, 4}; assertThat(ser.toByteArray(), is(expected)); + + final CLValueTuple1 deserialized = (CLValueTuple1) outerTuple1.deserialize(new DeserializerBuffer(expected), Target.BYTE); + assertThat(deserialized.getBytes(), is(outerTuple1.getBytes())); } @Test @@ -197,4 +198,129 @@ void nestedTuple3Serialization() throws Exception { assertThat(ser.toByteArray(), is(expected)); } + + @Test + void nestedMapSerialization() throws Exception { + + Map map = new LinkedHashMap<>(); + map.put(new CLValueString("ONE"), new CLValueU32(1L)); + final CLValueMap innerMap1 = new CLValueMap(map); + + map = new LinkedHashMap<>(); + map.put(new CLValueString("TWO"), new CLValueU32(2L)); + final CLValueMap innerMap2 = new CLValueMap(map); + + + Map map2 = new LinkedHashMap<>(); + map2.put(new CLValueString("THREE"), innerMap1); + map2.put(new CLValueString("FOUR"), innerMap2); + + final CLValueMap outerMap = new CLValueMap(map2); + + final SerializerBuffer ser = new SerializerBuffer(); + outerMap.serialize(ser, Target.BYTE); + + byte[] expected = {51, 0, 0, 0, 2, 0, 0, 0, 5, 0, 0, 0, 84, 72, 82, 69, 69, 1, 0, 0, 0, 3, 0, 0, 0, 79, 78, 69, 1, 0, 0, 0, 4, 0, 0, 0, 70, 79, 85, 82, 1, 0, 0, 0, 3, 0, 0, 0, 84, 87, 79, 2, 0, 0, 0, 17, 10, 17, 10, 4}; + + assertThat(ser.toByteArray(), is(expected)); + } + + @Test + void nestedMapDeserialization() throws Exception { + + byte[] bytes = {51, 0, 0, 0, 2, 0, 0, 0, 5, 0, 0, 0, 84, 72, 82, 69, 69, 1, 0, 0, 0, 3, 0, 0, 0, 79, 78, 69, 1, 0, 0, 0, 4, 0, 0, 0, 70, 79, 85, 82, 1, 0, 0, 0, 3, 0, 0, 0, 84, 87, 79, 2, 0, 0, 0, 17, 10, 17, 10, 4}; + + final CLTypeMap innerType = new CLTypeMap(); + innerType.setKeyValueTypes(new CLTypeMap.CLTypeMapEntryType(new CLTypeString(), new CLTypeU32())); + + final CLTypeMap outerType = new CLTypeMap(); + outerType.setKeyValueTypes(new CLTypeMap.CLTypeMapEntryType(new CLTypeString(), innerType)); + + final CLValueMap outerMap = new CLValueMap(); + outerMap.setClType(outerType); + outerMap.deserialize(new DeserializerBuffer(bytes), Target.BYTE); + } + + @SuppressWarnings("unchecked") + @Test + void nestedMapDeserialization2() throws Exception { + + // a nested map is created {1: {11: {111: "ONE_ONE_ONE"}, 12: {121: "ONE_TWO_ONE"}}, 2: {21: {211: "TWO_ONE_ONE"}, 22: {221: "TWO_TWO_ONE"}}} + //noinspection rawtypes + Map innerMap = new LinkedHashMap<>(); + innerMap.put(new CLValueU256(BigInteger.valueOf(111L)), new CLValueString("ONE_ONE_ONE")); + final CLValueMap innerMap111 = new CLValueMap(innerMap); + + innerMap = new LinkedHashMap<>(); + innerMap.put(new CLValueU256(BigInteger.valueOf(121L)), new CLValueString("ONE_TWO_ONE")); + final CLValueMap innerMap121 = new CLValueMap(innerMap); + + innerMap = new LinkedHashMap<>(); + innerMap.put(new CLValueU256(BigInteger.valueOf(11L)), innerMap111); + innerMap.put(new CLValueU256(BigInteger.valueOf(12L)), innerMap121); + final CLValueMap innerMap1 = new CLValueMap(innerMap); + + innerMap = new LinkedHashMap<>(); + innerMap.put(new CLValueU256(BigInteger.valueOf(211L)), new CLValueString("TWO_ONE_ONE")); + final CLValueMap innerMap21 = new CLValueMap(innerMap); + + innerMap = new LinkedHashMap<>(); + innerMap.put(new CLValueU256(BigInteger.valueOf(221)), new CLValueString("TWO_TWO_ONE")); + final CLValueMap innerMap22 = new CLValueMap(innerMap); + + innerMap = new LinkedHashMap<>(); + innerMap.put(new CLValueU256(BigInteger.valueOf(21L)), innerMap21); + innerMap.put(new CLValueU256(BigInteger.valueOf(22L)), innerMap22); + final CLValueMap innerMap2 = new CLValueMap(innerMap); + + innerMap = new LinkedHashMap<>(); + innerMap.put(new CLValueU256(BigInteger.valueOf(1L)), innerMap1); + innerMap.put(new CLValueU256(BigInteger.valueOf(2L)), innerMap2); + final CLValueMap outerMap = new CLValueMap(innerMap); + + final SerializerBuffer ser = new SerializerBuffer(); + outerMap.serialize(ser, Target.BYTE); + + byte[] actualBytes = ser.toByteArray(); + + assertThat(actualBytes.length, is(greaterThan(0))); + + CLValueMap clValueMap = new CLValueMap(); + clValueMap = (CLValueMap) clValueMap.deserialize(new DeserializerBuffer(actualBytes), Target.BYTE); + + assertThat(clValueMap.getBytes(), is(outerMap.getBytes())); + } + + @Test + void nestedListSerialization() throws Exception { + + final byte[] expectedBytes = {24, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 1, 1, 1, 2, 1, 3, 3, 0, 0, 0, 1, 4, 1, 5, 1, 6, 14, 14, 7}; + + List innerInernalList = new ArrayList<>(); + innerInernalList.add(new CLValueU256(BigInteger.valueOf(1L))); + innerInernalList.add(new CLValueU256(BigInteger.valueOf(2L))); + innerInernalList.add(new CLValueU256(BigInteger.valueOf(3L))); + CLValueList innerList1 = new CLValueList(innerInernalList); + + innerInernalList = new ArrayList<>(); + innerInernalList.add(new CLValueU256(BigInteger.valueOf(4L))); + innerInernalList.add(new CLValueU256(BigInteger.valueOf(5L))); + innerInernalList.add(new CLValueU256(BigInteger.valueOf(6L))); + CLValueList innerList2 = new CLValueList(innerInernalList); + + List internalOutList = new ArrayList<>(); + internalOutList.add(innerList1); + internalOutList.add(innerList2); + CLValueList outerList = new CLValueList(internalOutList); + + final SerializerBuffer ser = new SerializerBuffer(); + outerList.serialize(ser, Target.BYTE); + + byte[] actualBytes = ser.toByteArray(); + assertThat(actualBytes, is(expectedBytes)); + + CLValueList clValueList = (CLValueList) outerList.deserialize(new DeserializerBuffer(actualBytes), Target.BYTE); + + assertThat(clValueList.getBytes(), is(outerList.getBytes())); + } }