From f8bce2a6dca5faaf1039c759d9ff4e2c8c88c50b Mon Sep 17 00:00:00 2001 From: nikl1r Date: Thu, 31 Mar 2022 00:11:51 -0700 Subject: [PATCH 1/2] fix: let fromHeight of 0 mean the latest block instead of the genesis block --- rpcapi/api/ledger_v2.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rpcapi/api/ledger_v2.go b/rpcapi/api/ledger_v2.go index d7c5c66cc..2f9206541 100644 --- a/rpcapi/api/ledger_v2.go +++ b/rpcapi/api/ledger_v2.go @@ -448,8 +448,8 @@ func GetLogs(c chain.Chain, rangeMap map[string]*Range, topics [][]types.Hash) ( if acc == nil { continue } - if startHeight == 0 { - startHeight = 1 + if startHeight == 0 || startHeight > acc.Height { + startHeight = acc.Height } if endHeight == 0 || endHeight > acc.Height { endHeight = acc.Height From 41b0df6aec85d577c0743b157a9d73da185c0b9e Mon Sep 17 00:00:00 2001 From: niklr Date: Fri, 22 Apr 2022 15:23:43 +0000 Subject: [PATCH 2/2] feat: paginate ledger_getVmLogsByFilter --- rpcapi/api/filters/subscribe.go | 5 ++++- rpcapi/api/ledger_v2.go | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/rpcapi/api/filters/subscribe.go b/rpcapi/api/filters/subscribe.go index c6ce2c451..9beee4b87 100644 --- a/rpcapi/api/filters/subscribe.go +++ b/rpcapi/api/filters/subscribe.go @@ -75,6 +75,9 @@ func (s *SubscribeApi) timeoutLoop() { type RpcFilterParam struct { AddrRange map[string]*api.Range `json:"addrRange"` Topics [][]types.Hash `json:"topics"` + + PageIndex uint64 `json:"pageIndex"` + PageSize uint64 `json:"pageSize"` } type AccountBlock struct { @@ -720,7 +723,7 @@ func (s *SubscribeApi) createVmLogSubscription(ctx context.Context, rangeMap map // Deprecated: use ledger_getVmLogsByFilter instead func (s *SubscribeApi) GetLogs(param RpcFilterParam) ([]*Logs, error) { - logs, err := api.GetLogs(s.vite.Chain(), param.AddrRange, param.Topics) + logs, err := api.GetLogs(s.vite.Chain(), param.AddrRange, param.Topics, param.PageIndex, param.PageSize) if err != nil { return nil, err } diff --git a/rpcapi/api/ledger_v2.go b/rpcapi/api/ledger_v2.go index 2f9206541..65a54af65 100644 --- a/rpcapi/api/ledger_v2.go +++ b/rpcapi/api/ledger_v2.go @@ -362,6 +362,9 @@ func (l *LedgerApi) GetUnreceivedTransactionSummaryInBatch(addressList []types.A type VmLogFilterParam struct { AddrRange map[string]*Range `json:"addressHeightRange"` Topics [][]types.Hash `json:"topics"` + + PageIndex uint64 `json:"pageIndex"` + PageSize uint64 `json:"pageSize"` } type Range struct { FromHeight string `json:"fromHeight"` @@ -430,14 +433,26 @@ type Logs struct { } func (l *LedgerApi) GetVmLogsByFilter(param VmLogFilterParam) ([]*Logs, error) { - return GetLogs(l.chain, param.AddrRange, param.Topics) + return GetLogs(l.chain, param.AddrRange, param.Topics, param.PageIndex, param.PageSize) } -func GetLogs(c chain.Chain, rangeMap map[string]*Range, topics [][]types.Hash) ([]*Logs, error) { +func GetLogs(c chain.Chain, rangeMap map[string]*Range, topics [][]types.Hash, pageIndex uint64, pageSize uint64) ([]*Logs, error) { filterParam, err := ToFilterParam(rangeMap, topics) if err != nil { return nil, err } + + maxSize := uint64(1000) + if pageSize > maxSize { + return nil, fmt.Errorf("pageSize must be less than %d", maxSize) + } + if pageSize == 0 { + pageSize = maxSize + } + + skipCount := uint64(pageIndex * pageSize) + var logs []*Logs + var maxSizeReached bool for addr, hr := range filterParam.AddrRange { startHeight := hr.FromHeight endHeight := hr.ToHeight @@ -472,12 +487,23 @@ func GetLogs(c chain.Chain, rangeMap map[string]*Range, topics [][]types.Hash) ( } for _, l := range list { if FilterLog(filterParam, l) { + if skipCount > 0 { + skipCount-- + continue + } logs = append(logs, &Logs{l, blocks[i-1].Hash, Uint64ToString(blocks[i-1].Height), &addr}) + if uint64(len(logs)) >= pageSize { + maxSizeReached = true + break + } } } + if maxSizeReached { + break + } } } - if finish { + if finish || maxSizeReached { break } } @@ -492,6 +518,7 @@ func getHeightPage(start uint64, end uint64, count uint64) (uint64, uint64, bool } return start + count - 1, count, false } + func FilterLog(filter *FilterParam, l *ledger.VmLog) bool { if len(l.Topics) < len(filter.Topics) { return false