mirror of
				https://github.com/0glabs/0g-chain.git
				synced 2025-11-04 03:57:44 +00:00 
			
		
		
		
	add genesis tests
This commit is contained in:
		
							parent
							
								
									fbf67b4527
								
							
						
					
					
						commit
						66d368c722
					
				@ -24,7 +24,7 @@ func NewProposalHandler(k Keeper) govtypes.Handler {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func handleCommitteeChangeProposal(ctx sdk.Context, k Keeper, committeeProposal CommitteeChangeProposal) sdk.Error {
 | 
					func handleCommitteeChangeProposal(ctx sdk.Context, k Keeper, committeeProposal CommitteeChangeProposal) sdk.Error {
 | 
				
			||||||
	if err := committeeProposal.ValidateBasic(); err != nil {
 | 
						if err := committeeProposal.ValidateBasic(); err != nil {
 | 
				
			||||||
		return err
 | 
							return sdk.ErrInternal(err.Error())
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Remove all committee's ongoing proposals
 | 
						// 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 {
 | 
					func handleCommitteeDeleteProposal(ctx sdk.Context, k Keeper, committeeProposal CommitteeDeleteProposal) sdk.Error {
 | 
				
			||||||
	if err := committeeProposal.ValidateBasic(); err != nil {
 | 
						if err := committeeProposal.ValidateBasic(); err != nil {
 | 
				
			||||||
		return err
 | 
							return sdk.ErrInternal(err.Error())
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Remove all committee's ongoing proposals
 | 
						// Remove all committee's ongoing proposals
 | 
				
			||||||
 | 
				
			|||||||
@ -60,13 +60,8 @@ func (gs GenesisState) Validate() error {
 | 
				
			|||||||
		committeeMap[com.ID] = true
 | 
							committeeMap[com.ID] = true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// validate committee
 | 
							// validate committee
 | 
				
			||||||
		if len(com.Members) == 0 {
 | 
							if err := com.Validate(); err != nil {
 | 
				
			||||||
			return fmt.Errorf("committee %d invalid: cannot have zero members", com.ID)
 | 
								return err
 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		for _, m := range com.Members {
 | 
					 | 
				
			||||||
			if m.Empty() {
 | 
					 | 
				
			||||||
				return fmt.Errorf("committee %d invalid: found empty member address", com.ID)
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -2,22 +2,148 @@ package types
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/stretchr/testify/require"
 | 
						"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) {
 | 
					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 {
 | 
						testCases := []struct {
 | 
				
			||||||
		name       string
 | 
							name       string
 | 
				
			||||||
		genState   GenesisState
 | 
							genState   GenesisState
 | 
				
			||||||
		expectPass bool
 | 
							expectPass bool
 | 
				
			||||||
	}{
 | 
						}{
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			name:       "normal",
 | 
								name:       "default",
 | 
				
			||||||
			genState:   DefaultGenesisState(),
 | 
								genState:   DefaultGenesisState(),
 | 
				
			||||||
			expectPass: true,
 | 
								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 {
 | 
						for _, tc := range testCases {
 | 
				
			||||||
 | 
				
			|||||||
@ -56,7 +56,7 @@ func (ccp CommitteeChangeProposal) ValidateBasic() sdk.Error {
 | 
				
			|||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if err := ccp.NewCommittee.Validate(); err != nil {
 | 
						if err := ccp.NewCommittee.Validate(); err != nil {
 | 
				
			||||||
		return err
 | 
							return sdk.ErrInternal(err.Error())
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -44,17 +44,25 @@ func (c Committee) HasPermissionsFor(proposal PubProposal) bool {
 | 
				
			|||||||
	return false
 | 
						return false
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (c Committee) Validate() sdk.Error {
 | 
					func (c Committee) Validate() error {
 | 
				
			||||||
	// check for duplicate addresses
 | 
					
 | 
				
			||||||
	addressMap := make(map[string]bool, len(c.Members))
 | 
						addressMap := make(map[string]bool, len(c.Members))
 | 
				
			||||||
	for _, m := range c.Members {
 | 
						for _, m := range c.Members {
 | 
				
			||||||
		// check there are no duplicate members
 | 
							// check there are no duplicate members
 | 
				
			||||||
		if _, ok := addressMap[m.String()]; ok {
 | 
							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
 | 
							addressMap[m.String()] = true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if len(c.Members) == 0 {
 | 
				
			||||||
 | 
							return fmt.Errorf("committee %d invalid: cannot have zero members", c.ID)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user