rm key & doc.go in progress
This commit is contained in:
parent
78a89df393
commit
fd8713ef9c
|
@ -7,21 +7,16 @@ import (
|
||||||
"github.com/cosmos/cosmos-sdk/x/params"
|
"github.com/cosmos/cosmos-sdk/x/params"
|
||||||
)
|
)
|
||||||
|
|
||||||
// nolint
|
// Parameter store default namespace
|
||||||
const (
|
const (
|
||||||
DefaultParamSpace = "gov"
|
DefaultParamSpace = "gov"
|
||||||
)
|
)
|
||||||
|
|
||||||
// nolint - Paramstore key constructor
|
// Parameter store key
|
||||||
func ParamStoreKeyDepositProcedure() params.Key { return params.NewKey([]byte("depositprocedure")) }
|
const (
|
||||||
func ParamStoreKeyVotingProcedure() params.Key { return params.NewKey([]byte("votingprocedure")) }
|
ParamStoreKeyDepositProcedure = "depositprocedure"
|
||||||
func ParamStoreKeyTallyingProcedure() params.Key { return params.NewKey([]byte("tallyingprocedure")) }
|
ParamStoreKeyVotingProcedure = "votingprocedure"
|
||||||
|
ParamStoreKeyTallyingProcedure = "tallyingprocedure"
|
||||||
// Cached parameter store keys
|
|
||||||
var (
|
|
||||||
paramStoreKeyDepositProcedure = ParamStoreKeyDepositProcedure()
|
|
||||||
paramStoreKeyVotingProcedure = ParamStoreKeyVotingProcedure()
|
|
||||||
paramStoreKeyTallyingProcedure = ParamStoreKeyTallyingProcedure()
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Governance Keeper
|
// Governance Keeper
|
||||||
|
@ -224,7 +219,7 @@ func (keeper Keeper) activateVotingPeriod(ctx sdk.Context, proposal Proposal) {
|
||||||
// nolint: errcheck
|
// nolint: errcheck
|
||||||
func (keeper Keeper) GetDepositProcedure(ctx sdk.Context) DepositProcedure {
|
func (keeper Keeper) GetDepositProcedure(ctx sdk.Context) DepositProcedure {
|
||||||
var depositProcedure DepositProcedure
|
var depositProcedure DepositProcedure
|
||||||
keeper.ps.Get(ctx, paramStoreKeyDepositProcedure, &depositProcedure)
|
keeper.ps.Get(ctx, ParamStoreKeyDepositProcedure, &depositProcedure)
|
||||||
return depositProcedure
|
return depositProcedure
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -232,7 +227,7 @@ func (keeper Keeper) GetDepositProcedure(ctx sdk.Context) DepositProcedure {
|
||||||
// nolint: errcheck
|
// nolint: errcheck
|
||||||
func (keeper Keeper) GetVotingProcedure(ctx sdk.Context) VotingProcedure {
|
func (keeper Keeper) GetVotingProcedure(ctx sdk.Context) VotingProcedure {
|
||||||
var votingProcedure VotingProcedure
|
var votingProcedure VotingProcedure
|
||||||
keeper.ps.Get(ctx, paramStoreKeyVotingProcedure, &votingProcedure)
|
keeper.ps.Get(ctx, ParamStoreKeyVotingProcedure, &votingProcedure)
|
||||||
return votingProcedure
|
return votingProcedure
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -240,23 +235,23 @@ func (keeper Keeper) GetVotingProcedure(ctx sdk.Context) VotingProcedure {
|
||||||
// nolint: errcheck
|
// nolint: errcheck
|
||||||
func (keeper Keeper) GetTallyingProcedure(ctx sdk.Context) TallyingProcedure {
|
func (keeper Keeper) GetTallyingProcedure(ctx sdk.Context) TallyingProcedure {
|
||||||
var tallyingProcedure TallyingProcedure
|
var tallyingProcedure TallyingProcedure
|
||||||
keeper.ps.Get(ctx, paramStoreKeyTallyingProcedure, &tallyingProcedure)
|
keeper.ps.Get(ctx, ParamStoreKeyTallyingProcedure, &tallyingProcedure)
|
||||||
return tallyingProcedure
|
return tallyingProcedure
|
||||||
}
|
}
|
||||||
|
|
||||||
// nolint: errcheck
|
// nolint: errcheck
|
||||||
func (keeper Keeper) setDepositProcedure(ctx sdk.Context, depositProcedure DepositProcedure) {
|
func (keeper Keeper) setDepositProcedure(ctx sdk.Context, depositProcedure DepositProcedure) {
|
||||||
keeper.ps.Set(ctx, paramStoreKeyDepositProcedure, &depositProcedure)
|
keeper.ps.Set(ctx, ParamStoreKeyDepositProcedure, &depositProcedure)
|
||||||
}
|
}
|
||||||
|
|
||||||
// nolint: errcheck
|
// nolint: errcheck
|
||||||
func (keeper Keeper) setVotingProcedure(ctx sdk.Context, votingProcedure VotingProcedure) {
|
func (keeper Keeper) setVotingProcedure(ctx sdk.Context, votingProcedure VotingProcedure) {
|
||||||
keeper.ps.Set(ctx, paramStoreKeyVotingProcedure, &votingProcedure)
|
keeper.ps.Set(ctx, ParamStoreKeyVotingProcedure, &votingProcedure)
|
||||||
}
|
}
|
||||||
|
|
||||||
// nolint: errcheck
|
// nolint: errcheck
|
||||||
func (keeper Keeper) setTallyingProcedure(ctx sdk.Context, tallyingProcedure TallyingProcedure) {
|
func (keeper Keeper) setTallyingProcedure(ctx sdk.Context, tallyingProcedure TallyingProcedure) {
|
||||||
keeper.ps.Set(ctx, paramStoreKeyTallyingProcedure, &tallyingProcedure)
|
keeper.ps.Set(ctx, ParamStoreKeyTallyingProcedure, &tallyingProcedure)
|
||||||
}
|
}
|
||||||
|
|
||||||
// =====================================================
|
// =====================================================
|
||||||
|
|
|
@ -13,7 +13,7 @@ func QueryParams(cliCtx context.CLIContext, subStoreName string, ps params.Param
|
||||||
m := make(map[string][]byte)
|
m := make(map[string][]byte)
|
||||||
|
|
||||||
for _, p := range ps.KeyFieldPairs() {
|
for _, p := range ps.KeyFieldPairs() {
|
||||||
key := p.Key.String()
|
key := p.Key
|
||||||
bz, err := cliCtx.QueryStore([]byte(subStoreName+"/"+key), storeName)
|
bz, err := cliCtx.QueryStore([]byte(subStoreName+"/"+key), storeName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
package params
|
||||||
|
|
||||||
|
/*
|
||||||
|
Package params provides a globally available parameter store .
|
||||||
|
|
||||||
|
There are two main types, Keeper and Space. Space is an isolated namespace, prefixed by preconfigured spacename. Keeper has a permission to access all existing spaces and create new space.
|
||||||
|
|
||||||
|
Space can be used by the individual keepers, who needs a private parameter store that the other keeper are not able to modify. Keeper can be used by the Governance keeper, who need to modify any parameter in case of the proposal passes.
|
||||||
|
|
||||||
|
Basic Usage:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
|
@ -36,16 +36,16 @@ func createTestCodec() *codec.Codec {
|
||||||
|
|
||||||
func TestKeeper(t *testing.T) {
|
func TestKeeper(t *testing.T) {
|
||||||
kvs := []struct {
|
kvs := []struct {
|
||||||
key Key
|
key string
|
||||||
param int64
|
param int64
|
||||||
}{
|
}{
|
||||||
{NewKey([]byte("key1")), 10},
|
{"key1", 10},
|
||||||
{NewKey([]byte("key2")), 55},
|
{"key2", 55},
|
||||||
{NewKey([]byte("key3")), 182},
|
{"key3", 182},
|
||||||
{NewKey([]byte("key4")), 17582},
|
{"key4", 17582},
|
||||||
{NewKey([]byte("key5")), 2768554},
|
{"key5", 2768554},
|
||||||
{NewKey([]byte("space1"), []byte("key1")), 1157279},
|
{"space1/key1", 1157279},
|
||||||
{NewKey([]byte("space1"), []byte("key2")), 9058701},
|
{"space1/key2", 9058701},
|
||||||
}
|
}
|
||||||
|
|
||||||
skey := sdk.NewKVStoreKey("test")
|
skey := sdk.NewKVStoreKey("test")
|
||||||
|
@ -91,19 +91,19 @@ func TestGet(t *testing.T) {
|
||||||
space := keeper.Subspace("test")
|
space := keeper.Subspace("test")
|
||||||
|
|
||||||
kvs := []struct {
|
kvs := []struct {
|
||||||
key Key
|
key string
|
||||||
param interface{}
|
param interface{}
|
||||||
zero interface{}
|
zero interface{}
|
||||||
ptr interface{}
|
ptr interface{}
|
||||||
}{
|
}{
|
||||||
{NewKey([]byte("string")), "test", "", new(string)},
|
{"string", "test", "", new(string)},
|
||||||
{NewKey([]byte("bool")), true, false, new(bool)},
|
{"bool", true, false, new(bool)},
|
||||||
{NewKey([]byte("int16")), int16(1), int16(0), new(int16)},
|
{"int16", int16(1), int16(0), new(int16)},
|
||||||
{NewKey([]byte("int32")), int32(1), int32(0), new(int32)},
|
{"int32", int32(1), int32(0), new(int32)},
|
||||||
{NewKey([]byte("int64")), int64(1), int64(0), new(int64)},
|
{"int64", int64(1), int64(0), new(int64)},
|
||||||
{NewKey([]byte("uint16")), uint16(1), uint16(0), new(uint16)},
|
{"uint16", uint16(1), uint16(0), new(uint16)},
|
||||||
{NewKey([]byte("uint32")), uint32(1), uint32(0), new(uint32)},
|
{"uint32", uint32(1), uint32(0), new(uint32)},
|
||||||
{NewKey([]byte("uint64")), uint64(1), uint64(0), new(uint64)},
|
{"uint64", uint64(1), uint64(0), new(uint64)},
|
||||||
/*
|
/*
|
||||||
{NewKey("int"), sdk.NewInt(1), *new(sdk.Int), new(sdk.Int)},
|
{NewKey("int"), sdk.NewInt(1), *new(sdk.Int), new(sdk.Int)},
|
||||||
{NewKey("uint"), sdk.NewUint(1), *new(sdk.Uint), new(sdk.Uint)},
|
{NewKey("uint"), sdk.NewUint(1), *new(sdk.Uint), new(sdk.Uint)},
|
||||||
|
@ -116,9 +116,9 @@ func TestGet(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, kv := range kvs {
|
for _, kv := range kvs {
|
||||||
require.NotPanics(t, func() { space.GetIfExists(ctx, NewKey([]byte("invalid")), kv.ptr) })
|
require.NotPanics(t, func() { space.GetIfExists(ctx, "invalid", kv.ptr) })
|
||||||
require.Equal(t, kv.zero, reflect.ValueOf(kv.ptr).Elem().Interface())
|
require.Equal(t, kv.zero, reflect.ValueOf(kv.ptr).Elem().Interface())
|
||||||
require.Panics(t, func() { space.Get(ctx, NewKey([]byte("invalid")), kv.ptr) })
|
require.Panics(t, func() { space.Get(ctx, "invalid", kv.ptr) })
|
||||||
require.Equal(t, kv.zero, reflect.ValueOf(kv.ptr).Elem().Interface())
|
require.Equal(t, kv.zero, reflect.ValueOf(kv.ptr).Elem().Interface())
|
||||||
|
|
||||||
require.NotPanics(t, func() { space.GetIfExists(ctx, kv.key, kv.ptr) })
|
require.NotPanics(t, func() { space.GetIfExists(ctx, kv.key, kv.ptr) })
|
||||||
|
@ -126,7 +126,7 @@ func TestGet(t *testing.T) {
|
||||||
require.NotPanics(t, func() { space.Get(ctx, kv.key, kv.ptr) })
|
require.NotPanics(t, func() { space.Get(ctx, kv.key, kv.ptr) })
|
||||||
require.Equal(t, kv.param, reflect.ValueOf(kv.ptr).Elem().Interface())
|
require.Equal(t, kv.param, reflect.ValueOf(kv.ptr).Elem().Interface())
|
||||||
|
|
||||||
require.Panics(t, func() { space.Get(ctx, NewKey([]byte("invalid")), kv.ptr) })
|
require.Panics(t, func() { space.Get(ctx, "invalid", kv.ptr) })
|
||||||
require.Equal(t, kv.param, reflect.ValueOf(kv.ptr).Elem().Interface())
|
require.Equal(t, kv.param, reflect.ValueOf(kv.ptr).Elem().Interface())
|
||||||
|
|
||||||
require.Panics(t, func() { space.Get(ctx, kv.key, nil) })
|
require.Panics(t, func() { space.Get(ctx, kv.key, nil) })
|
||||||
|
|
|
@ -8,15 +8,9 @@ import (
|
||||||
// nolint - reexport
|
// nolint - reexport
|
||||||
type Space = space.Space
|
type Space = space.Space
|
||||||
type ReadOnlySpace = space.ReadOnlySpace
|
type ReadOnlySpace = space.ReadOnlySpace
|
||||||
type Key = space.Key
|
|
||||||
type KeyFieldPair = space.KeyFieldPair
|
|
||||||
type KeyFieldPairs = space.KeyFieldPairs
|
|
||||||
type ParamStruct = space.ParamStruct
|
type ParamStruct = space.ParamStruct
|
||||||
|
type KeyFieldPairs = space.KeyFieldPairs
|
||||||
|
|
||||||
// nolint - reexport
|
|
||||||
func NewKey(keys ...[]byte) Key {
|
|
||||||
return space.NewKey(keys...)
|
|
||||||
}
|
|
||||||
func UnmarshalParamsFromMap(m map[string][]byte, cdc *codec.Codec, ps space.ParamStruct) error {
|
func UnmarshalParamsFromMap(m map[string][]byte, cdc *codec.Codec, ps space.ParamStruct) error {
|
||||||
return space.UnmarshalParamsFromMap(m, cdc, ps)
|
return space.UnmarshalParamsFromMap(m, cdc, ps)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,73 +0,0 @@
|
||||||
package space
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/cosmos/cosmos-sdk/codec"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Wrapper for key string
|
|
||||||
type Key struct {
|
|
||||||
s []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
// Appending two keys with '/' as separator
|
|
||||||
// Checks alphanumericity
|
|
||||||
func (k Key) Append(keys ...[]byte) (res Key) {
|
|
||||||
res.s = make([]byte, len(k.s))
|
|
||||||
copy(res.s, k.s)
|
|
||||||
|
|
||||||
for _, key := range keys {
|
|
||||||
for _, b := range key {
|
|
||||||
if !(32 <= b && b <= 126) {
|
|
||||||
panic("parameter key expressions can only contain alphanumeric characters")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
res.s = append(append(res.s, byte('/')), key...)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewKey constructs a key from a list of strings
|
|
||||||
func NewKey(keys ...[]byte) (res Key) {
|
|
||||||
if len(keys) < 1 {
|
|
||||||
panic("length of parameter keys must not be zero")
|
|
||||||
}
|
|
||||||
res = Key{keys[0]}
|
|
||||||
|
|
||||||
return res.Append(keys[1:]...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// KeyBytes make KVStore key bytes from Key
|
|
||||||
func (k Key) Bytes() []byte {
|
|
||||||
return k.s
|
|
||||||
}
|
|
||||||
|
|
||||||
// Human readable string
|
|
||||||
func (k Key) String() string {
|
|
||||||
return string(k.s)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Used for associating paramstore key and field of param structs
|
|
||||||
type KeyFieldPair struct {
|
|
||||||
Key Key
|
|
||||||
Field interface{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Slice of KeyFieldPair
|
|
||||||
type KeyFieldPairs []KeyFieldPair
|
|
||||||
|
|
||||||
// Interface for structs containing parameters for a module
|
|
||||||
type ParamStruct interface {
|
|
||||||
KeyFieldPairs() KeyFieldPairs
|
|
||||||
}
|
|
||||||
|
|
||||||
// Takes a map from key string to byte slice and
|
|
||||||
// unmarshalles it to ParamStruct
|
|
||||||
func UnmarshalParamsFromMap(m map[string][]byte, cdc *codec.Codec, ps ParamStruct) error {
|
|
||||||
for _, p := range ps.KeyFieldPairs() {
|
|
||||||
err := cdc.UnmarshalJSON(m[p.Key.String()], p.Field)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
package space
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/cosmos/cosmos-sdk/codec"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Used for associating paramstore key and field of param structs
|
||||||
|
type KeyFieldPair struct {
|
||||||
|
Key string
|
||||||
|
Field interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Slice of KeyFieldPair
|
||||||
|
type KeyFieldPairs []KeyFieldPair
|
||||||
|
|
||||||
|
// Interface for structs containing parameters for a module
|
||||||
|
type ParamStruct interface {
|
||||||
|
KeyFieldPairs() KeyFieldPairs
|
||||||
|
}
|
||||||
|
|
||||||
|
// Takes a map from key string to byte slice and
|
||||||
|
// unmarshalles it to ParamStruct
|
||||||
|
func UnmarshalParamsFromMap(m map[string][]byte, cdc *codec.Codec, ps ParamStruct) error {
|
||||||
|
for _, p := range ps.KeyFieldPairs() {
|
||||||
|
err := cdc.UnmarshalJSON(m[p.Key], p.Field)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -35,9 +35,9 @@ func NewSpace(cdc *codec.Codec, key sdk.StoreKey, tkey sdk.StoreKey, space strin
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get parameter from store
|
// Get parameter from store
|
||||||
func (s Space) Get(ctx sdk.Context, key Key, ptr interface{}) {
|
func (s Space) Get(ctx sdk.Context, key string, ptr interface{}) {
|
||||||
store := ctx.KVStore(s.key).Prefix(s.space)
|
store := ctx.KVStore(s.key).Prefix(s.space)
|
||||||
bz := store.Get(key.Bytes())
|
bz := store.Get([]byte(key))
|
||||||
err := s.cdc.UnmarshalJSON(bz, ptr)
|
err := s.cdc.UnmarshalJSON(bz, ptr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -45,9 +45,9 @@ func (s Space) Get(ctx sdk.Context, key Key, ptr interface{}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetIfExists do not modify ptr if the stored parameter is nil
|
// GetIfExists do not modify ptr if the stored parameter is nil
|
||||||
func (s Space) GetIfExists(ctx sdk.Context, key Key, ptr interface{}) {
|
func (s Space) GetIfExists(ctx sdk.Context, key string, ptr interface{}) {
|
||||||
store := ctx.KVStore(s.key).Prefix(s.space)
|
store := ctx.KVStore(s.key).Prefix(s.space)
|
||||||
bz := store.Get(key.Bytes())
|
bz := store.Get([]byte(key))
|
||||||
if bz == nil {
|
if bz == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -58,28 +58,28 @@ func (s Space) GetIfExists(ctx sdk.Context, key Key, ptr interface{}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get raw bytes of parameter from store
|
// Get raw bytes of parameter from store
|
||||||
func (s Space) GetRaw(ctx sdk.Context, key Key) []byte {
|
func (s Space) GetRaw(ctx sdk.Context, key string) []byte {
|
||||||
store := ctx.KVStore(s.key).Prefix(s.space)
|
store := ctx.KVStore(s.key).Prefix(s.space)
|
||||||
res := store.Get(key.Bytes())
|
res := store.Get([]byte(key))
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the parameter is set in the store
|
// Check if the parameter is set in the store
|
||||||
func (s Space) Has(ctx sdk.Context, key Key) bool {
|
func (s Space) Has(ctx sdk.Context, key string) bool {
|
||||||
store := ctx.KVStore(s.key).Prefix(s.space)
|
store := ctx.KVStore(s.key).Prefix(s.space)
|
||||||
return store.Has(key.Bytes())
|
return store.Has([]byte(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the parameter is set in the block
|
// Returns true if the parameter is set in the block
|
||||||
func (s Space) Modified(ctx sdk.Context, key Key) bool {
|
func (s Space) Modified(ctx sdk.Context, key string) bool {
|
||||||
tstore := ctx.KVStore(s.tkey).Prefix(s.space)
|
tstore := ctx.KVStore(s.tkey).Prefix(s.space)
|
||||||
return tstore.Has(key.Bytes())
|
return tstore.Has([]byte(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set parameter, return error if stored parameter has different type from input
|
// Set parameter, return error if stored parameter has different type from input
|
||||||
func (s Space) Set(ctx sdk.Context, key Key, param interface{}) {
|
func (s Space) Set(ctx sdk.Context, key string, param interface{}) {
|
||||||
store := ctx.KVStore(s.key).Prefix(s.space)
|
store := ctx.KVStore(s.key).Prefix(s.space)
|
||||||
keybz := key.Bytes()
|
keybz := []byte(key)
|
||||||
|
|
||||||
bz := store.Get(keybz)
|
bz := store.Get(keybz)
|
||||||
if bz != nil {
|
if bz != nil {
|
||||||
|
@ -102,8 +102,8 @@ func (s Space) Set(ctx sdk.Context, key Key, param interface{}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set raw bytes of parameter
|
// Set raw bytes of parameter
|
||||||
func (s Space) SetRaw(ctx sdk.Context, key Key, param []byte) {
|
func (s Space) SetRaw(ctx sdk.Context, key string, param []byte) {
|
||||||
keybz := key.Bytes()
|
keybz := []byte(key)
|
||||||
|
|
||||||
store := ctx.KVStore(s.key).Prefix(s.space)
|
store := ctx.KVStore(s.key).Prefix(s.space)
|
||||||
store.Set(keybz, param)
|
store.Set(keybz, param)
|
||||||
|
@ -112,6 +112,13 @@ func (s Space) SetRaw(ctx sdk.Context, key Key, param []byte) {
|
||||||
tstore.Set(keybz, []byte{})
|
tstore.Set(keybz, []byte{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set from ParamStruct
|
||||||
|
func (s Space) SetFromParamStruct(ctx sdk.Context, ps ParamStruct) {
|
||||||
|
for _, pair := range ps.KeyFieldPairs() {
|
||||||
|
s.Set(ctx, pair.Key, pair.Field)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Returns a KVStore identical with the paramspace
|
// Returns a KVStore identical with the paramspace
|
||||||
func (s Space) KVStore(ctx sdk.Context) sdk.KVStore {
|
func (s Space) KVStore(ctx sdk.Context) sdk.KVStore {
|
||||||
return ctx.KVStore(s.key).Prefix(s.space)
|
return ctx.KVStore(s.key).Prefix(s.space)
|
||||||
|
@ -123,21 +130,21 @@ type ReadOnlySpace struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exposes Get
|
// Exposes Get
|
||||||
func (ros ReadOnlySpace) Get(ctx sdk.Context, key Key, ptr interface{}) {
|
func (ros ReadOnlySpace) Get(ctx sdk.Context, key string, ptr interface{}) {
|
||||||
ros.s.Get(ctx, key, ptr)
|
ros.s.Get(ctx, key, ptr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exposes GetRaw
|
// Exposes GetRaw
|
||||||
func (ros ReadOnlySpace) GetRaw(ctx sdk.Context, key Key) []byte {
|
func (ros ReadOnlySpace) GetRaw(ctx sdk.Context, key string) []byte {
|
||||||
return ros.s.GetRaw(ctx, key)
|
return ros.s.GetRaw(ctx, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exposes Has
|
// Exposes Has
|
||||||
func (ros ReadOnlySpace) Has(ctx sdk.Context, key Key) bool {
|
func (ros ReadOnlySpace) Has(ctx sdk.Context, key string) bool {
|
||||||
return ros.s.Has(ctx, key)
|
return ros.s.Has(ctx, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exposes Modified
|
// Exposes Modified
|
||||||
func (ros ReadOnlySpace) Modified(ctx sdk.Context, key Key) bool {
|
func (ros ReadOnlySpace) Modified(ctx sdk.Context, key string) bool {
|
||||||
return ros.s.Modified(ctx, key)
|
return ros.s.Modified(ctx, key)
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,15 +21,8 @@ func DefaultGenesisState() GenesisState {
|
||||||
// and the keeper's address to pubkey map
|
// and the keeper's address to pubkey map
|
||||||
func InitGenesis(ctx sdk.Context, keeper Keeper, data GenesisState, sdata types.GenesisState) {
|
func InitGenesis(ctx sdk.Context, keeper Keeper, data GenesisState, sdata types.GenesisState) {
|
||||||
for _, validator := range sdata.Validators {
|
for _, validator := range sdata.Validators {
|
||||||
keeper.addPubkey(ctx, validator.GetPubKey())
|
keeper.addPubkey(ctx, validator.KeyGetPub())
|
||||||
}
|
}
|
||||||
|
|
||||||
p := data.Params
|
keeper.paramstore.SetFromParamStruct(data.Params)
|
||||||
keeper.paramstore.Set(ctx, maxEvidenceAgeKey, p.MaxEvidenceAge)
|
|
||||||
keeper.paramstore.Set(ctx, signedBlocksWindowKey, p.SignedBlocksWindow)
|
|
||||||
keeper.paramstore.Set(ctx, minSignedPerWindowKey, p.MinSignedPerWindow)
|
|
||||||
keeper.paramstore.Set(ctx, doubleSignUnbondDurationKey, p.DoubleSignUnbondDuration)
|
|
||||||
keeper.paramstore.Set(ctx, downtimeUnbondDurationKey, p.DowntimeUnbondDuration)
|
|
||||||
keeper.paramstore.Set(ctx, slashFractionDoubleSignKey, p.SlashFractionDoubleSign)
|
|
||||||
keeper.paramstore.Set(ctx, slashFractionDowntimeKey, p.SlashFractionDowntime)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,8 +4,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
|
||||||
"github.com/cosmos/cosmos-sdk/x/params"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Default parameter namespace
|
// Default parameter namespace
|
||||||
|
@ -13,26 +11,15 @@ const (
|
||||||
DefaultParamSpace = "slashing"
|
DefaultParamSpace = "slashing"
|
||||||
)
|
)
|
||||||
|
|
||||||
// nolint - Key generators for parameter access
|
// Parameter store key
|
||||||
func MaxEvidenceAgeKey() params.Key { return params.NewKey([]byte("MaxEvidenceAge")) }
|
const (
|
||||||
func SignedBlocksWindowKey() params.Key { return params.NewKey([]byte("SignedBlocksWindow")) }
|
KeyMaxEvidenceAge = "MaxEvidenceAge"
|
||||||
func MinSignedPerWindowKey() params.Key { return params.NewKey([]byte("MinSignedPerWindow")) }
|
KeySignedBlocksWindow = "SignedBlocksWindow"
|
||||||
func DoubleSignUnbondDurationKey() params.Key {
|
KeyMinSignedPerWindow = "MinSignedPerWindow"
|
||||||
return params.NewKey([]byte("DoubleSignUnbondDuration"))
|
KeyDoubleSignUnbondDuration = "DoubleSignUnbondDuration"
|
||||||
}
|
KeyDowntimeUnbondDuration = "DowntimeUnbondDuration"
|
||||||
func DowntimeUnbondDurationKey() params.Key { return params.NewKey([]byte("DowntimeUnbondDuration")) }
|
KeySlashFractionDoubleSign = "SlashFractionDoubleSign"
|
||||||
func SlashFractionDoubleSignKey() params.Key { return params.NewKey([]byte("SlashFractionDoubleSign")) }
|
KeySlashFractionDowntime = "SlashFractionDowntime"
|
||||||
func SlashFractionDowntimeKey() params.Key { return params.NewKey([]byte("SlashFractionDowntime")) }
|
|
||||||
|
|
||||||
// Cached parameter keys
|
|
||||||
var (
|
|
||||||
maxEvidenceAgeKey = MaxEvidenceAgeKey()
|
|
||||||
signedBlocksWindowKey = SignedBlocksWindowKey()
|
|
||||||
minSignedPerWindowKey = MinSignedPerWindowKey()
|
|
||||||
doubleSignUnbondDurationKey = DoubleSignUnbondDurationKey()
|
|
||||||
downtimeUnbondDurationKey = DowntimeUnbondDurationKey()
|
|
||||||
slashFractionDoubleSignKey = SlashFractionDoubleSignKey()
|
|
||||||
slashFractionDowntimeKey = SlashFractionDowntimeKey()
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Params - used for initializing default parameter for slashing at genesis
|
// Params - used for initializing default parameter for slashing at genesis
|
||||||
|
@ -73,44 +60,44 @@ func DefaultParams() Params {
|
||||||
// MaxEvidenceAge - Max age for evidence - 21 days (3 weeks)
|
// MaxEvidenceAge - Max age for evidence - 21 days (3 weeks)
|
||||||
// MaxEvidenceAge = 60 * 60 * 24 * 7 * 3
|
// MaxEvidenceAge = 60 * 60 * 24 * 7 * 3
|
||||||
func (k Keeper) MaxEvidenceAge(ctx sdk.Context) (res time.Duration) {
|
func (k Keeper) MaxEvidenceAge(ctx sdk.Context) (res time.Duration) {
|
||||||
k.paramstore.Get(ctx, maxEvidenceAgeKey, &res)
|
k.paramstore.Get(ctx, KeyMaxEvidenceAge, &res)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// SignedBlocksWindow - sliding window for downtime slashing
|
// SignedBlocksWindow - sliding window for downtime slashing
|
||||||
func (k Keeper) SignedBlocksWindow(ctx sdk.Context) (res int64) {
|
func (k Keeper) SignedBlocksWindow(ctx sdk.Context) (res int64) {
|
||||||
k.paramstore.Get(ctx, signedBlocksWindowKey, &res)
|
k.paramstore.Get(ctx, KeySignedBlocksWindow, &res)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Downtime slashing thershold - default 50% of the SignedBlocksWindow
|
// Downtime slashing thershold - default 50% of the SignedBlocksWindow
|
||||||
func (k Keeper) MinSignedPerWindow(ctx sdk.Context) int64 {
|
func (k Keeper) MinSignedPerWindow(ctx sdk.Context) int64 {
|
||||||
var minSignedPerWindow sdk.Dec
|
var minSignedPerWindow sdk.Dec
|
||||||
k.paramstore.Get(ctx, minSignedPerWindowKey, &minSignedPerWindow)
|
k.paramstore.Get(ctx, KeyMinSignedPerWindow, &minSignedPerWindow)
|
||||||
signedBlocksWindow := k.SignedBlocksWindow(ctx)
|
signedBlocksWindow := k.SignedBlocksWindow(ctx)
|
||||||
return sdk.NewDec(signedBlocksWindow).Mul(minSignedPerWindow).RoundInt64()
|
return sdk.NewDec(signedBlocksWindow).Mul(minSignedPerWindow).RoundInt64()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Double-sign unbond duration
|
// Double-sign unbond duration
|
||||||
func (k Keeper) DoubleSignUnbondDuration(ctx sdk.Context) (res time.Duration) {
|
func (k Keeper) DoubleSignUnbondDuration(ctx sdk.Context) (res time.Duration) {
|
||||||
k.paramstore.Get(ctx, doubleSignUnbondDurationKey, &res)
|
k.paramstore.Get(ctx, KeyDoubleSignUnbondDuration, &res)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Downtime unbond duration
|
// Downtime unbond duration
|
||||||
func (k Keeper) DowntimeUnbondDuration(ctx sdk.Context) (res time.Duration) {
|
func (k Keeper) DowntimeUnbondDuration(ctx sdk.Context) (res time.Duration) {
|
||||||
k.paramstore.Get(ctx, downtimeUnbondDurationKey, &res)
|
k.paramstore.Get(ctx, KeyDowntimeUnbondDuration, &res)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// SlashFractionDoubleSign - currently default 5%
|
// SlashFractionDoubleSign - currently default 5%
|
||||||
func (k Keeper) SlashFractionDoubleSign(ctx sdk.Context) (res sdk.Dec) {
|
func (k Keeper) SlashFractionDoubleSign(ctx sdk.Context) (res sdk.Dec) {
|
||||||
k.paramstore.Get(ctx, slashFractionDoubleSignKey, &res)
|
k.paramstore.Get(ctx, KeySlashFractionDoubleSign, &res)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// SlashFractionDowntime - currently default 1%
|
// SlashFractionDowntime - currently default 1%
|
||||||
func (k Keeper) SlashFractionDowntime(ctx sdk.Context) (res sdk.Dec) {
|
func (k Keeper) SlashFractionDowntime(ctx sdk.Context) (res sdk.Dec) {
|
||||||
k.paramstore.Get(ctx, slashFractionDowntimeKey, &res)
|
k.paramstore.Get(ctx, KeySlashFractionDowntime, &res)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,56 +12,45 @@ const (
|
||||||
DefaultParamSpace = "stake"
|
DefaultParamSpace = "stake"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Cached parameter keys
|
|
||||||
var (
|
|
||||||
keyInflationRateChange = types.KeyInflationRateChange()
|
|
||||||
keyInflationMax = types.KeyInflationMax()
|
|
||||||
keyInflationMin = types.KeyInflationMin()
|
|
||||||
keyGoalBonded = types.KeyGoalBonded()
|
|
||||||
keyUnbondingTime = types.KeyUnbondingTime()
|
|
||||||
keyMaxValidators = types.KeyMaxValidators()
|
|
||||||
keyBondDenom = types.KeyBondDenom()
|
|
||||||
)
|
|
||||||
|
|
||||||
// InflationRateChange - Maximum annual change in inflation rate
|
// InflationRateChange - Maximum annual change in inflation rate
|
||||||
func (k Keeper) InflationRateChange(ctx sdk.Context) (res sdk.Dec) {
|
func (k Keeper) InflationRateChange(ctx sdk.Context) (res sdk.Dec) {
|
||||||
k.paramstore.Get(ctx, keyInflationRateChange, &res)
|
k.paramstore.Get(ctx, types.KeyInflationRateChange, &res)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// InflationMax - Maximum inflation rate
|
// InflationMax - Maximum inflation rate
|
||||||
func (k Keeper) InflationMax(ctx sdk.Context) (res sdk.Dec) {
|
func (k Keeper) InflationMax(ctx sdk.Context) (res sdk.Dec) {
|
||||||
k.paramstore.Get(ctx, keyInflationMax, &res)
|
k.paramstore.Get(ctx, types.KeyInflationMax, &res)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// InflationMin - Minimum inflation rate
|
// InflationMin - Minimum inflation rate
|
||||||
func (k Keeper) InflationMin(ctx sdk.Context) (res sdk.Dec) {
|
func (k Keeper) InflationMin(ctx sdk.Context) (res sdk.Dec) {
|
||||||
k.paramstore.Get(ctx, keyInflationMin, &res)
|
k.paramstore.Get(ctx, types.KeyInflationMin, &res)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// GoalBonded - Goal of percent bonded atoms
|
// GoalBonded - Goal of percent bonded atoms
|
||||||
func (k Keeper) GoalBonded(ctx sdk.Context) (res sdk.Dec) {
|
func (k Keeper) GoalBonded(ctx sdk.Context) (res sdk.Dec) {
|
||||||
k.paramstore.Get(ctx, keyGoalBonded, &res)
|
k.paramstore.Get(ctx, types.KeyGoalBonded, &res)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnbondingTime
|
// UnbondingTime
|
||||||
func (k Keeper) UnbondingTime(ctx sdk.Context) (res time.Duration) {
|
func (k Keeper) UnbondingTime(ctx sdk.Context) (res time.Duration) {
|
||||||
k.paramstore.Get(ctx, keyUnbondingTime, &res)
|
k.paramstore.Get(ctx, types.KeyUnbondingTime, &res)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// MaxValidators - Maximum number of validators
|
// MaxValidators - Maximum number of validators
|
||||||
func (k Keeper) MaxValidators(ctx sdk.Context) (res uint16) {
|
func (k Keeper) MaxValidators(ctx sdk.Context) (res uint16) {
|
||||||
k.paramstore.Get(ctx, keyMaxValidators, &res)
|
k.paramstore.Get(ctx, types.KeyMaxValidators, &res)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// BondDenom - Bondable coin denomination
|
// BondDenom - Bondable coin denomination
|
||||||
func (k Keeper) BondDenom(ctx sdk.Context) (res string) {
|
func (k Keeper) BondDenom(ctx sdk.Context) (res string) {
|
||||||
k.paramstore.Get(ctx, keyBondDenom, &res)
|
k.paramstore.Get(ctx, types.KeyBondDenom, &res)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,11 +79,11 @@ func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
|
||||||
|
|
||||||
// set the params without updating validator set
|
// set the params without updating validator set
|
||||||
func (k Keeper) SetNewParams(ctx sdk.Context, params types.Params) {
|
func (k Keeper) SetNewParams(ctx sdk.Context, params types.Params) {
|
||||||
k.paramstore.Set(ctx, keyInflationRateChange, params.InflationRateChange)
|
k.paramstore.Set(ctx, types.KeyInflationRateChange, params.InflationRateChange)
|
||||||
k.paramstore.Set(ctx, keyInflationMax, params.InflationMax)
|
k.paramstore.Set(ctx, types.KeyInflationMax, params.InflationMax)
|
||||||
k.paramstore.Set(ctx, keyInflationMin, params.InflationMin)
|
k.paramstore.Set(ctx, types.KeyInflationMin, params.InflationMin)
|
||||||
k.paramstore.Set(ctx, keyGoalBonded, params.GoalBonded)
|
k.paramstore.Set(ctx, types.KeyGoalBonded, params.GoalBonded)
|
||||||
k.paramstore.Set(ctx, keyUnbondingTime, params.UnbondingTime)
|
k.paramstore.Set(ctx, types.KeyUnbondingTime, params.UnbondingTime)
|
||||||
k.paramstore.Set(ctx, keyMaxValidators, params.MaxValidators)
|
k.paramstore.Set(ctx, types.KeyMaxValidators, params.MaxValidators)
|
||||||
k.paramstore.Set(ctx, keyBondDenom, params.BondDenom)
|
k.paramstore.Set(ctx, types.KeyBondDenom, params.BondDenom)
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,14 +14,16 @@ import (
|
||||||
// unbonding time.
|
// unbonding time.
|
||||||
const defaultUnbondingTime time.Duration = 60 * 60 * 24 * 3 * time.Second
|
const defaultUnbondingTime time.Duration = 60 * 60 * 24 * 3 * time.Second
|
||||||
|
|
||||||
// nolint - Key generators for parameter access
|
// nolint - Keys for parameter access
|
||||||
func KeyInflationRateChange() params.Key { return params.NewKey([]byte("InflationRateChange")) }
|
const (
|
||||||
func KeyInflationMax() params.Key { return params.NewKey([]byte("InflationMax")) }
|
KeyInflationRateChange = "InflationRateChange"
|
||||||
func KeyInflationMin() params.Key { return params.NewKey([]byte("InflationMin")) }
|
KeyInflationMax = "InflationMax"
|
||||||
func KeyGoalBonded() params.Key { return params.NewKey([]byte("GoalBonded")) }
|
KeyInflationMin = "InflationMin"
|
||||||
func KeyUnbondingTime() params.Key { return params.NewKey([]byte("UnbondingTime")) }
|
KeyGoalBonded = "GoalBonded"
|
||||||
func KeyMaxValidators() params.Key { return params.NewKey([]byte("MaxValidators")) }
|
KeyUnbondingTime = "UnbondingTime"
|
||||||
func KeyBondDenom() params.Key { return params.NewKey([]byte("BondDenom")) }
|
KeyMaxValidators = "MaxValidators"
|
||||||
|
KeyBondDenom = "BondDenom"
|
||||||
|
)
|
||||||
|
|
||||||
// Params defines the high level settings for staking
|
// Params defines the high level settings for staking
|
||||||
type Params struct {
|
type Params struct {
|
||||||
|
@ -39,13 +41,13 @@ type Params struct {
|
||||||
// Implements params.ParamStruct
|
// Implements params.ParamStruct
|
||||||
func (p *Params) KeyFieldPairs() params.KeyFieldPairs {
|
func (p *Params) KeyFieldPairs() params.KeyFieldPairs {
|
||||||
return params.KeyFieldPairs{
|
return params.KeyFieldPairs{
|
||||||
{KeyInflationRateChange(), &p.InflationRateChange},
|
{KeyInflationRateChange, &p.InflationRateChange},
|
||||||
{KeyInflationMax(), &p.InflationMax},
|
{KeyInflationMax, &p.InflationMax},
|
||||||
{KeyInflationMin(), &p.InflationMin},
|
{KeyInflationMin, &p.InflationMin},
|
||||||
{KeyGoalBonded(), &p.GoalBonded},
|
{KeyGoalBonded, &p.GoalBonded},
|
||||||
{KeyUnbondingTime(), &p.UnbondingTime},
|
{KeyUnbondingTime, &p.UnbondingTime},
|
||||||
{KeyMaxValidators(), &p.MaxValidators},
|
{KeyMaxValidators, &p.MaxValidators},
|
||||||
{KeyBondDenom(), &p.BondDenom},
|
{KeyBondDenom, &p.BondDenom},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue