From 6e359c241adc500096600041e7a5a2b8c3c4cae1 Mon Sep 17 00:00:00 2001 From: meywood <105049338+meywood@users.noreply.github.com> Date: Wed, 28 Feb 2024 14:43:09 +0000 Subject: [PATCH] issues/247 - Fixed CLAnyValue to correctly serialize for byte targets --- build.gradle | 2 +- .../casper/sdk/model/clvalue/CLValueAny.java | 19 ++++++++---- .../ExecutableDeployItem.java | 17 +++++++++-- .../ExecutableDeployItemWithEntryPoint.java | 4 +-- .../deploy/executabledeploy/ModuleBytes.java | 7 ++--- .../StoredContractByHash.java | 4 +-- .../StoredContractByName.java | 9 ++---- .../StoredVersionedContractByHash.java | 9 ++---- .../StoredVersionedContractByName.java | 9 ++---- .../deploy/executabledeploy/Transfer.java | 11 ++++--- .../sdk/model/clvalue/CLValueTests.java | 24 ++++++++++++--- .../sdk/model/clvalue/NestedAnyTest.java | 29 +++++++++++++++++++ 12 files changed, 98 insertions(+), 46 deletions(-) diff --git a/build.gradle b/build.gradle index 225ab520d..c8d99c900 100644 --- a/build.gradle +++ b/build.gradle @@ -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 diff --git a/src/main/java/com/casper/sdk/model/clvalue/CLValueAny.java b/src/main/java/com/casper/sdk/model/clvalue/CLValueAny.java index 7b387e7d7..c835842c4 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/CLValueAny.java +++ b/src/main/java/com/casper/sdk/model/clvalue/CLValueAny.java @@ -34,7 +34,7 @@ public class CLValueAny extends AbstractCLValue { @JsonSetter("cl_type") @ExcludeFromJacocoGeneratedReport - protected void setJsonClType(CLTypeAny clType) { + protected void setJsonClType(final CLTypeAny clType) { this.clType = clType; } @@ -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())); } diff --git a/src/main/java/com/casper/sdk/model/deploy/executabledeploy/ExecutableDeployItem.java b/src/main/java/com/casper/sdk/model/deploy/executabledeploy/ExecutableDeployItem.java index 89bba7fd5..29e7065d0 100644 --- a/src/main/java/com/casper/sdk/model/deploy/executabledeploy/ExecutableDeployItem.java +++ b/src/main/java/com/casper/sdk/model/deploy/executabledeploy/ExecutableDeployItem.java @@ -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; @@ -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> getArgs(); + public abstract List> getArgs(); @JsonIgnore - byte getOrder(); + abstract byte getOrder(); + + void serializeNamedArgs(SerializerBuffer ser, Target target) throws ValueSerializationException, NoSuchTypeException { + ser.writeI32(getArgs().size()); + for (NamedArg namedArg : getArgs()) { + namedArg.serialize(ser, target); + } + } } diff --git a/src/main/java/com/casper/sdk/model/deploy/executabledeploy/ExecutableDeployItemWithEntryPoint.java b/src/main/java/com/casper/sdk/model/deploy/executabledeploy/ExecutableDeployItemWithEntryPoint.java index 37843317a..9b85bb10c 100644 --- a/src/main/java/com/casper/sdk/model/deploy/executabledeploy/ExecutableDeployItemWithEntryPoint.java +++ b/src/main/java/com/casper/sdk/model/deploy/executabledeploy/ExecutableDeployItemWithEntryPoint.java @@ -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(); } diff --git a/src/main/java/com/casper/sdk/model/deploy/executabledeploy/ModuleBytes.java b/src/main/java/com/casper/sdk/model/deploy/executabledeploy/ModuleBytes.java index 36bc9e4e2..e155caf1c 100644 --- a/src/main/java/com/casper/sdk/model/deploy/executabledeploy/ModuleBytes.java +++ b/src/main/java/com/casper/sdk/model/deploy/executabledeploy/ModuleBytes.java @@ -27,7 +27,7 @@ @AllArgsConstructor @NoArgsConstructor @JsonTypeName("ModuleBytes") -public class ModuleBytes implements ExecutableDeployItem { +public class ModuleBytes extends ExecutableDeployItem { /** * Module bytes @@ -66,9 +66,6 @@ public void serialize(SerializerBuffer ser, Target target) throws ValueSerializa 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); } } diff --git a/src/main/java/com/casper/sdk/model/deploy/executabledeploy/StoredContractByHash.java b/src/main/java/com/casper/sdk/model/deploy/executabledeploy/StoredContractByHash.java index c31dfdeb9..a950e2ddf 100644 --- a/src/main/java/com/casper/sdk/model/deploy/executabledeploy/StoredContractByHash.java +++ b/src/main/java/com/casper/sdk/model/deploy/executabledeploy/StoredContractByHash.java @@ -26,7 +26,7 @@ @AllArgsConstructor @NoArgsConstructor @JsonTypeName("StoredContractByHash") -public class StoredContractByHash implements ExecutableDeployItemWithEntryPoint { +public class StoredContractByHash extends ExecutableDeployItemWithEntryPoint { /** * Hex-encoded Hash @@ -65,4 +65,4 @@ public void serialize(SerializerBuffer ser, Target target) throws NoSuchTypeExce namedArg.serialize(ser, Target.BYTE); } } -} \ No newline at end of file +} diff --git a/src/main/java/com/casper/sdk/model/deploy/executabledeploy/StoredContractByName.java b/src/main/java/com/casper/sdk/model/deploy/executabledeploy/StoredContractByName.java index d02b299e5..1a46eda60 100644 --- a/src/main/java/com/casper/sdk/model/deploy/executabledeploy/StoredContractByName.java +++ b/src/main/java/com/casper/sdk/model/deploy/executabledeploy/StoredContractByName.java @@ -25,7 +25,7 @@ @AllArgsConstructor @NoArgsConstructor @JsonTypeName("StoredContractByName") -public class StoredContractByName implements ExecutableDeployItemWithEntryPoint { +public class StoredContractByName extends ExecutableDeployItemWithEntryPoint { /** * Contract name @@ -59,9 +59,6 @@ public void serialize(SerializerBuffer ser, Target target) throws NoSuchTypeExce 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); } -} \ No newline at end of file +} diff --git a/src/main/java/com/casper/sdk/model/deploy/executabledeploy/StoredVersionedContractByHash.java b/src/main/java/com/casper/sdk/model/deploy/executabledeploy/StoredVersionedContractByHash.java index ec58d6a75..d5aa1b67b 100644 --- a/src/main/java/com/casper/sdk/model/deploy/executabledeploy/StoredVersionedContractByHash.java +++ b/src/main/java/com/casper/sdk/model/deploy/executabledeploy/StoredVersionedContractByHash.java @@ -25,7 +25,7 @@ @AllArgsConstructor @NoArgsConstructor @JsonTypeName("StoredVersionedContractByHash") -public class StoredVersionedContractByHash implements ExecutableDeployItemWithEntryPoint { +public class StoredVersionedContractByHash extends ExecutableDeployItemWithEntryPoint { /** * Hex-encoded Hash @@ -65,9 +65,6 @@ public void serialize(SerializerBuffer ser, Target target) throws NoSuchTypeExce 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); } -} \ No newline at end of file +} diff --git a/src/main/java/com/casper/sdk/model/deploy/executabledeploy/StoredVersionedContractByName.java b/src/main/java/com/casper/sdk/model/deploy/executabledeploy/StoredVersionedContractByName.java index 446df853e..ba27fe603 100644 --- a/src/main/java/com/casper/sdk/model/deploy/executabledeploy/StoredVersionedContractByName.java +++ b/src/main/java/com/casper/sdk/model/deploy/executabledeploy/StoredVersionedContractByName.java @@ -25,7 +25,7 @@ @AllArgsConstructor @NoArgsConstructor @JsonTypeName("StoredVersionedContractByName") -public class StoredVersionedContractByName implements ExecutableDeployItemWithEntryPoint { +public class StoredVersionedContractByName extends ExecutableDeployItemWithEntryPoint { /** * Contract Name @@ -65,10 +65,7 @@ public void serialize(SerializerBuffer ser, Target target) throws ValueSerializa 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); } -} \ No newline at end of file +} diff --git a/src/main/java/com/casper/sdk/model/deploy/executabledeploy/Transfer.java b/src/main/java/com/casper/sdk/model/deploy/executabledeploy/Transfer.java index 44f14c5c8..837a9946d 100644 --- a/src/main/java/com/casper/sdk/model/deploy/executabledeploy/Transfer.java +++ b/src/main/java/com/casper/sdk/model/deploy/executabledeploy/Transfer.java @@ -24,7 +24,7 @@ @NoArgsConstructor @AllArgsConstructor @JsonTypeName("Transfer") -public class Transfer implements ExecutableDeployItem { +public class Transfer extends ExecutableDeployItem { /** * List of {@link NamedArg} @@ -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); } -} \ No newline at end of file + + +} 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 53be10dd5..3f7d2f469 100644 --- a/src/test/java/com/casper/sdk/model/clvalue/CLValueTests.java +++ b/src/test/java/com/casper/sdk/model/clvalue/CLValueTests.java @@ -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 { @@ -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")); diff --git a/src/test/java/com/casper/sdk/model/clvalue/NestedAnyTest.java b/src/test/java/com/casper/sdk/model/clvalue/NestedAnyTest.java index 07d55dba1..7a49518bb 100644 --- a/src/test/java/com/casper/sdk/model/clvalue/NestedAnyTest.java +++ b/src/test/java/com/casper/sdk/model/clvalue/NestedAnyTest.java @@ -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; @@ -126,4 +134,25 @@ 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 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)); + + + } }