Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: logsByTx array does not match transaction array #99

Merged
merged 2 commits into from
Aug 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions ethreceipts/ethreceipts.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,13 +647,18 @@ func (l *ReceiptsListener) processBlocks(blocks ethmonitor.Blocks, subscribers [
reorged := block.Event == ethmonitor.Removed

receipts := make([]Receipt, len(block.Transactions()))
logs := groupLogsByTransaction(block.Logs, len(block.Transactions()))
logs := groupLogsByTransaction(block.Logs)

for i, txn := range block.Transactions() {
txnLog, ok := logs[txn.Hash().Hex()]
if !ok {
txnLog = []*types.Log{}
}

receipts[i] = Receipt{
Reorged: reorged,
Final: l.isBlockFinal(block.Number()),
logs: logs[i],
logs: txnLog,
transaction: txn,
}
txnMsg, err := ethtxn.AsMessage(txn)
Expand Down Expand Up @@ -821,19 +826,19 @@ func collectOk[T any](in []T, oks []bool, okCond bool) []T {
// return txnLogs
// }

func groupLogsByTransaction(logs []types.Log, numTxns int) [][]*types.Log {
if numTxns == 0 {
return [][]*types.Log{}
}
out := make([][]*types.Log, blockLogsCount(numTxns, logs))
func groupLogsByTransaction(logs []types.Log) map[string][]*types.Log {
var out = make(map[string][]*types.Log)
for _, log := range logs {
log := log
out[log.TxIndex] = append(out[log.TxIndex], &log)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, I do remember writing this function long ago -- and my original implementation was similar to what you have now, but then changed it to place in the TxnIndex ... I can't remember exactly why, but it might have been that some nodes gave these to us in the wrong order or something..? I can't recall, but there was a reason. We can just look out for stuff in future around here if anything with receipt listener is acting up

}
for i, logs := range out {
if logs == nil {
out[i] = []*types.Log{}

logTxHash := log.TxHash.Hex()
outLogs, ok := out[logTxHash]
if !ok {
outLogs = []*types.Log{}
}
marino39 marked this conversation as resolved.
Show resolved Hide resolved

outLogs = append(outLogs, &log)
out[logTxHash] = outLogs
}
return out
}
Expand Down
Loading