-
Notifications
You must be signed in to change notification settings - Fork 0
/
keeper.go
156 lines (132 loc) · 5.21 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package keeper
import (
"fmt"
tmbytes "github.com/brc20-collab/brczero/libs/tendermint/libs/bytes"
"github.com/brc20-collab/brczero/libs/tendermint/libs/log"
"github.com/brc20-collab/brczero/libs/cosmos-sdk/codec"
"github.com/brc20-collab/brczero/libs/cosmos-sdk/store/prefix"
sdk "github.com/brc20-collab/brczero/libs/cosmos-sdk/types"
sdkerrors "github.com/brc20-collab/brczero/libs/cosmos-sdk/types/errors"
"github.com/brc20-collab/brczero/x/evidence/exported"
"github.com/brc20-collab/brczero/x/evidence/internal/types"
"github.com/brc20-collab/brczero/x/params"
)
// Keeper defines the evidence module's keeper. The keeper is responsible for
// managing persistence, state transitions and query handling for the evidence
// module.
type Keeper struct {
cdc *codec.Codec
storeKey sdk.StoreKey
paramSpace params.Subspace
router types.Router
stakingKeeper types.StakingKeeper
slashingKeeper types.SlashingKeeper
}
func NewKeeper(
cdc *codec.Codec, storeKey sdk.StoreKey, paramSpace params.Subspace,
stakingKeeper types.StakingKeeper, slashingKeeper types.SlashingKeeper,
) *Keeper {
// set KeyTable if it has not already been set
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
}
return &Keeper{
cdc: cdc,
storeKey: storeKey,
paramSpace: paramSpace,
stakingKeeper: stakingKeeper,
slashingKeeper: slashingKeeper,
}
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
// SetRouter sets the Evidence Handler router for the x/evidence module. Note,
// we allow the ability to set the router after the Keeper is constructed as a
// given Handler may need access the Keeper before being constructed. The router
// may only be set once and will be sealed if it's not already sealed.
func (k *Keeper) SetRouter(rtr types.Router) {
// It is vital to seal the Evidence Handler router as to not allow further
// handlers to be registered after the keeper is created since this
// could create invalid or non-deterministic behavior.
if !rtr.Sealed() {
rtr.Seal()
}
if k.router != nil {
panic(fmt.Sprintf("attempting to reset router on x/%s", types.ModuleName))
}
k.router = rtr
}
// GetEvidenceHandler returns a registered Handler for a given Evidence type. If
// no handler exists, an error is returned.
func (k Keeper) GetEvidenceHandler(evidenceRoute string) (types.Handler, error) {
if !k.router.HasRoute(evidenceRoute) {
return nil, sdkerrors.Wrap(types.ErrNoEvidenceHandlerExists, evidenceRoute)
}
return k.router.GetRoute(evidenceRoute), nil
}
// SubmitEvidence attempts to match evidence against the keepers router and execute
// the corresponding registered Evidence Handler. An error is returned if no
// registered Handler exists or if the Handler fails. Otherwise, the evidence is
// persisted.
func (k Keeper) SubmitEvidence(ctx sdk.Context, evidence exported.Evidence) error {
if _, ok := k.GetEvidence(ctx, evidence.Hash()); ok {
return sdkerrors.Wrap(types.ErrEvidenceExists, evidence.Hash().String())
}
if !k.router.HasRoute(evidence.Route()) {
return sdkerrors.Wrap(types.ErrNoEvidenceHandlerExists, evidence.Route())
}
handler := k.router.GetRoute(evidence.Route())
if err := handler(ctx, evidence); err != nil {
return sdkerrors.Wrap(types.ErrInvalidEvidence, err.Error())
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeSubmitEvidence,
sdk.NewAttribute(types.AttributeKeyEvidenceHash, evidence.Hash().String()),
),
)
k.SetEvidence(ctx, evidence)
return nil
}
// SetEvidence sets Evidence by hash in the module's KVStore.
func (k Keeper) SetEvidence(ctx sdk.Context, evidence exported.Evidence) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixEvidence)
bz := k.cdc.MustMarshalBinaryLengthPrefixed(evidence)
store.Set(evidence.Hash(), bz)
}
// GetEvidence retrieves Evidence by hash if it exists. If no Evidence exists for
// the given hash, (nil, false) is returned.
func (k Keeper) GetEvidence(ctx sdk.Context, hash tmbytes.HexBytes) (evidence exported.Evidence, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixEvidence)
bz := store.Get(hash)
if len(bz) == 0 {
return nil, false
}
k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &evidence)
return evidence, true
}
// IterateEvidence provides an interator over all stored Evidence objects. For
// each Evidence object, cb will be called. If the cb returns true, the iterator
// will close and stop.
func (k Keeper) IterateEvidence(ctx sdk.Context, cb func(exported.Evidence) bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixEvidence)
iterator := sdk.KVStorePrefixIterator(store, nil)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var evidence exported.Evidence
k.cdc.MustUnmarshalBinaryLengthPrefixed(iterator.Value(), &evidence)
if cb(evidence) {
break
}
}
}
// GetAllEvidence returns all stored Evidence objects.
func (k Keeper) GetAllEvidence(ctx sdk.Context) (evidence []exported.Evidence) {
k.IterateEvidence(ctx, func(e exported.Evidence) bool {
evidence = append(evidence, e)
return false
})
return evidence
}