Search in sources :

Example 6 with TransactionContext

use of org.hyperledger.fabric.sdk.transaction.TransactionContext in project fabric-sdk-java by hyperledger.

the class Channel method sendUpgradeProposal.

/**
 * Send Upgrade proposal proposal to upgrade chaincode to a new version.
 *
 * @param upgradeProposalRequest
 * @param peers                  the specific peers to send to.
 * @return Collection of proposal responses.
 * @throws ProposalException
 * @throws InvalidArgumentException
 */
public Collection<ProposalResponse> sendUpgradeProposal(UpgradeProposalRequest upgradeProposalRequest, Collection<Peer> peers) throws InvalidArgumentException, ProposalException {
    checkChannelState();
    checkPeers(peers);
    if (null == upgradeProposalRequest) {
        throw new InvalidArgumentException("Upgradeproposal is null");
    }
    try {
        TransactionContext transactionContext = getTransactionContext(upgradeProposalRequest.getUserContext());
        // transactionContext.verify(false);  // Install will have no signing cause it's not really targeted to a channel.
        transactionContext.setProposalWaitTime(upgradeProposalRequest.getProposalWaitTime());
        UpgradeProposalBuilder upgradeProposalBuilder = UpgradeProposalBuilder.newBuilder();
        upgradeProposalBuilder.context(transactionContext);
        upgradeProposalBuilder.argss(upgradeProposalRequest.getArgs());
        upgradeProposalBuilder.chaincodeName(upgradeProposalRequest.getChaincodeName());
        upgradeProposalBuilder.chaincodePath(upgradeProposalRequest.getChaincodePath());
        upgradeProposalBuilder.chaincodeVersion(upgradeProposalRequest.getChaincodeVersion());
        upgradeProposalBuilder.chaincodEndorsementPolicy(upgradeProposalRequest.getChaincodeEndorsementPolicy());
        SignedProposal signedProposal = getSignedProposal(transactionContext, upgradeProposalBuilder.build());
        return sendProposalToPeers(peers, signedProposal, transactionContext);
    } catch (Exception e) {
        throw new ProposalException(e);
    }
}
Also used : InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) TransactionContext(org.hyperledger.fabric.sdk.transaction.TransactionContext) UpgradeProposalBuilder(org.hyperledger.fabric.sdk.transaction.UpgradeProposalBuilder) SignedProposal(org.hyperledger.fabric.protos.peer.FabricProposal.SignedProposal) ProposalException(org.hyperledger.fabric.sdk.exception.ProposalException) 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 TransactionContext

use of org.hyperledger.fabric.sdk.transaction.TransactionContext in project fabric-sdk-java by hyperledger.

the class Channel method getConfigBlock.

private Block getConfigBlock(List<Peer> peers) throws ProposalException {
    if (shutdown) {
        throw new ProposalException(format("Channel %s has been shutdown.", name));
    }
    if (peers.isEmpty()) {
        throw new ProposalException("No peers go get config block");
    }
    TransactionContext transactionContext = null;
    SignedProposal signedProposal = null;
    try {
        transactionContext = getTransactionContext();
        // can't verify till we get the config block.
        transactionContext.verify(false);
        FabricProposal.Proposal proposal = GetConfigBlockBuilder.newBuilder().context(transactionContext).channelId(name).build();
        logger.debug("Getting signed proposal.");
        signedProposal = getSignedProposal(transactionContext, proposal);
        logger.debug("Got signed proposal.");
    } catch (Exception e) {
        throw new ProposalException(e);
    }
    ProposalException lastException = new ProposalException(format("getConfigBlock for channel %s failed.", name));
    for (Peer peer : peers) {
        try {
            Collection<ProposalResponse> resp = sendProposalToPeers(new ArrayList<>(Collections.singletonList(peer)), signedProposal, transactionContext);
            if (!resp.isEmpty()) {
                ProposalResponse pro = resp.iterator().next();
                if (pro.getStatus() == ProposalResponse.Status.SUCCESS) {
                    logger.trace(format("getConfigBlock from peer %s on channel %s success", peer.getName(), name));
                    return Block.parseFrom(pro.getProposalResponse().getResponse().getPayload().toByteArray());
                } else {
                    lastException = new ProposalException(format("getConfigBlock for channel %s failed with peer %s.  Status %s, details: %s", name, peer.getName(), pro.getStatus().toString(), pro.getMessage()));
                    logger.warn(lastException.getMessage());
                }
            } else {
                logger.warn(format("Got empty proposals from %s", peer));
            }
        } catch (Exception e) {
            lastException = new ProposalException(format("getConfigBlock for channel %s failed with peer %s.", name, peer.getName()), e);
            logger.warn(lastException.getMessage());
        }
    }
    throw lastException;
}
Also used : FabricProposal(org.hyperledger.fabric.protos.peer.FabricProposal) TransactionContext(org.hyperledger.fabric.sdk.transaction.TransactionContext) SignedProposal(org.hyperledger.fabric.protos.peer.FabricProposal.SignedProposal) ProposalException(org.hyperledger.fabric.sdk.exception.ProposalException) FabricProposalResponse(org.hyperledger.fabric.protos.peer.FabricProposalResponse) 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 8 with TransactionContext

use of org.hyperledger.fabric.sdk.transaction.TransactionContext in project fabric-sdk-java by hyperledger.

the class Channel method sendInstantiationProposal.

/**
 * Send instantiate request to the channel. Chaincode is created and initialized.
 *
 * @param instantiateProposalRequest
 * @param peers
 * @return responses from peers.
 * @throws InvalidArgumentException
 * @throws ProposalException
 */
public Collection<ProposalResponse> sendInstantiationProposal(InstantiateProposalRequest instantiateProposalRequest, Collection<Peer> peers) throws InvalidArgumentException, ProposalException {
    checkChannelState();
    if (null == instantiateProposalRequest) {
        throw new InvalidArgumentException("InstantiateProposalRequest is null");
    }
    instantiateProposalRequest.setSubmitted();
    checkPeers(peers);
    try {
        TransactionContext transactionContext = getTransactionContext(instantiateProposalRequest.getUserContext());
        transactionContext.setProposalWaitTime(instantiateProposalRequest.getProposalWaitTime());
        InstantiateProposalBuilder instantiateProposalbuilder = InstantiateProposalBuilder.newBuilder();
        instantiateProposalbuilder.context(transactionContext);
        instantiateProposalbuilder.argss(instantiateProposalRequest.getArgs());
        instantiateProposalbuilder.chaincodeName(instantiateProposalRequest.getChaincodeName());
        instantiateProposalbuilder.chaincodeType(instantiateProposalRequest.getChaincodeLanguage());
        instantiateProposalbuilder.chaincodePath(instantiateProposalRequest.getChaincodePath());
        instantiateProposalbuilder.chaincodeVersion(instantiateProposalRequest.getChaincodeVersion());
        instantiateProposalbuilder.chaincodEndorsementPolicy(instantiateProposalRequest.getChaincodeEndorsementPolicy());
        instantiateProposalbuilder.setTransientMap(instantiateProposalRequest.getTransientMap());
        FabricProposal.Proposal instantiateProposal = instantiateProposalbuilder.build();
        SignedProposal signedProposal = getSignedProposal(transactionContext, instantiateProposal);
        return sendProposalToPeers(peers, signedProposal, transactionContext);
    } catch (Exception e) {
        throw new ProposalException(e);
    }
}
Also used : FabricProposal(org.hyperledger.fabric.protos.peer.FabricProposal) InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) InstantiateProposalBuilder(org.hyperledger.fabric.sdk.transaction.InstantiateProposalBuilder) TransactionContext(org.hyperledger.fabric.sdk.transaction.TransactionContext) SignedProposal(org.hyperledger.fabric.protos.peer.FabricProposal.SignedProposal) ProposalException(org.hyperledger.fabric.sdk.exception.ProposalException) 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 9 with TransactionContext

use of org.hyperledger.fabric.sdk.transaction.TransactionContext in project fabric-sdk-java by hyperledger.

the class Peer method reconnectPeerEventServiceClient.

void reconnectPeerEventServiceClient(final PeerEventServiceClient failedPeerEventServiceClient, final Throwable throwable) {
    if (shutdown) {
        logger.debug("Not reconnecting PeerEventServiceClient shutdown ");
        return;
    }
    PeerEventingServiceDisconnected ldisconnectedHandler = disconnectedHandler;
    if (null == ldisconnectedHandler) {
        // just wont reconnect.
        return;
    }
    TransactionContext ltransactionContext = transactionContext;
    if (ltransactionContext == null) {
        logger.warn("Not reconnecting PeerEventServiceClient no transaction available ");
        return;
    }
    final TransactionContext fltransactionContext = ltransactionContext.retryTransactionSameContext();
    final ExecutorService executorService = getExecutorService();
    final PeerOptions peerOptions = null != failedPeerEventServiceClient.getPeerOptions() ? failedPeerEventServiceClient.getPeerOptions() : PeerOptions.createPeerOptions();
    if (executorService != null && !executorService.isShutdown() && !executorService.isTerminated()) {
        executorService.execute(() -> ldisconnectedHandler.disconnected(new PeerEventingServiceDisconnectEvent() {

            @Override
            public BlockEvent getLatestBLockReceived() {
                return lastBlockEvent;
            }

            @Override
            public long getLastConnectTime() {
                return lastConnectTime;
            }

            @Override
            public long getReconnectCount() {
                return reconnectCount;
            }

            @Override
            public Throwable getExceptionThrown() {
                return throwable;
            }

            @Override
            public void reconnect(Long startBLockNumber) throws TransactionException {
                logger.trace("reconnecting startBLockNumber" + startBLockNumber);
                ++reconnectCount;
                if (startBLockNumber == null) {
                    peerOptions.startEventsNewest();
                } else {
                    peerOptions.startEvents(startBLockNumber);
                }
                PeerEventServiceClient lpeerEventingClient = new PeerEventServiceClient(Peer.this, new Endpoint(url, properties), properties, peerOptions);
                lpeerEventingClient.connect(fltransactionContext);
                peerEventingClient = lpeerEventingClient;
            }
        }));
    }
}
Also used : TransactionContext(org.hyperledger.fabric.sdk.transaction.TransactionContext) ExecutorService(java.util.concurrent.ExecutorService) PeerOptions(org.hyperledger.fabric.sdk.Channel.PeerOptions)

Example 10 with TransactionContext

use of org.hyperledger.fabric.sdk.transaction.TransactionContext in project fabric-sdk-java by hyperledger.

the class ChannelTest method testProposalBuilderWithOutMetaInf.

@Test
public void testProposalBuilderWithOutMetaInf() throws Exception {
    InstallProposalBuilder installProposalBuilder = InstallProposalBuilder.newBuilder();
    installProposalBuilder.setChaincodeLanguage(TransactionRequest.Type.GO_LANG);
    installProposalBuilder.chaincodePath("github.com/example_cc");
    installProposalBuilder.setChaincodeSource(new File(SAMPLE_GO_CC));
    installProposalBuilder.chaincodeName("example_cc.go");
    installProposalBuilder.chaincodeVersion("1");
    Channel channel = hfclient.newChannel("testProposalBuilderWithOutMetaInf");
    TransactionContext transactionContext = new TransactionContext(channel, getMockUser("rick", "rickORG"), CryptoSuite.Factory.getCryptoSuite());
    installProposalBuilder.context(transactionContext);
    // Build it get the proposal. Then unpack it to see if it's what we expect.
    FabricProposal.Proposal proposal = installProposalBuilder.build();
    FabricProposal.ChaincodeProposalPayload chaincodeProposalPayload = FabricProposal.ChaincodeProposalPayload.parseFrom(proposal.getPayload());
    Chaincode.ChaincodeInvocationSpec chaincodeInvocationSpec = Chaincode.ChaincodeInvocationSpec.parseFrom(chaincodeProposalPayload.getInput());
    Chaincode.ChaincodeSpec chaincodeSpec = chaincodeInvocationSpec.getChaincodeSpec();
    Chaincode.ChaincodeInput input = chaincodeSpec.getInput();
    Chaincode.ChaincodeDeploymentSpec chaincodeDeploymentSpec = Chaincode.ChaincodeDeploymentSpec.parseFrom(input.getArgs(1));
    ByteString codePackage = chaincodeDeploymentSpec.getCodePackage();
    ArrayList tarBytesToEntryArrayList = tarBytesToEntryArrayList(codePackage.toByteArray());
    ArrayList<String> expect = new ArrayList(Arrays.asList(new String[] { "src/github.com/example_cc/example_cc.go" }));
    assertArrayListEquals("Tar in Install Proposal's codePackage does not have expected entries. ", expect, tarBytesToEntryArrayList);
}
Also used : InstallProposalBuilder(org.hyperledger.fabric.sdk.transaction.InstallProposalBuilder) ByteString(com.google.protobuf.ByteString) Chaincode(org.hyperledger.fabric.protos.peer.Chaincode) ArrayList(java.util.ArrayList) TestUtils.tarBytesToEntryArrayList(org.hyperledger.fabric.sdk.testutils.TestUtils.tarBytesToEntryArrayList) ByteString(com.google.protobuf.ByteString) FabricProposal(org.hyperledger.fabric.protos.peer.FabricProposal) TransactionContext(org.hyperledger.fabric.sdk.transaction.TransactionContext) File(java.io.File) Test(org.junit.Test)

Aggregations

TransactionContext (org.hyperledger.fabric.sdk.transaction.TransactionContext)19 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)13 StatusRuntimeException (io.grpc.StatusRuntimeException)13 IOException (java.io.IOException)13 ExecutionException (java.util.concurrent.ExecutionException)13 TimeoutException (java.util.concurrent.TimeoutException)13 CryptoException (org.hyperledger.fabric.sdk.exception.CryptoException)13 EventHubException (org.hyperledger.fabric.sdk.exception.EventHubException)13 InvalidArgumentException (org.hyperledger.fabric.sdk.exception.InvalidArgumentException)13 ProposalException (org.hyperledger.fabric.sdk.exception.ProposalException)13 TransactionEventException (org.hyperledger.fabric.sdk.exception.TransactionEventException)13 TransactionException (org.hyperledger.fabric.sdk.exception.TransactionException)13 FabricProposal (org.hyperledger.fabric.protos.peer.FabricProposal)10 SignedProposal (org.hyperledger.fabric.protos.peer.FabricProposal.SignedProposal)9 InstallProposalBuilder (org.hyperledger.fabric.sdk.transaction.InstallProposalBuilder)7 ByteString (com.google.protobuf.ByteString)6 File (java.io.File)5 FabricProposalResponse (org.hyperledger.fabric.protos.peer.FabricProposalResponse)5 Test (org.junit.Test)5 BroadcastResponse (org.hyperledger.fabric.protos.orderer.Ab.BroadcastResponse)4