JAVA-API BACK
Setup firsts Back Methods for API
This commit is contained in:
parent
1440cd71fe
commit
78f0b30dda
|
@ -1,18 +1,20 @@
|
|||
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 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;
|
||||
private Channel channel;
|
||||
static private Channel channel;
|
||||
private FabricClientWrapper fabricClientWrapper;
|
||||
|
||||
public String getName(){
|
||||
|
@ -46,4 +48,52 @@ public class ChannelClientWrapper {
|
|||
|
||||
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");
|
||||
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<ProposalResponse> sendTransactionProposal (TransactionProposalRequest transactionProposalRequest){
|
||||
try{
|
||||
return channel.sendTransactionProposal(transactionProposalRequest);
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
//Wrapper for channel.sendTransaction
|
||||
public BlockEvent.TransactionEvent sendTransaction(Collection<ProposalResponse> proposalResponses){
|
||||
Logger logger = Logger.getLogger(ChannelClientWrapper.class);
|
||||
BasicConfigurator.configure();
|
||||
try{
|
||||
|
||||
List<ProposalResponse> 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -2,12 +2,11 @@ package blockchain.client;
|
|||
|
||||
import blockchain.configuration.Config;
|
||||
import blockchain.user.UserContext;
|
||||
import org.hyperledger.fabric.sdk.Channel;
|
||||
import org.hyperledger.fabric.sdk.HFClient;
|
||||
import org.hyperledger.fabric.sdk.Peer;
|
||||
import org.hyperledger.fabric.sdk.*;
|
||||
import org.hyperledger.fabric.sdk.exception.CryptoException;
|
||||
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
|
||||
import org.hyperledger.fabric.sdk.exception.ProposalException;
|
||||
import org.hyperledger.fabric.sdk.exception.TransactionException;
|
||||
import org.hyperledger.fabric.sdk.security.CryptoSuite;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
@ -40,4 +39,5 @@ public class FabricClientWrapper {
|
|||
Peer peer = client.newPeer(Config.ORG1_PEER_0,Config.ORG1_PEER_0_URL);
|
||||
return client.queryChannels(peer);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,4 +1,53 @@
|
|||
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.hyperledger.fabric.sdk.ChaincodeResponse;
|
||||
import org.hyperledger.fabric.sdk.ProposalResponse;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public class QueryWrapper {
|
||||
|
||||
public String getUserBalance(String userHashId){
|
||||
String response = null;
|
||||
try{
|
||||
UserContext userContext = Util.readUserContext(Config.ORG1,"User-org1");
|
||||
String chaincode = Config.CHAINCODE_NAME;
|
||||
FabricClientWrapper fabricClientWrapper = new FabricClientWrapper(userContext);
|
||||
|
||||
ChannelClientWrapper channelClientWrapper = ChannelClientWrapper.setupChannel(fabricClientWrapper);
|
||||
|
||||
String[] args = {userHashId};
|
||||
|
||||
Collection<ProposalResponse> responseQuery = channelClientWrapper.queryByChainCode(chaincode,"query",args);
|
||||
for(ProposalResponse pres : responseQuery){
|
||||
ChaincodeResponse.Status status = pres.getStatus();
|
||||
if(status.getStatus()!=200){
|
||||
throw new Exception(pres.getMessage());
|
||||
}
|
||||
|
||||
response = new String(pres.getChaincodeActionResponsePayload());
|
||||
}
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if(response==null) response = "Error";
|
||||
return response;
|
||||
}
|
||||
|
||||
public String getUser(String userHashId){
|
||||
return "";
|
||||
}
|
||||
|
||||
public List<String> getUserLastTransactions(String userHashId){
|
||||
List<String> transactions = new ArrayList<>();
|
||||
return transactions;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,41 @@
|
|||
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.hyperledger.fabric.sdk.BlockEvent;
|
||||
import org.hyperledger.fabric.sdk.ChaincodeID;
|
||||
import org.hyperledger.fabric.sdk.ProposalResponse;
|
||||
import org.hyperledger.fabric.sdk.TransactionProposalRequest;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public class TransactionWrapper {
|
||||
|
||||
public String sendTransaction(String from, String to, String amount){
|
||||
try{
|
||||
UserContext userContext = Util.readUserContext(Config.ORG1,"User-org1");
|
||||
String chaincode = Config.CHAINCODE_NAME;
|
||||
FabricClientWrapper fabricClientWrapper = new FabricClientWrapper(userContext);
|
||||
|
||||
ChannelClientWrapper channelClientWrapper = ChannelClientWrapper.setupChannel(fabricClientWrapper);
|
||||
|
||||
TransactionProposalRequest tpr = fabricClientWrapper.getClient().newTransactionProposalRequest();
|
||||
ChaincodeID cid = ChaincodeID.newBuilder().setName(Config.CHAINCODE_NAME).build();
|
||||
tpr.setChaincodeID(cid);
|
||||
tpr.setFcn("invoke");
|
||||
tpr.setArgs(new String[]{from,to,amount});
|
||||
|
||||
Collection<ProposalResponse> responses = channelClientWrapper.sendTransactionProposal(tpr);
|
||||
BlockEvent.TransactionEvent event = channelClientWrapper.sendTransaction(responses);
|
||||
|
||||
return event.getTransactionID();
|
||||
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -57,6 +57,7 @@ public class ChaincodeTransactionTest {
|
|||
|
||||
Collection<ProposalResponse> responses = channel.sendTransactionProposal(tpr);
|
||||
List<ProposalResponse> invalid = responses.stream().filter(r -> r.isInvalid()).collect(Collectors.toList());
|
||||
|
||||
if (!invalid.isEmpty()) {
|
||||
invalid.forEach(response -> {
|
||||
logger.error(response.getMessage());
|
||||
|
|
|
@ -26,16 +26,8 @@ public class QueryTest {
|
|||
try{
|
||||
String chaincode = Config.CHAINCODE_NAME;
|
||||
FabricClientWrapper fabricClientWrapper = new FabricClientWrapper(user);
|
||||
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:8053");
|
||||
Orderer orderer = fabricClientWrapper.getClient().newOrderer(Config.ORDERER_NAME,Config.ORDERER_URL);
|
||||
channel.addPeer(peer);
|
||||
channel.addEventHub(eventHub);
|
||||
channel.addOrderer(orderer);
|
||||
channel.initialize();
|
||||
ChannelClientWrapper channelClientWrapper = ChannelClientWrapper.setupChannel(fabricClientWrapper);
|
||||
|
||||
String[] args1 = {"a"};
|
||||
|
||||
|
|
|
@ -1,4 +1,18 @@
|
|||
package blockchain.queryWrapper;
|
||||
|
||||
import blockchain.query.QueryWrapper;
|
||||
import org.apache.log4j.BasicConfigurator;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.junit.Test;
|
||||
|
||||
public class QueryWrapperTest {
|
||||
private static Logger logger = Logger.getLogger(QueryWrapperTest.class);
|
||||
@Test
|
||||
public void TestGerUserBalance() {
|
||||
BasicConfigurator.configure();
|
||||
QueryWrapper queryWrapper = new QueryWrapper();
|
||||
String response = queryWrapper.getUserBalance("a");
|
||||
logger.info(response);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,4 +1,26 @@
|
|||
package blockchain.queryWrapper;
|
||||
|
||||
import blockchain.query.QueryWrapper;
|
||||
import blockchain.query.TransactionWrapper;
|
||||
import org.apache.log4j.BasicConfigurator;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TransactionWrapperTest {
|
||||
private static Logger logger = Logger.getLogger(TransactionWrapperTest.class);
|
||||
|
||||
@Test
|
||||
public void TestTransaction() {
|
||||
BasicConfigurator.configure();
|
||||
QueryWrapper queryWrapper = new QueryWrapper();
|
||||
TransactionWrapper transactionWrapper = new TransactionWrapper();
|
||||
String responseTransaction = transactionWrapper.sendTransaction("a","b","15"); //do transaction
|
||||
logger.info("TRANSACTION ID : "+responseTransaction);
|
||||
|
||||
String responseUserB = queryWrapper.getUserBalance("b"); //check new balance of user b
|
||||
logger.info("BALANCE USER B : "+responseUserB);
|
||||
|
||||
String responseUserA = queryWrapper.getUserBalance("a"); //check new balance of user a
|
||||
logger.info("BALANCE USER A :"+responseUserA);
|
||||
}
|
||||
}
|
||||
|
|
Binary file not shown.
Binary file not shown.
BIN
target/classes/blockchain/query/QueryWrapper.class
Normal file
BIN
target/classes/blockchain/query/QueryWrapper.class
Normal file
Binary file not shown.
BIN
target/classes/blockchain/query/TransactionWrapper.class
Normal file
BIN
target/classes/blockchain/query/TransactionWrapper.class
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in a new issue