-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec_test.go
213 lines (176 loc) · 6.68 KB
/
exec_test.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
package rbft
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/axiomesh/axiom-bft/common/consensus"
"github.com/axiomesh/axiom-bft/types"
"github.com/axiomesh/axiom-kit/txpool"
)
func TestExec_handleCoreRbftEvent_batchTimerEvent(t *testing.T) {
nodes, rbfts := newBasicClusterInstance[consensus.FltTransaction, *consensus.FltTransaction]()
// start state
ev := &LocalEvent{
Service: CoreRbftService,
EventType: CoreBatchTimerEvent,
}
assert.False(t, rbfts[0].timerMgr.getTimer(batchTimer))
// abnormal state
rbfts[0].handleCoreRbftEvent(ev)
assert.False(t, rbfts[0].timerMgr.getTimer(batchTimer))
// normal
rbfts[0].setNormal()
rbfts[0].atomicOn(InConfChange)
rbfts[0].handleCoreRbftEvent(ev)
assert.True(t, rbfts[0].timerMgr.getTimer(batchTimer))
rbfts[0].atomicOff(InConfChange)
tx := newTx()
err := rbfts[0].batchMgr.requestPool.AddLocalTx(tx)
assert.Nil(t, err)
reqBatch, err := rbfts[0].batchMgr.requestPool.GenerateRequestBatch(txpool.GenBatchTimeoutEvent)
assert.Nil(t, err)
batch := &RequestBatch[consensus.FltTransaction, *consensus.FltTransaction]{
RequestHashList: reqBatch.TxHashList,
RequestList: reqBatch.TxList,
Timestamp: reqBatch.Timestamp,
LocalList: reqBatch.LocalList,
BatchHash: reqBatch.BatchHash,
}
rbfts[0].batchMgr.cacheBatch = append(rbfts[0].batchMgr.cacheBatch, batch)
rbfts[0].setNormal()
rbfts[0].handleCoreRbftEvent(ev)
assert.Equal(t, consensus.Type_PRE_PREPARE, nodes[0].broadcastMessageCache.Type)
}
func TestExec_handleCoreRbftEvent_NullRequestTimerEvent(t *testing.T) {
nodes, rbfts := newBasicClusterInstance[consensus.FltTransaction, *consensus.FltTransaction]()
// replica will send view change, primary will send null request
ev := &LocalEvent{
Service: CoreRbftService,
EventType: CoreNullRequestTimerEvent,
}
rbfts[0].handleCoreRbftEvent(ev)
rbfts[1].handleCoreRbftEvent(ev)
rbfts[2].handleCoreRbftEvent(ev)
rbfts[3].handleCoreRbftEvent(ev)
assert.Equal(t, consensus.Type_NULL_REQUEST, nodes[0].broadcastMessageCache.Type)
assert.Equal(t, consensus.Type_VIEW_CHANGE, nodes[1].broadcastMessageCache.Type)
assert.Equal(t, consensus.Type_VIEW_CHANGE, nodes[2].broadcastMessageCache.Type)
assert.Equal(t, consensus.Type_VIEW_CHANGE, nodes[3].broadcastMessageCache.Type)
}
func TestExec_handleCoreRbftEvent_CheckPoolTimerEvent(t *testing.T) {
_, rbfts := newBasicClusterInstance[consensus.FltTransaction, *consensus.FltTransaction]()
// node will restart check pool timer
ev := &LocalEvent{
Service: CoreRbftService,
EventType: CoreCheckPoolTimerEvent,
}
assert.False(t, rbfts[0].timerMgr.getTimer(checkPoolTimer))
// for abnormal node
rbfts[0].handleCoreRbftEvent(ev)
assert.True(t, rbfts[0].timerMgr.getTimer(checkPoolTimer), "abnormal node should restart check pool timer as the same as normal node")
// for normal node
rbfts[0].setNormal()
rbfts[0].handleCoreRbftEvent(ev)
assert.True(t, rbfts[0].timerMgr.getTimer(checkPoolTimer))
}
func TestExec_handleCoreRbftEvent_StateUpdatedEvent(t *testing.T) {
_, rbfts := newBasicClusterInstance[consensus.FltTransaction, *consensus.FltTransaction]()
metaS := &types.MetaState{
Height: uint64(10),
Digest: "block-number-10",
}
ss := &types.ServiceState{
MetaState: metaS,
Epoch: uint64(0),
}
checkpoint := &consensus.Checkpoint{
Epoch: uint64(0),
ExecuteState: &consensus.Checkpoint_ExecuteState{
Height: uint64(10),
Digest: "block-number-10",
},
}
ev := &LocalEvent{
Service: CoreRbftService,
EventType: CoreStateUpdatedEvent,
Event: &types.ServiceSyncState{
ServiceState: *ss,
EpochChanged: false,
},
}
assert.Equal(t, uint64(0), rbfts[1].exec.lastExec)
rbfts[1].storeMgr.highStateTarget = &stateUpdateTarget{
metaState: metaS,
checkpointSet: []*consensus.SignedCheckpoint{
{Author: 1, Checkpoint: checkpoint, Signature: []byte("sig-1")},
{Author: 2, Checkpoint: checkpoint, Signature: []byte("sig-2")},
{Author: 3, Checkpoint: checkpoint, Signature: []byte("sig-3")},
},
}
rbfts[1].handleCoreRbftEvent(ev)
assert.Equal(t, uint64(10), rbfts[1].exec.lastExec)
}
func TestExec_handleRecoveryEvent_SyncStateRspTimerEvent(t *testing.T) {
_, rbfts := newBasicClusterInstance[consensus.FltTransaction, *consensus.FltTransaction]()
ev := &LocalEvent{
Service: RecoveryService,
EventType: RecoverySyncStateRspTimerEvent,
}
assert.False(t, rbfts[1].atomicIn(InRecovery))
// for abnormal
rbfts[1].handleRecoveryEvent(ev)
assert.False(t, rbfts[1].atomicIn(InRecovery))
// for normal
rbfts[1].setNormal()
rbfts[1].handleRecoveryEvent(ev)
assert.True(t, rbfts[1].atomicIn(InRecovery))
assert.Equal(t, uint64(1), rbfts[1].chainConfig.View)
}
func TestExec_handleRecoveryEvent_SyncStateRestartTimerEvent(t *testing.T) {
_, rbfts := newBasicClusterInstance[consensus.FltTransaction, *consensus.FltTransaction]()
ev := &LocalEvent{
Service: RecoveryService,
EventType: RecoverySyncStateRestartTimerEvent,
}
assert.False(t, rbfts[1].timerMgr.getTimer(syncStateRestartTimer))
rbfts[1].handleRecoveryEvent(ev)
assert.False(t, rbfts[1].timerMgr.getTimer(syncStateRestartTimer))
}
func TestExec_handleViewChangeEvent_ViewChangeTimerEvent(t *testing.T) {
nodes, rbfts := newBasicClusterInstance[consensus.FltTransaction, *consensus.FltTransaction]()
ev := &LocalEvent{
Service: ViewChangeService,
EventType: ViewChangeTimerEvent,
}
preView := rbfts[1].chainConfig.View
rbfts[1].sendViewChange()
assert.Equal(t, consensus.Type_VIEW_CHANGE, nodes[1].broadcastMessageCache.Type)
rbfts[1].handleViewChangeEvent(ev)
assert.Equal(t, consensus.Type_VIEW_CHANGE, nodes[1].broadcastMessageCache.Type)
assert.Equal(t, preView+1, rbfts[1].chainConfig.View)
rbfts[2].handleViewChangeEvent(ev)
assert.Equal(t, consensus.Type_VIEW_CHANGE, nodes[2].broadcastMessageCache.Type)
}
func TestExec_handleViewChangeEvent_ViewChangeResendTimerEvent(t *testing.T) {
nodes, rbfts := newBasicClusterInstance[consensus.FltTransaction, *consensus.FltTransaction]()
ev := &LocalEvent{
Service: ViewChangeService,
EventType: ViewChangeResendTimerEvent,
}
rbfts[1].atomicOn(InViewChange)
rbfts[1].handleViewChangeEvent(ev)
assert.Equal(t, consensus.Type_VIEW_CHANGE, nodes[1].broadcastMessageCache.Type)
}
func TestExec_handleEpochMgrEvent_FetchCheckpointEvent(t *testing.T) {
nodes, rbfts := newBasicClusterInstance[consensus.FltTransaction, *consensus.FltTransaction]()
ev := &LocalEvent{
Service: EpochMgrService,
EventType: FetchCheckpointEvent,
}
rbfts[1].handleEpochMgrEvent(ev)
rbfts[1].epochMgr.configBatchToCheck = &types.MetaState{
Height: 10,
Digest: "block-hash-10",
}
rbfts[1].handleEpochMgrEvent(ev)
assert.Equal(t, consensus.Type_FETCH_CHECKPOINT, nodes[1].broadcastMessageCache.Type)
}