@@ -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
@@ -1740,38 +1742,61 @@ func (w *worker) simulateBundles(env *environment, bundles []types.MevBundle, pe
1740
1742
w .bundleCacheMu .Lock ()
1741
1743
defer w .bundleCacheMu .Unlock ()
1742
1744
1743
- simulatedBundles := []simulatedBundle {}
1744
-
1745
- var bundleCache map [common.Hash ]simulatedBundle
1745
+ var cacheSuccess map [common.Hash ]simulatedBundle
1746
+ var cacheFailed map [common.Hash ]struct {}
1746
1747
if w .bundleCacheHeaderHash == env .header .Hash () {
1747
- bundleCache = w .bundleCache
1748
+ cacheSuccess = w .bundleCacheSuccess
1749
+ cacheFailed = w .bundleCacheFailed
1748
1750
} else {
1749
- bundleCache = make (map [common.Hash ]simulatedBundle )
1751
+ cacheSuccess = make (map [common.Hash ]simulatedBundle )
1752
+ cacheFailed = make (map [common.Hash ]struct {})
1750
1753
}
1751
1754
1752
- for _ , bundle := range bundles {
1753
- if simmed , ok := bundleCache [bundle .Hash ]; ok {
1754
- simulatedBundles = append (simulatedBundles , simmed )
1755
+ simResult := make ([]* simulatedBundle , len (bundles ))
1756
+
1757
+ var wg sync.WaitGroup
1758
+ for i , bundle := range bundles {
1759
+ if simmed , ok := cacheSuccess [bundle .Hash ]; ok {
1760
+ simResult [i ] = & simmed
1755
1761
continue
1756
1762
}
1757
1763
1758
- if len ( bundle . Txs ) == 0 {
1764
+ if _ , ok := cacheFailed [ bundle . Hash ]; ok {
1759
1765
continue
1760
1766
}
1761
- state := env .state .Copy ()
1762
- gasPool := new (core.GasPool ).AddGas (env .header .GasLimit )
1763
- simmed , err := w .computeBundleGas (env , bundle , state , gasPool , pendingTxs , 0 )
1764
1767
1765
- if err != nil {
1766
- log .Debug ("Error computing gas for a bundle" , "error" , err )
1767
- continue
1768
+ wg .Add (1 )
1769
+ go func (idx int , bundle types.MevBundle , state * state.StateDB ) {
1770
+ defer wg .Done ()
1771
+ if len (bundle .Txs ) == 0 {
1772
+ return
1773
+ }
1774
+ gasPool := new (core.GasPool ).AddGas (env .header .GasLimit )
1775
+ simmed , err := w .computeBundleGas (env , bundle , state , gasPool , pendingTxs , 0 )
1776
+
1777
+ if err != nil {
1778
+ log .Debug ("Error computing gas for a bundle" , "error" , err )
1779
+ return
1780
+ }
1781
+ simResult [idx ] = & simmed
1782
+ }(i , bundle , env .state .Copy ())
1783
+ }
1784
+
1785
+ wg .Wait ()
1786
+
1787
+ simulatedBundles := make ([]simulatedBundle , 0 , len (bundles ))
1788
+ for i , bundle := range simResult {
1789
+ if bundle != nil {
1790
+ simulatedBundles = append (simulatedBundles , * bundle )
1791
+ cacheSuccess [bundle .OriginalBundle .Hash ] = * bundle
1792
+ } else {
1793
+ cacheFailed [bundles [i ].Hash ] = struct {}{}
1768
1794
}
1769
- bundleCache [bundle .Hash ] = simmed
1770
- simulatedBundles = append (simulatedBundles , simmed )
1771
1795
}
1772
1796
1773
1797
w .bundleCacheHeaderHash = env .header .Hash ()
1774
- w .bundleCache = bundleCache
1798
+ w .bundleCacheSuccess = cacheSuccess
1799
+ w .bundleCacheFailed = cacheFailed
1775
1800
1776
1801
return simulatedBundles , nil
1777
1802
}
0 commit comments