-
Notifications
You must be signed in to change notification settings - Fork 47
/
fsm.go
219 lines (193 loc) · 5.92 KB
/
fsm.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package initialsync
import (
"errors"
"fmt"
"github.com/amazechain/amc/api/protocol/types_pb"
"github.com/holiman/uint256"
"sort"
"time"
"github.com/libp2p/go-libp2p/core/peer"
)
const (
stateNew stateID = iota
stateScheduled
stateDataParsed
stateSkipped
stateSent
)
const (
eventTick eventID = iota
eventDataReceived
)
// stateID is unique handle for a state.
type stateID uint8
// eventID is unique handle for an event.
type eventID uint8
// stateMachineManager is a collection of managed FSMs.
type stateMachineManager struct {
// todo
keys []*uint256.Int
machines map[uint64]*stateMachine
handlers map[stateID]map[eventID]eventHandlerFn
}
// stateMachine holds a state of a single block processing FSM.
// Each FSM allows deterministic state transitions: State(S) x Event(E) -> Actions (A), State(S').
type stateMachine struct {
smm *stateMachineManager
start *uint256.Int
state stateID
pid peer.ID
blocks []*types_pb.Block
updated time.Time
}
// eventHandlerFn is an event handler function's signature.
type eventHandlerFn func(m *stateMachine, data interface{}) (newState stateID, err error)
// newStateMachineManager returns fully initialized state machine manager.
func newStateMachineManager() *stateMachineManager {
return &stateMachineManager{
keys: make([]*uint256.Int, 0, lookaheadSteps),
machines: make(map[uint64]*stateMachine, lookaheadSteps),
handlers: make(map[stateID]map[eventID]eventHandlerFn),
}
}
// addHandler attaches an event handler to a state event.
func (smm *stateMachineManager) addEventHandler(event eventID, state stateID, fn eventHandlerFn) {
if _, ok := smm.handlers[state]; !ok {
smm.handlers[state] = make(map[eventID]eventHandlerFn)
}
if _, ok := smm.handlers[state][event]; !ok {
smm.handlers[state][event] = fn
}
}
// addStateMachine allocates memory for new FSM.
func (smm *stateMachineManager) addStateMachine(startBlockNr *uint256.Int) *stateMachine {
smm.machines[startBlockNr.Uint64()] = &stateMachine{
smm: smm,
start: startBlockNr.Clone(),
state: stateNew,
blocks: []*types_pb.Block{},
updated: time.Now(),
}
smm.recalculateMachineAttribs()
return smm.machines[startBlockNr.Uint64()]
}
// removeStateMachine frees memory of a processed/finished FSM.
func (smm *stateMachineManager) removeStateMachine(startSlot *uint256.Int) error {
if _, ok := smm.machines[startSlot.Uint64()]; !ok {
return fmt.Errorf("state for machine %v is not found", startSlot)
}
smm.machines[startSlot.Uint64()].blocks = nil
delete(smm.machines, startSlot.Uint64())
smm.recalculateMachineAttribs()
return nil
}
// removeAllStateMachines removes all managed machines.
func (smm *stateMachineManager) removeAllStateMachines() error {
for _, key := range smm.keys {
if err := smm.removeStateMachine(key); err != nil {
return err
}
}
smm.recalculateMachineAttribs()
return nil
}
// recalculateMachineAttribs updates cached attributes, which are used for efficiency.
func (smm *stateMachineManager) recalculateMachineAttribs() {
keys := make([]*uint256.Int, 0, lookaheadSteps)
for key := range smm.machines {
keys = append(keys, uint256.NewInt(key))
}
sort.Slice(keys, func(i, j int) bool {
return keys[i].Cmp(keys[j]) == -1
})
smm.keys = keys
}
// findStateMachine returns a state machine for a given start slot (if exists).
func (smm *stateMachineManager) findStateMachine(startSlot *uint256.Int) (*stateMachine, bool) {
fsm, ok := smm.machines[startSlot.Uint64()]
return fsm, ok
}
// highestStartSlot returns the start slot for the latest known state machine.
func (smm *stateMachineManager) highestStartSlot() (*uint256.Int, error) {
if len(smm.keys) == 0 {
return uint256.NewInt(0), errors.New("no state machine exist")
}
key := smm.keys[len(smm.keys)-1]
return smm.machines[key.Uint64()].start, nil
}
// allMachinesInState checks whether all registered state machines are in the same state.
func (smm *stateMachineManager) allMachinesInState(state stateID) bool {
if len(smm.machines) == 0 {
return false
}
for _, fsm := range smm.machines {
if fsm.state != state {
return false
}
}
return true
}
// String returns human readable representation of a FSM collection.
func (smm *stateMachineManager) String() string {
return fmt.Sprintf("%v", smm.machines)
}
// setState updates the current state of a given state machine.
func (m *stateMachine) setState(name stateID) {
if m.state == name {
return
}
m.state = name
m.updated = time.Now()
}
// trigger invokes the event handler on a given state machine.
func (m *stateMachine) trigger(event eventID, data interface{}) error {
handlers, ok := m.smm.handlers[m.state]
if !ok {
return fmt.Errorf("no event handlers registered for event: %v, state: %v", event, m.state)
}
if handlerFn, ok := handlers[event]; ok {
state, err := handlerFn(m, data)
if err != nil {
return err
}
m.setState(state)
}
return nil
}
// isFirst checks whether a given machine has the lowest start slot.
func (m *stateMachine) isFirst() bool {
return m.start == m.smm.keys[0]
}
// isLast checks whether a given machine has the highest start slot.
func (m *stateMachine) isLast() bool {
return m.start == m.smm.keys[len(m.smm.keys)-1]
}
// String returns human-readable representation of a FSM state.
func (m *stateMachine) String() string {
return fmt.Sprintf("{%d:%s}", m.start.Uint64(), m.state)
}
// String returns human-readable representation of a state.
func (s stateID) String() string {
states := map[stateID]string{
stateNew: "new",
stateScheduled: "scheduled",
stateDataParsed: "dataParsed",
stateSkipped: "skipped",
stateSent: "sent",
}
if _, ok := states[s]; !ok {
return "stateUnknown"
}
return states[s]
}
// String returns human-readable representation of an event.
func (e eventID) String() string {
events := map[eventID]string{
eventTick: "tick",
eventDataReceived: "dataReceived",
}
if _, ok := events[e]; !ok {
return "eventUnknown"
}
return events[e]
}