-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathkeeper.go
108 lines (82 loc) · 2.85 KB
/
keeper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package keeper
import (
"context"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
cmttypes "github.com/cometbft/cometbft/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"cosmossdk.io/collections"
"cosmossdk.io/core/event"
storetypes "cosmossdk.io/core/store"
"cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/consensus/exported"
"github.com/cosmos/cosmos-sdk/x/consensus/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
)
var StoreKey = "Consensus"
type Keeper struct {
storeService storetypes.KVStoreService
event event.Service
authority string
ParamsStore collections.Item[cmtproto.ConsensusParams]
}
var _ exported.ConsensusParamSetter = Keeper{}.ParamsStore
func NewKeeper(cdc codec.BinaryCodec, storeService storetypes.KVStoreService, authority string, em event.Service) Keeper {
sb := collections.NewSchemaBuilder(storeService)
return Keeper{
storeService: storeService,
authority: authority,
event: em,
ParamsStore: collections.NewItem(sb, collections.NewPrefix("Consensus"), "params", codec.CollValue[cmtproto.ConsensusParams](cdc)),
}
}
func (k *Keeper) GetAuthority() string {
return k.authority
}
// Querier
var _ types.QueryServer = Keeper{}
// Params queries params of consensus module
func (k Keeper) Params(ctx context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
params, err := k.ParamsStore.Get(ctx)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryParamsResponse{Params: ¶ms}, nil
}
// MsgServer
var _ types.MsgServer = Keeper{}
func (k Keeper) UpdateParams(ctx context.Context, msg *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
if k.GetAuthority() != msg.Authority {
return nil, errors.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.GetAuthority(), msg.Authority)
}
consensusParams, err := msg.ToProtoConsensusParams()
if err != nil {
return nil, err
}
paramsProto, err := k.ParamsStore.Get(ctx)
if err != nil {
return nil, err
}
params := cmttypes.ConsensusParamsFromProto(paramsProto)
nextParams := params.Update(&consensusParams)
if err := nextParams.ValidateBasic(); err != nil {
return nil, err
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
if err := params.ValidateUpdate(&consensusParams, sdkCtx.HeaderInfo().Height); err != nil {
return nil, err
}
if err := k.ParamsStore.Set(ctx, nextParams.ToProto()); err != nil {
return nil, err
}
if err := k.event.EventManager(ctx).EmitKV(
ctx,
"update_consensus_params",
event.Attribute{Key: "authority", Value: msg.Authority},
event.Attribute{Key: "parameters", Value: consensusParams.String()}); err != nil {
return nil, err
}
return &types.MsgUpdateParamsResponse{}, nil
}