2020-03-21 18:06:58 +00:00
package committee_test
import (
"testing"
"time"
2020-04-30 14:23:41 +00:00
"github.com/stretchr/testify/suite"
2020-03-21 18:06:58 +00:00
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
2023-04-04 00:08:45 +00:00
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
2020-04-30 14:23:41 +00:00
2022-01-08 00:39:27 +00:00
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
2020-03-21 18:06:58 +00:00
2024-05-01 03:17:24 +00:00
"github.com/0glabs/0g-chain/app"
"github.com/0glabs/0g-chain/x/committee"
"github.com/0glabs/0g-chain/x/committee/keeper"
"github.com/0glabs/0g-chain/x/committee/testutil"
"github.com/0glabs/0g-chain/x/committee/types"
2020-03-21 18:06:58 +00:00
)
var testTime time . Time = time . Date ( 1998 , time . January , 1 , 0 , 0 , 0 , 0 , time . UTC )
2022-01-08 00:39:27 +00:00
func NewCommitteeGenState ( cdc codec . Codec , gs * types . GenesisState ) app . GenesisState {
return app . GenesisState { types . ModuleName : cdc . MustMarshalJSON ( gs ) }
2020-03-21 18:06:58 +00:00
}
type ProposalHandlerTestSuite struct {
suite . Suite
2022-01-08 00:39:27 +00:00
keeper keeper . Keeper
2020-03-21 18:06:58 +00:00
app app . TestApp
ctx sdk . Context
addresses [ ] sdk . AccAddress
2022-01-08 00:39:27 +00:00
testGenesis * types . GenesisState
2020-03-21 18:06:58 +00:00
}
func ( suite * ProposalHandlerTestSuite ) SetupTest ( ) {
_ , suite . addresses = app . GeneratePrivKeyAddressPairs ( 5 )
2022-01-08 00:39:27 +00:00
suite . testGenesis = types . NewGenesisState (
2020-03-21 18:06:58 +00:00
2 ,
2022-01-08 00:39:27 +00:00
[ ] types . Committee {
types . MustNewMemberCommittee (
1 ,
"This committee is for testing." ,
suite . addresses [ : 3 ] ,
[ ] types . Permission { & types . GodPermission { } } ,
testutil . D ( "0.667" ) ,
time . Hour * 24 * 7 ,
types . TALLY_OPTION_FIRST_PAST_THE_POST ,
) ,
types . MustNewMemberCommittee (
2 ,
"member committee" ,
suite . addresses [ 2 : ] ,
nil ,
testutil . D ( "0.667" ) ,
time . Hour * 24 * 7 ,
types . TALLY_OPTION_FIRST_PAST_THE_POST ,
) ,
2020-03-21 18:06:58 +00:00
} ,
2022-01-08 00:39:27 +00:00
types . Proposals {
types . MustNewProposal (
2023-04-04 00:08:45 +00:00
govv1beta1 . NewTextProposal ( "A Title" , "A description of this proposal." ) , 1 , 1 , testTime . Add ( 7 * 24 * time . Hour ) ,
2022-01-08 00:39:27 +00:00
) ,
2020-03-21 18:06:58 +00:00
} ,
2022-01-08 00:39:27 +00:00
[ ] types . Vote {
{ ProposalID : 1 , Voter : suite . addresses [ 0 ] , VoteType : types . VOTE_TYPE_YES } ,
2020-03-21 18:06:58 +00:00
} ,
)
}
func ( suite * ProposalHandlerTestSuite ) TestProposalHandler_ChangeCommittee ( ) {
testCases := [ ] struct {
name string
2022-01-08 00:39:27 +00:00
proposal types . CommitteeChangeProposal
2020-03-21 18:06:58 +00:00
expectPass bool
} {
{
name : "add new" ,
2022-01-08 00:39:27 +00:00
proposal : types . MustNewCommitteeChangeProposal (
2020-03-21 18:06:58 +00:00
"A Title" ,
"A proposal description." ,
2022-01-08 00:39:27 +00:00
types . MustNewMemberCommittee (
34 ,
"member committee" ,
suite . addresses [ : 1 ] ,
[ ] types . Permission { } ,
testutil . D ( "1" ) ,
time . Hour * 24 ,
types . TALLY_OPTION_DEADLINE ,
) ,
2020-03-21 18:06:58 +00:00
) ,
expectPass : true ,
} ,
{
name : "update" ,
2022-01-08 00:39:27 +00:00
proposal : types . MustNewCommitteeChangeProposal (
2020-03-21 18:06:58 +00:00
"A Title" ,
"A proposal description." ,
2022-01-08 00:39:27 +00:00
types . MustNewMemberCommittee (
suite . testGenesis . GetCommittees ( ) [ 0 ] . GetID ( ) ,
"member committee" ,
suite . addresses , // add new members
suite . testGenesis . GetCommittees ( ) [ 0 ] . GetPermissions ( ) ,
suite . testGenesis . GetCommittees ( ) [ 0 ] . GetVoteThreshold ( ) ,
suite . testGenesis . GetCommittees ( ) [ 0 ] . GetProposalDuration ( ) ,
types . TALLY_OPTION_FIRST_PAST_THE_POST ,
) ,
2020-03-21 18:06:58 +00:00
) ,
expectPass : true ,
} ,
{
name : "invalid title" ,
2022-01-08 00:39:27 +00:00
proposal : types . MustNewCommitteeChangeProposal (
2020-04-30 14:13:31 +00:00
"A Title That Is Much Too Long And Really Quite Unreasonable Given That It Is Trying To Fulfill The Roll Of An Acceptable Governance Proposal Title That Should Succinctly Communicate The Goal And Contents Of The Proposed Proposal To All Parties Involved" ,
2020-03-21 18:06:58 +00:00
"A proposal description." ,
2022-01-08 00:39:27 +00:00
suite . testGenesis . GetCommittees ( ) [ 0 ] ,
2020-03-21 18:06:58 +00:00
) ,
expectPass : false ,
} ,
{
name : "invalid committee" ,
2022-01-08 00:39:27 +00:00
proposal : types . MustNewCommitteeChangeProposal (
2020-03-21 18:06:58 +00:00
"A Title" ,
"A proposal description." ,
2022-01-08 00:39:27 +00:00
types . MustNewMemberCommittee (
suite . testGenesis . GetCommittees ( ) [ 0 ] . GetID ( ) ,
"member committee" ,
append ( suite . addresses , suite . addresses [ 0 ] ) , // duplicate address
suite . testGenesis . GetCommittees ( ) [ 0 ] . GetPermissions ( ) ,
suite . testGenesis . GetCommittees ( ) [ 0 ] . GetVoteThreshold ( ) ,
suite . testGenesis . GetCommittees ( ) [ 0 ] . GetProposalDuration ( ) ,
types . TALLY_OPTION_DEADLINE ,
) ,
2020-03-21 18:06:58 +00:00
) ,
expectPass : false ,
} ,
}
for _ , tc := range testCases {
suite . Run ( tc . name , func ( ) {
// Setup
suite . app = app . NewTestApp ( )
suite . keeper = suite . app . GetCommitteeKeeper ( )
suite . app = suite . app . InitializeFromGenesisStates (
2022-01-08 00:39:27 +00:00
NewCommitteeGenState ( suite . app . AppCodec ( ) , suite . testGenesis ) ,
2020-03-21 18:06:58 +00:00
)
2022-01-08 00:39:27 +00:00
suite . ctx = suite . app . NewContext ( true , tmproto . Header { Height : 1 , Time : testTime } )
2020-03-21 18:06:58 +00:00
handler := committee . NewProposalHandler ( suite . keeper )
2022-01-08 00:39:27 +00:00
oldProposals := suite . keeper . GetProposalsByCommittee ( suite . ctx , tc . proposal . GetNewCommittee ( ) . GetID ( ) )
2020-03-21 18:06:58 +00:00
// Run
2022-01-08 00:39:27 +00:00
err := handler ( suite . ctx , & tc . proposal )
2020-03-21 18:06:58 +00:00
// Check
if tc . expectPass {
suite . NoError ( err )
2020-04-27 14:04:47 +00:00
// check committee is accurate
2022-01-08 00:39:27 +00:00
actualCom , found := suite . keeper . GetCommittee ( suite . ctx , tc . proposal . GetNewCommittee ( ) . GetID ( ) )
2020-03-21 18:06:58 +00:00
suite . True ( found )
2022-01-08 00:39:27 +00:00
testutil . AssertProtoMessageJSON ( suite . T ( ) , suite . app . AppCodec ( ) , tc . proposal . GetNewCommittee ( ) , actualCom )
2020-03-21 18:06:58 +00:00
// check proposals and votes for this committee have been removed
2022-01-08 00:39:27 +00:00
suite . Empty ( suite . keeper . GetProposalsByCommittee ( suite . ctx , tc . proposal . GetNewCommittee ( ) . GetID ( ) ) )
2020-04-27 14:04:47 +00:00
for _ , p := range oldProposals {
suite . Empty ( suite . keeper . GetVotesByProposal ( suite . ctx , p . ID ) )
2020-03-21 18:06:58 +00:00
}
} else {
suite . Error ( err )
2022-01-08 00:39:27 +00:00
testutil . AssertProtoMessageJSON ( suite . T ( ) , suite . app . AppCodec ( ) , suite . testGenesis , committee . ExportGenesis ( suite . ctx , suite . keeper ) )
2020-03-21 18:06:58 +00:00
}
} )
}
}
func ( suite * ProposalHandlerTestSuite ) TestProposalHandler_DeleteCommittee ( ) {
testCases := [ ] struct {
name string
2022-01-08 00:39:27 +00:00
proposal types . CommitteeDeleteProposal
2020-03-21 18:06:58 +00:00
expectPass bool
} {
{
name : "normal" ,
2022-01-08 00:39:27 +00:00
proposal : types . NewCommitteeDeleteProposal (
2020-03-21 18:06:58 +00:00
"A Title" ,
"A proposal description." ,
2022-01-08 00:39:27 +00:00
suite . testGenesis . GetCommittees ( ) [ 0 ] . GetID ( ) ,
2020-03-21 18:06:58 +00:00
) ,
expectPass : true ,
} ,
{
name : "invalid title" ,
2022-01-08 00:39:27 +00:00
proposal : types . NewCommitteeDeleteProposal (
2020-04-30 14:13:31 +00:00
"A Title That Is Much Too Long And Really Quite Unreasonable Given That It Is Trying To Fulfill The Roll Of An Acceptable Governance Proposal Title That Should Succinctly Communicate The Goal And Contents Of The Proposed Proposal To All Parties Involved" ,
2020-03-21 18:06:58 +00:00
"A proposal description." ,
2022-01-08 00:39:27 +00:00
suite . testGenesis . GetCommittees ( ) [ 1 ] . GetID ( ) ,
2020-03-21 18:06:58 +00:00
) ,
expectPass : false ,
} ,
}
for _ , tc := range testCases {
suite . Run ( tc . name , func ( ) {
// Setup
suite . app = app . NewTestApp ( )
suite . keeper = suite . app . GetCommitteeKeeper ( )
suite . app = suite . app . InitializeFromGenesisStates (
2022-01-08 00:39:27 +00:00
NewCommitteeGenState ( suite . app . AppCodec ( ) , suite . testGenesis ) ,
2020-03-21 18:06:58 +00:00
)
2022-01-08 00:39:27 +00:00
suite . ctx = suite . app . NewContext ( true , tmproto . Header { Height : 1 , Time : testTime } )
2020-03-21 18:06:58 +00:00
handler := committee . NewProposalHandler ( suite . keeper )
2020-04-27 14:04:47 +00:00
oldProposals := suite . keeper . GetProposalsByCommittee ( suite . ctx , tc . proposal . CommitteeID )
2020-03-21 18:06:58 +00:00
// Run
2022-01-08 00:39:27 +00:00
err := handler ( suite . ctx , & tc . proposal )
2020-03-21 18:06:58 +00:00
// Check
if tc . expectPass {
suite . NoError ( err )
2020-04-27 14:04:47 +00:00
// check committee has been removed
2020-03-21 18:06:58 +00:00
_ , found := suite . keeper . GetCommittee ( suite . ctx , tc . proposal . CommitteeID )
suite . False ( found )
// check proposals and votes for this committee have been removed
2020-04-27 14:04:47 +00:00
suite . Empty ( suite . keeper . GetProposalsByCommittee ( suite . ctx , tc . proposal . CommitteeID ) )
for _ , p := range oldProposals {
suite . Empty ( suite . keeper . GetVotesByProposal ( suite . ctx , p . ID ) )
2020-03-21 18:06:58 +00:00
}
} else {
suite . Error ( err )
2022-01-08 00:39:27 +00:00
testutil . AssertProtoMessageJSON ( suite . T ( ) , suite . app . AppCodec ( ) , suite . testGenesis , committee . ExportGenesis ( suite . ctx , suite . keeper ) )
2020-03-21 18:06:58 +00:00
}
} )
}
}
func TestProposalHandlerTestSuite ( t * testing . T ) {
suite . Run ( t , new ( ProposalHandlerTestSuite ) )
}