Skip to content
Open
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
37 changes: 35 additions & 2 deletions src/com/rubix/Ping/PingReceive.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ public static String receive(String userType,int port) throws JSONException {
PingReceiverLogger.info(userType + " Pong Sent "+currentVersion);

}
else
if(pingRequest.contains("Get-TokenChain-Height")) {
else if(pingRequest.contains("Get-TokenChain-Height")) {
String tokenHash;
try {
tokenHash = input.readLine();
Expand Down Expand Up @@ -125,9 +124,43 @@ public static String receive(String userType,int port) throws JSONException {
height = chainArray.length()-1;
PingReceiverLogger.info("Chain height: " + height);
}
output.println(height);
}

}
else if(pingRequest.contains("Get-TokenChain")){
String tokenHash;
try {
tokenHash = input.readLine();
} catch (SocketException e) {
PingReceiverLogger.warn("Sender Stream Null - tokenHash");
APIResponse.put("did", "");
APIResponse.put("tid", "null");
APIResponse.put("status", "Failed");
APIResponse.put("message", "Sender Stream Null - tokenHash");

output.close();
input.close();
sk.close();
ss.close();
return APIResponse.toString();

}
if (tokenHash != null && tokenHash.startsWith("Qm") && tokenHash.length() == 46) {
JSONArray tokenChain;
PingReceiverLogger.info("Token chain height requested for: " + tokenHash);
File tokenChainFile = new File(TOKENCHAIN_PATH.concat(tokenHash).concat(".json"));
if(!tokenChainFile.exists()) {
PingReceiverLogger.info("Token chain file not found");
tokenChain = new JSONArray();
}
else{
String tokenChainFileContent = readFile(TOKENCHAIN_PATH.concat(tokenHash).concat(".json"));
tokenChain = new JSONArray(tokenChainFileContent);
}
output.println(tokenChain);
}
}
else{
APIResponse.put("status", "Failed");
APIResponse.put("message", "Request Failed");
Expand Down
183 changes: 115 additions & 68 deletions src/com/rubix/Ping/VerifyStakedToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

Expand All @@ -26,8 +27,9 @@
public class VerifyStakedToken {
private static final Logger PingSenderLogger = Logger.getLogger(VerifyStakedToken.class);
public static IPFS ipfs = new IPFS("/ip4/127.0.0.1/tcp/" + IPFS_PORT);
public static JSONArray tokenChain;

public static boolean Contact(String pid, int port, String tokenHash, String tokenContent)
public static boolean Contact(String pid, int port, String tokenHash, String tokenContent, String pingMessage)
throws IOException, JSONException {
repo(ipfs);
PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
Expand All @@ -42,78 +44,123 @@ public static boolean Contact(String pid, int port, String tokenHash, String tok
BufferedReader input = new BufferedReader(new InputStreamReader(senderSocket.getInputStream()));
PrintStream output = new PrintStream(senderSocket.getOutputStream());

output.println("Get-TokenChain-Height");
output.println(tokenHash);
String heightResponse;
try {
heightResponse = input.readLine();
} catch (SocketException e) {
PingSenderLogger.warn("Quorum " + pid + " is unable to Respond! - Credits Ping");
executeIPFSCommands(" ipfs p2p close -t /p2p/" + pid);
output.close();
input.close();
senderSocket.close();
APIResponse.put("status", "Failed");
APIResponse.put("message", "Quorum " + pid + "is unable to respond! - Credits Ping");

return false;
}
output.println(pingMessage);

if(pingMessage.equals("Get-TokenChain-Height")) {
output.println(tokenHash);
String heightResponse;
try {
heightResponse = input.readLine();
} catch (SocketException e) {
PingSenderLogger.warn("Quorum " + pid + " is unable to Respond! - Credits Ping");
executeIPFSCommands(" ipfs p2p close -t /p2p/" + pid);
output.close();
input.close();
senderSocket.close();
APIResponse.put("status", "Failed");
APIResponse.put("message", "Quorum " + pid + "is unable to respond! - Credits Ping");

return false;
}

int height = 0;
if (heightResponse == null) {
executeIPFSCommands(" ipfs p2p close -t /p2p/" + pid);
PingSenderLogger.info("TokenChain height not received");
output.close();
input.close();
senderSocket.close();
APIResponse.put("status", "Failed");
APIResponse.put("message", "TokenChain height not received");

} else {
PingSenderLogger.info("TokenChain height received from " + pid);
PingSenderLogger.info("TokenChain height: " + heightResponse);
executeIPFSCommands(" ipfs p2p close -t /p2p/" + pid);
output.close();
input.close();
senderSocket.close();
APIResponse.put("status", "Success");
APIResponse.put("message", Integer.parseInt(heightResponse));
height = Integer.parseInt(heightResponse);
}
int height = 0;
if (heightResponse == null) {
executeIPFSCommands(" ipfs p2p close -t /p2p/" + pid);
PingSenderLogger.info("TokenChain height not received");
output.close();
input.close();
senderSocket.close();
APIResponse.put("status", "Failed");
APIResponse.put("message", "TokenChain height not received");

} else {
PingSenderLogger.info("TokenChain height received from " + pid);
PingSenderLogger.info("TokenChain height: " + heightResponse);
executeIPFSCommands(" ipfs p2p close -t /p2p/" + pid);
output.close();
input.close();
senderSocket.close();
APIResponse.put("status", "Success");
APIResponse.put("message", Integer.parseInt(heightResponse));
height = Integer.parseInt(heightResponse);
}

String tokenLevel = tokenContent.substring(0, 3);
int tokenLevelInt = Integer.parseInt(tokenLevel);
int tokenLevelValue = (int) Math.pow(2, tokenLevelInt + 2);
int requiredMinedTokenHeight = tokenLevelValue * 4;

String GET_URL_credit = SYNC_IP + "/getCurrentLevel";
URL URLobj_credit = new URL(GET_URL_credit);
HttpURLConnection con_credit = (HttpURLConnection) URLobj_credit.openConnection();
con_credit.setRequestMethod("GET");
int responseCode_credit = con_credit.getResponseCode();
System.out.println("GET Response Code :: " + responseCode_credit);
if (responseCode_credit == HttpURLConnection.HTTP_OK) {
BufferedReader in_credit = new BufferedReader(
new InputStreamReader(con_credit.getInputStream()));
String inputLine_credit;
StringBuffer response_credit = new StringBuffer();
while ((inputLine_credit = in_credit.readLine()) != null) {
response_credit.append(inputLine_credit);
String tokenLevel = tokenContent.substring(0, 3);
int tokenLevelInt = Integer.parseInt(tokenLevel);
int tokenLevelValue = (int) Math.pow(2, tokenLevelInt + 2);
int requiredMinedTokenHeight = tokenLevelValue * 4;

String GET_URL_credit = SYNC_IP + "/getCurrentLevel";
URL URLobj_credit = new URL(GET_URL_credit);
HttpURLConnection con_credit = (HttpURLConnection) URLobj_credit.openConnection();
con_credit.setRequestMethod("GET");
int responseCode_credit = con_credit.getResponseCode();
System.out.println("GET Response Code :: " + responseCode_credit);
if (responseCode_credit == HttpURLConnection.HTTP_OK) {
BufferedReader in_credit = new BufferedReader(
new InputStreamReader(con_credit.getInputStream()));
String inputLine_credit;
StringBuffer response_credit = new StringBuffer();
while ((inputLine_credit = in_credit.readLine()) != null) {
response_credit.append(inputLine_credit);
}
in_credit.close();
// QuorumConsensusLogger.debug("response from service " +
// response_credit.toString());
JSONObject resJsonData_credit = new JSONObject(response_credit.toString());
int level_credit = resJsonData_credit.getInt("level");

// ! release staked token if the mined token is from previous level(s)
if (level_credit > tokenLevelInt) {
return true;
}

} else
PingSenderLogger.debug("GET request not worked");

return height > requiredMinedTokenHeight;
}
else{
tokenChain = new JSONArray();
output.println(tokenHash);
String tokenChainResponse;
try {
tokenChainResponse = input.readLine();
} catch (SocketException e) {
PingSenderLogger.warn("Node " + pid + " is unable to Respond! - Token Chain Ping");
executeIPFSCommands(" ipfs p2p close -t /p2p/" + pid);
output.close();
input.close();
senderSocket.close();
APIResponse.put("status", "Failed");
APIResponse.put("message", "Node " + pid + "is unable to respond! - Token Chain Ping");

return false;
}
in_credit.close();
// QuorumConsensusLogger.debug("response from service " +
// response_credit.toString());
JSONObject resJsonData_credit = new JSONObject(response_credit.toString());
int level_credit = resJsonData_credit.getInt("level");

// ! release staked token if the mined token is from previous level(s)
if (level_credit > tokenLevelInt) {

if (tokenChainResponse == null) {
executeIPFSCommands(" ipfs p2p close -t /p2p/" + pid);
PingSenderLogger.info("TokenChain not received");
output.close();
input.close();
senderSocket.close();
APIResponse.put("status", "Failed");
APIResponse.put("message", "TokenChain not received");
return false;

} else {
tokenChain = new JSONArray(tokenChainResponse);
PingSenderLogger.info("TokenChain received from " + pid);
PingSenderLogger.info("TokenChain: " + tokenChainResponse);
executeIPFSCommands(" ipfs p2p close -t /p2p/" + pid);
output.close();
input.close();
senderSocket.close();
APIResponse.put("status", "Success");
APIResponse.put("message", "Tokenchain Received");
return true;
}
}

} else
PingSenderLogger.debug("GET request not worked");

return height > requiredMinedTokenHeight;
}
}
33 changes: 31 additions & 2 deletions src/com/rubix/Resources/APIHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,38 @@ public static String networkInfo() throws IOException, JSONException {
}

rd.close();

writeToFile(DATA_PATH + "DataTable.json", result.toString(), false);
if (syncFlag == 1) {
if(syncFlag ==1)
{
APILogger.debug("Network info synced to DataTable.json");
}

URL url2 = new URL(SYNC_IP + "/getPubKeyData");
HttpURLConnection conn2 = (HttpURLConnection) url2.openConnection();
conn2.setRequestMethod("GET");
StringBuilder result2 = new StringBuilder();
BufferedReader rd2 = new BufferedReader(new InputStreamReader(conn2.getInputStream()));
String line2;
int syncFlag2 =0;

while ((line2 = rd2.readLine()) != null) {
result2.append(line2);
syncFlag2=1;
}
rd2.close();

File dt_PubKey = new File(DATA_PATH + "DataTable_PublicKeys.json");
if (!dt_PubKey.exists()) {
dt_PubKey.createNewFile();
}

writeToFile(DATA_PATH + "DataTable_PublicKeys.json", result2.toString(), false);
if(syncFlag ==1)
{
APILogger.debug("Network info synced to DataTable_PublicKeys.json");
}

if (syncFlag == 1 && syncFlag2==1) {
jsonObject.put("message", "Synced Successfully!");
} else {
jsonObject.put("message", "Not synced! Try again after sometime.");
Expand Down
56 changes: 1 addition & 55 deletions src/com/rubix/Resources/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -1235,43 +1235,7 @@ public static void correctPartToken() throws JSONException {
}
}

public static void tokenBank() throws JSONException {
pathSet();
String bank = readFile(PAYMENTS_PATH.concat("BNK00.json"));
JSONArray bankArray = new JSONArray(bank);

ArrayList<String> bankDuplicates = new ArrayList<>();
for (int i = 0; i < bankArray.length(); i++) {
if (!bankDuplicates.contains(bankArray.getJSONObject(i).getString("tokenHash")))
bankDuplicates.add(bankArray.getJSONObject(i).getString("tokenHash"));
}

if (bankDuplicates.size() < bankArray.length()) {
FunctionsLogger.debug("Duplicates Found. Cleaning up ...");

JSONArray newBank = new JSONArray();
for (int i = 0; i < bankDuplicates.size(); i++) {
JSONObject tokenObject = new JSONObject();
tokenObject.put("tokenHash", bankDuplicates.get(i));
newBank.put(tokenObject);
}
writeToFile(PAYMENTS_PATH.concat("BNK00.json"), newBank.toString(), false);
}

File tokensPath = new File(TOKENS_PATH);
String contents[] = tokensPath.list();
ArrayList tokenFiles = new ArrayList();
for (int i = 0; i < contents.length; i++) {
if (!contents[i].contains("PARTS"))
tokenFiles.add(contents[i]);
}

for (int i = 0; i < tokenFiles.size(); i++) {
if (!bankDuplicates.contains(tokenFiles.get(i).toString()))
deleteFile(TOKENS_PATH.concat(tokenFiles.get(i).toString()));
}

}

public static Double getPartsBalance() throws JSONException {
pathSet();
Expand Down Expand Up @@ -1551,25 +1515,7 @@ public static void clearParts() throws JSONException {
writeToFile(PAYMENTS_PATH.concat("PartsToken.json"), partsArray.toString(), false);
}

public static void backgroundChecks() {
try {
Functions.tokenBank();
} catch (JSONException e) {
e.printStackTrace();
}

try {
Functions.clearParts();
} catch (JSONException e) {
e.printStackTrace();
}

IPFS ipfs = new IPFS("/ip4/127.0.0.1/tcp/" + IPFS_PORT);
IPFSNetwork.repo(ipfs);

addPublicData();

}


public static String sanityMessage;

Expand Down
Loading