@@ -2,6 +2,7 @@ package v2beta1
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
5
6
6
7
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
7
8
pb "github.com/spacemeshos/api/release/go/spacemesh/v2beta1"
@@ -13,11 +14,18 @@ import (
13
14
14
15
"github.com/spacemeshos/go-spacemesh/activation"
15
16
"github.com/spacemeshos/go-spacemesh/common/types"
17
+ identity "github.com/spacemeshos/go-spacemesh/identity"
16
18
"github.com/spacemeshos/go-spacemesh/sql/builder"
17
19
)
18
20
19
21
const SmeshingIdentities = "smeshing_identities_v2beta1"
20
22
23
+ type identityState interface {
24
+ All (ops builder.Operations ) ([]identity.IdStateInfo , error )
25
+ AllProposals () map [types.NodeID ][]* types.Proposal
26
+ AllEligibilities () map [types.NodeID ]map [types.LayerID ][]types.VotingEligibility
27
+ }
28
+
21
29
type SmeshingIdentitiesService struct {
22
30
states identityState
23
31
poetClients []activation.PoetService
@@ -59,31 +67,31 @@ func (s *SmeshingIdentitiesService) States(
59
67
return nil , status .Error (codes .InvalidArgument , "limit must be set to <= 100" )
60
68
}
61
69
62
- ops := toEventOperations (request )
63
-
64
- pbIdentities := make (map [string ]* pb.Identity , request .Limit )
65
- for nodeId , history := range s .states .All (ops ) {
66
- pbIdentities [nodeId .String ()] = & pb.Identity {
67
- History : []* pb.IdentityStateInfo {},
68
- }
69
-
70
- for i := len (history ) - 1 ; i >= 0 ; i -- {
71
- info := history [i ]
70
+ ops , err := toEventOperations (request )
71
+ if err != nil {
72
+ return nil , status .Error (codes .InvalidArgument , err .Error ())
73
+ }
72
74
73
- identityStateInfo := info .State .APIStateInfo ()
74
- identityStateInfo .Time = timestamppb .New (info .Time )
75
+ var states []* pb.IdentityStateInfo
76
+ events , err := s .states .All (ops )
77
+ if err != nil {
78
+ return nil , status .Error (codes .Internal , err .Error ())
79
+ }
80
+ for _ , info := range events {
81
+ identityStateInfo := info .State .APIStateInfo ()
82
+ identityStateInfo .Time = timestamppb .New (info .Time )
83
+ identityStateInfo .Smesher = info .ID .Bytes ()
75
84
76
- pbIdentities [nodeId .String ()].History = append (pbIdentities [nodeId .String ()].History , identityStateInfo )
77
- }
85
+ states = append (states , identityStateInfo )
78
86
}
79
87
80
- return & pb.IdentityStatesResponse {Identities : pbIdentities }, nil
88
+ return & pb.IdentityStatesResponse {States : states }, nil
81
89
}
82
90
83
- func toEventOperations (filter * pb.IdentityStatesRequest ) builder.Operations {
91
+ func toEventOperations (filter * pb.IdentityStatesRequest ) ( builder.Operations , error ) {
84
92
ops := builder.Operations {}
85
93
if filter == nil {
86
- return ops
94
+ return ops , nil
87
95
}
88
96
89
97
if len (filter .States ) > 0 {
@@ -98,21 +106,58 @@ func toEventOperations(filter *pb.IdentityStatesRequest) builder.Operations {
98
106
Value : states ,
99
107
})
100
108
}
109
+ if len (filter .Smeshers ) > 0 {
110
+ ops .Filter = append (ops .Filter , builder.Op {
111
+ Field : "id" ,
112
+ Token : builder .In ,
113
+ Value : filter .Smeshers ,
114
+ })
115
+ }
101
116
102
117
if filter .Limit != 0 {
103
118
ops .Modifiers = append (ops .Modifiers , builder.Modifier {
104
119
Key : builder .Limit ,
105
120
Value : int64 (filter .Limit ),
106
121
})
107
122
}
108
- if filter .Offset != 0 {
123
+
124
+ if filter .From != nil {
125
+ if err := filter .From .CheckValid (); err != nil {
126
+ return ops , fmt .Errorf ("'from' is invalid: %w" , err )
127
+ }
128
+ ops .Filter = append (ops .Filter , builder.Op {
129
+ Field : "timestamp" ,
130
+ Token : builder .Gte ,
131
+ Value : filter .From .AsTime ().UnixMicro (),
132
+ })
133
+ }
134
+ if filter .To != nil {
135
+ if err := filter .To .CheckValid (); err != nil {
136
+ return ops , fmt .Errorf ("'to' is invalid: %w" , err )
137
+ }
138
+ ops .Filter = append (ops .Filter , builder.Op {
139
+ Field : "timestamp" ,
140
+ Token : builder .Lt ,
141
+ Value : filter .To .AsTime ().UnixMicro (),
142
+ })
143
+ }
144
+
145
+ switch filter .Order {
146
+ case pb .SortOrder_ASC :
147
+ ops .Modifiers = append (ops .Modifiers , builder.Modifier {
148
+ Key : builder .OrderBy ,
149
+ Value : "timestamp asc" ,
150
+ })
151
+ case pb .SortOrder_DESC :
109
152
ops .Modifiers = append (ops .Modifiers , builder.Modifier {
110
- Key : builder .Offset ,
111
- Value : int64 ( filter . Offset ) ,
153
+ Key : builder .OrderBy ,
154
+ Value : "timestamp desc" ,
112
155
})
156
+ default :
157
+ return ops , fmt .Errorf ("unknown sort order: %d" , filter .Order )
113
158
}
114
159
115
- return ops
160
+ return ops , nil
116
161
}
117
162
118
163
func (s * SmeshingIdentitiesService ) PoetInfo (
0 commit comments