9
9
"context"
10
10
11
11
"github.com/facebookgo/clock"
12
+ "github.com/iotexproject/go-pkgs/hash"
12
13
"github.com/iotexproject/iotex-proto/golang/iotextypes"
13
14
"github.com/pkg/errors"
14
15
"go.uber.org/zap"
@@ -24,7 +25,6 @@ import (
24
25
"github.com/iotexproject/iotex-core/v2/pkg/lifecycle"
25
26
"github.com/iotexproject/iotex-core/v2/pkg/log"
26
27
"github.com/iotexproject/iotex-core/v2/state"
27
- "github.com/iotexproject/iotex-core/v2/state/factory"
28
28
)
29
29
30
30
// Consensus is the interface for handling IotxConsensus view change.
@@ -49,6 +49,7 @@ type optionParams struct {
49
49
broadcastHandler scheme.Broadcast
50
50
pp poll.Protocol
51
51
rp * rp.Protocol
52
+ bbf rolldpos.BlockBuilderFactory
52
53
}
53
54
54
55
// Option sets Consensus construction parameter.
@@ -78,11 +79,19 @@ func WithPollProtocol(pp poll.Protocol) Option {
78
79
}
79
80
}
80
81
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
+
81
90
// NewConsensus creates a IotxConsensus struct.
82
91
func NewConsensus (
83
92
cfg rolldpos.BuilderConfig ,
84
93
bc blockchain.Blockchain ,
85
- sf factory. Factory ,
94
+ sf rolldpos. StateReaderFactory ,
86
95
opts ... Option ,
87
96
) (Consensus , error ) {
88
97
var ops optionParams
@@ -100,7 +109,19 @@ func NewConsensus(
100
109
var err error
101
110
switch cfg .Scheme {
102
111
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
+ chainMgr := rolldpos .NewChainManager (bc , sf , ops .bbf )
116
+ delegatesByEpochFunc := func (epochNum uint64 , prevHash []byte ) ([]string , error ) {
117
+ fork , err := chainMgr .Fork (hash .Hash256 (prevHash ))
118
+ if err != nil {
119
+ return nil , err
120
+ }
121
+ forkSF , err := fork .StateReader ()
122
+ if err != nil {
123
+ return nil , err
124
+ }
104
125
re := protocol .NewRegistry ()
105
126
if err := ops .rp .Register (re ); err != nil {
106
127
return nil , err
@@ -110,15 +131,14 @@ func NewConsensus(
110
131
cfg .Genesis ,
111
132
)
112
133
ctx = protocol .WithFeatureWithHeightCtx (ctx )
113
- tipHeight := bc .TipHeight ()
134
+ tipHeight := fork .TipHeight ()
114
135
tipEpochNum := ops .rp .GetEpochNum (tipHeight )
115
136
var candidatesList state.CandidateList
116
- var err error
117
137
switch epochNum {
118
138
case tipEpochNum :
119
- candidatesList , err = ops .pp .Delegates (ctx , sf )
139
+ candidatesList , err = ops .pp .Delegates (ctx , forkSF )
120
140
case tipEpochNum + 1 :
121
- candidatesList , err = ops .pp .NextDelegates (ctx , sf )
141
+ candidatesList , err = ops .pp .NextDelegates (ctx , forkSF )
122
142
default :
123
143
err = errors .Errorf ("invalid epoch number %d compared to tip epoch number %d" , epochNum , tipEpochNum )
124
144
}
@@ -135,7 +155,7 @@ func NewConsensus(
135
155
bd := rolldpos .NewRollDPoSBuilder ().
136
156
SetPriKey (cfg .Chain .ProducerPrivateKeys ()... ).
137
157
SetConfig (cfg ).
138
- SetChainManager (rolldpos . NewChainManager ( bc ) ).
158
+ SetChainManager (chainMgr ).
139
159
SetBlockDeserializer (block .NewDeserializer (bc .EvmNetworkID ())).
140
160
SetClock (clock ).
141
161
SetBroadcast (ops .broadcastHandler ).
0 commit comments