Skip to content

Commit

Permalink
Merge pull request #583 from niklr/579
Browse files Browse the repository at this point in the history
fix: let fromHeight of 0 mean the latest block instead of the genesis…
  • Loading branch information
viteshan authored May 6, 2022
2 parents 00fd800 + 41b0df6 commit 0b27915
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
5 changes: 4 additions & 1 deletion rpcapi/api/filters/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
37 changes: 32 additions & 5 deletions rpcapi/api/ledger_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
Expand All @@ -448,8 +463,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
Expand All @@ -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
}
}
Expand All @@ -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
Expand Down

0 comments on commit 0b27915

Please sign in to comment.