From 8cfcb41e57401193a766565f56d2a1c3e318aa62 Mon Sep 17 00:00:00 2001
From: Sina Mahmoodi <1591639+s1na@users.noreply.github.com>
Date: Wed, 31 Aug 2022 16:14:53 +0200
Subject: [PATCH] graphql: return correct logs for tx (#25612)

* graphql: fix tx logs

* minor

* Use optimized search for selecting tx logs
---
 graphql/graphql.go | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/graphql/graphql.go b/graphql/graphql.go
index 97b460c20..66c25841d 100644
--- a/graphql/graphql.go
+++ b/graphql/graphql.go
@@ -22,6 +22,7 @@ import (
 	"errors"
 	"fmt"
 	"math/big"
+	"sort"
 	"strconv"
 
 	"github.com/ethereum/go-ethereum"
@@ -478,13 +479,16 @@ func (t *Transaction) getLogs(ctx context.Context) (*[]*Log, error) {
 	if err != nil {
 		return nil, err
 	}
-	ret := make([]*Log, 0, len(logs))
-	for _, log := range logs {
+	var ret []*Log
+	// Select tx logs from all block logs
+	ix := sort.Search(len(logs), func(i int) bool { return uint64(logs[i].TxIndex) == t.index })
+	for ix < len(logs) && uint64(logs[ix].TxIndex) == t.index {
 		ret = append(ret, &Log{
 			r:           t.r,
 			transaction: t,
-			log:         log,
+			log:         logs[ix],
 		})
+		ix++
 	}
 	return &ret, nil
 }