2020-03-02 06:52:43 -08:00
|
|
|
package keeper_test
|
2019-08-08 12:51:18 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-08-14 10:58:53 -07:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2019-08-08 12:51:18 -07:00
|
|
|
|
2020-03-02 06:52:43 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
2019-08-08 12:51:18 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDeposits(t *testing.T) {
|
2021-08-16 17:52:06 -07:00
|
|
|
app := simapp.Setup(t, false)
|
2020-08-14 10:58:53 -07:00
|
|
|
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
2020-03-02 06:52:43 -08:00
|
|
|
|
|
|
|
TestAddrs := simapp.AddTestAddrsIncremental(app, ctx, 2, sdk.NewInt(10000000))
|
2019-08-08 12:51:18 -07:00
|
|
|
|
|
|
|
tp := TestProposal
|
2020-03-02 06:52:43 -08:00
|
|
|
proposal, err := app.GovKeeper.SubmitProposal(ctx, tp)
|
2019-08-08 12:51:18 -07:00
|
|
|
require.NoError(t, err)
|
2020-08-14 01:55:54 -07:00
|
|
|
proposalID := proposal.ProposalId
|
2019-08-08 12:51:18 -07:00
|
|
|
|
2021-01-28 05:54:39 -08:00
|
|
|
fourStake := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, app.StakingKeeper.TokensFromConsensusPower(ctx, 4)))
|
|
|
|
fiveStake := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, app.StakingKeeper.TokensFromConsensusPower(ctx, 5)))
|
2019-08-08 12:51:18 -07:00
|
|
|
|
2020-03-02 06:52:43 -08:00
|
|
|
addr0Initial := app.BankKeeper.GetAllBalances(ctx, TestAddrs[0])
|
|
|
|
addr1Initial := app.BankKeeper.GetAllBalances(ctx, TestAddrs[1])
|
2019-08-08 12:51:18 -07:00
|
|
|
|
|
|
|
require.True(t, proposal.TotalDeposit.IsEqual(sdk.NewCoins()))
|
|
|
|
|
|
|
|
// Check no deposits at beginning
|
2020-03-02 06:52:43 -08:00
|
|
|
deposit, found := app.GovKeeper.GetDeposit(ctx, proposalID, TestAddrs[1])
|
2019-08-08 12:51:18 -07:00
|
|
|
require.False(t, found)
|
2020-03-02 06:52:43 -08:00
|
|
|
proposal, ok := app.GovKeeper.GetProposal(ctx, proposalID)
|
2019-08-08 12:51:18 -07:00
|
|
|
require.True(t, ok)
|
|
|
|
require.True(t, proposal.VotingStartTime.Equal(time.Time{}))
|
|
|
|
|
|
|
|
// Check first deposit
|
2020-03-02 06:52:43 -08:00
|
|
|
votingStarted, err := app.GovKeeper.AddDeposit(ctx, proposalID, TestAddrs[0], fourStake)
|
2019-08-08 12:51:18 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, votingStarted)
|
2020-03-02 06:52:43 -08:00
|
|
|
deposit, found = app.GovKeeper.GetDeposit(ctx, proposalID, TestAddrs[0])
|
2019-08-08 12:51:18 -07:00
|
|
|
require.True(t, found)
|
|
|
|
require.Equal(t, fourStake, deposit.Amount)
|
2020-09-25 03:25:37 -07:00
|
|
|
require.Equal(t, TestAddrs[0].String(), deposit.Depositor)
|
2020-03-02 06:52:43 -08:00
|
|
|
proposal, ok = app.GovKeeper.GetProposal(ctx, proposalID)
|
2019-08-08 12:51:18 -07:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, fourStake, proposal.TotalDeposit)
|
2020-03-02 06:52:43 -08:00
|
|
|
require.Equal(t, addr0Initial.Sub(fourStake), app.BankKeeper.GetAllBalances(ctx, TestAddrs[0]))
|
2019-08-08 12:51:18 -07:00
|
|
|
|
|
|
|
// Check a second deposit from same address
|
2020-03-02 06:52:43 -08:00
|
|
|
votingStarted, err = app.GovKeeper.AddDeposit(ctx, proposalID, TestAddrs[0], fiveStake)
|
2019-08-08 12:51:18 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, votingStarted)
|
2020-03-02 06:52:43 -08:00
|
|
|
deposit, found = app.GovKeeper.GetDeposit(ctx, proposalID, TestAddrs[0])
|
2019-08-08 12:51:18 -07:00
|
|
|
require.True(t, found)
|
2020-01-03 12:44:53 -08:00
|
|
|
require.Equal(t, fourStake.Add(fiveStake...), deposit.Amount)
|
2020-09-25 03:25:37 -07:00
|
|
|
require.Equal(t, TestAddrs[0].String(), deposit.Depositor)
|
2020-03-02 06:52:43 -08:00
|
|
|
proposal, ok = app.GovKeeper.GetProposal(ctx, proposalID)
|
2019-08-08 12:51:18 -07:00
|
|
|
require.True(t, ok)
|
2020-01-03 12:44:53 -08:00
|
|
|
require.Equal(t, fourStake.Add(fiveStake...), proposal.TotalDeposit)
|
2020-03-02 06:52:43 -08:00
|
|
|
require.Equal(t, addr0Initial.Sub(fourStake).Sub(fiveStake), app.BankKeeper.GetAllBalances(ctx, TestAddrs[0]))
|
2019-08-08 12:51:18 -07:00
|
|
|
|
|
|
|
// Check third deposit from a new address
|
2020-03-02 06:52:43 -08:00
|
|
|
votingStarted, err = app.GovKeeper.AddDeposit(ctx, proposalID, TestAddrs[1], fourStake)
|
2019-08-08 12:51:18 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, votingStarted)
|
2020-03-02 06:52:43 -08:00
|
|
|
deposit, found = app.GovKeeper.GetDeposit(ctx, proposalID, TestAddrs[1])
|
2019-08-08 12:51:18 -07:00
|
|
|
require.True(t, found)
|
2020-09-25 03:25:37 -07:00
|
|
|
require.Equal(t, TestAddrs[1].String(), deposit.Depositor)
|
2019-08-08 12:51:18 -07:00
|
|
|
require.Equal(t, fourStake, deposit.Amount)
|
2020-03-02 06:52:43 -08:00
|
|
|
proposal, ok = app.GovKeeper.GetProposal(ctx, proposalID)
|
2019-08-08 12:51:18 -07:00
|
|
|
require.True(t, ok)
|
2020-01-03 12:44:53 -08:00
|
|
|
require.Equal(t, fourStake.Add(fiveStake...).Add(fourStake...), proposal.TotalDeposit)
|
2020-03-02 06:52:43 -08:00
|
|
|
require.Equal(t, addr1Initial.Sub(fourStake), app.BankKeeper.GetAllBalances(ctx, TestAddrs[1]))
|
2019-08-08 12:51:18 -07:00
|
|
|
|
|
|
|
// Check that proposal moved to voting period
|
2020-03-02 06:52:43 -08:00
|
|
|
proposal, ok = app.GovKeeper.GetProposal(ctx, proposalID)
|
2019-08-08 12:51:18 -07:00
|
|
|
require.True(t, ok)
|
|
|
|
require.True(t, proposal.VotingStartTime.Equal(ctx.BlockHeader().Time))
|
|
|
|
|
|
|
|
// Test deposit iterator
|
|
|
|
// NOTE order of deposits is determined by the addresses
|
2020-03-02 06:52:43 -08:00
|
|
|
deposits := app.GovKeeper.GetAllDeposits(ctx)
|
2019-08-08 12:51:18 -07:00
|
|
|
require.Len(t, deposits, 2)
|
2020-03-02 06:52:43 -08:00
|
|
|
require.Equal(t, deposits, app.GovKeeper.GetDeposits(ctx, proposalID))
|
2020-09-25 03:25:37 -07:00
|
|
|
require.Equal(t, TestAddrs[0].String(), deposits[0].Depositor)
|
2020-01-03 12:44:53 -08:00
|
|
|
require.Equal(t, fourStake.Add(fiveStake...), deposits[0].Amount)
|
2020-09-25 03:25:37 -07:00
|
|
|
require.Equal(t, TestAddrs[1].String(), deposits[1].Depositor)
|
2019-08-08 12:51:18 -07:00
|
|
|
require.Equal(t, fourStake, deposits[1].Amount)
|
|
|
|
|
|
|
|
// Test Refund Deposits
|
2020-03-02 06:52:43 -08:00
|
|
|
deposit, found = app.GovKeeper.GetDeposit(ctx, proposalID, TestAddrs[1])
|
2019-08-08 12:51:18 -07:00
|
|
|
require.True(t, found)
|
|
|
|
require.Equal(t, fourStake, deposit.Amount)
|
2021-07-30 10:37:38 -07:00
|
|
|
app.GovKeeper.RefundAndDeleteDeposits(ctx, proposalID)
|
2020-03-02 06:52:43 -08:00
|
|
|
deposit, found = app.GovKeeper.GetDeposit(ctx, proposalID, TestAddrs[1])
|
2019-08-08 12:51:18 -07:00
|
|
|
require.False(t, found)
|
2020-03-02 06:52:43 -08:00
|
|
|
require.Equal(t, addr0Initial, app.BankKeeper.GetAllBalances(ctx, TestAddrs[0]))
|
|
|
|
require.Equal(t, addr1Initial, app.BankKeeper.GetAllBalances(ctx, TestAddrs[1]))
|
2021-05-07 05:00:19 -07:00
|
|
|
|
2021-07-30 10:37:38 -07:00
|
|
|
// Test delete and burn deposits
|
|
|
|
proposal, err = app.GovKeeper.SubmitProposal(ctx, tp)
|
|
|
|
require.NoError(t, err)
|
|
|
|
proposalID = proposal.ProposalId
|
2021-05-07 05:00:19 -07:00
|
|
|
_, err = app.GovKeeper.AddDeposit(ctx, proposalID, TestAddrs[0], fourStake)
|
|
|
|
require.NoError(t, err)
|
2021-07-30 10:37:38 -07:00
|
|
|
app.GovKeeper.DeleteAndBurnDeposits(ctx, proposalID)
|
2021-05-07 05:00:19 -07:00
|
|
|
deposits = app.GovKeeper.GetDeposits(ctx, proposalID)
|
|
|
|
require.Len(t, deposits, 0)
|
2021-07-30 10:37:38 -07:00
|
|
|
require.Equal(t, addr0Initial.Sub(fourStake), app.BankKeeper.GetAllBalances(ctx, TestAddrs[0]))
|
2019-08-08 12:51:18 -07:00
|
|
|
}
|