-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
174 lines (142 loc) · 3.53 KB
/
handler.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
package isaacstates
import (
"fmt"
"github.com/ProtoconNet/mitum2/base"
"github.com/ProtoconNet/mitum2/util"
"github.com/pkg/errors"
"github.com/rs/zerolog"
)
type StateType string
const (
StateEmpty = StateType("")
// StateStopped indicates all processes is finished.
StateStopped = StateType("STOPPED")
// StateBooting indicates node checks it's state.
StateBooting = StateType("BOOTING")
// StateJoining indicates node is trying to join consensus.
StateJoining = StateType("JOINING")
// StateConsensus indicates node participates consensus with the other
// nodes.
StateConsensus = StateType("CONSENSUS")
// StateSyncing indicates node is syncing block.
StateSyncing = StateType("SYNCING")
// StateHandover indicates node tries to replace the existing same node.
StateHandover = StateType("HANDOVER")
// StateBroken is used when something wrong in states.
StateBroken = StateType("BROKEN")
)
func (s StateType) String() string {
return string(s)
}
func (s StateType) IsValid([]byte) error {
switch s {
case StateEmpty,
StateStopped,
StateBooting,
StateJoining,
StateConsensus,
StateSyncing,
StateHandover,
StateBroken:
return nil
default:
return util.ErrInvalid.Errorf("unknown StateType, %q", s)
}
}
type newHandler interface {
new() (handler, error)
setStates(*States)
}
type handler interface {
state() StateType
enter(from StateType, _ switchContext) (func(), error)
exit(switchContext) (func(), error)
newVoteproof(base.Voteproof) error
whenEmptyMembers()
}
func handlerLog(st handler) fmt.Stringer {
return util.Stringer(func() string {
if st == nil {
return ""
}
return st.state().String()
})
}
type switchContext interface {
next() StateType
Error() string
ok(current StateType) bool
}
type baseSwitchContext struct { //nolint:errname //...
okf func(StateType) bool
n StateType
}
func newBaseSwitchContext(next StateType, okf func(StateType) bool) baseSwitchContext {
return baseSwitchContext{
n: next,
okf: okf,
}
}
func (s baseSwitchContext) next() StateType {
return s.n
}
func (s baseSwitchContext) ok(current StateType) bool {
return s.okf(current)
}
func (s baseSwitchContext) Error() string {
return s.String()
}
func (s baseSwitchContext) String() string {
return fmt.Sprintf("state switch next=%s", s.n)
}
func (s baseSwitchContext) MarshalZerologObject(e *zerolog.Event) {
e.Stringer("next", s.n)
}
type baseErrorSwitchContext struct { //nolint:errname //...
err error
baseSwitchContext
}
func newBaseErrorSwitchContext(next StateType, err error, okf func(StateType) bool) baseErrorSwitchContext {
return baseErrorSwitchContext{
baseSwitchContext: newBaseSwitchContext(next, okf),
err: err,
}
}
func (s baseErrorSwitchContext) Error() string {
if s.err != nil {
return s.err.Error()
}
return s.String()
}
func (s baseErrorSwitchContext) Unwrap() error {
return s.err
}
func (s baseErrorSwitchContext) MarshalZerologObject(e *zerolog.Event) {
s.baseSwitchContext.MarshalZerologObject(e)
if s.err != nil {
e.Err(s.err)
}
}
func switchContextLog(sctx switchContext) *zerolog.Event {
e := zerolog.Dict()
o, ok := sctx.(zerolog.LogObjectMarshaler)
switch {
case ok:
e = e.EmbedObject(o)
default:
e = e.Stringer("next", sctx.next())
}
return e
}
func isSwitchContextError(err error) bool {
var sctx switchContext
return errors.As(err, &sctx)
}
func switchContextOKFuncNil(StateType) bool {
return true
}
func switchContextOKFuncCheckFrom(st StateType) func(StateType) bool {
return func(current StateType) bool {
return current == st
}
}