2020-03-15 00:00:05 +00:00
|
|
|
package committee_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2021-06-07 16:08:03 +00:00
|
|
|
"time"
|
2020-03-15 00:00:05 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
2020-04-30 14:23:41 +00:00
|
|
|
|
2020-03-15 00:00:05 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2022-01-08 00:39:27 +00:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2020-03-15 00:00:05 +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-15 00:00:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type GenesisTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
2021-06-07 16:08:03 +00:00
|
|
|
app app.TestApp
|
|
|
|
ctx sdk.Context
|
2022-01-08 00:39:27 +00:00
|
|
|
keeper keeper.Keeper
|
2021-06-07 16:08:03 +00:00
|
|
|
addresses []sdk.AccAddress
|
2020-03-15 00:00:05 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 16:08:03 +00:00
|
|
|
func (suite *GenesisTestSuite) SetupTest() {
|
|
|
|
suite.app = app.NewTestApp()
|
|
|
|
suite.keeper = suite.app.GetCommitteeKeeper()
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.ctx = suite.app.NewContext(true, tmproto.Header{})
|
2021-06-07 16:08:03 +00:00
|
|
|
_, suite.addresses = app.GeneratePrivKeyAddressPairs(10)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *GenesisTestSuite) TestInitGenesis() {
|
2022-01-08 00:39:27 +00:00
|
|
|
memberCom := types.MustNewMemberCommittee(
|
|
|
|
1,
|
|
|
|
"This member committee is for testing.",
|
|
|
|
suite.addresses[:2],
|
|
|
|
[]types.Permission{&types.GodPermission{}},
|
|
|
|
testutil.D("0.667"),
|
|
|
|
time.Hour*24*7,
|
|
|
|
types.TALLY_OPTION_FIRST_PAST_THE_POST,
|
|
|
|
)
|
2021-06-07 16:08:03 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
tokenCom := types.MustNewTokenCommittee(
|
|
|
|
1,
|
|
|
|
"This token committee is for testing.",
|
|
|
|
suite.addresses[:2],
|
|
|
|
[]types.Permission{&types.GodPermission{}},
|
|
|
|
testutil.D("0.667"),
|
|
|
|
time.Hour*24*7,
|
|
|
|
types.TALLY_OPTION_FIRST_PAST_THE_POST,
|
|
|
|
testutil.D("0.4"),
|
|
|
|
"hard",
|
|
|
|
)
|
2021-06-07 16:08:03 +00:00
|
|
|
|
|
|
|
// Most genesis validation tests are located in the types directory. The 'invalid' test cases are
|
|
|
|
// randomly selected subset of those tests.
|
2020-03-15 00:00:05 +00:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
2022-01-08 00:39:27 +00:00
|
|
|
genState *types.GenesisState
|
2020-03-15 00:00:05 +00:00
|
|
|
expectPass bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "normal",
|
|
|
|
genState: types.DefaultGenesisState(),
|
|
|
|
expectPass: true,
|
|
|
|
},
|
|
|
|
{
|
2021-06-07 16:08:03 +00:00
|
|
|
name: "member committee is correctly validated",
|
|
|
|
genState: types.NewGenesisState(
|
|
|
|
1,
|
|
|
|
[]types.Committee{memberCom},
|
|
|
|
[]types.Proposal{},
|
|
|
|
[]types.Vote{},
|
|
|
|
),
|
|
|
|
expectPass: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "token committee is correctly validated",
|
|
|
|
genState: types.NewGenesisState(
|
|
|
|
1,
|
|
|
|
[]types.Committee{tokenCom},
|
|
|
|
[]types.Proposal{},
|
|
|
|
[]types.Vote{},
|
|
|
|
),
|
|
|
|
expectPass: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid: duplicate committee ID",
|
|
|
|
genState: types.NewGenesisState(
|
|
|
|
1,
|
|
|
|
[]types.Committee{memberCom, memberCom},
|
|
|
|
[]types.Proposal{},
|
|
|
|
[]types.Vote{},
|
|
|
|
),
|
|
|
|
expectPass: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid: proposal doesn't have committee",
|
2020-03-15 00:00:05 +00:00
|
|
|
genState: types.NewGenesisState(
|
|
|
|
2,
|
|
|
|
[]types.Committee{},
|
|
|
|
[]types.Proposal{{ID: 1, CommitteeID: 57}},
|
|
|
|
[]types.Vote{},
|
|
|
|
),
|
|
|
|
expectPass: false,
|
|
|
|
},
|
2021-06-07 16:08:03 +00:00
|
|
|
{
|
|
|
|
name: "invalid: vote doesn't have proposal",
|
|
|
|
genState: types.NewGenesisState(
|
|
|
|
1,
|
|
|
|
[]types.Committee{},
|
|
|
|
[]types.Proposal{},
|
2022-01-08 00:39:27 +00:00
|
|
|
[]types.Vote{{Voter: suite.addresses[0], ProposalID: 1, VoteType: types.VOTE_TYPE_YES}},
|
2021-06-07 16:08:03 +00:00
|
|
|
),
|
|
|
|
expectPass: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid: next proposal ID isn't greater than proposal ID",
|
|
|
|
genState: types.NewGenesisState(
|
|
|
|
4,
|
|
|
|
[]types.Committee{memberCom},
|
|
|
|
[]types.Proposal{{ID: 3, CommitteeID: 1}, {ID: 4, CommitteeID: 1}},
|
|
|
|
[]types.Vote{},
|
|
|
|
),
|
|
|
|
expectPass: false,
|
|
|
|
},
|
2020-03-15 00:00:05 +00:00
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(tc.name, func() {
|
|
|
|
// Setup (note: suite.SetupTest is not run before every suite.Run)
|
|
|
|
suite.app = app.NewTestApp()
|
|
|
|
suite.keeper = suite.app.GetCommitteeKeeper()
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.ctx = suite.app.NewContext(true, tmproto.Header{})
|
2020-03-15 00:00:05 +00:00
|
|
|
|
|
|
|
// Run
|
2022-01-08 00:39:27 +00:00
|
|
|
var exportedGenState *types.GenesisState
|
2020-03-15 00:00:05 +00:00
|
|
|
run := func() {
|
|
|
|
committee.InitGenesis(suite.ctx, suite.keeper, tc.genState)
|
|
|
|
exportedGenState = committee.ExportGenesis(suite.ctx, suite.keeper)
|
|
|
|
}
|
|
|
|
if tc.expectPass {
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.Require().NotPanics(run)
|
2020-03-15 00:00:05 +00:00
|
|
|
} else {
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.Require().Panics(run)
|
2020-03-15 00:00:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check
|
|
|
|
if tc.expectPass {
|
2022-01-08 00:39:27 +00:00
|
|
|
expectedJson, err := suite.app.AppCodec().MarshalJSON(tc.genState)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
actualJson, err := suite.app.AppCodec().MarshalJSON(exportedGenState)
|
|
|
|
suite.Equal(expectedJson, actualJson)
|
2020-03-15 00:00:05 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenesisTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(GenesisTestSuite))
|
|
|
|
}
|