Skip to content

Commit d7fc073

Browse files
committed
rpc: optimize logs sorting but keep it commented
1 parent d7ce655 commit d7fc073

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

db/version/app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var (
3131
const (
3232
Major = 3 // Major version component of the current release
3333
Minor = 3 // Minor version component of the current release
34-
Micro = 1 // Micro version component of the current release
34+
Micro = 2 // Micro version component of the current release
3535
Modifier = "" // Modifier component of the current release
3636
DefaultSnapshotGitBranch = "release/3.2" // Branch of erigontech/erigon-snapshot to use in OtterSync
3737
VersionKeyCreated = "ErigonVersionCreated"

rpc/jsonrpc/eth_receipts.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

rpc/requests/block.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ func (b *Block) UnmarshalJSON(input []byte) error {
107107
b.Transactions = bd.Transactions
108108

109109
if bd.Transactions != nil {
110-
b.TransactionHashes = make([]common.Hash, len(b.Transactions))
111-
for _, t := range bd.Transactions {
112-
b.TransactionHashes = append(b.TransactionHashes, t.Hash)
110+
b.TransactionHashes = make([]common.Hash, len(bd.Transactions))
111+
for i, t := range bd.Transactions {
112+
b.TransactionHashes[i] = t.Hash
113113
}
114114
}
115115

0 commit comments

Comments
 (0)