Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[config] move config.ActPool to actpool package refactor #3514

Merged
merged 6 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions action/protocol/poll/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ import (
"github.com/iotexproject/iotex-core/action/protocol/execution/evm"
"github.com/iotexproject/iotex-core/action/protocol/staking"
"github.com/iotexproject/iotex-core/action/protocol/vote"
"github.com/iotexproject/iotex-core/config"
"github.com/iotexproject/iotex-core/blockchain"
"github.com/iotexproject/iotex-core/blockchain/genesis"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/state"
)

const (
_protocolID = "poll"
// RollDPoSScheme means randomized delegated proof of stake
RollDPoSScheme = "ROLLDPOS"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_rollDPoSScheme no need to export

)

const (
Expand Down Expand Up @@ -122,7 +125,9 @@ func MustGetProtocol(registry *protocol.Registry) Protocol {

// NewProtocol instantiates a rewarding protocol instance.
func NewProtocol(
cfg config.Config,
scheme string,
chainConfig blockchain.Config,
genesisConfig genesis.Genesis,
candidateIndexer *CandidateIndexer,
readContract ReadContract,
getCandidates GetCandidates,
Expand All @@ -134,8 +139,7 @@ func NewProtocol(
productivity Productivity,
getBlockHash evm.GetBlockHash,
) (Protocol, error) {
genesisConfig := cfg.Genesis
if cfg.Consensus.Scheme != config.RollDPoSScheme {
if scheme != RollDPoSScheme {
return nil, nil
}

Expand Down Expand Up @@ -165,9 +169,9 @@ func NewProtocol(
if err != nil {
return nil, err
}
scoreThreshold, ok = new(big.Int).SetString(cfg.Genesis.ScoreThreshold, 10)
scoreThreshold, ok = new(big.Int).SetString(genesisConfig.ScoreThreshold, 10)
if !ok {
return nil, errors.Errorf("failed to parse score threshold %s", cfg.Genesis.ScoreThreshold)
return nil, errors.Errorf("failed to parse score threshold %s", genesisConfig.ScoreThreshold)
}
}

Expand All @@ -182,7 +186,7 @@ func NewProtocol(
electionCommittee,
genesisConfig.GravityChainStartHeight,
getBlockTimeFunc,
cfg.Chain.PollInitialCandidatesInterval,
chainConfig.PollInitialCandidatesInterval,
slasher,
)
if err != nil {
Expand All @@ -192,8 +196,8 @@ func NewProtocol(
electionCommittee,
governance,
readContract,
cfg.Genesis.NativeStakingContractAddress,
cfg.Genesis.NativeStakingContractCode,
genesisConfig.NativeStakingContractAddress,
genesisConfig.NativeStakingContractCode,
scoreThreshold,
)
if err != nil {
Expand Down
12 changes: 7 additions & 5 deletions action/protocol/poll/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@ import (
"github.com/iotexproject/iotex-election/test/mock/mock_committee"

"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/config"
"github.com/iotexproject/iotex-core/blockchain"
"github.com/iotexproject/iotex-core/blockchain/genesis"
)

func TestNewProtocol(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)
committee := mock_committee.NewMockCommittee(ctrl)
cfg := config.Default
cfg.Consensus.Scheme = config.RollDPoSScheme
cfg.Genesis.ScoreThreshold = "1200000"
g := genesis.Default
g.ScoreThreshold = "1200000"
p, err := NewProtocol(
cfg,
RollDPoSScheme,
blockchain.DefaultConfig,
g,
nil,
func(context.Context, string, []byte, bool) ([]byte, error) { return nil, nil },
nil,
Expand Down
17 changes: 9 additions & 8 deletions action/protocol/rewarding/reward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
"github.com/iotexproject/iotex-core/action/protocol/poll"
"github.com/iotexproject/iotex-core/action/protocol/rewarding/rewardingpb"
"github.com/iotexproject/iotex-core/action/protocol/rolldpos"
"github.com/iotexproject/iotex-core/blockchain"
"github.com/iotexproject/iotex-core/blockchain/genesis"
"github.com/iotexproject/iotex-core/config"
"github.com/iotexproject/iotex-core/db/batch"
"github.com/iotexproject/iotex-core/pkg/unit"
"github.com/iotexproject/iotex-core/state"
Expand Down Expand Up @@ -365,7 +365,8 @@ func TestProtocol_NoRewardAddr(t *testing.T) {
RewardAddress: identityset.Address(1).String(),
},
}
cfg := config.Default
g := genesis.Default
bcf := blockchain.DefaultConfig
committee := mock_committee.NewMockCommittee(ctrl)
slasher, err := poll.NewSlasher(
func(uint64, uint64) (map[string]uint64, error) {
Expand All @@ -382,18 +383,18 @@ func TestProtocol_NoRewardAddr(t *testing.T) {
nil,
2,
2,
cfg.Genesis.DardanellesNumSubEpochs,
cfg.Genesis.ProductivityThreshold,
cfg.Genesis.ProbationEpochPeriod,
cfg.Genesis.UnproductiveDelegateMaxCacheSize,
cfg.Genesis.ProbationIntensityRate)
g.DardanellesNumSubEpochs,
g.ProductivityThreshold,
g.ProbationEpochPeriod,
g.UnproductiveDelegateMaxCacheSize,
g.ProbationIntensityRate)
require.NoError(t, err)
pp, err := poll.NewGovernanceChainCommitteeProtocol(
nil,
committee,
uint64(123456),
func(uint64) (time.Time, error) { return time.Now(), nil },
cfg.Chain.PollInitialCandidatesInterval,
bcf.PollInitialCandidatesInterval,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

directly use blockchain.DefaultConfig.PollInitialCandidatesInterval here, no need to define bcf

slasher,
)
require.NoError(t, err)
Expand Down
5 changes: 2 additions & 3 deletions actpool/actpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util"
"github.com/iotexproject/iotex-core/blockchain/block"
"github.com/iotexproject/iotex-core/blockchain/genesis"
"github.com/iotexproject/iotex-core/config"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/pkg/prometheustimer"
"github.com/iotexproject/iotex-core/pkg/tracer"
Expand Down Expand Up @@ -94,7 +93,7 @@ func EnableExperimentalActions() Option {
// actPool implements ActPool interface
type actPool struct {
mutex sync.RWMutex
cfg config.ActPool
cfg Config
g genesis.Genesis
sf protocol.StateReader
accountActs map[string]ActQueue
Expand All @@ -108,7 +107,7 @@ type actPool struct {
}

// NewActPool constructs a new actpool
func NewActPool(g genesis.Genesis, sf protocol.StateReader, cfg config.ActPool, opts ...Option) (ActPool, error) {
func NewActPool(g genesis.Genesis, sf protocol.StateReader, cfg Config, opts ...Option) (ActPool, error) {
if sf == nil {
return nil, errors.New("Try to attach a nil state reader")
}
Expand Down
53 changes: 26 additions & 27 deletions actpool/actpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util"
"github.com/iotexproject/iotex-core/action/protocol/rewarding"
"github.com/iotexproject/iotex-core/blockchain"
"github.com/iotexproject/iotex-core/config"
"github.com/iotexproject/iotex-core/test/identityset"
"github.com/iotexproject/iotex-core/testutil"
)
Expand Down Expand Up @@ -61,28 +60,27 @@ var (
func TestActPool_NewActPool(t *testing.T) {
ctrl := gomock.NewController(t)
require := require.New(t)
cfg := config.Default

//error caused by nil blockchain
_, err := NewActPool(cfg.Genesis, nil, cfg.ActPool, nil)
_, err := NewActPool(genesis.Default, nil, DefaultConfig, nil)
require.Error(err)

// all good
opt := EnableExperimentalActions()
require.Panics(func() { blockchain.NewBlockchain(cfg.Chain, cfg.Genesis, nil, nil, nil) }, "option is nil")
require.Panics(func() { blockchain.NewBlockchain(blockchain.DefaultConfig, genesis.Default, nil, nil, nil) }, "option is nil")
sf := mock_chainmanager.NewMockStateReader(ctrl)
act, err := NewActPool(cfg.Genesis, sf, cfg.ActPool, opt)
act, err := NewActPool(genesis.Default, sf, DefaultConfig, opt)
require.NoError(err)
require.NotNil(act)

// panic caused by option is nil
require.Panics(func() { NewActPool(cfg.Genesis, sf, cfg.ActPool, nil) }, "option is nil")
require.Panics(func() { NewActPool(genesis.Default, sf, DefaultConfig, nil) }, "option is nil")

// error caused by option
opt2 := func(pool *actPool) error {
return errors.New("test error")
}
_, err = NewActPool(cfg.Genesis, sf, cfg.ActPool, opt2)
_, err = NewActPool(genesis.Default, sf, DefaultConfig, opt2)
require.Error(err)

// test AddAction nil
Expand All @@ -92,21 +90,22 @@ func TestActPool_NewActPool(t *testing.T) {
func TestValidate(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)
cfg := config.Default
cfg.Genesis.InitBalanceMap[_addr1] = "100"

g := genesis.Default
g.InitBalanceMap[_addr1] = "100"
re := protocol.NewRegistry()
acc := account.NewProtocol(rewarding.DepositGas)
require.NoError(acc.Register(re))
ctx := genesis.WithGenesisContext(
protocol.WithRegistry(context.Background(), re),
cfg.Genesis,
g,
)
sf := mock_chainmanager.NewMockStateReader(ctrl)
sev := mock_sealed_envelope_validator.NewMockSealedEnvelopeValidator(ctrl)
mockError := errors.New("mock error")
sev.EXPECT().Validate(gomock.Any(), gomock.Any()).Return(mockError).Times(1)
apConfig := getActPoolCfg()
Ap, err := NewActPool(cfg.Genesis, sf, apConfig, EnableExperimentalActions())
Ap, err := NewActPool(g, sf, apConfig, EnableExperimentalActions())
require.NoError(err)
ap, ok := Ap.(*actPool)
require.True(ok)
Expand Down Expand Up @@ -166,7 +165,7 @@ func TestActPool_AddActs(t *testing.T) {
tsf8, err := action.SignedTransfer(_addr2, _priKey2, uint64(4), big.NewInt(5), []byte{}, uint64(100000), big.NewInt(0))
require.NoError(err)

ctx := genesis.WithGenesisContext(context.Background(), config.Default.Genesis)
ctx := genesis.WithGenesisContext(context.Background(), genesis.Default)
require.NoError(ap.Add(ctx, tsf1))
require.NoError(ap.Add(ctx, tsf2))
require.NoError(ap.Add(ctx, tsf3))
Expand Down Expand Up @@ -294,8 +293,8 @@ func TestActPool_PickActs(t *testing.T) {
ctrl := gomock.NewController(t)
require := require.New(t)
sf := mock_chainmanager.NewMockStateReader(ctrl)
ctx := genesis.WithGenesisContext(context.Background(), config.Default.Genesis)
createActPool := func(cfg config.ActPool) (*actPool, []action.SealedEnvelope, []action.SealedEnvelope, []action.SealedEnvelope) {
ctx := genesis.WithGenesisContext(context.Background(), genesis.Default)
createActPool := func(cfg Config) (*actPool, []action.SealedEnvelope, []action.SealedEnvelope, []action.SealedEnvelope) {
// Create actpool
Ap, err := NewActPool(genesis.Default, sf, cfg, EnableExperimentalActions())
require.NoError(err)
Expand Down Expand Up @@ -400,7 +399,7 @@ func TestActPool_removeConfirmedActs(t *testing.T) {
return 0, nil
}).Times(8)
sf.EXPECT().Height().Return(uint64(1), nil).AnyTimes()
ctx := genesis.WithGenesisContext(context.Background(), config.Default.Genesis)
ctx := genesis.WithGenesisContext(context.Background(), genesis.Default)
require.NoError(ap.Add(ctx, tsf1))
require.NoError(ap.Add(ctx, tsf2))
require.NoError(ap.Add(ctx, tsf3))
Expand Down Expand Up @@ -506,7 +505,7 @@ func TestActPool_Reset(t *testing.T) {
tsf9, err := action.SignedTransfer(_addr1, _priKey3, uint64(4), big.NewInt(100), []byte{}, uint64(20000), big.NewInt(0))
require.NoError(err)

ctx := genesis.WithGenesisContext(context.Background(), config.Default.Genesis)
ctx := genesis.WithGenesisContext(context.Background(), genesis.Default)
require.NoError(ap1.Add(ctx, tsf1))
require.NoError(ap1.Add(ctx, tsf2))
err = ap1.Add(ctx, tsf3)
Expand Down Expand Up @@ -815,7 +814,7 @@ func TestActPool_removeInvalidActs(t *testing.T) {
return 0, nil
}).Times(8)
sf.EXPECT().Height().Return(uint64(1), nil).AnyTimes()
ctx := genesis.WithGenesisContext(context.Background(), config.Default.Genesis)
ctx := genesis.WithGenesisContext(context.Background(), genesis.Default)
require.NoError(ap.Add(ctx, tsf1))
require.NoError(ap.Add(ctx, tsf2))
require.NoError(ap.Add(ctx, tsf3))
Expand Down Expand Up @@ -862,7 +861,7 @@ func TestActPool_GetPendingNonce(t *testing.T) {
}).Times(10)
sf.EXPECT().Height().Return(uint64(1), nil).AnyTimes()

ctx := genesis.WithGenesisContext(context.Background(), config.Default.Genesis)
ctx := genesis.WithGenesisContext(context.Background(), genesis.Default)
require.NoError(ap.Add(ctx, tsf1))
require.NoError(ap.Add(ctx, tsf3))
require.NoError(ap.Add(ctx, tsf4))
Expand Down Expand Up @@ -910,7 +909,7 @@ func TestActPool_GetUnconfirmedActs(t *testing.T) {
return 0, nil
}).Times(10)
sf.EXPECT().Height().Return(uint64(1), nil).AnyTimes()
ctx := genesis.WithGenesisContext(context.Background(), config.Default.Genesis)
ctx := genesis.WithGenesisContext(context.Background(), genesis.Default)
require.NoError(ap.Add(ctx, tsf1))
require.NoError(ap.Add(ctx, tsf3))
require.NoError(ap.Add(ctx, tsf4))
Expand Down Expand Up @@ -1011,7 +1010,7 @@ func TestActPool_GetSize(t *testing.T) {
return 0, nil
}).Times(8)
sf.EXPECT().Height().Return(uint64(1), nil).AnyTimes()
ctx := genesis.WithGenesisContext(context.Background(), config.Default.Genesis)
ctx := genesis.WithGenesisContext(context.Background(), genesis.Default)
require.NoError(ap.Add(ctx, tsf1))
require.NoError(ap.Add(ctx, tsf2))
require.NoError(ap.Add(ctx, tsf3))
Expand All @@ -1037,7 +1036,7 @@ func TestActPool_AddActionNotEnoughGasPrice(t *testing.T) {
ctrl := gomock.NewController(t)
sf := mock_chainmanager.NewMockStateReader(ctrl)

apConfig := config.Default.ActPool
apConfig := DefaultConfig
ap, err := NewActPool(genesis.Default, sf, apConfig, EnableExperimentalActions())
require.NoError(t, err)
tsf, err := action.SignedTransfer(
Expand All @@ -1052,7 +1051,7 @@ func TestActPool_AddActionNotEnoughGasPrice(t *testing.T) {
require.NoError(t, err)

ctx := protocol.WithBlockchainCtx(context.Background(), protocol.BlockchainCtx{})
ctx = genesis.WithGenesisContext(ctx, config.Default.Genesis)
ctx = genesis.WithGenesisContext(ctx, genesis.Default)
require.Error(t, ap.Add(ctx, tsf))
}

Expand Down Expand Up @@ -1088,7 +1087,7 @@ func TestActPool_SpeedUpAction(t *testing.T) {
require.NoError(err)

// A send action tsf1 with nonce 1, B send action tsf2 with nonce 1
ctx := genesis.WithGenesisContext(context.Background(), config.Default.Genesis)
ctx := genesis.WithGenesisContext(context.Background(), genesis.Default)
require.NoError(ap.Add(ctx, tsf1))
require.NoError(ap.Add(ctx, tsf2))

Expand Down Expand Up @@ -1140,7 +1139,7 @@ func (ap *actPool) getPendingNonce(addr string) (uint64, error) {
return 0, err
}
committedState, err := accountutil.AccountState(
genesis.WithGenesisContext(context.Background(), config.Default.Genesis),
genesis.WithGenesisContext(context.Background(), genesis.Default),
ap.sf,
_addr1,
)
Expand All @@ -1160,7 +1159,7 @@ func (ap *actPool) getPendingBalance(addr string) (*big.Int, error) {
return nil, err
}
state, err := accountutil.AccountState(
genesis.WithGenesisContext(context.Background(), config.Default.Genesis),
genesis.WithGenesisContext(context.Background(), genesis.Default),
ap.sf,
_addr1,
)
Expand All @@ -1171,8 +1170,8 @@ func (ap *actPool) getPendingBalance(addr string) (*big.Int, error) {
return state.Balance, nil
}

func getActPoolCfg() config.ActPool {
return config.ActPool{
func getActPoolCfg() Config {
return Config{
MaxNumActsPerPool: _maxNumActsPerPool,
MaxGasLimitPerPool: _maxGasLimitPerPool,
MaxNumActsPerAcct: _maxNumActsPerAcct,
Expand Down
7 changes: 3 additions & 4 deletions actpool/actqueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/blockchain/genesis"
"github.com/iotexproject/iotex-core/config"
"github.com/iotexproject/iotex-core/state"
"github.com/iotexproject/iotex-core/test/identityset"
"github.com/iotexproject/iotex-core/test/mock/mock_chainmanager"
Expand Down Expand Up @@ -128,14 +127,14 @@ func TestActQueueUpdateNonce(t *testing.T) {
func TestActQueuePendingActs(t *testing.T) {
ctrl := gomock.NewController(t)
require := require.New(t)
cfg := config.Default

sf := mock_chainmanager.NewMockStateReader(ctrl)
sf.EXPECT().State(gomock.Any(), gomock.Any()).Do(func(accountState *state.Account, _ protocol.StateOption) {
require.NoError(accountState.SetPendingNonce(accountState.PendingNonce() + 1))
}).Return(uint64(0), nil).Times(1)
sf.EXPECT().Height().Return(uint64(1), nil).AnyTimes()
ctx := genesis.WithGenesisContext(context.Background(), config.Default.Genesis)
ap, err := NewActPool(cfg.Genesis, sf, cfg.ActPool, EnableExperimentalActions())
ctx := genesis.WithGenesisContext(context.Background(), genesis.Default)
ap, err := NewActPool(genesis.Default, sf, DefaultConfig, EnableExperimentalActions())
require.NoError(err)
q := NewActQueue(ap.(*actPool), identityset.Address(0).String()).(*actQueue)
tsf1, err := action.SignedTransfer(_addr2, _priKey1, 2, big.NewInt(100), nil, uint64(0), big.NewInt(0))
Expand Down
Loading