-
Notifications
You must be signed in to change notification settings - Fork 25
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/262 - Version should byte serialize as Option type. #266
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
72 changes: 72 additions & 0 deletions
72
.../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,72 @@ | ||
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 = new StoredVersionedContractByHash(); | ||
|
||
versionedContractByHash.setHash("92173d49744c790d47e50d011d89e1b5a33ed2d9fae8d9459325224d8f98f3e5"); | ||
versionedContractByHash.setEntryPoint("counter_inc"); | ||
versionedContractByHash.setVersion(1L); | ||
versionedContractByHash.setArgs(Arrays.asList( | ||
new NamedArg<>("one", new CLValueI32(1)), | ||
new NamedArg<>("two", new CLValueI32(2)) | ||
)); | ||
|
||
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 = new StoredVersionedContractByHash(); | ||
|
||
versionedContractByHash.setHash("92173d49744c790d47e50d011d89e1b5a33ed2d9fae8d9459325224d8f98f3e5"); | ||
versionedContractByHash.setVersion(1L); | ||
versionedContractByHash.setEntryPoint("transfer"); | ||
versionedContractByHash.setArgs(Arrays.asList( | ||
new NamedArg<>("one", new CLValueI32(1)), | ||
new NamedArg<>("two", new CLValueI32(2)) | ||
)); | ||
|
||
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)); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd use ternary here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It exceeds max line length as ternary so if else is preferable