为了账号安全,请及时绑定邮箱和手机立即绑定

如何解析 Envelope.Payload.Data 的 protobuf 数据?

如何解析 Envelope.Payload.Data 的 protobuf 数据?

回首忆惘然 2023-06-14 16:04:15
我正在使用 Hyperledger Fabric Java SDK 通过 txId 获取交易。返回对象包括交易信息。TransactionInfo txInfo = channel.queryTransactionByID(txId);Common.Envelope envelope = txInfo.getEnvelope();Common.Payload payload = Common.Payload.parseFrom(envelope.getPayload());该Payload消息包括标题和数据。我可以使用Common.Header.ChannelHeader和来解析标头Common.Header.SignatureHeader。Common.ChannelHeader channelHeader = Common.ChannelHeader.parseFrom(payload.getHeader().getChannelHeader());Common.SignatureHeader signatureHeader = Common.SignatureHeader.parseFrom(payload.getHeader().getSignatureHeader());问题是,我看不到任何消息类型来从中获取数据Payload。我的期望是,SomeMessage someMsg = SomeMessage.parseFrom(payload.getData());获取数据对象的理想方法是什么?
查看完整描述

3 回答

?
MM们

TA贡献1886条经验 获得超2个赞

终于找到了一种将 QSCC 响应解析为 TxReadWriteSet 甚至 KVRWSet 的方法!!这是一个例子:


TransactionInfo txInfo = channel.queryTransactionByID(txId);

Common.Envelope envelope = txInfo.getEnvelope();

Common.Payload payload = Common.Payload.parseFrom(envelope.getPayload());

FabricTransaction.Transaction transaction = FabricTransaction.Transaction.parseFrom(payload.getData());

FabricTransaction.TransactionAction action = transaction.getActionsList().get(0); // 0 is a index

FabricTransaction.ChaincodeActionPayload chaincodeActionPayload = FabricTransaction.ChaincodeActionPayload.parseFrom(action.getPayload());

FabricProposalResponse.ProposalResponsePayload prp = FabricProposalResponse.ProposalResponsePayload.parseFrom(chaincodeActionPayload.getAction().getProposalResponsePayload());

FabricProposal.ChaincodeAction ca = FabricProposal.ChaincodeAction.parseFrom(prp.getExtension());

Rwset.TxReadWriteSet txrws = Rwset.TxReadWriteSet.parseFrom(ca.getResults());

TxReadWriteSetInfo txrwsInfo = new TxReadWriteSetInfo(txrws);

KvRwset.KVRWSet kvrwSet = txrwsInfo.getNsRwsetInfo(0).getRwset();

KvRwset.KVWrite kvWrite = kvrwSet.getWrites(0);

String writeVal = kvWrite.getValue().toStringUtf8();


查看完整回答
反对 回复 2023-06-14
?
慕桂英3389331

TA贡献2036条经验 获得超8个赞

从事务中获取请求数据时,我们遇到了类似的问题。


以下代码将有助于获取交易的交易请求数据


面料 SDK 版本:2.1.4


        // get transaction from transaction ID

        TransactionInfo txInfo = channel.queryTransactionByID(txId);

        // transaction is stored inside the envelope containing the payload and signature

        Common.Envelope envelope = txInfo.getEnvelope();

        // parse payload from the envelope

        Common.Payload payload = Common.Payload.parseFrom(envelope.getPayload());

        // payload contains Header and Data. We are parsing data to get the transaction

        TransactionPackage.Transaction transaction = TransactionPackage.Transaction.parseFrom(payload.getData());

        // get first action from the transaction action list. it contains input and other details

        TransactionPackage.TransactionAction action = transaction.getActionsList().get(0); // 0 is a index

        // chaincode action payload contains input parameters. So we are taking the action payload

        TransactionPackage.ChaincodeActionPayload chaincodeActionPayload = TransactionPackage.ChaincodeActionPayload.parseFrom(action.getPayload());

        // chaincode ProposalPayload contains Input and TransientMap. We are parsing actionPayload to proposalPayload

        ProposalPackage.ChaincodeProposalPayload prp = ProposalPackage.ChaincodeProposalPayload.parseFrom(chaincodeActionPayload.getChaincodeProposalPayload());

        // parse the input to chaincodeInvocationSpec so that we can unmarshal the input

        Chaincode.ChaincodeInvocationSpec chaincodeInvocationSpec = Chaincode.ChaincodeInvocationSpec.parseFrom(prp.getInput());


        // get the input and parse the arg list and get input arguments 

       chaincodeInvocationSpec.getChaincodeSpec().getInput().getArgsList().get(ChaincodeInput.ARGS_FIELD_NUMBER).toStringUtf8();



查看完整回答
反对 回复 2023-06-14
?
侃侃尔雅

TA贡献1801条经验 获得超15个赞

我找到了答案。


FabricTransaction.Transaction transaction =  FabricTransaction.Transaction.parseFrom(payload.getData());

在那之后,


FabricTransaction.TransactionAction action = transaction.getActionsList().get(index);

FabricTransaction.ChaincodeActionPayload chaincodeActionPayload = FabricTransaction.ChaincodeActionPayload.parseFrom(action.getPayload());

chaincodeActionPayload.getAction().getEndorsementsList().forEach(endorsement -> {

    // This is my current point

    ???? endorser = ????.parseFrom(endorsement.getEndorser());

});

如果我能找到更多,让我补充一下。欢迎大家补充评论。


查看完整回答
反对 回复 2023-06-14
  • 3 回答
  • 0 关注
  • 115 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信