diff --git a/ethereumj-core/src/main/java/org/ethereum/solidity/SolidityType.java b/ethereumj-core/src/main/java/org/ethereum/solidity/SolidityType.java index ea9956d9f7..86228abc39 100644 --- a/ethereumj-core/src/main/java/org/ethereum/solidity/SolidityType.java +++ b/ethereumj-core/src/main/java/org/ethereum/solidity/SolidityType.java @@ -135,6 +135,23 @@ public byte[] encode(Object value) { throw new RuntimeException("List value expected for type " + getName()); } } + + @Override + public String getCanonicalName() { + return getArrayCanonicalName(""); + } + + String getArrayCanonicalName(String parentDimStr) { + String myDimStr = parentDimStr + getCanonicalDimension(); + if (getElementType() instanceof ArrayType) { + return ((ArrayType) getElementType()). + getArrayCanonicalName(myDimStr); + } else { + return getElementType().getCanonicalName() + myDimStr; + } + } + + protected abstract String getCanonicalDimension(); public SolidityType getElementType() { return elementType; @@ -156,7 +173,18 @@ public StaticArrayType(String name) { @Override public String getCanonicalName() { - return elementType.getCanonicalName() + "[" + size + "]"; + if (elementType instanceof ArrayType) { + String elementTypeName = elementType.getCanonicalName(); + int idx1 = elementTypeName.indexOf("["); + return elementTypeName.substring(0, idx1) + "[" + size + "]" + elementTypeName.substring(idx1); + } else { + return elementType.getCanonicalName() + "[" + size + "]"; + } + } + + @Override + protected String getCanonicalDimension() { + return "[" + size + "]"; } @Override @@ -193,7 +221,18 @@ public DynamicArrayType(String name) { @Override public String getCanonicalName() { - return elementType.getCanonicalName() + "[]"; + if (elementType instanceof ArrayType) { + String elementTypeName = elementType.getCanonicalName(); + int idx1 = elementTypeName.indexOf("["); + return elementTypeName.substring(0, idx1) + "[]" + elementTypeName.substring(idx1); + } else { + return elementType.getCanonicalName() + "[]"; + } + } + + @Override + protected String getCanonicalDimension() { + return "[]"; } @Override diff --git a/ethereumj-core/src/test/java/org/ethereum/core/ABITest.java b/ethereumj-core/src/test/java/org/ethereum/core/ABITest.java index 80ea8c946d..03e59be128 100644 --- a/ethereumj-core/src/test/java/org/ethereum/core/ABITest.java +++ b/ethereumj-core/src/test/java/org/ethereum/core/ABITest.java @@ -335,4 +335,38 @@ public void decodeWithUnknownFunctionTypeTest() { Assert.assertArrayEquals((Object[]) objects[1], strings); Assert.assertEquals(((Number) objects[2]).intValue(), 222); } + + @Test + public void twoDimensionalArrayType_hasDimensionDefinitionInCorrectOrder() { + String funcJson = "{ \n" + + " 'constant':false,\n" + + " 'inputs':[ \n" + + " { \n" + + " 'name':'param1',\n" + + " 'type':'address[5][]'\n" + + " },\n" + + " { \n" + + " 'name':'param2',\n" + + " 'type':'uint256[6][2]'\n" + + " },\n" + + " { \n" + + " 'name':'param2',\n" + + " 'type':'uint256[][]'\n" + + " },\n" + + " { \n" + + " 'name':'param3',\n" + + " 'type':'uint256[][2]'\n" + + " }\n" + + " ],\n" + + " 'name':'testTwoDimArray',\n" + + " 'outputs':[],\n" + + " 'payable':false,\n" + + " 'type':'function'\n" + + "}"; + funcJson = funcJson.replaceAll("'", "\""); + CallTransaction.Function function = CallTransaction.Function.fromJsonInterface(funcJson); + String expected = "testTwoDimArray(address[5][],uint256[6][2],uint256[][],uint256[][2])"; + String actual = function.toString(); + Assert.assertEquals(expected, actual); + } }