-
Notifications
You must be signed in to change notification settings - Fork 629
/
processors_builder.go
59 lines (52 loc) · 1.97 KB
/
processors_builder.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
package processor_manager
import (
"github.com/0xPolygonHermez/zkevm-node/etherman"
// "github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/synchronizer/actions"
)
// L1EventProcessorsBuilder is a builder for L1EventProcessors
// how to use:
//
// p := L1EventProcessorsBuilder{}
// p.Add(etherman.GlobalExitRootsOrder, l1events.NewGlobalExitRootLegacy(state))
// p.Set....
// return p.Build()
type L1EventProcessorsBuilder struct {
result *L1EventProcessors
}
// NewL1EventProcessorsBuilder returns a new L1EventProcessorsBuilder instance
func NewL1EventProcessorsBuilder() L1EventProcessorsBuilder {
return L1EventProcessorsBuilder{}
}
// Build return the L1EventProcessors builded
func (p *L1EventProcessorsBuilder) Build() *L1EventProcessors {
return p.result
}
// Register register a L1EventProcessor. It ask to the processor the supported forkId and events
// if there are a previous object register it will panic
func (p *L1EventProcessorsBuilder) Register(processor actions.L1EventProcessor) {
p.createResultIfNeeded()
for _, forkID := range processor.SupportedForkIds() {
for _, event := range processor.SupportedEvents() {
p.Set(forkID, event, processor, true)
}
}
}
// Set add a L1EventProcessor. If param panicIfExists is true, will panic if already exists the object
//
// the only use to panicIfExists=false is to override a processor in a unitttest
func (p *L1EventProcessorsBuilder) Set(forkID actions.ForkIdType, event etherman.EventOrder, processor actions.L1EventProcessor, panicIfExists bool) {
p.createResultIfNeeded()
if _, ok := p.result.processors[forkID]; !ok {
p.result.processors[forkID] = make(map[etherman.EventOrder]actions.L1EventProcessor)
}
if _, ok := p.result.processors[forkID][event]; ok && panicIfExists {
panic("processor already set")
}
p.result.processors[forkID][event] = processor
}
func (p *L1EventProcessorsBuilder) createResultIfNeeded() {
if p.result == nil {
p.result = NewL1EventProcessors()
}
}