Skip to content

Commit

Permalink
[config] move config.Consensus to consensus package (#3735)
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie authored Dec 24, 2022
1 parent 6d507a2 commit 027edc8
Show file tree
Hide file tree
Showing 12 changed files with 445 additions and 299 deletions.
12 changes: 11 additions & 1 deletion chainservice/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/iotexproject/iotex-core/blocksync"
"github.com/iotexproject/iotex-core/config"
"github.com/iotexproject/iotex-core/consensus"
rp "github.com/iotexproject/iotex-core/consensus/scheme/rolldpos"
"github.com/iotexproject/iotex-core/db"
"github.com/iotexproject/iotex-core/p2p"
"github.com/iotexproject/iotex-core/pkg/log"
Expand Down Expand Up @@ -557,7 +558,16 @@ func (builder *Builder) buildConsensusComponent() error {
}

// TODO: explorer dependency deleted at #1085, need to revive by migrating to api
component, err := consensus.NewConsensus(builder.cfg, builder.cs.chain, builder.cs.factory, copts...)
builderCfg := rp.BuilderConfig{
Chain: builder.cfg.Chain,
Consensus: builder.cfg.Consensus.RollDPoS,
Scheme: builder.cfg.Consensus.Scheme,
DardanellesUpgrade: builder.cfg.DardanellesUpgrade,
DB: builder.cfg.DB,
Genesis: builder.cfg.Genesis,
SystemActive: builder.cfg.System.Active,
}
component, err := consensus.NewConsensus(builderCfg, builder.cs.chain, builder.cs.factory, copts...)
if err != nil {
return errors.Wrap(err, "failed to create consensus component")
}
Expand Down
109 changes: 24 additions & 85 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/iotexproject/iotex-core/blockchain"
"github.com/iotexproject/iotex-core/blockchain/genesis"
"github.com/iotexproject/iotex-core/blockindex"
"github.com/iotexproject/iotex-core/consensus"
"github.com/iotexproject/iotex-core/consensus/consensusfsm"
"github.com/iotexproject/iotex-core/db"
"github.com/iotexproject/iotex-core/dispatcher"
"github.com/iotexproject/iotex-core/p2p"
Expand Down Expand Up @@ -57,38 +59,13 @@ func (ss *strs) Set(str string) error {
var (
// Default is the default config
Default = Config{
Plugins: make(map[int]interface{}),
SubLogs: make(map[string]log.GlobalConfig),
Network: p2p.DefaultConfig,
Chain: blockchain.DefaultConfig,
ActPool: actpool.DefaultConfig,
Consensus: Consensus{
Scheme: StandaloneScheme,
RollDPoS: RollDPoS{
FSM: ConsensusTiming{
UnmatchedEventTTL: 3 * time.Second,
UnmatchedEventInterval: 100 * time.Millisecond,
AcceptBlockTTL: 4 * time.Second,
AcceptProposalEndorsementTTL: 2 * time.Second,
AcceptLockEndorsementTTL: 2 * time.Second,
CommitTTL: 2 * time.Second,
EventChanSize: 10000,
},
ToleratedOvertime: 2 * time.Second,
Delay: 5 * time.Second,
ConsensusDBPath: "/var/data/consensus.db",
},
},
DardanellesUpgrade: DardanellesUpgrade{
UnmatchedEventTTL: 2 * time.Second,
UnmatchedEventInterval: 100 * time.Millisecond,
AcceptBlockTTL: 2 * time.Second,
AcceptProposalEndorsementTTL: time.Second,
AcceptLockEndorsementTTL: time.Second,
CommitTTL: time.Second,
BlockInterval: 5 * time.Second,
Delay: 2 * time.Second,
},
Plugins: make(map[int]interface{}),

This comment has been minimized.

Copy link
@Liuhaai

Liuhaai Dec 27, 2022

Member

Why is the code on L35 unremoved?

SubLogs: make(map[string]log.GlobalConfig),
Network: p2p.DefaultConfig,
Chain: blockchain.DefaultConfig,
ActPool: actpool.DefaultConfig,
Consensus: consensus.DefaultConfig,
DardanellesUpgrade: consensusfsm.DefaultDardanellesUpgradeConfig,
BlockSync: BlockSync{
Interval: 30 * time.Second,
ProcessSyncRequestTTL: 10 * time.Second,
Expand Down Expand Up @@ -140,13 +117,6 @@ var (

// Network is the config struct for network package
type (
// Consensus is the config struct for consensus package
Consensus struct {
// There are three schemes that are supported
Scheme string `yaml:"scheme"`
RollDPoS RollDPoS `yaml:"rollDPoS"`
}

// BlockSync is the config struct for the BlockSync
BlockSync struct {
Interval time.Duration `yaml:"interval"` // update duration
Expand All @@ -159,37 +129,6 @@ type (
RepeatDecayStep int `yaml:"repeatDecayStep"`
}

// DardanellesUpgrade is the config for dardanelles upgrade
DardanellesUpgrade struct {
UnmatchedEventTTL time.Duration `yaml:"unmatchedEventTTL"`
UnmatchedEventInterval time.Duration `yaml:"unmatchedEventInterval"`
AcceptBlockTTL time.Duration `yaml:"acceptBlockTTL"`
AcceptProposalEndorsementTTL time.Duration `yaml:"acceptProposalEndorsementTTL"`
AcceptLockEndorsementTTL time.Duration `yaml:"acceptLockEndorsementTTL"`
CommitTTL time.Duration `yaml:"commitTTL"`
BlockInterval time.Duration `yaml:"blockInterval"`
Delay time.Duration `yaml:"delay"`
}

// RollDPoS is the config struct for RollDPoS consensus package
RollDPoS struct {
FSM ConsensusTiming `yaml:"fsm"`
ToleratedOvertime time.Duration `yaml:"toleratedOvertime"`
Delay time.Duration `yaml:"delay"`
ConsensusDBPath string `yaml:"consensusDBPath"`
}

// ConsensusTiming defines a set of time durations used in fsm and event queue size
ConsensusTiming struct {
EventChanSize uint `yaml:"eventChanSize"`
UnmatchedEventTTL time.Duration `yaml:"unmatchedEventTTL"`
UnmatchedEventInterval time.Duration `yaml:"unmatchedEventInterval"`
AcceptBlockTTL time.Duration `yaml:"acceptBlockTTL"`
AcceptProposalEndorsementTTL time.Duration `yaml:"acceptProposalEndorsementTTL"`
AcceptLockEndorsementTTL time.Duration `yaml:"acceptLockEndorsementTTL"`
CommitTTL time.Duration `yaml:"commitTTL"`
}

// API is the api service config
API struct {
UseRDS bool `yaml:"useRDS"`
Expand Down Expand Up @@ -225,21 +164,21 @@ type (

// Config is the root config struct, each package's config should be put as its sub struct
Config struct {
Plugins map[int]interface{} `ymal:"plugins"`
Network p2p.Config `yaml:"network"`
Chain blockchain.Config `yaml:"chain"`
ActPool actpool.Config `yaml:"actPool"`
Consensus Consensus `yaml:"consensus"`
DardanellesUpgrade DardanellesUpgrade `yaml:"dardanellesUpgrade"`
BlockSync BlockSync `yaml:"blockSync"`
Dispatcher dispatcher.Config `yaml:"dispatcher"`
API API `yaml:"api"`
System System `yaml:"system"`
DB db.Config `yaml:"db"`
Indexer blockindex.Config `yaml:"indexer"`
Log log.GlobalConfig `yaml:"log"`
SubLogs map[string]log.GlobalConfig `yaml:"subLogs"`
Genesis genesis.Genesis `yaml:"genesis"`
Plugins map[int]interface{} `ymal:"plugins"`
Network p2p.Config `yaml:"network"`
Chain blockchain.Config `yaml:"chain"`
ActPool actpool.Config `yaml:"actPool"`
Consensus consensus.Config `yaml:"consensus"`
DardanellesUpgrade consensusfsm.DardanellesUpgrade `yaml:"dardanellesUpgrade"`
BlockSync BlockSync `yaml:"blockSync"`
Dispatcher dispatcher.Config `yaml:"dispatcher"`
API API `yaml:"api"`
System System `yaml:"system"`
DB db.Config `yaml:"db"`
Indexer blockindex.Config `yaml:"indexer"`
Log log.GlobalConfig `yaml:"log"`
SubLogs map[string]log.GlobalConfig `yaml:"subLogs"`
Genesis genesis.Genesis `yaml:"genesis"`
}

// Validate is the interface of validating the config
Expand Down
31 changes: 31 additions & 0 deletions consensus/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package consensus

import (
"github.com/iotexproject/iotex-core/consensus/scheme/rolldpos"
)

const (
// RollDPoSScheme means randomized delegated proof of stake
RollDPoSScheme = "ROLLDPOS"
// StandaloneScheme means that the node creates a block periodically regardless of others (if there is any)
StandaloneScheme = "STANDALONE"
// NOOPScheme means that the node does not create only block
NOOPScheme = "NOOP"
)

var (
//DefaultConfig is the default config for blocksync
DefaultConfig = Config{
Scheme: StandaloneScheme,
RollDPoS: rolldpos.DefaultConfig,
}
)

type (
// Config is the config struct for consensus package
Config struct {
// There are three schemes that are supported
Scheme string `yaml:"scheme"`
RollDPoS rolldpos.Config `yaml:"rollDPoS"`
}
)
22 changes: 12 additions & 10 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"context"

"github.com/facebookgo/clock"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"
"go.uber.org/zap"

Expand All @@ -18,14 +19,12 @@ import (
"github.com/iotexproject/iotex-core/blockchain"
"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/consensus/scheme"
"github.com/iotexproject/iotex-core/consensus/scheme/rolldpos"
"github.com/iotexproject/iotex-core/pkg/lifecycle"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/state"
"github.com/iotexproject/iotex-core/state/factory"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
)

// Consensus is the interface for handling IotxConsensus view change.
Expand All @@ -42,7 +41,7 @@ type Consensus interface {

// IotxConsensus implements Consensus
type IotxConsensus struct {
cfg config.Consensus
cfg Config
scheme scheme.Scheme
}

Expand Down Expand Up @@ -81,7 +80,7 @@ func WithPollProtocol(pp poll.Protocol) Option {

// NewConsensus creates a IotxConsensus struct.
func NewConsensus(
cfg config.Config,
cfg rolldpos.BuilderConfig,

This comment has been minimized.

Copy link
@CoderZhi

CoderZhi Dec 27, 2022

Collaborator

NewConsensus creates consensus struct of all types, thus, we shouldn't use rolldpos.BuilderConfig.

bc blockchain.Blockchain,
sf factory.Factory,
opts ...Option,
Expand All @@ -94,10 +93,13 @@ func NewConsensus(
}

clock := clock.New()
cs := &IotxConsensus{cfg: cfg.Consensus}
cs := &IotxConsensus{cfg: Config{
Scheme: cfg.Scheme,
RollDPoS: cfg.Consensus,
}}
var err error
switch cfg.Consensus.Scheme {
case config.RollDPoSScheme:
switch cfg.Scheme {
case RollDPoSScheme:
bd := rolldpos.NewRollDPoSBuilder().
SetAddr(cfg.Chain.ProducerAddress().String()).
SetPriKey(cfg.Chain.ProducerPrivateKey()).
Expand Down Expand Up @@ -143,9 +145,9 @@ func NewConsensus(
if err != nil {
log.Logger("consensus").Panic("Error when constructing RollDPoS.", zap.Error(err))
}
case config.NOOPScheme:
case NOOPScheme:
cs.scheme = scheme.NewNoop()
case config.StandaloneScheme:
case StandaloneScheme:
mintBlockCB := func() (*block.Block, error) {
blk, err := bc.MintNewBlock(clock.Now())
if err != nil {
Expand Down Expand Up @@ -178,7 +180,7 @@ func NewConsensus(
cfg.Genesis.BlockInterval,
)
default:
return nil, errors.Errorf("unexpected IotxConsensus scheme %s", cfg.Consensus.Scheme)
return nil, errors.Errorf("unexpected IotxConsensus scheme %s", cfg.Scheme)
}

return cs, nil
Expand Down
Loading

0 comments on commit 027edc8

Please sign in to comment.