Skip to content

Commit 4fe70bc

Browse files
committed
chainmanager fork
1 parent 4fdd995 commit 4fe70bc

File tree

18 files changed

+878
-154
lines changed

18 files changed

+878
-154
lines changed

blockchain/blockchain.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/prometheus/client_golang/prometheus"
2222
"go.uber.org/zap"
2323

24-
"github.com/iotexproject/iotex-core/v2/action"
2524
"github.com/iotexproject/iotex-core/v2/action/protocol"
2625
"github.com/iotexproject/iotex-core/v2/blockchain/block"
2726
"github.com/iotexproject/iotex-core/v2/blockchain/blockdao"
@@ -116,7 +115,8 @@ type (
116115
// BlockBuilderFactory is the factory interface of block builder
117116
BlockBuilderFactory interface {
118117
// NewBlockBuilder creates block builder
119-
NewBlockBuilder(context.Context, func(action.Envelope) (*action.SealedEnvelope, error)) (*block.Builder, error)
118+
Mint(ctx context.Context, pk crypto.PrivateKey) (*block.Block, error)
119+
Init(hash.Hash256)
120120
}
121121

122122
// blockchain implements the Blockchain interface
@@ -253,7 +253,17 @@ func (bc *blockchain) Start(ctx context.Context) error {
253253
GetBlockTime: bc.getBlockTime,
254254
},
255255
), bc.genesis))
256-
return bc.lifecycle.OnStart(ctx)
256+
err := bc.lifecycle.OnStart(ctx)
257+
if err != nil {
258+
return err
259+
}
260+
// init block builder factory
261+
if tip, err := bc.tipInfo(bc.TipHeight()); err != nil {
262+
return errors.Wrap(err, "failed to get tip info")
263+
} else {
264+
bc.bbf.Init(tip.Hash)
265+
return nil
266+
}
257267
}
258268

259269
// Stop stops the blockchain.
@@ -455,22 +465,13 @@ func (bc *blockchain) MintNewBlock(timestamp time.Time, opts ...MintOption) (*bl
455465
ctx = bc.contextWithBlock(ctx, minterAddress, newblockHeight, timestamp, protocol.CalcBaseFee(genesis.MustExtractGenesisContext(ctx).Blockchain, &tip), protocol.CalcExcessBlobGas(tip.ExcessBlobGas, tip.BlobGasUsed))
456466
ctx = protocol.WithFeatureCtx(ctx)
457467
// run execution and update state trie root hash
458-
blockBuilder, err := bc.bbf.NewBlockBuilder(
459-
ctx,
460-
func(elp action.Envelope) (*action.SealedEnvelope, error) {
461-
return action.Sign(elp, producerPrivateKey)
462-
},
463-
)
464-
if err != nil {
465-
return nil, errors.Wrapf(err, "failed to create block builder at new block height %d", newblockHeight)
466-
}
467-
blk, err := blockBuilder.SignAndBuild(producerPrivateKey)
468+
blk, err := bc.bbf.Mint(ctx, producerPrivateKey)
468469
if err != nil {
469470
return nil, errors.Wrapf(err, "failed to create block")
470471
}
471472
_blockMtc.WithLabelValues("MintGas").Set(float64(blk.GasUsed()))
472473
_blockMtc.WithLabelValues("MintActions").Set(float64(len(blk.Actions)))
473-
return &blk, nil
474+
return blk, nil
474475
}
475476

476477
// CommitBlock validates and appends a block to the chain

chainservice/builder.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,8 +513,8 @@ func (builder *Builder) createBlockchain(forSubChain, forTest bool) blockchain.B
513513
if builder.cfg.Consensus.Scheme == config.RollDPoSScheme {
514514
mintOpts = append(mintOpts, factory.WithTimeoutOption(builder.cfg.Chain.MintTimeout))
515515
}
516-
minter := factory.NewMinter(builder.cs.factory, builder.cs.actpool, mintOpts...)
517-
return blockchain.NewBlockchain(builder.cfg.Chain, builder.cfg.Genesis, builder.cs.blockdao, minter, chainOpts...)
516+
builder.cs.minter = factory.NewMinter(builder.cs.factory, builder.cs.actpool, mintOpts...)
517+
return blockchain.NewBlockchain(builder.cfg.Chain, builder.cfg.Genesis, builder.cs.blockdao, builder.cs.minter, chainOpts...)
518518
}
519519

520520
func (builder *Builder) buildNodeInfoManager() error {
@@ -781,6 +781,7 @@ func (builder *Builder) buildConsensusComponent() error {
781781
}
782782
if rDPoSProtocol := rolldpos.FindProtocol(builder.cs.registry); rDPoSProtocol != nil {
783783
copts = append(copts, consensus.WithRollDPoSProtocol(rDPoSProtocol))
784+
copts = append(copts, consensus.WithBlockBuilderFactory(builder.cs.minter))
784785
}
785786
if pollProtocol := poll.FindProtocol(builder.cs.registry); pollProtocol != nil {
786787
copts = append(copts, consensus.WithPollProtocol(pollProtocol))

chainservice/chainservice.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ type ChainService struct {
7777
nodeInfoManager *nodeinfo.InfoManager
7878
apiStats *nodestats.APILocalStats
7979
actionsync *actsync.ActionSync
80+
minter *factory.Minter
8081
}
8182

8283
// Start starts the server

consensus/consensus.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
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.
8291
func 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

Comments
 (0)