Skip to content

Commit 06ceafd

Browse files
author
=
committed
fix: concurrent writes to abi cache
1 parent 04b35e5 commit 06ceafd

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

internal/common/abi.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,26 @@ import (
55
"net/http"
66
"regexp"
77
"strings"
8+
"sync"
89

910
config "github.com/thirdweb-dev/indexer/configs"
1011

1112
"github.com/ethereum/go-ethereum/accounts/abi"
1213
)
1314

14-
func GetABIForContractWithCache(chainId string, contract string, abiCache map[string]*abi.ABI) *abi.ABI {
15+
func GetABIForContractWithCache(chainId string, contract string, abiCache map[string]*abi.ABI, mut *sync.Mutex) *abi.ABI {
1516
abi, ok := abiCache[contract]
1617
if !ok {
1718
abiResult, err := GetABIForContract(chainId, contract)
1819
if err != nil {
20+
mut.Lock()
1921
abiCache[contract] = nil
22+
mut.Unlock()
2023
return nil
2124
} else {
25+
mut.Lock()
2226
abiCache[contract] = abiResult
27+
mut.Unlock()
2328
abi = abiResult
2429
}
2530
}

internal/common/transaction.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ type DecodedTransaction struct {
5656
func DecodeTransactions(chainId string, txs []Transaction) []*DecodedTransaction {
5757
decodedTxs := make([]*DecodedTransaction, len(txs))
5858
abiCache := make(map[string]*abi.ABI)
59-
decodeTxFunc := func(transaction *Transaction) *DecodedTransaction {
59+
decodeTxFunc := func(transaction *Transaction, mut *sync.Mutex) *DecodedTransaction {
6060
decodedTransaction := DecodedTransaction{Transaction: *transaction}
61-
abi := GetABIForContractWithCache(chainId, transaction.ToAddress, abiCache)
61+
abi := GetABIForContractWithCache(chainId, transaction.ToAddress, abiCache, mut)
6262
if abi == nil {
6363
return &decodedTransaction
6464
}
@@ -84,13 +84,14 @@ func DecodeTransactions(chainId string, txs []Transaction) []*DecodedTransaction
8484
}
8585

8686
var wg sync.WaitGroup
87+
mut := &sync.Mutex{}
8788
for idx, transaction := range txs {
8889
wg.Add(1)
89-
go func(idx int, transaction Transaction) {
90+
go func(idx int, transaction Transaction, mut *sync.Mutex) {
9091
defer wg.Done()
91-
decodedTx := decodeTxFunc(&transaction)
92+
decodedTx := decodeTxFunc(&transaction, mut)
9293
decodedTxs[idx] = decodedTx
93-
}(idx, transaction)
94+
}(idx, transaction, mut)
9495
}
9596
wg.Wait()
9697
return decodedTxs

0 commit comments

Comments
 (0)