-
Notifications
You must be signed in to change notification settings - Fork 179
/
testing.go
183 lines (169 loc) · 8.09 KB
/
testing.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
package util
import (
"testing"
"github.com/dgraph-io/badger/v2"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/module/metrics"
modulemock "github.com/onflow/flow-go/module/mock"
"github.com/onflow/flow-go/module/trace"
"github.com/onflow/flow-go/state/protocol"
pbadger "github.com/onflow/flow-go/state/protocol/badger"
"github.com/onflow/flow-go/state/protocol/events"
mockprotocol "github.com/onflow/flow-go/state/protocol/mock"
"github.com/onflow/flow-go/storage"
"github.com/onflow/flow-go/storage/util"
"github.com/onflow/flow-go/utils/unittest"
)
// MockReceiptValidator returns a ReceiptValidator that accepts
// all receipts without performing any
// integrity checks.
func MockReceiptValidator() module.ReceiptValidator {
validator := &modulemock.ReceiptValidator{}
validator.On("Validate", mock.Anything).Return(nil)
validator.On("ValidatePayload", mock.Anything).Return(nil)
return validator
}
// MockBlockTimer returns BlockTimer that accepts all timestamps
// without performing any checks.
func MockBlockTimer() protocol.BlockTimer {
blockTimer := &mockprotocol.BlockTimer{}
blockTimer.On("Validate", mock.Anything, mock.Anything).Return(nil)
return blockTimer
}
// MockSealValidator returns a SealValidator that accepts
// all seals without performing any
// integrity checks, returns first seal in block as valid one
func MockSealValidator(sealsDB storage.Seals) module.SealValidator {
validator := &modulemock.SealValidator{}
validator.On("Validate", mock.Anything).Return(
func(candidate *flow.Block) *flow.Seal {
if len(candidate.Payload.Seals) > 0 {
return candidate.Payload.Seals[0]
}
last, _ := sealsDB.HighestInFork(candidate.Header.ParentID)
return last
},
func(candidate *flow.Block) error {
if len(candidate.Payload.Seals) > 0 {
return nil
}
_, err := sealsDB.HighestInFork(candidate.Header.ParentID)
return err
}).Maybe()
return validator
}
func RunWithBootstrapState(t testing.TB, rootSnapshot protocol.Snapshot, f func(*badger.DB, *pbadger.State)) {
unittest.RunWithBadgerDB(t, func(db *badger.DB) {
metrics := metrics.NewNoopCollector()
headers, _, seals, _, _, blocks, setups, commits, statuses, results := util.StorageLayer(t, db)
state, err := pbadger.Bootstrap(metrics, db, headers, seals, results, blocks, setups, commits, statuses, rootSnapshot)
require.NoError(t, err)
f(db, state)
})
}
func RunWithFullProtocolState(t testing.TB, rootSnapshot protocol.Snapshot, f func(*badger.DB, *pbadger.MutableState)) {
unittest.RunWithBadgerDB(t, func(db *badger.DB) {
metrics := metrics.NewNoopCollector()
tracer := trace.NewNoopTracer()
consumer := events.NewNoop()
headers, _, seals, index, payloads, blocks, setups, commits, statuses, results := util.StorageLayer(t, db)
state, err := pbadger.Bootstrap(metrics, db, headers, seals, results, blocks, setups, commits, statuses, rootSnapshot)
require.NoError(t, err)
receiptValidator := MockReceiptValidator()
sealValidator := MockSealValidator(seals)
mockTimer := MockBlockTimer()
fullState, err := pbadger.NewFullConsensusState(state, index, payloads, tracer, consumer, mockTimer, receiptValidator, sealValidator)
require.NoError(t, err)
f(db, fullState)
})
}
func RunWithFullProtocolStateAndMetrics(t testing.TB, rootSnapshot protocol.Snapshot, metrics module.ComplianceMetrics, f func(*badger.DB, *pbadger.MutableState)) {
unittest.RunWithBadgerDB(t, func(db *badger.DB) {
tracer := trace.NewNoopTracer()
consumer := events.NewNoop()
headers, _, seals, index, payloads, blocks, setups, commits, statuses, results := util.StorageLayer(t, db)
state, err := pbadger.Bootstrap(metrics, db, headers, seals, results, blocks, setups, commits, statuses, rootSnapshot)
require.NoError(t, err)
receiptValidator := MockReceiptValidator()
sealValidator := MockSealValidator(seals)
mockTimer := MockBlockTimer()
fullState, err := pbadger.NewFullConsensusState(state, index, payloads, tracer, consumer, mockTimer, receiptValidator, sealValidator)
require.NoError(t, err)
f(db, fullState)
})
}
func RunWithFullProtocolStateAndValidator(t testing.TB, rootSnapshot protocol.Snapshot, validator module.ReceiptValidator, f func(*badger.DB, *pbadger.MutableState)) {
unittest.RunWithBadgerDB(t, func(db *badger.DB) {
metrics := metrics.NewNoopCollector()
tracer := trace.NewNoopTracer()
consumer := events.NewNoop()
headers, _, seals, index, payloads, blocks, setups, commits, statuses, results := util.StorageLayer(t, db)
state, err := pbadger.Bootstrap(metrics, db, headers, seals, results, blocks, setups, commits, statuses, rootSnapshot)
require.NoError(t, err)
sealValidator := MockSealValidator(seals)
mockTimer := MockBlockTimer()
fullState, err := pbadger.NewFullConsensusState(state, index, payloads, tracer, consumer, mockTimer, validator, sealValidator)
require.NoError(t, err)
f(db, fullState)
})
}
func RunWithFollowerProtocolState(t testing.TB, rootSnapshot protocol.Snapshot, f func(*badger.DB, *pbadger.FollowerState)) {
unittest.RunWithBadgerDB(t, func(db *badger.DB) {
metrics := metrics.NewNoopCollector()
tracer := trace.NewNoopTracer()
consumer := events.NewNoop()
headers, _, seals, index, payloads, blocks, setups, commits, statuses, results := util.StorageLayer(t, db)
state, err := pbadger.Bootstrap(metrics, db, headers, seals, results, blocks, setups, commits, statuses, rootSnapshot)
require.NoError(t, err)
mockTimer := MockBlockTimer()
followerState, err := pbadger.NewFollowerState(state, index, payloads, tracer, consumer, mockTimer)
require.NoError(t, err)
f(db, followerState)
})
}
func RunWithFullProtocolStateAndConsumer(t testing.TB, rootSnapshot protocol.Snapshot, consumer protocol.Consumer, f func(*badger.DB, *pbadger.MutableState)) {
unittest.RunWithBadgerDB(t, func(db *badger.DB) {
metrics := metrics.NewNoopCollector()
tracer := trace.NewNoopTracer()
headers, _, seals, index, payloads, blocks, setups, commits, statuses, results := util.StorageLayer(t, db)
state, err := pbadger.Bootstrap(metrics, db, headers, seals, results, blocks, setups, commits, statuses, rootSnapshot)
require.NoError(t, err)
receiptValidator := MockReceiptValidator()
sealValidator := MockSealValidator(seals)
mockTimer := MockBlockTimer()
fullState, err := pbadger.NewFullConsensusState(state, index, payloads, tracer, consumer, mockTimer, receiptValidator, sealValidator)
require.NoError(t, err)
f(db, fullState)
})
}
func RunWithFullProtocolStateAndMetricsAndConsumer(t testing.TB, rootSnapshot protocol.Snapshot, metrics module.ComplianceMetrics, consumer protocol.Consumer, f func(*badger.DB, *pbadger.MutableState)) {
unittest.RunWithBadgerDB(t, func(db *badger.DB) {
tracer := trace.NewNoopTracer()
headers, _, seals, index, payloads, blocks, setups, commits, statuses, results := util.StorageLayer(t, db)
state, err := pbadger.Bootstrap(metrics, db, headers, seals, results, blocks, setups, commits, statuses, rootSnapshot)
require.NoError(t, err)
receiptValidator := MockReceiptValidator()
sealValidator := MockSealValidator(seals)
mockTimer := MockBlockTimer()
fullState, err := pbadger.NewFullConsensusState(state, index, payloads, tracer, consumer, mockTimer, receiptValidator, sealValidator)
require.NoError(t, err)
f(db, fullState)
})
}
func RunWithFollowerProtocolStateAndHeaders(t testing.TB, rootSnapshot protocol.Snapshot, f func(*badger.DB, *pbadger.FollowerState, storage.Headers, storage.Index)) {
unittest.RunWithBadgerDB(t, func(db *badger.DB) {
metrics := metrics.NewNoopCollector()
tracer := trace.NewNoopTracer()
consumer := events.NewNoop()
headers, _, seals, index, payloads, blocks, setups, commits, statuses, results := util.StorageLayer(t, db)
state, err := pbadger.Bootstrap(metrics, db, headers, seals, results, blocks, setups, commits, statuses, rootSnapshot)
require.NoError(t, err)
mockTimer := MockBlockTimer()
followerState, err := pbadger.NewFollowerState(state, index, payloads, tracer, consumer, mockTimer)
require.NoError(t, err)
f(db, followerState, headers, index)
})
}