Skip to content

Commit

Permalink
Updating staking logic
Browse files Browse the repository at this point in the history
  • Loading branch information
gklps committed Sep 26, 2022
1 parent 0429a73 commit 0c008e1
Show file tree
Hide file tree
Showing 4 changed files with 733 additions and 5 deletions.
28 changes: 28 additions & 0 deletions src/com/rubix/TokenTransfer/TokenReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,34 @@ public static String receive() {

if (wholeTokens.length() > 0) {

boolean pledged = false;
JSONArray pledgedTokens = new JSONArray();
for(int i = 0; i < wholeTokens.length(); i++){
JSONArray chain = new JSONArray(wholeTokenChainContent.get(i));
JSONObject genesisBlock = chain.getJSONObject(0);
if(genesisBlock.has("pledgeToken")) {
pledged = true;
JSONObject pledgedTokenObject = new JSONObject();
pledgedTokenObject.put("tokenHash", wholeTokens.getString(i));
pledgedTokens.put(pledgedTokenObject);
}
}
if(pledged){
output.println("419");
output.println(pledgedTokens.toString());
APIResponse.put("did", senderDidIpfsHash);
APIResponse.put("tid", "null");
APIResponse.put("status", "Failed");
APIResponse.put("message", "Few Tokens are Pledged");
TokenReceiverLogger.info("Few Tokens are Pledged");
IPFSNetwork.executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
output.close();
input.close();
sk.close();
ss.close();
return APIResponse.toString();
}

if (!(ipfsGetFlag == intPart)) {
output.println("422");
APIResponse.put("did", senderDidIpfsHash);
Expand Down
79 changes: 74 additions & 5 deletions src/com/rubix/TokenTransfer/TokenSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import com.rubix.Resources.Functions;
import com.rubix.Resources.IPFSNetwork;

import com.rubix.TokenTransfer.TransferPledge.Initiator;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.json.JSONArray;
Expand Down Expand Up @@ -94,6 +95,7 @@ public class TokenSender {
* @throws NoSuchAlgorithmException handles No Such Algorithm Exceptions
*/
public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception {
TokenSenderLogger.debug("Transfer with Pledge check initiated");
repo(ipfs);
JSONObject APIResponse = new JSONObject();
PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
Expand Down Expand Up @@ -404,7 +406,25 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
}

case 2: {
quorumArray = new JSONArray(readFile(DATA_PATH + "quorumlist.json"));
File quorumFile = new File(DATA_PATH.concat("quorumlist.json"));
if(!quorumFile.exists()){
TokenSenderLogger.error("Quorum List for Subnet not found");
APIResponse.put("status", "Failed");
APIResponse.put("message", "Quorum List for Subnet not found");
return APIResponse;
}else{
String quorumList = readFile(DATA_PATH + "quorumlist.json");
if(quorumList != null){
quorumArray = new JSONArray(readFile(DATA_PATH + "quorumlist.json"));
}else{
TokenSenderLogger.error("File for Quorum List for Subnet is empty");
APIResponse.put("status", "Failed");
APIResponse.put("message", "File for Quorum List for Subnet is empty");
return APIResponse;
}

}

break;
}
case 3: {
Expand All @@ -424,8 +444,7 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
ArrayList alphaPeersList;
ArrayList betaPeersList;
ArrayList gammaPeersList;

List<String> quorumList = new ArrayList<>();

String errMessage = null;
for (int i = 0; i < quorumArray.length(); i++) {

Expand Down Expand Up @@ -513,9 +532,39 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
return APIResponse;
}

TokenSenderLogger.debug("Final Selected Alpha Members: " + alphaPeersList);
JSONArray alphaList = new JSONArray();
for(int i = 0; i < alphaPeersList.size(); i++){
alphaList.put(alphaPeersList.get(i));
}
JSONObject dataToSendToInitiator = new JSONObject();
dataToSendToInitiator.put("alphaList", alphaList);
dataToSendToInitiator.put("tokenList", wholeTokens);
dataToSendToInitiator.put("amount", wholeTokens.length()+partTokens.length());

TokenSenderLogger.debug("Details being sent to Initiator: " + dataToSendToInitiator);

boolean abort = Initiator.pledgeSetUp(dataToSendToInitiator.toString(), ipfs, 22143);
if(abort){
Initiator.abort = false;
updateQuorum(quorumArray, null, false, type);
APIResponse.put("did", senderDidIpfsHash);
APIResponse.put("tid", "null");
APIResponse.put("status", "Failed");
if(Initiator.abortReason.has("Quorum"))
APIResponse.put("message", "Alpha Node " + Initiator.abortReason.getString("Quorum") + " " + Initiator.abortReason.getString("Reason"));
else
APIResponse.put("message", Initiator.abortReason.getString("Reason"));
TokenSenderLogger.warn("Quorum Members with insufficient Tokens/Credits");
senderMutex = false;
Initiator.abortReason = new JSONObject();
return APIResponse;
}

TokenSenderLogger.debug("Nodes that pledged tokens: " + Initiator.pledgedNodes);

syncDataTable(receiverDidIpfsHash, null);
// receiverPeerId = getValues(DATA_PATH + "DataTable.json", "peerid", "didHash",
// receiverDidIpfsHash);


if (!receiverPeerId.equals("")) {
TokenSenderLogger.debug("Swarm connecting to " + receiverPeerId);
Expand Down Expand Up @@ -718,6 +767,25 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
return APIResponse;
}else if (tokenAuth != null && (tokenAuth.startsWith("4"))) {
switch (tokenAuth) {
case "419":
String pledgedTokens = input.readLine();
JSONArray pledgedTokensArray = new JSONArray(pledgedTokens);
TokenSenderLogger.info("These tokens are pledged " + pledgedTokensArray);
TokenSenderLogger.info("Kindly re-initiate transaction");
APIResponse.put("message", "Pledged Tokens " + pledgedTokensArray + ". Kindly re-initiate transaction");
File pledgeFile = new File(PAYMENTS_PATH.concat("PledgedTokens.json"));
if(!pledgeFile.exists()) {
pledgeFile.createNewFile();
writeToFile(PAYMENTS_PATH.concat("PledgedTokens.json"), pledgedTokensArray.toString(), false);
}else{
String pledgedContent = readFile(PAYMENTS_PATH.concat("PledgedTokens.json"));
JSONArray pledgedArray = new JSONArray(pledgedContent);
for(int i = 0; i < pledgedTokensArray.length(); i++){
pledgedArray.put(pledgedTokensArray.getJSONObject(i));
}
writeToFile(PAYMENTS_PATH.concat("PledgedTokens.json"), pledgedArray.toString(), false);
}
break;
case "420":
String doubleSpent = input.readLine();
String owners = input.readLine();
Expand Down Expand Up @@ -785,6 +853,7 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
dataObject.put("betaList", betaPeersList);
dataObject.put("gammaList", gammaPeersList);


InitiatorProcedure.consensusSetUp(dataObject.toString(), ipfs, SEND_PORT + 100, alphaSize, "");

if (InitiatorConsensus.quorumSignature.length() < (minQuorum(alphaSize) + 2 * minQuorum(7))) {
Expand Down
Loading

0 comments on commit 0c008e1

Please sign in to comment.