Skip to content

Commit cdad4b6

Browse files
committed
refactor: dedupe when fetching address ens + contract info
1 parent 22c9032 commit cdad4b6

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

backend/pkg/api/data_access/vdb_helpers.go

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package dataaccess
33
import (
44
"context"
55
"fmt"
6+
"maps"
67
"math/big"
8+
"slices"
79
"time"
810

911
"github.com/doug-martin/goqu/v9"
@@ -705,62 +707,65 @@ func getViewAndDateColumn(aggregation enums.ChartAggregation) (string, string, e
705707
return view, dateColumn, nil
706708
}
707709

710+
func interactionRequestAsKey(req db.ContractInteractionAtRequest) string {
711+
return fmt.Sprintf("%s:%d:%d:%d", req.Address, req.Block, req.TxIdx, req.TraceIdx)
712+
}
713+
708714
// getElInfo is a generic function to get EL info for addresses and their contract status.
709715
// Returns a map of address + block + tx_index + itx_index to address info.
710716
// Use the func `getElInfoKey` to get the key for the map.
711717
func getElInfo[In any](ctx context.Context, d *DataAccessService, input []In, buildReqs func(In) []db.ContractInteractionAtRequest) (map[string]t.Address, error) {
712718
// build reqs and address mapping
713-
interactionsRequests := make([]db.ContractInteractionAtRequest, 0, len(input))
719+
requestMap := make(map[string]db.ContractInteractionAtRequest) // map for deduping
714720
addressMapping := make(map[string]*t.Address)
715721
for _, req := range input {
716-
reqs := buildReqs(req)
717-
interactionsRequests = append(interactionsRequests, reqs...)
718-
for _, interaction := range reqs {
719-
addressMapping["0x"+interaction.Address] = nil
722+
for _, req := range buildReqs(req) {
723+
requestMap[interactionRequestAsKey(req)] = req
724+
addressMapping["0x"+req.Address] = nil
720725
}
721726
}
722727

723728
elInfo := make(map[string]t.Address)
724-
if len(interactionsRequests) == 0 {
729+
if len(requestMap) == 0 {
725730
return elInfo, nil
726731
}
727732

728733
// fetch info
729734
wg := errgroup.Group{}
735+
736+
interactionRequests := slices.Collect(maps.Values(requestMap))
737+
contractStatuses := make([]types.ContractInteractionType, len(interactionRequests)) // status of interactionRequests[i] will be stored in contractStatuses[i]
730738
wg.Go(func() error {
731-
err := d.GetNamesAndEnsForAddresses(ctx, addressMapping)
739+
var err error
740+
contractStatuses, err = d.bigtable.GetAddressContractInteractionsAt(interactionRequests)
732741
if err != nil {
733-
return fmt.Errorf("failed to get names and ens for addresses: %w", err)
742+
return fmt.Errorf("failed to get contract interactions: %w", err)
734743
}
735744
return nil
736745
})
737-
contractStatuses := make([]types.ContractInteractionType, len(interactionsRequests))
746+
738747
wg.Go(func() error {
739-
var err error
740-
contractStatuses, err = d.bigtable.GetAddressContractInteractionsAt(interactionsRequests)
748+
err := d.GetNamesAndEnsForAddresses(ctx, addressMapping)
741749
if err != nil {
742-
return fmt.Errorf("failed to get contract interactions: %w", err)
750+
return fmt.Errorf("failed to get names and ens for addresses: %w", err)
743751
}
744752
return nil
745753
})
754+
746755
if err := wg.Wait(); err != nil {
747756
return nil, err
748757
}
749758

750759
// build result
751-
for i, req := range interactionsRequests {
760+
for i, req := range interactionRequests {
752761
address := req.Address
753762

754-
key := fmt.Sprintf("%s:%d:%d:%d", address, req.Block, req.TxIdx, req.TraceIdx)
755-
if _, ok := elInfo[key]; ok {
756-
continue
757-
}
758763
addressInfo, ok := addressMapping["0x"+address]
759764
if addressInfo == nil || !ok {
760765
return nil, fmt.Errorf("address %s not found in address mapping", address)
761766
}
762767
addressInfo.IsContract = contractStatuses[i] == types.CONTRACT_CREATION || contractStatuses[i] == types.CONTRACT_PRESENT
763-
elInfo[key] = *addressInfo
768+
elInfo[interactionRequestAsKey(req)] = *addressInfo
764769
}
765770

766771
return elInfo, nil

0 commit comments

Comments
 (0)