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

The following examples show how to use org.hyperledger.fabric.sdk.Channel#getPeers() . 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 ListPage<TbNode> queryNodeList(FabricConfig fabricConfig,
                                             Channel channel,
                                             Integer pageIndex,
                                             Integer pageSize) throws ProposalException, InvalidArgumentException {
    ListPage<TbNode> tbNodeListPage = new ListPage<>();
    List<TbNode> tbNodes = new ArrayList<>();
    BlockInfo blockInfo = getBlockInfo(fabricConfig, channel, null);

    Collection<Peer> peers = channel.getPeers();
    peers.forEach(peer -> {
        TbNode tbNode = new TbNode();
        tbNode.setBlockNumber(BigInteger.valueOf(blockInfo.getBlockNumber()));
        tbNode.setNodeId(peer.getUrl());
        tbNode.setNodeActive(1);
        tbNode.setNodeType(WeEventConstants.NODE_TYPE_SEALER);
        tbNodes.add(tbNode);
    });

    tbNodeListPage.setPageIndex(pageIndex);
    tbNodeListPage.setPageSize(pageSize);
    tbNodeListPage.setTotal(peers.size());
    tbNodeListPage.setPageData(tbNodes);
    return tbNodeListPage;
}
 
Example 2
Source File: CreateChannel.java    From blockchain-application-using-fabric-java-sdk with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
	try {
		CryptoSuite.Factory.getCryptoSuite();
		Util.cleanUp();
		// Construct Channel
		UserContext org1Admin = new UserContext();
		File pkFolder1 = new File(Config.ORG1_USR_ADMIN_PK);
		File[] pkFiles1 = pkFolder1.listFiles();
		File certFolder1 = new File(Config.ORG1_USR_ADMIN_CERT);
		File[] certFiles1 = certFolder1.listFiles();
		Enrollment enrollOrg1Admin = Util.getEnrollment(Config.ORG1_USR_ADMIN_PK, pkFiles1[0].getName(),
				Config.ORG1_USR_ADMIN_CERT, certFiles1[0].getName());
		org1Admin.setEnrollment(enrollOrg1Admin);
		org1Admin.setMspId(Config.ORG1_MSP);
		org1Admin.setName(Config.ADMIN);

		UserContext org2Admin = new UserContext();
		File pkFolder2 = new File(Config.ORG2_USR_ADMIN_PK);
		File[] pkFiles2 = pkFolder2.listFiles();
		File certFolder2 = new File(Config.ORG2_USR_ADMIN_CERT);
		File[] certFiles2 = certFolder2.listFiles();
		Enrollment enrollOrg2Admin = Util.getEnrollment(Config.ORG2_USR_ADMIN_PK, pkFiles2[0].getName(),
				Config.ORG2_USR_ADMIN_CERT, certFiles2[0].getName());
		org2Admin.setEnrollment(enrollOrg2Admin);
		org2Admin.setMspId(Config.ORG2_MSP);
		org2Admin.setName(Config.ADMIN);

		FabricClient fabClient = new FabricClient(org1Admin);

		// Create a new channel
		Orderer orderer = fabClient.getInstance().newOrderer(Config.ORDERER_NAME, Config.ORDERER_URL);
		ChannelConfiguration channelConfiguration = new ChannelConfiguration(new File(Config.CHANNEL_CONFIG_PATH));

		byte[] channelConfigurationSignatures = fabClient.getInstance()
				.getChannelConfigurationSignature(channelConfiguration, org1Admin);

		Channel mychannel = fabClient.getInstance().newChannel(Config.CHANNEL_NAME, orderer, channelConfiguration,
				channelConfigurationSignatures);

		Peer peer0_org1 = fabClient.getInstance().newPeer(Config.ORG1_PEER_0, Config.ORG1_PEER_0_URL);
		Peer peer1_org1 = fabClient.getInstance().newPeer(Config.ORG1_PEER_1, Config.ORG1_PEER_1_URL);
		Peer peer0_org2 = fabClient.getInstance().newPeer(Config.ORG2_PEER_0, Config.ORG2_PEER_0_URL);
		Peer peer1_org2 = fabClient.getInstance().newPeer(Config.ORG2_PEER_1, Config.ORG2_PEER_1_URL);

		mychannel.joinPeer(peer0_org1);
		mychannel.joinPeer(peer1_org1);
		
		mychannel.addOrderer(orderer);

		mychannel.initialize();
		
		fabClient.getInstance().setUserContext(org2Admin);
		mychannel = fabClient.getInstance().getChannel("mychannel");
		mychannel.joinPeer(peer0_org2);
		mychannel.joinPeer(peer1_org2);
		
		Logger.getLogger(CreateChannel.class.getName()).log(Level.INFO, "Channel created "+mychannel.getName());
           Collection peers = mychannel.getPeers();
           Iterator peerIter = peers.iterator();
           while (peerIter.hasNext())
           {
           	  Peer pr = (Peer) peerIter.next();
           	  Logger.getLogger(CreateChannel.class.getName()).log(Level.INFO,pr.getName()+ " at " + pr.getUrl());
           }
           
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 3
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";
	}

}