cosmos-sdk/x/params/types/subspace_test.go

245 lines
6.3 KiB
Go
Raw Normal View History

package types_test
2019-12-10 08:48:57 -08:00
import (
"bytes"
2019-12-10 08:48:57 -08:00
"fmt"
"testing"
"time"
refactor: Remove store type aliases (#10295) <!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description Closes: #9362 <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [X] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [X] added `!` to the type prefix if API or client breaking change - [X] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [X] provided a link to the relevant issue or specification - [X] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [X] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [X] added a changelog entry to `CHANGELOG.md` - [X] included comments for [documenting Go code](https://blog.golang.org/godoc) - [X] updated the relevant documentation or specification - [X] reviewed "Files changed" and left comments if necessary - [X] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2021-10-04 09:36:38 -07:00
storetypes "github.com/cosmos/cosmos-sdk/store/types"
2019-12-10 08:48:57 -08:00
"github.com/stretchr/testify/suite"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
2019-12-10 08:48:57 -08:00
dbm "github.com/tendermint/tm-db"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/params/types"
2019-12-10 08:48:57 -08:00
)
type SubspaceTestSuite struct {
suite.Suite
cdc codec.BinaryCodec
amino *codec.LegacyAmino
ctx sdk.Context
ss types.Subspace
2019-12-10 08:48:57 -08:00
}
func (suite *SubspaceTestSuite) SetupTest() {
db := dbm.NewMemDB()
ms := store.NewCommitMultiStore(db)
refactor: Remove store type aliases (#10295) <!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description Closes: #9362 <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [X] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [X] added `!` to the type prefix if API or client breaking change - [X] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [X] provided a link to the relevant issue or specification - [X] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [X] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [X] added a changelog entry to `CHANGELOG.md` - [X] included comments for [documenting Go code](https://blog.golang.org/godoc) - [X] updated the relevant documentation or specification - [X] reviewed "Files changed" and left comments if necessary - [X] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2021-10-04 09:36:38 -07:00
ms.MountStoreWithDB(key, storetypes.StoreTypeIAVL, db)
ms.MountStoreWithDB(tkey, storetypes.StoreTypeTransient, db)
2019-12-10 08:48:57 -08:00
suite.NoError(ms.LoadLatestVersion())
encCfg := simapp.MakeTestEncodingConfig()
refactor!: Remove `clientCtx.JSONCodec` and rename `EncodingConfig.Marshaler` to `Codec` (#9521) <!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description Closes: #9499 <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [x] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [x] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2021-07-01 01:52:38 -07:00
ss := types.NewSubspace(encCfg.Codec, encCfg.Amino, key, tkey, "testsubspace")
2019-12-10 08:48:57 -08:00
refactor!: Remove `clientCtx.JSONCodec` and rename `EncodingConfig.Marshaler` to `Codec` (#9521) <!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description Closes: #9499 <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [x] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [x] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2021-07-01 01:52:38 -07:00
suite.cdc = encCfg.Codec
suite.amino = encCfg.Amino
suite.ctx = sdk.NewContext(ms, tmproto.Header{}, false, log.NewNopLogger())
2019-12-10 08:48:57 -08:00
suite.ss = ss.WithKeyTable(paramKeyTable())
}
func (suite *SubspaceTestSuite) TestKeyTable() {
suite.Require().True(suite.ss.HasKeyTable())
suite.Require().Panics(func() {
suite.ss.WithKeyTable(paramKeyTable())
})
suite.Require().NotPanics(func() {
ss := types.NewSubspace(suite.cdc, suite.amino, key, tkey, "testsubspace2")
2019-12-10 08:48:57 -08:00
ss = ss.WithKeyTable(paramKeyTable())
})
}
func (suite *SubspaceTestSuite) TestGetSet() {
var v time.Duration
t := time.Hour * 48
suite.Require().Panics(func() {
suite.ss.Get(suite.ctx, keyUnbondingTime, &v)
})
suite.Require().NotEqual(t, v)
suite.Require().NotPanics(func() {
suite.ss.Set(suite.ctx, keyUnbondingTime, t)
})
suite.Require().NotPanics(func() {
suite.ss.Get(suite.ctx, keyUnbondingTime, &v)
})
suite.Require().Equal(t, v)
}
func (suite *SubspaceTestSuite) TestGetIfExists() {
var v time.Duration
suite.Require().NotPanics(func() {
suite.ss.GetIfExists(suite.ctx, keyUnbondingTime, &v)
})
suite.Require().Equal(time.Duration(0), v)
}
func (suite *SubspaceTestSuite) TestGetRaw() {
t := time.Hour * 48
suite.Require().NotPanics(func() {
suite.ss.Set(suite.ctx, keyUnbondingTime, t)
})
suite.Require().NotPanics(func() {
res := suite.ss.GetRaw(suite.ctx, keyUnbondingTime)
suite.Require().Equal("2231373238303030303030303030303022", fmt.Sprintf("%X", res))
})
}
func (suite *SubspaceTestSuite) TestIterateKeys() {
suite.Require().NotPanics(func() {
suite.ss.Set(suite.ctx, keyUnbondingTime, time.Second)
})
suite.Require().NotPanics(func() {
suite.ss.Set(suite.ctx, keyMaxValidators, uint16(50))
})
suite.Require().NotPanics(func() {
suite.ss.Set(suite.ctx, keyBondDenom, "stake")
})
var keys [][]byte
suite.ss.IterateKeys(suite.ctx, func(key []byte) bool {
keys = append(keys, key)
return false
})
suite.Require().Len(keys, 3)
suite.Require().Contains(keys, keyUnbondingTime)
suite.Require().Contains(keys, keyMaxValidators)
suite.Require().Contains(keys, keyBondDenom)
var keys2 [][]byte
suite.ss.IterateKeys(suite.ctx, func(key []byte) bool {
if bytes.Equal(key, keyUnbondingTime) {
return true
}
keys2 = append(keys2, key)
return false
})
suite.Require().Len(keys2, 2)
suite.Require().Contains(keys2, keyMaxValidators)
suite.Require().Contains(keys2, keyBondDenom)
}
2019-12-10 08:48:57 -08:00
func (suite *SubspaceTestSuite) TestHas() {
t := time.Hour * 48
suite.Require().False(suite.ss.Has(suite.ctx, keyUnbondingTime))
suite.Require().NotPanics(func() {
suite.ss.Set(suite.ctx, keyUnbondingTime, t)
})
suite.Require().True(suite.ss.Has(suite.ctx, keyUnbondingTime))
}
func (suite *SubspaceTestSuite) TestModified() {
t := time.Hour * 48
suite.Require().False(suite.ss.Modified(suite.ctx, keyUnbondingTime))
suite.Require().NotPanics(func() {
suite.ss.Set(suite.ctx, keyUnbondingTime, t)
})
suite.Require().True(suite.ss.Modified(suite.ctx, keyUnbondingTime))
}
func (suite *SubspaceTestSuite) TestUpdate() {
suite.Require().Panics(func() {
suite.ss.Update(suite.ctx, []byte("invalid_key"), nil) // nolint:errcheck
2019-12-10 08:48:57 -08:00
})
t := time.Hour * 48
suite.Require().NotPanics(func() {
suite.ss.Set(suite.ctx, keyUnbondingTime, t)
})
bad := time.Minute * 5
bz, err := suite.amino.MarshalJSON(bad)
2019-12-10 08:48:57 -08:00
suite.Require().NoError(err)
suite.Require().Error(suite.ss.Update(suite.ctx, keyUnbondingTime, bz))
good := time.Hour * 360
bz, err = suite.amino.MarshalJSON(good)
2019-12-10 08:48:57 -08:00
suite.Require().NoError(err)
suite.Require().NoError(suite.ss.Update(suite.ctx, keyUnbondingTime, bz))
var v time.Duration
suite.Require().NotPanics(func() {
suite.ss.Get(suite.ctx, keyUnbondingTime, &v)
})
suite.Require().Equal(good, v)
}
func (suite *SubspaceTestSuite) TestGetParamSet() {
a := params{
UnbondingTime: time.Hour * 48,
MaxValidators: 100,
BondDenom: "stake",
}
suite.Require().NotPanics(func() {
suite.ss.Set(suite.ctx, keyUnbondingTime, a.UnbondingTime)
suite.ss.Set(suite.ctx, keyMaxValidators, a.MaxValidators)
suite.ss.Set(suite.ctx, keyBondDenom, a.BondDenom)
})
b := params{}
suite.Require().NotPanics(func() {
suite.ss.GetParamSet(suite.ctx, &b)
})
suite.Require().Equal(a.UnbondingTime, b.UnbondingTime)
suite.Require().Equal(a.MaxValidators, b.MaxValidators)
suite.Require().Equal(a.BondDenom, b.BondDenom)
}
func (suite *SubspaceTestSuite) TestSetParamSet() {
testCases := []struct {
name string
ps types.ParamSet
2019-12-10 08:48:57 -08:00
}{
{"invalid unbonding time", &params{time.Hour * 1, 100, "stake"}},
{"invalid bond denom", &params{time.Hour * 48, 100, ""}},
}
for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
suite.Require().Panics(func() {
suite.ss.SetParamSet(suite.ctx, tc.ps)
})
})
}
a := params{
UnbondingTime: time.Hour * 48,
MaxValidators: 100,
BondDenom: "stake",
}
suite.Require().NotPanics(func() {
suite.ss.SetParamSet(suite.ctx, &a)
})
b := params{}
suite.Require().NotPanics(func() {
suite.ss.GetParamSet(suite.ctx, &b)
})
suite.Require().Equal(a.UnbondingTime, b.UnbondingTime)
suite.Require().Equal(a.MaxValidators, b.MaxValidators)
suite.Require().Equal(a.BondDenom, b.BondDenom)
}
func (suite *SubspaceTestSuite) TestName() {
suite.Require().Equal("testsubspace", suite.ss.Name())
}
func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(SubspaceTestSuite))
}