/
tokenomics.go
320 lines (276 loc) · 10.5 KB
/
tokenomics.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package keeper
import (
"context"
"testing"
"cosmossdk.io/log"
math "cosmossdk.io/math"
"cosmossdk.io/store"
"cosmossdk.io/store/metrics"
storetypes "cosmossdk.io/store/types"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
dbm "github.com/cosmos/cosmos-db"
"github.com/cosmos/cosmos-sdk/codec"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/testutil/integration"
sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"github.com/pokt-network/poktroll/app"
"github.com/pokt-network/poktroll/testutil/sample"
"github.com/pokt-network/poktroll/testutil/tokenomics/mocks"
appkeeper "github.com/pokt-network/poktroll/x/application/keeper"
apptypes "github.com/pokt-network/poktroll/x/application/types"
gatewaykeeper "github.com/pokt-network/poktroll/x/gateway/keeper"
gatewaytypes "github.com/pokt-network/poktroll/x/gateway/types"
proofkeeper "github.com/pokt-network/poktroll/x/proof/keeper"
prooftypes "github.com/pokt-network/poktroll/x/proof/types"
sessionkeeper "github.com/pokt-network/poktroll/x/session/keeper"
sessiontypes "github.com/pokt-network/poktroll/x/session/types"
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
supplierkeeper "github.com/pokt-network/poktroll/x/supplier/keeper"
suppliertypes "github.com/pokt-network/poktroll/x/supplier/types"
tokenomicskeeper "github.com/pokt-network/poktroll/x/tokenomics/keeper"
tokenomicstypes "github.com/pokt-network/poktroll/x/tokenomics/types"
)
// TokenomicsModuleKeepers is an aggregation of the tokenomics keeper, all its dependency
// keepers, and the codec that they share. Each keeper is embedded such that the
// TokenomicsModuleKeepers implements all the interfaces of the keepers.
// To call a method which is common to multiple keepers (e.g. `#SetParams()`),
// the field corresponding to the desired keeper on which to call the method
// MUST be specified (e.g. `keepers.AccountKeeper#SetParams()`).
type TokenomicsModuleKeepers struct {
*tokenomicskeeper.Keeper
tokenomicstypes.AccountKeeper
tokenomicstypes.BankKeeper
tokenomicstypes.ApplicationKeeper
tokenomicstypes.ProofKeeper
Codec *codec.ProtoCodec
}
// TokenomicsKeepersOpt is a function which receives and potentially modifies the context
// and tokenomics keepers during construction of the aggregation.
type TokenomicsKeepersOpt func(context.Context, *TokenomicsModuleKeepers) context.Context
func TokenomicsKeeper(t testing.TB) (
tokenomicsKeeper tokenomicskeeper.Keeper,
ctx context.Context,
appAddr string,
supplierAddr string,
) {
t.Helper()
storeKey := storetypes.NewKVStoreKey(tokenomicstypes.StoreKey)
// Initialize the in-memory database.
db := dbm.NewMemDB()
stateStore := store.NewCommitMultiStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
require.NoError(t, stateStore.LoadLatestVersion())
// Initialize the codec and other necessary components.
registry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(registry)
// The on-chain governance address.
authority := authtypes.NewModuleAddress(govtypes.ModuleName)
// Prepare the test application.
application := apptypes.Application{
Address: sample.AccAddress(),
Stake: &sdk.Coin{Denom: "upokt", Amount: math.NewInt(100000)},
}
// Prepare the test supplier.
supplier := sharedtypes.Supplier{
Address: sample.AccAddress(),
Stake: &sdk.Coin{Denom: "upokt", Amount: math.NewInt(100000)},
}
ctrl := gomock.NewController(t)
// Mock the application keeper.
mockApplicationKeeper := mocks.NewMockApplicationKeeper(ctrl)
// Get test application if the address matches.
mockApplicationKeeper.EXPECT().
GetApplication(gomock.Any(), gomock.Eq(application.Address)).
Return(application, true).
AnyTimes()
// Get zero-value application if the address does not match.
mockApplicationKeeper.EXPECT().
GetApplication(gomock.Any(), gomock.Not(application.Address)).
Return(apptypes.Application{}, false).
AnyTimes()
// Mock SetApplication.
mockApplicationKeeper.EXPECT().
SetApplication(gomock.Any(), gomock.Any()).
AnyTimes()
// Mock the bank keeper.
mockBankKeeper := mocks.NewMockBankKeeper(ctrl)
mockBankKeeper.EXPECT().
MintCoins(gomock.Any(), gomock.Any(), gomock.Any()).
AnyTimes()
mockBankKeeper.EXPECT().
BurnCoins(gomock.Any(), gomock.Any(), gomock.Any()).
AnyTimes()
mockBankKeeper.EXPECT().
SendCoinsFromModuleToAccount(gomock.Any(), suppliertypes.ModuleName, gomock.Any(), gomock.Any()).
AnyTimes()
// Mock the account keeper
mockAccountKeeper := mocks.NewMockAccountKeeper(ctrl)
mockAccountKeeper.EXPECT().GetAccount(gomock.Any(), gomock.Any()).AnyTimes()
// Mock the proof keeper
mockProofKeeper := mocks.NewMockProofKeeper(ctrl)
mockProofKeeper.EXPECT().GetAllClaims(gomock.Any()).AnyTimes()
k := tokenomicskeeper.NewKeeper(
cdc,
runtime.NewKVStoreService(storeKey),
log.NewNopLogger(),
authority.String(),
mockBankKeeper,
mockAccountKeeper,
mockApplicationKeeper,
mockProofKeeper,
)
sdkCtx := sdk.NewContext(stateStore, cmtproto.Header{}, false, log.NewNopLogger())
// Initialize params
require.NoError(t, k.SetParams(sdkCtx, tokenomicstypes.DefaultParams()))
return k, sdkCtx, application.Address, supplier.Address
}
// NewTokenomicsModuleKeepers is a helper function to create a tokenomics keeper
// and a context. It uses real dependencies for all upstream keepers.
func NewTokenomicsModuleKeepers(
t testing.TB,
opts ...TokenomicsKeepersOpt,
) (_ TokenomicsModuleKeepers, ctx context.Context) {
t.Helper()
// Collect store keys for all keepers which be constructed & interact with the state store.
keys := storetypes.NewKVStoreKeys(
tokenomicstypes.StoreKey,
banktypes.StoreKey,
gatewaytypes.StoreKey,
authtypes.StoreKey,
sessiontypes.StoreKey,
apptypes.StoreKey,
suppliertypes.StoreKey,
prooftypes.StoreKey,
)
// Construct a multistore & mount store keys for each keeper that will interact with the state store.
stateStore := integration.CreateMultiStore(keys, log.NewNopLogger())
// Prepare the context
logger := log.NewTestLogger(t)
ctx = sdk.NewContext(stateStore, cmtproto.Header{}, false, logger)
// ctx.SetAccount
// Prepare the account keeper.
registry := codectypes.NewInterfaceRegistry()
authtypes.RegisterInterfaces(registry)
cryptocodec.RegisterInterfaces(registry)
// Prepare the chain's authority
cdc := codec.NewProtoCodec(registry)
authority := authtypes.NewModuleAddress(govtypes.ModuleName)
t.Logf("authority.String(): %s", authority.String())
// Construct a real account keeper so that public keys can be queried.
addrCodec := addresscodec.NewBech32Codec(app.AccountAddressPrefix)
accountKeeper := authkeeper.NewAccountKeeper(
cdc,
runtime.NewKVStoreService(keys[authtypes.StoreKey]),
authtypes.ProtoBaseAccount,
// These module accounts are necessary in order to settle balances
// during claim expiration.
map[string][]string{
minttypes.ModuleName: {authtypes.Minter},
suppliertypes.ModuleName: {authtypes.Minter, authtypes.Burner},
apptypes.ModuleName: {authtypes.Minter, authtypes.Burner},
},
addrCodec,
app.AccountAddressPrefix,
authority.String(),
)
// Construct a real bank keeper so that the balances can be updated & verified
bankKeeper := bankkeeper.NewBaseKeeper(
cdc,
runtime.NewKVStoreService(keys[banktypes.StoreKey]),
accountKeeper,
make(map[string]bool),
authority.String(),
logger,
)
require.NoError(t, bankKeeper.SetParams(ctx, banktypes.DefaultParams()))
// Provide some initial funds to the suppliers & applications module accounts.
bankKeeper.MintCoins(ctx, suppliertypes.ModuleName, sdk.NewCoins(sdk.NewCoin("upokt", math.NewInt(1000000000000))))
bankKeeper.MintCoins(ctx, apptypes.ModuleName, sdk.NewCoins(sdk.NewCoin("upokt", math.NewInt(1000000000000))))
// Construct gateway keeper with a mocked bank keeper.
gatewayKeeper := gatewaykeeper.NewKeeper(
cdc,
runtime.NewKVStoreService(keys[gatewaytypes.StoreKey]),
logger,
authority.String(),
bankKeeper,
)
require.NoError(t, gatewayKeeper.SetParams(ctx, gatewaytypes.DefaultParams()))
// Construct an application keeper to add apps to sessions.
appKeeper := appkeeper.NewKeeper(
cdc,
runtime.NewKVStoreService(keys[apptypes.StoreKey]),
logger,
authority.String(),
bankKeeper,
accountKeeper,
gatewayKeeper,
)
require.NoError(t, appKeeper.SetParams(ctx, apptypes.DefaultParams()))
// Construct a real supplier keeper to add suppliers to sessions.
supplierKeeper := supplierkeeper.NewKeeper(
cdc,
runtime.NewKVStoreService(keys[suppliertypes.StoreKey]),
log.NewNopLogger(),
authority.String(),
bankKeeper,
)
require.NoError(t, supplierKeeper.SetParams(ctx, suppliertypes.DefaultParams()))
// Construct a real session keeper so that sessions can be queried.
sessionKeeper := sessionkeeper.NewKeeper(
cdc,
runtime.NewKVStoreService(keys[sessiontypes.StoreKey]),
logger,
authority.String(),
accountKeeper,
bankKeeper,
appKeeper,
supplierKeeper,
)
require.NoError(t, sessionKeeper.SetParams(ctx, sessiontypes.DefaultParams()))
// Construct a real proof keeper so that claims & proofs can be created.
proofKeeper := proofkeeper.NewKeeper(
cdc,
runtime.NewKVStoreService(keys[prooftypes.StoreKey]),
logger,
authority.String(),
sessionKeeper,
appKeeper,
accountKeeper,
)
// Construct a real tokenomics keeper so that claims & tokenomics can be created.
tokenomicsKeeper := tokenomicskeeper.NewKeeper(
cdc,
runtime.NewKVStoreService(keys[tokenomicstypes.StoreKey]),
logger,
authority.String(),
bankKeeper,
accountKeeper,
appKeeper,
proofKeeper,
)
require.NoError(t, tokenomicsKeeper.SetParams(ctx, tokenomicstypes.DefaultParams()))
keepers := TokenomicsModuleKeepers{
Keeper: &tokenomicsKeeper,
AccountKeeper: &accountKeeper,
BankKeeper: &bankKeeper,
ApplicationKeeper: &appKeeper,
ProofKeeper: &proofKeeper,
Codec: cdc,
}
// Apply any options to update the keepers or context prior to returning them.
for _, opt := range opts {
ctx = opt(ctx, &keepers)
}
return keepers, ctx
}