Skip to content

Commit 31210b2

Browse files
committed
add partial handling of event signature collision
1 parent a3f07b2 commit 31210b2

File tree

12 files changed

+168
-298
lines changed

12 files changed

+168
-298
lines changed

.githooks/detect-ethereum-keys

Lines changed: 0 additions & 18 deletions
This file was deleted.

.githooks/detect-rpc-urls

Lines changed: 0 additions & 17 deletions
This file was deleted.

.githooks/go-lint

Lines changed: 0 additions & 23 deletions
This file was deleted.

.githooks/go-mod-local-replace

Lines changed: 0 additions & 14 deletions
This file was deleted.

.githooks/go-mod-tidy

Lines changed: 0 additions & 23 deletions
This file was deleted.

.githooks/go-test-build

Lines changed: 0 additions & 12 deletions
This file was deleted.

.githooks/run-unit-tests

Lines changed: 0 additions & 5 deletions
This file was deleted.

.githooks/typos

Lines changed: 0 additions & 15 deletions
This file was deleted.

seth/client.go

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"math/big"
88
"net/http"
99
"path/filepath"
10+
"reflect"
1011
"strings"
1112
"time"
1213

@@ -1167,28 +1168,56 @@ func (t TransactionLog) GetData() []byte {
11671168
}
11681169

11691170
func (m *Client) decodeContractLogs(l zerolog.Logger, logs []types.Log, allABIs []*abi.ABI) ([]DecodedTransactionLog, error) {
1170-
l.Trace().Msg("Decoding events")
1171+
l.Trace().
1172+
Msg("Decoding events")
11711173
sigMap := buildEventSignatureMap(allABIs)
11721174

11731175
var eventsParsed []DecodedTransactionLog
11741176
for _, lo := range logs {
11751177
if len(lo.Topics) == 0 {
1176-
l.Debug().Msg("Log has no topics; skipping")
1178+
l.Debug().
1179+
Msg("Log has no topics; skipping")
11771180
continue
11781181
}
11791182

11801183
eventSig := lo.Topics[0].Hex()
11811184
possibleEvents, exists := sigMap[eventSig]
11821185
if !exists {
1183-
l.Trace().Str("Signature", eventSig).Msg("No matching events found for signature")
1186+
l.Trace().
1187+
Str("Event signature", eventSig).
1188+
Msg("No matching events found for signature")
11841189
continue
11851190
}
11861191

1192+
// Check if we know what contract is this log from and if we do, get its ABI to skip unnecessary iterations
1193+
var knownContractABI *abi.ABI
1194+
if contractName := m.ContractAddressToNameMap.GetContractName(lo.Address.Hex()); contractName != "" {
1195+
maybeABI, ok := m.ContractStore.GetABI(contractName)
1196+
if !ok {
1197+
l.Trace().
1198+
Str("Event signature", eventSig).
1199+
Str("Contract name", contractName).
1200+
Str("Contract address", lo.Address.Hex()).
1201+
Msg("No ABI found for known contract; this is unexpected. Continuing with step-by-step ABI search")
1202+
} else {
1203+
knownContractABI = maybeABI
1204+
}
1205+
}
1206+
11871207
// Iterate over possible events with the same signature
11881208
matched := false
11891209
for _, evWithABI := range possibleEvents {
11901210
evSpec := evWithABI.EventSpec
1191-
eventABI := evWithABI.EventABI
1211+
contractABI := evWithABI.ContractABI
1212+
1213+
// Check if known contract ABI matches candidate ABI and if not, skip this ABI and try the next one
1214+
if knownContractABI != nil && !reflect.DeepEqual(knownContractABI, contractABI) {
1215+
l.Trace().
1216+
Str("Event signature", eventSig).
1217+
Str("Contract address", lo.Address.Hex()).
1218+
Msg("ABI doesn't match known ABI for this address; trying next ABI")
1219+
continue
1220+
}
11921221

11931222
// Validate indexed parameters count
11941223
// Non-indexed parameters are stored in the Data field,
@@ -1220,7 +1249,7 @@ func (m *Client) decodeContractLogs(l zerolog.Logger, logs []types.Log, allABIs
12201249
Str("Signature", evSpec.Sig).
12211250
Msg("Unpacking event")
12221251

1223-
eventsMap, topicsMap, err := decodeEventFromLog(l, *eventABI, *evSpec, d)
1252+
eventsMap, topicsMap, err := decodeEventFromLog(l, *contractABI, *evSpec, d)
12241253
if err != nil {
12251254
l.Error().
12261255
Err(err).
@@ -1256,55 +1285,23 @@ func (m *Client) decodeContractLogs(l zerolog.Logger, logs []types.Log, allABIs
12561285
return eventsParsed, nil
12571286
}
12581287

1259-
// func (m *Client) decodeContractLogs(l zerolog.Logger, logs []types.Log, allABIs []*abi.ABI) ([]DecodedTransactionLog, error) {
1260-
// l.Trace().Msg("Decoding events")
1261-
// var eventsParsed []DecodedTransactionLog
1262-
// for _, lo := range logs {
1263-
// ABI_LOOP:
1264-
// for _, a := range allABIs {
1265-
// for _, evSpec := range a.Events {
1266-
// if evSpec.ID.Hex() == lo.Topics[0].Hex() {
1267-
// d := TransactionLog{lo.Topics, lo.Data}
1268-
// l.Trace().Str("Name", evSpec.RawName).Str("Signature", evSpec.Sig).Msg("Unpacking event")
1269-
// eventsMap, topicsMap, err := decodeEventFromLog(l, *a, evSpec, d)
1270-
// if err != nil {
1271-
// return nil, errors.Wrap(err, ErrDecodeLog)
1272-
// }
1273-
// parsedEvent := decodedLogFromMaps(&DecodedTransactionLog{}, eventsMap, topicsMap)
1274-
// decodedTransactionLog, ok := parsedEvent.(*DecodedTransactionLog)
1275-
// if ok {
1276-
// decodedTransactionLog.Signature = evSpec.Sig
1277-
// m.mergeLogMeta(decodedTransactionLog, lo)
1278-
// eventsParsed = append(eventsParsed, *decodedTransactionLog)
1279-
// l.Trace().Interface("Log", parsedEvent).Msg("Transaction log")
1280-
// break ABI_LOOP
1281-
// }
1282-
// l.Trace().
1283-
// Str("Actual type", fmt.Sprintf("%T", decodedTransactionLog)).
1284-
// Msg("Failed to cast decoded event to DecodedCommonLog")
1285-
// }
1286-
// }
1287-
// }
1288-
// }
1289-
// return eventsParsed, nil
1290-
// }
1291-
1292-
type EventWithABI struct {
1293-
EventABI *abi.ABI
1294-
EventSpec *abi.Event
1288+
type eventWithABI struct {
1289+
ContractABI *abi.ABI
1290+
EventSpec *abi.Event
12951291
}
12961292

12971293
// buildEventSignatureMap precomputes a mapping from event signature to events with their ABIs
1298-
func buildEventSignatureMap(allABIs []*abi.ABI) map[string][]*EventWithABI {
1299-
sigMap := make(map[string][]*EventWithABI)
1294+
func buildEventSignatureMap(allABIs []*abi.ABI) map[string][]*eventWithABI {
1295+
sigMap := make(map[string][]*eventWithABI)
13001296
for _, a := range allABIs {
13011297
for _, ev := range a.Events {
1302-
sigMap[ev.ID.Hex()] = append(sigMap[ev.ID.Hex()], &EventWithABI{
1303-
EventABI: a,
1304-
EventSpec: &ev,
1298+
sigMap[ev.ID.Hex()] = append(sigMap[ev.ID.Hex()], &eventWithABI{
1299+
ContractABI: a,
1300+
EventSpec: &ev,
13051301
})
13061302
}
13071303
}
1304+
13081305
return sigMap
13091306
}
13101307

0 commit comments

Comments
 (0)