@@ -164,6 +164,34 @@ func (api *APIImpl) GetLogs(ctx context.Context, crit filters.FilterCriteria) (t
164164 }
165165 }
166166
167+ // For a whole block query (blockHash set, no address/topics filters),
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) {
171+ sort.Slice(rpcLogs, func(i, j int) bool {
172+ if rpcLogs[i].BlockNumber == rpcLogs[j].BlockNumber {
173+ if rpcLogs[i].TxIndex == rpcLogs[j].TxIndex {
174+ return rpcLogs[i].Index < rpcLogs[j].Index
175+ }
176+ return rpcLogs[i].TxIndex < rpcLogs[j].TxIndex
177+ }
178+ return rpcLogs[i].BlockNumber < rpcLogs[j].BlockNumber
179+ })
180+
181+ currentBlock := rpcLogs[0].BlockHash
182+ var idx uint
183+ idx = 0
184+ for _, l := range rpcLogs {
185+ if l.BlockHash != currentBlock {
186+ currentBlock = l.BlockHash
187+ idx = 0
188+ }
189+ l.Index = idx
190+ idx++
191+ }
192+ }
193+ */
194+
167195 return rpcLogs , nil
168196}
169197
@@ -617,3 +645,25 @@ func isWholeBlockUnfiltered(crit filters.FilterCriteria) bool {
617645 }
618646 return true
619647}
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