99 "context"
1010
1111 "github.com/facebookgo/clock"
12+ "github.com/iotexproject/go-pkgs/hash"
1213 "github.com/iotexproject/iotex-proto/golang/iotextypes"
1314 "github.com/pkg/errors"
1415 "go.uber.org/zap"
@@ -24,7 +25,6 @@ import (
2425 "github.com/iotexproject/iotex-core/v2/pkg/lifecycle"
2526 "github.com/iotexproject/iotex-core/v2/pkg/log"
2627 "github.com/iotexproject/iotex-core/v2/state"
27- "github.com/iotexproject/iotex-core/v2/state/factory"
2828)
2929
3030// Consensus is the interface for handling IotxConsensus view change.
@@ -49,6 +49,7 @@ type optionParams struct {
4949 broadcastHandler scheme.Broadcast
5050 pp poll.Protocol
5151 rp * rp.Protocol
52+ bbf rolldpos.BlockBuilderFactory
5253}
5354
5455// Option sets Consensus construction parameter.
@@ -78,11 +79,19 @@ func WithPollProtocol(pp poll.Protocol) Option {
7879 }
7980}
8081
82+ // WithBlockBuilderFactory is an option to set block builder factory
83+ func WithBlockBuilderFactory (bbf rolldpos.BlockBuilderFactory ) Option {
84+ return func (ops * optionParams ) error {
85+ ops .bbf = bbf
86+ return nil
87+ }
88+ }
89+
8190// NewConsensus creates a IotxConsensus struct.
8291func NewConsensus (
8392 cfg rolldpos.BuilderConfig ,
8493 bc blockchain.Blockchain ,
85- sf factory. Factory ,
94+ sf rolldpos. StateReaderFactory ,
8695 opts ... Option ,
8796) (Consensus , error ) {
8897 var ops optionParams
@@ -100,7 +109,16 @@ func NewConsensus(
100109 var err error
101110 switch cfg .Scheme {
102111 case RollDPoSScheme :
103- delegatesByEpochFunc := func (epochNum uint64 ) ([]string , error ) {
112+ if ops .bbf == nil {
113+ return nil , errors .New ("block builder factory is not set" )
114+ }
115+ chanMgr := rolldpos .NewChainManager (bc , sf , ops .bbf )
116+ delegatesByEpochFunc := func (epochNum uint64 , prevHash []byte ) ([]string , error ) {
117+ fork , serr := chanMgr .Fork (hash .Hash256 (prevHash ))
118+ if serr != nil {
119+ return nil , serr
120+ }
121+ forkSF := fork .StateReader ()
104122 re := protocol .NewRegistry ()
105123 if err := ops .rp .Register (re ); err != nil {
106124 return nil , err
@@ -110,15 +128,15 @@ func NewConsensus(
110128 cfg .Genesis ,
111129 )
112130 ctx = protocol .WithFeatureWithHeightCtx (ctx )
113- tipHeight := bc .TipHeight ()
131+ tipHeight := fork .TipHeight ()
114132 tipEpochNum := ops .rp .GetEpochNum (tipHeight )
115133 var candidatesList state.CandidateList
116134 var err error
117135 switch epochNum {
118136 case tipEpochNum :
119- candidatesList , err = ops .pp .Delegates (ctx , sf )
137+ candidatesList , err = ops .pp .Delegates (ctx , forkSF )
120138 case tipEpochNum + 1 :
121- candidatesList , err = ops .pp .NextDelegates (ctx , sf )
139+ candidatesList , err = ops .pp .NextDelegates (ctx , forkSF )
122140 default :
123141 err = errors .Errorf ("invalid epoch number %d compared to tip epoch number %d" , epochNum , tipEpochNum )
124142 }
@@ -135,7 +153,7 @@ func NewConsensus(
135153 bd := rolldpos .NewRollDPoSBuilder ().
136154 SetPriKey (cfg .Chain .ProducerPrivateKeys ()... ).
137155 SetConfig (cfg ).
138- SetChainManager (rolldpos . NewChainManager ( bc ) ).
156+ SetChainManager (chanMgr ).
139157 SetBlockDeserializer (block .NewDeserializer (bc .EvmNetworkID ())).
140158 SetClock (clock ).
141159 SetBroadcast (ops .broadcastHandler ).
0 commit comments