web3j icon indicating copy to clipboard operation
web3j copied to clipboard

Contract Functions returning a static array throwing class cast exception when using the wrapper generated code

Open zaf187 opened this issue 9 years ago • 2 comments

I have a contract function;

function getFields() constant returns (bytes32[11], bytes32[11]) {
    var fields =[   bytes32('ASDFASDF'),
                    bytes32('ZXCVZXCV'),
                    bytes32('DFGHDFGH'),
                    bytes32('CVBNCVBN'),
                    bytes32('RTJFTGHJ'),
                    bytes32('SDFGSERT'),
                    bytes32('XCBVNCBVN'),
                    bytes32('DFGHDRTH'),
                    bytes32('ERTYERTY'),
                    bytes32('CVBNCXVB'),
                    bytes32('KTYIKTYUJ')];
                    
    var data = [    someStruct.id,
                    someStruct.name,
                    someStruct.number,
                    someStruct.cost,
                    someStruct.wombat,
                    someStruct.zombie,
                    someStruct.cow,
                    someStruct.chicken,
                    someStruct.anotherStruct.goose,
                    someStruct.anotherStruct.monkey,
                    someStruct.anotherStruct.pig];
    
    return (fields, data);
}

the java generated function for this is;

public Future<List<Type>> getFields() {
    Function function = new Function("getFields", 
            Arrays.<Type>asList(), 
            Arrays.<TypeReference<?>>asList(new TypeReference<StaticArray<Bytes32>>() {}, new TypeReference<StaticArray<Bytes32>>() {}));
    return executeCallMultipleValueReturnAsync(function);
}

There is a problem in the FunctionReturnDecoder;

private static List<Type> build(
        String input, List<TypeReference<Type>> outputParameters) {
    List<Type> results = new ArrayList<>(outputParameters.size());

    int offset = 0;
    for (TypeReference<?> typeReference:outputParameters) {
        try {
            Class<Type> type = (Class<Type>) typeReference.getClassType();

            int hexStringDataOffset = getDataOffset(input, offset, type);

            Type result;
            if (DynamicArray.class.isAssignableFrom(type)) {
                result = TypeDecoder.decodeDynamicArray(
                        input, hexStringDataOffset, typeReference);
                offset += MAX_BYTE_LENGTH_FOR_HEX_STRING;
            } else if (StaticArray.class.isAssignableFrom(type)) {
                int length = ((TypeReference.StaticArrayTypeReference) typeReference).getSize();
                result = TypeDecoder.decodeStaticArray(
                        input, hexStringDataOffset, typeReference, length);
                offset += length * MAX_BYTE_LENGTH_FOR_HEX_STRING;
            } else {
                result = TypeDecoder.decode(input, hexStringDataOffset, type);
                offset += MAX_BYTE_LENGTH_FOR_HEX_STRING;
            }
            results.add(result);

        } catch (ClassNotFoundException e) {
            throw new UnsupportedOperationException("Invalid class reference provided", e);
        }
    }
    return results;
}

int length = ((TypeReference.StaticArrayTypeReference) typeReference).getSize();

this line of code is trying to cast the typeReference as a StaticArray, the typeReference is actually the generated class, what i think it should be referencing is the type object ... to get the count of the number of items that need to be in the array.

zaf187 avatar Sep 05 '17 11:09 zaf187

Caused by: java.lang.ClassCastException: generatedContractCode$22 cannot be cast to org.web3j.abi.TypeReference$StaticArrayTypeReference at org.web3j.abi.FunctionReturnDecoder.build(FunctionReturnDecoder.java:107) at org.web3j.abi.FunctionReturnDecoder.decode(FunctionReturnDecoder.java:42) at org.web3j.tx.Contract.executeCall(Contract.java:157) at org.web3j.tx.Contract.executeCallMultipleValueReturn(Contract.java:182) at org.web3j.tx.Contract.lambda$executeCallMultipleValueReturnAsync$22(Contract.java:167) at org.web3j.utils.Async.lambda$run$29(Async.java:22) at java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1626) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

zaf187 avatar Sep 05 '17 11:09 zaf187

As a quick work around you need to modify the generated java file, see as follows;

this is what gets generated for a function which returns a static array; public Future<StaticArray> getIntData() { Function function = new Function("getIntData", Arrays.asList(), Arrays.<TypeReference>asList(new TypeReference<StaticArray>() {})); return executeCallSingleValueReturnAsync(function); }

Modify to;

public Future<StaticArray<Uint8>> getIntData() {
    Function function = new Function("getIntData", 
            Arrays.<Type>asList(), 
            Arrays.<TypeReference<?>>asList(new TypeReference.StaticArrayTypeReference<StaticArray<Uint8>>(3) {}));
    return executeCallSingleValueReturnAsync(function);
}

You need to add StaticArrayTypeReference and then an int in the brackets ( ) representing the length of the array

zaf187 avatar Sep 06 '17 09:09 zaf187