交易隐私保护与 MEV 防护系统,集成多种私有交易通道,防止三明治攻击与抢跑
┌─────────────────────────────────────────────────────────────┐ │ Transaction Gateway │ ├─────────────────────────────────────────────────────────────┤ │ TX Analyzer │ Risk Scorer │ Route Selector │ ├─────────────────────────────────────────────────────────────┤ │ MEV Protection Channels │ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │ │Flashbots │ │MEVBlocker│ │ Bloxroute│ │ Eden │ │ │ │ Protect │ │ │ │ Private │ │ Network │ │ │ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │ ├─────────────────────────────────────────────────────────────┤ │ Mempool Monitor │ │ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ │ │ Pending TX │ │ Sandwich │ │ Frontrun │ │ │ │ Watcher │ │ Detector │ │ Detector │ │ │ └──────────────┘ └──────────────┘ └──────────────┘ │ ├─────────────────────────────────────────────────────────────┤ │ Redis (TX Cache) │ Analytics DB │ Alert System │ └─────────────────────────────────────────────────────────────┘
1// SandwichDetector 三明治攻击检测器2type SandwichDetector struct {3 mempool *MempoolWatcher4 dexDecoder *DEXDecoder5 alertChan chan *SandwichAlert6}7 8// SandwichAlert 三明治攻击告警9type SandwichAlert struct {10 VictimTx common.Hash11 FrontrunTx common.Hash12 BackrunTx common.Hash13 Attacker common.Address14 EstProfit *big.Int15 TokenPair string16 DetectedAt time.Time17}18 19// Detect 检测潜在三明治攻击20func (d *SandwichDetector) Detect(21 ctx context.Context,22 pendingTxs []*types.Transaction,23) ([]*SandwichAlert, error) {24 alerts := make([]*SandwichAlert, 0)25 26 // 按 gas price 排序27 sort.Slice(pendingTxs, func(i, j int) bool {28 return pendingTxs[i].GasPrice().Cmp(29 pendingTxs[j].GasPrice()) > 030 })31 32 // 识别 DEX swap 交易33 swaps := make([]*SwapTx, 0)34 for _, tx := range pendingTxs {35 if swap, ok := d.dexDecoder.DecodeSwap(tx); ok {36 swaps = append(swaps, swap)37 }38 }39 40 // 检测三明治模式41 for i := 0; i < len(swaps)-2; i++ {42 front := swaps[i]43 victim := swaps[i+1]44 back := swaps[i+2]45 46 // 判断条件:47 // 1. 前后交易同一发送者48 // 2. 前后交易方向相反49 // 3. 交易同一代币对50 // 4. 前交易 gas price > 受害者 > 后交易51 if d.isSandwichPattern(front, victim, back) {52 profit := d.estimateProfit(front, victim, back)53 54 alert := &SandwichAlert{55 VictimTx: victim.Hash,56 FrontrunTx: front.Hash,57 BackrunTx: back.Hash,58 Attacker: front.From,59 EstProfit: profit,60 TokenPair: front.TokenPair(),61 DetectedAt: time.Now(),62 }63 64 alerts = append(alerts, alert)65 d.alertChan <- alert66 }67 }68 69 return alerts, nil70}71 72// isSandwichPattern 判断是否为三明治模式73func (d *SandwichDetector) isSandwichPattern(74 front, victim, back *SwapTx,75) bool {76 // 同一攻击者77 if front.From != back.From {78 return false79 }80 81 // 相同代币对82 if front.TokenPair() != victim.TokenPair() ||83 victim.TokenPair() != back.TokenPair() {84 return false85 }86 87 // 方向:前买后卖(或相反)88 if front.IsBuy == back.IsBuy {89 return false90 }91 92 // Gas 价格模式93 if front.GasPrice.Cmp(victim.GasPrice) <= 0 ||94 victim.GasPrice.Cmp(back.GasPrice) <= 0 {95 return false96 }97 98 return true99}