Skip to content

Commit

Permalink
Merge pull request #248 from /issues/247
Browse files Browse the repository at this point in the history
issues/247 - Fixed CLAnyValue to correctly serialize for byte targets
  • Loading branch information
cnorburn authored Feb 28, 2024
2 parents c6f2ad5 + e9ac855 commit 8ebfe2e
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 51 deletions.
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.0'
version='2.5.1'
sourceCompatibility = 1.8
targetCompatibility = 1.8

Expand Down
19 changes: 14 additions & 5 deletions src/main/java/com/casper/sdk/model/clvalue/CLValueAny.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class CLValueAny extends AbstractCLValue<byte[], CLTypeAny> {

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

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

public CLValueAny(byte[] value) throws ValueSerializationException {
public CLValueAny(final byte[] value) throws ValueSerializationException {
this.setValue(value);
}

@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)) {
super.serializePrefixWithLength(ser);
}

ser.writeByteArray(this.getValue());
this.setBytes(Hex.toHexString(ser.toByteArray()));
this.setBytes(Hex.toHexString(this.getValue()));

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

@Override
public void deserializeCustom(DeserializerBuffer deser)
public void deserializeCustom(final DeserializerBuffer deser)
throws Exception {
this.setValue(deser.readByteArray(deser.getBuffer().remaining()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.casper.sdk.model.deploy.executabledeploy;

import com.casper.sdk.exception.NoSuchTypeException;
import com.casper.sdk.model.clvalue.serde.CasperSerializableObject;
import com.casper.sdk.model.clvalue.serde.Target;
import com.casper.sdk.model.deploy.NamedArg;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import dev.oak3.sbs4j.SerializerBuffer;
import dev.oak3.sbs4j.exception.ValueSerializationException;

import java.util.List;

Expand All @@ -29,10 +33,17 @@
@JsonSubTypes.Type(value = StoredVersionedContractByHash.class, name = "StoredVersionedContractByHash"),
@JsonSubTypes.Type(value = StoredVersionedContractByName.class, name = "StoredVersionedContractByName"),
@JsonSubTypes.Type(value = Transfer.class, name = "Transfer")})
public interface ExecutableDeployItem extends CasperSerializableObject {
public abstract class ExecutableDeployItem implements CasperSerializableObject {

List<NamedArg<?>> getArgs();
public abstract List<NamedArg<?>> getArgs();

@JsonIgnore
byte getOrder();
abstract byte getOrder();

void serializeNamedArgs(final SerializerBuffer ser, final Target target) throws ValueSerializationException, NoSuchTypeException {
ser.writeI32(getArgs().size());
for (NamedArg<?> namedArg : getArgs()) {
namedArg.serialize(ser, target);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
* @see Transfer
* @since 2.0.0
*/
public interface ExecutableDeployItemWithEntryPoint extends ExecutableDeployItem {
String getEntryPoint();
public abstract class ExecutableDeployItemWithEntryPoint extends ExecutableDeployItem {
abstract String getEntryPoint();
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
@AllArgsConstructor
@NoArgsConstructor
@JsonTypeName("ModuleBytes")
public class ModuleBytes implements ExecutableDeployItem {
public class ModuleBytes extends ExecutableDeployItem {

/**
* Module bytes
Expand Down Expand Up @@ -62,13 +62,10 @@ public byte getOrder() {
* Implements the ModuleBytes encoder
*/
@Override
public void serialize(SerializerBuffer ser, Target target) throws ValueSerializationException, NoSuchTypeException {
public void serialize(final SerializerBuffer ser, final Target target) throws ValueSerializationException, NoSuchTypeException {
ser.writeU8(getOrder());
ser.writeI32(getBytes().length);
ser.writeByteArray(getBytes());
ser.writeI32(args.size());
for (NamedArg<?> namedArg : args) {
namedArg.serialize(ser, target);
}
serializeNamedArgs(ser, target);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
@AllArgsConstructor
@NoArgsConstructor
@JsonTypeName("StoredContractByHash")
public class StoredContractByHash implements ExecutableDeployItemWithEntryPoint {
public class StoredContractByHash extends ExecutableDeployItemWithEntryPoint {

/**
* Hex-encoded Hash
Expand Down Expand Up @@ -56,7 +56,7 @@ public byte getOrder() {
* Implements the StoredContractByHAsh encoder
*/
@Override
public void serialize(SerializerBuffer ser, Target target) throws NoSuchTypeException, ValueSerializationException {
public void serialize(final SerializerBuffer ser, final Target target) throws NoSuchTypeException, ValueSerializationException {
ser.writeU8(getOrder());
ser.writeByteArray(Hex.decode(getHash()));
ser.writeString(getEntryPoint());
Expand All @@ -65,4 +65,4 @@ public void serialize(SerializerBuffer ser, Target target) throws NoSuchTypeExce
namedArg.serialize(ser, Target.BYTE);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
@AllArgsConstructor
@NoArgsConstructor
@JsonTypeName("StoredContractByName")
public class StoredContractByName implements ExecutableDeployItemWithEntryPoint {
public class StoredContractByName extends ExecutableDeployItemWithEntryPoint {

/**
* Contract name
Expand Down Expand Up @@ -55,13 +55,10 @@ public byte getOrder() {
* Implements the StoredContractByHash encoder
*/
@Override
public void serialize(SerializerBuffer ser, Target target) throws NoSuchTypeException, ValueSerializationException {
public void serialize(final SerializerBuffer ser, final Target target) throws NoSuchTypeException, ValueSerializationException {
ser.writeU8(getOrder());
ser.writeString(getName());
ser.writeString(getEntryPoint());
ser.writeI32(args.size());
for (NamedArg<?> namedArg : args) {
namedArg.serialize(ser, Target.BYTE);
}
serializeNamedArgs(ser, target);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
@AllArgsConstructor
@NoArgsConstructor
@JsonTypeName("StoredVersionedContractByHash")
public class StoredVersionedContractByHash implements ExecutableDeployItemWithEntryPoint {
public class StoredVersionedContractByHash extends ExecutableDeployItemWithEntryPoint {

/**
* Hex-encoded Hash
Expand Down Expand Up @@ -60,14 +60,11 @@ public byte getOrder() {
* Implements the StoredVersionedContractByHash encoder
*/
@Override
public void serialize(SerializerBuffer ser, Target target) throws NoSuchTypeException, ValueSerializationException {
public void serialize(final SerializerBuffer ser, final Target target) throws NoSuchTypeException, ValueSerializationException {
ser.writeU8(getOrder());
ser.writeString(getHash());
ser.writeI64(getVersion());
ser.writeString(getEntryPoint());
ser.writeI32(args.size());
for (NamedArg<?> namedArg : args) {
namedArg.serialize(ser, Target.BYTE);
}
serializeNamedArgs(ser, target);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
@AllArgsConstructor
@NoArgsConstructor
@JsonTypeName("StoredVersionedContractByName")
public class StoredVersionedContractByName implements ExecutableDeployItemWithEntryPoint {
public class StoredVersionedContractByName extends ExecutableDeployItemWithEntryPoint {

/**
* Contract Name
Expand Down Expand Up @@ -60,15 +60,12 @@ public byte getOrder() {
* Implements the StoredVersionedContractName encoder
*/
@Override
public void serialize(SerializerBuffer ser, Target target) throws ValueSerializationException, NoSuchTypeException {
public void serialize(final SerializerBuffer ser, final Target target) throws ValueSerializationException, NoSuchTypeException {
ser.writeU8(getOrder());
ser.writeString(getName());
ser.writeI64(getVersion());
ser.writeString(getEntryPoint());
ser.writeI32(args.size());
for (NamedArg<?> namedArg : args) {
namedArg.serialize(ser, Target.BYTE);
}
serializeNamedArgs(ser, target);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
@NoArgsConstructor
@AllArgsConstructor
@JsonTypeName("Transfer")
public class Transfer implements ExecutableDeployItem {
public class Transfer extends ExecutableDeployItem {

/**
* List of {@link NamedArg}
Expand All @@ -45,9 +45,8 @@ public byte getOrder() {
@Override
public void serialize(SerializerBuffer ser, Target target) throws ValueSerializationException, NoSuchTypeException {
ser.writeU8(getOrder());
ser.writeI32(args.size());
for (NamedArg<?> namedArg : args) {
namedArg.serialize(ser, Target.BYTE);
}
serializeNamedArgs(ser, target);
}
}


}
24 changes: 20 additions & 4 deletions src/test/java/com/casper/sdk/model/clvalue/CLValueTests.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.casper.sdk.model.clvalue;

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.exception.DynamicInstanceException;
import com.casper.sdk.model.clvalue.serde.Target;
import com.syntifi.crypto.key.encdec.Hex;
import dev.oak3.sbs4j.SerializerBuffer;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.jupiter.api.Assertions.*;

public class CLValueTests {
private static final Logger LOGGER = LoggerFactory.getLogger(CLValueTests.class);

@Test
void allCLTypes_must_be_implemented() throws DynamicInstanceException, NoSuchTypeException {
Expand Down Expand Up @@ -61,6 +63,20 @@ void getCLTypeClassByName_from_CLTypeData_should_throw_NoSuchTypeException() {
assertThrows(NoSuchTypeException.class, () -> CLTypeData.getCLTypeClassByName("NE"));
}

@Test
void clValueAnyByteSerializationWithAndWithoutTypeInfo() throws Exception {

final CLValueAny clValueAny = new CLValueAny(Hex.decode("d2029649"));

SerializerBuffer ser = new SerializerBuffer();
clValueAny.serialize(ser, Target.JSON);
assertThat(ser.toByteArray(), is(Hex.decode("d2029649")));

ser = new SerializerBuffer();
clValueAny.serialize(ser, Target.BYTE);
assertThat(ser.toByteArray(), is(Hex.decode("04000000d202964915")));
}

@Test
void getTypeByName_from_CLTypeData_should_throw_NoSuchTypeException() {
assertThrows(NoSuchTypeException.class, () -> CLTypeData.getTypeByName("NE"));
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/com/casper/sdk/model/clvalue/NestedAnyTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package com.casper.sdk.model.clvalue;

import com.casper.sdk.model.clvalue.cltype.CLTypeAny;
import com.casper.sdk.model.clvalue.serde.Target;
import com.casper.sdk.model.deploy.NamedArg;
import com.casper.sdk.model.deploy.executabledeploy.ModuleBytes;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.syntifi.crypto.key.encdec.Hex;
import dev.oak3.sbs4j.SerializerBuffer;
import org.junit.jupiter.api.Test;

import java.util.Collections;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.notNullValue;
Expand Down Expand Up @@ -126,4 +134,23 @@ void nestedListWithoutAny() throws Exception {
assertThat(clValueList.getClType().isDeserializable(), is(true));
assertThat(clValueList.getBytes(), is("00000000"));
}


@Test
void anyNamedArgument() throws Exception {

final CLValueAny clValueAny = new CLValueAny(Hex.decode("d2029649"));
final NamedArg<CLTypeAny> namedArg = new NamedArg<>("any", clValueAny);

final ModuleBytes moduleBytes = new ModuleBytes();
moduleBytes.setJsonBytes("01020304");
moduleBytes.setArgs(Collections.singletonList(namedArg));

final SerializerBuffer ser = new SerializerBuffer();
moduleBytes.serialize(ser, Target.BYTE);

byte[] actual = ser.toByteArray();
byte[] expected = Hex.decode("0004000000010203040100000003000000616e7904000000d202964915");
assertThat(actual, is(expected));
}
}

0 comments on commit 8ebfe2e

Please sign in to comment.