-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge PR #5738: Migrate Gov module to Simapp
- Loading branch information
Showing
13 changed files
with
430 additions
and
500 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"bytes" | ||
|
||
"github.com/cosmos/cosmos-sdk/simapp/codec" | ||
|
||
"github.com/cosmos/cosmos-sdk/simapp" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/staking" | ||
|
||
"github.com/cosmos/cosmos-sdk/x/gov/types" | ||
) | ||
|
||
var ( | ||
TestProposal = types.NewTextProposal("Test", "description") | ||
) | ||
|
||
// ProposalEqual checks if two proposals are equal (note: slow, for tests only) | ||
func ProposalEqual(proposalA types.Proposal, proposalB types.Proposal) bool { | ||
return bytes.Equal(types.ModuleCdc.MustMarshalBinaryBare(proposalA), | ||
types.ModuleCdc.MustMarshalBinaryBare(proposalB)) | ||
} | ||
|
||
func createValidators(ctx sdk.Context, app *simapp.SimApp, powers []int64) ([]sdk.AccAddress, []sdk.ValAddress) { | ||
addrs := simapp.AddTestAddrsIncremental(app, ctx, 5, sdk.NewInt(30000000)) | ||
valAddrs := simapp.ConvertAddrsToValAddrs(addrs) | ||
pks := simapp.CreateTestPubKeys(5) | ||
|
||
appCodec := codec.NewAppCodec(app.Codec()) | ||
app.StakingKeeper = staking.NewKeeper( | ||
appCodec, | ||
app.GetKey(staking.StoreKey), | ||
app.BankKeeper, | ||
app.SupplyKeeper, | ||
app.GetSubspace(staking.ModuleName), | ||
) | ||
|
||
val1 := staking.NewValidator(valAddrs[0], pks[0], staking.Description{}) | ||
val2 := staking.NewValidator(valAddrs[1], pks[1], staking.Description{}) | ||
val3 := staking.NewValidator(valAddrs[2], pks[2], staking.Description{}) | ||
|
||
app.StakingKeeper.SetValidator(ctx, val1) | ||
app.StakingKeeper.SetValidator(ctx, val2) | ||
app.StakingKeeper.SetValidator(ctx, val3) | ||
app.StakingKeeper.SetValidatorByConsAddr(ctx, val1) | ||
app.StakingKeeper.SetValidatorByConsAddr(ctx, val2) | ||
app.StakingKeeper.SetValidatorByConsAddr(ctx, val3) | ||
app.StakingKeeper.SetNewValidatorByPowerIndex(ctx, val1) | ||
app.StakingKeeper.SetNewValidatorByPowerIndex(ctx, val2) | ||
app.StakingKeeper.SetNewValidatorByPowerIndex(ctx, val3) | ||
|
||
_, _ = app.StakingKeeper.Delegate(ctx, addrs[0], sdk.TokensFromConsensusPower(powers[0]), sdk.Unbonded, val1, true) | ||
_, _ = app.StakingKeeper.Delegate(ctx, addrs[1], sdk.TokensFromConsensusPower(powers[1]), sdk.Unbonded, val2, true) | ||
_, _ = app.StakingKeeper.Delegate(ctx, addrs[2], sdk.TokensFromConsensusPower(powers[2]), sdk.Unbonded, val3, true) | ||
|
||
_ = staking.EndBlocker(ctx, app.StakingKeeper) | ||
|
||
return addrs, valAddrs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,104 @@ | ||
package keeper | ||
package keeper_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
abci "github.com/tendermint/tendermint/abci/types" | ||
|
||
"github.com/cosmos/cosmos-sdk/simapp" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
func TestDeposits(t *testing.T) { | ||
ctx, _, bk, keeper, _, _ := createTestInput(t, false, 100) | ||
app := simapp.Setup(false) | ||
ctx := app.BaseApp.NewContext(false, abci.Header{}) | ||
|
||
TestAddrs := simapp.AddTestAddrsIncremental(app, ctx, 2, sdk.NewInt(10000000)) | ||
|
||
tp := TestProposal | ||
proposal, err := keeper.SubmitProposal(ctx, tp) | ||
proposal, err := app.GovKeeper.SubmitProposal(ctx, tp) | ||
require.NoError(t, err) | ||
proposalID := proposal.ProposalID | ||
|
||
fourStake := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromConsensusPower(4))) | ||
fiveStake := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromConsensusPower(5))) | ||
|
||
addr0Initial := bk.GetAllBalances(ctx, TestAddrs[0]) | ||
addr1Initial := bk.GetAllBalances(ctx, TestAddrs[1]) | ||
addr0Initial := app.BankKeeper.GetAllBalances(ctx, TestAddrs[0]) | ||
addr1Initial := app.BankKeeper.GetAllBalances(ctx, TestAddrs[1]) | ||
|
||
require.True(t, proposal.TotalDeposit.IsEqual(sdk.NewCoins())) | ||
|
||
// Check no deposits at beginning | ||
deposit, found := keeper.GetDeposit(ctx, proposalID, TestAddrs[1]) | ||
deposit, found := app.GovKeeper.GetDeposit(ctx, proposalID, TestAddrs[1]) | ||
require.False(t, found) | ||
proposal, ok := keeper.GetProposal(ctx, proposalID) | ||
proposal, ok := app.GovKeeper.GetProposal(ctx, proposalID) | ||
require.True(t, ok) | ||
require.True(t, proposal.VotingStartTime.Equal(time.Time{})) | ||
|
||
// Check first deposit | ||
votingStarted, err := keeper.AddDeposit(ctx, proposalID, TestAddrs[0], fourStake) | ||
votingStarted, err := app.GovKeeper.AddDeposit(ctx, proposalID, TestAddrs[0], fourStake) | ||
require.NoError(t, err) | ||
require.False(t, votingStarted) | ||
deposit, found = keeper.GetDeposit(ctx, proposalID, TestAddrs[0]) | ||
deposit, found = app.GovKeeper.GetDeposit(ctx, proposalID, TestAddrs[0]) | ||
require.True(t, found) | ||
require.Equal(t, fourStake, deposit.Amount) | ||
require.Equal(t, TestAddrs[0], deposit.Depositor) | ||
proposal, ok = keeper.GetProposal(ctx, proposalID) | ||
proposal, ok = app.GovKeeper.GetProposal(ctx, proposalID) | ||
require.True(t, ok) | ||
require.Equal(t, fourStake, proposal.TotalDeposit) | ||
require.Equal(t, addr0Initial.Sub(fourStake), bk.GetAllBalances(ctx, TestAddrs[0])) | ||
require.Equal(t, addr0Initial.Sub(fourStake), app.BankKeeper.GetAllBalances(ctx, TestAddrs[0])) | ||
|
||
// Check a second deposit from same address | ||
votingStarted, err = keeper.AddDeposit(ctx, proposalID, TestAddrs[0], fiveStake) | ||
votingStarted, err = app.GovKeeper.AddDeposit(ctx, proposalID, TestAddrs[0], fiveStake) | ||
require.NoError(t, err) | ||
require.False(t, votingStarted) | ||
deposit, found = keeper.GetDeposit(ctx, proposalID, TestAddrs[0]) | ||
deposit, found = app.GovKeeper.GetDeposit(ctx, proposalID, TestAddrs[0]) | ||
require.True(t, found) | ||
require.Equal(t, fourStake.Add(fiveStake...), deposit.Amount) | ||
require.Equal(t, TestAddrs[0], deposit.Depositor) | ||
proposal, ok = keeper.GetProposal(ctx, proposalID) | ||
proposal, ok = app.GovKeeper.GetProposal(ctx, proposalID) | ||
require.True(t, ok) | ||
require.Equal(t, fourStake.Add(fiveStake...), proposal.TotalDeposit) | ||
require.Equal(t, addr0Initial.Sub(fourStake).Sub(fiveStake), bk.GetAllBalances(ctx, TestAddrs[0])) | ||
require.Equal(t, addr0Initial.Sub(fourStake).Sub(fiveStake), app.BankKeeper.GetAllBalances(ctx, TestAddrs[0])) | ||
|
||
// Check third deposit from a new address | ||
votingStarted, err = keeper.AddDeposit(ctx, proposalID, TestAddrs[1], fourStake) | ||
votingStarted, err = app.GovKeeper.AddDeposit(ctx, proposalID, TestAddrs[1], fourStake) | ||
require.NoError(t, err) | ||
require.True(t, votingStarted) | ||
deposit, found = keeper.GetDeposit(ctx, proposalID, TestAddrs[1]) | ||
deposit, found = app.GovKeeper.GetDeposit(ctx, proposalID, TestAddrs[1]) | ||
require.True(t, found) | ||
require.Equal(t, TestAddrs[1], deposit.Depositor) | ||
require.Equal(t, fourStake, deposit.Amount) | ||
proposal, ok = keeper.GetProposal(ctx, proposalID) | ||
proposal, ok = app.GovKeeper.GetProposal(ctx, proposalID) | ||
require.True(t, ok) | ||
require.Equal(t, fourStake.Add(fiveStake...).Add(fourStake...), proposal.TotalDeposit) | ||
require.Equal(t, addr1Initial.Sub(fourStake), bk.GetAllBalances(ctx, TestAddrs[1])) | ||
require.Equal(t, addr1Initial.Sub(fourStake), app.BankKeeper.GetAllBalances(ctx, TestAddrs[1])) | ||
|
||
// Check that proposal moved to voting period | ||
proposal, ok = keeper.GetProposal(ctx, proposalID) | ||
proposal, ok = app.GovKeeper.GetProposal(ctx, proposalID) | ||
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 | ||
deposits := keeper.GetAllDeposits(ctx) | ||
deposits := app.GovKeeper.GetAllDeposits(ctx) | ||
require.Len(t, deposits, 2) | ||
require.Equal(t, deposits, keeper.GetDeposits(ctx, proposalID)) | ||
require.Equal(t, deposits, app.GovKeeper.GetDeposits(ctx, proposalID)) | ||
require.Equal(t, TestAddrs[0], deposits[0].Depositor) | ||
require.Equal(t, fourStake.Add(fiveStake...), deposits[0].Amount) | ||
require.Equal(t, TestAddrs[1], deposits[1].Depositor) | ||
require.Equal(t, fourStake, deposits[1].Amount) | ||
|
||
// Test Refund Deposits | ||
deposit, found = keeper.GetDeposit(ctx, proposalID, TestAddrs[1]) | ||
deposit, found = app.GovKeeper.GetDeposit(ctx, proposalID, TestAddrs[1]) | ||
require.True(t, found) | ||
require.Equal(t, fourStake, deposit.Amount) | ||
keeper.RefundDeposits(ctx, proposalID) | ||
deposit, found = keeper.GetDeposit(ctx, proposalID, TestAddrs[1]) | ||
app.GovKeeper.RefundDeposits(ctx, proposalID) | ||
deposit, found = app.GovKeeper.GetDeposit(ctx, proposalID, TestAddrs[1]) | ||
require.False(t, found) | ||
require.Equal(t, addr0Initial, bk.GetAllBalances(ctx, TestAddrs[0])) | ||
require.Equal(t, addr1Initial, bk.GetAllBalances(ctx, TestAddrs[1])) | ||
require.Equal(t, addr0Initial, app.BankKeeper.GetAllBalances(ctx, TestAddrs[0])) | ||
require.Equal(t, addr1Initial, app.BankKeeper.GetAllBalances(ctx, TestAddrs[1])) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,56 @@ | ||
package keeper | ||
package keeper_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
abci "github.com/tendermint/tendermint/abci/types" | ||
|
||
"github.com/cosmos/cosmos-sdk/simapp" | ||
"github.com/cosmos/cosmos-sdk/x/gov/types" | ||
) | ||
|
||
func TestIncrementProposalNumber(t *testing.T) { | ||
ctx, _, _, keeper, _, _ := createTestInput(t, false, 100) // nolint: dogsled | ||
app := simapp.Setup(false) | ||
ctx := app.BaseApp.NewContext(false, abci.Header{}) | ||
|
||
tp := TestProposal | ||
keeper.SubmitProposal(ctx, tp) | ||
keeper.SubmitProposal(ctx, tp) | ||
keeper.SubmitProposal(ctx, tp) | ||
keeper.SubmitProposal(ctx, tp) | ||
keeper.SubmitProposal(ctx, tp) | ||
proposal6, err := keeper.SubmitProposal(ctx, tp) | ||
app.GovKeeper.SubmitProposal(ctx, tp) | ||
app.GovKeeper.SubmitProposal(ctx, tp) | ||
app.GovKeeper.SubmitProposal(ctx, tp) | ||
app.GovKeeper.SubmitProposal(ctx, tp) | ||
app.GovKeeper.SubmitProposal(ctx, tp) | ||
proposal6, err := app.GovKeeper.SubmitProposal(ctx, tp) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, uint64(6), proposal6.ProposalID) | ||
} | ||
|
||
func TestProposalQueues(t *testing.T) { | ||
ctx, _, _, keeper, _, _ := createTestInput(t, false, 100) // nolint: dogsled | ||
app := simapp.Setup(false) | ||
ctx := app.BaseApp.NewContext(false, abci.Header{}) | ||
|
||
// create test proposals | ||
tp := TestProposal | ||
proposal, err := keeper.SubmitProposal(ctx, tp) | ||
proposal, err := app.GovKeeper.SubmitProposal(ctx, tp) | ||
require.NoError(t, err) | ||
|
||
inactiveIterator := keeper.InactiveProposalQueueIterator(ctx, proposal.DepositEndTime) | ||
inactiveIterator := app.GovKeeper.InactiveProposalQueueIterator(ctx, proposal.DepositEndTime) | ||
require.True(t, inactiveIterator.Valid()) | ||
|
||
proposalID := types.GetProposalIDFromBytes(inactiveIterator.Value()) | ||
require.Equal(t, proposalID, proposal.ProposalID) | ||
inactiveIterator.Close() | ||
|
||
keeper.activateVotingPeriod(ctx, proposal) | ||
app.GovKeeper.ActivateVotingPeriod(ctx, proposal) | ||
|
||
proposal, ok := keeper.GetProposal(ctx, proposal.ProposalID) | ||
proposal, ok := app.GovKeeper.GetProposal(ctx, proposal.ProposalID) | ||
require.True(t, ok) | ||
|
||
activeIterator := keeper.ActiveProposalQueueIterator(ctx, proposal.VotingEndTime) | ||
activeIterator := app.GovKeeper.ActiveProposalQueueIterator(ctx, proposal.VotingEndTime) | ||
require.True(t, activeIterator.Valid()) | ||
keeper.cdc.UnmarshalBinaryLengthPrefixed(activeIterator.Value(), &proposalID) | ||
app.Codec().UnmarshalBinaryLengthPrefixed(activeIterator.Value(), &proposalID) | ||
require.Equal(t, proposalID, proposal.ProposalID) | ||
activeIterator.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.