-
Notifications
You must be signed in to change notification settings - Fork 211
/
globalstate_service.go
455 lines (413 loc) · 16.8 KB
/
globalstate_service.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package grpcserver
import (
"context"
"fmt"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
pb "github.com/spacemeshos/api/release/go/spacemesh/v1"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/events"
)
// GlobalStateService exposes global state data, output from the STF.
type GlobalStateService struct {
mesh meshAPI
conState conservativeState
}
// RegisterService registers this service with a grpc server instance.
func (s GlobalStateService) RegisterService(server *grpc.Server) {
pb.RegisterGlobalStateServiceServer(server, s)
}
func (s GlobalStateService) RegisterHandlerService(mux *runtime.ServeMux) error {
return pb.RegisterGlobalStateServiceHandlerServer(context.Background(), mux, s)
}
// String returns the name of the service.
func (s GlobalStateService) String() string {
return "GlobalStateService"
}
// NewGlobalStateService creates a new grpc service using config data.
func NewGlobalStateService(msh meshAPI, conState conservativeState) *GlobalStateService {
return &GlobalStateService{
mesh: msh,
conState: conState,
}
}
// GlobalStateHash returns the latest layer and its computed global state hash.
func (s GlobalStateService) GlobalStateHash(
context.Context,
*pb.GlobalStateHashRequest,
) (*pb.GlobalStateHashResponse, error) {
root, err := s.conState.GetStateRoot()
if err != nil {
return nil, err
}
return &pb.GlobalStateHashResponse{Response: &pb.GlobalStateHash{
RootHash: root.Bytes(),
Layer: &pb.LayerNumber{Number: s.mesh.LatestLayerInState().Uint32()},
}}, nil
}
func (s GlobalStateService) getAccount(addr types.Address) (acct *pb.Account, err error) {
balanceActual, err := s.conState.GetBalance(addr)
if err != nil {
return nil, err
}
counterActual, err := s.conState.GetNonce(addr)
if err != nil {
return nil, err
}
counterProjected, balanceProjected := s.conState.GetProjection(addr)
return &pb.Account{
AccountId: &pb.AccountId{Address: addr.String()},
StateCurrent: &pb.AccountState{
Counter: counterActual,
Balance: &pb.Amount{Value: balanceActual},
},
StateProjected: &pb.AccountState{
Counter: counterProjected,
Balance: &pb.Amount{Value: balanceProjected},
},
}, nil
}
// Account returns current and projected counter and balance for one account.
func (s GlobalStateService) Account(ctx context.Context, in *pb.AccountRequest) (*pb.AccountResponse, error) {
if in.AccountId == nil {
return nil, status.Errorf(codes.InvalidArgument, "`AccountId` must be provided")
}
// Load data
addr, err := types.StringToAddress(in.AccountId.Address)
if err != nil {
return nil, fmt.Errorf("failed to parse in.AccountId.Address `%s`: %w", in.AccountId.Address, err)
}
acct, err := s.getAccount(addr)
if err != nil {
ctxzap.Error(ctx, "unable to fetch projected account state", zap.Error(err))
return nil, status.Errorf(codes.Internal, "error fetching projected account data")
}
ctxzap.Debug(ctx, "GRPC GlobalStateService.Account",
addr.Field().Zap(),
zap.Uint64("balance", acct.StateCurrent.Balance.Value),
zap.Uint64("counter", acct.StateCurrent.Counter),
zap.Uint64("balance projected", acct.StateProjected.Balance.Value),
zap.Uint64("counter projected", acct.StateProjected.Counter),
)
return &pb.AccountResponse{AccountWrapper: acct}, nil
}
// AccountDataQuery returns historical account data such as rewards and receipts.
func (s GlobalStateService) AccountDataQuery(
ctx context.Context,
in *pb.AccountDataQueryRequest,
) (*pb.AccountDataQueryResponse, error) {
if in.Filter == nil {
return nil, status.Errorf(codes.InvalidArgument, "`Filter` must be provided")
}
if in.Filter.AccountId == nil {
return nil, status.Errorf(codes.InvalidArgument, "`Filter.AccountId` must be provided")
}
if in.Filter.AccountDataFlags == uint32(pb.AccountDataFlag_ACCOUNT_DATA_FLAG_UNSPECIFIED) {
return nil, status.Errorf(codes.InvalidArgument, "`Filter.AccountMeshDataFlags` must set at least one bitfield")
}
// Read the filter flags
_ = in.Filter.AccountDataFlags&uint32(pb.AccountDataFlag_ACCOUNT_DATA_FLAG_TRANSACTION_RECEIPT) != 0
filterReward := in.Filter.AccountDataFlags&uint32(pb.AccountDataFlag_ACCOUNT_DATA_FLAG_REWARD) != 0
filterAccount := in.Filter.AccountDataFlags&uint32(pb.AccountDataFlag_ACCOUNT_DATA_FLAG_ACCOUNT) != 0
addr, err := types.StringToAddress(in.Filter.AccountId.Address)
if err != nil {
return nil, fmt.Errorf("failed to parse in.Filter.AccountId.Address `%s`: %w", in.Filter.AccountId.Address, err)
}
res := &pb.AccountDataQueryResponse{}
// TODO: Implement this. The node does not implement tx receipts yet.
// See https://github.com/spacemeshos/go-spacemesh/issues/2072
// if filterTxReceipt {}
if filterReward {
dbRewards, err := s.mesh.GetRewardsByCoinbase(addr)
if err != nil {
return nil, status.Errorf(codes.Internal, "error getting rewards data")
}
for _, r := range dbRewards {
res.AccountItem = append(res.AccountItem, &pb.AccountData{Datum: &pb.AccountData_Reward{
Reward: &pb.Reward{
Layer: &pb.LayerNumber{Number: r.Layer.Uint32()},
Total: &pb.Amount{Value: r.TotalReward},
LayerReward: &pb.Amount{Value: r.LayerReward},
Coinbase: &pb.AccountId{Address: addr.String()},
Smesher: &pb.SmesherId{Id: r.SmesherID[:]},
},
}})
}
}
if filterAccount {
acct, err := s.getAccount(addr)
if err != nil {
ctxzap.Error(ctx, "unable to fetch projected account state", zap.Error(err))
return nil, status.Errorf(codes.Internal, "error fetching projected account data")
}
res.AccountItem = append(res.AccountItem, &pb.AccountData{Datum: &pb.AccountData_AccountWrapper{
AccountWrapper: acct,
}})
}
// MAX RESULTS, OFFSET
// There is some code duplication here as this is implemented in other Query endpoints,
// but without generics, there's no clean way to do this for different types.
// Adjust for max results, offset
res.TotalResults = uint32(len(res.AccountItem))
// Skip to offset, don't send more than max results
// TODO: Optimize this. Obviously, we could do much smarter things than re-loading all
// of the data from scratch, then figuring out which data to return here. We could cache
// query results and/or figure out which data to load before loading it.
// See https://github.com/spacemeshos/go-spacemesh/issues/2073
offset := in.Offset
// If the offset is too high there is nothing to return (this is not an error)
if offset > uint32(len(res.AccountItem)) {
return &pb.AccountDataQueryResponse{}, nil
}
// If the max results is too high, trim it. If MaxResults is zero, that means unlimited
// (since we have no way to distinguish between zero and its not being provided).
maxResults := in.MaxResults
if maxResults == 0 || offset+maxResults > uint32(len(res.AccountItem)) {
maxResults = uint32(len(res.AccountItem)) - offset
}
res.AccountItem = res.AccountItem[offset : offset+maxResults]
return res, nil
}
// SmesherDataQuery returns historical info on smesher rewards.
func (s GlobalStateService) SmesherDataQuery(
_ context.Context,
in *pb.SmesherDataQueryRequest,
) (*pb.SmesherDataQueryResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "DEPRECATED")
}
// STREAMS
// AccountDataStream exposes a stream of account-related data.
func (s GlobalStateService) AccountDataStream(
in *pb.AccountDataStreamRequest,
stream pb.GlobalStateService_AccountDataStreamServer,
) error {
if in.Filter == nil {
return status.Errorf(codes.InvalidArgument, "`Filter` must be provided")
}
if in.Filter.AccountId == nil {
return status.Errorf(codes.InvalidArgument, "`Filter.AccountId` must be provided")
}
if in.Filter.AccountDataFlags == uint32(pb.AccountDataFlag_ACCOUNT_DATA_FLAG_UNSPECIFIED) {
return status.Errorf(codes.InvalidArgument, "`Filter.AccountDataFlags` must set at least one bitfield")
}
addr, err := types.StringToAddress(in.Filter.AccountId.Address)
if err != nil {
return fmt.Errorf("failed to parse in.Filter.AccountId.Address `%s`: %w", in.Filter.AccountId.Address, err)
}
filterAccount := in.Filter.AccountDataFlags&uint32(pb.AccountDataFlag_ACCOUNT_DATA_FLAG_ACCOUNT) != 0
filterReward := in.Filter.AccountDataFlags&uint32(pb.AccountDataFlag_ACCOUNT_DATA_FLAG_REWARD) != 0
// Subscribe to the various streams
var (
accountCh <-chan events.Account
rewardsCh <-chan types.Reward
receiptsCh <-chan any
accountBufFull <-chan struct{}
rewardsBufFull <-chan struct{}
)
if filterAccount {
if accountSubscription := events.SubscribeAccount(); accountSubscription != nil {
accountCh, accountBufFull = consumeEvents[events.Account](stream.Context(), accountSubscription)
}
}
if filterReward {
if rewardsSubscription := events.SubscribeRewards(); rewardsSubscription != nil {
rewardsCh, rewardsBufFull = consumeEvents[types.Reward](stream.Context(), rewardsSubscription)
}
}
if err := stream.SendHeader(metadata.MD{}); err != nil {
return err
}
for {
select {
case <-accountBufFull:
ctxzap.Info(stream.Context(), "account buffer is full, shutting down")
return status.Error(codes.Canceled, errAccountBufferFull)
case <-rewardsBufFull:
ctxzap.Info(stream.Context(), "rewards buffer is full, shutting down")
return status.Error(codes.Canceled, errRewardsBufferFull)
case updatedAccountEvent := <-accountCh:
// Apply address filter
if updatedAccountEvent.Address == addr {
// The Reporter service just sends us the account address. We are responsible
// for looking up the other required data here. Get the account balance and
// nonce.
acct, err := s.getAccount(addr)
if err != nil {
ctxzap.Error(stream.Context(), "unable to fetch projected account state", zap.Error(err))
return status.Errorf(codes.Internal, "error fetching projected account data")
}
resp := &pb.AccountDataStreamResponse{Datum: &pb.AccountData{Datum: &pb.AccountData_AccountWrapper{
AccountWrapper: acct,
}}}
if err := stream.Send(resp); err != nil {
return fmt.Errorf("send to stream: %w", err)
}
}
case reward := <-rewardsCh:
// Apply address filter
if reward.Coinbase == addr {
resp := &pb.AccountDataStreamResponse{Datum: &pb.AccountData{Datum: &pb.AccountData_Reward{
Reward: &pb.Reward{
Layer: &pb.LayerNumber{Number: reward.Layer.Uint32()},
Total: &pb.Amount{Value: reward.TotalReward},
LayerReward: &pb.Amount{Value: reward.LayerReward},
Coinbase: &pb.AccountId{Address: addr.String()},
Smesher: &pb.SmesherId{Id: reward.SmesherID[:]},
},
}}}
if err := stream.Send(resp); err != nil {
return fmt.Errorf("send to stream: %w", err)
}
}
case receiptEvent := <-receiptsCh:
receipt := receiptEvent.(events.TxReceipt)
// Apply address filter
if receipt.Address == addr {
resp := &pb.AccountDataStreamResponse{Datum: &pb.AccountData{Datum: &pb.AccountData_Receipt{
Receipt: &pb.TransactionReceipt{
Id: &pb.TransactionId{Id: receipt.ID.Bytes()},
// Result: receipt.Result,
GasUsed: receipt.GasUsed,
Fee: &pb.Amount{Value: receipt.Fee},
Layer: &pb.LayerNumber{Number: receipt.Layer.Uint32()},
Index: receipt.Index,
// SvmData: nil,
},
}}}
if err := stream.Send(resp); err != nil {
return fmt.Errorf("send to stream: %w", err)
}
}
case <-stream.Context().Done():
ctxzap.Info(stream.Context(), "AccountDataStream closing stream, client disconnected")
return nil
}
// TODO: do we need an additional case here for a context to indicate
// that the service needs to shut down?
// See https://github.com/spacemeshos/go-spacemesh/issues/2075
}
}
// SmesherRewardStream exposes a stream of smesher rewards.
func (s GlobalStateService) SmesherRewardStream(
in *pb.SmesherRewardStreamRequest,
stream pb.GlobalStateService_SmesherRewardStreamServer,
) error {
return status.Errorf(codes.Unimplemented, "DEPRECATED")
}
// AppEventStream exposes a stream of emitted app events.
func (s GlobalStateService) AppEventStream(
*pb.AppEventStreamRequest,
pb.GlobalStateService_AppEventStreamServer,
) error {
// TODO: implement me! We don't currently have any app events
// See https://github.com/spacemeshos/go-spacemesh/issues/2074
return status.Errorf(codes.Unimplemented, "this endpoint has not yet been implemented")
}
// GlobalStateStream exposes a stream of global data data items: rewards, receipts, account info, global state hash.
func (s GlobalStateService) GlobalStateStream(
in *pb.GlobalStateStreamRequest,
stream pb.GlobalStateService_GlobalStateStreamServer,
) error {
if in.GlobalStateDataFlags == uint32(pb.GlobalStateDataFlag_GLOBAL_STATE_DATA_FLAG_UNSPECIFIED) {
return status.Errorf(codes.InvalidArgument, "`GlobalStateDataFlags` must set at least one bitfield")
}
filterAccount := in.GlobalStateDataFlags&uint32(pb.GlobalStateDataFlag_GLOBAL_STATE_DATA_FLAG_ACCOUNT) != 0
filterReward := in.GlobalStateDataFlags&uint32(pb.GlobalStateDataFlag_GLOBAL_STATE_DATA_FLAG_REWARD) != 0
filterState := in.GlobalStateDataFlags&uint32(pb.GlobalStateDataFlag_GLOBAL_STATE_DATA_FLAG_GLOBAL_STATE_HASH) != 0
// Subscribe to the various streams
var (
accountCh <-chan events.Account
rewardsCh <-chan types.Reward
layersCh <-chan events.LayerUpdate
accountBufFull <-chan struct{}
rewardsBufFull <-chan struct{}
layersBufFull <-chan struct{}
)
if filterAccount {
if accountSubscription := events.SubscribeAccount(); accountSubscription != nil {
accountCh, accountBufFull = consumeEvents[events.Account](stream.Context(), accountSubscription)
}
}
if filterReward {
if rewardsSubscription := events.SubscribeRewards(); rewardsSubscription != nil {
rewardsCh, rewardsBufFull = consumeEvents[types.Reward](stream.Context(), rewardsSubscription)
}
}
if filterState {
// Whenever new state is applied to the mesh, a new layer is reported.
// There is no separate reporting specifically for new state.
if layersSubscription := events.SubscribeLayers(); layersSubscription != nil {
layersCh, layersBufFull = consumeEvents[events.LayerUpdate](stream.Context(), layersSubscription)
}
}
for {
select {
case <-accountBufFull:
ctxzap.Info(stream.Context(), "account buffer is full, shutting down")
return status.Error(codes.Canceled, errAccountBufferFull)
case <-rewardsBufFull:
ctxzap.Info(stream.Context(), "rewards buffer is full, shutting down")
return status.Error(codes.Canceled, errRewardsBufferFull)
case <-layersBufFull:
ctxzap.Info(stream.Context(), "layers buffer is full, shutting down")
return status.Error(codes.Canceled, errLayerBufferFull)
case updatedAccount := <-accountCh:
// The Reporter service just sends us the account address. We are responsible
// for looking up the other required data here. Get the account balance and
// nonce.
acct, err := s.getAccount(updatedAccount.Address)
if err != nil {
ctxzap.Error(stream.Context(), "unable to fetch projected account state", zap.Error(err))
return status.Errorf(codes.Internal, "error fetching projected account data")
}
resp := &pb.GlobalStateStreamResponse{Datum: &pb.GlobalStateData{Datum: &pb.GlobalStateData_AccountWrapper{
AccountWrapper: acct,
}}}
if err := stream.Send(resp); err != nil {
return fmt.Errorf("send to stream: %w", err)
}
case reward := <-rewardsCh:
resp := &pb.GlobalStateStreamResponse{Datum: &pb.GlobalStateData{Datum: &pb.GlobalStateData_Reward{
Reward: &pb.Reward{
Layer: &pb.LayerNumber{Number: reward.Layer.Uint32()},
Total: &pb.Amount{Value: reward.TotalReward},
LayerReward: &pb.Amount{Value: reward.LayerReward},
Coinbase: &pb.AccountId{Address: reward.Coinbase.String()},
Smesher: &pb.SmesherId{Id: reward.SmesherID[:]},
},
}}}
if err := stream.Send(resp); err != nil {
return fmt.Errorf("send to stream: %w", err)
}
case layer := <-layersCh:
if layer.Status != events.LayerStatusTypeApplied {
continue
}
root, err := s.conState.GetLayerStateRoot(layer.LayerID)
if err != nil {
ctxzap.Warn(stream.Context(), "error retrieving layer data", zap.Error(err))
root = types.Hash32{}
}
resp := &pb.GlobalStateStreamResponse{Datum: &pb.GlobalStateData{Datum: &pb.GlobalStateData_GlobalState{
GlobalState: &pb.GlobalStateHash{
RootHash: root.Bytes(),
Layer: &pb.LayerNumber{Number: layer.LayerID.Uint32()},
},
}}}
if err := stream.Send(resp); err != nil {
return fmt.Errorf("send to stream: %w", err)
}
case <-stream.Context().Done():
ctxzap.Info(stream.Context(), "AccountDataStream closing stream, client disconnected")
return nil
}
// TODO: do we need an additional case here for a context to indicate
// that the service needs to shut down?
// See https://github.com/spacemeshos/go-spacemesh/issues/2075
}
}