Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues/249 - Fixes for byte serialisation of CLValues type when nested in collection types such as maps and lists etc #250

Merged
merged 4 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ apply plugin: 'java'

group = 'network.casper'
// Version number update for release
version='2.5.1'
version='2.5.2'
sourceCompatibility = 1.8
targetCompatibility = 1.8

Expand Down
50 changes: 32 additions & 18 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,21 +114,35 @@ protected void serializePrefixWithLength(SerializerBuffer ser) throws ValueSeria
}

@Override
public abstract 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;

public abstract void deserializeCustom(DeserializerBuffer deserializerBuffer) throws Exception;
if (target.equals(Target.BYTE)) {
serializePrefixWithLength(ser);
}

serializeValue(ser);

if (target.equals(Target.BYTE)) {
this.encodeType(ser);
}
}

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

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);
}
}
17 changes: 2 additions & 15 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,24 +47,13 @@ public CLValueAny(final byte[] value) throws ValueSerializationException {
}

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

if (target.equals(Target.BYTE)) {
super.serializePrefixWithLength(ser);
}

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

if (target == Target.BYTE) {
this.encodeType(ser);
}
}

@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
22 changes: 4 additions & 18 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,29 +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
public void serialize(@NotNull SerializerBuffer ser, Target target) throws NoSuchTypeException, ValueSerializationException {
if (this.getValue() == null) return;

if (target.equals(Target.BYTE)) {
super.serializePrefixWithLength(ser);
}

protected void serializeValue(final SerializerBuffer ser) throws ValueSerializationException {
ser.writeBool(this.getValue());

if (target.equals(Target.BYTE)) {
this.encodeType(ser);
}

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
23 changes: 5 additions & 18 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,37 +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
public void serialize(SerializerBuffer ser, Target target) throws NoSuchTypeException, ValueSerializationException {
if (this.getValue() == null) return;

if (target.equals(Target.BYTE)) {
super.serializePrefixWithLength(ser);
}

protected void serializeValue(final SerializerBuffer ser) throws ValueSerializationException {
ser.writeByteArray(this.getValue());

if (target.equals(Target.BYTE)) {
this.encodeType(ser);
}

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
26 changes: 8 additions & 18 deletions src/main/java/com/casper/sdk/model/clvalue/CLValueI32.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.CLTypeI32;
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 @@ -40,29 +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
public void serialize(SerializerBuffer ser, Target target) throws NoSuchTypeException, ValueSerializationException {
if (this.getValue() == null) return;

if (target.equals(Target.BYTE)) {
super.serializePrefixWithLength(ser);
}

ser.writeI32(this.getValue());

if (target.equals(Target.BYTE)) {
this.encodeType(ser);
}

this.setBytes(Hex.toHexString(ser.toByteArray()));
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
28 changes: 9 additions & 19 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,29 +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
public void serialize(SerializerBuffer ser, Target target) throws NoSuchTypeException, ValueSerializationException {
if (this.getValue() == null) return;

if (target.equals(Target.BYTE)) {
super.serializePrefixWithLength(ser);
}

ser.writeI64(this.getValue());

if (target.equals(Target.BYTE)) {
this.encodeType(ser);
}

this.setBytes(Hex.toHexString(ser.toByteArray()));
protected void serializeValue(final SerializerBuffer ser) throws ValueSerializationException {
final SerializerBuffer serVal = new SerializerBuffer();
serVal.writeI64(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.readI64());
}

Expand Down
Loading
Loading