Skip to content

Commit

Permalink
chore: (#45) Fix failing java tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OT-kraftchain committed Dec 10, 2024
1 parent fe43b16 commit 6c8972b
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 347 deletions.
73 changes: 35 additions & 38 deletions src/main/java/com/axlabs/neo/grantshares/GrantSharesGov.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ public class GrantSharesGov {
static Event unpaused;
@DisplayName("ProposalMigrated")
static Event1Arg<Integer> migrated;
@DisplayName("Error")
static Event2Args<String, String> error;
//endregion EVENTS

/**
Expand Down Expand Up @@ -328,16 +326,16 @@ public static int createProposal(Hash160 proposer, Intent[] intents, String offc
* @return The id of the proposal.
*/
public static int createProposal(Hash160 proposer, Intent[] intents, String offchainUri, int linkedProposal,
int acceptanceRate, int quorum) {
int acceptanceRate, int quorum) {

if (!checkWitness(proposer)) fireErrorAndAbort("Not authorised", "createProposal");
if (!checkWitness(proposer)) Helper.abort("createProposal" + ": " + "Not authorised");
if (acceptanceRate < parameters.getInt(MIN_ACCEPTANCE_RATE_KEY) || acceptanceRate > 100)
fireErrorAndAbort("Invalid acceptance rate", "createProposal");
Helper.abort("createProposal" + ": " + "Invalid acceptance rate");
if (quorum < parameters.getInt(MIN_QUORUM_KEY) || quorum > 100)
fireErrorAndAbort("Invalid quorum", "createProposal");
Helper.abort("createProposal" + ": " + "Invalid quorum");
if (linkedProposal >= 0 && proposals.get(linkedProposal) == null)
fireErrorAndAbort("Linked proposal doesn't exist", "createProposal");
if (!areIntentsValid(intents)) fireErrorAndAbort("Invalid intents", "createProposal");
Helper.abort("createProposal" + ": " + "Linked proposal doesn't exist");
if (!areIntentsValid(intents)) Helper.abort("createProposal" + ": " + "Invalid intents");

int id = Storage.getInt(getReadOnlyContext(), PROPOSALS_COUNT_KEY);
int expiration = parameters.getInt(EXPIRATION_LENGTH_KEY) + getTime();
Expand Down Expand Up @@ -377,12 +375,12 @@ private static boolean areIntentsValid(Intent[] intents) {
public static void endorseProposal(int id, Hash160 endorser) {
abortIfPaused();
if (members.get(endorser.toByteString()) == null || !checkWitness(endorser))
fireErrorAndAbort("Not authorised", "endorseProposal");
Helper.abort("endorseProposal" + ": " + "Not authorised");
ByteString proposalBytes = proposals.get(id);
if (proposalBytes == null) fireErrorAndAbort("Proposal doesn't exist", "endorseProposal");
if (proposalBytes == null) Helper.abort("endorseProposal" + ": " + "Proposal doesn't exist");
Proposal proposal = (Proposal) new StdLib().deserialize(proposalBytes);
if (proposal.expiration <= getTime()) fireErrorAndAbort("Proposal expired", "endorseProposal");
if (proposal.endorser != null) fireErrorAndAbort("Proposal already endorsed", "endorseProposal");
if (proposal.expiration <= getTime()) Helper.abort("endorseProposal" + ": " + "Proposal expired");
if (proposal.endorser != null) Helper.abort("endorseProposal" + ": " + "Proposal already endorsed");

proposal.endorser = endorser;
proposal.reviewEnd = getTime() + parameters.getInt(REVIEW_LENGTH_KEY);
Expand All @@ -404,17 +402,17 @@ public static void endorseProposal(int id, Hash160 endorser) {
*/
public static void vote(int id, int vote, Hash160 voter) {
abortIfPaused();
if (vote < -1 || vote > 1) fireErrorAndAbort("Invalid vote", "vote");
if (vote < -1 || vote > 1) Helper.abort("vote" + ": " + "Invalid vote");
if (members.get(voter.toByteString()) == null || !checkWitness(voter))
fireErrorAndAbort("Not authorised", "vote");
Helper.abort("vote" + ": " + "Not authorised");
ByteString proposalBytes = proposals.get(id);
if (proposalBytes == null) fireErrorAndAbort("Proposal doesn't exist", "vote");
if (proposalBytes == null) Helper.abort("vote" + ": " + "Proposal doesn't exist");
Proposal proposal = (Proposal) new StdLib().deserialize(proposalBytes);
int time = getTime();
if (proposal.endorser == null || time < proposal.reviewEnd || time >= proposal.votingEnd)
fireErrorAndAbort("Proposal not active", "vote");
Helper.abort("vote" + ": " + "Proposal not active");
ProposalVotes pv = (ProposalVotes) new StdLib().deserialize(proposalVotes.get(id));
if (pv.voters.containsKey(voter)) fireErrorAndAbort("Already voted on this proposal", "vote");
if (pv.voters.containsKey(voter)) Helper.abort("vote" + ": " + "Already voted on this proposal");

pv.voters.put(voter, vote);
if (vote < 0) {
Expand All @@ -440,20 +438,20 @@ public static void vote(int id, int vote, Hash160 voter) {
public static Object[] execute(int id) {
abortIfPaused();
ByteString proposalBytes = proposals.get(id);
if (proposalBytes == null) fireErrorAndAbort("Proposal doesn't exist", "execute");
if (proposalBytes == null) Helper.abort("execute" + ": " + "Proposal doesn't exist");
Proposal proposal = (Proposal) new StdLib().deserialize(proposalBytes);
if (proposal.endorser == null || getTime() < proposal.timeLockEnd)
fireErrorAndAbort("Proposal not in execution phase", "execute");
if (proposal.executed) fireErrorAndAbort("Proposal already executed", "execute");
if (proposal.expiration <= getTime()) fireErrorAndAbort("Proposal expired", "execute");
Helper.abort("execute" + ": " + "Proposal not in execution phase");
if (proposal.executed) Helper.abort("execute" + ": " + "Proposal already executed");
if (proposal.expiration <= getTime()) Helper.abort("execute" + ": " + "Proposal expired");
ProposalData data = (ProposalData) new StdLib().deserialize(proposalData.get(id));
ProposalVotes votes = (ProposalVotes) new StdLib().deserialize(proposalVotes.get(id));
int voteCount = votes.approve + votes.abstain + votes.reject;
if (voteCount * 100 / Storage.getInt(getReadOnlyContext(), MEMBERS_COUNT_KEY) < data.quorum)
fireErrorAndAbort("Quorum not reached", "execute");
Helper.abort("execute" + ": " + "Quorum not reached");
int yesNoCount = votes.approve + votes.reject;
if (yesNoCount == 0 || (votes.approve * 100 / yesNoCount <= data.acceptanceRate))
fireErrorAndAbort("Proposal rejected", "execute");
Helper.abort("execute" + ": " + "Proposal rejected");

proposal.executed = true;
Object[] returnVals = new Object[data.intents.length];
Expand Down Expand Up @@ -491,17 +489,17 @@ private static void abortOnInvalidValue(String paramKey, int value) {
case VOTING_LENGTH_KEY:
case TIMELOCK_LENGTH_KEY:
case EXPIRATION_LENGTH_KEY:
if (value < 0) fireErrorAndAbort("Invalid parameter value", "changeParam");
if (value < 0) Helper.abort("changeParam" + ": " + "Invalid parameter value");
break;
case MIN_ACCEPTANCE_RATE_KEY:
case MIN_QUORUM_KEY:
if (value < 0 || value > 100) fireErrorAndAbort("Invalid parameter value", "changeParam");
if (value < 0 || value > 100) Helper.abort("changeParam" + ": " + "Invalid parameter value");
break;
case MULTI_SIG_THRESHOLD_KEY:
if (value <= 0 || value > 100) fireErrorAndAbort("Invalid parameter value", "changeParam");
if (value <= 0 || value > 100) Helper.abort("changeParam" + ": " + "Invalid parameter value");
break;
default:
fireErrorAndAbort("Unknown parameter", "changeParam");
Helper.abort("changeParam" + ": " + "Unknown parameter");
}
}

Expand All @@ -516,7 +514,7 @@ public static void addMember(ECPoint memberPubKey) {
abortIfPaused();
abortIfCallerIsNotSelf();
Hash160 memberHash = createStandardAccount(memberPubKey);
if (members.get(memberHash.toByteString()) != null) fireErrorAndAbort("Already a member", "addMember");
if (members.get(memberHash.toByteString()) != null) Helper.abort("addMember" + ": " + "Already a member");
members.put(memberHash.toByteString(), memberPubKey.toByteString());
Storage.put(ctx, MEMBERS_COUNT_KEY, Storage.getInt(getReadOnlyContext(), MEMBERS_COUNT_KEY) + 1);
memberAdded.fire(memberHash);
Expand All @@ -533,7 +531,7 @@ public static void removeMember(ECPoint memberPubKey) {
abortIfPaused();
abortIfCallerIsNotSelf();
Hash160 memberHash = createStandardAccount(memberPubKey);
if (members.get(memberHash.toByteString()) == null) fireErrorAndAbort("Not a member", "removeMember");
if (members.get(memberHash.toByteString()) == null) Helper.abort("removeMember" + ": " + "Not a member");
members.delete(memberHash.toByteString());
Storage.put(ctx, MEMBERS_COUNT_KEY, Storage.getInt(getReadOnlyContext(), MEMBERS_COUNT_KEY) - 1);
memberRemoved.fire(memberHash);
Expand Down Expand Up @@ -562,9 +560,9 @@ public static void pause() {
try {
membersMultiSigHash = calcMembersMultiSigAccount();
} catch (Exception e) {
fireErrorAndAbort(e.getMessage(), "pause");
Helper.abort("pause" + ": " + e.getMessage());
}
if (!checkWitness(membersMultiSigHash)) fireErrorAndAbort("Not authorized", "pause");
if (!checkWitness(membersMultiSigHash)) Helper.abort("pause" + ": " + "Not authorized");
Storage.put(ctx, PAUSED_KEY, 1);
paused.fire();
}
Expand All @@ -574,27 +572,26 @@ public static void unpause() {
try {
membersMultiSigHash = calcMembersMultiSigAccount();
} catch (Exception e) {
fireErrorAndAbort(e.getMessage(), "pause");
Helper.abort("pause" + ": " + e.getMessage());
}
if (!checkWitness(membersMultiSigHash)) fireErrorAndAbort("Not authorized", "unpause");
if (!checkWitness(membersMultiSigHash)) Helper.abort("unpause" + ": " + "Not authorized");
Storage.put(ctx, PAUSED_KEY, 0);
unpaused.fire();
}

private static void abortIfCallerIsNotSelf() {
if (Runtime.getCallingScriptHash() != Runtime.getExecutingScriptHash()) {
fireErrorAndAbort("Method only callable by the contract itself", "abortIfCallerIsNotSelf");
Helper.abort("abortIfCallerIsNotSelf" + ": " + "Method only callable by the contract itself");
}
}

public static void abortIfPaused() {
if (Storage.getBoolean(getReadOnlyContext(), PAUSED_KEY)) {
fireErrorAndAbort("Contract is paused", "abortIfPaused");
Helper.abort("abortIfPaused" + ": " + "Contract is paused");
}
}

private static void fireErrorAndAbort(String msg, String method) {
error.fire(msg, method);
Helper.abort();
private static void abortWithMessage(String method, String msg) {
Helper.abort(method + ": " + msg);
}
}
43 changes: 21 additions & 22 deletions src/main/java/com/axlabs/neo/grantshares/GrantSharesTreasury.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class GrantSharesTreasury {
static final StorageMap funders = new StorageMap(ctx, FUNDERS_PREFIX); // [hash, List<ECPoint>]
static final StorageMap whitelistedTokens = new StorageMap(ctx, WHITELISTED_TOKENS_PREFIX); // [hash, max_amount]

//region EVENTS
@DisplayName("FunderAdded")
static Event1Arg<Hash160> funderAdded;
@DisplayName("FunderRemoved")
Expand All @@ -87,8 +88,7 @@ public class GrantSharesTreasury {
static Event3Args<Hash160, Integer, Hash160> tokensReceived;
@DisplayName("WhitelistedTokenMigrated")
static Event2Args<Hash160, Integer> whitelistedTokenMigrated;
@DisplayName("Error")
static Event2Args<String, String> error;
//endregion EVENTS

/**
* Initialises this contract on deployment.
Expand Down Expand Up @@ -300,7 +300,7 @@ public static void setFundersMultiSigThresholdRatio(Integer value) {
abortIfPaused();
abortIfCallerIsNotOwner();
if (value <= 0 || value > 100)
fireErrorAndAbort("Invalid threshold ratio", "setFundersMultiSigThresholdRatio");
Helper.abort("setFundersMultiSigThresholdRatio" + ": " + "Invalid threshold ratio");
Storage.put(ctx, MULTI_SIG_THRESHOLD_KEY, value);
thresholdChanged.fire(value);
}
Expand All @@ -319,11 +319,11 @@ public static void setFundersMultiSigThresholdRatio(Integer value) {
public static void addFunder(Hash160 accountHash, ECPoint[] publicKeys) {
abortIfPaused();
abortIfCallerIsNotOwner();
if (funders.get(accountHash.toByteString()) != null) fireErrorAndAbort("Already a funder", "addFunder");
if (!isValid(accountHash) || accountHash == zero()) fireErrorAndAbort("Invalid funder hash", "addFunder");
if (publicKeys.length == 0) fireErrorAndAbort("List of public keys is empty", "addFunder");
if (funders.get(accountHash.toByteString()) != null) Helper.abort("addFunder" + ": " + "Already a funder");
if (!isValid(accountHash) || accountHash == zero()) Helper.abort("addFunder" + ": " + "Invalid funder hash");
if (publicKeys.length == 0) Helper.abort("addFunder" + ": " + "List of public keys is empty");
for (ECPoint key : publicKeys) {
if (!ECPoint.isValid(key)) fireErrorAndAbort("Invalid public key", "addFunder");
if (!ECPoint.isValid(key)) Helper.abort("addFunder" + ": " + "Invalid public key");
}
funders.put(accountHash.toByteString(), new StdLib().serialize(publicKeys));
funderAdded.fire(accountHash);
Expand All @@ -339,7 +339,7 @@ public static void addFunder(Hash160 accountHash, ECPoint[] publicKeys) {
public static void removeFunder(Hash160 accountHash) {
abortIfPaused();
abortIfCallerIsNotOwner();
if (funders.get(accountHash.toByteString()) == null) fireErrorAndAbort("Not a funder", "removeFunder");
if (funders.get(accountHash.toByteString()) == null) Helper.abort("removeFunder" + ": " + "Not a funder");
funders.delete(accountHash.toByteString());
funderRemoved.fire(accountHash);
}
Expand All @@ -356,8 +356,8 @@ public static void removeFunder(Hash160 accountHash) {
public static void addWhitelistedToken(Hash160 token, int maxFundingAmount) {
abortIfPaused();
abortIfCallerIsNotOwner();
if (!isValid(token) || token == zero()) fireErrorAndAbort("Invalid token hash", "addWhitelistedToken");
if (maxFundingAmount <= 0) fireErrorAndAbort("Invalid max funding amount", "addWhitelistedToken");
if (!isValid(token) || token == zero()) Helper.abort("addWhitelistedToken" + ": " + "Invalid token hash");
if (maxFundingAmount <= 0) Helper.abort("addWhitelistedToken" + ": " + "Invalid max funding amount");
whitelistedTokens.put(token.toByteString(), maxFundingAmount);
whitelistedTokenAdded.fire(token, maxFundingAmount);
}
Expand All @@ -373,7 +373,7 @@ public static void removeWhitelistedToken(Hash160 token) {
abortIfPaused();
abortIfCallerIsNotOwner();
if (whitelistedTokens.get(token.toByteString()) == null)
fireErrorAndAbort("Not a whitelisted token", "removeWhitelistedToken");
Helper.abort("removeWhitelistedToken" + ": " + "Not a whitelisted token");
whitelistedTokens.delete(token.toByteString());
whitelistedTokenRemoved.fire(token);
}
Expand All @@ -392,8 +392,8 @@ public static void releaseTokens(Hash160 tokenContract, Hash160 to, int amount)
abortIfPaused();
abortIfCallerIsNotOwner();
int maxFundingAmount = whitelistedTokens.getIntOrZero(tokenContract.toByteString());
if (maxFundingAmount == 0) fireErrorAndAbort("Token not whitelisted", "releaseTokens");
if (amount > maxFundingAmount) fireErrorAndAbort("Above token's max funding amount", "releaseTokens");
if (maxFundingAmount == 0) Helper.abort("releaseTokens" + ": " + "Token not whitelisted");
if (amount > maxFundingAmount) Helper.abort("releaseTokens" + ": " + "Above token's max funding amount");
Object[] params = new Object[]{Runtime.getExecutingScriptHash(), to, amount, new Object[]{}};
boolean success = (boolean) Contract.call(tokenContract, "transfer", CallFlags.All, params);
if (success) {
Expand All @@ -410,14 +410,14 @@ public static void releaseTokens(Hash160 tokenContract, Hash160 to, int amount)
* paused.
*/
public static void drain() {
if (!isPaused()) fireErrorAndAbort("Contract is not paused", "drain");
if (!isPaused()) Helper.abort("drain" + ": " + "Contract is not paused");
Hash160 fundersMultiAddress = null;
try {
fundersMultiAddress = calcFundersMultiSigAddress();
} catch (Exception e) {
fireErrorAndAbort(e.getMessage(), "drain");
Helper.abort("drain" + ": " + e.getMessage());
}
if (!checkWitness(fundersMultiAddress)) fireErrorAndAbort("Not authorized", "drain");
if (!checkWitness(fundersMultiAddress)) Helper.abort("drain" + ": " + "Not authorized");
Hash160 selfHash = Runtime.getExecutingScriptHash();
Iterator<ByteString> it = whitelistedTokens.find((byte) (RemovePrefix | KeysOnly));
while (it.next()) {
Expand All @@ -442,7 +442,7 @@ public static void voteCommitteeMemberWithLeastVotes() {
abortIfPaused();
ECPoint c = getCommitteeMemberWithLeastVotes();
if (!new NeoToken().vote(Runtime.getExecutingScriptHash(), c))
fireErrorAndAbort("Failed voting on candidate", "voteCommitteeMemberWithLeastVotes");
Helper.abort("voteCommitteeMemberWithLeastVotes" + ": " + "Failed voting on candidate");
voted.fire(c);
}

Expand Down Expand Up @@ -481,15 +481,14 @@ public static void updateContract(ByteString nef, String manifest, Object data)

private static void abortIfCallerIsNotOwner() {
if (Runtime.getCallingScriptHash().toByteString() != Storage.get(getReadOnlyContext(), OWNER_KEY))
fireErrorAndAbort("Not authorised", "abortIfCallerIsNotOwner");
Helper.abort("abortIfCallerIsNotOwner" + ": " + "Not authorised");
}

private static void abortIfPaused() {
if (isPaused()) fireErrorAndAbort("Contract is paused", "abortIfCallerIsNotOwner");
if (isPaused()) Helper.abort("abortIfCallerIsNotOwner" + ": " + "Contract is paused");
}

private static void fireErrorAndAbort(String msg, String method) {
error.fire(msg, method);
Helper.abort();
private static void abortWithMessage(String method, String msg) {
Helper.abort(method + ": " + msg);
}
}
Loading

0 comments on commit 6c8972b

Please sign in to comment.