0g-chain/x/dasigners/v1/genesis_test.go

156 lines
4.4 KiB
Go
Raw Normal View History

2024-08-06 06:43:05 +00:00
package dasigners_test
import (
"testing"
"github.com/stretchr/testify/suite"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/0glabs/0g-chain/app"
"github.com/0glabs/0g-chain/x/dasigners/v1"
2024-08-06 10:03:13 +00:00
"github.com/0glabs/0g-chain/x/dasigners/v1/testutil"
2024-08-06 06:43:05 +00:00
"github.com/0glabs/0g-chain/x/dasigners/v1/types"
)
type GenesisTestSuite struct {
2024-08-06 10:03:13 +00:00
testutil.Suite
2024-08-06 06:43:05 +00:00
}
func (suite *GenesisTestSuite) TestInitGenesis() {
// Most genesis validation tests are located in the types directory. The 'invalid' test cases are
// randomly selected subset of those tests.
testCases := []struct {
name string
genState *types.GenesisState
expectPass bool
}{
{
name: "normal",
genState: types.DefaultGenesisState(),
expectPass: true,
},
2024-08-06 07:24:53 +00:00
{
name: "normal-more-epochs",
genState: types.NewGenesisState(types.Params{
TokensPerVote: 10,
MaxVotesPerSigner: 1024,
MaxQuorums: 10,
EpochBlocks: 5760,
EncodedSlices: 1,
}, 0, []*types.Signer{{
Account: "0000000000000000000000000000000000000001",
Socket: "0.0.0.0:1234",
PubkeyG1: make([]byte, 64),
PubkeyG2: make([]byte, 128),
}}, []*types.Quorums{{
Quorums: []*types.Quorum{{Signers: []string{"0000000000000000000000000000000000000001"}}},
}}),
expectPass: true,
},
{
name: "invalid account format",
genState: types.NewGenesisState(types.Params{
TokensPerVote: 10,
MaxVotesPerSigner: 1024,
MaxQuorums: 10,
EpochBlocks: 5760,
EncodedSlices: 1,
}, 0, []*types.Signer{{
Account: "0x0000000000000000000000000000000000000001",
Socket: "0.0.0.0:1234",
PubkeyG1: make([]byte, 64),
PubkeyG2: make([]byte, 128),
}}, []*types.Quorums{{
Quorums: []*types.Quorum{{Signers: []string{"0x0000000000000000000000000000000000000001"}}},
}}),
expectPass: false,
},
{
name: "invalid pubkeyG1 format",
genState: types.NewGenesisState(types.Params{
TokensPerVote: 10,
MaxVotesPerSigner: 1024,
MaxQuorums: 10,
EpochBlocks: 5760,
EncodedSlices: 1,
}, 0, []*types.Signer{{
Account: "0000000000000000000000000000000000000001",
Socket: "0.0.0.0:1234",
PubkeyG1: make([]byte, 63),
PubkeyG2: make([]byte, 128),
}}, []*types.Quorums{{
Quorums: []*types.Quorum{{Signers: []string{"0000000000000000000000000000000000000001"}}},
}}),
expectPass: false,
},
{
name: "invalid pubkeyG2 format",
genState: types.NewGenesisState(types.Params{
TokensPerVote: 10,
MaxVotesPerSigner: 1024,
MaxQuorums: 10,
EpochBlocks: 5760,
EncodedSlices: 1,
}, 0, []*types.Signer{{
Account: "0000000000000000000000000000000000000001",
Socket: "0.0.0.0:1234",
PubkeyG1: make([]byte, 64),
PubkeyG2: make([]byte, 129),
}}, []*types.Quorums{{
Quorums: []*types.Quorum{{Signers: []string{"0000000000000000000000000000000000000001"}}},
}}),
expectPass: false,
},
{
name: "history missing",
genState: types.NewGenesisState(types.Params{
TokensPerVote: 10,
MaxVotesPerSigner: 1024,
MaxQuorums: 10,
EpochBlocks: 5760,
EncodedSlices: 1,
}, 0, []*types.Signer{{
Account: "0000000000000000000000000000000000000001",
Socket: "0.0.0.0:1234",
PubkeyG1: make([]byte, 64),
PubkeyG2: make([]byte, 128),
}}, []*types.Quorums{}),
expectPass: false,
},
2024-08-06 06:43:05 +00:00
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
// Setup (note: suite.SetupTest is not run before every suite.Run)
2024-08-06 10:03:13 +00:00
suite.App = app.NewTestApp()
suite.Keeper = suite.App.GetDASignersKeeper()
suite.Ctx = suite.App.NewContext(true, tmproto.Header{})
2024-08-06 06:43:05 +00:00
// Run
var exportedGenState *types.GenesisState
run := func() {
2024-08-06 10:03:13 +00:00
dasigners.InitGenesis(suite.Ctx, suite.Keeper, *tc.genState)
exportedGenState = dasigners.ExportGenesis(suite.Ctx, suite.Keeper)
2024-08-06 06:43:05 +00:00
}
if tc.expectPass {
suite.Require().NotPanics(run)
} else {
suite.Require().Panics(run)
}
// Check
if tc.expectPass {
2024-08-06 10:03:13 +00:00
expectedJson, err := suite.App.AppCodec().MarshalJSON(tc.genState)
2024-08-06 06:43:05 +00:00
suite.Require().NoError(err)
2024-08-06 10:03:13 +00:00
actualJson, err := suite.App.AppCodec().MarshalJSON(exportedGenState)
2024-08-06 06:43:05 +00:00
suite.Require().NoError(err)
suite.Equal(expectedJson, actualJson)
}
})
}
}
func TestGenesisTestSuite(t *testing.T) {
suite.Run(t, new(GenesisTestSuite))
}