-
Notifications
You must be signed in to change notification settings - Fork 697
/
l2block.go
111 lines (96 loc) · 2.93 KB
/
l2block.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package state
import (
"context"
"errors"
"math/big"
"sync"
"time"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/ethereum/go-ethereum/core/types"
)
// NewL2BlockEventHandler represent a func that will be called by the
// state when a NewL2BlockEvent is triggered
type NewL2BlockEventHandler func(e NewL2BlockEvent)
// NewL2BlockEvent is a struct provided from the state to the NewL2BlockEventHandler
// when a new l2 block is detected with data related to this new l2 block.
type NewL2BlockEvent struct {
Block types.Block
}
// PrepareWebSocket allows the RPC to prepare ws
func (s *State) PrepareWebSocket() {
lastL2Block, err := s.PostgresStorage.GetLastL2Block(context.Background(), nil)
if errors.Is(err, ErrStateNotSynchronized) {
lastL2Block = types.NewBlockWithHeader(&types.Header{Number: big.NewInt(0)})
} else if err != nil {
log.Fatalf("failed to load the last l2 block: %v", err)
}
s.lastL2BlockSeen = *lastL2Block
go s.monitorNewL2Blocks()
go s.handleEvents()
}
// RegisterNewL2BlockEventHandler add the provided handler to the list of handlers
// that will be triggered when a new l2 block event is triggered
func (s *State) RegisterNewL2BlockEventHandler(h NewL2BlockEventHandler) {
log.Info("new l2 block event handler registered")
s.newL2BlockEventHandlers = append(s.newL2BlockEventHandlers, h)
}
func (s *State) handleEvents() {
for newL2BlockEvent := range s.newL2BlockEvents {
if len(s.newL2BlockEventHandlers) == 0 {
continue
}
wg := sync.WaitGroup{}
for _, handler := range s.newL2BlockEventHandlers {
wg.Add(1)
go func(h NewL2BlockEventHandler) {
defer func() {
wg.Done()
if r := recover(); r != nil {
log.Errorf("failed and recovered in NewL2BlockEventHandler: %v", r)
}
}()
h(newL2BlockEvent)
}(handler)
}
wg.Wait()
}
}
func (s *State) monitorNewL2Blocks() {
waitNextCycle := func() {
time.Sleep(1 * time.Second)
}
for {
if len(s.newL2BlockEventHandlers) == 0 {
waitNextCycle()
continue
}
lastL2Block, err := s.GetLastL2Block(context.Background(), nil)
if errors.Is(err, ErrStateNotSynchronized) {
waitNextCycle()
continue
} else if err != nil {
log.Errorf("failed to get last l2 block while monitoring new blocks: %v", err)
waitNextCycle()
continue
}
// not updates until now
if lastL2Block == nil || s.lastL2BlockSeen.NumberU64() >= lastL2Block.NumberU64() {
waitNextCycle()
continue
}
for bn := s.lastL2BlockSeen.NumberU64() + uint64(1); bn <= lastL2Block.NumberU64(); bn++ {
block, err := s.GetL2BlockByNumber(context.Background(), bn, nil)
if err != nil {
log.Errorf("failed to l2 block while monitoring new blocks: %v", err)
break
}
s.newL2BlockEvents <- NewL2BlockEvent{
Block: *block,
}
log.Infof("new l2 blocks detected, Number %v, Hash %v", block.NumberU64(), block.Hash().String())
s.lastL2BlockSeen = *block
}
// interval to check for new l2 blocks
waitNextCycle()
}
}