-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathstate_machine.go
247 lines (222 loc) · 6.56 KB
/
state_machine.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package sbac
import (
"bytes"
fmt "fmt"
"sync"
"chainspace.io/chainspace-go/internal/log"
"chainspace.io/chainspace-go/internal/log/fld"
)
type StateTable struct {
actions map[State]Action
transitions map[StateTransition]Transition
}
type StateTransition struct {
From State
To State
}
type SignedDecision struct {
Decision SBACDecision
Signature []byte
}
// Action specify an action to execute when a new event is triggered.
// it returns a State, which will be either the new actual state, the next state
// which may required a transition from the current state to the new one (see the transition
// table
type Action func(s *States) (State, error)
// Transition are called when the state is change from a current state to a new one.
// return a State which may involved a new transition as well.
type Transition func(s *States) (State, error)
type StateMachineConfig struct {
ConsensusAction ConsensusEventAction
SBACAction SBACEventAction
Table *StateTable
Detail *DetailTx
InitialState State
}
type DetailTx struct {
ID []byte
RawTx []byte
Tx *Transaction
Evidences map[uint64][]byte
HashID uint32
}
type States struct {
consensus map[ConsensusOp]*ConsensusStateMachine
sbac map[SBACOp]*SBACStateMachine
detail *DetailTx
}
type StateMachine struct {
table *StateTable
mu sync.Mutex
events *pendingEvents
states *States
state State
}
func (sm *StateMachine) StateReport() *StateReport {
sm.mu.Lock()
defer sm.mu.Unlock()
s := StateReport{
HashID: sm.states.detail.HashID,
State: sm.state.String(),
CommitDecisions: map[uint64]bool{},
Phase1Decisions: map[uint64]bool{},
Phase2Decisions: map[uint64]bool{},
PendingEvents: int32(sm.events.Len()),
}
for k, v := range sm.states.sbac[SBACOp_Commit].GetDecisions() {
s.CommitDecisions[k] = v.Decision == SBACDecision_ACCEPT
}
for k, v := range sm.states.sbac[SBACOp_Phase1].GetDecisions() {
s.Phase1Decisions[k] = v.Decision == SBACDecision_ACCEPT
}
for k, v := range sm.states.sbac[SBACOp_Phase2].GetDecisions() {
s.Phase2Decisions[k] = v.Decision == SBACDecision_ACCEPT
}
return &s
}
func (sm *StateMachine) State() State {
sm.mu.Lock()
defer sm.mu.Unlock()
return sm.state
}
func (sm *StateMachine) setState(newState State) {
sm.mu.Lock()
defer sm.mu.Unlock()
sm.state = newState
}
func (sm *StateMachine) applyTransition(transitionTo State) error {
for {
curState := sm.State()
txtransition := StateTransition{curState, transitionTo}
f, ok := sm.table.transitions[txtransition]
if !ok {
// no more transitions available, this is not an error
return nil
}
log.Info("applying transition",
log.Uint32("id", sm.states.detail.HashID),
log.String("old_state", curState.String()),
log.String("new_state", transitionTo.String()),
)
nextstate, err := f(sm.states)
if err != nil {
log.Error("unable to apply transition",
log.Uint32("id", sm.states.detail.HashID),
log.String("old_state", curState.String()),
log.String("new_state", transitionTo.String()),
fld.Err(err),
)
return err
}
sm.setState(transitionTo)
transitionTo = nextstate
}
}
func (sm *StateMachine) moveState() error {
for {
curState := sm.State()
// first try to execute an action if possible with the current state
action, ok := sm.table.actions[curState]
if !ok {
log.Error("unable to find an action to map with the current state",
log.String("state", curState.String()), fld.TxID(sm.states.detail.HashID))
return nil
}
log.Info("applying action",
log.Uint32("id", sm.states.detail.HashID),
log.String("state", curState.String()),
)
newstate, err := action(sm.states)
if err != nil {
log.Error("unable to execute action", fld.Err(err))
return err
}
if newstate == sm.state {
// action returned the same state, we can return now as this action is not ready to be completed
// although this is not an error
return nil
}
// action succeed, we try to find a transition, if any we apply it, if none available, just set the new state.
txtransition := StateTransition{curState, newstate}
// if a transition exist for the new state, apply it
if _, ok := sm.table.transitions[txtransition]; ok {
err = sm.applyTransition(newstate)
if err != nil {
log.Error("unable to apply transition", fld.Err(err))
}
} else {
// else save the new state directly
sm.setState(newstate)
}
}
}
func (sm *StateMachine) processEvent(rawe Event) error {
if !bytes.Equal(rawe.TxID(), sm.states.detail.ID) {
return fmt.Errorf("event linked to another transaction")
}
switch rawe.Kind() {
case EventKindConsensus:
e := rawe.(*ConsensusEvent)
sm.states.consensus[e.data.Op].processEvent(sm.states, e)
return nil
case EventKindSBACMessage:
e := rawe.(*SBACEvent)
sm.states.sbac[e.msg.Op].processEvent(sm.states, e)
return nil
default:
return fmt.Errorf("unknown event")
}
}
func (sm *StateMachine) consumeEvent(e Event) bool {
curState := sm.State()
if log.AtDebug() {
log.Info("processing new event",
log.Uint32("id", sm.states.detail.HashID),
log.String("state", curState.String()),
fld.PeerID(e.PeerID()),
)
}
// process the current event
sm.processEvent(e)
// then try to move the state from this point
if curState == StateSucceeded || curState == StateAborted {
if log.AtDebug() {
log.Debug("statemachine reach end", log.String("final_state", sm.state.String()))
}
return true
}
err := sm.moveState()
if err != nil {
log.Error("something happend while moving states", fld.Err(err))
}
return true
}
func (sm *StateMachine) OnEvent(e Event) {
sm.events.OnEvent(e)
}
func NewStateMachine(cfg *StateMachineConfig) *StateMachine {
sm := &StateMachine{
table: cfg.Table,
states: &States{
detail: cfg.Detail,
consensus: map[ConsensusOp]*ConsensusStateMachine{},
sbac: map[SBACOp]*SBACStateMachine{},
},
state: cfg.InitialState,
}
sm.states.consensus[ConsensusOp_Consensus1] =
NewConsensuStateMachine(ConsensusOp_Consensus1, cfg.ConsensusAction)
sm.states.consensus[ConsensusOp_Consensus2] =
NewConsensuStateMachine(ConsensusOp_Consensus2, cfg.ConsensusAction)
sm.states.consensus[ConsensusOp_ConsensusCommit] =
NewConsensuStateMachine(ConsensusOp_ConsensusCommit, cfg.ConsensusAction)
sm.states.sbac[SBACOp_Phase1] =
NewSBACStateMachine(SBACOp_Phase1, cfg.SBACAction)
sm.states.sbac[SBACOp_Phase2] =
NewSBACStateMachine(SBACOp_Phase2, cfg.SBACAction)
sm.states.sbac[SBACOp_Commit] =
NewSBACStateMachine(SBACOp_Commit, cfg.SBACAction)
sm.events = NewPendingEvents(sm.consumeEvent)
go sm.events.Run()
return sm
}