/
state_query.go
342 lines (309 loc) · 14.3 KB
/
state_query.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package updaters
import (
"context"
"fmt"
"strconv"
"time"
downtimev1 "github.com/lavanet/lava/x/downtime/v1"
"github.com/cosmos/cosmos-sdk/client"
"github.com/dgraph-io/ristretto"
reliabilitymanager "github.com/lavanet/lava/protocol/rpcprovider/reliabilitymanager"
"github.com/lavanet/lava/utils"
conflicttypes "github.com/lavanet/lava/x/conflict/types"
epochstoragetypes "github.com/lavanet/lava/x/epochstorage/types"
pairingtypes "github.com/lavanet/lava/x/pairing/types"
plantypes "github.com/lavanet/lava/x/plans/types"
protocoltypes "github.com/lavanet/lava/x/protocol/types"
spectypes "github.com/lavanet/lava/x/spec/types"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
const (
CacheMaxCost = 10 * 1024 // 10K cost
CacheNumCounters = 100000 // expect 10K items
DefaultTimeToLiveExpiration = 30 * time.Minute
PairingRespKey = "pairing-resp"
VerifyPairingRespKey = "verify-pairing-resp"
MaxCuResponseKey = "max-cu-resp"
EffectivePolicyRespKey = "effective-policy-resp"
)
type ProtocolVersionResponse struct {
Version *protocoltypes.Version
BlockNumber string
}
type StateQuery struct {
SpecQueryClient spectypes.QueryClient
PairingQueryClient pairingtypes.QueryClient
EpochStorageQueryClient epochstoragetypes.QueryClient
ProtocolClient protocoltypes.QueryClient
DowntimeClient downtimev1.QueryClient
ResponsesCache *ristretto.Cache
}
func NewStateQuery(ctx context.Context, clientCtx client.Context) *StateQuery {
sq := &StateQuery{}
sq.SpecQueryClient = spectypes.NewQueryClient(clientCtx)
sq.PairingQueryClient = pairingtypes.NewQueryClient(clientCtx)
sq.EpochStorageQueryClient = epochstoragetypes.NewQueryClient(clientCtx)
sq.ProtocolClient = protocoltypes.NewQueryClient(clientCtx)
sq.DowntimeClient = downtimev1.NewQueryClient(clientCtx)
cache, err := ristretto.NewCache(&ristretto.Config{NumCounters: CacheNumCounters, MaxCost: CacheMaxCost, BufferItems: 64})
if err != nil {
utils.LavaFormatFatal("failed setting up cache for queries", err)
}
sq.ResponsesCache = cache
return sq
}
func (csq *StateQuery) GetProtocolVersion(ctx context.Context) (*ProtocolVersionResponse, error) {
header := metadata.MD{}
param, err := csq.ProtocolClient.Params(ctx, &protocoltypes.QueryParamsRequest{}, grpc.Header(&header))
if err != nil {
return nil, err
}
blockHeight := "unInitialized"
blockHeights := header.Get("x-cosmos-block-height")
if len(blockHeights) > 0 {
blockHeight = blockHeights[0]
}
return &ProtocolVersionResponse{BlockNumber: blockHeight, Version: ¶m.Params.Version}, nil
}
func (csq *StateQuery) GetSpec(ctx context.Context, chainID string) (*spectypes.Spec, error) {
spec, err := csq.SpecQueryClient.Spec(ctx, &spectypes.QueryGetSpecRequest{
ChainID: chainID,
})
if err != nil {
return nil, utils.LavaFormatError("Failed Querying spec for chain", err, utils.Attribute{Key: "ChainID", Value: chainID})
}
return &spec.Spec, nil
}
func (csq *StateQuery) GetDowntimeParams(ctx context.Context) (*downtimev1.Params, error) {
res, err := csq.DowntimeClient.QueryParams(ctx, &downtimev1.QueryParamsRequest{})
if err != nil {
return nil, err
}
return res.Params, nil
}
type ConsumerStateQuery struct {
StateQuery
clientCtx client.Context
lastChainID string
}
func NewConsumerStateQuery(ctx context.Context, clientCtx client.Context) *ConsumerStateQuery {
csq := &ConsumerStateQuery{StateQuery: *NewStateQuery(ctx, clientCtx), clientCtx: clientCtx, lastChainID: ""}
return csq
}
func (csq *ConsumerStateQuery) GetEffectivePolicy(ctx context.Context, consumerAddress, specID string) (*plantypes.Policy, error) {
cachedInterface, found := csq.ResponsesCache.Get(EffectivePolicyRespKey + specID)
if found && cachedInterface != nil {
if cachedResp, ok := cachedInterface.(*pairingtypes.QueryEffectivePolicyResponse); ok {
return cachedResp.GetPolicy(), nil
} else {
utils.LavaFormatError("invalid cache entry - failed casting response", nil, utils.Attribute{Key: "castingType", Value: "*pairingtypes.QueryEffectivePolicyResponse"}, utils.Attribute{Key: "type", Value: cachedInterface})
}
}
resp, err := csq.PairingQueryClient.EffectivePolicy(ctx, &pairingtypes.QueryEffectivePolicyRequest{
Consumer: consumerAddress,
SpecID: specID,
})
if err != nil || resp.GetPolicy() == nil {
return nil, err
}
csq.ResponsesCache.SetWithTTL(EffectivePolicyRespKey+specID, resp, 1, DefaultTimeToLiveExpiration)
return resp.GetPolicy(), nil
}
func (csq *ConsumerStateQuery) GetPairing(ctx context.Context, chainID string, latestBlock int64) (pairingList []epochstoragetypes.StakeEntry, epoch, nextBlockForUpdate uint64, errRet error) {
if chainID == "" {
if csq.lastChainID != "" {
chainID = csq.lastChainID
}
if chainID == "" {
chainID = "LAV1"
utils.LavaFormatWarning("failed to run get pairing as there is no entry for empty chainID call, using default chainID", nil, utils.Attribute{Key: "chainID", Value: chainID})
}
}
cachedInterface, found := csq.ResponsesCache.Get(PairingRespKey + chainID)
if found && cachedInterface != nil {
if cachedResp, ok := cachedInterface.(*pairingtypes.QueryGetPairingResponse); ok {
if cachedResp.BlockOfNextPairing > uint64(latestBlock) {
return cachedResp.Providers, cachedResp.CurrentEpoch, cachedResp.BlockOfNextPairing, nil
}
} else {
utils.LavaFormatError("invalid cache entry - failed casting response", nil, utils.Attribute{Key: "castingType", Value: "*pairingtypes.QueryGetPairingResponse"}, utils.Attribute{Key: "type", Value: cachedInterface})
}
}
pairingResp, err := csq.PairingQueryClient.GetPairing(ctx, &pairingtypes.QueryGetPairingRequest{
ChainID: chainID,
Client: csq.clientCtx.FromAddress.String(),
})
if err != nil {
return nil, 0, 0, utils.LavaFormatError("Failed in get pairing query", err, utils.Attribute{})
}
csq.lastChainID = chainID
csq.ResponsesCache.SetWithTTL(PairingRespKey+chainID, pairingResp, 1, DefaultTimeToLiveExpiration)
return pairingResp.Providers, pairingResp.CurrentEpoch, pairingResp.BlockOfNextPairing, nil
}
func (csq *ConsumerStateQuery) GetMaxCUForUser(ctx context.Context, chainID string, epoch uint64) (maxCu uint64, err error) {
address := csq.clientCtx.FromAddress.String()
UserEntryRes, err := csq.PairingQueryClient.UserEntry(ctx, &pairingtypes.QueryUserEntryRequest{ChainID: chainID, Address: address, Block: epoch})
if err != nil {
return 0, utils.LavaFormatError("failed querying StakeEntry for consumer", err, utils.Attribute{Key: "chainID", Value: chainID}, utils.Attribute{Key: "address", Value: address}, utils.Attribute{Key: "block", Value: epoch})
}
return UserEntryRes.GetMaxCU(), nil
}
type EpochStateQuery struct {
StateQuery
}
func (esq *EpochStateQuery) CurrentEpochStart(ctx context.Context) (uint64, error) {
epochDetails, err := esq.EpochStorageQueryClient.EpochDetails(ctx, &epochstoragetypes.QueryGetEpochDetailsRequest{})
if err != nil {
return 0, utils.LavaFormatError("Failed Querying EpochDetails", err)
}
details := epochDetails.GetEpochDetails()
return details.StartBlock, nil
}
func NewEpochStateQuery(stateQuery *StateQuery) *EpochStateQuery {
return &EpochStateQuery{StateQuery: *stateQuery}
}
type ProviderStateQuery struct {
StateQuery
EpochStateQuery
clientCtx client.Context
}
func NewProviderStateQuery(ctx context.Context, clientCtx client.Context) *ProviderStateQuery {
sq := NewStateQuery(ctx, clientCtx)
esq := NewEpochStateQuery(sq)
csq := &ProviderStateQuery{StateQuery: *sq, EpochStateQuery: *esq, clientCtx: clientCtx}
return csq
}
func (psq *ProviderStateQuery) GetMaxCuForUser(ctx context.Context, consumerAddress, chainID string, epoch uint64) (maxCu uint64, err error) {
key := psq.entryKey(consumerAddress, chainID, epoch, "")
cachedInterface, found := psq.ResponsesCache.Get(MaxCuResponseKey + key)
var userEntryRes *pairingtypes.QueryUserEntryResponse = nil
if found && cachedInterface != nil {
if cachedResp, ok := cachedInterface.(*pairingtypes.QueryUserEntryResponse); ok {
userEntryRes = cachedResp
} else {
utils.LavaFormatError("invalid cache entry - failed casting response", nil, utils.Attribute{Key: "castingType", Value: "*pairingtypes.QueryUserEntryResponse"}, utils.Attribute{Key: "type", Value: fmt.Sprintf("%T", cachedInterface)})
}
}
if userEntryRes == nil {
userEntryRes, err = psq.PairingQueryClient.UserEntry(ctx, &pairingtypes.QueryUserEntryRequest{ChainID: chainID, Address: consumerAddress, Block: epoch})
if err != nil {
return 0, utils.LavaFormatError("StakeEntry querying for consumer failed", err, utils.Attribute{Key: "chainID", Value: chainID}, utils.Attribute{Key: "address", Value: consumerAddress}, utils.Attribute{Key: "block", Value: epoch})
}
psq.ResponsesCache.SetWithTTL(MaxCuResponseKey+key, userEntryRes, 1, DefaultTimeToLiveExpiration)
}
return userEntryRes.GetMaxCU(), err
}
func (psq *ProviderStateQuery) entryKey(consumerAddress, chainID string, epoch uint64, providerAddress string) string {
return consumerAddress + chainID + strconv.FormatUint(epoch, 10) + providerAddress
}
func (psq *ProviderStateQuery) VoteEvents(ctx context.Context, latestBlock int64) (votes []*reliabilitymanager.VoteParams, err error) {
brp, err := TryIntoTendermintRPC(psq.clientCtx.Client)
if err != nil {
return nil, utils.LavaFormatError("failed to get block result provider", err)
}
blockResults, err := brp.BlockResults(ctx, &latestBlock)
if err != nil {
return nil, err
}
transactionResults := blockResults.TxsResults
for _, tx := range transactionResults {
events := tx.Events
for _, event := range events {
if event.Type == utils.EventPrefix+conflicttypes.ConflictVoteDetectionEventName {
vote, err := reliabilitymanager.BuildVoteParamsFromDetectionEvent(event)
if err != nil {
return nil, utils.LavaFormatError("failed conflict_vote_detection_event parsing", err, utils.Attribute{Key: "event", Value: event})
}
utils.LavaFormatDebug("conflict_vote_detection_event", utils.Attribute{Key: "voteID", Value: vote.VoteID})
votes = append(votes, vote)
}
}
}
beginBlockEvents := blockResults.BeginBlockEvents
for _, event := range beginBlockEvents {
if event.Type == utils.EventPrefix+conflicttypes.ConflictVoteRevealEventName {
voteID, voteDeadline, err := reliabilitymanager.BuildBaseVoteDataFromEvent(event)
if err != nil {
return nil, utils.LavaFormatError("failed conflict_vote_reveal_event parsing", err, utils.Attribute{Key: "event", Value: event})
}
vote_reveal := &reliabilitymanager.VoteParams{VoteID: voteID, VoteDeadline: voteDeadline, ParamsType: reliabilitymanager.RevealVoteType}
utils.LavaFormatDebug("conflict_vote_reveal_event", utils.Attribute{Key: "voteID", Value: voteID})
votes = append(votes, vote_reveal)
}
if event.Type == utils.EventPrefix+conflicttypes.ConflictVoteResolvedEventName {
voteID, _, err := reliabilitymanager.BuildBaseVoteDataFromEvent(event)
if err != nil {
if !reliabilitymanager.NoVoteDeadline.Is(err) {
return nil, utils.LavaFormatError("failed conflict_vote_resolved_event parsing", err, utils.Attribute{Key: "event", Value: event})
}
}
vote_resolved := &reliabilitymanager.VoteParams{VoteID: voteID, VoteDeadline: 0, ParamsType: reliabilitymanager.CloseVoteType, CloseVote: true}
votes = append(votes, vote_resolved)
utils.LavaFormatDebug("conflict_vote_resolved_event", utils.Attribute{Key: "voteID", Value: voteID})
}
}
return votes, err
}
func (psq *ProviderStateQuery) VerifyPairing(ctx context.Context, consumerAddress, providerAddress string, epoch uint64, chainID string) (valid bool, total int64, projectId string, err error) {
key := psq.entryKey(consumerAddress, chainID, epoch, providerAddress)
extractedResultFromCache := false
cachedInterface, found := psq.ResponsesCache.Get(VerifyPairingRespKey + key)
var verifyResponse *pairingtypes.QueryVerifyPairingResponse = nil
if found && cachedInterface != nil {
if cachedResp, ok := cachedInterface.(*pairingtypes.QueryVerifyPairingResponse); ok {
verifyResponse = cachedResp
extractedResultFromCache = true
} else {
utils.LavaFormatError("invalid cache entry - failed casting response", nil, utils.Attribute{Key: "castingType", Value: "*pairingtypes.QueryVerifyPairingResponse"}, utils.Attribute{Key: "type", Value: fmt.Sprintf("%T", cachedInterface)})
}
}
if verifyResponse == nil {
verifyResponse, err = psq.PairingQueryClient.VerifyPairing(context.Background(), &pairingtypes.QueryVerifyPairingRequest{
ChainID: chainID,
Client: consumerAddress,
Provider: providerAddress,
Block: epoch,
})
if err != nil {
return false, 0, "", err
}
psq.ResponsesCache.SetWithTTL(VerifyPairingRespKey+key, verifyResponse, 1, DefaultTimeToLiveExpiration)
}
if !verifyResponse.Valid {
return false, 0, "", utils.LavaFormatError("invalid self pairing with consumer", nil, utils.Attribute{Key: "provider", Value: providerAddress}, utils.Attribute{Key: "consumer address", Value: consumerAddress}, utils.Attribute{Key: "epoch", Value: epoch}, utils.Attribute{Key: "from_cache", Value: extractedResultFromCache})
}
return verifyResponse.Valid, int64(verifyResponse.GetPairedProviders()), verifyResponse.ProjectId, nil
}
func (psq *ProviderStateQuery) GetEpochSize(ctx context.Context) (uint64, error) {
res, err := psq.EpochStorageQueryClient.Params(ctx, &epochstoragetypes.QueryParamsRequest{})
if err != nil {
return 0, err
}
return res.Params.EpochBlocks, nil
}
func (psq *ProviderStateQuery) EarliestBlockInMemory(ctx context.Context) (uint64, error) {
res, err := psq.EpochStorageQueryClient.EpochDetails(ctx, &epochstoragetypes.QueryGetEpochDetailsRequest{})
if err != nil {
return 0, err
}
return res.EpochDetails.EarliestStart, nil
}
func (psq *ProviderStateQuery) GetRecommendedEpochNumToCollectPayment(ctx context.Context) (uint64, error) {
res, err := psq.PairingQueryClient.Params(ctx, &pairingtypes.QueryParamsRequest{})
if err != nil {
return 0, err
}
return res.GetParams().RecommendedEpochNumToCollectPayment, nil
}
func (psq *ProviderStateQuery) GetEpochSizeMultipliedByRecommendedEpochNumToCollectPayment(ctx context.Context) (uint64, error) {
epochSize, err := psq.GetEpochSize(ctx)
if err != nil {
return 0, err
}
recommendedEpochNumToCollectPayment, err := psq.GetRecommendedEpochNumToCollectPayment(ctx)
if err != nil {
return 0, err
}
return epochSize * recommendedEpochNumToCollectPayment, nil
}