Search in sources :

Example 1 with InvalidProtocolBufferRuntimeException

use of org.hyperledger.fabric.sdk.exception.InvalidProtocolBufferRuntimeException in project fabric-sdk-java by hyperledger.

the class End2endIT method blockWalker.

void blockWalker(HFClient client, Channel channel) throws InvalidArgumentException, ProposalException, IOException {
    try {
        BlockchainInfo channelInfo = channel.queryBlockchainInfo();
        for (long current = channelInfo.getHeight() - 1; current > -1; --current) {
            BlockInfo returnedBlock = channel.queryBlockByNumber(current);
            final long blockNumber = returnedBlock.getBlockNumber();
            out("current block number %d has data hash: %s", blockNumber, Hex.encodeHexString(returnedBlock.getDataHash()));
            out("current block number %d has previous hash id: %s", blockNumber, Hex.encodeHexString(returnedBlock.getPreviousHash()));
            out("current block number %d has calculated block hash is %s", blockNumber, Hex.encodeHexString(SDKUtils.calculateBlockHash(client, blockNumber, returnedBlock.getPreviousHash(), returnedBlock.getDataHash())));
            final int envelopeCount = returnedBlock.getEnvelopeCount();
            assertEquals(1, envelopeCount);
            out("current block number %d has %d envelope count:", blockNumber, returnedBlock.getEnvelopeCount());
            int i = 0;
            for (BlockInfo.EnvelopeInfo envelopeInfo : returnedBlock.getEnvelopeInfos()) {
                ++i;
                out("  Transaction number %d has transaction id: %s", i, envelopeInfo.getTransactionID());
                final String channelId = envelopeInfo.getChannelId();
                assertTrue("foo".equals(channelId) || "bar".equals(channelId));
                out("  Transaction number %d has channel id: %s", i, channelId);
                out("  Transaction number %d has epoch: %d", i, envelopeInfo.getEpoch());
                out("  Transaction number %d has transaction timestamp: %tB %<te,  %<tY  %<tT %<Tp", i, envelopeInfo.getTimestamp());
                out("  Transaction number %d has type id: %s", i, "" + envelopeInfo.getType());
                out("  Transaction number %d has nonce : %s", i, "" + Hex.encodeHexString(envelopeInfo.getNonce()));
                out("  Transaction number %d has submitter mspid: %s,  certificate: %s", i, envelopeInfo.getCreator().getMspid(), envelopeInfo.getCreator().getId());
                if (envelopeInfo.getType() == TRANSACTION_ENVELOPE) {
                    BlockInfo.TransactionEnvelopeInfo transactionEnvelopeInfo = (BlockInfo.TransactionEnvelopeInfo) envelopeInfo;
                    out("  Transaction number %d has %d actions", i, transactionEnvelopeInfo.getTransactionActionInfoCount());
                    // for now there is only 1 action per transaction.
                    assertEquals(1, transactionEnvelopeInfo.getTransactionActionInfoCount());
                    out("  Transaction number %d isValid %b", i, transactionEnvelopeInfo.isValid());
                    assertEquals(transactionEnvelopeInfo.isValid(), true);
                    out("  Transaction number %d validation code %d", i, transactionEnvelopeInfo.getValidationCode());
                    assertEquals(0, transactionEnvelopeInfo.getValidationCode());
                    int j = 0;
                    for (BlockInfo.TransactionEnvelopeInfo.TransactionActionInfo transactionActionInfo : transactionEnvelopeInfo.getTransactionActionInfos()) {
                        ++j;
                        out("   Transaction action %d has response status %d", j, transactionActionInfo.getResponseStatus());
                        assertEquals(200, transactionActionInfo.getResponseStatus());
                        out("   Transaction action %d has response message bytes as string: %s", j, printableString(new String(transactionActionInfo.getResponseMessageBytes(), "UTF-8")));
                        out("   Transaction action %d has %d endorsements", j, transactionActionInfo.getEndorsementsCount());
                        assertEquals(2, transactionActionInfo.getEndorsementsCount());
                        for (int n = 0; n < transactionActionInfo.getEndorsementsCount(); ++n) {
                            BlockInfo.EndorserInfo endorserInfo = transactionActionInfo.getEndorsementInfo(n);
                            out("Endorser %d signature: %s", n, Hex.encodeHexString(endorserInfo.getSignature()));
                            out("Endorser %d endorser: mspid %s \n certificate %s", n, endorserInfo.getMspid(), endorserInfo.getId());
                        }
                        out("   Transaction action %d has %d chaincode input arguments", j, transactionActionInfo.getChaincodeInputArgsCount());
                        for (int z = 0; z < transactionActionInfo.getChaincodeInputArgsCount(); ++z) {
                            out("     Transaction action %d has chaincode input argument %d is: %s", j, z, printableString(new String(transactionActionInfo.getChaincodeInputArgs(z), "UTF-8")));
                        }
                        out("   Transaction action %d proposal response status: %d", j, transactionActionInfo.getProposalResponseStatus());
                        out("   Transaction action %d proposal response payload: %s", j, printableString(new String(transactionActionInfo.getProposalResponsePayload())));
                        // Check to see if we have our expected event.
                        if (blockNumber == 2) {
                            ChaincodeEvent chaincodeEvent = transactionActionInfo.getEvent();
                            assertNotNull(chaincodeEvent);
                            assertTrue(Arrays.equals(EXPECTED_EVENT_DATA, chaincodeEvent.getPayload()));
                            assertEquals(testTxID, chaincodeEvent.getTxId());
                            assertEquals(CHAIN_CODE_NAME, chaincodeEvent.getChaincodeId());
                            assertEquals(EXPECTED_EVENT_NAME, chaincodeEvent.getEventName());
                        }
                        TxReadWriteSetInfo rwsetInfo = transactionActionInfo.getTxReadWriteSet();
                        if (null != rwsetInfo) {
                            out("   Transaction action %d has %d name space read write sets", j, rwsetInfo.getNsRwsetCount());
                            for (TxReadWriteSetInfo.NsRwsetInfo nsRwsetInfo : rwsetInfo.getNsRwsetInfos()) {
                                final String namespace = nsRwsetInfo.getNamespace();
                                KvRwset.KVRWSet rws = nsRwsetInfo.getRwset();
                                int rs = -1;
                                for (KvRwset.KVRead readList : rws.getReadsList()) {
                                    rs++;
                                    out("     Namespace %s read set %d key %s  version [%d:%d]", namespace, rs, readList.getKey(), readList.getVersion().getBlockNum(), readList.getVersion().getTxNum());
                                    if ("bar".equals(channelId) && blockNumber == 2) {
                                        if ("example_cc_go".equals(namespace)) {
                                            if (rs == 0) {
                                                assertEquals("a", readList.getKey());
                                                assertEquals(1, readList.getVersion().getBlockNum());
                                                assertEquals(0, readList.getVersion().getTxNum());
                                            } else if (rs == 1) {
                                                assertEquals("b", readList.getKey());
                                                assertEquals(1, readList.getVersion().getBlockNum());
                                                assertEquals(0, readList.getVersion().getTxNum());
                                            } else {
                                                fail(format("unexpected readset %d", rs));
                                            }
                                            TX_EXPECTED.remove("readset1");
                                        }
                                    }
                                }
                                rs = -1;
                                for (KvRwset.KVWrite writeList : rws.getWritesList()) {
                                    rs++;
                                    String valAsString = printableString(new String(writeList.getValue().toByteArray(), "UTF-8"));
                                    out("     Namespace %s write set %d key %s has value '%s' ", namespace, rs, writeList.getKey(), valAsString);
                                    if ("bar".equals(channelId) && blockNumber == 2) {
                                        if (rs == 0) {
                                            assertEquals("a", writeList.getKey());
                                            assertEquals("400", valAsString);
                                        } else if (rs == 1) {
                                            assertEquals("b", writeList.getKey());
                                            assertEquals("400", valAsString);
                                        } else {
                                            fail(format("unexpected writeset %d", rs));
                                        }
                                        TX_EXPECTED.remove("writeset1");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        if (!TX_EXPECTED.isEmpty()) {
            fail(TX_EXPECTED.get(0));
        }
    } catch (InvalidProtocolBufferRuntimeException e) {
        throw e.getCause();
    }
}
Also used : BlockchainInfo(org.hyperledger.fabric.sdk.BlockchainInfo) InvalidProtocolBufferRuntimeException(org.hyperledger.fabric.sdk.exception.InvalidProtocolBufferRuntimeException) BlockInfo(org.hyperledger.fabric.sdk.BlockInfo) TxReadWriteSetInfo(org.hyperledger.fabric.sdk.TxReadWriteSetInfo) KvRwset(org.hyperledger.fabric.protos.ledger.rwset.kvrwset.KvRwset) ChaincodeEvent(org.hyperledger.fabric.sdk.ChaincodeEvent)

Aggregations

KvRwset (org.hyperledger.fabric.protos.ledger.rwset.kvrwset.KvRwset)1 BlockInfo (org.hyperledger.fabric.sdk.BlockInfo)1 BlockchainInfo (org.hyperledger.fabric.sdk.BlockchainInfo)1 ChaincodeEvent (org.hyperledger.fabric.sdk.ChaincodeEvent)1 TxReadWriteSetInfo (org.hyperledger.fabric.sdk.TxReadWriteSetInfo)1 InvalidProtocolBufferRuntimeException (org.hyperledger.fabric.sdk.exception.InvalidProtocolBufferRuntimeException)1