2019-01-07 14:24:04 -08:00
|
|
|
package gov
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
)
|
|
|
|
|
2019-02-05 17:52:18 -08:00
|
|
|
func TestEqualProposalID(t *testing.T) {
|
|
|
|
state1 := GenesisState{}
|
|
|
|
state2 := GenesisState{}
|
|
|
|
require.Equal(t, state1, state2)
|
|
|
|
|
|
|
|
// Proposals
|
|
|
|
state1.StartingProposalID = 1
|
|
|
|
require.NotEqual(t, state1, state2)
|
|
|
|
require.False(t, state1.Equal(state2))
|
|
|
|
|
|
|
|
state2.StartingProposalID = 1
|
|
|
|
require.Equal(t, state1, state2)
|
|
|
|
require.True(t, state1.Equal(state2))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEqualProposals(t *testing.T) {
|
|
|
|
// Generate mock app and keepers
|
2019-04-30 09:31:38 -07:00
|
|
|
input := getMockApp(t, 2, GenesisState{}, nil)
|
|
|
|
SortAddresses(input.addrs)
|
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{})
|
2019-02-05 17:52:18 -08:00
|
|
|
|
2019-03-15 09:47:47 -07:00
|
|
|
// Submit two proposals
|
|
|
|
proposal := testProposal()
|
2019-04-30 09:31:38 -07:00
|
|
|
proposal1, err := input.keeper.SubmitProposal(ctx, proposal)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.NoError(t, err)
|
2019-04-30 09:31:38 -07:00
|
|
|
proposal2, err := input.keeper.SubmitProposal(ctx, proposal)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.NoError(t, err)
|
2019-02-05 17:52:18 -08:00
|
|
|
|
|
|
|
// They are similar but their IDs should be different
|
|
|
|
require.NotEqual(t, proposal1, proposal2)
|
|
|
|
require.False(t, ProposalEqual(proposal1, proposal2))
|
|
|
|
|
|
|
|
// Now create two genesis blocks
|
|
|
|
state1 := GenesisState{Proposals: []Proposal{proposal1}}
|
|
|
|
state2 := GenesisState{Proposals: []Proposal{proposal2}}
|
|
|
|
require.NotEqual(t, state1, state2)
|
|
|
|
require.False(t, state1.Equal(state2))
|
|
|
|
|
|
|
|
// Now make proposals identical by setting both IDs to 55
|
2019-03-15 09:47:47 -07:00
|
|
|
proposal1.ProposalID = 55
|
|
|
|
proposal2.ProposalID = 55
|
2019-02-05 17:52:18 -08:00
|
|
|
require.Equal(t, proposal1, proposal1)
|
|
|
|
require.True(t, ProposalEqual(proposal1, proposal2))
|
|
|
|
|
2019-03-15 09:47:47 -07:00
|
|
|
// Reassign proposals into state
|
|
|
|
state1.Proposals[0] = proposal1
|
|
|
|
state2.Proposals[0] = proposal2
|
|
|
|
|
2019-02-05 17:52:18 -08:00
|
|
|
// State should be identical now..
|
|
|
|
require.Equal(t, state1, state2)
|
|
|
|
require.True(t, state1.Equal(state2))
|
|
|
|
}
|
|
|
|
|
2019-01-07 14:24:04 -08:00
|
|
|
func TestImportExportQueues(t *testing.T) {
|
|
|
|
// Generate mock app and keepers
|
2019-04-30 09:31:38 -07:00
|
|
|
input := getMockApp(t, 2, GenesisState{}, nil)
|
|
|
|
SortAddresses(input.addrs)
|
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{})
|
2019-01-07 14:24:04 -08:00
|
|
|
|
|
|
|
// Create two proposals, put the second into the voting period
|
2019-03-15 09:47:47 -07:00
|
|
|
proposal := testProposal()
|
2019-04-30 09:31:38 -07:00
|
|
|
proposal1, err := input.keeper.SubmitProposal(ctx, proposal)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
proposalID1 := proposal1.ProposalID
|
2019-01-07 14:24:04 -08:00
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
proposal2, err := input.keeper.SubmitProposal(ctx, proposal)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
proposalID2 := proposal2.ProposalID
|
2019-01-07 14:24:04 -08:00
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
_, votingStarted := input.keeper.AddDeposit(ctx, proposalID2, input.addrs[0], input.keeper.GetDepositParams(ctx).MinDeposit)
|
2019-01-07 14:24:04 -08:00
|
|
|
require.True(t, votingStarted)
|
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
proposal1, ok := input.keeper.GetProposal(ctx, proposalID1)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.True(t, ok)
|
2019-04-30 09:31:38 -07:00
|
|
|
proposal2, ok = input.keeper.GetProposal(ctx, proposalID2)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.True(t, ok)
|
|
|
|
require.True(t, proposal1.Status == StatusDepositPeriod)
|
|
|
|
require.True(t, proposal2.Status == StatusVotingPeriod)
|
2019-01-07 14:24:04 -08:00
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
genAccs := input.mApp.AccountKeeper.GetAllAccounts(ctx)
|
2019-01-07 14:24:04 -08:00
|
|
|
|
|
|
|
// Export the state and import it into a new Mock App
|
2019-04-30 09:31:38 -07:00
|
|
|
genState := ExportGenesis(ctx, input.keeper)
|
|
|
|
input2 := getMockApp(t, 2, genState, genAccs)
|
2019-01-07 14:24:04 -08:00
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
header = abci.Header{Height: input.mApp.LastBlockHeight() + 1}
|
|
|
|
input2.mApp.BeginBlock(abci.RequestBeginBlock{Header: header})
|
2019-03-07 21:52:19 -08:00
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
ctx2 := input2.mApp.BaseApp.NewContext(false, abci.Header{})
|
2019-01-07 14:24:04 -08:00
|
|
|
|
|
|
|
// Jump the time forward past the DepositPeriod and VotingPeriod
|
2019-04-30 09:31:38 -07:00
|
|
|
ctx2 = ctx2.WithBlockTime(ctx2.BlockHeader().Time.Add(input2.keeper.GetDepositParams(ctx2).MaxDepositPeriod).Add(input2.keeper.GetVotingParams(ctx2).VotingPeriod))
|
2019-01-07 14:24:04 -08:00
|
|
|
|
|
|
|
// Make sure that they are still in the DepositPeriod and VotingPeriod respectively
|
2019-04-30 09:31:38 -07:00
|
|
|
proposal1, ok = input2.keeper.GetProposal(ctx2, proposalID1)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.True(t, ok)
|
2019-04-30 09:31:38 -07:00
|
|
|
proposal2, ok = input2.keeper.GetProposal(ctx2, proposalID2)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.True(t, ok)
|
|
|
|
require.True(t, proposal1.Status == StatusDepositPeriod)
|
|
|
|
require.True(t, proposal2.Status == StatusVotingPeriod)
|
2019-01-07 14:24:04 -08:00
|
|
|
|
|
|
|
// Run the endblocker. Check to make sure that proposal1 is removed from state, and proposal2 is finished VotingPeriod.
|
2019-04-30 09:31:38 -07:00
|
|
|
EndBlocker(ctx2, input2.keeper)
|
2019-01-07 14:24:04 -08:00
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
proposal1, ok = input2.keeper.GetProposal(ctx2, proposalID1)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.False(t, ok)
|
2019-04-30 09:31:38 -07:00
|
|
|
proposal2, ok = input2.keeper.GetProposal(ctx2, proposalID2)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.True(t, ok)
|
|
|
|
require.True(t, proposal2.Status == StatusRejected)
|
2019-01-07 14:24:04 -08:00
|
|
|
}
|