Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 52 additions & 115 deletions src/com/rubix/Consensus/InitiatorConsensus.java

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/com/rubix/Consensus/InitiatorProcedure.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public static void consensusSetUp(String data,IPFS ipfs, int PORT,int alphaSize)
String authSenderByQuorumHash="", authQuorumHash="";
authSenderByQuorumHash = message;
authQuorumHash = calculateHash(authSenderByQuorumHash.concat(receiverDidIpfs), "SHA3-256");
InitiatorProcedureLogger.debug("Sender by Quorum Hash" + authSenderByQuorumHash);
InitiatorProcedureLogger.debug("Quorum Auth Hash" + authQuorumHash);

try {
payload.put("sender", senderDidIpfs);
Expand Down
65 changes: 3 additions & 62 deletions src/com/rubix/Consensus/QuorumConsensus.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.rubix.Consensus;

import com.rubix.AuthenticateNode.Authenticate;
import com.rubix.AuthenticateNode.PropImage;
import com.rubix.Resources.IPFSNetwork;
import io.ipfs.api.IPFS;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
Expand Down Expand Up @@ -63,47 +63,11 @@ public void run() {
serverSocket = new ServerSocket(port);
socket = serverSocket.accept();

BufferedReader dataReq = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintStream dataResp = new PrintStream(socket.getOutputStream());
PrintStream out = new PrintStream(socket.getOutputStream());

JSONObject readSenderData;
String getData;
String qstReq;

//? check for incoming request for QST

qstReq = dataReq.readLine();
if (qstReq.contains("qstcmrequest")) {

QuorumConsensusLogger.debug("Sender reqesting QuorumSignedTransactions.json and CreditMapping.json: " + qstReq);

File creditsMapping = new File(WALLET_DATA_PATH + "CreditMapping.json");
if(!creditsMapping.exists()) {
QuorumConsensusLogger.debug("File doesn't exist");
creditsMapping.createNewFile();
writeToFile(creditsMapping.toString(), "[]", false);
}
JSONArray qstContent = new JSONArray(readFile(WALLET_DATA_PATH + "QuorumSignedTransactions.json"));
JSONObject qstObjectSend = new JSONObject();
if(qstContent.length() > 0)
qstObjectSend = qstContent.getJSONObject(qstContent.length() - 1);

String cmFileHash = IPFSNetwork.add(WALLET_DATA_PATH + "CreditMapping.json", ipfs);

JSONObject qResponse = new JSONObject();
qResponse.put("QuorumSignedTransactions", qstObjectSend.toString());
qResponse.put("CreditMapping", cmFileHash);

dataResp.println(qResponse.toString());
}


//TODO: if the incoming request contains the keyword "request", push the QST to IPFS and send the two hashes back to the sender.

//? This is where quorum fetched the data send from initiatorConsensus (Line 148)

getData = in.readLine();
if (getData.contains("ping check")) {
QuorumConsensusLogger.debug("Ping check from sender: " + getData);
Expand Down Expand Up @@ -136,7 +100,7 @@ public void run() {
ArrayList dhtOwnersList = dhtOwnerCheck(verifySenderHash);
QuorumConsensusLogger.debug("Providers: " + dhtOwnersList);
boolean consensusIDcheck = false;
if(dhtOwnersList.size() == 2 && dhtOwnersList.contains(senderPID) && dhtOwnersList.contains(receiverPID))
if(dhtOwnersList.size() <= 2 && dhtOwnersList.contains(senderPID))
consensusIDcheck = true;


Expand All @@ -151,39 +115,16 @@ public void run() {
String creditval;
creditval = in.readLine();
QuorumConsensusLogger.debug("credit value " + creditval);

if (!creditval.equals("null")) { //commented as per test for multiple consensus threads

FileWriter shareWriter = new FileWriter(new File(LOGGER_PATH + "mycredit.txt"), true);
shareWriter.write(creditval);
shareWriter.close();
File readCredit = new File(LOGGER_PATH + "mycredit.txt");
String credit = add(readCredit.toString(), ipfs);

// adding credit to credit mapping
JSONArray CreditBody = new JSONArray(creditval);
JSONObject creditMappingObject = new JSONObject();
JSONArray creditMappingArray = new JSONArray();

for(int i = 0; i < CreditBody.length(); i++){
JSONObject object = CreditBody.getJSONObject(i);
String key = object.getString("did");
String sign = object.getString("sign");
String creditHash = calculateHash(sign, "SHA3-256");

creditMappingObject.put("did", key);
creditMappingObject.put("sign", sign);
creditMappingObject.put("hash", creditHash);
creditMappingObject.put("tid", transactionID);

creditMappingArray.put(creditMappingObject);

writeToFile(WALLET_DATA_PATH + "CreditMapping.json", creditMappingArray.toString(), false);

}
JSONObject storeDetailsQuorum = new JSONObject();
storeDetailsQuorum.put("tid", transactionID);
storeDetailsQuorum.put("consensusID", verifySenderHash);
storeDetailsQuorum.put("minestatus", false);
storeDetailsQuorum.put("sign", senderPrivatePos);
storeDetailsQuorum.put("credits", credit);
storeDetailsQuorum.put("senderdid", senderDidIpfsHash);
Expand Down
30 changes: 26 additions & 4 deletions src/com/rubix/Resources/APIHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

import com.rubix.TokenTransfer.ProofCredits;
import com.rubix.TokenTransfer.TokenSender;
import io.ipfs.api.*;
import org.apache.log4j.*;
import org.json.*;
import io.ipfs.api.IPFS;
import io.ipfs.api.Peer;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
Expand All @@ -17,7 +22,7 @@
import java.util.*;

import static com.rubix.Resources.Functions.*;
import static com.rubix.Resources.IPFSNetwork.executeIPFSCommands;
import static com.rubix.Resources.IPFSNetwork.*;

public class APIHandler {
private static final Logger APILogger = Logger.getLogger(APIHandler.class);
Expand Down Expand Up @@ -197,6 +202,23 @@ public static JSONArray accountInformation() throws JSONException {
return resultArray;
}

/**
* A method to add and host your DID ans Public share to ipfs
*/
public static void addPublicData(){
String peerID = getPeerID(DATA_PATH + "DID.json");
String didHash = getValues(DATA_PATH + "DataTable.json", "didHash", "peerid", peerID);
String walletHash = getValues(DATA_PATH + "DataTable.json", "walletHash", "peerid", peerID);

add(DATA_PATH.concat(didHash).concat("/DID.png"), ipfs);
pin(didHash, ipfs);

add(DATA_PATH.concat(didHash).concat("/PublicShare.png"), ipfs);
pin(walletHash, ipfs);

APILogger.debug("Data Added and Pinned");
}

/**
* A call to sync all the nodes in the network
* @return Message if failed or succeeded
Expand Down
42 changes: 14 additions & 28 deletions src/com/rubix/Resources/Functions.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.rubix.Resources;

import static com.rubix.Resources.APIHandler.networkInfo;
import static com.rubix.Resources.IPFSNetwork.checkSwarmConnect;
import static com.rubix.Resources.IPFSNetwork.executeIPFSCommands;
import static com.rubix.Resources.IPFSNetwork.forwardCheck;
Expand All @@ -15,8 +14,6 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand Down Expand Up @@ -137,7 +134,6 @@ public static void pathSet() {
}


//?
public static void nodeData(String did, String wid, IPFS ipfs) throws IOException {
PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
File dataFolder = new File(DATA_PATH + did + "/");
Expand Down Expand Up @@ -221,7 +217,6 @@ public static String getSystemUser() {
* @return (String) hash
*/

//? rubix-crypto
public static String calculateHash(String message, String algorithm) {
PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
MessageDigest digest = null;
Expand Down Expand Up @@ -562,6 +557,7 @@ public static ArrayList<String> QuorumCheck(JSONArray quorum, int size) {
quorumPeer = getValues(DATA_PATH + "DataTable.json", "peerid", "didHash", quorum.getString(i));
if (checkSwarmConnect().contains(quorumPeer)) {
peers.add(quorumPeer);
FunctionsLogger.debug(quorumPeer + " added to list");
}
} catch (JSONException e) {
FunctionsLogger.error("JSON Exception Occurred", e);
Expand Down Expand Up @@ -589,8 +585,11 @@ public static void QuorumSwarmConnect(JSONArray quorum, IPFS ipfs) {
for (int i = 0; i < quorum.length(); i++) {
String quorumPeer;
try {
FunctionsLogger.debug("Quorum DID: "+ quorum.getString(i));
quorumPeer = getValues(DATA_PATH + "DataTable.json", "peerid", "didHash", quorum.getString(i));

FunctionsLogger.debug("Quorum PID: "+ quorumPeer);
// Commented by Anuradha K; A new method swamConnectP2P is implemented for swarm connection
// IPFSNetwork.swarmConnect(quorumPeer,ipfs);
IPFSNetwork.swarmConnectP2P(quorumPeer,ipfs);

} catch (JSONException e) {
Expand Down Expand Up @@ -643,6 +642,7 @@ public static int[] getPrivatePosition(int[] positions, int[] privateArray) {
public static JSONObject randomPositions(String role, String hash, int numberOfPositions, int[] pvt1) throws JSONException {

int u = 0, l = 0, m = 0;
long st = System.currentTimeMillis();
int[] hashCharacters = new int[256];
int[] randomPositions = new int[32];
int[] randPos = new int[256];
Expand Down Expand Up @@ -687,6 +687,8 @@ public static JSONObject randomPositions(String role, String hash, int numberOfP
JSONObject resultObject = new JSONObject();
resultObject.put("originalPos", originalPos);
resultObject.put("posForSign", posForSign);
long et = System.currentTimeMillis();
FunctionsLogger.debug("Time taken for randomPositions Calculation " + (et - st));
return resultObject;
}

Expand Down Expand Up @@ -723,7 +725,7 @@ public static void deleteFile(String fileName) {
FunctionsLogger.error("IOException Occurred", e);
e.printStackTrace();
}

FunctionsLogger.debug("File Deletion successful");
}


Expand Down Expand Up @@ -765,30 +767,15 @@ public static void deleteFile(String fileName) {
public static void launch() {
pathSet();
PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
int syncFlag = 0;

try {
executeIPFSCommands("ipfs daemon --enable-gc");
if (!SYNC_IP.contains("127.0.0.1")) {
networkInfo();
syncFlag = 1;
}

} catch (MalformedURLException e) {
FunctionsLogger.error("MalformedURL Exception Occurred", e);
e.printStackTrace();
} catch (ProtocolException e) {
FunctionsLogger.error("Protocol Exception Occurred", e);
e.printStackTrace();
} catch (IOException e) {
FunctionsLogger.error("IO Exception Occurred", e);
e.printStackTrace();
} catch (JSONException e) {
} catch (Exception e) {
e.printStackTrace();
}
if (syncFlag == 1)
FunctionsLogger.info("Synced Successfully!");
else
FunctionsLogger.info("Not synced! Try again after sometime.");

FunctionsLogger.debug("Enabled ipfs GC");
}

/**
Expand All @@ -797,7 +784,6 @@ public static void launch() {
* @return A message
* @throws JSONException handle all JSON Exceptions
*/
//? self-test
public static String checkDirectory() throws JSONException {
setDir();
File mainDir = new File(dirPath);
Expand Down Expand Up @@ -1049,7 +1035,7 @@ public static JSONArray getQuorum(String betaHash,String gammaHash,String sender
responseQuorumPick.append(outputQuorumPick);
}
inQuorumPick.close();

FunctionsLogger.debug(" responsequorumpick " + responseQuorumPick.toString());
quorumArray = new JSONArray(responseQuorumPick.toString());
return quorumArray;
}
Expand Down
Loading