Skip to content

Commit

Permalink
issues/251 - CLTuple2, and CLTuple3 Serialization fix
Browse files Browse the repository at this point in the history
  • Loading branch information
meywood committed Mar 7, 2024
1 parent 79458c5 commit ccce34b
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/casper/sdk/model/clvalue/CLValueOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ public void deserializeCustom(final DeserializerBuffer deser) throws Exception {
protected void encodeType(final SerializerBuffer ser) throws NoSuchTypeException {
super.encodeType(ser);

Optional<AbstractCLValue<?, ?>> child = getValue();
final Optional<AbstractCLValue<?, ?>> child = getValue();
if (child.isPresent()) {
child.get().encodeType(ser);
encodeChildType(ser, child.get(), this.getClType().getOptionType().getClTypeData());
}
}

Expand Down
10 changes: 1 addition & 9 deletions src/main/java/com/casper/sdk/model/clvalue/CLValueTuple1.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/casper/sdk/model/clvalue/CLValueTuple2.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/com/casper/sdk/model/clvalue/CLValueTuple3.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 30 additions & 3 deletions src/test/java/com/casper/sdk/model/clvalue/CLValueTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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));

Expand All @@ -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));
}
}

0 comments on commit ccce34b

Please sign in to comment.