猿问

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

我正在使用 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());

获取数据对象的理想方法是什么?


回首忆惘然
浏览 116回答 3
3回答

MM们

终于找到了一种将 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 indexFabricTransaction.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();

慕桂英3389331

从事务中获取请求数据时,我们遇到了类似的问题。以下代码将有助于获取交易的交易请求数据面料 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();

侃侃尔雅

我找到了答案。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());});如果我能找到更多,让我补充一下。欢迎大家补充评论。
随时随地看视频慕课网APP

相关分类

Java
我要回答