代码之家  ›  专栏  ›  技术社区  ›  Gqqnbig

交易的返回类型?

  •  0
  • Gqqnbig  · 技术社区  · 4 年前

    我在跑步 chaincode-java 从织物样品中提取。

        @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;
        }
    

    GetAllAssets()方法返回字符串,但我将其更改为ArrayList。

    因此,调用GetAllAssets时会抛出错误。

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

    日志上写着

    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
    

    我能回来吗 List 从交易中?此外 String ,我还可以退什么类型的?有什么文件我可以看一下吗?

    0 回复  |  直到 4 年前
        1
  •  1
  •   Calanais    4 年前

    先了解一下背景; Java、Go和Typescript中提供的ContractAPI用于生成整个契约的“模型”,包括从事务函数传递和返回的数据类型。(JavaScript根据其类型尽可能支持有限的子集)。

    为了支持这一点,必须有某种“序列化程序”来处理数据。just'invoke(byte[]):byte[]的底层链码API使开发人员能够按照自己的意愿进行序列化,尽管并非所有人都需要使用这种能力。

    ContractAPI中有一个默认的“序列化程序”;如果需要,可以将其换掉。

    具体回答问题;

    退货类型可以是:

    • 串,
    • 数字(对于Java,这是任何基本的“数字”类型)
    • 布尔人,
    • 已注释的其他类型。
    • 上面的数组

    对于“其他类型”,可以使用注释来定义类型,这些类型也可以传递给事务函数或从事务函数传递出去。

    你可能会看到这样的情况:

    @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": {
                    "$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"
                        }
                    }
                }
    

    在合同模型或合同元数据上

    这里有一个JSON模式
    https://github.com/hyperledger/fabric-chaincode-node/blob/main/apis/fabric-contract-api/schema/contract-schema.json

    这不是限制性的吗?名单呢? 这是一个公平的评论,从Java的角度来看,类似ArrayList或Map的东西是一个合理的回报。然而,挑战在于,合同有可能以不同的语言执行。此外,一旦部署,契约将运行一段时间,因此元数据在智能契约和客户端应用程序之间提供了强大的“API定义”。

    将明确定义哪些事务功能(也在元数据中)。

    总结 我想提供更多的例子(和文档!)但我想先写下来。我们可以进行扩展和更改,也希望进行扩展和更改,但时间有限!

    作为这些回购协议的维护者,如果这是我们感兴趣的领域,我们希望有人参与进来。