Skip to content

Commit

Permalink
Bug-Fix: parsing type strings for tuples containing static arrays of …
Browse files Browse the repository at this point in the history
…tuples (#431)
  • Loading branch information
ahangsu authored Nov 29, 2022
1 parent aa01220 commit d4ebc32
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/main/java/com/algorand/algosdk/abi/ABIType.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public static List<String> parseTupleContent(String str) {
return new ArrayList<>();

if (str.startsWith(",") || str.endsWith(","))
throw new IllegalArgumentException("parsing error: tuple content should not start with comma");
throw new IllegalArgumentException("parsing error: tuple content should not start or end with comma");

if (str.contains(",,"))
throw new IllegalArgumentException("parsing error: tuple content should not have consecutive commas");
Expand All @@ -118,8 +118,15 @@ else if (str.charAt(i) == ')') {
if (parenStack.isEmpty())
throw new IllegalArgumentException("parsing error: tuple parentheses are not balanced: " + str);
int leftParenIndex = parenStack.pop();
if (parenStack.isEmpty())
if (parenStack.isEmpty()) {
// iterate through the byte str, include all the bytes after closing round bracket, for array indicator
// increase the index until it meets comma, or end of string
int forwardIndex = i + 1;
while (forwardIndex < str.length() && str.charAt(forwardIndex) != ',')
forwardIndex++;
i = forwardIndex - 1;
parenSegments.add(new Segment(leftParenIndex, i));
}
}
}
if (!parenStack.isEmpty())
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/com/algorand/algosdk/abi/TestTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,54 @@ public void TestTypeFromStringValid() {
)
)
);
assertThat(ABIType.valueOf("(uint32,(uint64,bool)[10],byte)")).isEqualTo(
new TypeTuple(
Arrays.asList(
new TypeUint(32),
new TypeArrayStatic(
new TypeTuple(
Arrays.asList(
new TypeUint(64),
new TypeBool()
)
),
10),
new TypeByte()
)
)
);
assertThat(ABIType.valueOf("(uint32,byte,(uint64,bool)[10])")).isEqualTo(
new TypeTuple(
Arrays.asList(
new TypeUint(32),
new TypeByte(),
new TypeArrayStatic(
new TypeTuple(
Arrays.asList(
new TypeUint(64),
new TypeBool()
)
),
10)
)
)
);
assertThat(ABIType.valueOf("((uint64,bool)[10],uint32,byte)")).isEqualTo(
new TypeTuple(
Arrays.asList(
new TypeArrayStatic(
new TypeTuple(
Arrays.asList(
new TypeUint(64),
new TypeBool()
)
),
10),
new TypeUint(32),
new TypeByte()
)
)
);
}

@Test
Expand Down

0 comments on commit d4ebc32

Please sign in to comment.