-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathbundle_cache.go
88 lines (70 loc) · 1.8 KB
/
bundle_cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package miner
import (
"sync"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
const (
maxHeaders = 3
)
type BundleCache struct {
mu sync.Mutex
entries []*BundleCacheEntry
}
func NewBundleCache() *BundleCache {
return &BundleCache{
entries: make([]*BundleCacheEntry, maxHeaders),
}
}
func (b *BundleCache) GetBundleCache(header common.Hash) *BundleCacheEntry {
b.mu.Lock()
defer b.mu.Unlock()
for _, entry := range b.entries {
if entry != nil && entry.headerHash == header {
return entry
}
}
newEntry := newCacheEntry(header)
b.entries = b.entries[1:]
b.entries = append(b.entries, newEntry)
return newEntry
}
type BundleCacheEntry struct {
mu sync.Mutex
headerHash common.Hash
successfulBundles map[common.Hash]*types.SimulatedBundle
failedBundles map[common.Hash]struct{}
}
func newCacheEntry(header common.Hash) *BundleCacheEntry {
return &BundleCacheEntry{
headerHash: header,
successfulBundles: make(map[common.Hash]*types.SimulatedBundle),
failedBundles: make(map[common.Hash]struct{}),
}
}
func (c *BundleCacheEntry) GetSimulatedBundle(bundle common.Hash) (*types.SimulatedBundle, bool) {
c.mu.Lock()
defer c.mu.Unlock()
if simmed, ok := c.successfulBundles[bundle]; ok {
return simmed, true
}
if _, ok := c.failedBundles[bundle]; ok {
return nil, true
}
return nil, false
}
func (c *BundleCacheEntry) UpdateSimulatedBundles(result map[common.Hash]*types.SimulatedBundle, bundles []*types.Bundle) {
c.mu.Lock()
defer c.mu.Unlock()
for _, bundle := range bundles {
if bundle == nil {
continue
}
bundleHash := bundle.Hash()
if result[bundleHash] != nil {
c.successfulBundles[bundleHash] = result[bundleHash]
} else {
c.failedBundles[bundleHash] = struct{}{}
}
}
}