Java Code Examples for org.hyperledger.fabric.sdk.Channel#getName()

The following examples show how to use org.hyperledger.fabric.sdk.Channel#getName() . 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: ChaincodeServiceImpl.java    From balance-transfer-java with Apache License 2.0 5 votes vote down vote up
/**
 * gives blockchain info
 * 
 */
public void blockchainInfo(Org sampleOrg, Channel channel) {

	try {
		checkConfig();

		String channelName = channel.getName();
		Set<Peer> peerSet = sampleOrg.getPeers();
		// Peer queryPeer = peerSet.iterator().next();
		// out("Using peer %s for channel queries", queryPeer.getName());

		BlockchainInfo channelInfo = channel.queryBlockchainInfo();
		logger.info("Channel info for : " + channelName);
		logger.info("Channel height: " + channelInfo.getHeight());

		String chainCurrentHash = Hex.encodeHexString(channelInfo.getCurrentBlockHash());
		String chainPreviousHash = Hex.encodeHexString(channelInfo.getPreviousBlockHash());
		logger.info("Chain current block hash: " + chainCurrentHash);
		logger.info("Chainl previous block hash: " + chainPreviousHash);

		// Query by block number. Should return latest block, i.e. block
		// number 2
		BlockInfo returnedBlock = channel.queryBlockByNumber(channelInfo.getHeight() - 1);
		String previousHash = Hex.encodeHexString(returnedBlock.getPreviousHash());
		logger.info("queryBlockByNumber returned correct block with blockNumber " + returnedBlock.getBlockNumber()
		+ " \n previous_hash " + previousHash);

		// Query by block hash. Using latest block's previous hash so should
		// return block number 1
		byte[] hashQuery = returnedBlock.getPreviousHash();
		returnedBlock = channel.queryBlockByHash(hashQuery);
		logger.info("queryBlockByHash returned block with blockNumber " + returnedBlock.getBlockNumber());

	} catch (Exception e) {
		logger.error("ChaincodeServiceImpl | blockchainInfo | "+ e.getMessage());
	}
}
 
Example 2
Source File: ChaincodeServiceImpl.java    From balance-transfer-java with Apache License 2.0 4 votes vote down vote up
/**
 * installs the chaincode takes as input chaincode name returns status as
 * string
 */
public String installChaincode(String chaincodeName) {

	try {
		checkConfig();

		chaincodeID = getChaincodeId(chaincodeName);
		Org sampleOrg = Conf.getSampleOrg("peerOrg1");
		Channel channel = reconstructChannel();
		final String channelName = channel.getName();
		boolean isFooChain = channelName.equals(channelName);
		logger.info("Running channel %s", channelName);
		Collection<Peer> channelPeers = channel.getPeers();
		Collection<Orderer> orderers = channel.getOrderers();

		client.setUserContext(sampleOrg.getPeerAdmin());
		logger.info("Creating install proposal");
		InstallProposalRequest installProposalRequest = client.newInstallProposalRequest();
		installProposalRequest.setChaincodeID(chaincodeID);
		installProposalRequest.setChaincodeSourceLocation(new File(PATH + "/artifacts/"));
		installProposalRequest.setChaincodeVersion(chainCodeVersion);
		logger.info("Sending install proposal");
		int numInstallProposal = 0;

		Set<Peer> peersFromOrg = sampleOrg.getPeers();
		numInstallProposal = numInstallProposal + peersFromOrg.size();
		responses = client.sendInstallProposal(installProposalRequest, peersFromOrg);
		for (ProposalResponse response : responses) {
			if (response.getStatus() == ProposalResponse.Status.SUCCESS) {
				out("Successful install proposal response Txid: %s from peer %s", response.getTransactionID(),
						response.getPeer().getName());
				successful.add(response);
			} else {
				failed.add(response);
			}
		}
		SDKUtils.getProposalConsistencySets(responses);
		// }
		logger.info("Received %d install proposal responses. Successful+verified: %d . Failed: %d",
				numInstallProposal, successful.size(), failed.size());

		if (failed.size() > 0) {
			ProposalResponse first = failed.iterator().next();
			fail("Not enough endorsers for install :" + successful.size() + ".  " + first.getMessage());
			return "Not enough endorsers for install :" + successful.size() + ".  " + first.getMessage();
		}

		return "Chaincode installed successfully";

	} catch (Exception e) {
		logger.error("ChaincodeServiceImpl | installChaincode | " +e.getMessage());
		return "Chaincode installation failed";
	}

}
 
Example 3
Source File: NetworkConfigIT.java    From fabric-sdk-java with Apache License 2.0 4 votes vote down vote up
@Test
public void testUpdate1() throws Exception {

    // Setup client and channel instances
    HFClient client = getTheClient();
    Channel channel = constructChannel(client, FOO_CHANNEL_NAME);

    final ChaincodeID chaincodeID = ChaincodeID.newBuilder().setName(CHAIN_CODE_NAME)
            .setVersion(CHAIN_CODE_VERSION)
            .setPath(CHAIN_CODE_PATH).build();

    final String channelName = channel.getName();

    out("Running testUpdate1 - Channel %s", channelName);

    final Collection<String> peersOrganizationMSPIDs = channel.getPeersOrganizationMSPIDs();
    assertNotNull(peersOrganizationMSPIDs);
    assertEquals(1, peersOrganizationMSPIDs.size());
    assertEquals("Org1MSP", peersOrganizationMSPIDs.iterator().next());

    int moveAmount = 5;
    String originalVal = queryChaincodeForCurrentValue(client, channel, chaincodeID);
    String newVal = "" + (Integer.parseInt(originalVal) + moveAmount);

    out("Original value = %s", originalVal);

    //user registered user
    client.setUserContext(orgRegisteredUsers.get("Org1")); // only using org1

    // Move some assets
    moveAmount(client, channel, chaincodeID, "a", "b", "" + moveAmount, null).thenApply(transactionEvent -> {
        // Check that they were moved
        queryChaincodeForExpectedValue(client, channel, newVal, chaincodeID);
        return null;

    }).thenApply(transactionEvent -> {
        // Move them back
        try {
            return moveAmount(client, channel, chaincodeID, "b", "a", "" + moveAmount, null).get(testConfig.getTransactionWaitTime(), TimeUnit.SECONDS);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }).thenApply(transactionEvent -> {
        // Check that they were moved back
        queryChaincodeForExpectedValue(client, channel, originalVal, chaincodeID);
        return null;

    }).exceptionally(e -> {
        if (e instanceof CompletionException && e.getCause() != null) {
            e = e.getCause();
        }
        if (e instanceof TransactionEventException) {
            BlockEvent.TransactionEvent te = ((TransactionEventException) e).getTransactionEvent();
            if (te != null) {

                e.printStackTrace(System.err);
                fail(format("Transaction with txid %s failed. %s", te.getTransactionID(), e.getMessage()));
            }
        }

        e.printStackTrace(System.err);
        fail(format("Test failed with %s exception %s", e.getClass().getName(), e.getMessage()));

        return null;

    }).get(testConfig.getTransactionWaitTime(), TimeUnit.SECONDS);

    channel.shutdown(true); // Force channel to shutdown clean up resources.

    out("testUpdate1 - done");
    out("That's all folks!");
}