package blockchain.client; import blockchain.configuration.Config; import org.apache.log4j.BasicConfigurator; import org.apache.log4j.Logger; import org.hyperledger.fabric.sdk.*; import org.hyperledger.fabric.sdk.exception.InvalidArgumentException; import org.hyperledger.fabric.sdk.exception.ProposalException; import org.hyperledger.fabric.sdk.exception.TransactionException; import java.util.Collection; import java.util.List; import java.util.stream.Collectors; public class ChannelClientWrapper { private String name; static private Channel channel; private FabricClientWrapper fabricClientWrapper; public String getName(){ return name; } public Channel getChannel(){ return channel; } public FabricClientWrapper getFabricClientWrapper(){ return fabricClientWrapper; } public ChannelClientWrapper(String name, Channel channel, FabricClientWrapper fabricClientWrapper){ this.name=name; this.channel=channel; this.fabricClientWrapper=fabricClientWrapper; } public Collection queryByChainCode(String chaincodeName,String fuctionName, String[] args) throws InvalidArgumentException, ProposalException { QueryByChaincodeRequest request = fabricClientWrapper.getClient().newQueryProposalRequest(); ChaincodeID chaincodeID = ChaincodeID.newBuilder().setName(chaincodeName).build(); request.setChaincodeID(chaincodeID); request.setFcn(fuctionName); if(args != null){ request.setArgs(args); } Collection response = channel.queryByChaincode(request); return response; } //Method to init the channel for query static public ChannelClientWrapper setupChannel(FabricClientWrapper fabricClientWrapper) throws InvalidArgumentException, TransactionException { ChannelClientWrapper channelClientWrapper = fabricClientWrapper.createChannelClient(Config.CHANNEL_NAME); channel = channelClientWrapper.getChannel(); Peer peer = fabricClientWrapper.getClient().newPeer(Config.ORG1_PEER_0,Config.ORG1_PEER_0_URL); //EventHub eventHub = fabricClientWrapper.getClient().newEventHub("eventhub01", "grpc://vps577432.ovh.net:8053"); EventHub eventHub = fabricClientWrapper.getClient().newEventHub("eventhub01", "grpc://93.30.148.59:7053"); Orderer orderer = fabricClientWrapper.getClient().newOrderer(Config.ORDERER_NAME,Config.ORDERER_URL); channel.addPeer(peer); channel.addEventHub(eventHub); channel.addOrderer(orderer); channel.initialize(); return channelClientWrapper; } //Wrapper for channel.sendTransactionProposal public Collection sendTransactionProposal (TransactionProposalRequest transactionProposalRequest){ try{ return channel.sendTransactionProposal(transactionProposalRequest); }catch (Exception e){ e.printStackTrace(); return null; } } //Wrapper for channel.sendTransaction public BlockEvent.TransactionEvent sendTransaction(Collection proposalResponses){ Logger logger = Logger.getLogger(ChannelClientWrapper.class); BasicConfigurator.configure(); try{ List invalid = proposalResponses.stream().filter(r -> r.isInvalid()).collect(Collectors.toList()); if (!invalid.isEmpty()) { invalid.forEach(response -> { logger.error(response.getMessage()); }); } return channel.sendTransaction(proposalResponses).get(); }catch (Exception e){ e.printStackTrace(); return null; } } }