@@ -20,7 +20,6 @@ import (
2020 "context"
2121 "errors"
2222 "fmt"
23- "sort"
2423
2524 "github.com/RoaringBitmap/roaring/v2"
2625
@@ -166,8 +165,9 @@ func (api *APIImpl) GetLogs(ctx context.Context, crit filters.FilterCriteria) (t
166165 }
167166
168167 // For a whole block query (blockHash set, no address/topics filters),
169- // normalize logIndex to be continuous 0..N-1 in canonical order.
170- if isWholeBlockUnfiltered (crit ) && len (rpcLogs ) > 0 {
168+ // normalize logIndex (if required) to be continuous 0..N-1 in canonical order.
169+ /* TODO deactivated for memory spike, although already optimizated with `needsRenumber` addition
170+ if isWholeBlockUnfiltered(crit) && len(rpcLogs) > 0 && needsRenumber(rpcLogs) {
171171 sort.Slice(rpcLogs, func(i, j int) bool {
172172 if rpcLogs[i].BlockNumber == rpcLogs[j].BlockNumber {
173173 if rpcLogs[i].TxIndex == rpcLogs[j].TxIndex {
@@ -178,7 +178,6 @@ func (api *APIImpl) GetLogs(ctx context.Context, crit filters.FilterCriteria) (t
178178 return rpcLogs[i].BlockNumber < rpcLogs[j].BlockNumber
179179 })
180180
181- // Now renumber per block so logIndex is 0..N-1 with no gaps.
182181 currentBlock := rpcLogs[0].BlockHash
183182 var idx uint
184183 idx = 0
@@ -191,6 +190,7 @@ func (api *APIImpl) GetLogs(ctx context.Context, crit filters.FilterCriteria) (t
191190 idx++
192191 }
193192 }
193+ */
194194
195195 return rpcLogs , nil
196196}
@@ -645,3 +645,25 @@ func isWholeBlockUnfiltered(crit filters.FilterCriteria) bool {
645645 }
646646 return true
647647}
648+
649+ // needsRenumber checks if the log indexes are continuous 0..N-1 per block.
650+ func needsRenumber (rpcLogs types.RPCLogs ) bool {
651+ if len (rpcLogs ) == 0 {
652+ return false
653+ }
654+ curBlock := rpcLogs [0 ].BlockHash
655+ var expected uint
656+ expected = 0
657+
658+ for _ , l := range rpcLogs {
659+ if l .BlockHash != curBlock {
660+ curBlock = l .BlockHash
661+ expected = 0
662+ }
663+ if l .Index != expected {
664+ return true
665+ }
666+ expected ++
667+ }
668+ return false
669+ }
0 commit comments