feat: create v2-prep branch with comprehensive planning
Restructured project for V2 refactor: **Structure Changes:** - Moved all V1 code to orig/ folder (preserved with git mv) - Created docs/planning/ directory - Added orig/README_V1.md explaining V1 preservation **Planning Documents:** - 00_V2_MASTER_PLAN.md: Complete architecture overview - Executive summary of critical V1 issues - High-level component architecture diagrams - 5-phase implementation roadmap - Success metrics and risk mitigation - 07_TASK_BREAKDOWN.md: Atomic task breakdown - 99+ hours of detailed tasks - Every task < 2 hours (atomic) - Clear dependencies and success criteria - Organized by implementation phase **V2 Key Improvements:** - Per-exchange parsers (factory pattern) - Multi-layer strict validation - Multi-index pool cache - Background validation pipeline - Comprehensive observability **Critical Issues Addressed:** - Zero address tokens (strict validation + cache enrichment) - Parsing accuracy (protocol-specific parsers) - No audit trail (background validation channel) - Inefficient lookups (multi-index cache) - Stats disconnection (event-driven metrics) Next Steps: 1. Review planning documents 2. Begin Phase 1: Foundation (P1-001 through P1-010) 3. Implement parsers in Phase 2 4. Build cache system in Phase 3 5. Add validation pipeline in Phase 4 6. Migrate and test in Phase 5 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
82
orig/pkg/scanner/market/pool_validator.go
Normal file
82
orig/pkg/scanner/market/pool_validator.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package market
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/ethclient"
|
||||
|
||||
"github.com/fraktal/mev-beta/internal/logger"
|
||||
)
|
||||
|
||||
// PoolValidator provides pool address validation before RPC queries
|
||||
type PoolValidator struct {
|
||||
logger *logger.Logger
|
||||
client *ethclient.Client
|
||||
}
|
||||
|
||||
// NewPoolValidator creates a new pool validator
|
||||
func NewPoolValidator(logger *logger.Logger, client *ethclient.Client) *PoolValidator {
|
||||
return &PoolValidator{
|
||||
logger: logger,
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
// IsValidPoolAddress performs comprehensive validation on a pool address
|
||||
// Returns true only if the address is worth querying from RPC
|
||||
func (pv *PoolValidator) IsValidPoolAddress(ctx context.Context, addr common.Address) (bool, string) {
|
||||
// Check 1: Address must not be zero
|
||||
if addr == (common.Address{}) {
|
||||
return false, "zero address"
|
||||
}
|
||||
|
||||
// Check 2: Address must be a valid Ethereum address format
|
||||
if !isValidEthereumAddress(addr) {
|
||||
return false, "invalid address format"
|
||||
}
|
||||
|
||||
// Check 3: If we have a client, verify contract exists at this address
|
||||
// This is the primary defense against invalid pool addresses
|
||||
if pv.client != nil {
|
||||
codeSize, err := getContractCodeSize(ctx, pv.client, addr)
|
||||
if err != nil {
|
||||
// Network errors are transient - allow retry
|
||||
pv.logger.Debug(fmt.Sprintf("Transient error checking contract for %s: %v (will retry)", addr.Hex(), err))
|
||||
return true, "" // Allow retry for transient failures
|
||||
}
|
||||
|
||||
// Zero bytecode means definitely no contract
|
||||
if codeSize == 0 {
|
||||
return false, "no contract deployed"
|
||||
}
|
||||
|
||||
// Contract exists - but may still be non-standard, let RPC call handle that
|
||||
return true, ""
|
||||
}
|
||||
|
||||
return true, ""
|
||||
}
|
||||
|
||||
// getContractCodeSize returns the size of bytecode at an address
|
||||
// Size 0 means no contract is deployed
|
||||
func getContractCodeSize(ctx context.Context, client *ethclient.Client, addr common.Address) (int, error) {
|
||||
code, err := client.CodeAt(ctx, addr, nil)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
return len(code), nil
|
||||
}
|
||||
|
||||
// isValidEthereumAddress validates basic Ethereum address format
|
||||
func isValidEthereumAddress(addr common.Address) bool {
|
||||
// Address must not be all zeros or all ones (obviously fake)
|
||||
zeroAddr := common.Address{}
|
||||
if addr == zeroAddr {
|
||||
return false
|
||||
}
|
||||
|
||||
// Check if it's a valid hex address length (already checked by common.Address type)
|
||||
return true
|
||||
}
|
||||
1973
orig/pkg/scanner/market/scanner.go
Normal file
1973
orig/pkg/scanner/market/scanner.go
Normal file
File diff suppressed because it is too large
Load Diff
45
orig/pkg/scanner/market/validation_test.go
Normal file
45
orig/pkg/scanner/market/validation_test.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package market
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/fraktal/mev-beta/internal/config"
|
||||
"github.com/fraktal/mev-beta/internal/logger"
|
||||
)
|
||||
|
||||
func TestNormalizeAndValidatePoolAddress(t *testing.T) {
|
||||
cfg := &config.BotConfig{
|
||||
MaxWorkers: 1,
|
||||
RPCTimeout: 1,
|
||||
}
|
||||
log := logger.New("info", "text", "")
|
||||
scanner := NewMarketScanner(cfg, log, nil, nil)
|
||||
|
||||
t.Run("accepts known pool", func(t *testing.T) {
|
||||
address := "0xC6962004f452bE9203591991D15f6b388e09E8D0" // known Uniswap V3 pool
|
||||
normalized, result, err := scanner.normalizeAndValidatePoolAddress(address)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.Equal(t, "0xc6962004f452be9203591991d15f6b388e09e8d0", normalized)
|
||||
require.True(t, result.IsValid)
|
||||
})
|
||||
|
||||
t.Run("rejects known token misclassified as pool", func(t *testing.T) {
|
||||
address := "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1" // WETH
|
||||
_, result, err := scanner.normalizeAndValidatePoolAddress(address)
|
||||
require.Error(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.True(t, errors.Is(err, ErrInvalidPoolCandidate))
|
||||
})
|
||||
|
||||
t.Run("rejects corrupted address", func(t *testing.T) {
|
||||
address := "0x0000000000000000000000000000000000000000"
|
||||
_, result, err := scanner.normalizeAndValidatePoolAddress(address)
|
||||
require.Error(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.True(t, errors.Is(err, ErrInvalidPoolCandidate))
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user