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 f90e00d86..8112bc036 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/AbstractCLValueWithChildren.java +++ b/src/main/java/com/casper/sdk/model/clvalue/AbstractCLValueWithChildren.java @@ -1,9 +1,12 @@ 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; @@ -51,4 +54,25 @@ protected void setJsonBytes(String bytes) { this.deserialize(deser); } } + + /** + * Encodes the bytes of the child type, if the child value is not present but the type is known from the parent type + * info, the childDataType is used to encode the bytes of the child rather than the child value. + * + * @param ser the serializer buffer + * @param child the child value whose type is to be encoded + * @param childDataType the data type of the child + * @throws NoSuchTypeException if the child type is not found + */ + protected void encodeChildType(final SerializerBuffer ser, + final AbstractCLValue 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); + } + } } 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 093ef84c2..e120dd5cd 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/CLValueOption.java +++ b/src/main/java/com/casper/sdk/model/clvalue/CLValueOption.java @@ -96,9 +96,9 @@ public void deserializeCustom(final DeserializerBuffer deser) throws Exception { protected void encodeType(final SerializerBuffer ser) throws NoSuchTypeException { super.encodeType(ser); - Optional> child = getValue(); + final Optional> child = getValue(); if (child.isPresent()) { - child.get().encodeType(ser); + encodeChildType(ser, child.get(), this.getClType().getOptionType().getClTypeData()); } } 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 b8f953369..4281f622e 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/CLValueTuple1.java +++ b/src/main/java/com/casper/sdk/model/clvalue/CLValueTuple1.java @@ -59,15 +59,7 @@ protected void serializeValue(final SerializerBuffer ser) throws ValueSerializat @Override protected void encodeType(final SerializerBuffer ser) throws NoSuchTypeException { super.encodeType(ser); - - final AbstractCLValue child = this.getValue().getValue0(); - if (child instanceof AbstractCLValueWithChildren) { - child.encodeType(ser); - } else { - // If there on no AbstractCLValueWithChildren as children we just need a simple tag - byte element0TypeTag = getClType().getChildClTypeData(0).getSerializationTag(); - ser.writeU8(element0TypeTag); - } + encodeChildType(ser, this.getValue().getValue0(), getClType().getChildClTypeData(0)); } @Override 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 0bd635f84..e032f76f7 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/CLValueTuple2.java +++ b/src/main/java/com/casper/sdk/model/clvalue/CLValueTuple2.java @@ -63,12 +63,11 @@ protected void serializeValue(final SerializerBuffer ser) throws ValueSerializat protected void encodeType(final SerializerBuffer ser) throws NoSuchTypeException { super.encodeType(ser); - final byte element0TypeTag = getClType().getChildClTypeData(0).getSerializationTag(); - ser.writeU8(element0TypeTag); - final byte element1TypeTag = getClType().getChildClTypeData(1).getSerializationTag(); - ser.writeU8(element1TypeTag); + 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 b48557b20..6c7dbfa0f 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/CLValueTuple3.java +++ b/src/main/java/com/casper/sdk/model/clvalue/CLValueTuple3.java @@ -63,12 +63,9 @@ protected void serializeValue(SerializerBuffer ser) throws ValueSerializationExc protected void encodeType(SerializerBuffer ser) throws NoSuchTypeException { super.encodeType(ser); - byte element0TypeTag = getClType().getChildClTypeData(0).getSerializationTag(); - ser.writeU8(element0TypeTag); - byte element1TypeTag = getClType().getChildClTypeData(1).getSerializationTag(); - ser.writeU8(element1TypeTag); - byte element2TypeTag = getClType().getChildClTypeData(2).getSerializationTag(); - ser.writeU8(element2TypeTag); + 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 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 7e74800d7..2de521603 100644 --- a/src/test/java/com/casper/sdk/model/clvalue/CLValueTests.java +++ b/src/test/java/com/casper/sdk/model/clvalue/CLValueTests.java @@ -11,6 +11,8 @@ import com.syntifi.crypto.key.encdec.Hex; import dev.oak3.sbs4j.DeserializerBuffer; import dev.oak3.sbs4j.SerializerBuffer; +import org.javatuples.Pair; +import org.javatuples.Triplet; import org.javatuples.Unit; import org.junit.jupiter.api.Test; @@ -155,10 +157,8 @@ void mapDeserialization() throws Exception { assertThat(valueDeser.getBytes(), is(value.getBytes())); } - - @Test - void nestedTupleSerialization() throws Exception { + void nestedTuple1Serialization() throws Exception { final CLValueTuple1 innerTuple1 = new CLValueTuple1(new Unit<>(new CLValueU32(1L))); final CLValueTuple1 outerTuple1 = new CLValueTuple1(new Unit<>(innerTuple1)); @@ -168,6 +168,33 @@ void nestedTupleSerialization() throws Exception { final byte[] expected = {4, 0, 0, 0, 1, 0, 0, 0, 18, 18, 4}; assertThat(ser.toByteArray(), is(expected)); + } + + @Test + void nestedTuple2Serialization() throws Exception { + final CLValueTuple2 innerTuple1 = new CLValueTuple2(new Pair<>(new CLValueU32(1L), new CLValueU32(2L))); + final CLValueTuple2 innerTuple2 = new CLValueTuple2(new Pair<>(new CLValueU32(3L), new CLValueU32(4L))); + final CLValueTuple2 outerTuple = new CLValueTuple2(new Pair<>(innerTuple1, innerTuple2)); + final SerializerBuffer ser = new SerializerBuffer(); + outerTuple.serialize(ser, Target.BYTE); + + final byte[] expected = {16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 19, 19, 4, 4, 19, 4, 4}; + + assertThat(ser.toByteArray(), is(expected)); + } + + @Test + void nestedTuple3Serialization() throws Exception { + final CLValueTuple3 innerTuple1 = new CLValueTuple3(new Triplet<>(new CLValueU32(1L), new CLValueU32(2L), new CLValueU32(3L))); + final CLValueTuple3 innerTuple2 = new CLValueTuple3(new Triplet<>(innerTuple1, new CLValueU32(4L), new CLValueU32(5L))); + final CLValueTuple3 outerTuple = new CLValueTuple3(new Triplet<>(innerTuple2, new CLValueU32(6L), new CLValueU32(7L))); + + final SerializerBuffer ser = new SerializerBuffer(); + outerTuple.serialize(ser, Target.BYTE); + + final byte[] expected = {28, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0, 0, 6, 0, 0, 0, 7, 0, 0, 0, 20, 20, 20, 4, 4, 4, 4, 4, 4, 4}; + + assertThat(ser.toByteArray(), is(expected)); } }