Skip to content

Commit

Permalink
two thirds test pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Swastik47 committed Aug 15, 2024
1 parent aed121d commit 63f48e4
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class CPFTreasury extends SetterGetter implements CPFTreasuryInterface {
private final VarDB<Boolean> swapFlag = Context.newVarDB(SWAP_FLAG, Boolean.class);
private final VarDB<BigInteger> swapLimitAmount = Context.newVarDB(SWAP_LIMIT_AMOUNT, BigInteger.class);

private final VarDB<Boolean> councilFlag = Context.newVarDB(COUNCIL_FLAG, Boolean.class);

public CPFTreasury(@Optional Address cpsScore) {
if (treasuryFund.get() == null) {
treasuryFund.set(BigInteger.valueOf(1000000).multiply(EXA));
Expand Down Expand Up @@ -499,6 +501,27 @@ public void migrateOldHashToNewHash(String oldHash, String newHash){
proposalBudgets.set(newHash,totalBudget);
}

@External
public void toggleCouncilFlag() {
validateAdmins();
councilFlag.set(!councilFlag.getOrDefault(false));
}

@External(readonly = true)
public boolean getCouncilFlag() {
return councilFlag.getOrDefault(false);
}

// @External
// public List<Address> setCouncilManagers() {
// validateAdmins();

// }

// @External
// public List<Address> getCouncilManagers() {

// }

//EventLogs
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public class Constants {
public static final String STATE = "state";
public static final Address SYSTEM_ADDRESS = Address.fromString("cx0000000000000000000000000000000000000000");

public static final String COUNCIL_FLAG = "council_flag";

public static final int sICXICXPoolID = 1;
public static final int sICXBNUSDPoolID = 2;

Expand Down
82 changes: 66 additions & 16 deletions CPSCore/src/main/java/community/icon/cps/score/cpscore/CPSCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,28 +66,28 @@ public CPSCore(@Optional BigInteger bondValue, @Optional BigInteger applicationP

// Fix for ICON Dashboard Proposal not being able to submit progress report being rejected

String progressReport = "bafybeic4jhoowlgeyewjqkcpgbm3fs7mgtxzhqd5kbguihoehm4cwwwzcq";
String proposalKey = "bafybeidoxysex3jloigzi4pvdeqgy35a4nykd46w66pobxrkephpawyjoi";
// String progressReport = "bafybeic4jhoowlgeyewjqkcpgbm3fs7mgtxzhqd5kbguihoehm4cwwwzcq";
// String proposalKey = "bafybeidoxysex3jloigzi4pvdeqgy35a4nykd46w66pobxrkephpawyjoi";

String proposalPrefix = proposalPrefix(proposalKey);
String progressReportPrefix = progressReportPrefix(progressReport);
// String proposalPrefix = proposalPrefix(proposalKey);
// String progressReportPrefix = progressReportPrefix(progressReport);

ProposalDataDb.submitProgressReport.at(proposalPrefix).set(Boolean.FALSE);
// ProposalDataDb.submitProgressReport.at(proposalPrefix).set(Boolean.FALSE);

ProgressReportDataDb.status.at(progressReportPrefix).set(PROGRESS_REPORT_REJECTED);
ProgressReportDataDb.timestamp.at(progressReportPrefix).set(BigInteger.valueOf(1706244342109937L));
Status status = new Status();
status.progressReportStatus.get(PROGRESS_REPORT_REJECTED).add(progressReport);
// ProgressReportDataDb.status.at(progressReportPrefix).set(PROGRESS_REPORT_REJECTED);
// ProgressReportDataDb.timestamp.at(progressReportPrefix).set(BigInteger.valueOf(1706244342109937L));
// Status status = new Status();
// status.progressReportStatus.get(PROGRESS_REPORT_REJECTED).add(progressReport);

// End of fix

// Migrate sponsor bond from Techiast- to Techiast
Address oldAddr = Address.fromString("hxdc4b3fb5b47d6c14c7f9a0bac8eea9f3f48d3288");
Address address = Address.fromString("hxfe0256b41f1db0186f9e6cbebf8c71cf006d5d17");
DictDB<String, BigInteger> userAmounts = sponsorBondReturn.at(oldAddr.toString());
// Address oldAddr = Address.fromString("hxdc4b3fb5b47d6c14c7f9a0bac8eea9f3f48d3288");
// Address address = Address.fromString("hxfe0256b41f1db0186f9e6cbebf8c71cf006d5d17");
// DictDB<String, BigInteger> userAmounts = sponsorBondReturn.at(oldAddr.toString());

BigInteger bnUSDAmount = userAmounts.getOrDefault(bnUSD, BigInteger.ZERO);
sponsorBondReturn.at(address.toString()).set(bnUSD, bnUSDAmount);
// BigInteger bnUSDAmount = userAmounts.getOrDefault(bnUSD, BigInteger.ZERO);
// sponsorBondReturn.at(address.toString()).set(bnUSD, bnUSDAmount);

}

Expand Down Expand Up @@ -723,7 +723,15 @@ public void voteProposal(String ipfsKey, String vote, String voteReason, @Option
TAG + ": Proposals can be voted only on Voting Period.");
Address caller = Context.getCaller();
PReps pReps = new PReps();
Context.require(ArrayDBUtils.containsInArrayDb(caller, pReps.validPreps),

List<Address> callLocation;
if (getCouncilFlag()) {
callLocation = getCouncilManagers();
} else {
callLocation = ArrayDBUtils.arrayDBtoList(pReps.validPreps);
}

Context.require(ArrayDBUtils.containsInList(caller, callLocation),
TAG + ": Voting can only be done by registered P-Reps.");
Context.require(List.of(APPROVE, REJECT, ABSTAIN).contains(vote),
TAG + ": Vote should be either _approve, _reject or _abstain");
Expand Down Expand Up @@ -976,9 +984,17 @@ public void voteProgressReport(String reportKey, String voteReason, MilestoneVot
TAG + ": Progress Reports can be voted only on Voting Period.");
Address caller = Context.getCaller();
PReps pReps = new PReps();

List<Address> callLocation = new ArrayList<>();
if (getCouncilFlag()) {
callLocation = getCouncilManagers();
} else {
callLocation = ArrayDBUtils.arrayDBtoList(pReps.validPreps);
}

Context.require(!ArrayDBUtils.containsInArrayDb(caller, blockAddresses),
TAG + ": You are blocked from CPS.");
Context.require(ArrayDBUtils.containsInArrayDb(caller, pReps.validPreps),
Context.require(ArrayDBUtils.containsInList(caller, callLocation),
TAG + ": Voting can only be done by registered P-Reps.");
String progressReportPrefix = progressReportPrefix(reportKey);
ArrayDB<Integer> submittedMilestones = milestoneSubmitted.at(progressReportPrefix);// CAN TAKE FROM READONLY METHOD
Expand Down Expand Up @@ -2656,6 +2672,35 @@ public Map<String, Object> getProposalsHistory(@Optional int startIndex) {
return milestoneIdList;
}

@External(readonly = true)
public Boolean hasTwoThirdsMajority(String key, boolean isMilestone) {
//can also use if else if this doesnt work as intended

//error: milestonedb doesnt have total votes or voters

Map<String, Object> milestoneDbData = MilestoneDb.getDataFromMilestoneDB(key);
BigInteger totalVotes = isMilestone ? (BigInteger)milestoneDbData.get(TOTAL_VOTES) : ProgressReportDataDb.totalVotes.at(key).getOrDefault(BigInteger.ZERO);
int totalVoters = isMilestone ? (Integer)milestoneDbData.get(TOTAL_VOTERS) : ProgressReportDataDb.totalVoters.at(key).getOrDefault(0);

BigInteger approveVotes = isMilestone ? MilestoneDb.approvedVotes.at(key).getOrDefault(BigInteger.ZERO) : ProgressReportDataDb.approvedVotes.at(key).getOrDefault(BigInteger.ZERO);
int approveVoters = isMilestone ? MilestoneDb.approveVoters.at(key).size() : ProgressReportDataDb.approveVoters.at(key).size();

//need to give vote weights = 100 (arbitrary)
boolean voteWeightCheck = approveVotes.multiply(BigInteger.valueOf(3)).compareTo(totalVotes.multiply(BigInteger.valueOf(2))) >= 0;
boolean voterCountCheck = approveVoters * 3 >= totalVoters * 2;

return voteWeightCheck && voterCountCheck;
}

public List<Address> getCouncilManagers() {
return callScore(List.class, getCpfTreasuryScore(), "getCouncilManagers");
}


public boolean getCouncilFlag() {
return callScore(boolean.class, getCpfTreasuryScore(), "getCouncilFlag");
}

// =====================================TEMPORARY MIGRATIONS METHODS===============================================

@External
Expand Down Expand Up @@ -2864,5 +2909,10 @@ public void onlyCPFTreasury() {
Context.require(Context.getCaller().equals(getCpfTreasuryScore()), TAG + ": Only CPF treasury can call this method");
}

public void callScore(Class<Map> eq, Class<MilestoneDb> eq2, String eq3, String eq4) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'callScore'");
}

// =====================================Checkers===============================================
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ public class MilestoneDb {

public static final BranchDB<String, BranchDB<Address, DictDB<String, Integer>>> votersListIndices = Context.newBranchDB(VOTERS_LIST_INDEXES, Integer.class);

public static final BranchDB<String, VarDB<Boolean>> majorityFlag = Context.newBranchDB(MAJORITY_FLAG, Boolean.class);

public static void addDataToMilestoneDb(CPSCoreInterface.MilestonesAttributes milestoneData, String prefix) {
id.at(prefix).set(milestoneData.id);
approvedVotes.at(prefix).set(BigInteger.ZERO);
rejectedVotes.at(prefix).set(BigInteger.ZERO);
completionPeriod.at(prefix).set(milestoneData.completionPeriod);
budget.at(prefix).set(milestoneData.budget);

majorityFlag.at(prefix).set(false);

}

public static Map<String, Object> getDataFromMilestoneDB(String prefix) {
Expand All @@ -46,7 +50,10 @@ public static Map<String, Object> getDataFromMilestoneDB(String prefix) {
Map.entry(TOTAL_VOTERS, ProgressReportDataDb.totalVoters.at(progressReportPrefix(reportHash)).getOrDefault(0)),
Map.entry(APPROVE_VOTERS, approveVoters.at(prefix).size()),
Map.entry(REJECT_VOTERS, rejectVoters.at(prefix).size()),
Map.entry(EXTENSION_FLAG, extensionFlag.at(prefix).getOrDefault(false)));
Map.entry(EXTENSION_FLAG, extensionFlag.at(prefix).getOrDefault(false)),

Map.entry(MAJORITY_FLAG, majorityFlag.at(prefix).getOrDefault(false)));

}

public static String progressReportPrefix(String progressHash) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.math.BigInteger;
import java.util.Map;

import community.icon.cps.score.lib.interfaces.CPSCoreInterface.ProposalAttributes;

import static community.icon.cps.score.cpscore.utils.ArrayDBUtils.recordTxHash;
import static community.icon.cps.score.cpscore.utils.Constants.*;
import static community.icon.cps.score.lib.interfaces.CPSCoreInterface.ProposalAttributes;
Expand Down Expand Up @@ -46,6 +48,9 @@ public class ProposalDataDb {
public static final BranchDB<String, VarDB<Integer>> proposalPeriod = Context.newBranchDB(PROPOSAL_PERIOD, Integer.class);
public static final BranchDB<String, ArrayDB<Integer>> milestoneIds = Context.newBranchDB("milestoneIds", Integer.class);

public static final BranchDB<String, VarDB<Boolean>> majorityFlag = Context.newBranchDB(MAJORITY_FLAG, Boolean.class);
public static final BranchDB<String, VarDB<Boolean>> councilFlag = Context.newBranchDB(COUNCIL_FLAG, Boolean.class);

public static void addDataToProposalDB(ProposalAttributes proposalData, String prefix) {
ipfsHash.at(prefix).set(proposalData.ipfs_hash);
projectTitle.at(prefix).set(proposalData.project_title);
Expand All @@ -69,6 +74,9 @@ public static void addDataToProposalDB(ProposalAttributes proposalData, String p
milestoneCount.at(prefix).set(proposalData.milestoneCount);
isMilestone.at(prefix).set(true);

majorityFlag.at(prefix).set(false);
councilFlag.at(prefix).set(false);

}

public static void updatePercentageCompleted(String prefix, int percentage) {
Expand Down Expand Up @@ -96,7 +104,11 @@ public static Map<String, Object> getDataFromProposalDB(String prefix) {
Map.entry(IS_MILESTONE,isMilestone.at(prefix).getOrDefault(false)),
Map.entry(PERCENTAGE_COMPLETED,percentageCompleted.at(prefix).getOrDefault(0)),
Map.entry(SUBMIT_PROGRESS_REPORT, submitProgressReport.at(prefix).getOrDefault(false)),
Map.entry(PROPOSAL_PERIOD,proposalPeriod.at(prefix).getOrDefault(0)));
Map.entry(PROPOSAL_PERIOD,proposalPeriod.at(prefix).getOrDefault(0)),

Map.entry(MAJORITY_FLAG, majorityFlag.at(prefix).getOrDefault(false)),
Map.entry(COUNCIL_FLAG, councilFlag.at(prefix).getOrDefault(false)));

}

public static int getMilestoneCount(String prefix) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ public class Constants {
public static final Integer MILESTONE_REPORT_APPROVED = 3;
public static final Integer MILESTONE_REPORT_NOT_COMPLETED = 4;

// new flags

public static final String MAJORITY_FLAG = "majority_flag";
public static final String COUNCIL_FLAG = "council_flag";

public static final BigInteger TOTAL_PERIOD = BigInteger.valueOf(30);
}
Loading

0 comments on commit 63f48e4

Please sign in to comment.