-
Notifications
You must be signed in to change notification settings - Fork 2
/
automation.go
267 lines (227 loc) · 6.39 KB
/
automation.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package automation
import (
"encoding/json"
"fmt"
"github.com/fibonacci-chain/fbc/libs/tendermint/libs/log"
"github.com/spf13/viper"
"io/ioutil"
"sync"
"time"
)
var (
tlog log.Logger
enableRoleTest bool
roleAction map[string]*action
once sync.Once
)
const (
ConsensusRole = "consensus-role"
ConsensusTestcase = "consensus-testcase"
)
func init() {
once.Do(func() {
roleAction = make(map[string]*action)
})
}
type round struct {
Round int64
PreVote map[string]bool // true vote nil, false default vote
PreCommit map[string]bool // true vote nil, false default vote
PrevotesMaj23 map[string]bool // true not received +2/3 prevotes, false actual received
PrecommitsMaj23 map[string]bool // true not received +2/3 precommits, false actual received
PreRun map[string]int // int => control prerun sleep time
AddBlockPart map[string]int // int => control sleep time before receiver a block
RecvBlock map[string]bool // true not received proposed block, false actual received
Disconnect map[string]int // int => control consensus reactor sleep time
}
type action struct {
preVote bool // true vote nil, false default vote
preCommit bool // true vote nil, false default vote
prevotesMaj23 bool // true not received +2/3 prevotes, false actual received
precommitsMaj23 bool // true not received +2/3 precommits, false actual received
preRunWait int // control prerun sleep time
addBlockPartWait int // control sleep time before receiver a block
recvBlock bool // true not received proposed block, false actual received
disconnect int // int => control consensus reactor sleep time
}
func LoadTestCase(log log.Logger) {
confFilePath := viper.GetString(ConsensusTestcase)
if len(confFilePath) == 0 {
return
}
tlog = log
content, err := ioutil.ReadFile(confFilePath)
if err != nil {
panic(fmt.Sprintf("read file : %s fail err : %s", confFilePath, err))
}
confTmp := make(map[string][]round)
err = json.Unmarshal(content, &confTmp)
if err != nil {
panic(fmt.Sprintf("json Unmarshal err : %s", err))
}
enableRoleTest = true
log.Info("Load consensus test case", "file", confFilePath, "err", err, "confTmp", confTmp)
role := viper.GetString(ConsensusRole)
for height, roundEvents := range confTmp {
if _, ok := roleAction[height]; !ok {
for _, event := range roundEvents {
act := &action{}
act.preVote = event.PreVote[role]
act.preCommit = event.PreCommit[role]
act.prevotesMaj23 = event.PrevotesMaj23[role]
act.precommitsMaj23 = event.PrecommitsMaj23[role]
act.preRunWait = event.PreRun[role]
act.addBlockPartWait = event.AddBlockPart[role]
act.recvBlock = event.RecvBlock[role]
act.disconnect = event.Disconnect[role]
roleAction[fmt.Sprintf("%s-%d", height, event.Round)] = act
}
}
}
}
func PrevoteNil(height int64, round int) bool {
if !enableRoleTest {
return false
}
act, ok := roleAction[actionKey(height, round)]
if ok {
tlog.Info("PrevoteNil", "height", height, "round", round, "act", act.preVote)
return act.preVote
}
return false
}
func PrecommitNil(height int64, round int) bool {
if !enableRoleTest {
return false
}
act, ok := roleAction[actionKey(height, round)]
if ok {
tlog.Info("PrecommitNil", "height", height, "round", round, "act", act.preCommit)
return act.preCommit
}
return false
}
func PrevotesNotMaj23(height int64, round int) bool {
if !enableRoleTest {
return false
}
act, ok := roleAction[actionKey(height, round)]
if ok && act.prevotesMaj23 {
tlog.Info("PrecommitsNotMaj23.", "height", height, "round", round)
return true
}
return false
}
func PrecommitsNotMaj23(height int64, round int) bool {
if !enableRoleTest {
return false
}
act, ok := roleAction[actionKey(height, round)]
if ok && act.precommitsMaj23 {
tlog.Info("PrecommitsNotMaj23.", "height", height, "round", round)
return true
}
return false
}
func PrerunTimeOut(height int64, round int) {
if !enableRoleTest {
return
}
if act, ok := roleAction[actionKey(height, round)]; ok {
timeSleep := act.preRunWait
time.Sleep(time.Duration(timeSleep) * time.Second)
}
}
func AddBlockTimeOut(height int64, round int) {
if !enableRoleTest {
return
}
if act, ok := roleAction[actionKey(height, round)]; ok {
timeSleep := act.addBlockPartWait
time.Sleep(time.Duration(timeSleep) * time.Second)
}
}
func BlockIsNotCompleted(height int64, round int) bool {
if !enableRoleTest {
return false
}
act, ok := roleAction[actionKey(height, round)]
if ok && act.recvBlock {
tlog.Info("BlockIsNotCompleted.", "height", height, "round", round)
return true
}
return false
}
func NetworkDisconnect(height int64, round int) bool {
if !enableRoleTest {
return false
}
if act, ok := roleAction[actionKey(height, round)]; ok {
timeSleep := act.disconnect
if timeSleep > 0 {
sleepTimer := SleepTimerInstance()
return sleepTimer.shouldSleep(timeSleep, height, round)
}
}
return false
}
func actionKey(height int64, round int) string {
return fmt.Sprintf("%d-%d", height, round)
}
//----------------------------------------------------
type SleepTimer struct {
timer *time.Timer
duration int
isSleeping bool
mtx sync.RWMutex
height int64
round int
}
var (
_sleepTimerInstance *SleepTimer = nil
SleepTimerOnce sync.Once
)
func SleepTimerInstance() *SleepTimer {
SleepTimerOnce.Do(func() {
if _sleepTimerInstance == nil {
_sleepTimerInstance = &SleepTimer{}
_sleepTimerInstance.timer = time.NewTimer(0)
_sleepTimerInstance.timer.Stop()
_sleepTimerInstance.mtx = sync.RWMutex{}
}
})
return _sleepTimerInstance
}
func (st *SleepTimer) shouldSleep(d int, height int64, round int) bool {
if d <= 0 {
return false
}
if st.height != height || st.round != round {
tlog.Info("NetworkDisconnect.", "sleep", d, "height", height, "round", round)
st.height = height
st.round = round
st.doSleep(d)
}
return st.isSleeping
}
func (st *SleepTimer) doSleep(d int) {
st.isSleeping = true
defer func() {
st.isSleeping = false
}()
if !st.timer.Stop() { // Stop() returns false if it was already fired or was stopped
select {
case <-st.timer.C:
default:
}
}
st.timer.Reset(time.Duration(d) * time.Second)
for {
select {
case <-st.timer.C:
tlog.Info("NetworkDisconnect finished.", "sleep", d, "height", st.height, "round", st.round)
st.timer.Stop()
break
}
}
}