@@ -15,6 +15,7 @@ import (
15
15
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
16
16
"github.com/nspcc-dev/neo-go/pkg/wallet"
17
17
"github.com/stretchr/testify/require"
18
+ "go.uber.org/zap"
18
19
"go.uber.org/zap/zaptest"
19
20
)
20
21
57
58
standByCommittee []string
58
59
)
59
60
61
+ // Options contains parameters to customize parameters of the test chain.
62
+ type Options struct {
63
+ // Logger allows to customize logging performed by the test chain.
64
+ // If Logger is not set, zaptest.Logger will be used with default configuration.
65
+ Logger * zap.Logger
66
+ // BlockchainConfigHook function is sort of visitor pattern for blockchain configuration.
67
+ // It takes in the default configuration as an argument and can perform any adjustments in it.
68
+ BlockchainConfigHook func (* config.Blockchain )
69
+ // Store allows to customize storage for blockchain data.
70
+ // If Store is not set, MemoryStore is used by default.
71
+ Store storage.Store
72
+ // If SkipRun is false, then the blockchain will be started (if its' construction
73
+ // has succeeded) and will be registered for cleanup when the test completes.
74
+ // If SkipRun is true, it is caller's responsibility to call Run before using
75
+ // the chain and to properly Close the chain when done.
76
+ SkipRun bool
77
+ }
78
+
60
79
func init () {
61
80
committeeAcc , _ = wallet .NewAccountFromWIF (singleValidatorWIF )
62
81
pubs := keys.PublicKeys {committeeAcc .PublicKey ()}
@@ -138,7 +157,21 @@ func NewSingleWithCustomConfig(t testing.TB, f func(*config.Blockchain)) (*core.
138
157
// responsibility to do that before using the chain and
139
158
// to properly Close the chain when done.
140
159
func NewSingleWithCustomConfigAndStore (t testing.TB , f func (cfg * config.Blockchain ), st storage.Store , run bool ) (* core.Blockchain , neotest.Signer ) {
141
- var cfg = config.Blockchain {
160
+ return NewSingleWithOptions (t , & Options {
161
+ BlockchainConfigHook : f ,
162
+ Store : st ,
163
+ SkipRun : ! run ,
164
+ })
165
+ }
166
+
167
+ // NewSingleWithOptions creates a new blockchain instance with a single validator
168
+ // using specified options.
169
+ func NewSingleWithOptions (t testing.TB , options * Options ) (* core.Blockchain , neotest.Signer ) {
170
+ if options == nil {
171
+ options = & Options {}
172
+ }
173
+
174
+ cfg := config.Blockchain {
142
175
ProtocolConfiguration : config.ProtocolConfiguration {
143
176
Magic : netmode .UnitTestNet ,
144
177
MaxTraceableBlocks : MaxTraceableBlocks ,
@@ -148,17 +181,23 @@ func NewSingleWithCustomConfigAndStore(t testing.TB, f func(cfg *config.Blockcha
148
181
VerifyTransactions : true ,
149
182
},
150
183
}
184
+ if options .BlockchainConfigHook != nil {
185
+ options .BlockchainConfigHook (& cfg )
186
+ }
151
187
152
- if f != nil {
153
- f (& cfg )
188
+ store := options .Store
189
+ if store == nil {
190
+ store = storage .NewMemoryStore ()
154
191
}
155
- if st == nil {
156
- st = storage .NewMemoryStore ()
192
+
193
+ logger := options .Logger
194
+ if logger == nil {
195
+ logger = zaptest .NewLogger (t )
157
196
}
158
- log := zaptest . NewLogger ( t )
159
- bc , err := core .NewBlockchain (st , cfg , log )
197
+
198
+ bc , err := core .NewBlockchain (store , cfg , logger )
160
199
require .NoError (t , err )
161
- if run {
200
+ if ! options . SkipRun {
162
201
go bc .Run ()
163
202
t .Cleanup (bc .Close )
164
203
}
@@ -193,10 +232,33 @@ func NewMultiWithCustomConfigAndStore(t testing.TB, f func(*config.Blockchain),
193
232
return bc , validator , committee
194
233
}
195
234
235
+ // NewMultiWithOptions creates a new blockchain instance with four validators and six
236
+ // committee members. Otherwise, it does not differ much from NewSingle. The
237
+ // second value returned contains the validators Signer, the third -- the committee one.
238
+ func NewMultiWithOptions (t testing.TB , options * Options ) (* core.Blockchain , neotest.Signer , neotest.Signer ) {
239
+ bc , validator , committee , err := NewMultiWithOptionsNoCheck (t , options )
240
+ require .NoError (t , err )
241
+ return bc , validator , committee
242
+ }
243
+
196
244
// NewMultiWithCustomConfigAndStoreNoCheck is similar to NewMultiWithCustomConfig,
197
245
// but do not perform Blockchain run and do not check Blockchain constructor error.
198
246
func NewMultiWithCustomConfigAndStoreNoCheck (t testing.TB , f func (* config.Blockchain ), st storage.Store ) (* core.Blockchain , neotest.Signer , neotest.Signer , error ) {
199
- var cfg = config.Blockchain {
247
+ return NewMultiWithOptionsNoCheck (t , & Options {
248
+ BlockchainConfigHook : f ,
249
+ Store : st ,
250
+ SkipRun : true ,
251
+ })
252
+ }
253
+
254
+ // NewMultiWithOptionsNoCheck is similar to NewMultiWithOptions, but does not verify blockchain constructor error.
255
+ // It will start blockchain only if construction has completed successfully.
256
+ func NewMultiWithOptionsNoCheck (t testing.TB , options * Options ) (* core.Blockchain , neotest.Signer , neotest.Signer , error ) {
257
+ if options == nil {
258
+ options = & Options {}
259
+ }
260
+
261
+ cfg := config.Blockchain {
200
262
ProtocolConfiguration : config.ProtocolConfiguration {
201
263
Magic : netmode .UnitTestNet ,
202
264
MaxTraceableBlocks : MaxTraceableBlocks ,
@@ -206,14 +268,24 @@ func NewMultiWithCustomConfigAndStoreNoCheck(t testing.TB, f func(*config.Blockc
206
268
VerifyTransactions : true ,
207
269
},
208
270
}
209
- if f != nil {
210
- f (& cfg )
271
+ if options .BlockchainConfigHook != nil {
272
+ options .BlockchainConfigHook (& cfg )
273
+ }
274
+
275
+ store := options .Store
276
+ if store == nil {
277
+ store = storage .NewMemoryStore ()
211
278
}
212
- if st == nil {
213
- st = storage .NewMemoryStore ()
279
+
280
+ logger := options .Logger
281
+ if logger == nil {
282
+ logger = zaptest .NewLogger (t )
214
283
}
215
284
216
- log := zaptest .NewLogger (t )
217
- bc , err := core .NewBlockchain (st , cfg , log )
285
+ bc , err := core .NewBlockchain (store , cfg , logger )
286
+ if err == nil && ! options .SkipRun {
287
+ go bc .Run ()
288
+ t .Cleanup (bc .Close )
289
+ }
218
290
return bc , neotest .NewMultiSigner (multiValidatorAcc ... ), neotest .NewMultiSigner (multiCommitteeAcc ... ), err
219
291
}
0 commit comments