首页 > 解决方案 > Hyperledger Fabric 评估交易是否返回签名查询结果?

问题描述

当交易提交到链码时,读写集连同背书节点的签名一起返回,然后交易提案连同背书签名一起发送到排序服务。我想知道当对链码发出查询请求时会发生什么。

假设我想查询资产的世界状态。我调用了 evaluateTransaction 方法。此方法的输出是否也由背书节点签名?

标签: hyperledger-fabricblockchainhyperledgerhyperledger-composerhyperledger-chaincode

解决方案


对于你问题的第一段

参考https://hyperledger-fabric.readthedocs.io/en/release-1.4/orderer/ordering_service.html#orderers-and-the-transaction-flow,我们简单将 tx flow 分为两步,proposal 和 ordering,当你查询chaincode,只需要proposl,不涉及ordering,proposal成功后会得到result。

对于你问题的第二段

在fabric v1.4.2源代码中peer/chaincode/common.go,该函数chaincodeInvokeOrQuery用作chaicode查询或调用,该方法将调用方法ChaincodeInvokeOrQuery

在方法的评论中ChaincodeInvokeOrQuery

// ChaincodeInvokeOrQuery invokes or queries the chaincode. If successful, the
// INVOKE form prints the ProposalResponse to STDOUT, and the QUERY form prints
// the query result on STDOUT. 

这意味着如果您在 cli 或终端中调用链代码查询,它将在 STDOUT 上输出结果(资产值),并且链代码调用将输出 ProposalResponse

但是您可以观察到,无论您调用查询或调用,ChaincodeInvokeOrQuery 返回相同格式的结果(ProposalResponse),这意味着查询的结果也包括背书签名,但我们选择不输出它。

func ChaincodeInvokeOrQuery(
    spec *pb.ChaincodeSpec,
    cID string,
    txID string,
    invoke bool,
    signer msp.SigningIdentity,
    certificate tls.Certificate,
    endorserClients []pb.EndorserClient,
    deliverClients []api.PeerDeliverClient,
    bc common.BroadcastClient,
) (*pb.ProposalResponse, error) {
    // Build the ChaincodeInvocationSpec message
    invocation := &pb.ChaincodeInvocationSpec{ChaincodeSpec: spec}

    creator, err := signer.Serialize()
    if err != nil {
        return nil, errors.WithMessage(err, fmt.Sprintf("error serializing identity for %s", signer.GetIdentifier()))
    }

    funcName := "invoke"
    if !invoke {
        funcName = "query"
    }

    // extract the transient field if it exists
    var tMap map[string][]byte
    if transient != "" {
        if err := json.Unmarshal([]byte(transient), &tMap); err != nil {
            return nil, errors.Wrap(err, "error parsing transient string")
        }
    }

    prop, txid, err := putils.CreateChaincodeProposalWithTxIDAndTransient(pcommon.HeaderType_ENDORSER_TRANSACTION, cID, invocation, creator, txID, tMap)
    if err != nil {
        return nil, errors.WithMessage(err, fmt.Sprintf("error creating proposal for %s", funcName))
    }

    signedProp, err := putils.GetSignedProposal(prop, signer)
    if err != nil {
        return nil, errors.WithMessage(err, fmt.Sprintf("error creating signed proposal for %s", funcName))
    }
    var responses []*pb.ProposalResponse
    for _, endorser := range endorserClients {
        proposalResp, err := endorser.ProcessProposal(context.Background(), signedProp)
        if err != nil {
            return nil, errors.WithMessage(err, fmt.Sprintf("error endorsing %s", funcName))
        }
        responses = append(responses, proposalResp)
    }

    if len(responses) == 0 {
        // this should only happen if some new code has introduced a bug
        return nil, errors.New("no proposal responses received - this might indicate a bug")
    }
    // all responses will be checked when the signed transaction is created.
    // for now, just set this so we check the first response's status
    proposalResp := responses[0]
   
    //till here,query and invoke almost same,if invoke ,it will continue execution 
      ordering step,if query ,it will return 

    if invoke {
        if proposalResp != nil {
            if proposalResp.Response.Status >= shim.ERRORTHRESHOLD {
                return proposalResp, nil
            }
            // assemble a signed transaction (it's an Envelope message)
            env, err := putils.CreateSignedTx(prop, signer, responses...)
            if err != nil {
                return proposalResp, errors.WithMessage(err, "could not assemble transaction")
            }
            var dg *deliverGroup
            var ctx context.Context
            if waitForEvent {
                var cancelFunc context.CancelFunc
                ctx, cancelFunc = context.WithTimeout(context.Background(), waitForEventTimeout)
                defer cancelFunc()

                dg = newDeliverGroup(deliverClients, peerAddresses, certificate, channelID, txid)
                // connect to deliver service on all peers
                err := dg.Connect(ctx)
                if err != nil {
                    return nil, err
                }
            }

            // send the envelope for ordering
            if err = bc.Send(env); err != nil {
                return proposalResp, errors.WithMessage(err, fmt.Sprintf("error sending transaction for %s", funcName))
            }

            if dg != nil && ctx != nil {
                // wait for event that contains the txid from all peers
                err = dg.Wait(ctx)
                if err != nil {
                    return nil, err
                }
            }
        }
    }

    return proposalResp, nil
}

推荐阅读