@@ -42,6 +42,7 @@ import (
42
42
"github.com/iotexproject/iotex-core/db"
43
43
"github.com/iotexproject/iotex-core/pkg/log"
44
44
"github.com/iotexproject/iotex-core/pkg/unit"
45
+ "github.com/iotexproject/iotex-core/state/factory"
45
46
"github.com/iotexproject/iotex-core/test/identityset"
46
47
"github.com/iotexproject/iotex-core/testutil"
47
48
"github.com/iotexproject/iotex-proto/golang/iotextypes"
@@ -204,11 +205,12 @@ func NewSmartContractTest(t *testing.T, file string) {
204
205
205
206
func runExecution (
206
207
bc blockchain.Blockchain ,
208
+ sf factory.Factory ,
207
209
ecfg * ExecutionConfig ,
208
210
contractAddr string ,
209
211
) ([]byte , * action.Receipt , error ) {
210
212
log .S ().Info (ecfg .Comment )
211
- state , err := bc . Factory () .AccountState (ecfg .Executor ().String ())
213
+ state , err := sf .AccountState (ecfg .Executor ().String ())
212
214
if err != nil {
213
215
return nil , nil , err
214
216
}
@@ -233,7 +235,7 @@ func runExecution(
233
235
return nil , nil , err
234
236
}
235
237
236
- return bc . Factory () .SimulateExecution (ctx , addr , exec , bc .BlockDAO ().GetBlockHash )
238
+ return sf .SimulateExecution (ctx , addr , exec , bc .BlockDAO ().GetBlockHash )
237
239
}
238
240
builder := & action.EnvelopeBuilder {}
239
241
elp := builder .SetAction (exec ).
@@ -273,7 +275,7 @@ func runExecution(
273
275
func (sct * SmartContractTest ) prepareBlockchain (
274
276
ctx context.Context ,
275
277
r * require.Assertions ,
276
- ) blockchain.Blockchain {
278
+ ) ( blockchain.Blockchain , factory. Factory ) {
277
279
cfg := config .Default
278
280
defer func () {
279
281
delete (cfg .Plugins , config .GatewayPlugin )
@@ -292,6 +294,9 @@ func (sct *SmartContractTest) prepareBlockchain(
292
294
r .NoError (acc .Register (registry ))
293
295
rp := rolldpos .NewProtocol (cfg .Genesis .NumCandidateDelegates , cfg .Genesis .NumDelegates , cfg .Genesis .NumSubEpochs )
294
296
r .NoError (rp .Register (registry ))
297
+ // create state factory
298
+ sf , err := factory .NewFactory (cfg , factory .InMemTrieOption ())
299
+ r .NoError (err )
295
300
// create indexer
296
301
indexer , err := blockindex .NewIndexer (db .NewMemKVStore (), cfg .Genesis .Hash ())
297
302
r .NoError (err )
@@ -301,32 +306,31 @@ func (sct *SmartContractTest) prepareBlockchain(
301
306
bc := blockchain .NewBlockchain (
302
307
cfg ,
303
308
dao ,
304
- blockchain . InMemStateFactoryOption () ,
309
+ sf ,
305
310
blockchain .RegistryOption (registry ),
306
311
)
307
312
reward := rewarding .NewProtocol (nil , rp )
308
313
r .NoError (reward .Register (registry ))
309
314
310
315
r .NotNil (bc )
311
- bc .Validator ().AddActionEnvelopeValidators (protocol .NewGenericValidator (bc .Factory ().AccountState ))
312
- sf := bc .Factory ()
313
- r .NotNil (sf )
316
+ bc .Validator ().AddActionEnvelopeValidators (protocol .NewGenericValidator (sf .AccountState ))
314
317
execution := NewProtocol (bc .BlockDAO ().GetBlockHash )
315
318
r .NoError (execution .Register (registry ))
316
319
r .NoError (bc .Start (ctx ))
317
320
318
- return bc
321
+ return bc , sf
319
322
}
320
323
321
324
func (sct * SmartContractTest ) deployContracts (
322
325
bc blockchain.Blockchain ,
326
+ sf factory.Factory ,
323
327
r * require.Assertions ,
324
328
) (contractAddresses []string ) {
325
329
for i , contract := range sct .Deployments {
326
330
if contract .AppendContractAddress {
327
331
contract .ContractAddressToAppend = contractAddresses [contract .ContractIndexToAppend ]
328
332
}
329
- _ , receipt , err := runExecution (bc , & contract , action .EmptyAddress )
333
+ _ , receipt , err := runExecution (bc , sf , & contract , action .EmptyAddress )
330
334
r .NoError (err )
331
335
r .NotNil (receipt )
332
336
if sct .InitGenesis .IsBering {
@@ -347,7 +351,7 @@ func (sct *SmartContractTest) deployContracts(
347
351
r .Equal (sct .Deployments [i ].ExpectedGasConsumed (), receipt .GasConsumed )
348
352
}
349
353
350
- ws , err := bc . Factory () .NewWorkingSet ()
354
+ ws , err := sf .NewWorkingSet ()
351
355
r .NoError (err )
352
356
stateDB := evm .NewStateDBAdapter (ws , uint64 (0 ), true , hash .ZeroHash256 )
353
357
var evmContractAddrHash common.Address
@@ -367,13 +371,13 @@ func (sct *SmartContractTest) deployContracts(
367
371
func (sct * SmartContractTest ) run (r * require.Assertions ) {
368
372
// prepare blockchain
369
373
ctx := context .Background ()
370
- bc := sct .prepareBlockchain (ctx , r )
374
+ bc , sf := sct .prepareBlockchain (ctx , r )
371
375
defer func () {
372
376
r .NoError (bc .Stop (ctx ))
373
377
}()
374
378
375
379
// deploy smart contract
376
- contractAddresses := sct .deployContracts (bc , r )
380
+ contractAddresses := sct .deployContracts (bc , sf , r )
377
381
if len (contractAddresses ) == 0 {
378
382
return
379
383
}
@@ -384,7 +388,7 @@ func (sct *SmartContractTest) run(r *require.Assertions) {
384
388
if exec .AppendContractAddress {
385
389
exec .ContractAddressToAppend = contractAddresses [exec .ContractIndexToAppend ]
386
390
}
387
- retval , receipt , err := runExecution (bc , & exec , contractAddr )
391
+ retval , receipt , err := runExecution (bc , sf , & exec , contractAddr )
388
392
r .NoError (err )
389
393
r .NotNil (receipt )
390
394
@@ -414,7 +418,7 @@ func (sct *SmartContractTest) run(r *require.Assertions) {
414
418
if account == "" {
415
419
account = contractAddr
416
420
}
417
- state , err := bc . Factory () .AccountState (account )
421
+ state , err := sf .AccountState (account )
418
422
r .NoError (err )
419
423
r .Equal (
420
424
0 ,
@@ -462,6 +466,9 @@ func TestProtocol_Handle(t *testing.T) {
462
466
require .NoError (acc .Register (registry ))
463
467
rp := rolldpos .NewProtocol (cfg .Genesis .NumCandidateDelegates , cfg .Genesis .NumDelegates , cfg .Genesis .NumSubEpochs )
464
468
require .NoError (rp .Register (registry ))
469
+ // create state factory
470
+ sf , err := factory .NewStateDB (cfg , factory .DefaultStateDBOption ())
471
+ require .NoError (err )
465
472
// create indexer
466
473
cfg .DB .DbPath = cfg .Chain .IndexDBPath
467
474
indexer , err := blockindex .NewIndexer (db .NewBoltDB (cfg .DB ), hash .ZeroHash256 )
@@ -473,14 +480,12 @@ func TestProtocol_Handle(t *testing.T) {
473
480
bc := blockchain .NewBlockchain (
474
481
cfg ,
475
482
dao ,
476
- blockchain . DefaultStateFactoryOption () ,
483
+ sf ,
477
484
blockchain .RegistryOption (registry ),
478
485
)
479
486
exeProtocol := NewProtocol (bc .BlockDAO ().GetBlockHash )
480
487
require .NoError (exeProtocol .Register (registry ))
481
- bc .Validator ().AddActionEnvelopeValidators (protocol .NewGenericValidator (bc .Factory ().AccountState ))
482
- sf := bc .Factory ()
483
- require .NotNil (sf )
488
+ bc .Validator ().AddActionEnvelopeValidators (protocol .NewGenericValidator (sf .AccountState ))
484
489
require .NoError (bc .Start (ctx ))
485
490
require .NotNil (bc )
486
491
defer func () {
0 commit comments