Skip to content

Commit

Permalink
Merge pull request #228 from ommfinance/fix/prep-status-check
Browse files Browse the repository at this point in the history
Check for prep status
  • Loading branch information
Suyog007 authored Mar 7, 2024
2 parents 0eaa872 + 4398ace commit 2c662bf
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ private BigInteger distributeFeeToValidator(Address lendingPoolCoreAddr,BigInteg

private boolean checkPrepStatus(Address prepAddr){
Map<String, Object> prepDict = call(Map.class,ZERO_SCORE_ADDRESS, "getPRep", prepAddr);
BigInteger status = (BigInteger) prepDict.get("status");
if (!status.equals(BigInteger.ZERO)){
return false;
}
BigInteger jailFlags = (BigInteger) prepDict.get("jailFlags");

return jailFlags == null || jailFlags.signum() == 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,10 @@ public void claimRewards(){
// validator 2 = 90% of 67.5 = 60.75 ICX
tokenFallback();

doReturn(Map.of()).when(spyScore).call(Map.class,ZERO_SCORE_ADDRESS, "getPRep", validator1.getAddress());
doReturn(Map.of("jailFlags",BigInteger.ZERO)).when(spyScore).call(Map.class,ZERO_SCORE_ADDRESS, "getPRep", validator2.getAddress());
doReturn(Map.of("status",BigInteger.ZERO)).when(spyScore).call(Map.class,ZERO_SCORE_ADDRESS, "getPRep", validator1.getAddress());
doReturn(Map.of(
"jailFlags",BigInteger.ZERO,
"status",BigInteger.ZERO)).when(spyScore).call(Map.class,ZERO_SCORE_ADDRESS, "getPRep", validator2.getAddress());

BigInteger calimAmountValidator1 = BigInteger.valueOf(675).multiply(ICX).divide(BigInteger.valueOf(100));

Expand Down Expand Up @@ -183,9 +185,11 @@ void distributeFeeToValidator_withJailedPreps(){
// validator 3 = 20% of 67.5 = 13.5 ICX


doReturn(Map.of()).when(spyScore).call(Map.class,ZERO_SCORE_ADDRESS, "getPRep", validator1.getAddress());
doReturn(Map.of("jailFlags",BigInteger.ZERO)).when(spyScore).call(Map.class,ZERO_SCORE_ADDRESS, "getPRep", validator2.getAddress());
doReturn(Map.of("jailFlags",BigInteger.TEN)).when(spyScore).call(Map.class,ZERO_SCORE_ADDRESS, "getPRep", validator3.getAddress());
doReturn(Map.of("status",BigInteger.ZERO)).when(spyScore).call(Map.class,ZERO_SCORE_ADDRESS, "getPRep", validator1.getAddress());
doReturn(Map.of("jailFlags",BigInteger.ZERO,
"status",BigInteger.ZERO)).when(spyScore).call(Map.class,ZERO_SCORE_ADDRESS, "getPRep", validator2.getAddress());
doReturn(Map.of("jailFlags",BigInteger.TEN,
"status",BigInteger.ZERO)).when(spyScore).call(Map.class,ZERO_SCORE_ADDRESS, "getPRep", validator3.getAddress());


// only 2 validators of omm are in valid Preps of staking
Expand Down Expand Up @@ -223,6 +227,7 @@ void distributeFeeToValidator_withJailedPreps(){
}


@Test
void distributeFee_withClaimRewards(){
tokenFallback();

Expand All @@ -234,6 +239,11 @@ void distributeFee_withClaimRewards(){
assertEquals(BigInteger.ZERO,score.call("getCollectedFee",validator2.getAddress()));
assertEquals(BigInteger.ZERO,score.call("getAccumulatedFee",validator2.getAddress()));


doReturn(Map.of("status",BigInteger.ZERO)).when(spyScore).call(Map.class,ZERO_SCORE_ADDRESS, "getPRep", validator1.getAddress());
doReturn(Map.of("jailFlags",BigInteger.ZERO,
"status",BigInteger.ZERO)).when(spyScore).call(Map.class,ZERO_SCORE_ADDRESS, "getPRep", validator2.getAddress());

score.invoke(validator2,"claimRewards",validator2.getAddress());

assertEquals(BigInteger.ZERO,score.call("getCollectedFee",claimAddress.getAddress()));
Expand Down Expand Up @@ -295,4 +305,43 @@ void check_claimableFee(){

}

@Test
void distributeFeeToValidator_withUnregisteredPrep(){
// omm delegation has validator who was active validator once but
// now the validator is unregistered

tokenFallback();
// validator = 67.5 ICX
// validator1 = 100% of 67.5 = 67.5 ICX


doReturn(Map.of("status",BigInteger.ONE)).when(spyScore).call(Map.class,ZERO_SCORE_ADDRESS, "getPRep",
validator1.getAddress());


// the only validator of omm is unregistered prep
doReturn(Map.of(
validator1.getAddress().toString(),BigInteger.valueOf(100).multiply(ICX)
)).when(spyScore).call(eq(Map.class),any(Contracts.class),eq("getActualUserDelegationPercentage"),any());

contextMock.when(mockCaller()).thenReturn(validator1.getAddress());

// fee will be disbursed here
score.invoke(validator1,"claimRewards",validator1.getAddress());



Address daoFund = MOCK_CONTRACT_ADDRESS.get(Contracts.DAO_FUND).getAddress();
// daoFund = daoFund + validatorRewards
BigInteger val = BigInteger.valueOf(90).multiply(ICX);
assertEquals(val,score.call("getCollectedFee",daoFund));


assertEquals(BigInteger.ZERO,score.call("getCollectedFee",validator1.getAddress()));

verify(spyScore).FeeDisbursed(BigInteger.valueOf(100).multiply(ICX));


}

}

0 comments on commit 2c662bf

Please sign in to comment.