types updates

This commit is contained in:
Aleksandr Bezobchuk 2020-03-02 15:50:20 -05:00
parent 81830807e7
commit fb42f8b2a0
No known key found for this signature in database
GPG Key ID: 7DAC30FBD99879B0
9 changed files with 99 additions and 73 deletions

View File

@ -94,9 +94,3 @@ func createValidators(t *testing.T, stakingHandler sdk.Handler, ctx sdk.Context,
require.NotNil(t, res)
}
}
// 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))
}

View File

@ -124,7 +124,7 @@ func TestEqualProposals(t *testing.T) {
// They are similar but their IDs should be different
require.NotEqual(t, proposal1, proposal2)
require.False(t, ProposalEqual(proposal1, proposal2))
require.NotEqual(t, proposal1, proposal2)
// Now create two genesis blocks
state1 := gov.GenesisState{Proposals: []gov.Proposal{proposal1}}
@ -136,7 +136,7 @@ func TestEqualProposals(t *testing.T) {
proposal1.ProposalID = 55
proposal2.ProposalID = 55
require.Equal(t, proposal1, proposal1)
require.True(t, ProposalEqual(proposal1, proposal2))
require.Equal(t, proposal1, proposal2)
// Reassign proposals into state
state1.Proposals[0] = proposal1

View File

@ -1,10 +1,8 @@
package keeper_test
import (
"bytes"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/simapp/codec"
simappcodec "github.com/cosmos/cosmos-sdk/simapp/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/cosmos-sdk/x/staking"
@ -14,18 +12,12 @@ 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())
appCodec := simappcodec.NewAppCodec(app.Codec())
app.StakingKeeper = staking.NewKeeper(
appCodec,
app.GetKey(staking.StoreKey),

View File

@ -9,7 +9,6 @@ import (
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov/types"
@ -27,7 +26,7 @@ func TestGetSetProposal(t *testing.T) {
gotProposal, ok := app.GovKeeper.GetProposal(ctx, proposalID)
require.True(t, ok)
require.True(t, ProposalEqual(proposal, gotProposal))
require.Equal(t, proposal.String(), gotProposal.String())
}
func TestActivateVotingPeriod(t *testing.T) {
@ -90,22 +89,20 @@ func (invalidProposalValidation) ValidateBasic() error {
return errors.New("invalid proposal")
}
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 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) {
app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, abci.Header{})
registerTestCodec(app.Codec())
testCases := []struct {
content types.Content
expectedErr error

View File

@ -7,10 +7,11 @@ import (
"time"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermit/abci/types"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp"
simappcodec "github.com/cosmos/cosmos-sdk/simapp/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov/keeper"
"github.com/cosmos/cosmos-sdk/x/gov/types"
@ -146,6 +147,7 @@ func getQueriedVotes(t *testing.T, ctx sdk.Context, cdc codec.Marshaler, querier
func TestQueries(t *testing.T) {
app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, abci.Header{})
appCodec := simappcodec.NewAppCodec(app.Codec())
querier := keeper.NewQuerier(app.GovKeeper)
@ -156,7 +158,7 @@ func TestQueries(t *testing.T) {
tp := TestProposal
depositParams, _, _ := getQueriedParams(t, ctx, app.Codec(), querier)
depositParams, _, _ := getQueriedParams(t, ctx, appCodec, querier)
// TestAddrs[0] proposes (and deposits) proposals #1 and #2
proposal1, err := app.GovKeeper.SubmitProposal(ctx, tp)
@ -204,35 +206,35 @@ func TestQueries(t *testing.T) {
deposit5.Amount = deposit5.Amount.Add(deposit3.Amount...)
// check deposits on proposal1 match individual deposits
deposits := getQueriedDeposits(t, ctx, app.Codec(), querier, proposal1.ProposalID)
deposits := getQueriedDeposits(t, ctx, appCodec, querier, proposal1.ProposalID)
require.Len(t, deposits, 1)
require.Equal(t, deposit1, deposits[0])
deposit := getQueriedDeposit(t, ctx, app.Codec(), querier, proposal1.ProposalID, TestAddrs[0])
deposit := getQueriedDeposit(t, ctx, appCodec, querier, proposal1.ProposalID, TestAddrs[0])
require.Equal(t, deposit1, deposit)
// check deposits on proposal2 match individual deposits
deposits = getQueriedDeposits(t, ctx, app.Codec(), querier, proposal2.ProposalID)
deposits = getQueriedDeposits(t, ctx, appCodec, querier, proposal2.ProposalID)
require.Len(t, deposits, 2)
// NOTE order of deposits is determined by the addresses
require.Equal(t, deposit2, deposits[0])
require.Equal(t, deposit4, deposits[1])
// check deposits on proposal3 match individual deposits
deposits = getQueriedDeposits(t, ctx, app.Codec(), querier, proposal3.ProposalID)
deposits = getQueriedDeposits(t, ctx, appCodec, querier, proposal3.ProposalID)
require.Len(t, deposits, 1)
require.Equal(t, deposit5, deposits[0])
deposit = getQueriedDeposit(t, ctx, app.Codec(), querier, proposal3.ProposalID, TestAddrs[1])
deposit = getQueriedDeposit(t, ctx, appCodec, querier, proposal3.ProposalID, TestAddrs[1])
require.Equal(t, deposit5, deposit)
// Only proposal #1 should be in types.Deposit Period
proposals := getQueriedProposals(t, ctx, app.Codec(), querier, nil, nil, types.StatusDepositPeriod, 1, 0)
proposals := getQueriedProposals(t, ctx, appCodec, querier, nil, nil, types.StatusDepositPeriod, 1, 0)
require.Len(t, proposals, 1)
require.Equal(t, proposal1, proposals[0])
// Only proposals #2 and #3 should be in Voting Period
proposals = getQueriedProposals(t, ctx, app.Codec(), querier, nil, nil, types.StatusVotingPeriod, 1, 0)
proposals = getQueriedProposals(t, ctx, appCodec, querier, nil, nil, types.StatusVotingPeriod, 1, 0)
require.Len(t, proposals, 2)
require.Equal(t, proposal2, proposals[0])
require.Equal(t, proposal3, proposals[1])
@ -248,56 +250,60 @@ func TestQueries(t *testing.T) {
app.GovKeeper.SetVote(ctx, vote3)
// Test query voted by TestAddrs[0]
proposals = getQueriedProposals(t, ctx, app.Codec(), querier, nil, TestAddrs[0], types.StatusNil, 1, 0)
proposals = getQueriedProposals(t, ctx, appCodec, querier, nil, TestAddrs[0], types.StatusNil, 1, 0)
require.Equal(t, proposal2, proposals[0])
require.Equal(t, proposal3, proposals[1])
// Test query votes on types.Proposal 2
votes := getQueriedVotes(t, ctx, app.Codec(), querier, proposal2.ProposalID, 1, 0)
votes := getQueriedVotes(t, ctx, appCodec, querier, proposal2.ProposalID, 1, 0)
require.Len(t, votes, 1)
require.Equal(t, vote1, votes[0])
vote := getQueriedVote(t, ctx, app.Codec(), querier, proposal2.ProposalID, TestAddrs[0])
vote := getQueriedVote(t, ctx, appCodec, querier, proposal2.ProposalID, TestAddrs[0])
require.Equal(t, vote1, vote)
// Test query votes on types.Proposal 3
votes = getQueriedVotes(t, ctx, app.Codec(), querier, proposal3.ProposalID, 1, 0)
votes = getQueriedVotes(t, ctx, appCodec, querier, proposal3.ProposalID, 1, 0)
require.Len(t, votes, 2)
require.Equal(t, vote2, votes[0])
require.Equal(t, vote3, votes[1])
// Test query all proposals
proposals = getQueriedProposals(t, ctx, app.Codec(), querier, nil, nil, types.StatusNil, 1, 0)
proposals = getQueriedProposals(t, ctx, appCodec, querier, nil, nil, types.StatusNil, 1, 0)
require.Equal(t, proposal1, proposals[0])
require.Equal(t, proposal2, proposals[1])
require.Equal(t, proposal3, proposals[2])
// Test query voted by TestAddrs[1]
proposals = getQueriedProposals(t, ctx, app.Codec(), querier, nil, TestAddrs[1], types.StatusNil, 1, 0)
proposals = getQueriedProposals(t, ctx, appCodec, querier, nil, TestAddrs[1], types.StatusNil, 1, 0)
require.Equal(t, proposal3.ProposalID, proposals[0].ProposalID)
// Test query deposited by TestAddrs[0]
proposals = getQueriedProposals(t, ctx, app.Codec(), querier, TestAddrs[0], nil, types.StatusNil, 1, 0)
proposals = getQueriedProposals(t, ctx, appCodec, querier, TestAddrs[0], nil, types.StatusNil, 1, 0)
require.Equal(t, proposal1.ProposalID, proposals[0].ProposalID)
// Test query deposited by addr2
proposals = getQueriedProposals(t, ctx, app.Codec(), querier, TestAddrs[1], nil, types.StatusNil, 1, 0)
proposals = getQueriedProposals(t, ctx, appCodec, querier, TestAddrs[1], nil, types.StatusNil, 1, 0)
require.Equal(t, proposal2.ProposalID, proposals[0].ProposalID)
require.Equal(t, proposal3.ProposalID, proposals[1].ProposalID)
// Test query voted AND deposited by addr1
proposals = getQueriedProposals(t, ctx, app.Codec(), querier, TestAddrs[0], TestAddrs[0], types.StatusNil, 1, 0)
proposals = getQueriedProposals(t, ctx, appCodec, querier, TestAddrs[0], TestAddrs[0], types.StatusNil, 1, 0)
require.Equal(t, proposal2.ProposalID, proposals[0].ProposalID)
}
func TestPaginatedVotesQuery(t *testing.T) {
app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, abci.Header{})
appCodec := simappcodec.NewAppCodec(app.Codec())
proposal := types.Proposal{
ProposalID: 100,
Status: types.StatusVotingPeriod,
ProposalBase: types.ProposalBase{
ProposalID: 100,
Status: types.StatusVotingPeriod,
},
}
app.GovKeeper.SetProposal(ctx, proposal)
votes := make([]types.Vote, 20)
@ -317,7 +323,7 @@ func TestPaginatedVotesQuery(t *testing.T) {
querier := keeper.NewQuerier(app.GovKeeper)
// keeper preserves consistent order for each query, but this is not the insertion order
all := getQueriedVotes(t, ctx, app.Codec(), querier, proposal.ProposalID, 1, 0)
all := getQueriedVotes(t, ctx, appCodec, querier, proposal.ProposalID, 1, 0)
require.Equal(t, len(all), len(votes))
type testCase struct {
@ -351,7 +357,7 @@ func TestPaginatedVotesQuery(t *testing.T) {
} {
tc := tc
t.Run(tc.description, func(t *testing.T) {
votes := getQueriedVotes(t, ctx, app.Codec(), querier, proposal.ProposalID, tc.page, tc.limit)
votes := getQueriedVotes(t, ctx, appCodec, querier, proposal.ProposalID, tc.page, tc.limit)
require.Equal(t, len(tc.votes), len(votes))
for i := range votes {
require.Equal(t, tc.votes[i], votes[i])

View File

@ -21,6 +21,21 @@ func (d Deposit) String() string {
// Deposits is a collection of Deposit objects
type Deposits []Deposit
// Equal returns true if two slices (order-dependant) of deposits are equal.
func (d Deposits) Equal(other Deposits) bool {
if len(d) != len(other) {
return false
}
for i, deposit := range d {
if !deposit.Equal(other[i]) {
return false
}
}
return true
}
func (d Deposits) String() string {
if len(d) == 0 {
return "[]"
@ -32,12 +47,7 @@ func (d Deposits) String() string {
return out
}
// Equals returns whether two deposits are equal.
func (d Deposit) Equals(comp Deposit) bool {
return d.Depositor.Equals(comp.Depositor) && d.ProposalID == comp.ProposalID && d.Amount.IsEqual(comp.Amount)
}
// Empty returns whether a deposit is empty.
func (d Deposit) Empty() bool {
return d.Equals(Deposit{})
return d.Equal(Deposit{})
}

View File

@ -37,14 +37,18 @@ func DefaultGenesisState() GenesisState {
)
}
func (data GenesisState) Equal(other GenesisState) bool {
return data.Deposits.Equal(other.Deposits) &&
data.Votes.Equal(other.Votes) &&
data.Proposals.Equal(other.Proposals) &&
data.DepositParams.Equal(other.DepositParams) &&
data.TallyParams.Equal(other.TallyParams) &&
data.VotingParams.Equal(other.VotingParams)
}
// IsEmpty returns true if a GenesisState is empty
func (data GenesisState) IsEmpty() bool {
return data.Deposits == nil &&
data.Votes == nil &&
data.Proposals == nil &&
data.DepositParams.Equal(DepositParams{}) &&
data.TallyParams.Equal(TallyParams{}) &&
data.VotingParams.Equal(VotingParams{})
return data.Equal(GenesisState{})
}
// ValidateGenesis checks if parameters are within valid ranges

View File

@ -46,6 +46,21 @@ func (p Proposal) String() string {
// Proposals is an array of proposal
type Proposals []Proposal
// Equal returns true if two slices (order-dependant) of proposals are equal.
func (p Proposals) Equal(other Proposals) bool {
if len(p) != len(other) {
return false
}
for i, proposal := range p {
if !proposal.Equal(other[i]) {
return false
}
}
return true
}
// String implements stringer interface
func (p Proposals) String() string {
out := "ID - (Status) [Type] Title\n"

View File

@ -22,6 +22,21 @@ func (v Vote) String() string {
// Votes is a collection of Vote objects
type Votes []Vote
// Equal returns true if two slices (order-dependant) of votes are equal.
func (v Votes) Equal(other Votes) bool {
if len(v) != len(other) {
return false
}
for i, vote := range v {
if !vote.Equal(other[i]) {
return false
}
}
return true
}
func (v Votes) String() string {
if len(v) == 0 {
return "[]"
@ -33,16 +48,9 @@ func (v Votes) String() string {
return out
}
// Equals returns whether two votes are equal.
func (v Vote) Equals(comp Vote) bool {
return v.Voter.Equals(comp.Voter) &&
v.ProposalID == comp.ProposalID &&
v.Option == comp.Option
}
// Empty returns whether a vote is empty.
func (v Vote) Empty() bool {
return v.Equals(Vote{})
return v.Equal(Vote{})
}
// VoteOptionFromString returns a VoteOption from a string. It returns an error