注意:x/params 自 Cosmos SDK v0.53 起已弃用,并将在下一次发布中移除。

摘要

params 包提供了一个全局可用的参数存储。 主要有两种类型:Keeper 和 Subspace。Subspace 是 paramstore 的隔离命名空间,其中键会带有预先配置的空间名称前缀。Keeper 具有访问所有现有空间的权限。 Subspace 可供各个 keeper 使用,以便它们拥有一个其他 keeper 无法修改的私有参数存储。params 的 Keeper 可用于向 x/gov 路由器添加一条路由,从而在提案通过时修改任意参数。 以下内容说明如何在主模块和用户模块中使用 params 模块。

目录

Keeper

在应用初始化阶段,可以使用 Keeper.Subspace 为其他模块的 keeper 分配 subspaces,并将其存储在 Keeper.spaces 中。随后,这些模块可以通过 Keeper.GetSubspace 获取各自特定参数存储的引用。 示例:
type ExampleKeeper struct {
    paramSpace paramtypes.Subspace
}

func (k ExampleKeeper)

SetParams(ctx sdk.Context, params types.Params) {
    k.paramSpace.SetParamSet(ctx, &params)
}

Subspace

Subspace 是参数存储的一个带前缀的子空间。每个使用参数存储的模块都会使用一个 Subspace,以隔离访问权限。

Key

参数键是人类可读的字母数字字符串。键 "ExampleParameter" 对应的参数会存储在 []byte("SubspaceName" + "/" + "ExampleParameter") 下,其中 "SubspaceName" 是该子空间的名称。 子键是与主参数键配合使用的次级参数键。子键可用于分组,或在运行时动态生成参数键。

KeyTable

所有将要使用的参数键都应在编译期完成注册。KeyTable 本质上是一个 map[string]attribute,其中 string 是参数键。 目前,attribute 由一个 reflect.Type 组成,它用于指示参数类型,以检查给定的键和值是否兼容且已注册,同时还包含一个用于校验值的函数 ValueValidatorFn。 只有主键需要注册到 KeyTable 中。子键会继承主键的 attribute。

ParamSet

模块通常将参数定义为一个 proto message。生成的结构体可以实现 ParamSet 接口,以配合以下方法使用:
  • KeyTable.RegisterParamSet():注册结构体中的所有参数
  • Subspace.{Get, Set}ParamSet():从结构体中获取参数并设置回结构体
实现者应为指针类型,以便使用 GetParamSet()。
NOTE: x/params is deprecated as of Cosmos SDK v0.53 and will be removed in the next release.

Abstract

Package params provides a globally available parameter store. There are two main types, Keeper and Subspace. Subspace is an isolated namespace for a paramstore, where keys are prefixed by preconfigured spacename. Keeper has a permission to access all existing spaces. Subspace can be used by the individual keepers, which need a private parameter store that the other keepers cannot modify. The params Keeper can be used to add a route to x/gov router in order to modify any parameter in case a proposal passes. The following contents explains how to use params module for master and user modules.

Contents

Keeper

In the app initialization stage, subspaces can be allocated for other modules’ keeper using Keeper.Subspace and are stored in Keeper.spaces. Then, those modules can have a reference to their specific parameter store through Keeper.GetSubspace. Example:
type ExampleKeeper struct {
    paramSpace paramtypes.Subspace
}

func (k ExampleKeeper)

SetParams(ctx sdk.Context, params types.Params) {
    k.paramSpace.SetParamSet(ctx, &params)
}

Subspace

Subspace is a prefixed subspace of the parameter store. Each module which uses the parameter store will take a Subspace to isolate permission to access.

Key

Parameter keys are human readable alphanumeric strings. A parameter for the key "ExampleParameter" is stored under []byte("SubspaceName" + "/" + "ExampleParameter"), where "SubspaceName" is the name of the subspace. Subkeys are secondary parameter keys those are used along with a primary parameter key. Subkeys can be used for grouping or dynamic parameter key generation during runtime.

KeyTable

All of the parameter keys that will be used should be registered at the compile time. KeyTable is essentially a map[string]attribute, where the string is a parameter key. Currently, attribute consists of a reflect.Type, which indicates the parameter type to check that provided key and value are compatible and registered, as well as a function ValueValidatorFn to validate values. Only primary keys have to be registered on the KeyTable. Subkeys inherit the attribute of the primary key.

ParamSet

Modules often define parameters as a proto message. The generated struct can implement ParamSet interface to be used with the following methods:
  • KeyTable.RegisterParamSet(): registers all parameters in the struct
  • Subspace.{Get, Set}ParamSet(): Get to & Set from the struct
The implementor should be a pointer in order to use GetParamSet().