Skip to content

Commit

Permalink
Merge pull request #259 from /issues/258
Browse files Browse the repository at this point in the history
issues/258 - Fix for option with byte array child.
  • Loading branch information
cnorburn authored Mar 20, 2024
2 parents b702266 + 5b643ab commit 8d1ef64
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 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.2'
version='2.5.3'
sourceCompatibility = 1.8
targetCompatibility = 1.8

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ protected void childTypesSet() {
protected void populateChildTypesFromParent(final AbstractCLValue<?, ?> child, final AbstractCLType type) {
if (type instanceof AbstractCLTypeWithChildren) {
if (child.getClType() instanceof AbstractCLTypeWithChildren) {
((AbstractCLTypeWithChildren) child.getClType()).setChildTypes(((AbstractCLTypeWithChildren)type).getChildTypes());
} else if (child instanceof CLValueByteArray) {
((CLValueByteArray) child).setClType((CLTypeByteArray) ((AbstractCLTypeWithChildren)type).getChildTypes().get(0));
((AbstractCLTypeWithChildren) child.getClType()).setChildTypes(((AbstractCLTypeWithChildren) type).getChildTypes());
}
} else if (type instanceof CLTypeByteArray) {
((CLValueByteArray) child).setClType((CLTypeByteArray) type);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ protected void setChildTypeObjects(final List<Object> childTypeObjects)
this.loadCLTypes(childTypeObjects);
}

public List<AbstractCLType> getChildTypes() {
return childTypes == null ? this.childTypes = new ArrayList<>() : childTypes;
}

protected List<Object> getChildTypeObjects() {
if (this.childTypeObjects == null) {
this.childTypeObjects = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public void serializeChildTypes(final SerializerBuffer ser) throws NoSuchTypeExc
}

@Override
public void deserializeChildTypes(final DeserializerBuffer deser) throws ValueDeserializationException, NoSuchTypeException, DynamicInstanceException {
public void deserializeChildTypes(final DeserializerBuffer deser) throws
ValueDeserializationException, NoSuchTypeException, DynamicInstanceException {
setOptionType(deserializeChildType(deser));
}
}
49 changes: 49 additions & 0 deletions src/test/java/com/casper/sdk/model/clvalue/CLValueTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.casper.sdk.model.clvalue.serde.Target;
import com.casper.sdk.model.deploy.NamedArg;
import com.casper.sdk.model.key.PublicKey;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.syntifi.crypto.key.AbstractPublicKey;
import com.syntifi.crypto.key.Ed25519PrivateKey;
import com.syntifi.crypto.key.Secp256k1PrivateKey;
Expand Down Expand Up @@ -384,4 +385,52 @@ void nestedOptionWithMap() throws Exception {
final CLValueOption deserialized = (CLValueOption) clValueOption.deserialize(new DeserializerBuffer(bytes), Target.BYTE);
assertThat(deserialized.getBytes(), is(clValueOption.getBytes()));
}

@Test
void testOptionByteTypeSerialization() throws Exception {

final String hexBytes = "d2029649";
CLValueOption clValueOption = new CLValueOption(Optional.of(new CLValueByteArray(Hex.decode(hexBytes))));
final SerializerBuffer ser = new SerializerBuffer();
assertThat(clValueOption.getBytes(), is("01" + hexBytes));
clValueOption.serialize(ser, Target.BYTE);

final byte[] bytes = ser.toByteArray();

assertThat(bytes, is(Hex.decode("0500000001d20296490D0F04000000")));

final String json = "{\n" +
" \"cl_type\" : {\n" +
" \"Option\" : {\n" +
" \"ByteArray\" : 4\n" +
" }\n" +
" },\n" +
" \"bytes\" : \"01d2029649\",\n" +
" \"parsed\" : \"d2029649\"\n" +
"}";

final ObjectMapper objectMapper = new ObjectMapper();
final CLValueOption fromJson = objectMapper.reader().readValue(json, CLValueOption.class);
assertThat(fromJson.getBytes(), is("01" + hexBytes));
}

@Test
void testEmptyOptionSerialization() throws Exception {

CLValueOption clValueOption = new CLValueOption(Optional.empty());
clValueOption.getClType().setOptionType(new CLTypeString());
final SerializerBuffer ser = new SerializerBuffer();
assertThat(clValueOption.getBytes(), is("00"));
clValueOption.serialize(ser, Target.BYTE);

final byte[] bytes = ser.toByteArray();
assertThat(bytes, is(Hex.decode("01000000000D0A")));

final CLValueOption deserialized = (CLValueOption) clValueOption.deserialize(new DeserializerBuffer(bytes), Target.BYTE);
assertThat(deserialized.getBytes(), is(clValueOption.getBytes()));
assertThat(deserialized.getClType().getOptionType().getClTypeData(), is(CLTypeData.STRING));

clValueOption = new CLValueOption(Optional.empty());
assertThat(clValueOption.getBytes(), is("00"));
}
}

0 comments on commit 8d1ef64

Please sign in to comment.