QueryChaincode Test
Don't work, grpc Unavailable ?
This commit is contained in:
parent
66a7dd8f9a
commit
57a55272ea
|
@ -1,7 +1,14 @@
|
|||
package blockchain.client;
|
||||
|
||||
|
||||
import org.hyperledger.fabric.sdk.ChaincodeID;
|
||||
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 {
|
||||
private String name;
|
||||
|
@ -25,4 +32,18 @@ public class ChannelClientWrapper {
|
|||
this.channel=channel;
|
||||
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;
|
||||
|
||||
import blockchain.client.ChannelClientWrapper;
|
||||
import blockchain.client.FabricClientWrapper;
|
||||
import blockchain.configuration.Config;
|
||||
import blockchain.user.UserContext;
|
||||
import blockchain.utility.Util;
|
||||
import org.apache.log4j.BasicConfigurator;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.hyperledger.fabric.sdk.Peer;
|
||||
import org.hyperledger.fabric.sdk.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
|
||||
public class QueryTest {
|
||||
|
@ -19,9 +21,33 @@ public class QueryTest {
|
|||
BasicConfigurator.configure();
|
||||
UserContext admin = Util.readUserContext(Config.ORG1,Config.ADMIN);
|
||||
try{
|
||||
String chaincode = "mycc";
|
||||
FabricClientWrapper fabricClientWrapper = new FabricClientWrapper(admin);
|
||||
Set<String> channels = fabricClientWrapper.queryForChannels();
|
||||
logger.info("Channels : "+channels);
|
||||
ChannelClientWrapper channelClientWrapper = fabricClientWrapper.createChannelClient(Config.CHANNEL_NAME);
|
||||
|
||||
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){
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in a new issue