Skip to content

Commit

Permalink
Fix a bug in getActionsByAddress (#1568)
Browse files Browse the repository at this point in the history
  • Loading branch information
lizhefeng authored and Yutong Pei committed Oct 23, 2019
1 parent 47af592 commit 7d21bdb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
18 changes: 18 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,9 @@ func (api *Server) getActionsFromIndex(totalActions, start, count uint64) (*iote

// GetActions returns actions within the range
func (api *Server) getActions(start uint64, count uint64) (*iotexapi.GetActionsResponse, error) {
if count == 0 {
return nil, status.Error(codes.InvalidArgument, "count must be greater than zero")
}
if count > api.cfg.API.RangeQueryLimit {
return nil, status.Error(codes.InvalidArgument, "range exceeds the limit")
}
Expand Down Expand Up @@ -794,6 +797,9 @@ 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 {
return nil, status.Error(codes.InvalidArgument, "count must be greater than zero")
}
if count > api.cfg.API.RangeQueryLimit {
return nil, status.Error(codes.InvalidArgument, "range exceeds the limit")
}
Expand All @@ -819,6 +825,9 @@ 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 {
return nil, status.Error(codes.InvalidArgument, "count must be greater than zero")
}
if count > api.cfg.API.RangeQueryLimit {
return nil, status.Error(codes.InvalidArgument, "range exceeds the limit")
}
Expand Down Expand Up @@ -847,6 +856,9 @@ 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 {
return nil, status.Error(codes.InvalidArgument, "count must be greater than zero")
}
if count > api.cfg.API.RangeQueryLimit {
return nil, status.Error(codes.InvalidArgument, "range exceeds the limit")
}
Expand Down Expand Up @@ -875,6 +887,9 @@ func (api *Server) getActionsByBlock(blkHash string, start uint64, count uint64)

// getBlockMetas returns blockmetas response within the height range
func (api *Server) getBlockMetas(start uint64, count uint64) (*iotexapi.GetBlockMetasResponse, error) {
if count == 0 {
return nil, status.Error(codes.InvalidArgument, "count must be greater than zero")
}
if count > api.cfg.API.RangeQueryLimit {
return nil, status.Error(codes.InvalidArgument, "range exceeds the limit")
}
Expand Down Expand Up @@ -1155,6 +1170,9 @@ func (api *Server) reverseActionsInBlock(blk *block.Block, reverseStart, count u
}

func (api *Server) getLogsInBlock(filter *LogFilter, start, count uint64) ([]*iotextypes.Log, error) {
if count == 0 {
return nil, status.Error(codes.InvalidArgument, "count must be greater than zero")
}
// filter logs within start --> end
var logs []*iotextypes.Log
end := start + count - 1
Expand Down
36 changes: 18 additions & 18 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,12 +679,12 @@ func TestServer_GetActions(t *testing.T) {
},
}
res, err := svr.GetActions(context.Background(), request)
if test.count == 0 {
require.Error(err)
continue
}
require.NoError(err)
require.Equal(test.numActions, len(res.ActionInfo))
if test.numActions == 0 {
// returns empty response body in case of no result
require.Equal(&iotexapi.GetActionsResponse{}, res)
}
}
}

Expand Down Expand Up @@ -792,15 +792,13 @@ func TestServer_GetUnconfirmedActionsByAddress(t *testing.T) {
},
}
res, err := svr.GetActions(context.Background(), request)
if test.count == 0 {
require.Error(err)
continue
}
require.NoError(err)
require.Equal(test.numActions, len(res.ActionInfo))
if test.numActions > 0 {
require.Equal(test.address, res.ActionInfo[0].Sender)
} else {
// returns empty response body in case of no result
res.Total = 0
require.Equal(&iotexapi.GetActionsResponse{}, res)
}
require.Equal(test.address, res.ActionInfo[0].Sender)
}
}

Expand All @@ -825,15 +823,13 @@ func TestServer_GetActionsByBlock(t *testing.T) {
},
}
res, err := svr.GetActions(context.Background(), request)
if test.count == 0 {
require.Error(err)
continue
}
require.NoError(err)
require.Equal(test.numActions, len(res.ActionInfo))
if test.numActions > 0 {
require.Equal(test.blkHeight, res.ActionInfo[0].BlkHeight)
} else {
// returns empty response body in case of no result
res.Total = 0
require.Equal(&iotexapi.GetActionsResponse{}, res)
}
require.Equal(test.blkHeight, res.ActionInfo[0].BlkHeight)
}
}

Expand All @@ -854,6 +850,10 @@ func TestServer_GetBlockMetas(t *testing.T) {
},
}
res, err := svr.GetBlockMetas(context.Background(), request)
if test.count == 0 {
require.Error(err)
continue
}
require.NoError(err)
require.Equal(test.numBlks, len(res.BlkMetas))
var prevBlkPb *iotextypes.BlockMeta
Expand Down
3 changes: 3 additions & 0 deletions blockchain/blockdao.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,9 @@ func (dao *blockDAO) getActionsByAddress(addrBytes hash.Hash160, start, count ui
return nil, err
}
total := address.Size()
if start >= total {
return nil, errors.New("invalid start index")
}
if start+count > total {
count = total - start
}
Expand Down

0 comments on commit 7d21bdb

Please sign in to comment.