hyperledger-fabrichyperledger-chaincode

Return type of transaction?


I'm running chaincode-java from fabric-samples.

    @Transaction(intent = Transaction.TYPE.EVALUATE)
    public ArrayList<Asset> GetAllAssets(final Context ctx) {
        ChaincodeStub stub = ctx.getStub();

        ArrayList<Asset> queryResults = new ArrayList<Asset>();

        // To retrieve all assets from the ledger use getStateByRange with empty startKey & endKey.
        // Giving empty startKey & endKey is interpreted as all the keys from beginning to end.
        // As another example, if you use startKey = 'asset0', endKey = 'asset9' ,
        // then getStateByRange will retrieve asset with keys between asset0 (inclusive) and asset9 (exclusive) in lexical order.
        QueryResultsIterator<KeyValue> results = stub.getStateByRange("", "");

        for (KeyValue result: results) {
            Asset asset = genson.deserialize(result.getStringValue(), Asset.class);
            System.out.println(asset);
            queryResults.add(asset);
        }

//        final String response = genson.serialize(queryResults);

        return queryResults;
    }

The GetAllAssets() method was returning String, but I changed it to ArrayList.

As a result, GetAllAssets throws error when invoked.

$ peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
Error: endorsement failure during query. response: status:500 message:"Unexpected error"

The log says

Thread[fabric-txinvoke:2,5,main] 11:15:01:224 INFO    org.hyperledger.fabric.contract.ContractRouter processRequest                    Got invoke routing request
Thread[fabric-txinvoke:2,5,main] 11:15:01:226 INFO    org.hyperledger.fabric.contract.ContractRouter processRequest                    Got the invoke request for:GetAllAssets []
Thread[fabric-txinvoke:2,5,main] 11:15:01:234 INFO    org.hyperledger.fabric.contract.ContractRouter processRequest                    Got routing:GetAllAssets:org.hyperledger.fabric.samples.assettransfer.AssetTransfer
Thread[fabric-txinvoke:2,5,main] 11:15:01:274 SEVERE  org.hyperledger.fabric.Logger error                                              nulljava.lang.NullPointerException
        at org.hyperledger.fabric.contract.execution.JSONTransactionSerializer.toBuffer(JSONTransactionSerializer.java:84)
        at org.hyperledger.fabric.contract.execution.impl.ContractExecutionService.convertReturn(ContractExecutionService.java:89)
        at org.hyperledger.fabric.contract.execution.impl.ContractExecutionService.executeRequest(ContractExecutionService.java:67)
        at org.hyperledger.fabric.contract.ContractRouter.processRequest(ContractRouter.java:123)
        at org.hyperledger.fabric.contract.ContractRouter.invoke(ContractRouter.java:134)
        at org.hyperledger.fabric.shim.impl.ChaincodeInvocationTask.call(ChaincodeInvocationTask.java:106)
        at org.hyperledger.fabric.shim.impl.InvocationTaskManager.lambda$newTask$17(InvocationTaskManager.java:265)
        at java.base/java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1736)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        at java.base/java.lang.Thread.run(Thread.java:834)

Thread[fabric-txinvoke:2,5,main] 11:15:01:276 SEVERE  org.hyperledger.fabric.shim.impl.ChaincodeInvocationTask call                    [13733a23] Invoke failed with error code 500. Sending ERROR

Can I return List from a transaction? Besides String, what other types can I return? Is there any documentation that I can take a look?


Solution

  • Bit of background first; the ContractAPI that is available in Java, Go and Typescript is used to generate a 'model' of the overall contract including the data type that be passed and returned from transaction functions. (JavaScript supports a limited subset to the extent possible based on it's typing).

    In order to support this there has to be a 'serializer' of some sort to process the data. The underlying chaincode API of just 'invoke(byte[]): byte[]' gives the developer the power to serialize how they wish though not all of us need to use that power.

    There is a default 'serializer' in the ContractAPI; this can be swapped out if needed.

    To specifically answer the question;

    The return types can be:

    For the 'other types', there are annotations that can be used to define types that can also be passed to and from the transaction functions.

    You might see something like this:

    @DataType()
    public final class Complex {
    
        @Property()
        private final String id;
    
        @Property()
        private final Description description;
    
        @Property()
        private final int value;
    
        public String getID() {
            return id;
        }
    
        public int getValue() {
            return value;
        }
    
        public Description getDescription(){
            return description;
        }
    }
    

    Description there is also a class annotated in a similar manner.

    This would produce the Contract Metadata that would look like

                "Description": {
                    "$id": "Description",
                    "type": "object",
                    "properties": {
                        "colour": {
                            "type": "string"
                        },
                        "owners": {
                            "type": "array",
                            "items": {
                                "type": "string"
                            }
                        }
                    }
                },
                "Complex": {
                    "$id": "Complex",
                    "type": "object",
                    "properties": {
                        "id": {
                            "type": "string"
                        },
                        "value": {
                            "type": "number"
                        },
                        "description": {
                            "$ref": "Description"
                        }
                    }
                }
    

    On the Contract Model, or Contract Metadata

    There is a JSON schema for this at
    https://github.com/hyperledger/fabric-chaincode-node/blob/main/apis/fabric-contract-api/schema/contract-schema.json

    Isn't this restrictive? what about Lists? It's a fair comment, from a Java perspective, something like an ArrayList or Map would be a reasonable thing to return. However the challenge is that it is possible for the contracts to be implemented in different languages. Plus once deployed the Contract will be running for some time, therefore the metadata provides a strong 'API Definition' between the Smart Contract and the Client Application.

    What transaction functions (also in the metadata) will be clearly defined.

    Summary I would like to provide some more examples (and docs!) but wanted to get this written up first. There are extensions and changes we could make, and would like to make but there are only so many hours!

    As a maintainer of these repos, we'd love to have people come on board if this is an area of interest.