-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #266 from /issues/262
issues/262 - Version should byte serialize as Option type.
- Loading branch information
Showing
6 changed files
with
224 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...in/java/com/casper/sdk/model/deploy/executabledeploy/AbstractStoredVersionedContract.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.casper.sdk.model.deploy.executabledeploy; | ||
|
||
import com.casper.sdk.exception.NoSuchTypeException; | ||
import com.casper.sdk.model.clvalue.AbstractCLValue; | ||
import com.casper.sdk.model.clvalue.CLValueOption; | ||
import com.casper.sdk.model.clvalue.CLValueU32; | ||
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.JsonProperty; | ||
import dev.oak3.sbs4j.SerializerBuffer; | ||
import dev.oak3.sbs4j.exception.ValueSerializationException; | ||
import lombok.*; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Base class for versioned contracts. | ||
* | ||
* @author [email protected] | ||
*/ | ||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
abstract class AbstractStoredVersionedContract extends ExecutableDeployItemWithEntryPoint { | ||
|
||
/** contract version */ | ||
@JsonProperty("version") | ||
private Long version; | ||
|
||
/** Entry Point */ | ||
@JsonProperty("entry_point") | ||
private String entryPoint; | ||
|
||
/** @see NamedArg */ | ||
private List<NamedArg<?>> args; | ||
|
||
@JsonIgnore | ||
public Optional<Long> getVersion() { | ||
return Optional.ofNullable(version); | ||
} | ||
|
||
@Override | ||
public void serialize(final SerializerBuffer ser, final Target target) throws NoSuchTypeException, ValueSerializationException { | ||
ser.writeU8(getOrder()); | ||
serializeCustom(ser); | ||
|
||
final Optional<AbstractCLValue<?, ?>> innerValue; | ||
if (version == null) { | ||
innerValue = Optional.empty(); | ||
} else { | ||
innerValue = Optional.of(new CLValueU32(version)); | ||
} | ||
new CLValueOption(innerValue).serialize(ser, Target.JSON); | ||
|
||
ser.writeString(getEntryPoint()); | ||
serializeNamedArgs(ser, target); | ||
} | ||
|
||
protected abstract void serializeCustom(final SerializerBuffer ser); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
.../java/com/casper/sdk/model/deploy/executabledeploy/StoredVersionedContractByHashTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.casper.sdk.model.deploy.executabledeploy; | ||
|
||
import com.casper.sdk.model.clvalue.CLValueI32; | ||
import com.casper.sdk.model.clvalue.serde.Target; | ||
import com.casper.sdk.model.deploy.NamedArg; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.syntifi.crypto.key.encdec.Hex; | ||
import dev.oak3.sbs4j.SerializerBuffer; | ||
import dev.oak3.sbs4j.exception.ValueSerializationException; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Arrays; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.core.Is.is; | ||
import static org.hamcrest.core.IsNull.notNullValue; | ||
|
||
/** | ||
* Unit tests for the StoredVersionedContractByHash class. | ||
* | ||
* @author [email protected] | ||
*/ | ||
class StoredVersionedContractByHashTest { | ||
|
||
@Test | ||
void jsonSerializeStoredVersionedContractByHash() throws ValueSerializationException, JsonProcessingException { | ||
|
||
final StoredVersionedContractByHash versionedContractByHash = StoredVersionedContractByHash.builder() | ||
.hash("92173d49744c790d47e50d011d89e1b5a33ed2d9fae8d9459325224d8f98f3e5") | ||
.version(1L) | ||
.entryPoint("counter_inc") | ||
.args(Arrays.asList(new NamedArg<>("one", new CLValueI32(1)), new NamedArg<>("two", new CLValueI32(2)))) | ||
.build(); | ||
|
||
final String json = new ObjectMapper().writeValueAsString(versionedContractByHash); | ||
|
||
assertThat(json, is(notNullValue())); | ||
|
||
JsonNode root = new ObjectMapper().reader().readTree(json); | ||
assertThat(root.at("/StoredVersionedContractByHash/hash").asText(), is("92173d49744c790d47e50d011d89e1b5a33ed2d9fae8d9459325224d8f98f3e5")); | ||
assertThat(root.at("/StoredVersionedContractByHash/entry_point").asText(), is("counter_inc")); | ||
assertThat(root.at("/StoredVersionedContractByHash/version").asInt(), is(1)); | ||
assertThat(root.at("/StoredVersionedContractByHash/args/0/0").asText(), is("one")); | ||
assertThat(root.at("/StoredVersionedContractByHash/args/0/1/bytes").asText(), is("01000000")); | ||
assertThat(root.at("/StoredVersionedContractByHash/args/0/1/cl_type").asText(), is("I32")); | ||
} | ||
|
||
@Test | ||
void serializeStoredVersionedContractByHashTest() throws Exception { | ||
|
||
final StoredVersionedContractByHash versionedContractByHash = StoredVersionedContractByHash.builder() | ||
.hash("92173d49744c790d47e50d011d89e1b5a33ed2d9fae8d9459325224d8f98f3e5") | ||
.version(1L) | ||
.entryPoint("transfer") | ||
.args(Arrays.asList(new NamedArg<>("one", new CLValueI32(1)), new NamedArg<>("two", new CLValueI32(2)))) | ||
.build(); | ||
|
||
final SerializerBuffer serializerBuffer = new SerializerBuffer(); | ||
versionedContractByHash.serialize(serializerBuffer, Target.BYTE); | ||
final String expectedBytes = "0340000000393231373364343937343463373930643437653530643031316438396531623561333365643264396661653864393435393332353232346438663938663365350101000000080000007472616e7366657202000000030000006f6e650400000001000000010300000074776f040000000200000001"; | ||
final String actual = Hex.encode(serializerBuffer.toByteArray()); | ||
assertThat(actual, is(expectedBytes)); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
.../java/com/casper/sdk/model/deploy/executabledeploy/StoredVersionedContractByNameTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.casper.sdk.model.deploy.executabledeploy; | ||
|
||
import com.casper.sdk.model.clvalue.CLValueI32; | ||
import com.casper.sdk.model.clvalue.serde.Target; | ||
import com.casper.sdk.model.deploy.NamedArg; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.syntifi.crypto.key.encdec.Hex; | ||
import dev.oak3.sbs4j.SerializerBuffer; | ||
import dev.oak3.sbs4j.exception.ValueSerializationException; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Arrays; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.core.Is.is; | ||
import static org.hamcrest.core.IsNull.notNullValue; | ||
|
||
/** | ||
* Unit tests for the StoredVersionedContractByName class | ||
* | ||
* @author [email protected] | ||
*/ | ||
class StoredVersionedContractByNameTest { | ||
|
||
@Test | ||
void jsonSerializeStoredVersionedContractByName() throws ValueSerializationException, JsonProcessingException { | ||
|
||
final StoredVersionedContractByName versionedContractByName = new StoredVersionedContractByName(); | ||
|
||
versionedContractByName.setName("counter"); | ||
versionedContractByName.setEntryPoint("counter_inc"); | ||
versionedContractByName.setVersion(1L); | ||
versionedContractByName.setArgs(Arrays.asList( | ||
new NamedArg<>("one", new CLValueI32(1)), | ||
new NamedArg<>("two", new CLValueI32(2)) | ||
)); | ||
|
||
final String json = new ObjectMapper().writeValueAsString(versionedContractByName); | ||
|
||
assertThat(json, is(notNullValue())); | ||
|
||
JsonNode root = new ObjectMapper().reader().readTree(json); | ||
assertThat(root.at("/StoredVersionedContractByName/name").asText(), is("counter")); | ||
assertThat(root.at("/StoredVersionedContractByName/entry_point").asText(), is("counter_inc")); | ||
assertThat(root.at("/StoredVersionedContractByName/version").asInt(), is(1)); | ||
assertThat(root.at("/StoredVersionedContractByName/args/0/0").asText(), is("one")); | ||
assertThat(root.at("/StoredVersionedContractByName/args/0/1/bytes").asText(), is("01000000")); | ||
assertThat(root.at("/StoredVersionedContractByName/args/0/1/cl_type").asText(), is("I32")); | ||
} | ||
|
||
@Test | ||
void serializeStoredVersionedContractByHash() throws Exception { | ||
|
||
final StoredVersionedContractByName versionedContractByName = new StoredVersionedContractByName(); | ||
|
||
versionedContractByName.setName("counter"); | ||
versionedContractByName.setEntryPoint("transfer"); | ||
versionedContractByName.setArgs(Arrays.asList( | ||
new NamedArg<>("one", new CLValueI32(1)), | ||
new NamedArg<>("two", new CLValueI32(2)) | ||
)); | ||
|
||
final SerializerBuffer serializerBuffer = new SerializerBuffer(); | ||
versionedContractByName.serialize(serializerBuffer, Target.BYTE); | ||
final String expectedBytes = "0407000000636f756e74657200080000007472616e7366657202000000030000006f6e650400000001000000010300000074776f040000000200000001"; | ||
final String actual = Hex.encode(serializerBuffer.toByteArray()); | ||
assertThat(actual, is(expectedBytes)); | ||
} | ||
} |