Skip to content

Commit 8d0f2ed

Browse files
lizhefengzjshen14
authored andcommitted
Update sanity check for API range queries (#1203)
1 parent 84b4a96 commit 8d0f2ed

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

api/api.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -600,14 +600,17 @@ func (api *Server) readState(ctx context.Context, in *iotexapi.ReadStateRequest)
600600
// GetActions returns actions within the range
601601
// This is a workaround for the slow access issue if the start index is very big
602602
func (api *Server) getActions(start uint64, count uint64) (*iotexapi.GetActionsResponse, error) {
603-
if count == 0 || count > api.cfg.RangeQueryLimit {
603+
if count > api.cfg.RangeQueryLimit {
604604
return nil, status.Error(codes.InvalidArgument, "range exceeds the limit")
605605
}
606606

607607
totalActions, err := api.bc.GetTotalActions()
608608
if err != nil {
609609
return nil, status.Error(codes.Internal, err.Error())
610610
}
611+
if totalActions == uint64(0) {
612+
return &iotexapi.GetActionsResponse{}, nil
613+
}
611614
if start >= totalActions {
612615
return nil, status.Error(codes.InvalidArgument, "start exceeds the limit")
613616
}
@@ -661,14 +664,17 @@ func (api *Server) getSingleAction(actionHash string, checkPending bool) (*iotex
661664

662665
// getActionsByAddress returns all actions associated with an address
663666
func (api *Server) getActionsByAddress(address string, start uint64, count uint64) (*iotexapi.GetActionsResponse, error) {
664-
if count == 0 || count > api.cfg.RangeQueryLimit {
667+
if count > api.cfg.RangeQueryLimit {
665668
return nil, status.Error(codes.InvalidArgument, "range exceeds the limit")
666669
}
667670

668671
actions, err := api.getTotalActionsByAddress(address)
669672
if err != nil {
670673
return nil, status.Error(codes.NotFound, err.Error())
671674
}
675+
if len(actions) == 0 {
676+
return &iotexapi.GetActionsResponse{}, nil
677+
}
672678
if start >= uint64(len(actions)) {
673679
return nil, status.Error(codes.InvalidArgument, "start exceeds the limit")
674680
}
@@ -693,11 +699,14 @@ func (api *Server) getActionsByAddress(address string, start uint64, count uint6
693699

694700
// getUnconfirmedActionsByAddress returns all unconfirmed actions in actpool associated with an address
695701
func (api *Server) getUnconfirmedActionsByAddress(address string, start uint64, count uint64) (*iotexapi.GetActionsResponse, error) {
696-
if count == 0 || count > api.cfg.RangeQueryLimit {
702+
if count > api.cfg.RangeQueryLimit {
697703
return nil, status.Error(codes.InvalidArgument, "range exceeds the limit")
698704
}
699705

700706
selps := api.ap.GetUnconfirmedActs(address)
707+
if len(selps) == 0 {
708+
return &iotexapi.GetActionsResponse{}, nil
709+
}
701710
if start >= uint64(len(selps)) {
702711
return nil, status.Error(codes.InvalidArgument, "start exceeds the limit")
703712
}
@@ -718,7 +727,7 @@ func (api *Server) getUnconfirmedActionsByAddress(address string, start uint64,
718727

719728
// getActionsByBlock returns all actions in a block
720729
func (api *Server) getActionsByBlock(blkHash string, start uint64, count uint64) (*iotexapi.GetActionsResponse, error) {
721-
if count == 0 || count > api.cfg.RangeQueryLimit {
730+
if count > api.cfg.RangeQueryLimit {
722731
return nil, status.Error(codes.InvalidArgument, "range exceeds the limit")
723732
}
724733

@@ -730,20 +739,23 @@ func (api *Server) getActionsByBlock(blkHash string, start uint64, count uint64)
730739
if err != nil {
731740
return nil, status.Error(codes.NotFound, err.Error())
732741
}
742+
if len(blk.Actions) == 0 {
743+
return &iotexapi.GetActionsResponse{}, nil
744+
}
733745
if start >= uint64(len(blk.Actions)) {
734746
return nil, status.Error(codes.InvalidArgument, "start exceeds the limit")
735747
}
736748

737749
res := api.actionsInBlock(blk, start, count)
738750
return &iotexapi.GetActionsResponse{
739-
Total: uint64(len(res)),
751+
Total: uint64(len(blk.Actions)),
740752
ActionInfo: res,
741753
}, nil
742754
}
743755

744756
// getBlockMetas gets block within the height range
745757
func (api *Server) getBlockMetas(start uint64, count uint64) (*iotexapi.GetBlockMetasResponse, error) {
746-
if count == 0 || count > api.cfg.RangeQueryLimit {
758+
if count > api.cfg.RangeQueryLimit {
747759
return nil, status.Error(codes.InvalidArgument, "range exceeds the limit")
748760
}
749761

api/api_test.go

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ var (
146146
5,
147147
4,
148148
},
149+
{
150+
1,
151+
0,
152+
0,
153+
},
149154
}
150155

151156
getActionTests = []struct {
@@ -198,6 +203,12 @@ var (
198203
8,
199204
8,
200205
},
206+
{
207+
ta.Addrinfo["foxtrot"].String(),
208+
2,
209+
1,
210+
0,
211+
},
201212
}
202213

203214
getUnconfirmedActionsByAddressTests = []struct {
@@ -212,6 +223,12 @@ var (
212223
4,
213224
4,
214225
},
226+
{
227+
ta.Addrinfo["producer"].String(),
228+
2,
229+
0,
230+
0,
231+
},
215232
}
216233

217234
getActionsByBlockTests = []struct {
@@ -232,6 +249,12 @@ var (
232249
5,
233250
5,
234251
},
252+
{
253+
1,
254+
0,
255+
0,
256+
0,
257+
},
235258
}
236259

237260
getBlockMetasTests = []struct {
@@ -249,6 +272,11 @@ var (
249272
5,
250273
3,
251274
},
275+
{
276+
1,
277+
0,
278+
0,
279+
},
252280
}
253281

254282
getBlockMetaTests = []struct {
@@ -691,7 +719,9 @@ func TestServer_GetActionsByAddress(t *testing.T) {
691719
res, err := svr.GetActions(context.Background(), request)
692720
require.NoError(err)
693721
require.Equal(test.numActions, len(res.ActionInfo))
694-
require.Equal(test.address, res.ActionInfo[0].Sender)
722+
if test.numActions > 0 {
723+
require.Equal(test.address, res.ActionInfo[0].Sender)
724+
}
695725
}
696726
}
697727

@@ -715,7 +745,9 @@ func TestServer_GetUnconfirmedActionsByAddress(t *testing.T) {
715745
res, err := svr.GetActions(context.Background(), request)
716746
require.NoError(err)
717747
require.Equal(test.numActions, len(res.ActionInfo))
718-
require.Equal(test.address, res.ActionInfo[0].Sender)
748+
if test.numActions > 0 {
749+
require.Equal(test.address, res.ActionInfo[0].Sender)
750+
}
719751
}
720752
}
721753

@@ -741,8 +773,10 @@ func TestServer_GetActionsByBlock(t *testing.T) {
741773
}
742774
res, err := svr.GetActions(context.Background(), request)
743775
require.NoError(err)
744-
require.Equal(test.blkHeight, res.ActionInfo[0].BlkHeight)
745776
require.Equal(test.numActions, len(res.ActionInfo))
777+
if test.numActions > 0 {
778+
require.Equal(test.blkHeight, res.ActionInfo[0].BlkHeight)
779+
}
746780
}
747781
}
748782

0 commit comments

Comments
 (0)