package test import ( "fmt" "strings" "testing" "github.com/ethereum/go-ethereum/common" "github.com/fraktal/mev-beta/internal/validation" ) // TestValidationFixForKnownAddresses tests the validation fixes against the specific addresses causing errors in production func TestValidationFixForKnownAddresses(t *testing.T) { validator := validation.NewAddressValidator() // Test the specific addresses that were causing "Error getting pool data" logs testCases := []struct { name string address string expectERC20 bool expectValidPool bool expectValidation bool description string }{ { name: "WETH - should be recognized as ERC-20", address: "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1", expectERC20: true, expectValidPool: false, expectValidation: true, description: "WETH was being treated as a pool causing slot0 errors", }, { name: "USDC - should be recognized as ERC-20", address: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831", expectERC20: true, expectValidPool: false, expectValidation: true, description: "USDC was being treated as a pool causing execution reverts", }, { name: "USDT - should be recognized as ERC-20", address: "0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9", expectERC20: true, expectValidPool: false, expectValidation: true, description: "USDT was being treated as a pool causing execution reverts", }, { name: "ARB Token - should be recognized as ERC-20", address: "0x912CE59144191C1204E64559FE8253a0e49E6548", expectERC20: true, expectValidPool: false, expectValidation: true, description: "ARB token was being treated as a pool", }, { name: "Zero address - should be rejected", address: "0x0000000000000000000000000000000000000000", expectERC20: false, expectValidPool: false, expectValidation: false, description: "Zero address corruption causing massive log spam", }, { name: "Corrupted address pattern 1", address: "0x0000000000000000000000000000001202a5e794", expectERC20: false, expectValidPool: false, expectValidation: false, description: "Leading zeros corruption pattern", }, { name: "Corrupted address pattern 2", address: "0x00000003000000000000000000000000003528C0", expectERC20: false, expectValidPool: false, expectValidation: false, description: "Mixed zero corruption pattern", }, } fmt.Println("๐Ÿงช Testing Validation Fixes for Production Error Addresses") fmt.Println(strings.Repeat("=", 80)) for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { addr := common.HexToAddress(tc.address) // Test basic validation result := validator.ValidateAddress(tc.address) // Test pool operation validation poolOpErr := validator.ValidatePoolOperation(addr, "slot0") // Test detailed analysis analysis := validator.GetDetailedAddressAnalysis(addr) fmt.Printf("\n๐Ÿ” Testing: %s\n", tc.name) fmt.Printf(" Address: %s\n", tc.address) fmt.Printf(" Description: %s\n", tc.description) fmt.Printf(" Valid: %v (Expected: %v)\n", result.IsValid, tc.expectValidation) fmt.Printf(" Corruption Score: %d\n", result.CorruptionScore) fmt.Printf(" Contract Type: %s\n", result.ContractType.String()) if isERC20, ok := analysis["is_known_erc20"].(bool); ok { fmt.Printf(" Known ERC-20: %v (Expected: %v)\n", isERC20, tc.expectERC20) if isERC20 != tc.expectERC20 { t.Errorf("ERC-20 recognition failed: expected %v, got %v", tc.expectERC20, isERC20) } } if tokenName, ok := analysis["token_name"].(string); ok && tc.expectERC20 { fmt.Printf(" Token Name: %s\n", tokenName) } // Test pool operation validation if tc.expectERC20 { if poolOpErr == nil { t.Errorf("Expected pool operation to be rejected for ERC-20 token %s", tc.address) fmt.Printf(" โŒ Pool Operation: Should have been rejected but was allowed\n") } else { fmt.Printf(" โœ… Pool Operation: Correctly rejected - %s\n", poolOpErr.Error()) } } else if tc.expectValidation { if poolOpErr != nil { fmt.Printf(" โ„น๏ธ Pool Operation: Rejected - %s\n", poolOpErr.Error()) } else { fmt.Printf(" โœ… Pool Operation: Allowed for valid pool\n") } } else { if poolOpErr != nil { fmt.Printf(" โœ… Pool Operation: Correctly rejected for invalid address\n") } else { t.Errorf("Expected pool operation to be rejected for invalid address %s", tc.address) } } // Verify corruption pattern detection if corruptionPattern, ok := analysis["corruption_pattern"].(string); ok { fmt.Printf(" Corruption Pattern: %s\n", corruptionPattern) } // Verify basic validation expectation if result.IsValid != tc.expectValidation { t.Errorf("Validation result mismatch for %s: expected %v, got %v", tc.address, tc.expectValidation, result.IsValid) } }) } fmt.Println("\n" + strings.Repeat("=", 80)) fmt.Println("๐ŸŽฏ Validation Fix Test Summary:") fmt.Println(" - Major ERC-20 tokens (WETH, USDC, USDT, ARB) should be recognized") fmt.Println(" - Pool operations on ERC-20 tokens should be prevented") fmt.Println(" - Corrupted addresses should be properly detected and rejected") fmt.Println(" - This should eliminate the 535K+ log spam from production") } // TestCorruptionSourceAnalysis tests the system's ability to identify corruption sources func TestCorruptionSourceAnalysis(t *testing.T) { validator := validation.NewAddressValidator() // Test various corruption patterns found in the logs corruptionCases := []struct { corrupted string pattern string description string }{ { corrupted: "0x0000000000000000000000000000000000000000", pattern: "ZERO_ADDRESS", description: "Complete zero address - most common corruption (6,895 occurrences)", }, { corrupted: "0x0000000000000000000000000000001202a5e794", pattern: "LEADING_ZEROS_CORRUPTION", description: "Leading zeros with trailing data - ABI decoding issue", }, { corrupted: "0x82af49447d8a07e3bd95bd0d56f35241523fbab1weth", pattern: "EMBEDDED_WETH_PATTERN", description: "WETH address with embedded corruption", }, } fmt.Println("\n๐Ÿ” Testing Corruption Pattern Detection") fmt.Println(strings.Repeat("=", 60)) for _, tc := range corruptionCases { t.Run(tc.pattern, func(t *testing.T) { // Note: This would require modifying the test to handle invalid hex // For now, test with valid hex patterns if len(tc.corrupted) == 42 && tc.corrupted[:2] == "0x" { addr := common.HexToAddress(tc.corrupted) analysis := validator.GetDetailedAddressAnalysis(addr) fmt.Printf("\n Pattern: %s\n", tc.pattern) fmt.Printf(" Address: %s\n", tc.corrupted) fmt.Printf(" Description: %s\n", tc.description) if detectedPattern, ok := analysis["corruption_pattern"].(string); ok { fmt.Printf(" Detected: %s\n", detectedPattern) if detectedPattern == tc.pattern { fmt.Printf(" โœ… Correctly identified\n") } else { fmt.Printf(" โš ๏ธ Different pattern detected\n") } } if corruptionScore, ok := analysis["corruption_score"].(int); ok { fmt.Printf(" Corruption Score: %d\n", corruptionScore) } } }) } }