Skip to content

Commit 8372b72

Browse files
committed
move pool into forkchain
1 parent 972fdb5 commit 8372b72

File tree

5 files changed

+21
-57
lines changed

5 files changed

+21
-57
lines changed

blockchain/blockchain.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ type (
116116
BlockBuilderFactory interface {
117117
// NewBlockBuilder creates block builder
118118
Mint(ctx context.Context, pk crypto.PrivateKey) (*block.Block, error)
119-
Init(hash.Hash256)
120119
}
121120

122121
// blockchain implements the Blockchain interface
@@ -257,13 +256,7 @@ func (bc *blockchain) Start(ctx context.Context) error {
257256
if err != nil {
258257
return err
259258
}
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-
}
259+
return nil
267260
}
268261

269262
// Stop stops the blockchain.

consensus/scheme/rolldpos/chainmanager.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ type (
7171
BlockBuilderFactory interface {
7272
Mint(ctx context.Context, pk crypto.PrivateKey) (*block.Block, error)
7373
ReceiveBlock(*block.Block) error
74-
Init(hash.Hash256)
75-
AddProposal(*block.Block) error
76-
Block(hash.Hash256) *block.Block
7774
}
7875

7976
chainManager struct {
@@ -87,14 +84,15 @@ type (
8784
sr protocol.StateReader
8885
timerFactory *prometheustimer.TimerFactory
8986
bbf BlockBuilderFactory
87+
pool *proposalPool
9088
}
9189
)
9290

9391
func init() {
9492
prometheus.MustRegister(blockMtc)
9593
}
9694

97-
func newForkChain(bc blockchain.Blockchain, head *block.Header, sr protocol.StateReader, bbf BlockBuilderFactory) *forkChain {
95+
func newForkChain(bc blockchain.Blockchain, head *block.Header, sr protocol.StateReader, bbf BlockBuilderFactory, pool *proposalPool) *forkChain {
9896
timerFactory, err := prometheustimer.New(
9997
"iotex_blockchain_perf",
10098
"Performance of blockchain module",
@@ -110,13 +108,14 @@ func newForkChain(bc blockchain.Blockchain, head *block.Header, sr protocol.Stat
110108
sr: sr,
111109
bbf: bbf,
112110
timerFactory: timerFactory,
111+
pool: pool,
113112
}
114113
}
115114

116115
// NewChainManager creates a chain manager
117116
func NewChainManager(bc blockchain.Blockchain, srf StateReaderFactory, bbf BlockBuilderFactory) ChainManager {
118117
return &chainManager{
119-
forkChain: newForkChain(bc, nil, nil, bbf),
118+
forkChain: newForkChain(bc, nil, nil, bbf, newProposalPool()),
120119
srf: srf,
121120
}
122121
}
@@ -169,7 +168,7 @@ func (fc *forkChain) MintNewBlock(timestamp time.Time, pk crypto.PrivateKey, pre
169168
if err != nil {
170169
return nil, errors.Wrapf(err, "failed to create block")
171170
}
172-
if err = fc.bbf.AddProposal(blk); err != nil {
171+
if err = fc.pool.AddBlock(blk); err != nil {
173172
blkHash := blk.HashBlock()
174173
log.L().Error("failed to add proposal", zap.Error(err), zap.Uint64("height", blk.Height()), log.Hex("hash", blkHash[:]))
175174
}
@@ -204,7 +203,7 @@ func (cm *forkChain) ValidateBlock(blk *block.Block) error {
204203
if err != nil {
205204
return err
206205
}
207-
if err = cm.bbf.AddProposal(blk); err != nil {
206+
if err = cm.pool.AddBlock(blk); err != nil {
208207
blkHash := blk.HashBlock()
209208
log.L().Error("failed to add proposal", zap.Error(err), zap.Uint64("height", blk.Height()), log.Hex("hash", blkHash[:]))
210209
}
@@ -220,6 +219,10 @@ func (cm *forkChain) CommitBlock(blk *block.Block) error {
220219
blkHash := blk.HashBlock()
221220
log.L().Error("failed to receive block", zap.Error(err), zap.Uint64("height", blk.Height()), log.Hex("hash", blkHash[:]))
222221
}
222+
if err := cm.pool.ReceiveBlock(blk); err != nil {
223+
blkHash := blk.HashBlock()
224+
log.L().Error("failed to receive block", zap.Error(err), zap.Uint64("height", blk.Height()), log.Hex("hash", blkHash[:]))
225+
}
223226
return nil
224227
}
225228

@@ -232,8 +235,9 @@ func (cm *chainManager) Start(ctx context.Context) error {
232235
if err != nil {
233236
return errors.Wrapf(err, "failed to create state reader at %d, hash %x", head.Height(), head.HashBlock())
234237
}
235-
cm.forkChain = newForkChain(cm.bc, head, sr, cm.bbf)
236-
cm.bbf.Init(cm.forkChain.TipHash())
238+
cm.forkChain.head = head
239+
cm.forkChain.sr = sr
240+
cm.pool.Init(cm.forkChain.TipHash())
237241
return nil
238242
}
239243

@@ -245,7 +249,7 @@ func (cm *chainManager) Final() ForkChain {
245249
func (cm *chainManager) Fork(hash hash.Hash256) (ForkChain, error) {
246250
head := cm.head
247251
if hash != cm.tipHash() {
248-
blk := cm.bbf.Block(hash)
252+
blk := cm.pool.BlockByHash(hash)
249253
if blk == nil {
250254
return nil, errors.Errorf("block %x not found when fork", hash)
251255
}
@@ -255,11 +259,11 @@ func (cm *chainManager) Fork(hash hash.Hash256) (ForkChain, error) {
255259
if err != nil {
256260
return nil, errors.Wrapf(err, "failed to create state reader at %d, hash %x", head.Height(), head.HashBlock())
257261
}
258-
return newForkChain(cm.bc, head, sr, cm.bbf), nil
262+
return newForkChain(cm.bc, head, sr, cm.bbf, cm.pool), nil
259263
}
260264

261265
func (fc *forkChain) draftBlockByHeight(height uint64) *block.Block {
262-
for blk := fc.bbf.Block(fc.tipHash()); blk != nil && blk.Height() >= height; blk = fc.bbf.Block(blk.PrevHash()) {
266+
for blk := fc.pool.BlockByHash(fc.tipHash()); blk != nil && blk.Height() >= height; blk = fc.pool.BlockByHash(blk.PrevHash()) {
263267
if blk.Height() == height {
264268
return blk
265269
}

state/factory/proposalpool.go renamed to consensus/scheme/rolldpos/proposalpool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package factory
1+
package rolldpos
22

33
import (
44
"sync"

consensus/scheme/rolldpos/rolldpos.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,7 @@ func (r *RollDPoS) Calibrate(height uint64) {
162162
// ValidateBlockFooter validates the signatures in the block footer
163163
func (r *RollDPoS) ValidateBlockFooter(blk *block.Block) error {
164164
height := blk.Height()
165-
prevHash := blk.PrevHash()
166-
fork, err := r.ctx.Chain().Fork(prevHash)
167-
if err != nil {
168-
return errors.Wrapf(err, "failed to get fork at height %d with prevHash %x", height, prevHash)
169-
}
165+
fork := r.ctx.Chain().Final()
170166
roundCalc := r.ctx.RoundCalculator().Fork(fork)
171167
round, err := roundCalc.NewRound(height, r.ctx.BlockInterval(height), blk.Timestamp(), nil)
172168
if err != nil {

state/factory/minter.go

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@ import (
1010
"sync"
1111
"time"
1212

13-
"go.uber.org/zap"
14-
1513
"github.com/iotexproject/go-pkgs/crypto"
16-
"github.com/iotexproject/go-pkgs/hash"
14+
"github.com/pkg/errors"
1715

1816
"github.com/iotexproject/iotex-core/v2/action"
1917
"github.com/iotexproject/iotex-core/v2/action/protocol"
2018
"github.com/iotexproject/iotex-core/v2/actpool"
2119
"github.com/iotexproject/iotex-core/v2/blockchain/block"
22-
"github.com/iotexproject/iotex-core/v2/pkg/log"
2320
)
2421

2522
type MintOption func(*Minter)
@@ -36,7 +33,6 @@ type Minter struct {
3633
ap actpool.ActPool
3734
timeout time.Duration
3835
blockPreparer *blockPreparer
39-
proposalPool *proposalPool
4036
mu sync.Mutex
4137
}
4238

@@ -46,18 +42,13 @@ func NewMinter(f Factory, ap actpool.ActPool, opts ...MintOption) *Minter {
4642
f: f,
4743
ap: ap,
4844
blockPreparer: newBlockPreparer(),
49-
proposalPool: newProposalPool(),
5045
}
5146
for _, opt := range opts {
5247
opt(m)
5348
}
5449
return m
5550
}
5651

57-
func (m *Minter) Init(root hash.Hash256) {
58-
m.proposalPool.Init(root)
59-
}
60-
6152
func (m *Minter) Mint(ctx context.Context, pk crypto.PrivateKey) (*block.Block, error) {
6253
bcCtx := protocol.MustGetBlockchainCtx(ctx)
6354
blkCtx := protocol.MustGetBlockCtx(ctx)
@@ -68,9 +59,6 @@ func (m *Minter) Mint(ctx context.Context, pk crypto.PrivateKey) (*block.Block,
6859
if err != nil {
6960
return nil, err
7061
}
71-
if err = m.proposalPool.AddBlock(blk); err != nil {
72-
log.L().Error("failed to add block to proposal pool", zap.Error(err))
73-
}
7462
return blk, nil
7563
})
7664
if err != nil {
@@ -79,30 +67,13 @@ func (m *Minter) Mint(ctx context.Context, pk crypto.PrivateKey) (*block.Block,
7967
return blk, nil
8068
}
8169

82-
func (m *Minter) AddProposal(blk *block.Block) error {
83-
return m.proposalPool.AddBlock(blk)
84-
}
85-
8670
func (m *Minter) ReceiveBlock(blk *block.Block) error {
87-
prevHash := blk.PrevHash()
88-
l := log.L().With(zap.Uint64("height", blk.Height()), log.Hex("prevHash", prevHash[:]), zap.Time("timestamp", blk.Timestamp()))
8971
if err := m.blockPreparer.ReceiveBlock(blk); err != nil {
90-
l.Error("failed to receive block", zap.Error(err))
91-
}
92-
if err := m.proposalPool.ReceiveBlock(blk); err != nil {
93-
l.Error("failed to receive block", zap.Error(err))
72+
return errors.Wrap(err, "failed to receive block")
9473
}
9574
return nil
9675
}
9776

98-
func (m *Minter) Block(hash hash.Hash256) *block.Block {
99-
return m.proposalPool.BlockByHash(hash)
100-
}
101-
102-
func (m *Minter) BlockByHeight(height uint64) *block.Block {
103-
return m.proposalPool.Block(height)
104-
}
105-
10677
func (m *Minter) mint(ctx context.Context, pk crypto.PrivateKey) (*block.Block, error) {
10778
builder, err := m.newBlockBuilder(ctx, func(e action.Envelope) (*action.SealedEnvelope, error) {
10879
return action.Sign(e, pk)

0 commit comments

Comments
 (0)