-
Notifications
You must be signed in to change notification settings - Fork 202
/
epochStartSubscriptionHandler.go
114 lines (96 loc) · 4.1 KB
/
epochStartSubscriptionHandler.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
112
113
114
package notifier
import (
"sort"
"sync"
"github.com/ElrondNetwork/elrond-go-core/data"
"github.com/ElrondNetwork/elrond-go/epochStart"
)
// EpochStartNotifier defines which actions should be done for handling new epoch's events
type EpochStartNotifier interface {
RegisterHandler(handler epochStart.ActionHandler)
UnregisterHandler(handler epochStart.ActionHandler)
NotifyAll(hdr data.HeaderHandler)
NotifyAllPrepare(metaHdr data.HeaderHandler, body data.BodyHandler)
NotifyEpochChangeConfirmed(epoch uint32)
RegisterForEpochChangeConfirmed(handler func(epoch uint32))
IsInterfaceNil() bool
}
var _ EpochStartNotifier = (*epochStartSubscriptionHandler)(nil)
// epochStartSubscriptionHandler will handle subscription of function and notifying them
type epochStartSubscriptionHandler struct {
epochStartHandlers []epochStart.ActionHandler
epochFinalizedHandler []func(epoch uint32)
mutEpochStartHandler sync.RWMutex
}
// NewEpochStartSubscriptionHandler returns a new instance of epochStartSubscriptionHandler
func NewEpochStartSubscriptionHandler() *epochStartSubscriptionHandler {
return &epochStartSubscriptionHandler{
epochStartHandlers: make([]epochStart.ActionHandler, 0),
epochFinalizedHandler: make([]func(epoch uint32), 0),
mutEpochStartHandler: sync.RWMutex{},
}
}
// RegisterHandler will subscribe a function so it will be called when NotifyAll method is called
func (essh *epochStartSubscriptionHandler) RegisterHandler(handler epochStart.ActionHandler) {
if handler != nil {
essh.mutEpochStartHandler.Lock()
essh.epochStartHandlers = append(essh.epochStartHandlers, handler)
essh.mutEpochStartHandler.Unlock()
}
}
// UnregisterHandler will unsubscribe a function from the slice
func (essh *epochStartSubscriptionHandler) UnregisterHandler(handlerToUnregister epochStart.ActionHandler) {
if handlerToUnregister != nil {
essh.mutEpochStartHandler.Lock()
for idx, handler := range essh.epochStartHandlers {
if handler == handlerToUnregister {
essh.epochStartHandlers = append(essh.epochStartHandlers[:idx], essh.epochStartHandlers[idx+1:]...)
}
}
essh.mutEpochStartHandler.Unlock()
}
}
// NotifyAll will call all the subscribed functions from the internal slice
func (essh *epochStartSubscriptionHandler) NotifyAll(hdr data.HeaderHandler) {
essh.mutEpochStartHandler.RLock()
sort.Slice(essh.epochStartHandlers, func(i, j int) bool {
return essh.epochStartHandlers[i].NotifyOrder() < essh.epochStartHandlers[j].NotifyOrder()
})
for i := 0; i < len(essh.epochStartHandlers); i++ {
essh.epochStartHandlers[i].EpochStartAction(hdr)
}
essh.mutEpochStartHandler.RUnlock()
}
// NotifyAllPrepare will call all the subscribed clients to notify them that an epoch change block has been
// observed, but not yet confirmed/committed. Some components may need to do some initialisation/preparation
func (essh *epochStartSubscriptionHandler) NotifyAllPrepare(metaHdr data.HeaderHandler, body data.BodyHandler) {
essh.mutEpochStartHandler.RLock()
sort.Slice(essh.epochStartHandlers, func(i, j int) bool {
return essh.epochStartHandlers[i].NotifyOrder() < essh.epochStartHandlers[j].NotifyOrder()
})
for i := 0; i < len(essh.epochStartHandlers); i++ {
essh.epochStartHandlers[i].EpochStartPrepare(metaHdr, body)
}
essh.mutEpochStartHandler.RUnlock()
}
// RegisterForEpochChangeConfirmed will register the handler function to be called when epoch change is confirmed
func (essh *epochStartSubscriptionHandler) RegisterForEpochChangeConfirmed(handler func(epoch uint32)) {
if handler == nil {
return
}
essh.mutEpochStartHandler.Lock()
essh.epochFinalizedHandler = append(essh.epochFinalizedHandler, handler)
essh.mutEpochStartHandler.Unlock()
}
// NotifyEpochChangeConfirmed will call all the subscribed clients to notify them that an epoch change is confirmed
func (essh *epochStartSubscriptionHandler) NotifyEpochChangeConfirmed(epoch uint32) {
essh.mutEpochStartHandler.RLock()
for _, handler := range essh.epochFinalizedHandler {
go handler(epoch)
}
essh.mutEpochStartHandler.RUnlock()
}
// IsInterfaceNil -
func (essh *epochStartSubscriptionHandler) IsInterfaceNil() bool {
return essh == nil
}