Skip to content

Commit

Permalink
#40: add deserialize and test.
Browse files Browse the repository at this point in the history
  • Loading branch information
gsmachado committed Jun 12, 2019
1 parent 681cc79 commit 775e4f1
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 9 deletions.
Expand Up @@ -29,6 +29,9 @@ public class ContractFunctionProperties extends NeoSerializable {

private boolean isPayable;

public ContractFunctionProperties() {
}

public ContractFunctionProperties(List<ContractParameterType> parameterTypes, ContractParameterType returnType, boolean needsStorage, boolean needsDynamicInvoke, boolean isPayable) {
this.parameterTypes = toSerializable(parameterTypes);
this.returnType = toSerializable(returnType);
Expand Down Expand Up @@ -89,7 +92,7 @@ private ContractParameterType fromSerializable(ContractParameterTypeSerializable
return parameterType.getContractParameterType();
}

private int computeFlagsValue() {
protected int packFlagsValue() {
int flagsValue = 0;
if (this.needsStorage) {
flagsValue += (1 << 0);
Expand All @@ -103,6 +106,18 @@ private int computeFlagsValue() {
return flagsValue;
}

protected static boolean unpackNeedsStorage(int value) {
return (value & 1) == 1;
}

protected static boolean unpackNeedsDynamicInvoke(int value) {
return (value & 2) == 2;
}

protected static boolean unpackIsPayable(int value) {
return (value & 4) == 4;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down Expand Up @@ -133,19 +148,30 @@ public String toString() {

@Override
public void deserialize(BinaryReader reader) throws IOException {
// try {
//
// } catch (IllegalAccessException e) {
// LOG.error("Can't access the specified object.", e);
// } catch (InstantiationException e) {
// LOG.error("Can't instantiate the specified object type.", e);
// }
try {
int functionProperties = reader.readPushInteger();
this.needsStorage = unpackNeedsStorage(functionProperties);
this.needsDynamicInvoke = unpackNeedsDynamicInvoke(functionProperties);
this.isPayable = unpackIsPayable(functionProperties);
int returnTypeInt = reader.readPushInteger();
this.returnType = toSerializable(ContractParameterType.valueOf((byte) returnTypeInt));

// TODO: 2019-06-04 Guil:
// Maybe, we should use readPushData() instead of readVarInt?!
// Tests are required for parameters with the size larger than 22
this.parameterTypes = reader.readSerializableList(ContractParameterTypeSerializable.class);

} catch (IllegalAccessException e) {
LOG.error("Can't access the specified object.", e);
} catch (InstantiationException e) {
LOG.error("Can't instantiate the specified object type.", e);
}
}

@Override
public void serialize(BinaryWriter writer) throws IOException {
// flags:
int flagsValue = computeFlagsValue();
int flagsValue = packFlagsValue();
writer.pushInteger(flagsValue);

// return type:
Expand Down
@@ -1,11 +1,15 @@
package io.neow3j.contract;

import io.neow3j.io.NeoSerializableInterface;
import io.neow3j.model.types.ContractParameterType;
import io.neow3j.utils.Numeric;
import org.junit.Test;

import java.util.Arrays;

import static io.neow3j.contract.ContractFunctionProperties.unpackIsPayable;
import static io.neow3j.contract.ContractFunctionProperties.unpackNeedsDynamicInvoke;
import static io.neow3j.contract.ContractFunctionProperties.unpackNeedsStorage;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

Expand Down Expand Up @@ -124,4 +128,80 @@ public void test_All_False() {
assertThat(expected, is(result));
}

@Test
public void test_Deserialize() throws IllegalAccessException, InstantiationException {
ContractFunctionProperties expected = new ContractFunctionProperties(
Arrays.asList(ContractParameterType.STRING, ContractParameterType.ARRAY,
ContractParameterType.BOOLEAN, ContractParameterType.INTEGER, ContractParameterType.INTEGER),
ContractParameterType.BYTE_ARRAY,
true,
true,
true
);

byte[] raw = Numeric.hexStringToByteArray("5755050710010202");

ContractFunctionProperties result = NeoSerializableInterface.from(raw, ContractFunctionProperties.class);
assertThat(result, is(expected));
}

@Test
public void testPackAndUnpackFlagsValue() {
ContractFunctionProperties fp1 = buildFunctionProperties(false, false, false);
ContractFunctionProperties fp2 = buildFunctionProperties(true, false, false);
ContractFunctionProperties fp3 = buildFunctionProperties(true, true, false);
ContractFunctionProperties fp4 = buildFunctionProperties(true, true, true);
ContractFunctionProperties fp5 = buildFunctionProperties(true, false, true);
ContractFunctionProperties fp6 = buildFunctionProperties(false, false, true);
ContractFunctionProperties fp7 = buildFunctionProperties(false, true, true);
ContractFunctionProperties fp8 = buildFunctionProperties(false, true, false);
assertThat(Integer.toBinaryString(fp1.packFlagsValue()), is("0"));
assertThat(Integer.toBinaryString(fp2.packFlagsValue()), is("1"));
assertThat(Integer.toBinaryString(fp3.packFlagsValue()), is("11"));
assertThat(Integer.toBinaryString(fp4.packFlagsValue()), is("111"));
assertThat(Integer.toBinaryString(fp5.packFlagsValue()), is("101"));
assertThat(Integer.toBinaryString(fp6.packFlagsValue()), is("100"));
assertThat(Integer.toBinaryString(fp7.packFlagsValue()), is("110"));
assertThat(Integer.toBinaryString(fp8.packFlagsValue()), is("10"));

assertThat(unpackNeedsStorage(0), is(false));
assertThat(unpackNeedsStorage(1), is(true));
assertThat(unpackNeedsStorage(3), is(true));
assertThat(unpackNeedsStorage(7), is(true));
assertThat(unpackNeedsStorage(5), is(true));
assertThat(unpackNeedsStorage(4), is(false));
assertThat(unpackNeedsStorage(6), is(false));
assertThat(unpackNeedsStorage(2), is(false));

assertThat(unpackNeedsDynamicInvoke(0), is(false));
assertThat(unpackNeedsDynamicInvoke(1), is(false));
assertThat(unpackNeedsDynamicInvoke(3), is(true));
assertThat(unpackNeedsDynamicInvoke(7), is(true));
assertThat(unpackNeedsDynamicInvoke(5), is(false));
assertThat(unpackNeedsDynamicInvoke(4), is(false));
assertThat(unpackNeedsDynamicInvoke(6), is(true));
assertThat(unpackNeedsDynamicInvoke(2), is(true));

assertThat(unpackIsPayable(0), is(false));
assertThat(unpackIsPayable(1), is(false));
assertThat(unpackIsPayable(3), is(false));
assertThat(unpackIsPayable(7), is(true));
assertThat(unpackIsPayable(5), is(true));
assertThat(unpackIsPayable(4), is(true));
assertThat(unpackIsPayable(6), is(true));
assertThat(unpackIsPayable(2), is(false));
}

private ContractFunctionProperties buildFunctionProperties(boolean needsStorage,
boolean needsDynamicInvoke,
boolean isPayable) {
return new ContractFunctionProperties(
null,
null,
needsStorage,
needsDynamicInvoke,
isPayable
);
}

}
Expand Up @@ -10,6 +10,9 @@ public class ContractParameterTypeSerializable extends NeoSerializable {

private ContractParameterType contractParameterType;

public ContractParameterTypeSerializable() {
}

public ContractParameterTypeSerializable(ContractParameterType contractParameterType) {
this.contractParameterType = contractParameterType;
}
Expand Down

0 comments on commit 775e4f1

Please sign in to comment.