Merge branch 'features/queryChaincode' into develop
This commit is contained in:
commit
fa10c7e91c
|
@ -1,7 +1,14 @@
|
||||||
package blockchain.client;
|
package blockchain.client;
|
||||||
|
|
||||||
|
|
||||||
|
import org.hyperledger.fabric.sdk.ChaincodeID;
|
||||||
import org.hyperledger.fabric.sdk.Channel;
|
import org.hyperledger.fabric.sdk.Channel;
|
||||||
|
import org.hyperledger.fabric.sdk.ProposalResponse;
|
||||||
|
import org.hyperledger.fabric.sdk.QueryByChaincodeRequest;
|
||||||
|
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
|
||||||
|
import org.hyperledger.fabric.sdk.exception.ProposalException;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
public class ChannelClientWrapper {
|
public class ChannelClientWrapper {
|
||||||
private String name;
|
private String name;
|
||||||
|
@ -25,4 +32,18 @@ public class ChannelClientWrapper {
|
||||||
this.channel=channel;
|
this.channel=channel;
|
||||||
this.fabricClientWrapper=fabricClientWrapper;
|
this.fabricClientWrapper=fabricClientWrapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Collection<ProposalResponse> 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<ProposalResponse> response = channel.queryByChaincode(request);
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,16 @@
|
||||||
package blockchain.query;
|
package blockchain.query;
|
||||||
|
|
||||||
|
import blockchain.client.ChannelClientWrapper;
|
||||||
import blockchain.client.FabricClientWrapper;
|
import blockchain.client.FabricClientWrapper;
|
||||||
import blockchain.configuration.Config;
|
import blockchain.configuration.Config;
|
||||||
import blockchain.user.UserContext;
|
import blockchain.user.UserContext;
|
||||||
import blockchain.utility.Util;
|
import blockchain.utility.Util;
|
||||||
import org.apache.log4j.BasicConfigurator;
|
import org.apache.log4j.BasicConfigurator;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
import org.hyperledger.fabric.sdk.Peer;
|
import org.hyperledger.fabric.sdk.*;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public class QueryTest {
|
public class QueryTest {
|
||||||
|
@ -19,9 +21,33 @@ public class QueryTest {
|
||||||
BasicConfigurator.configure();
|
BasicConfigurator.configure();
|
||||||
UserContext admin = Util.readUserContext(Config.ORG1,Config.ADMIN);
|
UserContext admin = Util.readUserContext(Config.ORG1,Config.ADMIN);
|
||||||
try{
|
try{
|
||||||
|
String chaincode = "mycc";
|
||||||
FabricClientWrapper fabricClientWrapper = new FabricClientWrapper(admin);
|
FabricClientWrapper fabricClientWrapper = new FabricClientWrapper(admin);
|
||||||
Set<String> channels = fabricClientWrapper.queryForChannels();
|
ChannelClientWrapper channelClientWrapper = fabricClientWrapper.createChannelClient(Config.CHANNEL_NAME);
|
||||||
logger.info("Channels : "+channels);
|
|
||||||
|
Channel 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:7053");
|
||||||
|
Orderer orderer = fabricClientWrapper.getClient().newOrderer(Config.ORDERER_NAME,Config.ORDERER_URL);
|
||||||
|
channel.addPeer(peer);
|
||||||
|
channel.addEventHub(eventHub);
|
||||||
|
channel.addOrderer(orderer);
|
||||||
|
channel.initialize();
|
||||||
|
|
||||||
|
|
||||||
|
String[] args1 = {"a"};
|
||||||
|
|
||||||
|
Collection<ProposalResponse> responseQuery = channelClientWrapper.queryByChainCode(chaincode,"query",args1);
|
||||||
|
for(ProposalResponse pres : responseQuery){
|
||||||
|
ChaincodeResponse.Status status = pres.getStatus();
|
||||||
|
if(status.getStatus()!=200){
|
||||||
|
throw new Exception(pres.getMessage());
|
||||||
|
}
|
||||||
|
String stringResponse = new String(pres.getChaincodeActionResponsePayload());
|
||||||
|
logger.info("RESPONSE : "+stringResponse);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}catch (Exception e){
|
}catch (Exception e){
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in a new issue