Search in sources :

Example 6 with TransactionException

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

the class Channel method parseConfigBlock.

protected void parseConfigBlock() throws TransactionException {
    Map<String, MSP> lmsps = msps;
    if (lmsps != null && !lmsps.isEmpty()) {
        return;
    }
    try {
        Block parseFrom = getConfigBlock(getShuffledPeers());
        // final Block configBlock = getConfigurationBlock();
        logger.debug(format("Channel %s Got config block getting MSP data and anchorPeers data", name));
        Envelope envelope = Envelope.parseFrom(parseFrom.getData().getData(0));
        Payload payload = Payload.parseFrom(envelope.getPayload());
        ConfigEnvelope configEnvelope = ConfigEnvelope.parseFrom(payload.getData());
        ConfigGroup channelGroup = configEnvelope.getConfig().getChannelGroup();
        Map<String, MSP> newMSPS = traverseConfigGroupsMSP("", channelGroup, new HashMap<>(20));
        msps = Collections.unmodifiableMap(newMSPS);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new TransactionException(e);
    }
}
Also used : TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) ConfigEnvelope(org.hyperledger.fabric.protos.common.Configtx.ConfigEnvelope) Block(org.hyperledger.fabric.protos.common.Common.Block) Payload(org.hyperledger.fabric.protos.common.Common.Payload) ProtoUtils.getSignatureHeaderAsByteString(org.hyperledger.fabric.sdk.transaction.ProtoUtils.getSignatureHeaderAsByteString) ByteString(com.google.protobuf.ByteString) ConfigEnvelope(org.hyperledger.fabric.protos.common.Configtx.ConfigEnvelope) ConfigUpdateEnvelope(org.hyperledger.fabric.protos.common.Configtx.ConfigUpdateEnvelope) Envelope(org.hyperledger.fabric.protos.common.Common.Envelope) ProtoUtils.createSeekInfoEnvelope(org.hyperledger.fabric.sdk.transaction.ProtoUtils.createSeekInfoEnvelope) ConfigGroup(org.hyperledger.fabric.protos.common.Configtx.ConfigGroup) EventHubException(org.hyperledger.fabric.sdk.exception.EventHubException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) TransactionEventException(org.hyperledger.fabric.sdk.exception.TransactionEventException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) StatusRuntimeException(io.grpc.StatusRuntimeException) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException) TimeoutException(java.util.concurrent.TimeoutException) ProposalException(org.hyperledger.fabric.sdk.exception.ProposalException)

Example 7 with TransactionException

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

the class PeerEventServiceClient method peerVent.

// =========================================================
// Peer eventing
void peerVent(TransactionContext transactionContext) throws TransactionException {
    final Envelope envelope;
    try {
        Ab.SeekPosition.Builder start = Ab.SeekPosition.newBuilder();
        if (null != peerOptions.getNewest()) {
            start.setNewest(Ab.SeekNewest.getDefaultInstance());
        } else if (peerOptions.getStartEvents() != null) {
            start.setSpecified(Ab.SeekSpecified.newBuilder().setNumber(peerOptions.getStartEvents()));
        } else {
            start.setNewest(Ab.SeekNewest.getDefaultInstance());
        }
        // properties.
        envelope = createSeekInfoEnvelope(transactionContext, start.build(), Ab.SeekPosition.newBuilder().setSpecified(Ab.SeekSpecified.newBuilder().setNumber(peerOptions.getStopEvents()).build()).build(), SeekInfo.SeekBehavior.BLOCK_UNTIL_READY, clientTLSCertificateDigest);
        connectEnvelope(envelope);
    } catch (CryptoException e) {
        throw new TransactionException(e);
    }
}
Also used : TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) ProtoUtils.createSeekInfoEnvelope(org.hyperledger.fabric.sdk.transaction.ProtoUtils.createSeekInfoEnvelope) Envelope(org.hyperledger.fabric.protos.common.Common.Envelope) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException)

Example 8 with TransactionException

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

the class PeerEventServiceClient method connectEnvelope.

/**
 * Get the last block received by this peer.
 *
 * @return The last block received by this peer. May return null if no block has been received since first reactivated.
 */
void connectEnvelope(Envelope envelope) throws TransactionException {
    if (shutdown) {
        throw new TransactionException("Peer eventing client is shutdown");
    }
    ManagedChannel lmanagedChannel = managedChannel;
    if (lmanagedChannel == null || lmanagedChannel.isTerminated() || lmanagedChannel.isShutdown()) {
        lmanagedChannel = channelBuilder.build();
        managedChannel = lmanagedChannel;
    }
    try {
        DeliverGrpc.DeliverStub broadcast = DeliverGrpc.newStub(lmanagedChannel);
        // final DeliverResponse[] ret = new DeliverResponse[1];
        // final List<DeliverResponse> retList = new ArrayList<>();
        final List<Throwable> throwableList = new ArrayList<>();
        final CountDownLatch finishLatch = new CountDownLatch(1);
        so = new StreamObserver<DeliverResponse>() {

            @Override
            public void onNext(DeliverResponse resp) {
                // logger.info("Got Broadcast response: " + resp);
                logger.trace(format("DeliverResponse channel %s peer %s resp status value:%d  status %s, typecase %s ", channelName, peer.getName(), resp.getStatusValue(), resp.getStatus(), resp.getTypeCase()));
                final DeliverResponse.TypeCase typeCase = resp.getTypeCase();
                if (typeCase == STATUS) {
                    logger.debug(format("DeliverResponse channel %s peer %s setting done.", channelName, peer.getName()));
                    if (resp.getStatus() == Common.Status.SUCCESS) {
                        // unlike you may think this only happens when all blocks are fetched.
                        peer.setLastConnectTime(System.currentTimeMillis());
                        peer.resetReconnectCount();
                    } else {
                        throwableList.add(new TransactionException(format("Channel %s peer %s Status returned failure code %d (%s) during peer service event registration", channelName, peer.getName(), resp.getStatusValue(), resp.getStatus().name())));
                    }
                } else if (typeCase == FILTERED_BLOCK || typeCase == BLOCK) {
                    if (typeCase == BLOCK) {
                        logger.trace(format("Channel %s peer %s got event block hex hashcode: %016x, block number: %d", channelName, peer.getName(), resp.getBlock().hashCode(), resp.getBlock().getHeader().getNumber()));
                    } else {
                        logger.trace(format("Channel %s peer %s got event block hex hashcode: %016x, block number: %d", channelName, peer.getName(), resp.getFilteredBlock().hashCode(), resp.getFilteredBlock().getNumber()));
                    }
                    peer.setLastConnectTime(System.currentTimeMillis());
                    long reconnectCount = peer.getReconnectCount();
                    if (reconnectCount > 1) {
                        logger.info(format("Peer eventing service reconnected after %d attempts on channel %s, peer %s, url %s", reconnectCount, channelName, name, url));
                    }
                    peer.resetReconnectCount();
                    BlockEvent blockEvent = new BlockEvent(peer, resp);
                    peer.setLastBlockSeen(blockEvent);
                    channelEventQue.addBEvent(blockEvent);
                } else {
                    logger.error(format("Channel %s peer %s got event block with unknown type: %s, %d", channelName, peer.getName(), typeCase.name(), typeCase.getNumber()));
                    throwableList.add(new TransactionException(format("Channel %s peer %s Status got unknown type %s, %d", channelName, peer.getName(), typeCase.name(), typeCase.getNumber())));
                }
                finishLatch.countDown();
            }

            @Override
            public void onError(Throwable t) {
                ManagedChannel llmanagedChannel = managedChannel;
                if (llmanagedChannel != null) {
                    llmanagedChannel.shutdownNow();
                    managedChannel = null;
                }
                if (!shutdown) {
                    final long reconnectCount = peer.getReconnectCount();
                    if (PEER_EVENT_RECONNECTION_WARNING_RATE > 1 && reconnectCount % PEER_EVENT_RECONNECTION_WARNING_RATE == 1) {
                        logger.warn(format("Received error on peer eventing service on channel %s, peer %s, url %s, attempts %d. %s", channelName, name, url, reconnectCount, t.getMessage()));
                    } else {
                        logger.trace(format("Received error on peer eventing service on channel %s, peer %s, url %s, attempts %d. %s", channelName, name, url, reconnectCount, t.getMessage()));
                    }
                    peer.reconnectPeerEventServiceClient(PeerEventServiceClient.this, t);
                }
                finishLatch.countDown();
            }

            @Override
            public void onCompleted() {
                logger.debug(format("DeliverResponse onCompleted channel %s peer %s setting done.", channelName, peer.getName()));
                // done = true;
                // There should have been a done before this...
                finishLatch.countDown();
            }
        };
        nso = filterBlock ? broadcast.deliverFiltered(so) : broadcast.deliver(so);
        nso.onNext(envelope);
        // try {
        if (!finishLatch.await(peerEventRegistrationWaitTimeMilliSecs, TimeUnit.MILLISECONDS)) {
            TransactionException ex = new TransactionException(format("Channel %s connect time exceeded for peer eventing service %s, timed out at %d ms.", channelName, name, peerEventRegistrationWaitTimeMilliSecs));
            throwableList.add(0, ex);
        }
        logger.trace("Done waiting for reply!");
        if (!throwableList.isEmpty()) {
            ManagedChannel llmanagedChannel = managedChannel;
            if (llmanagedChannel != null) {
                llmanagedChannel.shutdownNow();
                managedChannel = null;
            }
            Throwable throwable = throwableList.get(0);
            peer.reconnectPeerEventServiceClient(this, throwable);
        }
    } catch (InterruptedException e) {
        ManagedChannel llmanagedChannel = managedChannel;
        if (llmanagedChannel != null) {
            llmanagedChannel.shutdownNow();
            managedChannel = null;
        }
        // not likely
        logger.error(e);
        peer.reconnectPeerEventServiceClient(this, e);
    } finally {
        if (null != nso) {
            try {
                nso.onCompleted();
            } catch (Exception e) {
                // Best effort only report on debug
                logger.debug(format("Exception completing connect with channel %s,  name %s, url %s %s", channelName, name, url, e.getMessage()), e);
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException) TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) DeliverGrpc(org.hyperledger.fabric.protos.peer.DeliverGrpc) ManagedChannel(io.grpc.ManagedChannel) DeliverResponse(org.hyperledger.fabric.protos.peer.PeerEvents.DeliverResponse)

Example 9 with TransactionException

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

the class OrdererClient method sendDeliver.

DeliverResponse[] sendDeliver(Common.Envelope envelope) throws TransactionException {
    if (shutdown) {
        throw new TransactionException("Orderer client is shutdown");
    }
    StreamObserver<Common.Envelope> nso = null;
    ManagedChannel lmanagedChannel = managedChannel;
    if (lmanagedChannel == null || lmanagedChannel.isTerminated() || lmanagedChannel.isShutdown()) {
        lmanagedChannel = channelBuilder.build();
        managedChannel = lmanagedChannel;
    }
    try {
        AtomicBroadcastGrpc.AtomicBroadcastStub broadcast = AtomicBroadcastGrpc.newStub(lmanagedChannel);
        // final DeliverResponse[] ret = new DeliverResponse[1];
        final List<DeliverResponse> retList = new ArrayList<>();
        final List<Throwable> throwableList = new ArrayList<>();
        final CountDownLatch finishLatch = new CountDownLatch(1);
        StreamObserver<DeliverResponse> so = new StreamObserver<DeliverResponse>() {

            boolean done = false;

            @Override
            public void onNext(DeliverResponse resp) {
                // logger.info("Got Broadcast response: " + resp);
                logger.debug("resp status value: " + resp.getStatusValue() + ", resp: " + resp.getStatus() + ", type case: " + resp.getTypeCase());
                if (done) {
                    return;
                }
                if (resp.getTypeCase() == STATUS) {
                    done = true;
                    retList.add(0, resp);
                    finishLatch.countDown();
                } else {
                    retList.add(resp);
                }
            }

            @Override
            public void onError(Throwable t) {
                if (!shutdown) {
                    logger.error(format("Received error on channel %s, orderer %s, url %s, %s", channelName, name, url, t.getMessage()), t);
                }
                throwableList.add(t);
                finishLatch.countDown();
            }

            @Override
            public void onCompleted() {
                logger.trace("onCompleted");
                finishLatch.countDown();
            }
        };
        nso = broadcast.deliver(so);
        nso.onNext(envelope);
        try {
            if (!finishLatch.await(ordererWaitTimeMilliSecs, TimeUnit.MILLISECONDS)) {
                TransactionException ex = new TransactionException(format("Channel %s sendDeliver time exceeded for orderer %s, timed out at %d ms.", channelName, name, ordererWaitTimeMilliSecs));
                logger.error(ex.getMessage(), ex);
                throw ex;
            }
            logger.trace("Done waiting for reply!");
        } catch (InterruptedException e) {
            logger.error(e);
        }
        if (!throwableList.isEmpty()) {
            Throwable throwable = throwableList.get(0);
            TransactionException e = new TransactionException(format("Channel %s sendDeliver failed on orderer %s. Reason: %s", channelName, name, throwable.getMessage()), throwable);
            logger.error(e.getMessage(), e);
            throw e;
        }
        return retList.toArray(new DeliverResponse[retList.size()]);
    } catch (Throwable t) {
        managedChannel = null;
        throw t;
    } finally {
        if (null != nso) {
            try {
                nso.onCompleted();
            } catch (Exception e) {
                // Best effort only report on debug
                logger.debug(format("Exception completing sendDeliver with channel %s,  name %s, url %s %s", channelName, name, url, e.getMessage()), e);
            }
        }
    }
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) AtomicBroadcastGrpc(org.hyperledger.fabric.protos.orderer.AtomicBroadcastGrpc) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) ManagedChannel(io.grpc.ManagedChannel) DeliverResponse(org.hyperledger.fabric.protos.orderer.Ab.DeliverResponse)

Example 10 with TransactionException

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

the class ChannelTest method setupClient.

@BeforeClass
public static void setupClient() {
    try {
        hfclient = TestHFClient.newInstance();
        shutdownChannel = new Channel("shutdown", hfclient);
        shutdownChannel.addOrderer(hfclient.newOrderer("shutdow_orderer", "grpc://localhost:99"));
        setField(shutdownChannel, "shutdown", true);
        throwOrderer = new Orderer("foo", "grpc://localhost:8", null) {

            @Override
            Ab.BroadcastResponse sendTransaction(Common.Envelope transaction) throws Exception {
                throw new Exception(BAD_STUFF);
            }

            @Override
            Ab.DeliverResponse[] sendDeliver(Common.Envelope transaction) throws TransactionException {
                throw new TransactionException(BAD_STUFF);
            }
        };
        throwChannel = new Channel("throw", hfclient);
        throwChannel.addOrderer(throwOrderer);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Unexpected Exception " + e.getMessage());
    }
}
Also used : Ab(org.hyperledger.fabric.protos.orderer.Ab) TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) PeerException(org.hyperledger.fabric.sdk.exception.PeerException) ExpectedException(org.junit.rules.ExpectedException) InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) ProposalException(org.hyperledger.fabric.sdk.exception.ProposalException) StatusRuntimeException(io.grpc.StatusRuntimeException) TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) Common(org.hyperledger.fabric.protos.common.Common) BeforeClass(org.junit.BeforeClass)

Aggregations

TransactionException (org.hyperledger.fabric.sdk.exception.TransactionException)15 CryptoException (org.hyperledger.fabric.sdk.exception.CryptoException)11 StatusRuntimeException (io.grpc.StatusRuntimeException)10 InvalidArgumentException (org.hyperledger.fabric.sdk.exception.InvalidArgumentException)10 ProposalException (org.hyperledger.fabric.sdk.exception.ProposalException)10 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)9 IOException (java.io.IOException)9 ExecutionException (java.util.concurrent.ExecutionException)9 TimeoutException (java.util.concurrent.TimeoutException)9 EventHubException (org.hyperledger.fabric.sdk.exception.EventHubException)9 TransactionEventException (org.hyperledger.fabric.sdk.exception.TransactionEventException)9 Block (org.hyperledger.fabric.protos.common.Common.Block)6 ArrayList (java.util.ArrayList)5 Envelope (org.hyperledger.fabric.protos.common.Common.Envelope)5 DeliverResponse (org.hyperledger.fabric.protos.orderer.Ab.DeliverResponse)5 ProtoUtils.createSeekInfoEnvelope (org.hyperledger.fabric.sdk.transaction.ProtoUtils.createSeekInfoEnvelope)5 ConfigEnvelope (org.hyperledger.fabric.protos.common.Configtx.ConfigEnvelope)4 ConfigUpdateEnvelope (org.hyperledger.fabric.protos.common.Configtx.ConfigUpdateEnvelope)4 ByteString (com.google.protobuf.ByteString)3 ManagedChannel (io.grpc.ManagedChannel)3