Skip to content
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/258 - Fix for option with byte array child. #259

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"));
}
}
Loading