Skip to content

Commit

Permalink
fix payout
Browse files Browse the repository at this point in the history
  • Loading branch information
akremstudy committed Dec 9, 2023
1 parent f1c0989 commit 15e284f
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 68 deletions.
142 changes: 105 additions & 37 deletions tests/integration/dispute_keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (s *IntegrationTestSuite) TestVotingOnDispute() {
DisputeCategory: types.Warning,
})
s.NoError(err)
s.Equal(uint64(1), s.disputekeeper.GetDisputeCount(s.ctx))
s.Equal(uint64(2), s.disputekeeper.GetDisputeCount(s.ctx))
s.Equal(1, len(s.disputekeeper.GetOpenDisputeIds(s.ctx).Ids))

// check validator wasn't slashed/jailed
Expand All @@ -53,7 +53,7 @@ func (s *IntegrationTestSuite) TestVotingOnDispute() {
// Add dispute fee to complete the fee and jail/slash validator
_, err = msgServer.AddFeeToDispute(s.ctx, &types.MsgAddFeeToDispute{
Creator: addrs[1].String(),
DisputeId: 0,
DisputeId: 1,
Amount: sdk.NewCoin(s.denom, sdk.NewInt(5_000_000)),
})
s.NoError(err)
Expand All @@ -63,18 +63,18 @@ func (s *IntegrationTestSuite) TestVotingOnDispute() {
s.True(val.IsJailed())
// check validator was slashed 1% of tokens
s.Equal(val.GetBondedTokens(), bondedTokensBefore.Sub(bondedTokensBefore.Mul(math.NewInt(1)).Quo(math.NewInt(100))))
dispute := s.disputekeeper.GetDisputeById(s.ctx, 0)
dispute := s.disputekeeper.GetDisputeById(s.ctx, 1)
s.Equal(types.Voting, dispute.DisputeStatus)
// vote on dispute
_, err = msgServer.Vote(s.ctx, &types.MsgVote{
Voter: Addr.String(),
Id: 0,
Id: 1,
Vote: types.VoteEnum_VOTE_SUPPORT,
})
s.NoError(err)
voterV := s.disputekeeper.GetVoterVote(s.ctx, Addr.String(), 0)
voterV := s.disputekeeper.GetVoterVote(s.ctx, Addr.String(), 1)
s.Equal(types.VoteEnum_VOTE_SUPPORT, voterV.Vote)
v := s.disputekeeper.GetVote(s.ctx, 0)
v := s.disputekeeper.GetVote(s.ctx, 1)
s.Equal(v.VoteResult, types.VoteResult_NO_TALLY)
s.Equal(v.Voters, []string{Addr.String()})
}
Expand Down Expand Up @@ -153,22 +153,22 @@ func (s *IntegrationTestSuite) TestExecuteVoteInvalid() {
votes := []types.MsgVote{
{
Voter: reporterAddr,
Id: 0,
Id: 1,
Vote: types.VoteEnum_VOTE_INVALID,
},
{
Voter: disputerAddr,
Id: 0,
Id: 1,
Vote: types.VoteEnum_VOTE_INVALID,
},
{
Voter: addrs[2].String(),
Id: 0,
Id: 1,
Vote: types.VoteEnum_VOTE_INVALID,
},
{
Voter: addrs[3].String(),
Id: 0,
Id: 1,
Vote: types.VoteEnum_VOTE_INVALID,
},
}
Expand All @@ -186,7 +186,7 @@ func (s *IntegrationTestSuite) TestExecuteVoteInvalid() {
s.True(s.stakingKeeper.Validator(s.ctx, valAddrs[0]).GetBondedTokens().GT(valTknBeforeExecuteVote))
// dispute fee returned so balance should be the same as before paying fee
disputerBalanceAfterExecuteVote := s.bankKeeper.GetBalance(s.ctx, disputerAcc, s.denom)
voters := s.disputekeeper.GetVote(s.ctx, 0).Voters
voters := s.disputekeeper.GetVote(s.ctx, 1).Voters
rewards := s.disputekeeper.CalculateVoterShare(s.ctx, voters, burnAmount.QuoRaw(2))
voterReward := rewards[disputerAddr]
// add dispute fee returned minus burn amount plus the voter reward
Expand Down Expand Up @@ -237,7 +237,7 @@ func (s *IntegrationTestSuite) TestExecuteVoteNoQuorumInvalid() {
vote := []types.MsgVote{
{
Voter: addrs[0].String(),
Id: 0,
Id: 1,
Vote: types.VoteEnum_VOTE_INVALID,
},
}
Expand All @@ -246,14 +246,14 @@ func (s *IntegrationTestSuite) TestExecuteVoteNoQuorumInvalid() {
s.NoError(err)

ctx := s.ctx.WithBlockTime(s.ctx.BlockTime().Add(keeper.TWO_DAYS + 1))
s.disputekeeper.TallyVote(ctx, 0)
s.disputekeeper.TallyVote(ctx, 1)

reporter = s.stakingKeeper.Validator(ctx, valAddrs[0])
bond := reporter.GetBondedTokens()
// execute vote
s.disputekeeper.ExecuteVotes(ctx, []uint64{0})

voteInfo := s.disputekeeper.GetVote(ctx, 0)
voteInfo := s.disputekeeper.GetVote(ctx, 1)
s.Equal(types.VoteResult_NO_QUORUM_MAJORITY_INVALID, voteInfo.VoteResult)
s.True(s.stakingKeeper.Validator(ctx, valAddrs[0]).GetBondedTokens().Equal(bond))
}
Expand Down Expand Up @@ -294,22 +294,22 @@ func (s *IntegrationTestSuite) TestExecuteVoteSupport() {
votes := []types.MsgVote{
{
Voter: addrs[0].String(),
Id: 0,
Id: 1,
Vote: types.VoteEnum_VOTE_SUPPORT,
},
{
Voter: addrs[1].String(),
Id: 0,
Id: 1,
Vote: types.VoteEnum_VOTE_SUPPORT,
},
{
Voter: addrs[2].String(),
Id: 0,
Id: 1,
Vote: types.VoteEnum_VOTE_SUPPORT,
},
{
Voter: addrs[3].String(),
Id: 0,
Id: 1,
Vote: types.VoteEnum_VOTE_SUPPORT,
},
}
Expand All @@ -331,7 +331,7 @@ func (s *IntegrationTestSuite) TestExecuteVoteSupport() {
s.bankKeeper.GetBalance(s.ctx, addrs[2], s.denom),
s.bankKeeper.GetBalance(s.ctx, addrs[3], s.denom),
}
voters := s.disputekeeper.GetVote(s.ctx, 0).Voters
voters := s.disputekeeper.GetVote(s.ctx, 1).Voters
votersReward := s.disputekeeper.CalculateVoterShare(s.ctx, voters, twoPercentBurn)
for i := range votersBalanceBefore {
votersBalanceBefore[i].Amount = votersBalanceBefore[i].Amount.Add(votersReward[addrs[i].String()])
Expand Down Expand Up @@ -375,22 +375,22 @@ func (s *IntegrationTestSuite) TestExecuteVoteAgainst() {
votes := []types.MsgVote{
{
Voter: addrs[0].String(),
Id: 0,
Id: 1,
Vote: types.VoteEnum_VOTE_AGAINST,
},
{
Voter: addrs[1].String(),
Id: 0,
Id: 1,
Vote: types.VoteEnum_VOTE_AGAINST,
},
{
Voter: addrs[2].String(),
Id: 0,
Id: 1,
Vote: types.VoteEnum_VOTE_AGAINST,
},
{
Voter: addrs[3].String(),
Id: 0,
Id: 1,
Vote: types.VoteEnum_VOTE_AGAINST,
},
}
Expand All @@ -399,9 +399,9 @@ func (s *IntegrationTestSuite) TestExecuteVoteAgainst() {
s.NoError(err)
}
// tally vote
s.disputekeeper.TallyVote(s.ctx, 0)
s.disputekeeper.TallyVote(s.ctx, 1)
// execute vote
s.disputekeeper.ExecuteVote(s.ctx, 0)
s.disputekeeper.ExecuteVote(s.ctx, 1)
reporterAfterDispute := s.stakingKeeper.Validator(s.ctx, valAddrs[0])
s.Equal(reporterBefore.GetBondedTokens().Add(disputeFeeMinusBurn), reporterAfterDispute.GetBondedTokens())

Expand All @@ -411,7 +411,7 @@ func (s *IntegrationTestSuite) TestExecuteVoteAgainst() {
s.bankKeeper.GetBalance(s.ctx, addrs[2], s.denom),
s.bankKeeper.GetBalance(s.ctx, addrs[3], s.denom),
}
voters := s.disputekeeper.GetVote(s.ctx, 0).Voters
voters := s.disputekeeper.GetVote(s.ctx, 1).Voters
votersReward := s.disputekeeper.CalculateVoterShare(s.ctx, voters, twoPercentBurn)
for i := range votersBalanceBefore {
votersBalanceBefore[i].Amount = votersBalanceBefore[i].Amount.Add(votersReward[addrs[i].String()])
Expand All @@ -422,6 +422,23 @@ func (s *IntegrationTestSuite) TestExecuteVoteAgainst() {
func (s *IntegrationTestSuite) TestDisputeMultipleRounds() {
_, msgServer := s.disputeKeeper()
addrs, valAddrs := s.createValidators([]int64{100, 200, 300})
accBals1 := s.bankKeeper.GetAccountsBalances(s.ctx)
moduleAccs := s.ModuleAccs()
for _, acc := range accBals1 {
if acc.Address == addrs[0].String() {
s.Equal(acc.Coins.AmountOf(s.denom), sdk.NewInt(900_000_000))
}
if acc.Address == addrs[1].String() {
s.Equal(acc.Coins.AmountOf(s.denom), sdk.NewInt(800_000_000))
}
if acc.Address == addrs[2].String() {
s.Equal(acc.Coins.AmountOf(s.denom), sdk.NewInt(700_000_000))
}
if acc.Address == moduleAccs.staking.GetAddress().String() {
s.Equal(acc.Coins.AmountOf(s.denom), sdk.NewInt(601_000_000))
}
}

reporter := s.stakingKeeper.Validator(s.ctx, valAddrs[0])
reporterStakeBefore := reporter.GetBondedTokens()
report := types.MicroReport{
Expand All @@ -443,6 +460,24 @@ func (s *IntegrationTestSuite) TestDisputeMultipleRounds() {
// Propose dispute pay half of the fee from account
_, err := msgServer.ProposeDispute(s.ctx, &dispute)
s.NoError(err)
accBals2 := s.bankKeeper.GetAccountsBalances(s.ctx)
for _, acc := range accBals2 {
if acc.Address == addrs[0].String() {
s.Equal(acc.Coins.AmountOf(s.denom), sdk.NewInt(900_000_000))
}
if acc.Address == addrs[1].String() {
s.Equal(acc.Coins.AmountOf(s.denom), sdk.NewInt(799_000_000))
}
if acc.Address == addrs[2].String() {
s.Equal(acc.Coins.AmountOf(s.denom), sdk.NewInt(700_000_000))
}
if acc.Address == moduleAccs.staking.GetAddress().String() {
s.Equal(acc.Coins.AmountOf(s.denom), sdk.NewInt(600_000_000))
}
if acc.Address == moduleAccs.dispute.GetAddress().String() {
s.Equal(acc.Coins.AmountOf(s.denom), disputeFee.MulRaw(2)) // disputeFee + slashAmount
}
}
balanceAfter := s.bankKeeper.GetBalance(s.ctx, addrs[1], s.denom)
s.True(balanceBefore.Amount.Sub(disputeFee).Equal(balanceAfter.Amount))
// check reporter stake
Expand All @@ -455,7 +490,7 @@ func (s *IntegrationTestSuite) TestDisputeMultipleRounds() {
votes := []types.MsgVote{
{
Voter: addrs[0].String(),
Id: 0,
Id: 1,
Vote: types.VoteEnum_VOTE_INVALID,
},
}
Expand All @@ -467,7 +502,7 @@ func (s *IntegrationTestSuite) TestDisputeMultipleRounds() {
header = tmproto.Header{Height: s.app.LastBlockHeight() + 1, AppHash: s.app.LastCommitID().Hash, Time: s.ctx.BlockTime()}
s.app.BeginBlock(abci.RequestBeginBlock{Header: header})
_, err = msgServer.ProposeDispute(s.ctx, &dispute)
s.Equal(err.Error(), "can't start a new round for this dispute 0; dispute status DISPUTE_STATUS_VOTING")
s.Equal(err.Error(), "can't start a new round for this dispute 1; dispute status DISPUTE_STATUS_VOTING")
// check reporter stake
s.True(s.stakingKeeper.Validator(s.ctx, valAddrs[0]).GetBondedTokens().LT(reporterStakeBefore))
s.Equal(s.stakingKeeper.Validator(s.ctx, valAddrs[0]).GetBondedTokens(), reporterStakeBefore.Sub(disputeFee))
Expand All @@ -489,7 +524,7 @@ func (s *IntegrationTestSuite) TestDisputeMultipleRounds() {
votes = []types.MsgVote{
{
Voter: addrs[0].String(),
Id: 1,
Id: 2,
Vote: types.VoteEnum_VOTE_INVALID,
},
}
Expand All @@ -498,7 +533,7 @@ func (s *IntegrationTestSuite) TestDisputeMultipleRounds() {
s.NoError(err)
}
_, err = msgServer.ProposeDispute(s.ctx, &dispute)
s.Equal(err.Error(), "can't start a new round for this dispute 1; dispute status DISPUTE_STATUS_VOTING") //fails since hasn't been tallied and executed
s.Equal(err.Error(), "can't start a new round for this dispute 2; dispute status DISPUTE_STATUS_VOTING") //fails since hasn't been tallied and executed

s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(keeper.TWO_DAYS))
header = tmproto.Header{Height: s.app.LastBlockHeight() + 1, AppHash: s.app.LastCommitID().Hash, Time: s.ctx.BlockTime().Add(1)}
Expand All @@ -517,7 +552,7 @@ func (s *IntegrationTestSuite) TestDisputeMultipleRounds() {
votes = []types.MsgVote{
{
Voter: addrs[0].String(),
Id: 2,
Id: 3,
Vote: types.VoteEnum_VOTE_INVALID,
},
}
Expand All @@ -544,7 +579,7 @@ func (s *IntegrationTestSuite) TestDisputeMultipleRounds() {
votes = []types.MsgVote{
{
Voter: addrs[0].String(),
Id: 3,
Id: 4,
Vote: types.VoteEnum_VOTE_INVALID,
},
}
Expand All @@ -553,7 +588,7 @@ func (s *IntegrationTestSuite) TestDisputeMultipleRounds() {
s.NoError(err)
}
_, err = msgServer.ProposeDispute(s.ctx, &dispute)
s.Equal(err.Error(), "can't start a new round for this dispute 3; dispute status DISPUTE_STATUS_VOTING")
s.Equal(err.Error(), "can't start a new round for this dispute 4; dispute status DISPUTE_STATUS_VOTING")
s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(keeper.TWO_DAYS))
header = tmproto.Header{Height: s.app.LastBlockHeight() + 1, AppHash: s.app.LastCommitID().Hash, Time: s.ctx.BlockTime().Add(1)}
s.app.BeginBlock(abci.RequestBeginBlock{Header: header})
Expand All @@ -571,7 +606,7 @@ func (s *IntegrationTestSuite) TestDisputeMultipleRounds() {
votes = []types.MsgVote{
{
Voter: addrs[0].String(),
Id: 4,
Id: 5,
Vote: types.VoteEnum_VOTE_INVALID,
},
}
Expand All @@ -580,7 +615,7 @@ func (s *IntegrationTestSuite) TestDisputeMultipleRounds() {
s.NoError(err)
}
_, err = msgServer.ProposeDispute(s.ctx, &dispute)
s.Equal(err.Error(), "can't start a new round for this dispute 4; dispute status DISPUTE_STATUS_VOTING") //fails since hasn't been tallied and executed
s.Equal(err.Error(), "can't start a new round for this dispute 5; dispute status DISPUTE_STATUS_VOTING") //fails since hasn't been tallied and executed
// forward time to end vote
s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(keeper.TWO_DAYS))
header = tmproto.Header{Height: s.app.LastBlockHeight() + 1, AppHash: s.app.LastCommitID().Hash, Time: s.ctx.BlockTime().Add(1)}
Expand All @@ -598,6 +633,24 @@ func (s *IntegrationTestSuite) TestDisputeMultipleRounds() {
balanceBefore = s.bankKeeper.GetBalance(s.ctx, addrs[1], s.denom)
_, err = msgServer.ProposeDispute(s.ctx, &dispute)
s.NoError(err)
accBals3 := s.bankKeeper.GetAccountsBalances(s.ctx)
for _, acc := range accBals3 {
if acc.Address == addrs[0].String() {
s.Equal(acc.Coins.AmountOf(s.denom), sdk.NewInt(900_000_000))
}
if acc.Address == addrs[1].String() {
s.Equal(acc.Coins.AmountOf(s.denom), sdk.NewInt(795_500_000))
}
if acc.Address == addrs[2].String() {
s.Equal(acc.Coins.AmountOf(s.denom), sdk.NewInt(700_000_000))
}
if acc.Address == moduleAccs.staking.GetAddress().String() {
s.Equal(acc.Coins.AmountOf(s.denom), sdk.NewInt(600_000_000))
}
if acc.Address == moduleAccs.dispute.GetAddress().String() {
s.Equal(acc.Coins.AmountOf(s.denom), sdk.NewInt(5_500_000)) // disputeFee + slashAmount + round 1(100000) + round 2(200000) + round 3(400000) + round 4(800000) + round 5(1000000) + round 6(1000000)
}
}
balanceAfter = s.bankKeeper.GetBalance(s.ctx, addrs[1], s.denom)
s.Equal(balanceBefore.Sub(sdk.NewCoin(s.denom, disputeFee)), balanceAfter)
s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(keeper.TWO_DAYS))
Expand All @@ -614,6 +667,21 @@ func (s *IntegrationTestSuite) TestDisputeMultipleRounds() {
s.app.BeginBlock(abci.RequestBeginBlock{Header: header})
// check reporter stake, stake should be restored due to invalid vote final result
s.Equal(s.stakingKeeper.Validator(s.ctx, valAddrs[0]).GetBondedTokens(), reporterStakeBefore)
accBals4 := s.bankKeeper.GetAccountsBalances(s.ctx)
for _, acc := range accBals4 {
if acc.Address == addrs[0].String() {
s.Equal(acc.Coins.AmountOf(s.denom), sdk.NewInt(902_275_000)) // voter reward half the total burn Amount(the 5%)
}
if acc.Address == addrs[1].String() {
s.Equal(acc.Coins.AmountOf(s.denom), sdk.NewInt(795_450_000))
}
if acc.Address == addrs[2].String() {
s.Equal(acc.Coins.AmountOf(s.denom), sdk.NewInt(700_000_000))
}
if acc.Address == moduleAccs.staking.GetAddress().String() {
s.Equal(acc.Coins.AmountOf(s.denom), sdk.NewInt(601_000_000)) // stake restored
}
}
}

func (s *IntegrationTestSuite) TestNoQorumSingleRound() {
Expand Down Expand Up @@ -643,12 +711,12 @@ func (s *IntegrationTestSuite) TestNoQorumSingleRound() {
votes := []types.MsgVote{
{
Voter: addrs[0].String(),
Id: 0,
Id: 1,
Vote: types.VoteEnum_VOTE_INVALID,
},
{
Voter: addrs[1].String(),
Id: 0,
Id: 1,
Vote: types.VoteEnum_VOTE_INVALID,
},
}
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,18 @@ func (s *IntegrationTestSuite) addTestAddrs(accNum int, accAmt math.Int, strateg

return testAddrs
}

type ModuleAccs struct {
staking authtypes.AccountI
dispute authtypes.AccountI
}

func (s *IntegrationTestSuite) ModuleAccs() ModuleAccs {
return ModuleAccs{
staking: s.accountKeeper.GetModuleAccount(s.ctx, "bonded_tokens_pool"),
dispute: s.accountKeeper.GetModuleAccount(s.ctx, "dispute"),
}
}
func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(IntegrationTestSuite))
}
Loading

0 comments on commit 15e284f

Please sign in to comment.