Skip to content

Commit bb27133

Browse files
authored
Remove Factory() from blockchain (iotexproject#1783)
* remove Factory() from blockchain interface
1 parent ff74007 commit bb27133

26 files changed

+378
-338
lines changed

action/protocol/execution/protocol_test.go

+23-18
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
"github.com/iotexproject/iotex-core/db"
4343
"github.com/iotexproject/iotex-core/pkg/log"
4444
"github.com/iotexproject/iotex-core/pkg/unit"
45+
"github.com/iotexproject/iotex-core/state/factory"
4546
"github.com/iotexproject/iotex-core/test/identityset"
4647
"github.com/iotexproject/iotex-core/testutil"
4748
"github.com/iotexproject/iotex-proto/golang/iotextypes"
@@ -204,11 +205,12 @@ func NewSmartContractTest(t *testing.T, file string) {
204205

205206
func runExecution(
206207
bc blockchain.Blockchain,
208+
sf factory.Factory,
207209
ecfg *ExecutionConfig,
208210
contractAddr string,
209211
) ([]byte, *action.Receipt, error) {
210212
log.S().Info(ecfg.Comment)
211-
state, err := bc.Factory().AccountState(ecfg.Executor().String())
213+
state, err := sf.AccountState(ecfg.Executor().String())
212214
if err != nil {
213215
return nil, nil, err
214216
}
@@ -233,7 +235,7 @@ func runExecution(
233235
return nil, nil, err
234236
}
235237

236-
return bc.Factory().SimulateExecution(ctx, addr, exec, bc.BlockDAO().GetBlockHash)
238+
return sf.SimulateExecution(ctx, addr, exec, bc.BlockDAO().GetBlockHash)
237239
}
238240
builder := &action.EnvelopeBuilder{}
239241
elp := builder.SetAction(exec).
@@ -273,7 +275,7 @@ func runExecution(
273275
func (sct *SmartContractTest) prepareBlockchain(
274276
ctx context.Context,
275277
r *require.Assertions,
276-
) blockchain.Blockchain {
278+
) (blockchain.Blockchain, factory.Factory) {
277279
cfg := config.Default
278280
defer func() {
279281
delete(cfg.Plugins, config.GatewayPlugin)
@@ -292,6 +294,9 @@ func (sct *SmartContractTest) prepareBlockchain(
292294
r.NoError(acc.Register(registry))
293295
rp := rolldpos.NewProtocol(cfg.Genesis.NumCandidateDelegates, cfg.Genesis.NumDelegates, cfg.Genesis.NumSubEpochs)
294296
r.NoError(rp.Register(registry))
297+
// create state factory
298+
sf, err := factory.NewFactory(cfg, factory.InMemTrieOption())
299+
r.NoError(err)
295300
// create indexer
296301
indexer, err := blockindex.NewIndexer(db.NewMemKVStore(), cfg.Genesis.Hash())
297302
r.NoError(err)
@@ -301,32 +306,31 @@ func (sct *SmartContractTest) prepareBlockchain(
301306
bc := blockchain.NewBlockchain(
302307
cfg,
303308
dao,
304-
blockchain.InMemStateFactoryOption(),
309+
sf,
305310
blockchain.RegistryOption(registry),
306311
)
307312
reward := rewarding.NewProtocol(nil, rp)
308313
r.NoError(reward.Register(registry))
309314

310315
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))
314317
execution := NewProtocol(bc.BlockDAO().GetBlockHash)
315318
r.NoError(execution.Register(registry))
316319
r.NoError(bc.Start(ctx))
317320

318-
return bc
321+
return bc, sf
319322
}
320323

321324
func (sct *SmartContractTest) deployContracts(
322325
bc blockchain.Blockchain,
326+
sf factory.Factory,
323327
r *require.Assertions,
324328
) (contractAddresses []string) {
325329
for i, contract := range sct.Deployments {
326330
if contract.AppendContractAddress {
327331
contract.ContractAddressToAppend = contractAddresses[contract.ContractIndexToAppend]
328332
}
329-
_, receipt, err := runExecution(bc, &contract, action.EmptyAddress)
333+
_, receipt, err := runExecution(bc, sf, &contract, action.EmptyAddress)
330334
r.NoError(err)
331335
r.NotNil(receipt)
332336
if sct.InitGenesis.IsBering {
@@ -347,7 +351,7 @@ func (sct *SmartContractTest) deployContracts(
347351
r.Equal(sct.Deployments[i].ExpectedGasConsumed(), receipt.GasConsumed)
348352
}
349353

350-
ws, err := bc.Factory().NewWorkingSet()
354+
ws, err := sf.NewWorkingSet()
351355
r.NoError(err)
352356
stateDB := evm.NewStateDBAdapter(ws, uint64(0), true, hash.ZeroHash256)
353357
var evmContractAddrHash common.Address
@@ -367,13 +371,13 @@ func (sct *SmartContractTest) deployContracts(
367371
func (sct *SmartContractTest) run(r *require.Assertions) {
368372
// prepare blockchain
369373
ctx := context.Background()
370-
bc := sct.prepareBlockchain(ctx, r)
374+
bc, sf := sct.prepareBlockchain(ctx, r)
371375
defer func() {
372376
r.NoError(bc.Stop(ctx))
373377
}()
374378

375379
// deploy smart contract
376-
contractAddresses := sct.deployContracts(bc, r)
380+
contractAddresses := sct.deployContracts(bc, sf, r)
377381
if len(contractAddresses) == 0 {
378382
return
379383
}
@@ -384,7 +388,7 @@ func (sct *SmartContractTest) run(r *require.Assertions) {
384388
if exec.AppendContractAddress {
385389
exec.ContractAddressToAppend = contractAddresses[exec.ContractIndexToAppend]
386390
}
387-
retval, receipt, err := runExecution(bc, &exec, contractAddr)
391+
retval, receipt, err := runExecution(bc, sf, &exec, contractAddr)
388392
r.NoError(err)
389393
r.NotNil(receipt)
390394

@@ -414,7 +418,7 @@ func (sct *SmartContractTest) run(r *require.Assertions) {
414418
if account == "" {
415419
account = contractAddr
416420
}
417-
state, err := bc.Factory().AccountState(account)
421+
state, err := sf.AccountState(account)
418422
r.NoError(err)
419423
r.Equal(
420424
0,
@@ -462,6 +466,9 @@ func TestProtocol_Handle(t *testing.T) {
462466
require.NoError(acc.Register(registry))
463467
rp := rolldpos.NewProtocol(cfg.Genesis.NumCandidateDelegates, cfg.Genesis.NumDelegates, cfg.Genesis.NumSubEpochs)
464468
require.NoError(rp.Register(registry))
469+
// create state factory
470+
sf, err := factory.NewStateDB(cfg, factory.DefaultStateDBOption())
471+
require.NoError(err)
465472
// create indexer
466473
cfg.DB.DbPath = cfg.Chain.IndexDBPath
467474
indexer, err := blockindex.NewIndexer(db.NewBoltDB(cfg.DB), hash.ZeroHash256)
@@ -473,14 +480,12 @@ func TestProtocol_Handle(t *testing.T) {
473480
bc := blockchain.NewBlockchain(
474481
cfg,
475482
dao,
476-
blockchain.DefaultStateFactoryOption(),
483+
sf,
477484
blockchain.RegistryOption(registry),
478485
)
479486
exeProtocol := NewProtocol(bc.BlockDAO().GetBlockHash)
480487
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))
484489
require.NoError(bc.Start(ctx))
485490
require.NotNil(bc)
486491
defer func() {

actpool/actpool.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import (
2121
"github.com/iotexproject/iotex-address/address"
2222
"github.com/iotexproject/iotex-core/action"
2323
"github.com/iotexproject/iotex-core/action/protocol"
24-
"github.com/iotexproject/iotex-core/blockchain"
2524
"github.com/iotexproject/iotex-core/config"
2625
"github.com/iotexproject/iotex-core/pkg/log"
26+
"github.com/iotexproject/iotex-core/state/factory"
2727
)
2828

2929
var (
@@ -85,7 +85,7 @@ func EnableExperimentalActions() Option {
8585
type actPool struct {
8686
mutex sync.RWMutex
8787
cfg config.ActPool
88-
bc blockchain.Blockchain
88+
sf factory.Factory
8989
accountActs map[string]ActQueue
9090
accountDesActs map[string]map[hash.Hash256]action.SealedEnvelope
9191
allActions map[hash.Hash256]action.SealedEnvelope
@@ -97,9 +97,9 @@ type actPool struct {
9797
}
9898

9999
// NewActPool constructs a new actpool
100-
func NewActPool(bc blockchain.Blockchain, cfg config.ActPool, opts ...Option) (ActPool, error) {
101-
if bc == nil {
102-
return nil, errors.New("Try to attach a nil blockchain")
100+
func NewActPool(sf factory.Factory, cfg config.ActPool, opts ...Option) (ActPool, error) {
101+
if sf == nil {
102+
return nil, errors.New("Try to attach a nil state factory")
103103
}
104104

105105
senderBlackList := make(map[string]bool)
@@ -109,7 +109,7 @@ func NewActPool(bc blockchain.Blockchain, cfg config.ActPool, opts ...Option) (A
109109

110110
ap := &actPool{
111111
cfg: cfg,
112-
bc: bc,
112+
sf: sf,
113113
senderBlackList: senderBlackList,
114114
accountActs: make(map[string]ActQueue),
115115
accountDesActs: make(map[string]map[hash.Hash256]action.SealedEnvelope),
@@ -256,7 +256,7 @@ func (ap *actPool) GetPendingNonce(addr string) (uint64, error) {
256256
if queue, ok := ap.accountActs[addr]; ok {
257257
return queue.PendingNonce(), nil
258258
}
259-
confirmedState, err := ap.bc.Factory().AccountState(addr)
259+
confirmedState, err := ap.sf.AccountState(addr)
260260
if err != nil {
261261
return 0, err
262262
}
@@ -326,7 +326,7 @@ func (ap *actPool) GetGasCapacity() uint64 {
326326
// private functions
327327
//======================================
328328
func (ap *actPool) enqueueAction(sender string, act action.SealedEnvelope, actHash hash.Hash256, actNonce uint64) error {
329-
confirmedState, err := ap.bc.Factory().AccountState(sender)
329+
confirmedState, err := ap.sf.AccountState(sender)
330330
if err != nil {
331331
actpoolMtc.WithLabelValues("failedToGetNonce").Inc()
332332
return errors.Wrapf(err, "failed to get sender's nonce for action %x", actHash)
@@ -342,7 +342,7 @@ func (ap *actPool) enqueueAction(sender string, act action.SealedEnvelope, actHa
342342
pendingNonce := confirmedNonce + 1
343343
queue.SetPendingNonce(pendingNonce)
344344
// Initialize balance for new account
345-
state, err := ap.bc.Factory().AccountState(sender)
345+
state, err := ap.sf.AccountState(sender)
346346
if err != nil {
347347
actpoolMtc.WithLabelValues("failedToGetBalance").Inc()
348348
return errors.Wrapf(err, "failed to get sender's balance for action %x", actHash)
@@ -412,7 +412,7 @@ func (ap *actPool) enqueueAction(sender string, act action.SealedEnvelope, actHa
412412
// removeConfirmedActs removes processed (committed to block) actions from pool
413413
func (ap *actPool) removeConfirmedActs() {
414414
for from, queue := range ap.accountActs {
415-
confirmedState, err := ap.bc.Factory().AccountState(from)
415+
confirmedState, err := ap.sf.AccountState(from)
416416
if err != nil {
417417
log.L().Error("Error when removing confirmed actions", zap.Error(err))
418418
return
@@ -476,7 +476,7 @@ func (ap *actPool) reset() {
476476
ap.removeConfirmedActs()
477477
for from, queue := range ap.accountActs {
478478
// Reset pending balance for each account
479-
state, err := ap.bc.Factory().AccountState(from)
479+
state, err := ap.sf.AccountState(from)
480480
if err != nil {
481481
log.L().Error("Error when resetting actpool state.", zap.Error(err))
482482
return

0 commit comments

Comments
 (0)