org.hyperledger.fabric.sdk.TransactionRequest Java Examples

The following examples show how to use org.hyperledger.fabric.sdk.TransactionRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FabricSDKWrapper.java    From WeEvent with Apache License 2.0 6 votes vote down vote up
public static Collection<ProposalResponse> instantiateProposal(HFClient client, Channel channel, ChaincodeID chaincodeID,
                                                               TransactionRequest.Type chaincodeLang, Long proposalTimeout) throws InvalidArgumentException, ProposalException {
    InstantiateProposalRequest instantiateProposalRequest = client.newInstantiationProposalRequest();
    instantiateProposalRequest.setProposalWaitTime(proposalTimeout);//time in milliseconds
    instantiateProposalRequest.setChaincodeID(chaincodeID);
    instantiateProposalRequest.setChaincodeLanguage(chaincodeLang);
    instantiateProposalRequest.setFcn("init");
    instantiateProposalRequest.setArgs(new String[]{});

    // I do not know the purpose of transient map works for.
    Map<String, byte[]> transientMap = new HashMap<>();
    transientMap.put("HyperLedgerFabric", "InstantiateProposalRequest:JavaSDK".getBytes(UTF_8));
    transientMap.put("method", "InstantiateProposalRequest".getBytes(UTF_8));
    instantiateProposalRequest.setTransientMap(transientMap);
    return channel.sendInstantiationProposal(instantiateProposalRequest, channel.getPeers());
}
 
Example #2
Source File: InstallProposalBuilderTest.java    From fabric-sdk-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testBuildInvalidSource() throws Exception {

    // A mock InputStream that throws an IOException
    class MockInputStream extends InputStream {
        @Override
        public int read() throws IOException {
            throw new IOException("Cannot read!");
        }
    }

    thrown.expect(ProposalException.class);
    thrown.expectMessage("IO Error");

    InstallProposalBuilder builder = createTestBuilder();

    builder.setChaincodeLanguage(TransactionRequest.Type.JAVA);
    builder.setChaincodeInputStream(new MockInputStream());

    builder.build();
}
 
Example #3
Source File: FabricSDKWrapper.java    From WeEvent with Apache License 2.0 5 votes vote down vote up
public static Collection<ProposalResponse> installProposal(HFClient client, Channel channel, ChaincodeID chaincodeID,
                                                           TransactionRequest.Type chaincodeLang,         // Type.GO_LANG
                                                           String chaincodeVer,        // "v1"
                                                           String chaincodeSourceLoc,  // "/opt/gopath"
                                                           String chaincodePath        // "github.com/hyperledger/fabric/peer/chaincode/go/chaincode_example02
) throws InvalidArgumentException, ProposalException {
    InstallProposalRequest installProposalRequest = client.newInstallProposalRequest();
    installProposalRequest.setChaincodeID(chaincodeID);
    installProposalRequest.setChaincodeVersion(chaincodeVer);
    installProposalRequest.setChaincodeLanguage(chaincodeLang);
    installProposalRequest.setChaincodeSourceLocation(new File(chaincodeSourceLoc));
    installProposalRequest.setChaincodePath(chaincodePath);
    return client.sendInstallProposal(installProposalRequest, channel.getPeers());
}
 
Example #4
Source File: InstallProposalBuilderTest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuildChaincodePathGolangFile() throws Exception {

    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("Missing chaincodePath");

    InstallProposalBuilder builder = createTestBuilder();

    builder.setChaincodeLanguage(TransactionRequest.Type.GO_LANG);
    builder.setChaincodeSource(new File("some/dir"));
    builder.chaincodePath(null);

    builder.build();
}
 
Example #5
Source File: InstallProposalBuilderTest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuildChaincodePathGolangStream() throws Exception {

    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("Missing chaincodePath");

    InstallProposalBuilder builder = createTestBuilder();

    builder.setChaincodeLanguage(TransactionRequest.Type.GO_LANG);
    builder.setChaincodeInputStream(new ByteArrayInputStream("test string".getBytes()));
    builder.chaincodePath(null);

    builder.build();
}
 
Example #6
Source File: InstallProposalBuilderTest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuildChaincodePathJavaFile() throws Exception {

    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("chaincodePath must be null for Java chaincode");

    InstallProposalBuilder builder = createTestBuilder();

    builder.setChaincodeLanguage(TransactionRequest.Type.JAVA);
    builder.setChaincodeSource(new File("some/dir"));
    builder.chaincodePath("null or empty string");

    builder.build();
}
 
Example #7
Source File: InstallProposalBuilderTest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuildChaincodePathJavaStream() throws Exception {

    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("chaincodePath must be null for Java chaincode");

    InstallProposalBuilder builder = createTestBuilder();

    builder.setChaincodeLanguage(TransactionRequest.Type.JAVA);
    builder.setChaincodeInputStream(new ByteArrayInputStream("test string".getBytes()));
    builder.chaincodePath("null or empty string");

    builder.build();
}
 
Example #8
Source File: InstallProposalBuilderTest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuildSourceNotExistGolang() throws Exception {

    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("The project source directory does not exist");

    InstallProposalBuilder builder = createTestBuilder();

    builder.setChaincodeLanguage(TransactionRequest.Type.JAVA);
    builder.chaincodePath(null);
    builder.setChaincodeSource(new File("some/dir"));

    builder.build();
}
 
Example #9
Source File: InstallProposalBuilderTest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuildChaincodePathNodeFile() throws Exception {

    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("chaincodePath must be null for Node chaincode");

    InstallProposalBuilder builder = createTestBuilder();

    builder.setChaincodeLanguage(TransactionRequest.Type.NODE);
    builder.setChaincodeSource(new File("some/dir"));
    builder.chaincodePath("src");

    builder.build();
}
 
Example #10
Source File: InstallProposalBuilderTest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuildChaincodePathNodeStream() throws Exception {

    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("chaincodePath must be null for Node chaincode");

    InstallProposalBuilder builder = createTestBuilder();

    builder.setChaincodeLanguage(TransactionRequest.Type.NODE);
    builder.setChaincodeInputStream(new ByteArrayInputStream("test string".getBytes()));
    builder.chaincodePath("src");

    builder.build();
}
 
Example #11
Source File: FabricSDKWrapper.java    From WeEvent with Apache License 2.0 4 votes vote down vote up
public static TransactionInfo executeTransaction(HFClient client, Channel channel, ChaincodeID chaincodeID, boolean invoke, String func,
                                                 Long transactionTimeout, String... args) throws InvalidArgumentException, ProposalException, InterruptedException, ExecutionException, TimeoutException {
    TransactionProposalRequest transactionProposalRequest = client.newTransactionProposalRequest();
    transactionProposalRequest.setChaincodeID(chaincodeID);
    transactionProposalRequest.setChaincodeLanguage(TransactionRequest.Type.GO_LANG);

    transactionProposalRequest.setFcn(func);
    transactionProposalRequest.setArgs(args);
    transactionProposalRequest.setProposalWaitTime(120000);

    List<ProposalResponse> successful = new LinkedList<>();
    List<ProposalResponse> failed = new LinkedList<>();
    // there is no need to retry. If not, you should re-send the transaction proposal.
    Collection<ProposalResponse> transactionPropResp = channel.sendTransactionProposal(transactionProposalRequest);
    TransactionInfo transactionInfo = new TransactionInfo();
    boolean result = true;
    for (ProposalResponse response : transactionPropResp) {
        if (response.getStatus() == ProposalResponse.Status.SUCCESS) {
            transactionInfo.setCode(ErrorCode.SUCCESS.getCode());
            transactionInfo.setPayLoad(new String(response.getChaincodeActionResponsePayload()));
            log.info("[√] Got success response from peer:{} , payload:{}", response.getPeer().getName(), transactionInfo.getPayLoad());
            successful.add(response);
        } else {
            result = false;
            transactionInfo.setCode(ErrorCode.FABRICSDK_CHAINCODE_INVOKE_FAILED.getCode());
            transactionInfo.setMessage(response.getMessage());
            String status = response.getStatus().toString();
            log.error("[×] Got failed response from peer:{}, status:{}, error message:{}", response.getPeer().getName(), status, transactionInfo.getMessage());
            failed.add(response);
        }
    }

    if (invoke && result) {
        log.info("Sending transaction to orderers...");
        CompletableFuture<BlockEvent.TransactionEvent> carfuture = channel.sendTransaction(successful);
        BlockEvent.TransactionEvent transactionEvent = carfuture.get(transactionTimeout, TimeUnit.MILLISECONDS);
        transactionInfo.setBlockNumber(transactionEvent.getBlockEvent().getBlockNumber());
        log.info("Wait event return: " + transactionEvent.getChannelId() + " " + transactionEvent.getTransactionID() + " " + transactionEvent.getType() + " " + transactionEvent.getValidationCode());
    }
    return transactionInfo;
}
 
Example #12
Source File: FabricSDKWrapper.java    From WeEvent with Apache License 2.0 4 votes vote down vote up
public static CompletableFuture<TransactionInfo> executeTransactionAsync(HFClient client, Channel channel, ChaincodeID chaincodeID, boolean invoke, String func,
                                                                         String... args) throws InvalidArgumentException, ProposalException {
    TransactionProposalRequest transactionProposalRequest = client.newTransactionProposalRequest();
    transactionProposalRequest.setChaincodeID(chaincodeID);
    transactionProposalRequest.setChaincodeLanguage(TransactionRequest.Type.GO_LANG);

    transactionProposalRequest.setFcn(func);
    transactionProposalRequest.setArgs(args);
    transactionProposalRequest.setProposalWaitTime(120000);

    List<ProposalResponse> successful = new LinkedList<>();
    List<ProposalResponse> failed = new LinkedList<>();
    // there is no need to retry. If not, you should re-send the transaction proposal.
    Collection<ProposalResponse> transactionPropResp = channel.sendTransactionProposal(transactionProposalRequest);
    TransactionInfo transactionInfo = new TransactionInfo();
    boolean result = true;
    for (ProposalResponse response : transactionPropResp) {
        if (response.getStatus() == ProposalResponse.Status.SUCCESS) {
            transactionInfo.setCode(ErrorCode.SUCCESS.getCode());
            transactionInfo.setPayLoad(new String(response.getChaincodeActionResponsePayload()));
            log.info("[√] Got success response from peer:{} , payload:{}", response.getPeer().getName(), transactionInfo.getPayLoad());
            successful.add(response);
        } else {
            result = false;
            transactionInfo.setCode(ErrorCode.FABRICSDK_CHAINCODE_INVOKE_FAILED.getCode());
            transactionInfo.setMessage(response.getMessage());
            String status = response.getStatus().toString();
            log.error("[×] Got failed response from peer:{}, status:{}, error message:{}", response.getPeer().getName(), status, transactionInfo.getMessage());
            failed.add(response);
        }
    }

    if (invoke && result) {
        log.info("Sending transaction to orders...");
        return channel.sendTransaction(successful).thenApply(
                (transactionEvent) -> {
                    log.info("Wait event return: {} {} {} {}",
                            transactionEvent.getChannelId(),
                            transactionEvent.getTransactionID(),
                            transactionEvent.getType(),
                            transactionEvent.getValidationCode());
                    transactionInfo.setBlockNumber(transactionEvent.getBlockEvent().getBlockNumber());
                    return transactionInfo;
                });
    }

    CompletableFuture<TransactionInfo> completableFuture = new CompletableFuture<>();
    completableFuture.complete(transactionInfo);
    return completableFuture;
}
 
Example #13
Source File: FabricTransaction.java    From spring-fabric-gateway with MIT License 4 votes vote down vote up
private void configureRequest(TransactionRequest request, String[] args) {
	request.setChaincodeID(getChaincodeId());
	request.setFcn(getName());
	request.setArgs(args);
	request.setProposalWaitTime(getProposalTimeout() * 1000);
}
 
Example #14
Source File: LifecycleInstallProposalBuilder.java    From fabric-sdk-java with Apache License 2.0 4 votes vote down vote up
public void setChaincodeLanguage(TransactionRequest.Type chaincodeLanguage) {
    this.chaincodeLanguage = chaincodeLanguage;
}
 
Example #15
Source File: InstallProposalBuilder.java    From fabric-sdk-java with Apache License 2.0 4 votes vote down vote up
public void setChaincodeLanguage(TransactionRequest.Type chaincodeLanguage) {
    this.chaincodeLanguage = chaincodeLanguage;
}
 
Example #16
Source File: InstallProposalBuilderTest.java    From fabric-sdk-java with Apache License 2.0 3 votes vote down vote up
private InstallProposalBuilder createTestBuilder() {

        InstallProposalBuilder builder = InstallProposalBuilder.newBuilder();

        builder.chaincodeName("mycc");
        builder.chaincodeVersion("1.0");
        builder.setChaincodeLanguage(TransactionRequest.Type.GO_LANG);

        return builder;
    }