-
Notifications
You must be signed in to change notification settings - Fork 8
/
session.go
229 lines (196 loc) · 7.26 KB
/
session.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
package keeper
import (
"context"
"encoding/hex"
"testing"
"cosmossdk.io/log"
"cosmossdk.io/math"
"cosmossdk.io/store"
"cosmossdk.io/store/metrics"
"cosmossdk.io/store/prefix"
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"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"github.com/pokt-network/poktroll/testutil/sample"
"github.com/pokt-network/poktroll/testutil/session/mocks"
apptypes "github.com/pokt-network/poktroll/x/application/types"
"github.com/pokt-network/poktroll/x/session/keeper"
"github.com/pokt-network/poktroll/x/session/types"
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
)
var (
TestServiceId1 = "svc1" // staked for by app1 & supplier1
TestServiceId11 = "svc11" // staked for by app1
TestServiceId2 = "svc2" // staked for by app2 & supplier1
TestServiceId22 = "svc22" // staked for by app2
TestServiceId12 = "svc12" // staked for by app1, app2 & supplier1
TestApp1Address = "pokt1mdccn4u38eyjdxkk4h0jaddw4n3c72u82m5m9e" // Generated via sample.AccAddress()
TestApp1 = apptypes.Application{
Address: TestApp1Address,
Stake: &sdk.Coin{Denom: "upokt", Amount: math.NewInt(100)},
ServiceConfigs: []*sharedtypes.ApplicationServiceConfig{
{
Service: &sharedtypes.Service{Id: TestServiceId1},
},
{
Service: &sharedtypes.Service{Id: TestServiceId11},
},
{
Service: &sharedtypes.Service{Id: TestServiceId12},
},
},
}
TestApp2Address = "pokt133amv5suh75zwkxxcq896azvmmwszg99grvk9f" // Generated via sample.AccAddress()
TestApp2 = apptypes.Application{
Address: TestApp1Address,
Stake: &sdk.Coin{Denom: "upokt", Amount: math.NewInt(100)},
ServiceConfigs: []*sharedtypes.ApplicationServiceConfig{
{
Service: &sharedtypes.Service{Id: TestServiceId2},
},
{
Service: &sharedtypes.Service{Id: TestServiceId22},
},
{
Service: &sharedtypes.Service{Id: TestServiceId12},
},
},
}
TestSupplierUrl = "http://olshansky.info"
TestSupplierAddress = sample.AccAddress()
TestSupplier = sharedtypes.Supplier{
Address: TestSupplierAddress,
Stake: &sdk.Coin{Denom: "upokt", Amount: math.NewInt(100)},
Services: []*sharedtypes.SupplierServiceConfig{
{
Service: &sharedtypes.Service{Id: TestServiceId1},
Endpoints: []*sharedtypes.SupplierEndpoint{
{
Url: TestSupplierUrl,
RpcType: sharedtypes.RPCType_JSON_RPC,
Configs: make([]*sharedtypes.ConfigOption, 0),
},
},
},
{
Service: &sharedtypes.Service{Id: TestServiceId2},
Endpoints: []*sharedtypes.SupplierEndpoint{
{
Url: TestSupplierUrl,
RpcType: sharedtypes.RPCType_GRPC,
Configs: make([]*sharedtypes.ConfigOption, 0),
},
},
},
{
Service: &sharedtypes.Service{Id: TestServiceId12},
Endpoints: []*sharedtypes.SupplierEndpoint{
{
Url: TestSupplierUrl,
RpcType: sharedtypes.RPCType_GRPC,
Configs: make([]*sharedtypes.ConfigOption, 0),
},
},
},
},
}
)
func SessionKeeper(t testing.TB) (keeper.Keeper, context.Context) {
t.Helper()
storeKey := storetypes.NewKVStoreKey(types.StoreKey)
db := dbm.NewMemDB()
stateStore := store.NewCommitMultiStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
require.NoError(t, stateStore.LoadLatestVersion())
registry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(registry)
authority := authtypes.NewModuleAddress(govtypes.ModuleName)
ctrl := gomock.NewController(t)
mockBankKeeper := mocks.NewMockBankKeeper(ctrl)
mockAccountKeeper := mocks.NewMockAccountKeeper(ctrl)
mockAccountKeeper.EXPECT().GetAccount(gomock.Any(), gomock.Any()).AnyTimes()
mockAppKeeper := defaultAppKeeperMock(t)
mockSupplierKeeper := defaultSupplierKeeperMock(t)
k := keeper.NewKeeper(
cdc,
runtime.NewKVStoreService(storeKey),
log.NewNopLogger(),
authority.String(),
mockAccountKeeper,
mockBankKeeper,
mockAppKeeper,
mockSupplierKeeper,
)
// TODO_TECHDEBT: See the comment at the bottom of this file explaining
// why we don't support options yet.
// for _, opt := range opts {
// opt(k)
// }
ctx := sdk.NewContext(stateStore, cmtproto.Header{}, false, log.NewNopLogger())
// Initialize params
require.NoError(t, k.SetParams(ctx, types.DefaultParams()))
// In prod, the hashes of all block heights are stored in the hash store while
// the block hashes below are hardcoded to match the hardcoded session IDs used
// in the `session_hydrator_test.go`.
// TODO_IMPROVE: Use fixtures populated by block hashes and their corresponding
// session IDs for each block height in the [0, N] interval, instead of using
// in-place hardcoded values.
// Store block hashes to be used in tests
blockHash := map[int64]string{
0: "", // no session at block height 0
1: "1b1051b7bf236fea13efa65b6be678514fa5b6ea0ae9a7a4b68d45f95e4f18e0", // 1st session
5: "261594ddc3c8afc5b4c63f59ee58e89d3a115bcd164c83fd79349de0b1ffd21d", // 2nd session
9: "251665c7cf286a30fbd98acd983c63e9a34efc16496511373405e24eb02a8fb9", // 3rd session
}
storeAdapter := runtime.KVStoreAdapter(runtime.NewKVStoreService(storeKey).OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.BlockHashKeyPrefix))
for height, hash := range blockHash {
hashBz, err := hex.DecodeString(hash)
require.NoError(t, err)
store.Set(types.BlockHashKey(height), hashBz)
}
return k, ctx
}
func defaultAppKeeperMock(t testing.TB) types.ApplicationKeeper {
t.Helper()
ctrl := gomock.NewController(t)
getAppFn := func(_ context.Context, appAddr string) (apptypes.Application, bool) {
switch appAddr {
case TestApp1Address:
return TestApp1, true
case TestApp2Address:
return TestApp2, true
default:
return apptypes.Application{}, false
}
}
mockAppKeeper := mocks.NewMockApplicationKeeper(ctrl)
mockAppKeeper.EXPECT().GetApplication(gomock.Any(), gomock.Any()).AnyTimes().DoAndReturn(getAppFn)
mockAppKeeper.EXPECT().GetApplication(gomock.Any(), TestApp1Address).AnyTimes().Return(TestApp1, true)
return mockAppKeeper
}
func defaultSupplierKeeperMock(t testing.TB) types.SupplierKeeper {
t.Helper()
ctrl := gomock.NewController(t)
allSuppliers := []sharedtypes.Supplier{TestSupplier}
mockSupplierKeeper := mocks.NewMockSupplierKeeper(ctrl)
mockSupplierKeeper.EXPECT().GetAllSuppliers(gomock.Any()).AnyTimes().Return(allSuppliers)
return mockSupplierKeeper
}
// TODO_TECHDEBT: Figure out how to vary the supplierKeep on a per test basis with exposing `SupplierKeeper publically`
// type option[V any] func(k *keeper.Keeper)
// WithPublisher returns an option function which sets the given publishCh of the
// resulting observable when passed to NewObservable().
// func WithSupplierKeeperMock(supplierKeeper types.SupplierKeeper) option[any] {
// return func(k *keeper.Keeper) {
// k.supplierKeeper = supplierKeeper
// }
// }