This repository has been archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathget_user_states.go
105 lines (85 loc) · 3.09 KB
/
get_user_states.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
package api
import (
"context"
"github.com/Worldcoin/hubble-commander/models"
"github.com/Worldcoin/hubble-commander/models/dto"
"github.com/Worldcoin/hubble-commander/o11y"
"github.com/Worldcoin/hubble-commander/storage"
"github.com/Worldcoin/hubble-commander/utils/consts"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
var getUserStatesTracer = otel.Tracer("api.getUserStates")
var getUserStatesAPIErrors = map[error]*APIError{
storage.AnyNotFoundError: NewAPIError(99003, "user states not found"),
}
func (a *API) GetUserStates(ctx context.Context, publicKey *models.PublicKey) ([]dto.UserStateWithID, error) {
batch, err := a.unsafeGetUserStates(ctx, publicKey)
if err != nil {
return nil, sanitizeError(err, getUserStatesAPIErrors)
}
return batch, nil
}
func (a *API) unsafeGetUserStates(ctx context.Context, publicKey *models.PublicKey) ([]dto.UserStateWithID, error) {
span := trace.SpanFromContext(ctx)
span.SetAttributes(attribute.String("hubble.publicKey", publicKey.String()))
userStates := make([]dto.UserStateWithID, 0)
err := a.storage.ExecuteInReadWriteTransactionWithSpan(ctx, func(txCtx context.Context, txStorage *storage.Storage) error {
leaves, err := func() ([]models.StateLeaf, error) {
_, innerSpan := getUserStatesTracer.Start(txCtx, "GetStateLeavesByPublicKey")
defer innerSpan.End()
return txStorage.GetStateLeavesByPublicKey(publicKey)
}()
if err != nil && !storage.IsNotFoundError(err) {
span.SetAttributes(attribute.String("hubble.error", err.Error()))
log.WithFields(o11y.TraceFields(txCtx)).Errorf("Error getting leaves by public key: %v", err)
return err
}
for i := range leaves {
stateID := leaves[i].StateID
pendingState, innerErr := func() (*models.UserState, error) {
_, innerSpan := getUserStatesTracer.Start(txCtx, "GetPendingUserState")
defer innerSpan.End()
return txStorage.GetPendingUserState(stateID)
}()
if innerErr != nil {
return innerErr
}
userStates = append(userStates, dto.MakeUserStateWithID(stateID, pendingState))
}
pendingC2TState, err := func() (*models.UserState, error) {
_, innerSpan := getUserStatesTracer.Start(txCtx, "GetPendingC2TState")
defer innerSpan.End()
return txStorage.GetPendingC2TState(publicKey)
}()
if err != nil {
return err
}
if pendingC2TState != nil {
userStates = append(
userStates,
dto.MakeUserStateWithID(consts.PendingID, pendingC2TState),
)
}
return nil
})
if err != nil {
return nil, err
}
if len(userStates) == 0 {
span.SetAttributes(attribute.String("hubble.error", "user states not found"))
return nil, errors.WithStack(
storage.NewNotFoundError("user states"),
)
}
totalBalance := models.NewUint256(0)
for _, userState := range userStates {
totalBalance = totalBalance.Add(&userState.Balance)
}
span.SetAttributes(attribute.String("hubble.response.totalBalance", totalBalance.String()))
span.SetAttributes(attribute.Int("hubble.response.userStatesCount", len(userStates)))
return userStates, nil
}