From 66d368c722c96c15f94dde5f31be1c3d1378737e Mon Sep 17 00:00:00 2001 From: rhuairahrighairigh Date: Sat, 21 Mar 2020 19:48:01 +0000 Subject: [PATCH] add genesis tests --- x/committee/proposal_handler.go | 4 +- x/committee/types/genesis.go | 9 +-- x/committee/types/genesis_test.go | 130 +++++++++++++++++++++++++++++- x/committee/types/gov_proposal.go | 2 +- x/committee/types/types.go | 14 +++- 5 files changed, 144 insertions(+), 15 deletions(-) diff --git a/x/committee/proposal_handler.go b/x/committee/proposal_handler.go index 45cf0554..e9d1eef8 100644 --- a/x/committee/proposal_handler.go +++ b/x/committee/proposal_handler.go @@ -24,7 +24,7 @@ func NewProposalHandler(k Keeper) govtypes.Handler { func handleCommitteeChangeProposal(ctx sdk.Context, k Keeper, committeeProposal CommitteeChangeProposal) sdk.Error { if err := committeeProposal.ValidateBasic(); err != nil { - return err + return sdk.ErrInternal(err.Error()) } // Remove all committee's ongoing proposals @@ -46,7 +46,7 @@ func handleCommitteeChangeProposal(ctx sdk.Context, k Keeper, committeeProposal func handleCommitteeDeleteProposal(ctx sdk.Context, k Keeper, committeeProposal CommitteeDeleteProposal) sdk.Error { if err := committeeProposal.ValidateBasic(); err != nil { - return err + return sdk.ErrInternal(err.Error()) } // Remove all committee's ongoing proposals diff --git a/x/committee/types/genesis.go b/x/committee/types/genesis.go index a259f228..fe9a85c0 100644 --- a/x/committee/types/genesis.go +++ b/x/committee/types/genesis.go @@ -60,13 +60,8 @@ func (gs GenesisState) Validate() error { committeeMap[com.ID] = true // validate committee - if len(com.Members) == 0 { - return fmt.Errorf("committee %d invalid: cannot have zero members", com.ID) - } - for _, m := range com.Members { - if m.Empty() { - return fmt.Errorf("committee %d invalid: found empty member address", com.ID) - } + if err := com.Validate(); err != nil { + return err } } diff --git a/x/committee/types/genesis_test.go b/x/committee/types/genesis_test.go index 3a9835ee..9b76d4f2 100644 --- a/x/committee/types/genesis_test.go +++ b/x/committee/types/genesis_test.go @@ -2,22 +2,148 @@ package types import ( "testing" + "time" "github.com/stretchr/testify/require" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/gov" + "github.com/tendermint/tendermint/crypto" ) func TestGenesisState_Validate(t *testing.T) { + testTime := time.Date(1998, time.January, 1, 0, 0, 0, 0, time.UTC) + addresses := []sdk.AccAddress{ + sdk.AccAddress(crypto.AddressHash([]byte("KavaTest1"))), + sdk.AccAddress(crypto.AddressHash([]byte("KavaTest2"))), + sdk.AccAddress(crypto.AddressHash([]byte("KavaTest3"))), + sdk.AccAddress(crypto.AddressHash([]byte("KavaTest4"))), + sdk.AccAddress(crypto.AddressHash([]byte("KavaTest5"))), + } + testGenesis := GenesisState{ + NextProposalID: 2, + Committees: []Committee{ + { + ID: 1, + Members: addresses[:3], + Permissions: []Permission{GodPermission{}}, + }, + { + ID: 2, + Members: addresses[2:], + Permissions: nil, + }, + }, + Proposals: []Proposal{ + {ID: 1, CommitteeID: 1, PubProposal: gov.NewTextProposal("A Title", "A description of this proposal."), Deadline: testTime.Add(7 * 24 * time.Hour)}, + }, + Votes: []Vote{ + {ProposalID: 1, Voter: addresses[0]}, + {ProposalID: 1, Voter: addresses[1]}, + }, + } + testCases := []struct { name string genState GenesisState expectPass bool }{ { - name: "normal", + name: "default", genState: DefaultGenesisState(), expectPass: true, }, - // TODO test failure cases + { + name: "normal", + genState: testGenesis, + expectPass: true, + }, + { + name: "duplicate committee IDs", + genState: GenesisState{ + NextProposalID: testGenesis.NextProposalID, + Committees: append(testGenesis.Committees, testGenesis.Committees[0]), + Proposals: testGenesis.Proposals, + Votes: testGenesis.Votes, + }, + expectPass: false, + }, + { + name: "invalid committee", + genState: GenesisState{ + NextProposalID: testGenesis.NextProposalID, + Committees: append(testGenesis.Committees, Committee{}), + Proposals: testGenesis.Proposals, + Votes: testGenesis.Votes, + }, + expectPass: false, + }, + { + name: "duplicate proposal IDs", + genState: GenesisState{ + NextProposalID: testGenesis.NextProposalID, + Committees: testGenesis.Committees, + Proposals: append(testGenesis.Proposals, testGenesis.Proposals[0]), + Votes: testGenesis.Votes, + }, + expectPass: false, + }, + { + name: "invalid NextProposalID", + genState: GenesisState{ + NextProposalID: 0, + Committees: testGenesis.Committees, + Proposals: testGenesis.Proposals, + Votes: testGenesis.Votes, + }, + expectPass: false, + }, + { + name: "proposal without committee", + genState: GenesisState{ + NextProposalID: testGenesis.NextProposalID + 1, + Committees: testGenesis.Committees, + Proposals: append( + testGenesis.Proposals, + Proposal{ + ID: testGenesis.NextProposalID, + PubProposal: gov.NewTextProposal("A Title", "A description of this proposal."), + CommitteeID: 247, // doesn't exist + }), + Votes: testGenesis.Votes, + }, + expectPass: false, + }, + { + name: "invalid proposal", + genState: GenesisState{ + NextProposalID: testGenesis.NextProposalID, + Committees: testGenesis.Committees, + Proposals: append(testGenesis.Proposals, Proposal{}), + Votes: testGenesis.Votes, + }, + expectPass: false, + }, + { + name: "vote without proposal", + genState: GenesisState{ + NextProposalID: testGenesis.NextProposalID, + Committees: testGenesis.Committees, + Proposals: nil, + Votes: testGenesis.Votes, + }, + expectPass: false, + }, + { + name: "invalid vote", + genState: GenesisState{ + NextProposalID: testGenesis.NextProposalID, + Committees: testGenesis.Committees, + Proposals: testGenesis.Proposals, + Votes: append(testGenesis.Votes, Vote{}), + }, + expectPass: false, + }, } for _, tc := range testCases { diff --git a/x/committee/types/gov_proposal.go b/x/committee/types/gov_proposal.go index 6b2a94b6..c444d16a 100644 --- a/x/committee/types/gov_proposal.go +++ b/x/committee/types/gov_proposal.go @@ -56,7 +56,7 @@ func (ccp CommitteeChangeProposal) ValidateBasic() sdk.Error { return err } if err := ccp.NewCommittee.Validate(); err != nil { - return err + return sdk.ErrInternal(err.Error()) } return nil } diff --git a/x/committee/types/types.go b/x/committee/types/types.go index 8ef97602..0d91d979 100644 --- a/x/committee/types/types.go +++ b/x/committee/types/types.go @@ -44,17 +44,25 @@ func (c Committee) HasPermissionsFor(proposal PubProposal) bool { return false } -func (c Committee) Validate() sdk.Error { - // check for duplicate addresses +func (c Committee) Validate() error { + addressMap := make(map[string]bool, len(c.Members)) for _, m := range c.Members { // check there are no duplicate members if _, ok := addressMap[m.String()]; ok { - return sdk.ErrInternal(fmt.Sprintf("duplicate member found in committee, %s", m)) + return fmt.Errorf("duplicate member found in committee, %s", m) + } + // check for valid addresses + if m.Empty() { + return fmt.Errorf("committee %d invalid: found empty member address", c.ID) } addressMap[m.String()] = true } + + if len(c.Members) == 0 { + return fmt.Errorf("committee %d invalid: cannot have zero members", c.ID) + } return nil }