Skip to content

Commit 3d05e4b

Browse files
Merge pull request #144 from thirdweb-dev/vt-abicache-conc-writes
fix: concurrent writes to abi cache
2 parents 04b35e5 + dbed644 commit 3d05e4b

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
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/log.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ func DecodeLogs(chainId string, logs []Log) []*DecodedLog {
4444
decodedLogs := make([]*DecodedLog, len(logs))
4545
abiCache := make(map[string]*abi.ABI)
4646

47-
decodeLogFunc := func(eventLog *Log) *DecodedLog {
47+
decodeLogFunc := func(eventLog *Log, mut *sync.Mutex) *DecodedLog {
4848
decodedLog := DecodedLog{Log: *eventLog}
49-
abi := GetABIForContractWithCache(chainId, eventLog.Address, abiCache)
49+
abi := GetABIForContractWithCache(chainId, eventLog.Address, abiCache, mut)
5050
if abi == nil {
5151
return &decodedLog
5252
}
@@ -63,13 +63,14 @@ func DecodeLogs(chainId string, logs []Log) []*DecodedLog {
6363
}
6464

6565
var wg sync.WaitGroup
66+
var mut sync.Mutex
6667
for idx, eventLog := range logs {
6768
wg.Add(1)
68-
go func(idx int, eventLog Log) {
69+
go func(idx int, eventLog Log, mut *sync.Mutex) {
6970
defer wg.Done()
70-
decodedLog := decodeLogFunc(&eventLog)
71+
decodedLog := decodeLogFunc(&eventLog, mut)
7172
decodedLogs[idx] = decodedLog
72-
}(idx, eventLog)
73+
}(idx, eventLog, &mut)
7374
}
7475
wg.Wait()
7576
return decodedLogs

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)