Skip to content

Commit 51537df

Browse files
authored
Merge pull request #3404 from stevvns/neotest-options
neotest: add options to customize test chain
2 parents ee1ee56 + 7aa5983 commit 51537df

File tree

1 file changed

+87
-15
lines changed

1 file changed

+87
-15
lines changed

pkg/neotest/chain/chain.go

Lines changed: 87 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
1616
"github.com/nspcc-dev/neo-go/pkg/wallet"
1717
"github.com/stretchr/testify/require"
18+
"go.uber.org/zap"
1819
"go.uber.org/zap/zaptest"
1920
)
2021

@@ -57,6 +58,24 @@ var (
5758
standByCommittee []string
5859
)
5960

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+
6079
func init() {
6180
committeeAcc, _ = wallet.NewAccountFromWIF(singleValidatorWIF)
6281
pubs := keys.PublicKeys{committeeAcc.PublicKey()}
@@ -138,7 +157,21 @@ func NewSingleWithCustomConfig(t testing.TB, f func(*config.Blockchain)) (*core.
138157
// responsibility to do that before using the chain and
139158
// to properly Close the chain when done.
140159
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{
142175
ProtocolConfiguration: config.ProtocolConfiguration{
143176
Magic: netmode.UnitTestNet,
144177
MaxTraceableBlocks: MaxTraceableBlocks,
@@ -148,17 +181,23 @@ func NewSingleWithCustomConfigAndStore(t testing.TB, f func(cfg *config.Blockcha
148181
VerifyTransactions: true,
149182
},
150183
}
184+
if options.BlockchainConfigHook != nil {
185+
options.BlockchainConfigHook(&cfg)
186+
}
151187

152-
if f != nil {
153-
f(&cfg)
188+
store := options.Store
189+
if store == nil {
190+
store = storage.NewMemoryStore()
154191
}
155-
if st == nil {
156-
st = storage.NewMemoryStore()
192+
193+
logger := options.Logger
194+
if logger == nil {
195+
logger = zaptest.NewLogger(t)
157196
}
158-
log := zaptest.NewLogger(t)
159-
bc, err := core.NewBlockchain(st, cfg, log)
197+
198+
bc, err := core.NewBlockchain(store, cfg, logger)
160199
require.NoError(t, err)
161-
if run {
200+
if !options.SkipRun {
162201
go bc.Run()
163202
t.Cleanup(bc.Close)
164203
}
@@ -193,10 +232,33 @@ func NewMultiWithCustomConfigAndStore(t testing.TB, f func(*config.Blockchain),
193232
return bc, validator, committee
194233
}
195234

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+
196244
// NewMultiWithCustomConfigAndStoreNoCheck is similar to NewMultiWithCustomConfig,
197245
// but do not perform Blockchain run and do not check Blockchain constructor error.
198246
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{
200262
ProtocolConfiguration: config.ProtocolConfiguration{
201263
Magic: netmode.UnitTestNet,
202264
MaxTraceableBlocks: MaxTraceableBlocks,
@@ -206,14 +268,24 @@ func NewMultiWithCustomConfigAndStoreNoCheck(t testing.TB, f func(*config.Blockc
206268
VerifyTransactions: true,
207269
},
208270
}
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()
211278
}
212-
if st == nil {
213-
st = storage.NewMemoryStore()
279+
280+
logger := options.Logger
281+
if logger == nil {
282+
logger = zaptest.NewLogger(t)
214283
}
215284

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+
}
218290
return bc, neotest.NewMultiSigner(multiValidatorAcc...), neotest.NewMultiSigner(multiCommitteeAcc...), err
219291
}

0 commit comments

Comments
 (0)