Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proto: migrate params to hybrid codec #5619

Merged
merged 34 commits into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
dec703b
Migrate staking types to proto
alexanderbez Feb 1, 2020
31311cb
Use int32 for validator status
alexanderbez Feb 3, 2020
0d2938b
Update staking types
alexanderbez Feb 3, 2020
60dc367
Add benchmarks for bech32 pubkey funcs
alexanderbez Feb 3, 2020
f0f6f50
Update BondStatus type alias to int32
alexanderbez Feb 3, 2020
c268a9e
Remove PubKey proto type
alexanderbez Feb 3, 2020
95edc79
Fix unit test
alexanderbez Feb 3, 2020
46be4b4
Update staking keeper
alexanderbez Feb 3, 2020
7503396
Fix staking keeper tests
alexanderbez Feb 3, 2020
97ac0b5
Update query client
alexanderbez Feb 3, 2020
8e84801
Update staking genesis and app_test
alexanderbez Feb 3, 2020
c35939a
Update staking handler
alexanderbez Feb 4, 2020
febaae8
Use Int64Value instead of IntProto
alexanderbez Feb 4, 2020
5b83d9b
Updates tests and APIs
alexanderbez Feb 4, 2020
0a904f1
Fix iteration
alexanderbez Feb 4, 2020
2450409
Linting
alexanderbez Feb 4, 2020
97148e2
Fix linting
alexanderbez Feb 4, 2020
da9d51f
Merge branch 'master' into bez/5444-staking-proto-enc
alexanderbez Feb 4, 2020
11a0d8b
Add changelog entry
alexanderbez Feb 4, 2020
db45cfc
Update changelog entry
alexanderbez Feb 4, 2020
c5a7baa
Merge branch 'master' into bez/5444-staking-proto-enc
alexanderbez Feb 4, 2020
3e1b445
Merge branch 'master' into bez/5444-staking-proto-enc
alexanderbez Feb 5, 2020
d31577c
Start boilerplate for app-level codec
alexanderbez Feb 5, 2020
beade6d
init of params
tac0turtle Feb 6, 2020
1b78d65
change to app codec for params, still have one test failing
tac0turtle Feb 6, 2020
86d5991
bump buf to 0.7
tac0turtle Feb 6, 2020
b9b1034
Merge branch 'master' into proto/params
tac0turtle Feb 7, 2020
81b85ff
Merge branch 'master' into proto/params
tac0turtle Feb 11, 2020
937a77b
Merge branch 'master' into proto/params
alexanderbez Feb 12, 2020
47cf6a3
Update x/params proto
alexanderbez Feb 12, 2020
d079f66
Fix params test harness
alexanderbez Feb 12, 2020
3a7cfdf
Call s.checkType on Get
alexanderbez Feb 12, 2020
e5a2983
Merge branch 'master' into proto/params
alexanderbez Feb 12, 2020
15481e7
Add changelog entry
alexanderbez Feb 12, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ balances or a single balance by denom when the `denom` query parameter is presen

### API Breaking Changes

* (x/params) [\#5619](https://github.com/cosmos/cosmos-sdk/pull/5619) The `x/params` keeper now accepts a `codec.Marshaller` instead of
a reference to an amino codec. Amino is still used for JSON serialization.
* (types) [\#5579](https://github.com/cosmos/cosmos-sdk/pull/5579) The `keepRecent` field has been removed from the `PruningOptions` type.
The `PruningOptions` type now only includes fields `KeepEvery` and `SnapshotEvery`, where `KeepEvery`
determines which committed heights are flushed to disk and `SnapshotEvery` determines which of these
Expand Down
2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func NewSimApp(
}

// init params keeper and subspaces
app.ParamsKeeper = params.NewKeeper(app.cdc, keys[params.StoreKey], tkeys[params.TStoreKey])
app.ParamsKeeper = params.NewKeeper(appCodec.Params, keys[params.StoreKey], tkeys[params.TStoreKey])
app.subspaces[auth.ModuleName] = app.ParamsKeeper.Subspace(auth.DefaultParamspace)
app.subspaces[bank.ModuleName] = app.ParamsKeeper.Subspace(bank.DefaultParamspace)
app.subspaces[staking.ModuleName] = app.ParamsKeeper.Subspace(staking.DefaultParamspace)
Expand Down
3 changes: 3 additions & 0 deletions simapp/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
distr "github.com/cosmos/cosmos-sdk/x/distribution"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/cosmos/cosmos-sdk/x/staking"
)

Expand All @@ -13,6 +14,7 @@ import (
type AppCodec struct {
amino *codec.Codec

Params *params.Codec
Staking *staking.Codec
Distribution *distr.Codec
}
Expand All @@ -22,6 +24,7 @@ func NewAppCodec() *AppCodec {

return &AppCodec{
amino: amino,
Params: params.NewCodec(amino),
Staking: staking.NewCodec(amino),
Distribution: distr.NewCodec(amino),
}
Expand Down
2 changes: 1 addition & 1 deletion x/distribution/keeper/test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func CreateTestInputAdvanced(
blacklistedAddrs[distrAcc.GetAddress().String()] = true

cdc := MakeTestCodec()
pk := params.NewKeeper(cdc, keyParams, tkeyParams)
pk := params.NewKeeper(params.ModuleCdc, keyParams, tkeyParams)

ctx := sdk.NewContext(ms, abci.Header{ChainID: "foochainid"}, isCheckTx, log.NewNopLogger())
accountKeeper := auth.NewAccountKeeper(cdc, keyAcc, pk.Subspace(auth.DefaultParamspace), auth.ProtoBaseAccount)
Expand Down
2 changes: 1 addition & 1 deletion x/gov/keeper/test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func createTestInput(
blacklistedAddrs[notBondedPool.GetAddress().String()] = true
blacklistedAddrs[bondPool.GetAddress().String()] = true

pk := params.NewKeeper(cdc, keyParams, tkeyParams)
pk := params.NewKeeper(params.ModuleCdc, keyParams, tkeyParams)
accountKeeper := auth.NewAccountKeeper(cdc, keyAcc, pk.Subspace(auth.DefaultParamspace), auth.ProtoBaseAccount)
bankKeeper := bank.NewBaseKeeper(cdc, keyBank, accountKeeper, pk.Subspace(bank.DefaultParamspace), blacklistedAddrs)
supplyKeeper := supply.NewKeeper(cdc, keySupply, accountKeeper, bankKeeper, maccPerms)
Expand Down
2 changes: 1 addition & 1 deletion x/mock/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func NewApp() *App {
TotalCoinsSupply: sdk.NewCoins(),
}

app.ParamsKeeper = params.NewKeeper(app.Cdc, app.KeyParams, app.TKeyParams)
app.ParamsKeeper = params.NewKeeper(params.ModuleCdc, app.KeyParams, app.TKeyParams)
app.AccountKeeper = auth.NewAccountKeeper(
app.Cdc,
app.KeyAccount,
Expand Down
2 changes: 2 additions & 0 deletions x/params/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ var (
NewParameterChangeProposal = types.NewParameterChangeProposal
NewParamChange = types.NewParamChange
ValidateChanges = types.ValidateChanges
NewCodec = types.NewCodec

// variable aliases
ModuleCdc = types.ModuleCdc
)

type (
Codec = types.Codec
ParamSetPair = subspace.ParamSetPair
ParamSetPairs = subspace.ParamSetPairs
ParamSet = subspace.ParamSet
Expand Down
6 changes: 3 additions & 3 deletions x/params/commmon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ type s struct {
I int
}

func createTestCodec() *codec.Codec {
func createTestCodec() codec.Marshaler {
cdc := codec.New()
sdk.RegisterCodec(cdc)
cdc.RegisterConcrete(s{}, "test/s", nil)
cdc.RegisterConcrete(invalid{}, "test/invalid", nil)
return cdc
return NewCodec(cdc)
}

func defaultContext(key sdk.StoreKey, tkey sdk.StoreKey) sdk.Context {
Expand All @@ -38,7 +38,7 @@ func defaultContext(key sdk.StoreKey, tkey sdk.StoreKey) sdk.Context {
return ctx
}

func testComponents() (*codec.Codec, sdk.Context, sdk.StoreKey, sdk.StoreKey, Keeper) {
func testComponents() (codec.Marshaler, sdk.Context, sdk.StoreKey, sdk.StoreKey, Keeper) {
cdc := createTestCodec()
mkey := sdk.NewKVStoreKey("test")
tkey := sdk.NewTransientStoreKey("transient_test")
Expand Down
4 changes: 2 additions & 2 deletions x/params/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (

// Keeper of the global paramstore
type Keeper struct {
cdc *codec.Codec
cdc codec.Marshaler
key sdk.StoreKey
tkey sdk.StoreKey
spaces map[string]*Subspace
}

// NewKeeper constructs a params keeper
func NewKeeper(cdc *codec.Codec, key, tkey sdk.StoreKey) Keeper {
func NewKeeper(cdc codec.Marshaler, key, tkey sdk.StoreKey) Keeper {
return Keeper{
cdc: cdc,
key: key,
Expand Down
2 changes: 1 addition & 1 deletion x/params/proposal_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func newTestInput(t *testing.T) testInput {
err := cms.LoadLatestVersion()
require.Nil(t, err)

keeper := params.NewKeeper(cdc, keyParams, tKeyParams)
keeper := params.NewKeeper(types.ModuleCdc, keyParams, tKeyParams)
ctx := sdk.NewContext(cms, abci.Header{}, false, log.NewNopLogger())

return testInput{ctx, cdc, keeper}
Expand Down
6 changes: 4 additions & 2 deletions x/params/subspace/subspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ const (
// Transient store persists for a block, so we use it for
// recording whether the parameter has been changed or not
type Subspace struct {
cdc *codec.Codec
cdc codec.Marshaler
key sdk.StoreKey // []byte -> []byte, stores parameter
tkey sdk.StoreKey // []byte -> bool, stores parameter change
name []byte
table KeyTable
}

// NewSubspace constructs a store with namestore
func NewSubspace(cdc *codec.Codec, key sdk.StoreKey, tkey sdk.StoreKey, name string) Subspace {
func NewSubspace(cdc codec.Marshaler, key sdk.StoreKey, tkey sdk.StoreKey, name string) Subspace {
return Subspace{
cdc: cdc,
key: key,
Expand Down Expand Up @@ -99,6 +99,8 @@ func (s Subspace) Validate(ctx sdk.Context, key []byte, value interface{}) error
// Get queries for a parameter by key from the Subspace's KVStore and sets the
// value to the provided pointer. If the value does not exist, it will panic.
func (s Subspace) Get(ctx sdk.Context, key []byte, ptr interface{}) {
s.checkType(key, ptr)

store := s.kvStore(ctx)
bz := store.Get(key)

Expand Down
7 changes: 4 additions & 3 deletions x/params/subspace/subspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@ import (
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/params/subspace"
"github.com/cosmos/cosmos-sdk/x/params/types"
)

type SubspaceTestSuite struct {
suite.Suite

cdc *codec.Codec
cdc codec.Marshaler
ctx sdk.Context
ss subspace.Subspace
}

func (suite *SubspaceTestSuite) SetupTest() {
cdc := codec.New()
cdc := types.ModuleCdc
db := dbm.NewMemDB()

ms := store.NewCommitMultiStore(db)
Expand All @@ -46,7 +47,7 @@ func (suite *SubspaceTestSuite) TestKeyTable() {
suite.ss.WithKeyTable(paramKeyTable())
})
suite.Require().NotPanics(func() {
ss := subspace.NewSubspace(codec.New(), key, tkey, "testsubspace2")
ss := subspace.NewSubspace(types.ModuleCdc, key, tkey, "testsubspace2")
ss = ss.WithKeyTable(paramKeyTable())
})
}
Expand Down
21 changes: 17 additions & 4 deletions x/params/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,26 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
)

type Codec struct {
codec.Marshaler

// Keep reference to the amino codec to allow backwards compatibility along
// with type, and interface registration.
amino *codec.Codec
}

func NewCodec(amino *codec.Codec) *Codec {
return &Codec{Marshaler: codec.NewHybridCodec(amino), amino: amino}
}

// module codec
var ModuleCdc *codec.Codec
var ModuleCdc *Codec

func init() {
ModuleCdc = codec.New()
RegisterCodec(ModuleCdc)
ModuleCdc.Seal()
ModuleCdc = NewCodec(codec.New())

RegisterCodec(ModuleCdc.amino)
ModuleCdc.amino.Seal()
}

// RegisterCodec registers all necessary param module types with a given codec.
Expand Down
24 changes: 4 additions & 20 deletions x/params/types/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"strings"

"gopkg.in/yaml.v2"

govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
)

Expand All @@ -20,14 +22,6 @@ func init() {
govtypes.RegisterProposalTypeCodec(ParameterChangeProposal{}, "cosmos-sdk/ParameterChangeProposal")
}

// ParameterChangeProposal defines a proposal which contains multiple parameter
// changes.
type ParameterChangeProposal struct {
Title string `json:"title" yaml:"title"`
Description string `json:"description" yaml:"description"`
Changes []ParamChange `json:"changes" yaml:"changes"`
}

func NewParameterChangeProposal(title, description string, changes []ParamChange) ParameterChangeProposal {
return ParameterChangeProposal{title, description, changes}
}
Expand Down Expand Up @@ -75,24 +69,14 @@ func (pcp ParameterChangeProposal) String() string {
return b.String()
}

// ParamChange defines a parameter change.
type ParamChange struct {
Subspace string `json:"subspace" yaml:"subspace"`
Key string `json:"key" yaml:"key"`
Value string `json:"value" yaml:"value"`
}

func NewParamChange(subspace, key, value string) ParamChange {
return ParamChange{subspace, key, value}
}

// String implements the Stringer interface.
func (pc ParamChange) String() string {
return fmt.Sprintf(`Param Change:
Subspace: %s
Key: %s
Value: %X
`, pc.Subspace, pc.Key, pc.Value)
out, _ := yaml.Marshal(pc)
return string(out)
}

// ValidateChanges performs basic validation checks over a set of ParamChange. It
Expand Down
Loading