Skip to content

Commit 01890ab

Browse files
authored
move actpool into factory package (iotexproject#2147)
* move actpool into factory package * add actpool as block subscriber to reset actpool
1 parent 140f52d commit 01890ab

33 files changed

+877
-926
lines changed

action/protocol/execution/protocol_test.go

+29-38
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/iotexproject/iotex-core/action/protocol/execution/evm"
3333
"github.com/iotexproject/iotex-core/action/protocol/rewarding"
3434
"github.com/iotexproject/iotex-core/action/protocol/rolldpos"
35+
"github.com/iotexproject/iotex-core/actpool"
3536
"github.com/iotexproject/iotex-core/blockchain"
3637
"github.com/iotexproject/iotex-core/blockchain/block"
3738
"github.com/iotexproject/iotex-core/blockchain/blockdao"
@@ -215,6 +216,7 @@ func runExecution(
215216
bc blockchain.Blockchain,
216217
sf factory.Factory,
217218
dao blockdao.BlockDAO,
219+
ap actpool.ActPool,
218220
ecfg *ExecutionConfig,
219221
contractAddr string,
220222
) ([]byte, *action.Receipt, error) {
@@ -256,12 +258,10 @@ func runExecution(
256258
if err != nil {
257259
return nil, nil, err
258260
}
259-
actionMap := make(map[string][]action.SealedEnvelope)
260-
actionMap[ecfg.Executor().String()] = []action.SealedEnvelope{selp}
261-
blk, err := bc.MintNewBlock(
262-
actionMap,
263-
testutil.TimestampNow(),
264-
)
261+
if err := ap.Add(context.Background(), selp); err != nil {
262+
return nil, nil, err
263+
}
264+
blk, err := bc.MintNewBlock(testutil.TimestampNow())
265265
if err != nil {
266266
return nil, nil, err
267267
}
@@ -276,14 +276,15 @@ func runExecution(
276276
func (sct *SmartContractTest) prepareBlockchain(
277277
ctx context.Context,
278278
r *require.Assertions,
279-
) (blockchain.Blockchain, factory.Factory, blockdao.BlockDAO) {
279+
) (blockchain.Blockchain, factory.Factory, blockdao.BlockDAO, actpool.ActPool) {
280280
cfg := config.Default
281281
defer func() {
282282
delete(cfg.Plugins, config.GatewayPlugin)
283283
}()
284284
cfg.Plugins[config.GatewayPlugin] = true
285285
cfg.Chain.EnableAsyncIndexWrite = false
286286
cfg.Genesis.EnableGravityChainVoting = false
287+
cfg.ActPool.MinGasPriceStr = "0"
287288
if sct.InitGenesis.IsBering {
288289
cfg.Genesis.Blockchain.BeringBlockHeight = 0
289290
}
@@ -298,6 +299,8 @@ func (sct *SmartContractTest) prepareBlockchain(
298299
// create state factory
299300
sf, err := factory.NewFactory(cfg, factory.InMemTrieOption(), factory.RegistryOption(registry))
300301
r.NoError(err)
302+
ap, err := actpool.NewActPool(sf, cfg.ActPool)
303+
r.NoError(err)
301304
// create indexer
302305
indexer, err := blockindex.NewIndexer(db.NewMemKVStore(), cfg.Genesis.Hash())
303306
r.NoError(err)
@@ -307,7 +310,7 @@ func (sct *SmartContractTest) prepareBlockchain(
307310
bc := blockchain.NewBlockchain(
308311
cfg,
309312
dao,
310-
sf,
313+
factory.NewMinter(sf, ap),
311314
blockchain.BlockValidatorOption(block.NewValidator(
312315
sf,
313316
protocol.NewGenericValidator(sf, accountutil.AccountState),
@@ -321,20 +324,21 @@ func (sct *SmartContractTest) prepareBlockchain(
321324
r.NoError(execution.Register(registry))
322325
r.NoError(bc.Start(ctx))
323326

324-
return bc, sf, dao
327+
return bc, sf, dao, ap
325328
}
326329

327330
func (sct *SmartContractTest) deployContracts(
328331
bc blockchain.Blockchain,
329332
sf factory.Factory,
330333
dao blockdao.BlockDAO,
334+
ap actpool.ActPool,
331335
r *require.Assertions,
332336
) (contractAddresses []string) {
333337
for i, contract := range sct.Deployments {
334338
if contract.AppendContractAddress {
335339
contract.ContractAddressToAppend = contractAddresses[contract.ContractIndexToAppend]
336340
}
337-
_, receipt, err := runExecution(bc, sf, dao, &contract, action.EmptyAddress)
341+
_, receipt, err := runExecution(bc, sf, dao, ap, &contract, action.EmptyAddress)
338342
r.NoError(err)
339343
r.NotNil(receipt)
340344
if sct.InitGenesis.IsBering {
@@ -372,13 +376,13 @@ func (sct *SmartContractTest) deployContracts(
372376
func (sct *SmartContractTest) run(r *require.Assertions) {
373377
// prepare blockchain
374378
ctx := context.Background()
375-
bc, sf, dao := sct.prepareBlockchain(ctx, r)
379+
bc, sf, dao, ap := sct.prepareBlockchain(ctx, r)
376380
defer func() {
377381
r.NoError(bc.Stop(ctx))
378382
}()
379383

380384
// deploy smart contract
381-
contractAddresses := sct.deployContracts(bc, sf, dao, r)
385+
contractAddresses := sct.deployContracts(bc, sf, dao, ap, r)
382386
if len(contractAddresses) == 0 {
383387
return
384388
}
@@ -389,7 +393,7 @@ func (sct *SmartContractTest) run(r *require.Assertions) {
389393
if exec.AppendContractAddress {
390394
exec.ContractAddressToAppend = contractAddresses[exec.ContractIndexToAppend]
391395
}
392-
retval, receipt, err := runExecution(bc, sf, dao, &exec, contractAddr)
396+
retval, receipt, err := runExecution(bc, sf, dao, ap, &exec, contractAddr)
393397
r.NoError(err)
394398
r.NotNil(receipt)
395399

@@ -482,6 +486,7 @@ func TestProtocol_Handle(t *testing.T) {
482486
cfg.Chain.IndexDBPath = testIndexPath
483487
cfg.Chain.EnableAsyncIndexWrite = false
484488
cfg.Genesis.EnableGravityChainVoting = false
489+
cfg.ActPool.MinGasPriceStr = "0"
485490
cfg.Genesis.InitBalanceMap[identityset.Address(27).String()] = unit.ConvertIotxToRau(1000000000).String()
486491
registry := protocol.NewRegistry()
487492
acc := account.NewProtocol(rewarding.DepositGas)
@@ -491,6 +496,8 @@ func TestProtocol_Handle(t *testing.T) {
491496
// create state factory
492497
sf, err := factory.NewStateDB(cfg, factory.DefaultStateDBOption(), factory.RegistryStateDBOption(registry))
493498
require.NoError(err)
499+
ap, err := actpool.NewActPool(sf, cfg.ActPool)
500+
require.NoError(err)
494501
// create indexer
495502
cfg.DB.DbPath = cfg.Chain.IndexDBPath
496503
indexer, err := blockindex.NewIndexer(db.NewBoltDB(cfg.DB), hash.ZeroHash256)
@@ -502,7 +509,7 @@ func TestProtocol_Handle(t *testing.T) {
502509
bc := blockchain.NewBlockchain(
503510
cfg,
504511
dao,
505-
sf,
512+
factory.NewMinter(sf, ap),
506513
blockchain.BlockValidatorOption(block.NewValidator(
507514
sf,
508515
protocol.NewGenericValidator(sf, accountutil.AccountState),
@@ -527,12 +534,8 @@ func TestProtocol_Handle(t *testing.T) {
527534
selp, err := action.Sign(elp, identityset.PrivateKey(27))
528535
require.NoError(err)
529536

530-
actionMap := make(map[string][]action.SealedEnvelope)
531-
actionMap[identityset.Address(27).String()] = []action.SealedEnvelope{selp}
532-
blk, err := bc.MintNewBlock(
533-
actionMap,
534-
testutil.TimestampNow(),
535-
)
537+
require.NoError(ap.Add(context.Background(), selp))
538+
blk, err := bc.MintNewBlock(testutil.TimestampNow())
536539
require.NoError(err)
537540
require.NoError(bc.CommitBlock(blk))
538541
require.Equal(1, len(blk.Receipts))
@@ -580,12 +583,8 @@ func TestProtocol_Handle(t *testing.T) {
580583

581584
log.S().Infof("execution %+v", execution)
582585

583-
actionMap = make(map[string][]action.SealedEnvelope)
584-
actionMap[identityset.Address(27).String()] = []action.SealedEnvelope{selp}
585-
blk, err = bc.MintNewBlock(
586-
actionMap,
587-
testutil.TimestampNow(),
588-
)
586+
require.NoError(ap.Add(context.Background(), selp))
587+
blk, err = bc.MintNewBlock(testutil.TimestampNow())
589588
require.NoError(err)
590589
require.NoError(bc.CommitBlock(blk))
591590
require.Equal(1, len(blk.Receipts))
@@ -618,12 +617,8 @@ func TestProtocol_Handle(t *testing.T) {
618617
require.NoError(err)
619618

620619
log.S().Infof("execution %+v", execution)
621-
actionMap = make(map[string][]action.SealedEnvelope)
622-
actionMap[identityset.Address(27).String()] = []action.SealedEnvelope{selp}
623-
blk, err = bc.MintNewBlock(
624-
actionMap,
625-
testutil.TimestampNow(),
626-
)
620+
require.NoError(ap.Add(context.Background(), selp))
621+
blk, err = bc.MintNewBlock(testutil.TimestampNow())
627622
require.NoError(err)
628623
require.NoError(bc.CommitBlock(blk))
629624
require.Equal(1, len(blk.Receipts))
@@ -644,12 +639,8 @@ func TestProtocol_Handle(t *testing.T) {
644639
selp, err = action.Sign(elp, identityset.PrivateKey(27))
645640
require.NoError(err)
646641

647-
actionMap = make(map[string][]action.SealedEnvelope)
648-
actionMap[identityset.Address(27).String()] = []action.SealedEnvelope{selp}
649-
blk, err = bc.MintNewBlock(
650-
actionMap,
651-
testutil.TimestampNow(),
652-
)
642+
require.NoError(ap.Add(context.Background(), selp))
643+
blk, err = bc.MintNewBlock(testutil.TimestampNow())
653644
require.NoError(err)
654645
require.NoError(bc.CommitBlock(blk))
655646
require.Equal(1, len(blk.Receipts))

actpool/actpool.go

+19
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/iotexproject/iotex-core/action"
2323
"github.com/iotexproject/iotex-core/action/protocol"
2424
accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util"
25+
"github.com/iotexproject/iotex-core/blockchain/block"
2526
"github.com/iotexproject/iotex-core/config"
2627
"github.com/iotexproject/iotex-core/pkg/log"
2728
"github.com/iotexproject/iotex-core/pkg/prometheustimer"
@@ -61,6 +62,10 @@ type ActPool interface {
6162
GetGasSize() uint64
6263
// GetGasCapacity returns the act pool gas capacity
6364
GetGasCapacity() uint64
65+
// DeleteAction deletes an invalid action from pool
66+
DeleteAction(action.SealedEnvelope)
67+
// ReceiveBlock will be called when a new block is committed
68+
ReceiveBlock(*block.Block) error
6469

6570
AddActionEnvelopeValidators(...action.SealedEnvelopeValidator)
6671
}
@@ -154,6 +159,14 @@ func (ap *actPool) Reset() {
154159
ap.reset()
155160
}
156161

162+
func (ap *actPool) ReceiveBlock(*block.Block) error {
163+
ap.mutex.Lock()
164+
defer ap.mutex.Unlock()
165+
166+
ap.reset()
167+
return nil
168+
}
169+
157170
// PendingActionIterator returns an action interator with all accepted actions
158171
func (ap *actPool) PendingActionMap() map[string][]action.SealedEnvelope {
159172
ap.mutex.Lock()
@@ -294,6 +307,12 @@ func (ap *actPool) Validate(ctx context.Context, selp action.SealedEnvelope) err
294307
return ap.validate(ctx, selp)
295308
}
296309

310+
func (ap *actPool) DeleteAction(act action.SealedEnvelope) {
311+
ap.mutex.RLock()
312+
defer ap.mutex.RUnlock()
313+
ap.removeInvalidActs([]action.SealedEnvelope{act})
314+
}
315+
297316
func (ap *actPool) validate(ctx context.Context, selp action.SealedEnvelope) error {
298317
caller, err := address.FromBytes(selp.SrcPubkey().Hash())
299318
if err != nil {

0 commit comments

Comments
 (0)