2018-06-21 17:19:14 -07:00
|
|
|
package gov
|
|
|
|
|
|
|
|
import (
|
2019-04-30 09:31:38 -07:00
|
|
|
"strings"
|
2018-06-21 17:19:14 -07:00
|
|
|
"testing"
|
2018-09-20 07:53:52 -07:00
|
|
|
"time"
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2018-06-29 18:10:15 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2018-06-28 17:54:47 -07:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2018-06-21 17:19:14 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetSetProposal(t *testing.T) {
|
2019-04-30 09:31:38 -07:00
|
|
|
input := getMockApp(t, 0, GenesisState{}, nil)
|
2019-03-07 21:52:19 -08:00
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
header := abci.Header{Height: input.mApp.LastBlockHeight() + 1}
|
|
|
|
input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header})
|
2019-03-07 21:52:19 -08:00
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
ctx := input.mApp.BaseApp.NewContext(false, abci.Header{})
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2019-03-15 09:47:47 -07:00
|
|
|
tp := testProposal()
|
2019-04-30 09:31:38 -07:00
|
|
|
proposal, err := input.keeper.SubmitProposal(ctx, tp)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
proposalID := proposal.ProposalID
|
2019-04-30 09:31:38 -07:00
|
|
|
input.keeper.SetProposal(ctx, proposal)
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
gotProposal, ok := input.keeper.GetProposal(ctx, proposalID)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.True(t, ok)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, ProposalEqual(proposal, gotProposal))
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementProposalNumber(t *testing.T) {
|
2019-04-30 09:31:38 -07:00
|
|
|
input := getMockApp(t, 0, GenesisState{}, nil)
|
2019-03-07 21:52:19 -08:00
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
header := abci.Header{Height: input.mApp.LastBlockHeight() + 1}
|
|
|
|
input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header})
|
2019-03-07 21:52:19 -08:00
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
ctx := input.mApp.BaseApp.NewContext(false, abci.Header{})
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2019-03-15 09:47:47 -07:00
|
|
|
tp := testProposal()
|
2019-04-30 09:31:38 -07:00
|
|
|
input.keeper.SubmitProposal(ctx, tp)
|
|
|
|
input.keeper.SubmitProposal(ctx, tp)
|
|
|
|
input.keeper.SubmitProposal(ctx, tp)
|
|
|
|
input.keeper.SubmitProposal(ctx, tp)
|
|
|
|
input.keeper.SubmitProposal(ctx, tp)
|
|
|
|
proposal6, err := input.keeper.SubmitProposal(ctx, tp)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.NoError(t, err)
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2019-03-15 09:47:47 -07:00
|
|
|
require.Equal(t, uint64(6), proposal6.ProposalID)
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestActivateVotingPeriod(t *testing.T) {
|
2019-04-30 09:31:38 -07:00
|
|
|
input := getMockApp(t, 0, GenesisState{}, nil)
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
header := abci.Header{Height: input.mApp.LastBlockHeight() + 1}
|
|
|
|
input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header})
|
2019-03-07 21:52:19 -08:00
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
ctx := input.mApp.BaseApp.NewContext(false, abci.Header{})
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2019-03-15 09:47:47 -07:00
|
|
|
tp := testProposal()
|
2019-04-30 09:31:38 -07:00
|
|
|
proposal, err := input.keeper.SubmitProposal(ctx, tp)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.True(t, proposal.VotingStartTime.Equal(time.Time{}))
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
input.keeper.activateVotingPeriod(ctx, proposal)
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2019-03-15 09:47:47 -07:00
|
|
|
require.True(t, proposal.VotingStartTime.Equal(ctx.BlockHeader().Time))
|
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
proposal, ok := input.keeper.GetProposal(ctx, proposal.ProposalID)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.True(t, ok)
|
2018-11-06 23:33:18 -08:00
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
activeIterator := input.keeper.ActiveProposalQueueIterator(ctx, proposal.VotingEndTime)
|
2018-11-06 23:33:18 -08:00
|
|
|
require.True(t, activeIterator.Valid())
|
|
|
|
var proposalID uint64
|
2019-04-30 09:31:38 -07:00
|
|
|
input.keeper.cdc.UnmarshalBinaryLengthPrefixed(activeIterator.Value(), &proposalID)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.Equal(t, proposalID, proposal.ProposalID)
|
2018-11-06 23:33:18 -08:00
|
|
|
activeIterator.Close()
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeposits(t *testing.T) {
|
2019-04-30 09:31:38 -07:00
|
|
|
input := getMockApp(t, 2, GenesisState{}, nil)
|
2019-06-28 13:11:27 -07:00
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
SortAddresses(input.addrs)
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
header := abci.Header{Height: input.mApp.LastBlockHeight() + 1}
|
|
|
|
input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header})
|
2019-03-07 21:52:19 -08:00
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
ctx := input.mApp.BaseApp.NewContext(false, abci.Header{})
|
2019-03-15 09:47:47 -07:00
|
|
|
|
|
|
|
tp := testProposal()
|
2019-04-30 09:31:38 -07:00
|
|
|
proposal, err := input.keeper.SubmitProposal(ctx, tp)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
proposalID := proposal.ProposalID
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2019-06-12 08:57:47 -07:00
|
|
|
fourStake := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromConsensusPower(4)))
|
|
|
|
fiveStake := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromConsensusPower(5)))
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2019-06-28 13:11:27 -07:00
|
|
|
addr0Initial := input.mApp.AccountKeeper.GetAccount(ctx, input.addrs[0]).GetCoins()
|
|
|
|
addr1Initial := input.mApp.AccountKeeper.GetAccount(ctx, input.addrs[1]).GetCoins()
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2019-06-12 08:57:47 -07:00
|
|
|
expTokens := sdk.TokensFromConsensusPower(42)
|
2019-03-07 16:55:08 -08:00
|
|
|
require.Equal(t, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, expTokens)), addr0Initial)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.True(t, proposal.TotalDeposit.IsEqual(sdk.NewCoins()))
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
// Check no deposits at beginning
|
2019-04-30 09:31:38 -07:00
|
|
|
deposit, found := input.keeper.GetDeposit(ctx, proposalID, input.addrs[1])
|
2018-06-29 18:10:15 -07:00
|
|
|
require.False(t, found)
|
2019-04-30 09:31:38 -07:00
|
|
|
proposal, ok := input.keeper.GetProposal(ctx, proposalID)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.True(t, ok)
|
|
|
|
require.True(t, proposal.VotingStartTime.Equal(time.Time{}))
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
// Check first deposit
|
2019-04-30 09:31:38 -07:00
|
|
|
err, votingStarted := input.keeper.AddDeposit(ctx, proposalID, input.addrs[0], fourStake)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.False(t, votingStarted)
|
2019-04-30 09:31:38 -07:00
|
|
|
deposit, found = input.keeper.GetDeposit(ctx, proposalID, input.addrs[0])
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, found)
|
2019-03-14 07:32:46 -07:00
|
|
|
require.Equal(t, fourStake, deposit.Amount)
|
2019-04-30 09:31:38 -07:00
|
|
|
require.Equal(t, input.addrs[0], deposit.Depositor)
|
|
|
|
proposal, ok = input.keeper.GetProposal(ctx, proposalID)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, fourStake, proposal.TotalDeposit)
|
2019-06-28 13:11:27 -07:00
|
|
|
require.Equal(t, addr0Initial.Sub(fourStake), input.mApp.AccountKeeper.GetAccount(ctx, input.addrs[0]).GetCoins())
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
// Check a second deposit from same address
|
2019-04-30 09:31:38 -07:00
|
|
|
err, votingStarted = input.keeper.AddDeposit(ctx, proposalID, input.addrs[0], fiveStake)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.False(t, votingStarted)
|
2019-04-30 09:31:38 -07:00
|
|
|
deposit, found = input.keeper.GetDeposit(ctx, proposalID, input.addrs[0])
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, found)
|
2019-03-14 07:32:46 -07:00
|
|
|
require.Equal(t, fourStake.Add(fiveStake), deposit.Amount)
|
2019-04-30 09:31:38 -07:00
|
|
|
require.Equal(t, input.addrs[0], deposit.Depositor)
|
|
|
|
proposal, ok = input.keeper.GetProposal(ctx, proposalID)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, fourStake.Add(fiveStake), proposal.TotalDeposit)
|
2019-06-28 13:11:27 -07:00
|
|
|
require.Equal(t, addr0Initial.Sub(fourStake).Sub(fiveStake), input.mApp.AccountKeeper.GetAccount(ctx, input.addrs[0]).GetCoins())
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
// Check third deposit from a new address
|
2019-04-30 09:31:38 -07:00
|
|
|
err, votingStarted = input.keeper.AddDeposit(ctx, proposalID, input.addrs[1], fourStake)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.True(t, votingStarted)
|
2019-04-30 09:31:38 -07:00
|
|
|
deposit, found = input.keeper.GetDeposit(ctx, proposalID, input.addrs[1])
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, found)
|
2019-04-30 09:31:38 -07:00
|
|
|
require.Equal(t, input.addrs[1], deposit.Depositor)
|
2019-03-14 07:32:46 -07:00
|
|
|
require.Equal(t, fourStake, deposit.Amount)
|
2019-04-30 09:31:38 -07:00
|
|
|
proposal, ok = input.keeper.GetProposal(ctx, proposalID)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, fourStake.Add(fiveStake).Add(fourStake), proposal.TotalDeposit)
|
2019-06-28 13:11:27 -07:00
|
|
|
require.Equal(t, addr1Initial.Sub(fourStake), input.mApp.AccountKeeper.GetAccount(ctx, input.addrs[1]).GetCoins())
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
// Check that proposal moved to voting period
|
2019-04-30 09:31:38 -07:00
|
|
|
proposal, ok = input.keeper.GetProposal(ctx, proposalID)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.True(t, ok)
|
|
|
|
require.True(t, proposal.VotingStartTime.Equal(ctx.BlockHeader().Time))
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
// Test deposit iterator
|
2019-06-04 11:38:11 -07:00
|
|
|
depositsIterator := input.keeper.GetDepositsIterator(ctx, proposalID)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, depositsIterator.Valid())
|
2019-04-30 09:31:38 -07:00
|
|
|
input.keeper.cdc.MustUnmarshalBinaryLengthPrefixed(depositsIterator.Value(), &deposit)
|
|
|
|
require.Equal(t, input.addrs[0], deposit.Depositor)
|
2019-03-14 07:32:46 -07:00
|
|
|
require.Equal(t, fourStake.Add(fiveStake), deposit.Amount)
|
2018-06-21 17:19:14 -07:00
|
|
|
depositsIterator.Next()
|
2019-04-30 09:31:38 -07:00
|
|
|
input.keeper.cdc.MustUnmarshalBinaryLengthPrefixed(depositsIterator.Value(), &deposit)
|
|
|
|
require.Equal(t, input.addrs[1], deposit.Depositor)
|
2019-03-14 07:32:46 -07:00
|
|
|
require.Equal(t, fourStake, deposit.Amount)
|
2018-06-21 17:19:14 -07:00
|
|
|
depositsIterator.Next()
|
2018-06-29 18:10:15 -07:00
|
|
|
require.False(t, depositsIterator.Valid())
|
2018-08-20 23:54:12 -07:00
|
|
|
depositsIterator.Close()
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
// Test Refund Deposits
|
2019-04-30 09:31:38 -07:00
|
|
|
deposit, found = input.keeper.GetDeposit(ctx, proposalID, input.addrs[1])
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, found)
|
2019-03-14 07:32:46 -07:00
|
|
|
require.Equal(t, fourStake, deposit.Amount)
|
2019-04-30 09:31:38 -07:00
|
|
|
input.keeper.RefundDeposits(ctx, proposalID)
|
|
|
|
deposit, found = input.keeper.GetDeposit(ctx, proposalID, input.addrs[1])
|
2018-06-29 18:10:15 -07:00
|
|
|
require.False(t, found)
|
2019-06-28 13:11:27 -07:00
|
|
|
require.Equal(t, addr0Initial, input.mApp.AccountKeeper.GetAccount(ctx, input.addrs[0]).GetCoins())
|
|
|
|
require.Equal(t, addr1Initial, input.mApp.AccountKeeper.GetAccount(ctx, input.addrs[1]).GetCoins())
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVotes(t *testing.T) {
|
2019-04-30 09:31:38 -07:00
|
|
|
input := getMockApp(t, 2, GenesisState{}, nil)
|
|
|
|
SortAddresses(input.addrs)
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
header := abci.Header{Height: input.mApp.LastBlockHeight() + 1}
|
|
|
|
input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header})
|
2019-03-07 21:52:19 -08:00
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
ctx := input.mApp.BaseApp.NewContext(false, abci.Header{})
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2019-03-15 09:47:47 -07:00
|
|
|
tp := testProposal()
|
2019-04-30 09:31:38 -07:00
|
|
|
proposal, err := input.keeper.SubmitProposal(ctx, tp)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
proposalID := proposal.ProposalID
|
|
|
|
|
|
|
|
proposal.Status = StatusVotingPeriod
|
2019-04-30 09:31:38 -07:00
|
|
|
input.keeper.SetProposal(ctx, proposal)
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
// Test first vote
|
2019-04-30 09:31:38 -07:00
|
|
|
input.keeper.AddVote(ctx, proposalID, input.addrs[0], OptionAbstain)
|
|
|
|
vote, found := input.keeper.GetVote(ctx, proposalID, input.addrs[0])
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, found)
|
2019-04-30 09:31:38 -07:00
|
|
|
require.Equal(t, input.addrs[0], vote.Voter)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Equal(t, proposalID, vote.ProposalID)
|
|
|
|
require.Equal(t, OptionAbstain, vote.Option)
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
// Test change of vote
|
2019-04-30 09:31:38 -07:00
|
|
|
input.keeper.AddVote(ctx, proposalID, input.addrs[0], OptionYes)
|
|
|
|
vote, found = input.keeper.GetVote(ctx, proposalID, input.addrs[0])
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, found)
|
2019-04-30 09:31:38 -07:00
|
|
|
require.Equal(t, input.addrs[0], vote.Voter)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Equal(t, proposalID, vote.ProposalID)
|
|
|
|
require.Equal(t, OptionYes, vote.Option)
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
// Test second vote
|
2019-04-30 09:31:38 -07:00
|
|
|
input.keeper.AddVote(ctx, proposalID, input.addrs[1], OptionNoWithVeto)
|
|
|
|
vote, found = input.keeper.GetVote(ctx, proposalID, input.addrs[1])
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, found)
|
2019-04-30 09:31:38 -07:00
|
|
|
require.Equal(t, input.addrs[1], vote.Voter)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Equal(t, proposalID, vote.ProposalID)
|
|
|
|
require.Equal(t, OptionNoWithVeto, vote.Option)
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
// Test vote iterator
|
2019-06-04 11:38:11 -07:00
|
|
|
votesIterator := input.keeper.GetVotesIterator(ctx, proposalID)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, votesIterator.Valid())
|
2019-04-30 09:31:38 -07:00
|
|
|
input.keeper.cdc.MustUnmarshalBinaryLengthPrefixed(votesIterator.Value(), &vote)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, votesIterator.Valid())
|
2019-04-30 09:31:38 -07:00
|
|
|
require.Equal(t, input.addrs[0], vote.Voter)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Equal(t, proposalID, vote.ProposalID)
|
|
|
|
require.Equal(t, OptionYes, vote.Option)
|
2018-06-21 17:19:14 -07:00
|
|
|
votesIterator.Next()
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, votesIterator.Valid())
|
2019-04-30 09:31:38 -07:00
|
|
|
input.keeper.cdc.MustUnmarshalBinaryLengthPrefixed(votesIterator.Value(), &vote)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, votesIterator.Valid())
|
2019-04-30 09:31:38 -07:00
|
|
|
require.Equal(t, input.addrs[1], vote.Voter)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Equal(t, proposalID, vote.ProposalID)
|
|
|
|
require.Equal(t, OptionNoWithVeto, vote.Option)
|
2018-06-21 17:19:14 -07:00
|
|
|
votesIterator.Next()
|
2018-06-29 18:10:15 -07:00
|
|
|
require.False(t, votesIterator.Valid())
|
2018-08-20 23:54:12 -07:00
|
|
|
votesIterator.Close()
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProposalQueues(t *testing.T) {
|
2019-04-30 09:31:38 -07:00
|
|
|
input := getMockApp(t, 0, GenesisState{}, nil)
|
2019-03-07 21:52:19 -08:00
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
header := abci.Header{Height: input.mApp.LastBlockHeight() + 1}
|
|
|
|
input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header})
|
2019-03-07 21:52:19 -08:00
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
ctx := input.mApp.BaseApp.NewContext(false, abci.Header{})
|
|
|
|
input.mApp.InitChainer(ctx, abci.RequestInitChain{})
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
// create test proposals
|
2019-03-15 09:47:47 -07:00
|
|
|
tp := testProposal()
|
2019-04-30 09:31:38 -07:00
|
|
|
proposal, err := input.keeper.SubmitProposal(ctx, tp)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.NoError(t, err)
|
2018-11-06 23:33:18 -08:00
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
inactiveIterator := input.keeper.InactiveProposalQueueIterator(ctx, proposal.DepositEndTime)
|
2018-11-06 23:33:18 -08:00
|
|
|
require.True(t, inactiveIterator.Valid())
|
|
|
|
var proposalID uint64
|
2019-04-30 09:31:38 -07:00
|
|
|
input.keeper.cdc.UnmarshalBinaryLengthPrefixed(inactiveIterator.Value(), &proposalID)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.Equal(t, proposalID, proposal.ProposalID)
|
2018-11-06 23:33:18 -08:00
|
|
|
inactiveIterator.Close()
|
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
input.keeper.activateVotingPeriod(ctx, proposal)
|
2018-11-06 23:33:18 -08:00
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
proposal, ok := input.keeper.GetProposal(ctx, proposal.ProposalID)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.True(t, ok)
|
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
activeIterator := input.keeper.ActiveProposalQueueIterator(ctx, proposal.VotingEndTime)
|
2018-11-06 23:33:18 -08:00
|
|
|
require.True(t, activeIterator.Valid())
|
2019-04-30 09:31:38 -07:00
|
|
|
input.keeper.cdc.UnmarshalBinaryLengthPrefixed(activeIterator.Value(), &proposalID)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.Equal(t, proposalID, proposal.ProposalID)
|
2018-11-06 23:33:18 -08:00
|
|
|
activeIterator.Close()
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
2019-04-30 09:31:38 -07:00
|
|
|
|
|
|
|
type validProposal struct{}
|
|
|
|
|
|
|
|
func (validProposal) GetTitle() string { return "title" }
|
|
|
|
func (validProposal) GetDescription() string { return "description" }
|
|
|
|
func (validProposal) ProposalRoute() string { return RouterKey }
|
|
|
|
func (validProposal) ProposalType() string { return ProposalTypeText }
|
|
|
|
func (validProposal) String() string { return "" }
|
|
|
|
func (validProposal) ValidateBasic() sdk.Error { return nil }
|
|
|
|
|
|
|
|
type invalidProposalTitle1 struct{ validProposal }
|
|
|
|
|
|
|
|
func (invalidProposalTitle1) GetTitle() string { return "" }
|
|
|
|
|
|
|
|
type invalidProposalTitle2 struct{ validProposal }
|
|
|
|
|
|
|
|
func (invalidProposalTitle2) GetTitle() string { return strings.Repeat("1234567890", 100) }
|
|
|
|
|
|
|
|
type invalidProposalDesc1 struct{ validProposal }
|
|
|
|
|
|
|
|
func (invalidProposalDesc1) GetDescription() string { return "" }
|
|
|
|
|
|
|
|
type invalidProposalDesc2 struct{ validProposal }
|
|
|
|
|
|
|
|
func (invalidProposalDesc2) GetDescription() string { return strings.Repeat("1234567890", 1000) }
|
|
|
|
|
|
|
|
type invalidProposalRoute struct{ validProposal }
|
|
|
|
|
|
|
|
func (invalidProposalRoute) ProposalRoute() string { return "nonexistingroute" }
|
|
|
|
|
|
|
|
type invalidProposalValidation struct{ validProposal }
|
|
|
|
|
|
|
|
func (invalidProposalValidation) ValidateBasic() sdk.Error {
|
|
|
|
return sdk.NewError(sdk.CodespaceUndefined, sdk.CodeInternal, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func registerTestCodec(cdc *codec.Codec) {
|
|
|
|
cdc.RegisterConcrete(validProposal{}, "test/validproposal", nil)
|
|
|
|
cdc.RegisterConcrete(invalidProposalTitle1{}, "test/invalidproposalt1", nil)
|
|
|
|
cdc.RegisterConcrete(invalidProposalTitle2{}, "test/invalidproposalt2", nil)
|
|
|
|
cdc.RegisterConcrete(invalidProposalDesc1{}, "test/invalidproposald1", nil)
|
|
|
|
cdc.RegisterConcrete(invalidProposalDesc2{}, "test/invalidproposald2", nil)
|
|
|
|
cdc.RegisterConcrete(invalidProposalRoute{}, "test/invalidproposalr", nil)
|
|
|
|
cdc.RegisterConcrete(invalidProposalValidation{}, "test/invalidproposalv", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubmitProposal(t *testing.T) {
|
|
|
|
input := getMockApp(t, 0, GenesisState{}, nil)
|
|
|
|
|
|
|
|
registerTestCodec(input.keeper.cdc)
|
|
|
|
|
|
|
|
header := abci.Header{Height: input.mApp.LastBlockHeight() + 1}
|
|
|
|
input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header})
|
|
|
|
|
|
|
|
ctx := input.mApp.BaseApp.NewContext(false, abci.Header{})
|
|
|
|
input.mApp.InitChainer(ctx, abci.RequestInitChain{})
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
content Content
|
|
|
|
expectedErr sdk.Error
|
|
|
|
}{
|
|
|
|
{validProposal{}, nil},
|
|
|
|
// Keeper does not check the validity of title and description, no error
|
|
|
|
{invalidProposalTitle1{}, nil},
|
|
|
|
{invalidProposalTitle2{}, nil},
|
|
|
|
{invalidProposalDesc1{}, nil},
|
|
|
|
{invalidProposalDesc2{}, nil},
|
|
|
|
// error only when invalid route
|
|
|
|
{invalidProposalRoute{}, ErrNoProposalHandlerExists(DefaultCodespace, invalidProposalRoute{})},
|
|
|
|
// Keeper does not call ValidateBasic, msg.ValidateBasic does
|
|
|
|
{invalidProposalValidation{}, nil},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
_, err := input.keeper.SubmitProposal(ctx, tc.content)
|
|
|
|
require.Equal(t, tc.expectedErr, err, "unexpected type of error: %s", err)
|
|
|
|
}
|
|
|
|
}
|