-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexport_test.go
More file actions
226 lines (180 loc) · 6.47 KB
/
export_test.go
File metadata and controls
226 lines (180 loc) · 6.47 KB
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
package sync
import (
"github.com/klever-io/klever-go/core"
"github.com/klever-io/klever-go/core/consensus"
"github.com/klever-io/klever-go/core/process"
"github.com/klever-io/klever-go/data"
"github.com/klever-io/klever-go/data/block"
)
// BaseBootstrap is an alias so tests in the sync_test package can refer to
// the unexported baseBootstrap type by name.
type BaseBootstrap = baseBootstrap
// NewBaseBootstrapForKLC1920Test builds a minimal baseBootstrap wired only
// with the dependencies computeNodeState needs to exercise the KLC-1920
// gossip-ahead-of-probable branch. Internal-only helper, not for production.
func NewBaseBootstrapForKLC1920Test(
forkDetector process.ForkDetector,
chainHandler data.ChainHandler,
slotManager consensus.SlotManager,
networkWatcher process.NetworkConnectionWatcher,
statusHandler core.AppStatusHandler,
) *BaseBootstrap {
return &baseBootstrap{
forkDetector: forkDetector,
chainHandler: chainHandler,
slotManager: slotManager,
networkWatcher: networkWatcher,
statusHandler: statusHandler,
syncStateListeners: []func(bool){},
hasStarted: true,
}
}
func (boot *baseBootstrap) IsNodeSynchronized() bool {
boot.mutNodeState.RLock()
defer boot.mutNodeState.RUnlock()
return boot.isNodeSynchronized
}
func (boot *baseBootstrap) HasLastBlock() bool {
boot.mutNodeState.RLock()
defer boot.mutNodeState.RUnlock()
return boot.hasLastBlock
}
func (boot *MetaBootstrap) ReceivedHeaders(header data.HeaderHandler, key []byte) {
boot.processReceivedHeader(header, key)
}
func (boot *MetaBootstrap) RollBack(revertUsingForkNonce bool) error {
return boot.rollBack(revertUsingForkNonce)
}
func (bfd *baseForkDetector) GetHeaders(nonce uint64) []*headerInfo {
bfd.mutHeaders.Lock()
defer bfd.mutHeaders.Unlock()
headers := bfd.headers[nonce]
if headers == nil {
return nil
}
newHeaders := make([]*headerInfo, len(headers))
copy(newHeaders, headers)
return newHeaders
}
func (bfd *baseForkDetector) LastCheckpointNonce() uint64 {
return bfd.lastCheckpoint().nonce
}
func (bfd *baseForkDetector) LastCheckpointSlot() uint64 {
return bfd.lastCheckpoint().slot
}
func (bfd *baseForkDetector) SetFinalCheckpoint(nonce uint64, slot uint64, hash []byte) {
bfd.setFinalCheckpoint(&checkpointInfo{nonce: nonce, slot: slot, hash: hash})
}
func (bfd *baseForkDetector) FinalCheckpointNonce() uint64 {
return bfd.finalCheckpoint().nonce
}
func (bfd *baseForkDetector) FinalCheckpointSlot() uint64 {
return bfd.finalCheckpoint().slot
}
func (bfd *baseForkDetector) CheckBlockValidity(header *block.Block, headerHash []byte) error {
return bfd.checkBlockBasicValidity(header, headerHash)
}
func (bfd *baseForkDetector) RemovePastHeaders() {
bfd.removePastHeaders()
}
func (bfd *baseForkDetector) RemoveInvalidReceivedHeaders() {
bfd.removeInvalidReceivedHeaders()
}
func (bfd *baseForkDetector) ComputeProbableHighestNonce() uint64 {
return bfd.computeProbableHighestNonce()
}
func (bfd *baseForkDetector) IsConsensusStuck() bool {
return bfd.isConsensusStuck()
}
func (hi *headerInfo) Hash() []byte {
return hi.hash
}
func (hi *headerInfo) GetBlockHeaderState() process.BlockHeaderState {
return hi.state
}
func (boot *MetaBootstrap) NotifySyncStateListeners() {
isNodeSynchronized := boot.GetNodeState() == core.NsSynchronized
boot.notifySyncStateListeners(isNodeSynchronized)
}
func (boot *MetaBootstrap) SyncStateListeners() []func(bool) {
return boot.syncStateListeners
}
func (boot *MetaBootstrap) SetForkNonce(nonce uint64) {
boot.forkInfo.Nonce = nonce
}
func (boot *MetaBootstrap) IsForkDetected() bool {
return boot.forkInfo.IsDetected
}
func (boot *MetaBootstrap) GetNotarizedInfo(
lastNotarized map[uint32]*HdrInfo,
finalNotarized map[uint32]*HdrInfo,
blockWithLastNotarized map[uint32]uint64,
blockWithFinalNotarized map[uint32]uint64,
startNonce uint64,
) *notarizedInfo {
return ¬arizedInfo{
lastNotarized: lastNotarized,
finalNotarized: finalNotarized,
blockWithLastNotarized: blockWithLastNotarized,
blockWithFinalNotarized: blockWithFinalNotarized,
startNonce: startNonce,
}
}
func (boot *baseBootstrap) ProcessReceivedHeader(headerHandler data.HeaderHandler, headerHash []byte) {
boot.processReceivedHeader(headerHandler, headerHash)
}
func (bfd *baseForkDetector) IsHeaderReceivedTooLate(header data.HeaderHandler, state process.BlockHeaderState, finality int64) bool {
return bfd.isHeaderReceivedTooLate(header, state, finality)
}
func (bfd *baseForkDetector) SetProbableHighestNonce(nonce uint64) {
bfd.setProbableHighestNonce(nonce)
}
func (bfd *baseForkDetector) AddCheckPoint(slot uint64, nonce uint64, hash []byte) {
bfd.addCheckpoint(&checkpointInfo{slot: slot, nonce: nonce, hash: hash})
}
func (bfd *baseForkDetector) ComputeGenesisTimeFromHeader(headerHandler data.HeaderHandler) int64 {
return bfd.computeGenesisTimeFromHeader(headerHandler)
}
func (boot *baseBootstrap) InitNotarizedMap() map[uint32]*HdrInfo {
return make(map[uint32]*HdrInfo)
}
func (boot *baseBootstrap) SetNotarizedMap(notarizedMap map[uint32]*HdrInfo, shardId uint32, nonce uint64, hash []byte) {
hdrInfo, ok := notarizedMap[shardId]
if !ok {
notarizedMap[shardId] = &HdrInfo{Nonce: nonce, Hash: hash}
return
}
hdrInfo.Nonce = nonce
hdrInfo.Hash = hash
}
func (boot *baseBootstrap) SetNodeStateCalculated(state bool) {
boot.mutNodeState.Lock()
boot.isNodeStateCalculated = state
boot.mutNodeState.Unlock()
}
func (boot *baseBootstrap) ComputeNodeState() {
boot.computeNodeState()
}
func (boot *baseBootstrap) DoJobOnSyncBlockFail(headerHandler data.HeaderHandler, err error) {
boot.doJobOnSyncBlockFail(headerHandler, err)
}
func (boot *baseBootstrap) SetNumSyncedWithErrorsForNonce(nonce uint64, numSyncedWithErrors uint32) {
boot.mutNonceSyncedWithErrors.Lock()
boot.mapNonceSyncedWithErrors[nonce] = numSyncedWithErrors
boot.mutNonceSyncedWithErrors.Unlock()
}
func (boot *baseBootstrap) GetNumSyncedWithErrorsForNonce(nonce uint64) uint32 {
boot.mutNonceSyncedWithErrors.RLock()
numSyncedWithErrors := boot.mapNonceSyncedWithErrors[nonce]
boot.mutNonceSyncedWithErrors.RUnlock()
return numSyncedWithErrors
}
func (boot *baseBootstrap) GetMapNonceSyncedWithErrorsLen() int {
boot.mutNonceSyncedWithErrors.RLock()
mapNonceSyncedWithErrorsLen := len(boot.mapNonceSyncedWithErrors)
boot.mutNonceSyncedWithErrors.RUnlock()
return mapNonceSyncedWithErrorsLen
}
func (boot *baseBootstrap) CleanNoncesSyncedWithErrorsBehindFinal() {
boot.cleanNoncesSyncedWithErrorsBehindFinal()
}