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

R4R: Paramstore Subkey #2605

Merged
merged 51 commits into from
Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
c1940a2
add subkey methods, add circuit ante/genesis
mossid Oct 15, 2018
8a8c641
move to x/circuit, add tests
mossid Oct 16, 2018
11c6d78
add circuit breaker for name, simplify test
mossid Oct 16, 2018
186a5c7
apply to cmd
mossid Oct 17, 2018
d241edf
rename TypeTable -> KeyTable, KeyValuePairs -> ParamSetPairs
mossid Oct 18, 2018
90fb578
fix ante
mossid Oct 22, 2018
787a9c8
fix lint
mossid Oct 23, 2018
a45c67f
rm circuit
mossid Oct 26, 2018
f8394dd
rm circuit ante from gaia
mossid Oct 26, 2018
0e43f4e
Update x/params/subspace/subspace.go
alexanderbez Oct 26, 2018
e0c8b34
Update x/params/subspace/subspace.go
alexanderbez Oct 26, 2018
885ab0b
Update x/params/subspace/subspace.go
alexanderbez Oct 26, 2018
c74b5f9
Update x/params/subspace/subspace.go
alexanderbez Oct 26, 2018
1fc6113
Update x/params/subspace/subspace.go
alexanderbez Oct 26, 2018
aa6f4b3
Update x/params/subspace/subspace.go
alexanderbez Oct 26, 2018
4e226d2
Update x/params/subspace/subspace.go
alexanderbez Oct 26, 2018
f75ba8b
apply requests
mossid Oct 26, 2018
be4ce67
add subkey methods, add circuit ante/genesis
mossid Oct 15, 2018
268f15c
move to x/circuit, add tests
mossid Oct 16, 2018
2d08fe1
add circuit breaker for name, simplify test
mossid Oct 16, 2018
21594f7
apply to cmd
mossid Oct 17, 2018
0d7be1e
rename TypeTable -> KeyTable, KeyValuePairs -> ParamSetPairs
mossid Oct 18, 2018
bdd82bb
fix ante
mossid Oct 22, 2018
ae72633
fix lint
mossid Oct 23, 2018
b0aa784
rm circuit
mossid Oct 26, 2018
ee34467
rm circuit ante from gaia
mossid Oct 26, 2018
0f54f29
Update x/params/subspace/subspace.go
alexanderbez Oct 26, 2018
4abca2d
Update x/params/subspace/subspace.go
alexanderbez Oct 26, 2018
14efb41
Update x/params/subspace/subspace.go
alexanderbez Oct 26, 2018
7406dd5
Update x/params/subspace/subspace.go
alexanderbez Oct 26, 2018
fe45b6a
Update x/params/subspace/subspace.go
alexanderbez Oct 26, 2018
34a1a52
Update x/params/subspace/subspace.go
alexanderbez Oct 26, 2018
d04978c
Update x/params/subspace/subspace.go
alexanderbez Oct 26, 2018
021a0ea
apply requests
mossid Oct 26, 2018
4e2df1e
remove sync
mossid Oct 26, 2018
8c673bb
Merge branch 'joon/paramstore-subkey' of github.com:cosmos/cosmos-sdk…
mossid Oct 26, 2018
37c165a
add doc in progress
mossid Dec 13, 2018
4bdee5e
Merge branch 'develop' into joon/paramstore-subkey
mossid Dec 20, 2018
b13a031
fix lint
mossid Dec 20, 2018
078aead
Merge branch 'develop' into joon/paramstore-subkey
mossid Jan 25, 2019
531db59
Update x/params/doc.go
alexanderbez Jan 27, 2019
b613dbb
address comments
mossid Jan 27, 2019
7305a69
Merge branch 'joon/paramstore-subkey' of github.com:cosmos/cosmos-sdk…
mossid Jan 27, 2019
3c8f641
Merge branch 'develop' into joon/paramstore-subkey
mossid Jan 29, 2019
1723f0c
fix error
mossid Jan 29, 2019
d3235a0
fix docs
mossid Jan 31, 2019
2e9fa5a
Update docs/spec/params/subspace.md
alessio Feb 4, 2019
9177b05
Update docs/spec/params/subspace.md
alessio Feb 4, 2019
6ded81d
Update x/auth/params.go
alessio Feb 4, 2019
b9ae05d
Merge branch 'develop' into joon/paramstore-subkey
mossid Feb 4, 2019
3ee96fa
Merge branch 'develop' into joon/paramstore-subkey
mossid Feb 5, 2019
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
6 changes: 3 additions & 3 deletions x/distribution/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, paramSpace params.Subspace, c
keeper := Keeper{
storeKey: key,
cdc: cdc,
paramSpace: paramSpace.WithTypeTable(ParamTypeTable()),
paramSpace: paramSpace.WithKeyTable(ParamKeyTable()),
bankKeeper: ck,
stakeKeeper: sk,
feeCollectionKeeper: fck,
Expand Down Expand Up @@ -109,8 +109,8 @@ func (k Keeper) GetWithdrawContext(ctx sdk.Context,
// PARAM STORE

// Type declaration for parameters
func ParamTypeTable() params.TypeTable {
return params.NewTypeTable(
func ParamKeyTable() params.KeyTable {
return params.NewKeyTable(
ParamStoreKeyCommunityTax, sdk.Dec{},
ParamStoreKeyBaseProposerReward, sdk.Dec{},
ParamStoreKeyBonusProposerReward, sdk.Dec{},
Expand Down
6 changes: 3 additions & 3 deletions x/gov/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ var (
)

// Type declaration for parameters
func ParamTypeTable() params.TypeTable {
return params.NewTypeTable(
func ParamKeyTable() params.KeyTable {
return params.NewKeyTable(
ParamStoreKeyDepositProcedure, DepositProcedure{},
ParamStoreKeyVotingProcedure, VotingProcedure{},
ParamStoreKeyTallyingProcedure, TallyingProcedure{},
Expand Down Expand Up @@ -64,7 +64,7 @@ func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, paramsKeeper params.Keeper, p
return Keeper{
storeKey: key,
paramsKeeper: paramsKeeper,
paramSpace: paramSpace.WithTypeTable(ParamTypeTable()),
paramSpace: paramSpace.WithKeyTable(ParamKeyTable()),
ck: ck,
ds: ds,
vs: ds.GetValidatorSet(),
Expand Down
6 changes: 3 additions & 3 deletions x/mint/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewKeeper(cdc *codec.Codec, key sdk.StoreKey,
keeper := Keeper{
storeKey: key,
cdc: cdc,
paramSpace: paramSpace.WithTypeTable(ParamTypeTable()),
paramSpace: paramSpace.WithKeyTable(ParamKeyTable()),
sk: sk,
fck: fck,
}
Expand All @@ -39,8 +39,8 @@ var (
)

// ParamTable for stake module
func ParamTypeTable() params.TypeTable {
return params.NewTypeTable(
func ParamKeyTable() params.KeyTable {
return params.NewKeyTable(
ParamStoreKeyParams, Params{},
)
}
Expand Down
8 changes: 4 additions & 4 deletions x/params/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestKeeper(t *testing.T) {
{"key7", 9058701},
}

table := NewTypeTable(
table := NewKeyTable(
[]byte("key1"), int64(0),
[]byte("key2"), int64(0),
[]byte("key3"), int64(0),
Expand All @@ -70,7 +70,7 @@ func TestKeeper(t *testing.T) {
tkey := sdk.NewTransientStoreKey("transient_test")
ctx := defaultContext(skey, tkey)
keeper := NewKeeper(cdc, skey, tkey)
space := keeper.Subspace("test").WithTypeTable(table)
space := keeper.Subspace("test").WithKeyTable(table)
store := ctx.KVStore(skey).Prefix([]byte("test/"))

// Set params
Expand Down Expand Up @@ -162,7 +162,7 @@ func TestSubspace(t *testing.T) {
{"struct", s{1}, s{0}, new(s)},
}

table := NewTypeTable(
table := NewKeyTable(
[]byte("string"), string(""),
[]byte("bool"), bool(false),
[]byte("int16"), int16(0),
Expand All @@ -178,7 +178,7 @@ func TestSubspace(t *testing.T) {
)

store := ctx.KVStore(key).Prefix([]byte("test/"))
space := keeper.Subspace("test").WithTypeTable(table)
space := keeper.Subspace("test").WithKeyTable(table)

// Test space.Set, space.Modified
for i, kv := range kvs {
Expand Down
17 changes: 12 additions & 5 deletions x/params/subspace.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package params

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/cosmos-sdk/x/params/subspace"
)

Expand All @@ -9,11 +13,14 @@ type (
Subspace = subspace.Subspace
ReadOnlySubspace = subspace.ReadOnlySubspace
ParamSet = subspace.ParamSet
KeyValuePairs = subspace.KeyValuePairs
TypeTable = subspace.TypeTable
ParamSetPairs = subspace.ParamSetPairs
KeyTable = subspace.KeyTable
)

// re-export functions from subspace
func NewTypeTable(keytypes ...interface{}) TypeTable {
return subspace.NewTypeTable(keytypes...)
// nolint - re-export functions from subspace
func NewKeyTable(keytypes ...interface{}) KeyTable {
return subspace.NewKeyTable(keytypes...)
}
func DefaultTestComponents(t *testing.T) (sdk.Context, Subspace, func() sdk.CommitID) {
return subspace.DefaultTestComponents(t)
}
6 changes: 3 additions & 3 deletions x/params/subspace/pair.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package subspace

// Used for associating paramsubspace key and field of param structs
type KeyValuePair struct {
type ParamSetPair struct {
Key []byte
Value interface{}
}

// Slice of KeyFieldPair
type KeyValuePairs []KeyValuePair
type ParamSetPairs []ParamSetPair

// Interface for structs containing parameters for a module
type ParamSet interface {
KeyValuePairs() KeyValuePairs
ParamSetPairs() ParamSetPairs
}
77 changes: 60 additions & 17 deletions x/params/subspace/subspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Subspace struct {

name []byte

table TypeTable
table KeyTable
}

// NewSubspace constructs a store with namestore
Expand All @@ -27,21 +27,21 @@ func NewSubspace(cdc *codec.Codec, key sdk.StoreKey, tkey sdk.StoreKey, name str
key: key,
tkey: tkey,
name: []byte(name),
table: TypeTable{
m: make(map[string]reflect.Type),
table: KeyTable{
m: make(map[string]attribute),
},
}

return
}

// WithTypeTable initializes TypeTable and returns modified Subspace
func (s Subspace) WithTypeTable(table TypeTable) Subspace {
// WithKeyTable initializes KeyTable and returns modified Subspace
func (s Subspace) WithKeyTable(table KeyTable) Subspace {
if table.m == nil {
panic("SetTypeTable() called with nil TypeTable")
panic("SetKeyTable() called with nil KeyTable")
}
if len(s.table.m) != 0 {
panic("SetTypeTable() called on already initialized Subspace")
panic("SetKeyTable() called on already initialized Subspace")
}

for k, v := range table.m {
Expand All @@ -64,11 +64,19 @@ func (s Subspace) kvStore(ctx sdk.Context) sdk.KVStore {
return ctx.KVStore(s.key).Prefix(append(s.name, '/'))
}

// Returns a KVStore identical with ctx.TransientStore(s.tkey).Prefix()
// Returns a transient store for modification
func (s Subspace) transientStore(ctx sdk.Context) sdk.KVStore {
// append here is safe, appends within a function won't cause
// weird side effects when its singlethreaded
return ctx.TransientStore(s.tkey).Prefix(append(s.name, '/'))
return ctx.TransientStore(s.tkey).Prefix(append(s.name, "/m/"...))
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
}

func concatKeys(key, subkey []byte) (res []byte) {
res = make([]byte, len(key)+1+len(subkey))
copy(res, key)
res[len(key)] = '/'
copy(res[len(key)+1:], subkey)
return
}

// Get parameter from store
Expand All @@ -94,6 +102,17 @@ func (s Subspace) GetIfExists(ctx sdk.Context, key []byte, ptr interface{}) {
}
}

// GetWithSubkey returns a parameter with a given key and a subkey.
func (s Subspace) GetWithSubkey(ctx sdk.Context, key, subkey []byte, ptr interface{}) {
s.Get(ctx, concatKeys(key, subkey), ptr)
}

// GetWithSubkeyIfExists returns a parameter with a given key and a subkey but does not
// modify ptr if the stored parameter is nil.
func (s Subspace) GetWithSubkeyIfExists(ctx sdk.Context, key, subkey []byte, ptr interface{}) {
s.GetIfExists(ctx, concatKeys(key, subkey), ptr)
}

// Get raw bytes of parameter from store
func (s Subspace) GetRaw(ctx sdk.Context, key []byte) []byte {
store := s.kvStore(ctx)
Expand All @@ -112,16 +131,13 @@ func (s Subspace) Modified(ctx sdk.Context, key []byte) bool {
return tstore.Has(key)
}

// Set parameter, return error if stored parameter has different type from input
// Also set to the transient store to record change
func (s Subspace) Set(ctx sdk.Context, key []byte, param interface{}) {
store := s.kvStore(ctx)

ty, ok := s.table.m[string(key)]
func (s Subspace) checkType(store sdk.KVStore, key []byte, param interface{}) {
attr, ok := s.table.m[string(key)]
if !ok {
panic("Parameter not registered")
}

ty := attr.ty
pty := reflect.TypeOf(param)
if pty.Kind() == reflect.Ptr {
pty = pty.Elem()
Expand All @@ -130,6 +146,14 @@ func (s Subspace) Set(ctx sdk.Context, key []byte, param interface{}) {
if pty != ty {
panic("Type mismatch with registered table")
}
}

// Set stores the parameter. It returns error if stored parameter has different type from input.
// It also set to the transient store to record change.
func (s Subspace) Set(ctx sdk.Context, key []byte, param interface{}) {
store := s.kvStore(ctx)

s.checkType(store, key, param)

bz, err := s.cdc.MarshalJSON(param)
if err != nil {
Expand All @@ -142,16 +166,35 @@ func (s Subspace) Set(ctx sdk.Context, key []byte, param interface{}) {

}

// SetWithSubkey set a parameter with a key and subkey
// Checks parameter type only over the key
func (s Subspace) SetWithSubkey(ctx sdk.Context, key []byte, subkey []byte, param interface{}) {
store := s.kvStore(ctx)

s.checkType(store, key, param)

newkey := concatKeys(key, subkey)

bz, err := s.cdc.MarshalJSON(param)
if err != nil {
panic(err)
}
store.Set(newkey, bz)

tstore := s.transientStore(ctx)
tstore.Set(newkey, []byte{})
}

// Get to ParamSet
func (s Subspace) GetParamSet(ctx sdk.Context, ps ParamSet) {
for _, pair := range ps.KeyValuePairs() {
for _, pair := range ps.ParamSetPairs() {
s.Get(ctx, pair.Key, pair.Value)
}
}

// Set from ParamSet
func (s Subspace) SetParamSet(ctx sdk.Context, ps ParamSet) {
for _, pair := range ps.KeyValuePairs() {
for _, pair := range ps.ParamSetPairs() {
// pair.Field is a pointer to the field, so indirecting the ptr.
// go-amino automatically handles it but just for sure,
// since SetStruct is meant to be used in InitGenesis
Expand Down
30 changes: 18 additions & 12 deletions x/params/subspace/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@ import (
"reflect"
)

// TypeTable subspaces appropriate type for each parameter key
type TypeTable struct {
m map[string]reflect.Type
type attribute struct {
ty reflect.Type
}

// KeyTable subspaces appropriate type for each parameter key
type KeyTable struct {
m map[string]attribute
}

// Constructs new table
func NewTypeTable(keytypes ...interface{}) (res TypeTable) {
func NewKeyTable(keytypes ...interface{}) (res KeyTable) {
if len(keytypes)%2 != 0 {
panic("odd number arguments in NewTypeTypeTable")
panic("odd number arguments in NewTypeKeyTable")
}

res = TypeTable{
m: make(map[string]reflect.Type),
res = KeyTable{
m: make(map[string]attribute),
}

for i := 0; i < len(keytypes); i += 2 {
Expand All @@ -38,7 +42,7 @@ func isAlphaNumeric(key []byte) bool {
}

// Register single key-type pair
func (t TypeTable) RegisterType(key []byte, ty interface{}) TypeTable {
func (t KeyTable) RegisterType(key []byte, ty interface{}) KeyTable {
if len(key) == 0 {
panic("cannot register empty key")
}
Expand All @@ -57,20 +61,22 @@ func (t TypeTable) RegisterType(key []byte, ty interface{}) TypeTable {
rty = rty.Elem()
}

t.m[keystr] = rty
t.m[keystr] = attribute{
ty: rty,
}

return t
}

// Register multiple pairs from ParamSet
func (t TypeTable) RegisterParamSet(ps ParamSet) TypeTable {
for _, kvp := range ps.KeyValuePairs() {
func (t KeyTable) RegisterParamSet(ps ParamSet) KeyTable {
for _, kvp := range ps.ParamSetPairs() {
t = t.RegisterType(kvp.Key, kvp.Value)
}
return t
}

func (t TypeTable) maxKeyLength() (res int) {
func (t KeyTable) maxKeyLength() (res int) {
for k := range t.m {
l := len(k)
if l > res {
Expand Down
8 changes: 4 additions & 4 deletions x/params/subspace/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ type testparams struct {
b bool
}

func (tp *testparams) KeyValuePairs() KeyValuePairs {
return KeyValuePairs{
func (tp *testparams) ParamSetPairs() ParamSetPairs {
return ParamSetPairs{
{[]byte("i"), &tp.i},
{[]byte("b"), &tp.b},
}
}

func TestTypeTable(t *testing.T) {
table := NewTypeTable()
func TestKeyTable(t *testing.T) {
table := NewKeyTable()

require.Panics(t, func() { table.RegisterType([]byte(""), nil) })
require.Panics(t, func() { table.RegisterType([]byte("!@#$%"), nil) })
Expand Down
Loading