@@ -253,7 +253,8 @@ type worker struct {
253
253
254
254
bundleCacheMu sync.Mutex
255
255
bundleCacheHeaderHash common.Hash
256
- bundleCache map [common.Hash ]simulatedBundle
256
+ bundleCacheSuccess map [common.Hash ]simulatedBundle
257
+ bundleCacheFailed map [common.Hash ]struct {}
257
258
258
259
snapshotMu sync.RWMutex // The lock used to protect the snapshots below
259
260
snapshotBlock * types.Block
@@ -370,7 +371,8 @@ func newWorker(config *Config, chainConfig *params.ChainConfig, engine consensus
370
371
resubmitAdjustCh : make (chan * intervalAdjust , resubmitAdjustChanSize ),
371
372
coinbase : builderCoinbase ,
372
373
flashbots : flashbots ,
373
- bundleCache : make (map [common.Hash ]simulatedBundle ),
374
+ bundleCacheSuccess : make (map [common.Hash ]simulatedBundle ),
375
+ bundleCacheFailed : make (map [common.Hash ]struct {}),
374
376
}
375
377
376
378
// Subscribe NewTxsEvent for tx pool
@@ -1739,38 +1741,61 @@ func (w *worker) simulateBundles(env *environment, bundles []types.MevBundle, pe
1739
1741
w .bundleCacheMu .Lock ()
1740
1742
defer w .bundleCacheMu .Unlock ()
1741
1743
1742
- simulatedBundles := []simulatedBundle {}
1743
-
1744
- var bundleCache map [common.Hash ]simulatedBundle
1744
+ var cacheSuccess map [common.Hash ]simulatedBundle
1745
+ var cacheFailed map [common.Hash ]struct {}
1745
1746
if w .bundleCacheHeaderHash == env .header .Hash () {
1746
- bundleCache = w .bundleCache
1747
+ cacheSuccess = w .bundleCacheSuccess
1748
+ cacheFailed = w .bundleCacheFailed
1747
1749
} else {
1748
- bundleCache = make (map [common.Hash ]simulatedBundle )
1750
+ cacheSuccess = make (map [common.Hash ]simulatedBundle )
1751
+ cacheFailed = make (map [common.Hash ]struct {})
1749
1752
}
1750
1753
1751
- for _ , bundle := range bundles {
1752
- if simmed , ok := bundleCache [bundle .Hash ]; ok {
1753
- simulatedBundles = append (simulatedBundles , simmed )
1754
+ simResult := make ([]* simulatedBundle , len (bundles ))
1755
+
1756
+ var wg sync.WaitGroup
1757
+ for i , bundle := range bundles {
1758
+ if simmed , ok := cacheSuccess [bundle .Hash ]; ok {
1759
+ simResult [i ] = & simmed
1754
1760
continue
1755
1761
}
1756
1762
1757
- if len ( bundle . Txs ) == 0 {
1763
+ if _ , ok := cacheFailed [ bundle . Hash ]; ok {
1758
1764
continue
1759
1765
}
1760
- state := env .state .Copy ()
1761
- gasPool := new (core.GasPool ).AddGas (env .header .GasLimit )
1762
- simmed , err := w .computeBundleGas (env , bundle , state , gasPool , pendingTxs , 0 )
1763
1766
1764
- if err != nil {
1765
- log .Debug ("Error computing gas for a bundle" , "error" , err )
1766
- continue
1767
+ wg .Add (1 )
1768
+ go func (idx int , bundle types.MevBundle , state * state.StateDB ) {
1769
+ defer wg .Done ()
1770
+ if len (bundle .Txs ) == 0 {
1771
+ return
1772
+ }
1773
+ gasPool := new (core.GasPool ).AddGas (env .header .GasLimit )
1774
+ simmed , err := w .computeBundleGas (env , bundle , state , gasPool , pendingTxs , 0 )
1775
+
1776
+ if err != nil {
1777
+ log .Debug ("Error computing gas for a bundle" , "error" , err )
1778
+ return
1779
+ }
1780
+ simResult [idx ] = & simmed
1781
+ }(i , bundle , env .state .Copy ())
1782
+ }
1783
+
1784
+ wg .Wait ()
1785
+
1786
+ simulatedBundles := make ([]simulatedBundle , 0 , len (bundles ))
1787
+ for i , bundle := range simResult {
1788
+ if bundle != nil {
1789
+ simulatedBundles = append (simulatedBundles , * bundle )
1790
+ cacheSuccess [bundle .OriginalBundle .Hash ] = * bundle
1791
+ } else {
1792
+ cacheFailed [bundles [i ].Hash ] = struct {}{}
1767
1793
}
1768
- bundleCache [bundle .Hash ] = simmed
1769
- simulatedBundles = append (simulatedBundles , simmed )
1770
1794
}
1771
1795
1772
1796
w .bundleCacheHeaderHash = env .header .Hash ()
1773
- w .bundleCache = bundleCache
1797
+ w .bundleCacheSuccess = cacheSuccess
1798
+ w .bundleCacheFailed = cacheFailed
1774
1799
1775
1800
return simulatedBundles , nil
1776
1801
}
0 commit comments