-
Notifications
You must be signed in to change notification settings - Fork 13
/
harness.go
254 lines (213 loc) · 9.72 KB
/
harness.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
// Copyright 2019 the orbs-network-go authors
// This file is part of the orbs-network-go library in the Orbs project.
//
// This source code is licensed under the MIT license found in the LICENSE file in the root directory of this source tree.
// The above notice should be included in all copies or substantial portions of the software.
package internodesync
import (
"context"
"github.com/orbs-network/go-mock"
"github.com/orbs-network/orbs-network-go/instrumentation/metric"
"github.com/orbs-network/orbs-network-go/synchronization"
"github.com/orbs-network/orbs-network-go/test"
testKeys "github.com/orbs-network/orbs-network-go/test/crypto/keys"
"github.com/orbs-network/orbs-spec/types/go/primitives"
"github.com/orbs-network/orbs-spec/types/go/services"
"github.com/orbs-network/orbs-spec/types/go/services/gossiptopics"
"github.com/orbs-network/scribe/log"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"testing"
"time"
)
type blockSyncConfigForTests struct {
nodeAddress primitives.NodeAddress
batchSize uint32
noCommit time.Duration
collectResponses time.Duration
collectChunks time.Duration
referenceDistance time.Duration
descendingEnabled bool
}
func (c *blockSyncConfigForTests) NodeAddress() primitives.NodeAddress {
return c.nodeAddress
}
func (c *blockSyncConfigForTests) BlockSyncNumBlocksInBatch() uint32 {
return c.batchSize
}
func (c *blockSyncConfigForTests) BlockSyncNoCommitInterval() time.Duration {
return c.noCommit
}
func (c *blockSyncConfigForTests) BlockSyncCollectResponseTimeout() time.Duration {
return c.collectResponses
}
func (c *blockSyncConfigForTests) BlockSyncCollectChunksTimeout() time.Duration {
return c.collectChunks
}
func (c *blockSyncConfigForTests) BlockSyncReferenceMaxAllowedDistance() time.Duration {
return c.referenceDistance
}
func (c *blockSyncConfigForTests) BlockSyncDescendingEnabled() bool {
return c.descendingEnabled
}
func newDefaultBlockSyncConfigForTests() *blockSyncConfigForTests {
return &blockSyncConfigForTests{
nodeAddress: testKeys.EcdsaSecp256K1KeyPairForTests(1).NodeAddress(),
batchSize: 10,
noCommit: 3 * time.Millisecond,
collectResponses: 3 * time.Millisecond,
collectChunks: 3 * time.Millisecond,
referenceDistance: 100 * time.Second,
descendingEnabled: true,
}
}
type blockSyncHarness struct {
factory *stateFactory
config *blockSyncConfigForTests
gossip *gossiptopics.MockBlockSync
storage *blockSyncStorageMock
logger log.Logger
metricFactory metric.Factory
}
func newBlockSyncHarnessWithTimers(
logger log.Logger,
createCollectTimeoutTimer func() *synchronization.Timer,
createNoCommitTimeoutTimer func() *synchronization.Timer,
createWaitForChunksTimeoutTimer func() *synchronization.Timer,
) *blockSyncHarness {
cfg := newDefaultBlockSyncConfigForTests()
gossip := &gossiptopics.MockBlockSync{}
storage := &blockSyncStorageMock{}
conduit := make(blockSyncConduit)
metricFactory := metric.NewRegistry()
return &blockSyncHarness{
logger: logger,
factory: NewStateFactoryWithTimers(cfg, gossip, storage, conduit, createCollectTimeoutTimer, createNoCommitTimeoutTimer, createWaitForChunksTimeoutTimer, logger, metricFactory),
config: cfg,
gossip: gossip,
storage: storage,
metricFactory: metricFactory,
}
}
func newBlockSyncHarness(logger log.Logger) *blockSyncHarness {
return newBlockSyncHarnessWithTimers(logger, nil, nil, nil)
}
func newBlockSyncHarnessWithCollectResponsesTimer(logger log.Logger, createTimer func() *synchronization.Timer) *blockSyncHarness {
return newBlockSyncHarnessWithTimers(logger, createTimer, nil, nil)
}
func newBlockSyncHarnessWithManualNoCommitTimeoutTimer(logger log.Logger, createTimer func() *synchronization.Timer) *blockSyncHarness {
return newBlockSyncHarnessWithTimers(logger, nil, createTimer, nil)
}
func newBlockSyncHarnessWithManualWaitForChunksTimeoutTimer(logger log.Logger, createTimer func() *synchronization.Timer) *blockSyncHarness {
return newBlockSyncHarnessWithTimers(logger, nil, nil, createTimer)
}
func (h *blockSyncHarness) withWaitForChunksTimeout(d time.Duration) *blockSyncHarness {
h.config.collectChunks = d
return h
}
func (h *blockSyncHarness) withNodeAddress(address primitives.NodeAddress) *blockSyncHarness {
h.config.nodeAddress = address
return h
}
func (h *blockSyncHarness) withBatchSize(size uint32) *blockSyncHarness {
h.config.batchSize = size
return h
}
func (h *blockSyncHarness) withReferenceDistance(d time.Duration) *blockSyncHarness {
h.config.referenceDistance = d
return h
}
func (h *blockSyncHarness) withDescendingEnabled(isEnabled bool) *blockSyncHarness {
h.config.descendingEnabled = isEnabled
return h
}
func (h *blockSyncHarness) expectSyncOnStart() {
h.expectUpdateConsensusAlgosAboutLastCommittedBlockInLocalPersistence(10)
h.expectBroadcastOfBlockAvailabilityRequest()
}
func (h *blockSyncHarness) eventuallyVerifyMocks(t *testing.T, times int) {
err := test.EventuallyVerify(test.EVENTUALLY_ACCEPTANCE_TIMEOUT*time.Duration(times), h.gossip)
require.NoError(t, err)
}
func (h *blockSyncHarness) consistentlyVerifyMocks(t *testing.T, times int, message string) {
err := test.ConsistentlyVerify(test.EVENTUALLY_ACCEPTANCE_TIMEOUT*time.Duration(times), h.gossip)
require.NoError(t, err, message)
}
func (h *blockSyncHarness) verifyMocks(t *testing.T) {
ok, err := mock.VerifyMocks(h.storage, h.gossip)
require.NoError(t, err)
require.True(t, ok)
}
func (h *blockSyncHarness) processStateInBackgroundAndWaitUntilFinished(ctx context.Context, state syncState, whileStateIsProcessing func()) syncState {
var nextState syncState
stateProcessingFinished := make(chan bool)
go func() {
nextState = state.processState(ctx)
stateProcessingFinished <- true
}()
whileStateIsProcessing()
<-stateProcessingFinished
return nextState
}
func (h *blockSyncHarness) expectLastCommittedBlockHeightQueryFromStorage(expectedHeight int) {
out := &services.GetLastCommittedBlockHeightOutput{
LastCommittedBlockHeight: primitives.BlockHeight(expectedHeight),
LastCommittedBlockTimestamp: primitives.TimestampNano(time.Now().UnixNano()),
}
h.storage.When("GetLastCommittedBlockHeight", mock.Any, mock.Any).Return(out, nil).Times(1)
}
func (h *blockSyncHarness) expectUpdateConsensusAlgosAboutLastCommittedBlockInLocalPersistence(expectedHeight int) {
h.storage.When("UpdateConsensusAlgosAboutLastCommittedBlockInLocalPersistence", mock.Any).Times(1)
h.expectLastCommittedBlockHeightQueryFromStorage(expectedHeight)
}
func (h *blockSyncHarness) expectBroadcastOfBlockAvailabilityRequestToFail() {
h.storage.When("GetSyncState").Return(nil).Times(1)
h.gossip.When("BroadcastBlockAvailabilityRequest", mock.Any, mock.Any).Return(nil, errors.New("gossip failure")).Times(1)
}
func (h *blockSyncHarness) expectBroadcastOfBlockAvailabilityRequest() {
h.storage.When("GetSyncState").Return(nil).Times(1)
h.gossip.When("BroadcastBlockAvailabilityRequest", mock.Any, mock.Any).Return(nil, nil).Times(1)
}
func (h *blockSyncHarness) verifyBroadcastOfBlockAvailabilityRequest(t *testing.T) {
require.NoError(t, test.EventuallyVerify(50*time.Millisecond, h.gossip), "broadcast should be sent")
}
func (h *blockSyncHarness) expectBlockValidationQueriesFromStorage(numExpectedBlocks int) {
h.storage.When("GetSyncState").Return( nil).Times(1)
h.storage.When("GetBlock", mock.Any).Return( nil).Times(1)
h.storage.When("ValidateBlockForCommit", mock.Any, mock.Any).Return(nil, nil).Times(numExpectedBlocks)
}
func (h *blockSyncHarness) expectBlockValidationQueriesFromStorageAndFailLastValidation(numExpectedBlocks int, expectedFailedBlockHeight primitives.BlockHeight) {
h.storage.When("GetSyncState").Return( nil).Times(1)
h.storage.When("GetBlock", mock.Any).Return( nil).Times(1)
h.storage.When("ValidateBlockForCommit", mock.Any, mock.Any).Call(func(ctx context.Context, input *services.ValidateBlockForCommitInput) (*services.ValidateBlockForCommitOutput, error) {
if input.BlockPair.ResultsBlock.Header.BlockHeight().Equal(expectedFailedBlockHeight) {
return nil, errors.Errorf("failed to validate block #%d", expectedFailedBlockHeight)
}
return nil, nil
}).Times(numExpectedBlocks)
}
func (h *blockSyncHarness) expectBlockChunkRangeValidationFailure(syncState SyncState, numValidations int) {
h.storage.When("GetSyncState").Return( syncState).Times(numValidations)
}
func (h *blockSyncHarness) expectBlockCommitsToStorage(numExpectedBlocks int) {
outCommit := &services.CommitBlockOutput{}
h.storage.When("NodeSyncCommitBlock", mock.Any, mock.Any).Return(outCommit, nil).Times(numExpectedBlocks)
h.storage.When("UpdateConsensusAlgosAboutLastCommittedBlockInLocalPersistence", mock.Any)
}
func (h *blockSyncHarness) expectBlockCommitsToStorageAndFailLastCommit(numExpectedBlocks int, expectedFirstBlockHeight primitives.BlockHeight) {
h.storage.When("UpdateConsensusAlgosAboutLastCommittedBlockInLocalPersistence", mock.Any)
h.storage.When("NodeSyncCommitBlock", mock.Any, mock.Any).Call(func(ctx context.Context, input *services.CommitBlockInput) (*services.CommitBlockOutput, error) {
if input.BlockPair.ResultsBlock.Header.BlockHeight().Equal(expectedFirstBlockHeight + primitives.BlockHeight(numExpectedBlocks-1)) {
return nil, errors.Errorf("failed to commit block #%d", numExpectedBlocks)
}
return nil, nil
}).Times(numExpectedBlocks)
}
func (h *blockSyncHarness) expectSendingOfBlockSyncRequest() {
h.storage.When("GetSyncState").Return(nil).Times(1)
h.gossip.When("SendBlockSyncRequest", mock.Any, mock.Any).Return(nil, nil).Times(1)
}
func (h *blockSyncHarness) expectSendingOfBlockSyncRequestToFail() {
h.storage.When("GetSyncState").Return(nil).Times(1)
h.gossip.When("SendBlockSyncRequest", mock.Any, mock.Any).Return(nil, errors.New("gossip failure")).Times(1)
}