2020-02-14 07:30:51 -08:00
|
|
|
package gov_test
|
2019-01-07 14:24:04 -08:00
|
|
|
|
|
|
|
import (
|
2020-08-11 07:22:30 -07:00
|
|
|
"encoding/json"
|
2019-01-07 14:24:04 -08:00
|
|
|
"testing"
|
|
|
|
|
2020-01-30 13:31:16 -08:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-01-07 14:24:04 -08:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2020-02-14 07:30:51 -08:00
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
2020-08-14 10:58:53 -07:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2020-02-14 07:30:51 -08:00
|
|
|
dbm "github.com/tendermint/tm-db"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
2021-02-17 10:20:33 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-02-14 07:30:51 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
2020-06-17 11:42:27 -07:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2020-06-14 16:06:16 -07:00
|
|
|
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
2020-02-14 07:30:51 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/gov"
|
2020-06-13 02:07:51 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/gov/types"
|
2019-01-07 14:24:04 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestImportExportQueues(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-02-14 07:30:51 -08:00
|
|
|
addrs := simapp.AddTestAddrs(app, ctx, 2, valTokens)
|
|
|
|
|
|
|
|
SortAddresses(addrs)
|
2019-03-07 21:52:19 -08:00
|
|
|
|
2020-08-14 10:58:53 -07:00
|
|
|
header := tmproto.Header{Height: app.LastBlockHeight() + 1}
|
2020-02-14 07:30:51 -08:00
|
|
|
app.BeginBlock(abci.RequestBeginBlock{Header: header})
|
2019-03-07 21:52:19 -08:00
|
|
|
|
2020-08-14 10:58:53 -07:00
|
|
|
ctx = app.BaseApp.NewContext(false, tmproto.Header{})
|
2019-01-07 14:24:04 -08:00
|
|
|
|
|
|
|
// Create two proposals, put the second into the voting period
|
2020-03-02 10:56:29 -08:00
|
|
|
proposal := TestProposal
|
2020-02-14 07:30:51 -08:00
|
|
|
proposal1, err := app.GovKeeper.SubmitProposal(ctx, proposal)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.NoError(t, err)
|
2020-08-14 01:55:54 -07:00
|
|
|
proposalID1 := proposal1.ProposalId
|
2019-01-07 14:24:04 -08:00
|
|
|
|
2020-02-14 07:30:51 -08:00
|
|
|
proposal2, err := app.GovKeeper.SubmitProposal(ctx, proposal)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.NoError(t, err)
|
2020-08-14 01:55:54 -07:00
|
|
|
proposalID2 := proposal2.ProposalId
|
2019-01-07 14:24:04 -08:00
|
|
|
|
2020-02-14 07:30:51 -08:00
|
|
|
votingStarted, err := app.GovKeeper.AddDeposit(ctx, proposalID2, addrs[0], app.GovKeeper.GetDepositParams(ctx).MinDeposit)
|
2019-06-28 13:11:27 -07:00
|
|
|
require.NoError(t, err)
|
2019-01-07 14:24:04 -08:00
|
|
|
require.True(t, votingStarted)
|
|
|
|
|
2020-02-14 07:30:51 -08:00
|
|
|
proposal1, ok := app.GovKeeper.GetProposal(ctx, proposalID1)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.True(t, ok)
|
2020-02-14 07:30:51 -08:00
|
|
|
proposal2, ok = app.GovKeeper.GetProposal(ctx, proposalID2)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.True(t, ok)
|
2020-06-13 02:07:51 -07:00
|
|
|
require.True(t, proposal1.Status == types.StatusDepositPeriod)
|
|
|
|
require.True(t, proposal2.Status == types.StatusVotingPeriod)
|
2020-02-14 07:30:51 -08:00
|
|
|
|
|
|
|
authGenState := auth.ExportGenesis(ctx, app.AccountKeeper)
|
2020-07-12 23:55:58 -07:00
|
|
|
bankGenState := app.BankKeeper.ExportGenesis(ctx)
|
2019-01-07 14:24:04 -08:00
|
|
|
|
2020-02-14 07:30:51 -08:00
|
|
|
// export the state and import it into a new app
|
|
|
|
govGenState := gov.ExportGenesis(ctx, app.GovKeeper)
|
2021-01-06 07:20:12 -08:00
|
|
|
genesisState := simapp.NewDefaultGenesisState(app.AppCodec())
|
2019-01-07 14:24:04 -08:00
|
|
|
|
2020-06-17 11:42:27 -07:00
|
|
|
genesisState[authtypes.ModuleName] = app.AppCodec().MustMarshalJSON(authGenState)
|
2020-06-14 16:06:16 -07:00
|
|
|
genesisState[banktypes.ModuleName] = app.AppCodec().MustMarshalJSON(bankGenState)
|
2020-06-13 02:07:51 -07:00
|
|
|
genesisState[types.ModuleName] = app.AppCodec().MustMarshalJSON(govGenState)
|
2019-03-07 21:52:19 -08:00
|
|
|
|
2020-08-11 07:22:30 -07:00
|
|
|
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
|
2020-02-14 07:30:51 -08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-02-18 04:50:13 -08:00
|
|
|
db := dbm.NewMemDB()
|
2020-11-02 11:10:14 -08:00
|
|
|
app2 := simapp.NewSimApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, simapp.DefaultNodeHome, 0, simapp.MakeTestEncodingConfig(), simapp.EmptyAppOptions{})
|
2020-02-18 04:50:13 -08:00
|
|
|
|
2020-02-14 07:30:51 -08:00
|
|
|
app2.InitChain(
|
|
|
|
abci.RequestInitChain{
|
2020-04-16 08:10:39 -07:00
|
|
|
Validators: []abci.ValidatorUpdate{},
|
|
|
|
ConsensusParams: simapp.DefaultConsensusParams,
|
|
|
|
AppStateBytes: stateBytes,
|
2020-02-14 07:30:51 -08:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
app2.Commit()
|
2020-08-14 10:58:53 -07:00
|
|
|
app2.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: app2.LastBlockHeight() + 1}})
|
2020-02-14 07:30:51 -08:00
|
|
|
|
2020-08-14 10:58:53 -07:00
|
|
|
header = tmproto.Header{Height: app2.LastBlockHeight() + 1}
|
2020-02-14 07:30:51 -08:00
|
|
|
app2.BeginBlock(abci.RequestBeginBlock{Header: header})
|
|
|
|
|
2020-08-14 10:58:53 -07:00
|
|
|
ctx2 := app2.BaseApp.NewContext(false, tmproto.Header{})
|
2019-01-07 14:24:04 -08:00
|
|
|
|
|
|
|
// Jump the time forward past the DepositPeriod and VotingPeriod
|
2020-02-14 07:30:51 -08:00
|
|
|
ctx2 = ctx2.WithBlockTime(ctx2.BlockHeader().Time.Add(app2.GovKeeper.GetDepositParams(ctx2).MaxDepositPeriod).Add(app2.GovKeeper.GetVotingParams(ctx2).VotingPeriod))
|
2019-01-07 14:24:04 -08:00
|
|
|
|
|
|
|
// Make sure that they are still in the DepositPeriod and VotingPeriod respectively
|
2020-02-14 07:30:51 -08:00
|
|
|
proposal1, ok = app2.GovKeeper.GetProposal(ctx2, proposalID1)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.True(t, ok)
|
2020-02-14 07:30:51 -08:00
|
|
|
proposal2, ok = app2.GovKeeper.GetProposal(ctx2, proposalID2)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.True(t, ok)
|
2020-06-13 02:07:51 -07:00
|
|
|
require.True(t, proposal1.Status == types.StatusDepositPeriod)
|
|
|
|
require.True(t, proposal2.Status == types.StatusVotingPeriod)
|
2019-01-07 14:24:04 -08:00
|
|
|
|
2020-02-14 07:30:51 -08:00
|
|
|
macc := app2.GovKeeper.GetGovernanceAccount(ctx2)
|
|
|
|
require.Equal(t, app2.GovKeeper.GetDepositParams(ctx2).MinDeposit, app2.BankKeeper.GetAllBalances(ctx2, macc.GetAddress()))
|
2019-06-28 13:11:27 -07:00
|
|
|
|
|
|
|
// Run the endblocker. Check to make sure that proposal1 is removed from state, and proposal2 is finished VotingPeriod.
|
2020-02-14 07:30:51 -08:00
|
|
|
gov.EndBlocker(ctx2, app2.GovKeeper)
|
2019-01-07 14:24:04 -08:00
|
|
|
|
2020-02-14 07:30:51 -08:00
|
|
|
proposal1, ok = app2.GovKeeper.GetProposal(ctx2, proposalID1)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.False(t, ok)
|
2020-02-14 07:30:51 -08:00
|
|
|
|
|
|
|
proposal2, ok = app2.GovKeeper.GetProposal(ctx2, proposalID2)
|
2019-03-15 09:47:47 -07:00
|
|
|
require.True(t, ok)
|
2020-06-13 02:07:51 -07:00
|
|
|
require.True(t, proposal2.Status == types.StatusRejected)
|
2019-01-07 14:24:04 -08:00
|
|
|
}
|
2019-08-08 12:51:18 -07:00
|
|
|
|
2021-02-17 10:20:33 -08:00
|
|
|
func TestImportExportQueues_ErrorUnconsistentState(t *testing.T) {
|
2021-08-16 17:52:06 -07:00
|
|
|
app := simapp.Setup(t, false)
|
2021-02-17 10:20:33 -08:00
|
|
|
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
|
|
|
require.Panics(t, func() {
|
|
|
|
gov.InitGenesis(ctx, app.AccountKeeper, app.BankKeeper, app.GovKeeper, &types.GenesisState{
|
|
|
|
Deposits: types.Deposits{
|
|
|
|
{
|
2021-05-07 05:00:19 -07:00
|
|
|
ProposalId: 1234,
|
|
|
|
Depositor: "me",
|
|
|
|
Amount: sdk.Coins{
|
|
|
|
sdk.NewCoin(
|
2021-02-17 10:20:33 -08:00
|
|
|
"stake",
|
|
|
|
sdk.NewInt(1234),
|
2021-05-07 05:00:19 -07:00
|
|
|
),
|
2021-02-17 10:20:33 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-08 12:51:18 -07:00
|
|
|
func TestEqualProposals(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-02-14 07:30:51 -08:00
|
|
|
addrs := simapp.AddTestAddrs(app, ctx, 2, valTokens)
|
2019-08-08 12:51:18 -07:00
|
|
|
|
2020-02-14 07:30:51 -08:00
|
|
|
SortAddresses(addrs)
|
2019-08-08 12:51:18 -07:00
|
|
|
|
2020-08-14 10:58:53 -07:00
|
|
|
header := tmproto.Header{Height: app.LastBlockHeight() + 1}
|
2020-02-14 07:30:51 -08:00
|
|
|
app.BeginBlock(abci.RequestBeginBlock{Header: header})
|
2019-08-08 12:51:18 -07:00
|
|
|
|
|
|
|
// Submit two proposals
|
2020-03-02 10:56:29 -08:00
|
|
|
proposal := TestProposal
|
2020-02-14 07:30:51 -08:00
|
|
|
proposal1, err := app.GovKeeper.SubmitProposal(ctx, proposal)
|
2019-08-08 12:51:18 -07:00
|
|
|
require.NoError(t, err)
|
2020-02-14 07:30:51 -08:00
|
|
|
|
|
|
|
proposal2, err := app.GovKeeper.SubmitProposal(ctx, proposal)
|
2019-08-08 12:51:18 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// They are similar but their IDs should be different
|
|
|
|
require.NotEqual(t, proposal1, proposal2)
|
2020-03-02 12:50:20 -08:00
|
|
|
require.NotEqual(t, proposal1, proposal2)
|
2019-08-08 12:51:18 -07:00
|
|
|
|
|
|
|
// Now create two genesis blocks
|
2020-06-13 02:07:51 -07:00
|
|
|
state1 := types.GenesisState{Proposals: []types.Proposal{proposal1}}
|
|
|
|
state2 := types.GenesisState{Proposals: []types.Proposal{proposal2}}
|
2019-08-08 12:51:18 -07:00
|
|
|
require.NotEqual(t, state1, state2)
|
|
|
|
require.False(t, state1.Equal(state2))
|
|
|
|
|
|
|
|
// Now make proposals identical by setting both IDs to 55
|
2020-08-14 01:55:54 -07:00
|
|
|
proposal1.ProposalId = 55
|
|
|
|
proposal2.ProposalId = 55
|
2019-08-08 12:51:18 -07:00
|
|
|
require.Equal(t, proposal1, proposal1)
|
2020-03-02 12:50:20 -08:00
|
|
|
require.Equal(t, proposal1, proposal2)
|
2019-08-08 12:51:18 -07:00
|
|
|
|
|
|
|
// Reassign proposals into state
|
|
|
|
state1.Proposals[0] = proposal1
|
|
|
|
state2.Proposals[0] = proposal2
|
|
|
|
|
|
|
|
// State should be identical now..
|
|
|
|
require.Equal(t, state1, state2)
|
|
|
|
require.True(t, state1.Equal(state2))
|
|
|
|
}
|