Skip to content

Commit

Permalink
fix incorrect encoding (and decoding) for arrays of uint8
Browse files Browse the repository at this point in the history
now encoded similarly to bool, uint256, etc and not like bytes;
update tests;
  • Loading branch information
esaulpaugh committed May 16, 2019
1 parent d8da2cb commit f63680e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/esaulpaugh/headlong/abi/TypeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private static ABIType<?> resolveBaseType(final String baseTypeStr, boolean isEl
case "int248":
case "int256": type = new BigIntegerType(baseTypeStr, info.bitLen, false); break;
case "int": type = new BigIntegerType("int256", info.bitLen, false); break;
case "uint8": type = isElement ? ByteType.SIGNED : new IntType(baseTypeStr, info.bitLen, true); break;
case "uint8":
case "uint16":
case "uint24": type = new IntType(baseTypeStr, info.bitLen, true); break;
case "uint32": type = isElement ? new IntType(baseTypeStr, info.bitLen, true) : new LongType(baseTypeStr, info.bitLen, true); break;
Expand Down
14 changes: 11 additions & 3 deletions src/test/java/com/esaulpaugh/headlong/abi/ABIJsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,15 @@ public void testIntegerAndAddress() throws ParseException {
testCase.test(argsArray);
}

private static final TestUtils.Parser<Object> BYTES_PARSER = (b) -> TestUtils.parseBytes(b.getAsString());
private static final TestUtils.Parser<Object> INT_ARRAY_PARSER = (e) -> {
JsonArray arr = e.getAsJsonArray();
final int len = arr.size();
int[] ints = new int[len];
for (int i = 0; i < len; i++) {
ints[i] = arr.get(i).getAsInt();
}
return ints;
};

@Test
public void testExperimental1() throws ParseException {
Expand All @@ -165,7 +173,7 @@ public void testExperimental1() throws ParseException {

JsonArray arr = testCase.args.get(1).getAsJsonArray();

TestUtils.Parser<Tuple> tupleParser = (json) -> TestUtils.parseTuple(json, BYTES_PARSER);
TestUtils.Parser<Tuple> tupleParser = (json) -> TestUtils.parseTuple(json, INT_ARRAY_PARSER);

argsArray[1] = TestUtils.getArrayParser(Tuple.class, 3, tupleParser).apply(arr);

Expand All @@ -182,7 +190,7 @@ public void testExperimental1() throws ParseException {
argsArray[2] = TestUtils.parseTuple(
args2,
TestUtils.getTupleParser(2, (b) -> TestUtils.parseInteger(b.getAsJsonPrimitive())),
BYTES_PARSER
(b) -> TestUtils.parseBytes(b.getAsString())
);

testCase.test(argsArray);
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/com/esaulpaugh/headlong/abi/EncodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ public void simpleFunctionTest() throws ParseException {
System.out.println(decoded.equals(args));
}

@Test
public void uint8ArrayTest() throws ParseException {
Function f = new Function("baz(uint8[])");

Tuple args = Tuple.singleton(new int[] { 0xFF, -1, 1, 2, 0 });
ByteBuffer two = f.encodeCall(args);

Tuple decoded = f.decodeCall((ByteBuffer) two.flip());

System.out.println(decoded.equals(args));
}

@Test
public void tupleArrayTest() throws ParseException {
Function f = new Function("((int16)[2][][1])");
Expand Down
6 changes: 3 additions & 3 deletions src/test/resources/tests/ABITests/basic_abi_tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@
[
{
"elements": [
"0xff000102"
[1, 2, 3]
]
},
{
"elements": [
"0x807978"
[-1, -2, -3, -128, 127]
]
}
]
Expand All @@ -71,7 +71,7 @@
]
}
],
"result": "0000000000000000000000000000000000000000000000000000000005d94e830000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000a3078666630303031303200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000083078383037393738000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000063078666631310000000000000000000000000000000000000000000000000000",
"result": "0000000000000000000000000000000000000000000000000000000005d94e83000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000002c000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000005fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffdffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000000000000000000000000000000000000000000000000000000000007fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000063078666631310000000000000000000000000000000000000000000000000000",
"types": [
"uint256",
"(uint8[])[2][][1]",
Expand Down

0 comments on commit f63680e

Please sign in to comment.