Skip to content

Commit

Permalink
Update sanity check for API range queries (#1203)
Browse files Browse the repository at this point in the history
  • Loading branch information
lizhefeng authored and zjshen14 committed May 24, 2019
1 parent 84b4a96 commit 8d0f2ed
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
24 changes: 18 additions & 6 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,14 +600,17 @@ func (api *Server) readState(ctx context.Context, in *iotexapi.ReadStateRequest)
// GetActions returns actions within the range
// This is a workaround for the slow access issue if the start index is very big
func (api *Server) getActions(start uint64, count uint64) (*iotexapi.GetActionsResponse, error) {
if count == 0 || count > api.cfg.RangeQueryLimit {
if count > api.cfg.RangeQueryLimit {
return nil, status.Error(codes.InvalidArgument, "range exceeds the limit")
}

totalActions, err := api.bc.GetTotalActions()
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
if totalActions == uint64(0) {
return &iotexapi.GetActionsResponse{}, nil
}
if start >= totalActions {
return nil, status.Error(codes.InvalidArgument, "start exceeds the limit")
}
Expand Down Expand Up @@ -661,14 +664,17 @@ func (api *Server) getSingleAction(actionHash string, checkPending bool) (*iotex

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

actions, err := api.getTotalActionsByAddress(address)
if err != nil {
return nil, status.Error(codes.NotFound, err.Error())
}
if len(actions) == 0 {
return &iotexapi.GetActionsResponse{}, nil
}
if start >= uint64(len(actions)) {
return nil, status.Error(codes.InvalidArgument, "start exceeds the limit")
}
Expand All @@ -693,11 +699,14 @@ func (api *Server) getActionsByAddress(address string, start uint64, count uint6

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

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

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

Expand All @@ -730,20 +739,23 @@ func (api *Server) getActionsByBlock(blkHash string, start uint64, count uint64)
if err != nil {
return nil, status.Error(codes.NotFound, err.Error())
}
if len(blk.Actions) == 0 {
return &iotexapi.GetActionsResponse{}, nil
}
if start >= uint64(len(blk.Actions)) {
return nil, status.Error(codes.InvalidArgument, "start exceeds the limit")
}

res := api.actionsInBlock(blk, start, count)
return &iotexapi.GetActionsResponse{
Total: uint64(len(res)),
Total: uint64(len(blk.Actions)),
ActionInfo: res,
}, nil
}

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

Expand Down
40 changes: 37 additions & 3 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ var (
5,
4,
},
{
1,
0,
0,
},
}

getActionTests = []struct {
Expand Down Expand Up @@ -198,6 +203,12 @@ var (
8,
8,
},
{
ta.Addrinfo["foxtrot"].String(),
2,
1,
0,
},
}

getUnconfirmedActionsByAddressTests = []struct {
Expand All @@ -212,6 +223,12 @@ var (
4,
4,
},
{
ta.Addrinfo["producer"].String(),
2,
0,
0,
},
}

getActionsByBlockTests = []struct {
Expand All @@ -232,6 +249,12 @@ var (
5,
5,
},
{
1,
0,
0,
0,
},
}

getBlockMetasTests = []struct {
Expand All @@ -249,6 +272,11 @@ var (
5,
3,
},
{
1,
0,
0,
},
}

getBlockMetaTests = []struct {
Expand Down Expand Up @@ -691,7 +719,9 @@ func TestServer_GetActionsByAddress(t *testing.T) {
res, err := svr.GetActions(context.Background(), request)
require.NoError(err)
require.Equal(test.numActions, len(res.ActionInfo))
require.Equal(test.address, res.ActionInfo[0].Sender)
if test.numActions > 0 {
require.Equal(test.address, res.ActionInfo[0].Sender)
}
}
}

Expand All @@ -715,7 +745,9 @@ func TestServer_GetUnconfirmedActionsByAddress(t *testing.T) {
res, err := svr.GetActions(context.Background(), request)
require.NoError(err)
require.Equal(test.numActions, len(res.ActionInfo))
require.Equal(test.address, res.ActionInfo[0].Sender)
if test.numActions > 0 {
require.Equal(test.address, res.ActionInfo[0].Sender)
}
}
}

Expand All @@ -741,8 +773,10 @@ func TestServer_GetActionsByBlock(t *testing.T) {
}
res, err := svr.GetActions(context.Background(), request)
require.NoError(err)
require.Equal(test.blkHeight, res.ActionInfo[0].BlkHeight)
require.Equal(test.numActions, len(res.ActionInfo))
if test.numActions > 0 {
require.Equal(test.blkHeight, res.ActionInfo[0].BlkHeight)
}
}
}

Expand Down

0 comments on commit 8d0f2ed

Please sign in to comment.