从Hyperledger Fabric事务中检索结果

时间:2018-05-16 19:52:50

标签: javascript hyperledger-fabric hyperledger hyperledger-composer

我向Hyperledger Fabric提交了一个事务,但是我想通过它创建对象。

我从中得到的对象是Undefined。 Obs:在Hyperledger Fabric中成功创建了事务。

async submit(resource, method) {
    try{
      this.businessNetworkDefinition = await this.bizNetworkConnection.connect(cardname);
      if (!this.businessNetworkDefinition) {
        console.log("Error in network connection");
        throw "Error in network connection";
      }

      let factory        = this.businessNetworkDefinition.getFactory();
      let transaction    = factory.newTransaction(NS, method);

      Object.assign(transaction, resource)
      return await this.bizNetworkConnection.submitTransaction(transaction);
    }catch(error){
      console.log(error);
      throw error;
    }
  }

2 个答案:

答案 0 :(得分:1)

目前,submitTransaction函数没有返回任何内容。这是一个错误或按预期工作。

进一步详细说明:当您深入研究作曲家的源代码时,您最终会在composer-connector-hlfv1中找到以下代码。

invokeChainCode(securityContext, functionName, args, options) {
        const method = 'invokeChainCode';
        LOG.entry(method, securityContext, functionName, args, options);

        if (!this.businessNetworkIdentifier) {
            return Promise.reject(new Error('No business network has been specified for this connection'));
        }

        // Check that a valid security context has been specified.
        HLFUtil.securityCheck(securityContext);

        // Validate all the arguments.
        if (!functionName) {
            return Promise.reject(new Error('functionName not specified'));
        } else if (!Array.isArray(args)) {
            return Promise.reject(new Error('args not specified'));
        }

        try {
            args.forEach((arg) => {
                if (typeof arg !== 'string') {
                    throw new Error('invalid arg specified: ' + arg);
                }
            });
        } catch(error) {
            return Promise.reject(error);
        }

        let txId = this._validateTxId(options);

        let eventHandler;

        // initialize the channel if it hasn't been initialized already otherwise verification will fail.
        LOG.debug(method, 'loading channel configuration');
        return this._initializeChannel()
            .then(() => {

                // check the event hubs and reconnect if possible. Do it here as the connection attempts are asynchronous
                this._checkEventhubs();

                // Submit the transaction to the endorsers.
                const request = {
                    chaincodeId: this.businessNetworkIdentifier,
                    txId: txId,
                    fcn: functionName,
                    args: args
                };
                return this.channel.sendTransactionProposal(request); // node sdk will target all peers on the channel that are endorsingPeer
            })
            .then((results) => {
                // Validate the endorsement results.
                LOG.debug(method, `Received ${results.length} result(s) from invoking the composer runtime chaincode`, results);
                const proposalResponses = results[0];
                let {validResponses} = this._validatePeerResponses(proposalResponses, true);

                // Submit the endorsed transaction to the primary orderers.
                const proposal = results[1];
                const header = results[2];

                // check that we have a Chaincode listener setup and ready.
                this._checkCCListener();
                eventHandler = HLFConnection.createTxEventHandler(this.eventHubs, txId.getTransactionID(), this.commitTimeout);
                eventHandler.startListening();
                return this.channel.sendTransaction({
                    proposalResponses: validResponses,
                    proposal: proposal,
                    header: header
                });
            })
            .then((response) => {
                // If the transaction was successful, wait for it to be committed.
                LOG.debug(method, 'Received response from orderer', response);

                if (response.status !== 'SUCCESS') {
                    eventHandler.cancelListening();
                    throw new Error(`Failed to send peer responses for transaction '${txId.getTransactionID()}' to orderer. Response status '${response.status}'`);
                }
                return eventHandler.waitForEvents();
            })
            .then(() => {
                LOG.exit(method);
            })
            .catch((error) => {
                const newError = new Error('Error trying invoke business network. ' + error);
                LOG.error(method, newError);
                throw newError;
            });
    }

正如您在最后看到的那样,所有发生的事情都在等待事件和Log.exit,它们什么都不返回。所以目前你必须以另一种方式获得交易结果。

答案 1 :(得分:0)

我从链码中获取内容的唯一方法是通过事件。有原生界面可能能够查询交易数据或类似的事情,但我还没有调查过。