Skip to content

Commit

Permalink
Test Genesis
Browse files Browse the repository at this point in the history
  • Loading branch information
joelsmith-2019 committed Nov 27, 2023
1 parent 1f2b4c5 commit a5d52a6
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 17 deletions.
33 changes: 32 additions & 1 deletion x/clock/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,26 @@ func GetGenesisStateFromAppState(cdc codec.Codec, appState map[string]json.RawMe
}

func ValidateGenesis(data types.GenesisState) error {
return data.Params.Validate()
err := data.Params.Validate()
if err != nil {
return err
}

// Validate contracts
for _, addr := range data.ContractAddresses {
if _, err := sdk.AccAddressFromBech32(addr); err != nil {
return err
}
}

// Validate jailed contracts
for _, addr := range data.JailedContractAddresses {
if _, err := sdk.AccAddressFromBech32(addr); err != nil {
return err
}
}

return nil
}

// InitGenesis import module genesis
Expand All @@ -44,13 +63,25 @@ func InitGenesis(
k keeper.Keeper,
data types.GenesisState,
) {
// Validate init contents
if err := ValidateGenesis(data); err != nil {
panic(err)
}

// Set params
if err := k.SetParams(ctx, data.Params); err != nil {
panic(err)
}

// Register unjailed contracts
for _, addr := range data.ContractAddresses {
k.SetClockContract(ctx, addr, false)
}

// Register jailed contracts
for _, addr := range data.JailedContractAddresses {
k.SetClockContract(ctx, addr, true)
}
}

// ExportGenesis export module state
Expand Down
54 changes: 38 additions & 16 deletions x/clock/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,65 +45,87 @@ func (suite *GenesisTestSuite) TestClockInitGenesis() {
defaultParams := types.DefaultParams()

testCases := []struct {
name string
genesis types.GenesisState
expPanic bool
name string
genesis types.GenesisState
success bool
}{
{
"default genesis",
"Success - Default Genesis",
*clock.DefaultGenesisState(),
false,
true,
},
{
"custom genesis - none",
"Success - Custom Genesis",
types.GenesisState{
Params: types.Params{
ContractGasLimit: defaultParams.ContractGasLimit,
},
ContractAddresses: []string(nil),
JailedContractAddresses: []string(nil),
},
false,
true,
},
{
"custom genesis - incorrect addr",
"Fail - Incorrect Contract Address",
types.GenesisState{
Params: types.Params{
ContractGasLimit: defaultParams.ContractGasLimit,
},
ContractAddresses: []string{"incorrectaddr"},
JailedContractAddresses: []string(nil),
},
true,
false,
},
{
"custom genesis - only one addr allowed",
"Fail - Incorrect Jailed Contract Address",
types.GenesisState{
Params: types.Params{
ContractGasLimit: defaultParams.ContractGasLimit,
},
ContractAddresses: []string(nil),
JailedContractAddresses: []string{"incorrectaddr"},
},
false,
},
{
"Fail - Incorrect Invalid Contracts",
types.GenesisState{
Params: types.Params{
ContractGasLimit: defaultParams.ContractGasLimit,
},
ContractAddresses: []string{addr.String(), addr2.String()},
JailedContractAddresses: []string(nil),
},
false,
true,
},
{
"Fail - Incorrect Jailed Invalid Contracts",
types.GenesisState{
Params: types.Params{
ContractGasLimit: defaultParams.ContractGasLimit,
},
ContractAddresses: []string(nil),
JailedContractAddresses: []string{addr.String(), addr2.String()},
},
true,
},
}

for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
suite.SetupTest() // reset

if tc.expPanic {
suite.Require().Panics(func() {
clock.InitGenesis(suite.ctx, suite.app.AppKeepers.ClockKeeper, tc.genesis)
})
} else {
if tc.success {
suite.Require().NotPanics(func() {
clock.InitGenesis(suite.ctx, suite.app.AppKeepers.ClockKeeper, tc.genesis)
})

params := suite.app.AppKeepers.ClockKeeper.GetParams(suite.ctx)
suite.Require().Equal(tc.genesis.Params, params)
} else {
suite.Require().Panics(func() {
clock.InitGenesis(suite.ctx, suite.app.AppKeepers.ClockKeeper, tc.genesis)
})
}
})
}
Expand Down

0 comments on commit a5d52a6

Please sign in to comment.