Skip to content

Commit

Permalink
issues/249 - Fixes for byte serialization of nested values
Browse files Browse the repository at this point in the history
  • Loading branch information
meywood committed Mar 4, 2024
1 parent 33be6ee commit 6d9e959
Show file tree
Hide file tree
Showing 27 changed files with 209 additions and 236 deletions.
37 changes: 18 additions & 19 deletions src/main/java/com/casper/sdk/model/clvalue/AbstractCLValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ public T getValue() {
return this.value;
}

public void setValue(T value) throws ValueSerializationException {
public void setValue(final T value) throws ValueSerializationException {
this.value = value;
this.serialize(new SerializerBuffer());
}

public static AbstractCLValue<?, ?> createInstanceFromBytes(DeserializerBuffer deser) throws ValueDeserializationException {
int length = deser.readI32();
byte[] bytes = deser.readByteArray(length);
byte clType = deser.readU8();
public static AbstractCLValue<?, ?> createInstanceFromBytes(final DeserializerBuffer deser) throws ValueDeserializationException {
final int length = deser.readI32();
final byte[] bytes = deser.readByteArray(length);
final byte clType = deser.readU8();
try {
AbstractCLValue<?, ?> clValue = CLTypeData.getTypeBySerializationTag(clType).getClazz().getDeclaredConstructor().newInstance();
final AbstractCLValue<?, ?> clValue = CLTypeData.getTypeBySerializationTag(clType).getClazz().getDeclaredConstructor().newInstance();
clValue.deserializeCustom(new DeserializerBuffer(Hex.encode(bytes)));
return clValue;
} catch (Exception e) {
Expand All @@ -71,7 +71,7 @@ public void setValue(T value) throws ValueSerializationException {
@JsonGetter(value = "bytes")
@ExcludeFromJacocoGeneratedReport
protected String getJsonBytes() {
SerializerBuffer ser = new SerializerBuffer();
final SerializerBuffer ser = new SerializerBuffer();
this.serialize(ser, Target.JSON);

this.bytes = ByteUtils.encodeHexString(ser.toByteArray());
Expand All @@ -82,10 +82,10 @@ protected String getJsonBytes() {
@SneakyThrows({ValueDeserializationException.class})
@JsonSetter(value = "bytes")
@ExcludeFromJacocoGeneratedReport
protected void setJsonBytes(String bytes) {
protected void setJsonBytes(final String bytes) {
this.bytes = bytes;

DeserializerBuffer deser = new DeserializerBuffer(this.bytes);
final DeserializerBuffer deser = new DeserializerBuffer(this.bytes);

this.deserialize(deser);
}
Expand All @@ -96,15 +96,15 @@ protected void setJsonBytes(String bytes) {
public abstract void setClType(P value);


protected void serializePrefixWithLength(SerializerBuffer ser) throws ValueSerializationException {
SerializerBuffer localSer = new SerializerBuffer();
protected void serializePrefixWithLength(final SerializerBuffer ser) throws ValueSerializationException {
final SerializerBuffer localSer = new SerializerBuffer();
serialize(localSer);
int size = localSer.toByteArray().length;
final int size = localSer.toByteArray().length;
ser.writeI32(size);
}

@Override
public AbstractCLValue<?, ?> deserialize(DeserializerBuffer deser, Target target) throws ValueDeserializationException {
public AbstractCLValue<?, ?> deserialize(final DeserializerBuffer deser, final Target target) throws ValueDeserializationException {
if (target.equals(Target.BYTE)) {
return AbstractCLValue.createInstanceFromBytes(deser);
} else {
Expand All @@ -114,7 +114,7 @@ protected void serializePrefixWithLength(SerializerBuffer ser) throws ValueSeria
}

@Override
public void serialize(SerializerBuffer ser, Target target) throws ValueSerializationException, NoSuchTypeException {
public void serialize(final SerializerBuffer ser, final Target target) throws ValueSerializationException, NoSuchTypeException {
if (this.getValue() == null) return;

if (target.equals(Target.BYTE)) {
Expand All @@ -128,22 +128,21 @@ public void serialize(SerializerBuffer ser, Target target) throws ValueSerializa
}
}


protected abstract void serializeValue(final SerializerBuffer ser) throws ValueSerializationException;

public abstract void deserializeCustom(DeserializerBuffer deserializerBuffer) throws Exception;
public abstract void deserializeCustom(final DeserializerBuffer deserializerBuffer) throws Exception;

@Override
public void deserialize(DeserializerBuffer deserializerBuffer) throws ValueDeserializationException {
public void deserialize(final DeserializerBuffer deserializerBuffer) throws ValueDeserializationException {
try {
this.deserializeCustom(deserializerBuffer);
} catch (Exception e) {
throw new ValueDeserializationException("Error deserializing value", e);
}
}

protected void encodeType(SerializerBuffer ser) throws NoSuchTypeException {
byte typeTag = (getClType().getClTypeData().getSerializationTag());
protected void encodeType(final SerializerBuffer ser) throws NoSuchTypeException {
final byte typeTag = (getClType().getClTypeData().getSerializationTag());
ser.writeU8(typeTag);
}
}
7 changes: 2 additions & 5 deletions src/main/java/com/casper/sdk/model/clvalue/CLValueAny.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
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.CLTypeAny;
import com.casper.sdk.model.clvalue.serde.Target;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonSetter;
import dev.oak3.sbs4j.DeserializerBuffer;
Expand Down Expand Up @@ -49,14 +47,13 @@ public CLValueAny(final byte[] value) throws ValueSerializationException {
}

@Override
protected void serializeValue(SerializerBuffer ser) throws ValueSerializationException {
protected void serializeValue(final SerializerBuffer ser) throws ValueSerializationException {
ser.writeByteArray(this.getValue());
this.setBytes(Hex.toHexString(this.getValue()));
}

@Override
public void deserializeCustom(final DeserializerBuffer deser)
throws Exception {
public void deserializeCustom(final DeserializerBuffer deser) throws Exception {
this.setValue(deser.readByteArray(deser.getBuffer().remaining()));
}

Expand Down
11 changes: 4 additions & 7 deletions src/main/java/com/casper/sdk/model/clvalue/CLValueBool.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
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.CLTypeBool;
import com.casper.sdk.model.clvalue.serde.Target;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonSetter;
import dev.oak3.sbs4j.DeserializerBuffer;
Expand All @@ -14,7 +12,6 @@
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.bouncycastle.util.encoders.Hex;
import org.jetbrains.annotations.NotNull;

/**
* Casper Bool CLValue implementation
Expand All @@ -33,7 +30,7 @@ public class CLValueBool extends AbstractCLValue<Boolean, CLTypeBool> {

@JsonSetter("cl_type")
@ExcludeFromJacocoGeneratedReport
protected void setJsonClType(CLTypeBool clType) {
protected void setJsonClType(final CLTypeBool clType) {
this.clType = clType;
}

Expand All @@ -43,18 +40,18 @@ protected String getJsonClType() {
return this.getClType().getTypeName();
}

public CLValueBool(Boolean value) throws ValueSerializationException {
public CLValueBool(final Boolean value) throws ValueSerializationException {
this.setValue(value);
}

@Override
protected void serializeValue(SerializerBuffer ser) throws ValueSerializationException {
protected void serializeValue(final SerializerBuffer ser) throws ValueSerializationException {
ser.writeBool(this.getValue());
this.setBytes(Hex.toHexString(ser.toByteArray()));
}

@Override
public void deserializeCustom(DeserializerBuffer deser) throws Exception {
public void deserializeCustom(final DeserializerBuffer deser) throws Exception {
this.setValue(deser.readBool());
}

Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/casper/sdk/model/clvalue/CLValueByteArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.casper.sdk.annotation.ExcludeFromJacocoGeneratedReport;
import com.casper.sdk.exception.NoSuchTypeException;
import com.casper.sdk.model.clvalue.cltype.CLTypeByteArray;
import com.casper.sdk.model.clvalue.serde.Target;
import com.fasterxml.jackson.annotation.JsonProperty;
import dev.oak3.sbs4j.DeserializerBuffer;
import dev.oak3.sbs4j.SerializerBuffer;
Expand Down Expand Up @@ -32,25 +31,25 @@ public class CLValueByteArray extends AbstractCLValue<byte[], CLTypeByteArray> {
@JsonProperty("cl_type")
private CLTypeByteArray clType = new CLTypeByteArray();

public CLValueByteArray(byte[] value) throws ValueSerializationException {
public CLValueByteArray(final byte[] value) throws ValueSerializationException {
this.setValue(value);
this.clType.setLength(value.length);
}

@Override
protected void serializeValue(SerializerBuffer ser) throws ValueSerializationException {
protected void serializeValue(final SerializerBuffer ser) throws ValueSerializationException {
ser.writeByteArray(this.getValue());
this.setBytes(Hex.toHexString(ser.toByteArray()));
this.setBytes(Hex.toHexString(getValue()));
}

@Override
protected void encodeType(SerializerBuffer ser) throws NoSuchTypeException {
protected void encodeType(final SerializerBuffer ser) throws NoSuchTypeException {
super.encodeType(ser);
ser.writeI32(this.getClType().getLength());
}

@Override
public void deserializeCustom(DeserializerBuffer deser) throws Exception {
public void deserializeCustom(final DeserializerBuffer deser) throws Exception {
this.setValue(deser.readByteArray(this.getClType().getLength()));
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/casper/sdk/model/clvalue/CLValueI32.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,21 @@ protected String getJsonClType() {
return this.getClType().getTypeName();
}

public CLValueI32(Integer value) throws ValueSerializationException {
public CLValueI32(final Integer value) throws ValueSerializationException {
this.setValue(value);
}

@Override
protected void serializeValue(SerializerBuffer ser) throws ValueSerializationException {
final SerializerBuffer serVal = new SerializerBuffer();
protected void serializeValue(final SerializerBuffer ser) throws ValueSerializationException {
final SerializerBuffer serVal = new SerializerBuffer();
serVal.writeI32(this.getValue());
final byte[] bytes = serVal.toByteArray();
ser.writeByteArray(bytes);
this.setBytes(Hex.toHexString(bytes));
}

@Override
public void deserializeCustom(DeserializerBuffer deser) throws Exception {
public void deserializeCustom(final DeserializerBuffer deser) throws Exception {
this.setValue(deser.readI32());
}

Expand Down
15 changes: 7 additions & 8 deletions src/main/java/com/casper/sdk/model/clvalue/CLValueI64.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
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.CLTypeI64;
import com.casper.sdk.model.clvalue.serde.Target;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonSetter;
import dev.oak3.sbs4j.DeserializerBuffer;
Expand Down Expand Up @@ -32,7 +30,7 @@ public class CLValueI64 extends AbstractCLValue<Long, CLTypeI64> {

@JsonSetter("cl_type")
@ExcludeFromJacocoGeneratedReport
protected void setJsonClType(CLTypeI64 clType) {
protected void setJsonClType(final CLTypeI64 clType) {
this.clType = clType;
}

Expand All @@ -42,20 +40,21 @@ protected String getJsonClType() {
return this.getClType().getTypeName();
}

public CLValueI64(Long value) throws ValueSerializationException {
public CLValueI64(final Long value) throws ValueSerializationException {
this.setValue(value);
}

@Override
protected void serializeValue(SerializerBuffer ser) throws ValueSerializationException {
ser.writeI64(this.getValue());
protected void serializeValue(final SerializerBuffer ser) throws ValueSerializationException {
final SerializerBuffer serVal = new SerializerBuffer();
serVal.writeI64(this.getValue());
this.setBytes(Hex.toHexString(serVal.toByteArray()));
final byte[] bytes = serVal.toByteArray();
ser.writeByteArray(bytes);
this.setBytes(Hex.toHexString(bytes));
}

@Override
public void deserializeCustom(DeserializerBuffer deser) throws Exception {
public void deserializeCustom(final DeserializerBuffer deser) throws Exception {
this.setValue(deser.readI64());
}

Expand Down
9 changes: 3 additions & 6 deletions src/main/java/com/casper/sdk/model/clvalue/CLValueKey.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
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.CLTypeKey;
import com.casper.sdk.model.clvalue.serde.Target;
import com.casper.sdk.model.key.Key;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonSetter;
Expand All @@ -15,7 +13,6 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.bouncycastle.util.encoders.Hex;

/**
* Casper Key CLValue implementation
Expand All @@ -34,7 +31,7 @@ public class CLValueKey extends AbstractCLValue<Key, CLTypeKey> {

@JsonSetter("cl_type")
@ExcludeFromJacocoGeneratedReport
protected void setJsonClType(CLTypeKey clType) {
protected void setJsonClType(final CLTypeKey clType) {
this.clType = clType;
}

Expand All @@ -44,7 +41,7 @@ protected String getJsonClType() {
return this.getClType().getTypeName();
}

public CLValueKey(Key value) throws ValueSerializationException {
public CLValueKey(final Key value) throws ValueSerializationException {
this.setValue(value);
}

Expand All @@ -56,7 +53,7 @@ protected void serializeValue(final SerializerBuffer ser) throws ValueSerializat
}

@Override
public void deserializeCustom(DeserializerBuffer deser) throws Exception {
public void deserializeCustom(final DeserializerBuffer deser) throws Exception {
this.setValue(Key.fromTaggedHexString(ByteUtils.encodeHexString(deser.readByteArray(33))));
}

Expand Down
Loading

0 comments on commit 6d9e959

Please sign in to comment.