Search in sources :

Example 11 with TransactionContext

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

the class ChannelTest method testProposalBuilderWithMetaInf.

@Test
public void testProposalBuilderWithMetaInf() 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.setChaincodeMetaInfLocation(new File("src/test/fixture/meta-infs/test1"));
    installProposalBuilder.chaincodeVersion("1");
    Channel channel = hfclient.newChannel("testProposalBuilderWithMetaInf");
    TestUtils.MockUser mockUser = getMockUser("rick", "rickORG");
    TransactionContext transactionContext = new TransactionContext(channel, mockUser, 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[] { "META-INF/statedb/couchdb/indexes/MockFakeIndex.json", "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) TestUtils(org.hyperledger.fabric.sdk.testutils.TestUtils) FabricProposal(org.hyperledger.fabric.protos.peer.FabricProposal) TransactionContext(org.hyperledger.fabric.sdk.transaction.TransactionContext) File(java.io.File) Test(org.junit.Test)

Example 12 with TransactionContext

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

the class ChannelTest method testProposalBuilderWithMetaInfEmpty.

@Test
public void testProposalBuilderWithMetaInfEmpty() throws Exception {
    thrown.expect(java.lang.IllegalArgumentException.class);
    thrown.expectMessage(matchesRegex("The META-INF directory.*src.test.fixture.meta-infs.emptyMetaInf.META-INF is empty\\."));
    // make it cause git won't check in empty directory
    File emptyINF = new File("src/test/fixture/meta-infs/emptyMetaInf/META-INF");
    if (!emptyINF.exists()) {
        emptyINF.mkdirs();
        emptyINF.deleteOnExit();
    }
    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");
    // points into which is not what's expected.
    installProposalBuilder.setChaincodeMetaInfLocation(new File("src/test/fixture/meta-infs/emptyMetaInf"));
    Channel channel = hfclient.newChannel("testProposalBuilderWithMetaInfEmpty");
    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 epect.
    FabricProposal.Proposal proposal = installProposalBuilder.build();
}
Also used : FabricProposal(org.hyperledger.fabric.protos.peer.FabricProposal) InstallProposalBuilder(org.hyperledger.fabric.sdk.transaction.InstallProposalBuilder) TransactionContext(org.hyperledger.fabric.sdk.transaction.TransactionContext) File(java.io.File) Test(org.junit.Test)

Example 13 with TransactionContext

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

the class ChannelTest method testProposalBuilderWithMetaInfExistsNOT.

@Test
public void testProposalBuilderWithMetaInfExistsNOT() throws Exception {
    thrown.expect(java.lang.IllegalArgumentException.class);
    thrown.expectMessage(matchesRegex("Directory to find chaincode META-INF.*tmp.fdsjfksfj.fjksfjskd.fjskfjdsk.should never exist does not exist"));
    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");
    // points into which is not what's expected.
    installProposalBuilder.setChaincodeMetaInfLocation(new File("/tmp/fdsjfksfj/fjksfjskd/fjskfjdsk/should never exist"));
    Channel channel = hfclient.newChannel("testProposalBuilderWithMetaInfExistsNOT");
    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 epect.
    installProposalBuilder.build();
}
Also used : InstallProposalBuilder(org.hyperledger.fabric.sdk.transaction.InstallProposalBuilder) TransactionContext(org.hyperledger.fabric.sdk.transaction.TransactionContext) File(java.io.File) Test(org.junit.Test)

Example 14 with TransactionContext

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

the class ChannelTest method testProposalBuilderWithNoMetaInfDir.

@Test
public void testProposalBuilderWithNoMetaInfDir() throws Exception {
    thrown.expect(java.lang.IllegalArgumentException.class);
    thrown.expectMessage(matchesRegex("The META-INF directory does not exist in.*src.test.fixture.meta-infs.test1.META-INF"));
    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");
    // points into which is not what's expected.
    installProposalBuilder.setChaincodeMetaInfLocation(new File("src/test/fixture/meta-infs/test1/META-INF"));
    Channel channel = hfclient.newChannel("testProposalBuilderWithNoMetaInfDir");
    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 epect.
    installProposalBuilder.build();
}
Also used : InstallProposalBuilder(org.hyperledger.fabric.sdk.transaction.InstallProposalBuilder) TransactionContext(org.hyperledger.fabric.sdk.transaction.TransactionContext) File(java.io.File) Test(org.junit.Test)

Example 15 with TransactionContext

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

the class Channel method seekBlock.

private int seekBlock(SeekInfo seekInfo, List<DeliverResponse> deliverResponses, Orderer ordererIn) throws TransactionException {
    logger.trace(format("seekBlock for channel %s", name));
    final long start = System.currentTimeMillis();
    @SuppressWarnings("UnusedAssignment") int statusRC = 404;
    try {
        do {
            statusRC = 404;
            final Orderer orderer = ordererIn != null ? ordererIn : getRandomOrderer();
            TransactionContext txContext = getTransactionContext();
            DeliverResponse[] deliver = orderer.sendDeliver(createSeekInfoEnvelope(txContext, seekInfo, orderer.getClientTLSCertificateDigest()));
            if (deliver.length < 1) {
                logger.warn(format("Genesis block for channel %s fetch bad deliver missing status block only got blocks:%d", name, deliver.length));
                // odd so lets try again....
                statusRC = 404;
            } else {
                DeliverResponse status = deliver[0];
                statusRC = status.getStatusValue();
                if (statusRC == 404 || statusRC == 503) {
                    // 404 - block not found.  503 - service not available usually means kafka is not ready but starting.
                    logger.warn(format("Bad deliver expected status 200  got  %d, Channel %s", status.getStatusValue(), name));
                    // keep trying... else
                    statusRC = 404;
                } else if (statusRC != 200) {
                    // Assume for anything other than 200 we have a non retryable situation
                    throw new TransactionException(format("Bad newest block expected status 200  got  %d, Channel %s", status.getStatusValue(), name));
                } else {
                    if (deliver.length < 2) {
                        throw new TransactionException(format("Newest block for channel %s fetch bad deliver missing genesis block only got %d:", name, deliver.length));
                    } else {
                        deliverResponses.addAll(Arrays.asList(deliver));
                    }
                }
            }
            if (200 != statusRC) {
                long duration = System.currentTimeMillis() - start;
                if (duration > config.getGenesisBlockWaitTime()) {
                    throw new TransactionException(format("Getting block time exceeded %s seconds for channel %s", Long.toString(TimeUnit.MILLISECONDS.toSeconds(duration)), name));
                }
                try {
                    // try again
                    Thread.sleep(ORDERER_RETRY_WAIT_TIME);
                } catch (InterruptedException e) {
                    TransactionException te = new TransactionException("seekBlock thread Sleep", e);
                    logger.warn(te.getMessage(), te);
                }
            }
        } while (statusRC != 200);
    } catch (TransactionException e) {
        logger.error(e.getMessage(), e);
        throw e;
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new TransactionException(e);
    }
    return statusRC;
}
Also used : TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) TransactionContext(org.hyperledger.fabric.sdk.transaction.TransactionContext) DeliverResponse(org.hyperledger.fabric.protos.orderer.Ab.DeliverResponse) 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)

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