Files
mev-beta/pkg/lifecycle/error_enrichment.go
Krypto Kajun 850223a953 fix(multicall): resolve critical multicall parsing corruption issues
- Added comprehensive bounds checking to prevent buffer overruns in multicall parsing
- Implemented graduated validation system (Strict/Moderate/Permissive) to reduce false positives
- Added LRU caching system for address validation with 10-minute TTL
- Enhanced ABI decoder with missing Universal Router and Arbitrum-specific DEX signatures
- Fixed duplicate function declarations and import conflicts across multiple files
- Added error recovery mechanisms with multiple fallback strategies
- Updated tests to handle new validation behavior for suspicious addresses
- Fixed parser test expectations for improved validation system
- Applied gofmt formatting fixes to ensure code style compliance
- Fixed mutex copying issues in monitoring package by introducing MetricsSnapshot
- Resolved critical security vulnerabilities in heuristic address extraction
- Progress: Updated TODO audit from 10% to 35% complete

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-17 00:12:55 -05:00

110 lines
2.1 KiB
Go

package lifecycle
import (
"errors"
"fmt"
"regexp"
"strings"
)
var txHashPattern = regexp.MustCompile(`0x[a-fA-F0-9]{64}`)
type RecordedError struct {
Err error
TxHash string
}
func (re RecordedError) Error() string {
if re.Err == nil {
return ""
}
return re.Err.Error()
}
func enrichErrorWithTxHash(message string, err error, attrs []interface{}) (error, string, []interface{}) {
txHash, attrsWithTx := ensureTxHash(attrs, err)
wrapped := fmt.Errorf("%s: %w", message, err)
if txHash != "" {
wrapped = fmt.Errorf("%s [tx_hash=%s]: %w", message, txHash, err)
}
return wrapped, txHash, attrsWithTx
}
func ensureTxHash(attrs []interface{}, err error) (string, []interface{}) {
txHash := extractTxHashFromAttrs(attrs)
if txHash == "" {
txHash = extractTxHashFromError(err)
}
if txHash == "" {
return "", attrs
}
hasTxAttr := false
for i := 0; i+1 < len(attrs); i += 2 {
key, ok := attrs[i].(string)
if !ok {
continue
}
if key == "tx_hash" || key == "transaction_hash" || key == "tx" {
hasTxAttr = true
break
}
}
if !hasTxAttr {
attrs = append(attrs, "tx_hash", txHash)
}
return txHash, attrs
}
func extractTxHashFromAttrs(attrs []interface{}) string {
for i := 0; i+1 < len(attrs); i += 2 {
key, ok := attrs[i].(string)
if !ok {
continue
}
if key == "tx_hash" || key == "transaction_hash" || key == "tx" {
if value, ok := attrs[i+1].(string); ok && isValidTxHash(value) {
return strings.ToLower(value)
}
}
}
return ""
}
func extractTxHashFromError(err error) string {
for err != nil {
if match := txHashPattern.FindString(err.Error()); match != "" {
return strings.ToLower(match)
}
err = errors.Unwrap(err)
}
return ""
}
func isValidTxHash(value string) bool {
if value == "" {
return false
}
if len(value) != 66 {
return false
}
if !strings.HasPrefix(value, "0x") {
return false
}
for _, r := range value[2:] {
if !isHexChar(r) {
return false
}
}
return true
}
func isHexChar(r rune) bool {
return (r >= '0' && r <= '9') ||
(r >= 'a' && r <= 'f') ||
(r >= 'A' && r <= 'F')
}