1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| public class FabricTest {
private static final String BASE_PATH = "./fabric"; private static final String CHANNEL_NAME = "CHANNEL_NAME"; private static final String CHAINCODE_ID = "CHAINCODE_ID"; private static final Map<String, String> ORDERER_MAP = new HashMap<>(); private static final Map<String, String> PEER_MAP = new HashMap<>(); private static final boolean TLS = true;
static String protocol = "grpc"; if(TLS) { protocol += "s"; } ORDERER_MAP.put("test.com", protocol + "://127.0.0.1:7050"); PEER_MAP.put("peer.test.com", protocol + "://127.0.0.1:7051"); }
public static void main(String[] args) throws Exception { String keyFile = BASE_PATH + "***/user-key.pem"; String certFile = BASE_PATH + "***/user-cert.pem"; LocalUser user = new LocalUser("username", "mspId", keyFile, certFile);
HFClient client = HFClient.createNewInstance(); client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite()); client.setUserContext(user);
Channel channel = client.newChannel(CHANNEL_NAME); for (Map.Entry<String, String> entry : ORDERER_MAP.entrySet()) { Properties properties = new Properties(); if(TLS) { properties.setProperty("clientCertFile", "对应配置目录下users/***/tls/client.crt文件"); properties.setProperty("clientKeyFile", "对应配置目录下users/***/tls/client.key文件"); properties.setProperty("pemFile", "对应配置目录下/tls/server.crt文件"); properties.setProperty("hostnameOverride", entry.getKey()); properties.setProperty("sslProvider", "openSSL"); properties.setProperty("negotiationType", "TLS"); } channel.addOrderer(client.newOrderer(entry.getKey(), entry.getValue(), properties)); } for (Map.Entry<String, String> entry : PEER_MAP.entrySet()) { Properties properties = new Properties(); if(TLS) { properties.setProperty("clientCertFile", "对应配置目录下users/***/tls/client.crt文件"); properties.setProperty("clientKeyFile", "对应配置目录下users/***/tls/client.key文件"); properties.setProperty("pemFile", "对应配置目录下/tls/server.crt文件"); properties.setProperty("hostnameOverride", entry.getKey()); properties.setProperty("sslProvider", "openSSL"); properties.setProperty("negotiationType", "TLS"); } channel.addPeer(client.newPeer(entry.getKey(), entry.getValue(), properties)); } channel.initialize();
System.out.format("Valid: %s\n", invokeRequest(client, channel, "insert", "ID1","VALUE222")); System.out.format("Message: %s\n", queryRequest(client, channel, "query", "ID1")); }
private static String queryRequest(HFClient client, Channel channel, String function, String... args) throws Exception { QueryByChaincodeRequest request = client.newQueryProposalRequest(); request.setChaincodeName(CHAINCODE_ID); request.setFcn(function); request.setArgs(args); ProposalResponse[] responses = channel.queryByChaincode(request).toArray(new ProposalResponse[0]); return responses[0].getProposalResponse().getResponse().getPayload().toStringUtf8(); }
private static boolean invokeRequest(HFClient client, Channel channel, String function, String... args) throws Exception { TransactionProposalRequest request = client.newTransactionProposalRequest(); request.setChaincodeName(CHAINCODE_ID); request.setFcn(function); request.setArgs(args); Collection<ProposalResponse> responses = channel.sendTransactionProposal(request); BlockEvent.TransactionEvent event = channel.sendTransaction(responses).get(); return event.isValid(); } }
|