-
Notifications
You must be signed in to change notification settings - Fork 4
/
keeper.go
276 lines (236 loc) · 12.1 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package keeper
import (
"context"
"fmt"
"time"
"cosmossdk.io/collections"
corestoretypes "cosmossdk.io/core/store"
"cosmossdk.io/log"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/authz"
capabilitykeeper "github.com/cosmos/ibc-go/modules/capability/keeper"
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
host "github.com/cosmos/ibc-go/v8/modules/core/24-host"
owasm "github.com/odin-protocol/go-owasm/api"
"github.com/ODIN-PROTOCOL/odin-core/pkg/filecache"
"github.com/ODIN-PROTOCOL/odin-core/x/oracle/types"
)
type Keeper struct {
cdc codec.BinaryCodec
fileCache filecache.Cache
feeCollectorName string
owasmVM *owasm.Vm
AuthKeeper types.AccountKeeper
BankKeeper types.BankKeeper
stakingKeeper types.StakingKeeper
distrKeeper types.DistrKeeper
authzKeeper types.AuthzKeeper
channelKeeper types.ChannelKeeper
portKeeper types.PortKeeper
scopedKeeper capabilitykeeper.ScopedKeeper
// the address capable of executing a MsgUpdateParams message. Typically, this
// should be the x/gov module account.
authority string
// The (unexposed) keys used to access the stores from the Context.
storeService corestoretypes.KVStoreService
Schema collections.Schema
Params collections.Item[types.Params]
DataSources collections.Map[uint64, types.DataSource]
OracleScripts collections.Map[uint64, types.OracleScript]
Requests collections.Map[uint64, types.Request]
PendingResolveList collections.Item[types.PendingResolveList]
Reports collections.Map[collections.Pair[uint64, []byte], types.Report]
Results collections.Map[uint64, types.Result]
ValidatorStatuses collections.Map[[]byte, types.ValidatorStatus]
RequestID collections.Sequence
DataSourceID collections.Sequence
OracleScriptID collections.Sequence
RollingSeed collections.Item[[]byte]
RequestLastExpired collections.Item[uint64]
DataProviderAccumulatedRewards collections.Map[[]byte, types.DataProviderAccumulatedReward]
AccumulatedDataProvidersRewards collections.Item[types.DataProvidersAccumulatedRewards]
AccumulatedPaymentsForData collections.Item[types.AccumulatedPaymentsForData]
}
// NewKeeper creates a new oracle Keeper instance.
func NewKeeper(
cdc codec.BinaryCodec,
storeService corestoretypes.KVStoreService,
fileDir string,
feeCollectorName string,
authKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,
stakingKeeper types.StakingKeeper,
distrKeeper types.DistrKeeper,
authzKeeper types.AuthzKeeper,
channelKeeper types.ChannelKeeper,
portKeeper types.PortKeeper,
scopeKeeper capabilitykeeper.ScopedKeeper,
owasmVM *owasm.Vm,
authority string,
) Keeper {
sb := collections.NewSchemaBuilder(storeService)
k := Keeper{
cdc: cdc,
fileCache: filecache.New(fileDir),
feeCollectorName: feeCollectorName,
owasmVM: owasmVM,
AuthKeeper: authKeeper,
BankKeeper: bankKeeper,
stakingKeeper: stakingKeeper,
distrKeeper: distrKeeper,
authzKeeper: authzKeeper,
channelKeeper: channelKeeper,
portKeeper: portKeeper,
scopedKeeper: scopeKeeper,
authority: authority,
storeService: storeService,
Params: collections.NewItem(sb, types.ParamsKeyPrefix, "params", codec.CollValue[types.Params](cdc)),
DataSources: collections.NewMap(sb, types.DataSourceStoreKeyPrefix, "data_sources", collections.Uint64Key, codec.CollValue[types.DataSource](cdc)),
OracleScripts: collections.NewMap(sb, types.OracleScriptStoreKeyPrefix, "oracle_scripts", collections.Uint64Key, codec.CollValue[types.OracleScript](cdc)),
Requests: collections.NewMap(sb, types.RequestStoreKeyPrefix, "requests", collections.Uint64Key, codec.CollValue[types.Request](cdc)),
PendingResolveList: collections.NewItem(sb, types.PendingResolveListStoreKey, "pending_resolve_list", codec.CollValue[types.PendingResolveList](cdc)),
Reports: collections.NewMap(sb, types.ReportStoreKeyPrefix, "reports", collections.PairKeyCodec(collections.Uint64Key, collections.BytesKey), codec.CollValue[types.Report](cdc)), //collections.Map[collections.Pair[uint64, sdk.ValAddress], types.Report]{},
Results: collections.NewMap(sb, types.ResultStoreKeyPrefix, "results", collections.Uint64Key, codec.CollValue[types.Result](cdc)),
ValidatorStatuses: collections.NewMap(sb, types.ValidatorStatusKeyPrefix, "validator_statuses", collections.BytesKey, codec.CollValue[types.ValidatorStatus](cdc)),
RequestID: collections.NewSequence(sb, types.RequestCountStoreKey, "request_id"),
DataSourceID: collections.NewSequence(sb, types.DataSourceCountStoreKey, "data_source_id"),
OracleScriptID: collections.NewSequence(sb, types.OracleScriptCountStoreKey, "oracle_script_id"),
RollingSeed: collections.NewItem(sb, types.RollingSeedStoreKey, "rolling_seed", collections.BytesValue),
RequestLastExpired: collections.NewItem(sb, types.RequestLastExpiredStoreKey, "request_last_expired", collections.Uint64Value),
DataProviderAccumulatedRewards: collections.NewMap(sb, types.DataProviderRewardsKeyPrefix, "data_provider_accumulated_rewards", collections.BytesKey, codec.CollValue[types.DataProviderAccumulatedReward](cdc)),
AccumulatedDataProvidersRewards: collections.NewItem(sb, types.AccumulatedDataProvidersRewardsStoreKey, "accumulated_data_providers_rewards", codec.CollValue[types.DataProvidersAccumulatedRewards](cdc)),
AccumulatedPaymentsForData: collections.NewItem(sb, types.AccumulatedPaymentsForDataStoreKey, "accumulated_payments_for_data", codec.CollValue[types.AccumulatedPaymentsForData](cdc)),
}
schema, err := sb.Build()
if err != nil {
panic(err)
}
k.Schema = schema
return k
}
// GetAuthority returns the x/oracle module's authority.
func (k Keeper) GetAuthority() string {
return k.authority
}
// 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))
}
// SetRollingSeed sets the rolling seed value to be provided value.
func (k Keeper) SetRollingSeed(ctx context.Context, rollingSeed []byte) error {
return k.RollingSeed.Set(ctx, rollingSeed)
}
// GetRollingSeed returns the current rolling seed value.
func (k Keeper) GetRollingSeed(ctx context.Context) ([]byte, error) {
return k.RollingSeed.Get(ctx)
}
// SetRequestCount sets the number of request count to the given value. Useful for genesis state.
func (k Keeper) SetRequestCount(ctx context.Context, count uint64) error {
return k.RequestID.Set(ctx, count)
}
// GetRequestCount returns the current number of all requests ever exist.
func (k Keeper) GetRequestCount(ctx context.Context) (uint64, error) {
return k.RequestID.Peek(ctx)
}
// SetRequestLastExpired sets the ID of the last expired request.
func (k Keeper) SetRequestLastExpired(ctx context.Context, id types.RequestID) error {
return k.RequestLastExpired.Set(ctx, uint64(id))
}
// GetRequestLastExpired returns the ID of the last expired request.
func (k Keeper) GetRequestLastExpired(ctx context.Context) (types.RequestID, error) {
lastExpired, err := k.RequestLastExpired.Get(ctx)
return types.RequestID(lastExpired), err
}
// GetNextRequestID increments and returns the current number of requests.
func (k Keeper) GetNextRequestID(ctx context.Context) (types.RequestID, error) {
count, err := k.RequestID.Next(ctx)
return types.RequestID(count + 1), err
}
// SetDataSourceCount sets the number of data source count to the given value.
func (k Keeper) SetDataSourceCount(ctx context.Context, count uint64) error {
return k.DataSourceID.Set(ctx, count)
}
// GetDataSourceCount returns the current number of all data sources ever exist.
func (k Keeper) GetDataSourceCount(ctx context.Context) (uint64, error) {
return k.DataSourceID.Peek(ctx)
}
// GetNextDataSourceID increments and returns the current number of data sources.
func (k Keeper) GetNextDataSourceID(ctx context.Context) (types.DataSourceID, error) {
count, err := k.DataSourceID.Next(ctx)
return types.DataSourceID(count + 1), err
}
// SetOracleScriptCount sets the number of oracle script count to the given value.
func (k Keeper) SetOracleScriptCount(ctx context.Context, count uint64) error {
return k.OracleScriptID.Set(ctx, count)
}
// GetOracleScriptCount returns the current number of all oracle scripts ever exist.
func (k Keeper) GetOracleScriptCount(ctx context.Context) (uint64, error) {
return k.OracleScriptID.Peek(ctx)
}
// GetNextOracleScriptID increments and returns the current number of oracle scripts.
func (k Keeper) GetNextOracleScriptID(ctx context.Context) (types.OracleScriptID, error) {
count, err := k.OracleScriptID.Next(ctx)
return types.OracleScriptID(count + 1), err
}
// GetFile loads the file from the file storage. Panics if the file does not exist.
func (k Keeper) GetFile(name string) []byte {
return k.fileCache.MustGetFile(name)
}
// IsBound checks if the oracle module is already bound to the desired port
func (k Keeper) IsBound(ctx sdk.Context, portID string) bool {
_, ok := k.scopedKeeper.GetCapability(ctx, host.PortPath(portID))
return ok
}
// BindPort defines a wrapper function for the ort Keeper's function in
// order to expose it to module's InitGenesis function
func (k Keeper) BindPort(ctx sdk.Context, portID string) error {
capability := k.portKeeper.BindPort(ctx, portID)
return k.ClaimCapability(ctx, capability, host.PortPath(portID))
}
// GetPort returns the portID for the oracle module. Used in ExportGenesis
func (k Keeper) GetPort() string {
return types.PortID
}
// AuthenticateCapability wraps the scopedKeeper's AuthenticateCapability function
func (k Keeper) AuthenticateCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) bool {
return k.scopedKeeper.AuthenticateCapability(ctx, cap, name)
}
// ClaimCapability allows the oracle module that can claim a capability that IBC module
// passes to it
func (k Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) error {
return k.scopedKeeper.ClaimCapability(ctx, cap, name)
}
// IsReporter checks if the validator granted to the reporter
func (k Keeper) IsReporter(ctx sdk.Context, validator sdk.ValAddress, reporter sdk.AccAddress) bool {
capability, _ := k.authzKeeper.GetAuthorization(
ctx,
reporter,
sdk.AccAddress(validator),
sdk.MsgTypeURL(&types.MsgReportData{}),
)
return capability != nil
}
// GrantReporter grants the reporter to validator for testing
func (k Keeper) GrantReporter(ctx sdk.Context, validator sdk.ValAddress, reporter sdk.AccAddress) error {
expiration := ctx.BlockTime().Add(10 * time.Minute)
return k.authzKeeper.SaveGrant(ctx, reporter, sdk.AccAddress(validator),
authz.NewGenericAuthorization(sdk.MsgTypeURL(&types.MsgReportData{})), &expiration,
)
}
// RevokeReporter revokes grant from the reporter for testing
func (k Keeper) RevokeReporter(ctx context.Context, validator sdk.ValAddress, reporter sdk.AccAddress) error {
return k.authzKeeper.DeleteGrant(ctx, reporter, sdk.AccAddress(validator), sdk.MsgTypeURL(&types.MsgReportData{}))
}
func (k Keeper) SetAccumulatedDataProvidersRewards(ctx context.Context, reward types.DataProvidersAccumulatedRewards) error {
return k.AccumulatedDataProvidersRewards.Set(ctx, reward)
}
func (k Keeper) GetAccumulatedDataProvidersRewards(ctx context.Context) (reward types.DataProvidersAccumulatedRewards, err error) {
return k.AccumulatedDataProvidersRewards.Get(ctx)
}
func (k Keeper) SetAccumulatedPaymentsForData(ctx context.Context, payments types.AccumulatedPaymentsForData) error {
return k.AccumulatedPaymentsForData.Set(ctx, payments)
}
func (k Keeper) GetAccumulatedPaymentsForData(ctx context.Context) (payments types.AccumulatedPaymentsForData, err error) {
return k.AccumulatedPaymentsForData.Get(ctx)
}