Skip to content

Commit 331b9e1

Browse files
authored
support from, to and order in IdentitityStates API (#6718)
* support from, to and order in IdentitityStates API * bump 'api' and rename 'Id' to 'Smesher' * add filtering by smesher
1 parent 3f19d0d commit 331b9e1

File tree

10 files changed

+358
-201
lines changed

10 files changed

+358
-201
lines changed

api/grpcserver/v2beta1/interface.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,10 @@ import (
44
"context"
55

66
"github.com/spacemeshos/go-spacemesh/common/types"
7-
"github.com/spacemeshos/go-spacemesh/identity"
8-
"github.com/spacemeshos/go-spacemesh/sql/builder"
97
)
108

119
//go:generate mockgen -typed -package=v2beta1 -destination=./mocks.go -source=./interface.go
1210

1311
type malfeasanceInfo interface {
1412
Info(ctx context.Context, nodeID types.NodeID) (map[string]string, error)
1513
}
16-
17-
type identityState interface {
18-
All(ops builder.Operations) map[types.NodeID][]identity.StateInfo
19-
AllProposals() map[types.NodeID][]*types.Proposal
20-
AllEligibilities() map[types.NodeID]map[types.LayerID][]types.VotingEligibility
21-
}

api/grpcserver/v2beta1/mocks.go

Lines changed: 0 additions & 140 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/grpcserver/v2beta1/smeshing_identities.go

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package v2beta1
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
78
pb "github.com/spacemeshos/api/release/go/spacemesh/v2beta1"
@@ -13,11 +14,18 @@ import (
1314

1415
"github.com/spacemeshos/go-spacemesh/activation"
1516
"github.com/spacemeshos/go-spacemesh/common/types"
17+
identity "github.com/spacemeshos/go-spacemesh/identity"
1618
"github.com/spacemeshos/go-spacemesh/sql/builder"
1719
)
1820

1921
const SmeshingIdentities = "smeshing_identities_v2beta1"
2022

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+
2129
type SmeshingIdentitiesService struct {
2230
states identityState
2331
poetClients []activation.PoetService
@@ -59,31 +67,31 @@ func (s *SmeshingIdentitiesService) States(
5967
return nil, status.Error(codes.InvalidArgument, "limit must be set to <= 100")
6068
}
6169

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+
}
7274

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()
7584

76-
pbIdentities[nodeId.String()].History = append(pbIdentities[nodeId.String()].History, identityStateInfo)
77-
}
85+
states = append(states, identityStateInfo)
7886
}
7987

80-
return &pb.IdentityStatesResponse{Identities: pbIdentities}, nil
88+
return &pb.IdentityStatesResponse{States: states}, nil
8189
}
8290

83-
func toEventOperations(filter *pb.IdentityStatesRequest) builder.Operations {
91+
func toEventOperations(filter *pb.IdentityStatesRequest) (builder.Operations, error) {
8492
ops := builder.Operations{}
8593
if filter == nil {
86-
return ops
94+
return ops, nil
8795
}
8896

8997
if len(filter.States) > 0 {
@@ -98,21 +106,58 @@ func toEventOperations(filter *pb.IdentityStatesRequest) builder.Operations {
98106
Value: states,
99107
})
100108
}
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+
}
101116

102117
if filter.Limit != 0 {
103118
ops.Modifiers = append(ops.Modifiers, builder.Modifier{
104119
Key: builder.Limit,
105120
Value: int64(filter.Limit),
106121
})
107122
}
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:
109152
ops.Modifiers = append(ops.Modifiers, builder.Modifier{
110-
Key: builder.Offset,
111-
Value: int64(filter.Offset),
153+
Key: builder.OrderBy,
154+
Value: "timestamp desc",
112155
})
156+
default:
157+
return ops, fmt.Errorf("unknown sort order: %d", filter.Order)
113158
}
114159

115-
return ops
160+
return ops, nil
116161
}
117162

118163
func (s *SmeshingIdentitiesService) PoetInfo(

0 commit comments

Comments
 (0)