Skip to content

Commit 254d811

Browse files
committed
chainmanager fork
1 parent d8420af commit 254d811

31 files changed

+895
-279
lines changed

action/protocol/execution/protocol_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,6 @@ func (sct *SmartContractTest) prepareBlockchain(
474474
r.NoError(err)
475475
dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf, indexer}, cfg.DB.MaxCacheSize)
476476
r.NotNil(dao)
477-
btcd := testutil.DummyBlockTimeBuilder()
478477
bc := blockchain.NewBlockchain(
479478
cfg.Chain,
480479
cfg.Genesis,
@@ -484,7 +483,6 @@ func (sct *SmartContractTest) prepareBlockchain(
484483
sf,
485484
protocol.NewGenericValidator(sf, accountutil.AccountState),
486485
)),
487-
blockchain.BlockTimeCalculatorBuilderOption(btcd),
488486
)
489487
reward := rewarding.NewProtocol(cfg.Genesis.Rewarding)
490488
r.NoError(reward.Register(registry))
@@ -737,7 +735,6 @@ func TestProtocol_Handle(t *testing.T) {
737735
sf,
738736
protocol.NewGenericValidator(sf, accountutil.AccountState),
739737
)),
740-
blockchain.BlockTimeCalculatorBuilderOption(testutil.DummyBlockTimeBuilder()),
741738
)
742739
exeProtocol := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGas, getBlockTimeForTest)
743740
require.NoError(exeProtocol.Register(registry))

api/serverV2_integrity_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,6 @@ func setupChain(cfg testConfig) (blockchain.Blockchain, blockdao.BlockDAO, block
330330
sf,
331331
protocol.NewGenericValidator(sf, accountutil.AccountState),
332332
)),
333-
blockchain.BlockTimeCalculatorBuilderOption(testutil.DummyBlockTimeBuilder()),
334333
)
335334
if bc == nil {
336335
return nil, nil, nil, nil, nil, nil, nil, "", errors.New("failed to create blockchain")

blockchain/blockchain.go

Lines changed: 27 additions & 48 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"
@@ -31,7 +30,6 @@ import (
3130
"github.com/iotexproject/iotex-core/v2/pkg/log"
3231
"github.com/iotexproject/iotex-core/v2/pkg/prometheustimer"
3332
"github.com/iotexproject/iotex-core/v2/pkg/unit"
34-
"github.com/iotexproject/iotex-core/v2/pkg/util/blockutil"
3533
)
3634

3735
// const
@@ -117,7 +115,8 @@ type (
117115
// BlockBuilderFactory is the factory interface of block builder
118116
BlockBuilderFactory interface {
119117
// NewBlockBuilder creates block builder
120-
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)
121120
}
122121

123122
// blockchain implements the Blockchain interface
@@ -133,8 +132,7 @@ type (
133132
timerFactory *prometheustimer.TimerFactory
134133

135134
// used by account-based model
136-
bbf BlockBuilderFactory
137-
btcBuilder *blockutil.BlockTimeCalculatorBuilder
135+
bbf BlockBuilderFactory
138136
}
139137
)
140138

@@ -179,13 +177,6 @@ func ClockOption(clk clock.Clock) Option {
179177
}
180178
}
181179

182-
func BlockTimeCalculatorBuilderOption(builder *blockutil.BlockTimeCalculatorBuilder) Option {
183-
return func(bc *blockchain) error {
184-
bc.btcBuilder = builder
185-
return nil
186-
}
187-
}
188-
189180
type (
190181
BlockValidationCfg struct {
191182
skipSidecarValidation bool
@@ -229,9 +220,6 @@ func NewBlockchain(cfg Config, g genesis.Genesis, dao blockdao.BlockDAO, bbf Blo
229220
if chain.dao == nil {
230221
log.L().Panic("blockdao is nil")
231222
}
232-
if chain.btcBuilder == nil {
233-
log.L().Panic("block time calculator builder is nil")
234-
}
235223
chain.lifecycle.Add(chain.dao)
236224
chain.lifecycle.Add(chain.pubSubManager)
237225

@@ -254,10 +242,6 @@ func (bc *blockchain) ChainAddress() string {
254242
func (bc *blockchain) Start(ctx context.Context) error {
255243
bc.mu.Lock()
256244
defer bc.mu.Unlock()
257-
btc, err := bc.buildBlockTimeCalculator()
258-
if err != nil {
259-
return err
260-
}
261245
// pass registry to be used by state factory's initialization
262246
ctx = protocol.WithFeatureWithHeightCtx(genesis.WithGenesisContext(
263247
protocol.WithBlockchainCtx(
@@ -266,10 +250,20 @@ func (bc *blockchain) Start(ctx context.Context) error {
266250
ChainID: bc.ChainID(),
267251
EvmNetworkID: bc.EvmNetworkID(),
268252
GetBlockHash: bc.dao.GetBlockHash,
269-
GetBlockTime: btc.CalculateBlockTime,
253+
GetBlockTime: bc.getBlockTime,
270254
},
271255
), bc.genesis))
272-
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+
}
273267
}
274268

275269
// Stop stops the blockchain.
@@ -423,10 +417,6 @@ func (bc *blockchain) context(ctx context.Context, height uint64) (context.Conte
423417
if err != nil {
424418
return nil, err
425419
}
426-
btc, err := bc.buildBlockTimeCalculator()
427-
if err != nil {
428-
return nil, err
429-
}
430420
ctx = genesis.WithGenesisContext(
431421
protocol.WithBlockchainCtx(
432422
ctx,
@@ -435,7 +425,7 @@ func (bc *blockchain) context(ctx context.Context, height uint64) (context.Conte
435425
ChainID: bc.ChainID(),
436426
EvmNetworkID: bc.EvmNetworkID(),
437427
GetBlockHash: bc.dao.GetBlockHash,
438-
GetBlockTime: btc.CalculateBlockTime,
428+
GetBlockTime: bc.getBlockTime,
439429
},
440430
),
441431
bc.genesis,
@@ -475,22 +465,13 @@ func (bc *blockchain) MintNewBlock(timestamp time.Time, opts ...MintOption) (*bl
475465
ctx = bc.contextWithBlock(ctx, minterAddress, newblockHeight, timestamp, protocol.CalcBaseFee(genesis.MustExtractGenesisContext(ctx).Blockchain, &tip), protocol.CalcExcessBlobGas(tip.ExcessBlobGas, tip.BlobGasUsed))
476466
ctx = protocol.WithFeatureCtx(ctx)
477467
// run execution and update state trie root hash
478-
blockBuilder, err := bc.bbf.NewBlockBuilder(
479-
ctx,
480-
func(elp action.Envelope) (*action.SealedEnvelope, error) {
481-
return action.Sign(elp, producerPrivateKey)
482-
},
483-
)
484-
if err != nil {
485-
return nil, errors.Wrapf(err, "failed to create block builder at new block height %d", newblockHeight)
486-
}
487-
blk, err := blockBuilder.SignAndBuild(producerPrivateKey)
468+
blk, err := bc.bbf.Mint(ctx, producerPrivateKey)
488469
if err != nil {
489470
return nil, errors.Wrapf(err, "failed to create block")
490471
}
491472
_blockMtc.WithLabelValues("MintGas").Set(float64(blk.GasUsed()))
492473
_blockMtc.WithLabelValues("MintActions").Set(float64(len(blk.Actions)))
493-
return &blk, nil
474+
return blk, nil
494475
}
495476

496477
// CommitBlock validates and appends a block to the chain
@@ -598,15 +579,13 @@ func (bc *blockchain) emitToSubscribers(blk *block.Block) {
598579
bc.pubSubManager.SendBlockToSubscribers(blk)
599580
}
600581

601-
func (bc *blockchain) buildBlockTimeCalculator() (*blockutil.BlockTimeCalculator, error) {
602-
return bc.btcBuilder.Clone().SetTipHeight(bc.TipHeight).SetHistoryBlockTime(func(height uint64) (time.Time, error) {
603-
if height == 0 {
604-
return time.Unix(bc.genesis.Timestamp, 0), nil
605-
}
606-
header, err := bc.dao.HeaderByHeight(height)
607-
if err != nil {
608-
return time.Time{}, err
609-
}
610-
return header.Timestamp(), nil
611-
}).Build()
582+
func (bc *blockchain) getBlockTime(height uint64) (time.Time, error) {
583+
if height == 0 {
584+
return time.Unix(bc.genesis.Timestamp, 0), nil
585+
}
586+
header, err := bc.dao.HeaderByHeight(height)
587+
if err != nil {
588+
return time.Time{}, err
589+
}
590+
return header.Timestamp(), nil
612591
}

blockchain/integrity/benchmark_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,6 @@ func newChainInDB() (blockchain.Blockchain, actpool.ActPool, error) {
278278
sf,
279279
protocol.NewGenericValidator(sf, accountutil.AccountState),
280280
)),
281-
blockchain.BlockTimeCalculatorBuilderOption(testutil.DummyBlockTimeBuilder()),
282281
)
283282
if bc == nil {
284283
return nil, nil, errors.New("pointer is nil")

blockchain/integrity/integrity_test.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,6 @@ func TestCreateBlockchain(t *testing.T) {
506506
sf,
507507
protocol.NewGenericValidator(sf, accountutil.AccountState),
508508
)),
509-
blockchain.BlockTimeCalculatorBuilderOption(testutil.DummyBlockTimeBuilder()),
510509
)
511510
ep := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGas, fakeGetBlockTime)
512511
require.NoError(ep.Register(registry))
@@ -563,7 +562,6 @@ func TestGetBlockHash(t *testing.T) {
563562
sf,
564563
protocol.NewGenericValidator(sf, accountutil.AccountState),
565564
)),
566-
blockchain.BlockTimeCalculatorBuilderOption(testutil.DummyBlockTimeBuilder()),
567565
)
568566
ep := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGas, fakeGetBlockTime)
569567
require.NoError(ep.Register(registry))
@@ -729,7 +727,6 @@ func TestBlockchain_MintNewBlock(t *testing.T) {
729727
sf,
730728
protocol.NewGenericValidator(sf, accountutil.AccountState),
731729
)),
732-
blockchain.BlockTimeCalculatorBuilderOption(testutil.DummyBlockTimeBuilder()),
733730
)
734731
ep := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGas, fakeGetBlockTime)
735732
require.NoError(t, ep.Register(registry))
@@ -800,7 +797,6 @@ func TestBlockchain_MintNewBlock_PopAccount(t *testing.T) {
800797
sf,
801798
protocol.NewGenericValidator(sf, accountutil.AccountState),
802799
)),
803-
blockchain.BlockTimeCalculatorBuilderOption(testutil.DummyBlockTimeBuilder()),
804800
)
805801
rp := rolldpos.NewProtocol(cfg.Genesis.NumCandidateDelegates, cfg.Genesis.NumDelegates, cfg.Genesis.NumSubEpochs)
806802
require.NoError(t, rp.Register(registry))
@@ -944,7 +940,6 @@ func createChain(cfg config.Config, inMem bool) (blockchain.Blockchain, factory.
944940
sf,
945941
protocol.NewGenericValidator(sf, accountutil.AccountState),
946942
)),
947-
blockchain.BlockTimeCalculatorBuilderOption(testutil.DummyBlockTimeBuilder().SetBlockInterval(func(height uint64) time.Duration { return time.Second })),
948943
)
949944
btc, err := blockutil.NewBlockTimeCalculator(func(uint64) time.Duration { return time.Second },
950945
bc.TipHeight, func(height uint64) (time.Time, error) {
@@ -1490,7 +1485,6 @@ func TestConstantinople(t *testing.T) {
14901485
sf,
14911486
protocol.NewGenericValidator(sf, accountutil.AccountState),
14921487
)),
1493-
blockchain.BlockTimeCalculatorBuilderOption(testutil.DummyBlockTimeBuilder()),
14941488
)
14951489
ep := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGas, fakeGetBlockTime)
14961490
require.NoError(ep.Register(registry))
@@ -1745,7 +1739,6 @@ func TestLoadBlockchainfromDB(t *testing.T) {
17451739
sf,
17461740
protocol.NewGenericValidator(sf, accountutil.AccountState),
17471741
)),
1748-
blockchain.BlockTimeCalculatorBuilderOption(testutil.DummyBlockTimeBuilder()),
17491742
)
17501743
ep := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGas, fakeGetBlockTime)
17511744
require.NoError(ep.Register(registry))
@@ -1775,7 +1768,6 @@ func TestLoadBlockchainfromDB(t *testing.T) {
17751768
sf,
17761769
protocol.NewGenericValidator(sf, accountutil.AccountState),
17771770
)),
1778-
blockchain.BlockTimeCalculatorBuilderOption(testutil.DummyBlockTimeBuilder()),
17791771
)
17801772
require.NoError(bc.Start(ctx))
17811773
defer func() {
@@ -2054,7 +2046,6 @@ func TestBlockchainInitialCandidate(t *testing.T) {
20542046
dao,
20552047
factory.NewMinter(sf, ap),
20562048
blockchain.BlockValidatorOption(sf),
2057-
blockchain.BlockTimeCalculatorBuilderOption(testutil.DummyBlockTimeBuilder()),
20582049
)
20592050
rolldposProtocol := rolldpos.NewProtocol(
20602051
cfg.Genesis.NumCandidateDelegates,
@@ -2096,7 +2087,7 @@ func TestBlockchain_AccountState(t *testing.T) {
20962087
store, err := filedao.NewFileDAOInMemForTest()
20972088
require.NoError(err)
20982089
dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf}, cfg.DB.MaxCacheSize)
2099-
bc := blockchain.NewBlockchain(cfg.Chain, cfg.Genesis, dao, factory.NewMinter(sf, ap), blockchain.BlockTimeCalculatorBuilderOption(testutil.DummyBlockTimeBuilder()))
2090+
bc := blockchain.NewBlockchain(cfg.Chain, cfg.Genesis, dao, factory.NewMinter(sf, ap))
21002091
require.NoError(bc.Start(ctx))
21012092
require.NotNil(bc)
21022093
defer func() {
@@ -2128,7 +2119,7 @@ func TestNewAccountAction(t *testing.T) {
21282119
store, err := filedao.NewFileDAOInMemForTest()
21292120
require.NoError(err)
21302121
dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf}, cfg.DB.MaxCacheSize)
2131-
bc := blockchain.NewBlockchain(cfg.Chain, cfg.Genesis, dao, factory.NewMinter(sf, ap), blockchain.BlockTimeCalculatorBuilderOption(testutil.DummyBlockTimeBuilder()))
2122+
bc := blockchain.NewBlockchain(cfg.Chain, cfg.Genesis, dao, factory.NewMinter(sf, ap))
21322123
require.NoError(bc.Start(ctx))
21332124
require.NotNil(bc)
21342125
defer func() {
@@ -2173,7 +2164,7 @@ func TestNewAccountAction(t *testing.T) {
21732164
store, err := filedao.NewFileDAOInMemForTest()
21742165
require.NoError(err)
21752166
dao1 := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf1}, cfg.DB.MaxCacheSize)
2176-
bc1 := blockchain.NewBlockchain(cfg.Chain, cfg.Genesis, dao1, factory.NewMinter(sf1, ap), blockchain.BlockTimeCalculatorBuilderOption(testutil.DummyBlockTimeBuilder()))
2167+
bc1 := blockchain.NewBlockchain(cfg.Chain, cfg.Genesis, dao1, factory.NewMinter(sf1, ap))
21772168
require.NoError(bc1.Start(ctx))
21782169
require.NotNil(bc1)
21792170
defer func() {
@@ -2242,7 +2233,7 @@ func TestBlocks(t *testing.T) {
22422233
dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf}, dbcfg.MaxCacheSize)
22432234

22442235
// Create a blockchain from scratch
2245-
bc := blockchain.NewBlockchain(cfg.Chain, cfg.Genesis, dao, factory.NewMinter(sf, ap), blockchain.BlockTimeCalculatorBuilderOption(testutil.DummyBlockTimeBuilder()))
2236+
bc := blockchain.NewBlockchain(cfg.Chain, cfg.Genesis, dao, factory.NewMinter(sf, ap))
22462237
require.NoError(bc.Start(context.Background()))
22472238
defer func() {
22482239
require.NoError(bc.Stop(context.Background()))
@@ -2325,7 +2316,6 @@ func TestActions(t *testing.T) {
23252316
sf,
23262317
protocol.NewGenericValidator(sf, accountutil.AccountState),
23272318
)),
2328-
blockchain.BlockTimeCalculatorBuilderOption(testutil.DummyBlockTimeBuilder()),
23292319
)
23302320
require.NoError(bc.Start(context.Background()))
23312321
defer func() {
@@ -2381,7 +2371,7 @@ func TestBlockchain_AddRemoveSubscriber(t *testing.T) {
23812371
store, err := filedao.NewFileDAOInMemForTest()
23822372
req.NoError(err)
23832373
dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf}, cfg.DB.MaxCacheSize)
2384-
bc := blockchain.NewBlockchain(cfg.Chain, cfg.Genesis, dao, factory.NewMinter(sf, ap), blockchain.BlockTimeCalculatorBuilderOption(testutil.DummyBlockTimeBuilder()))
2374+
bc := blockchain.NewBlockchain(cfg.Chain, cfg.Genesis, dao, factory.NewMinter(sf, ap))
23852375
// mock
23862376
ctrl := gomock.NewController(t)
23872377
mb := mock_blockcreationsubscriber.NewMockBlockCreationSubscriber(ctrl)
@@ -2621,7 +2611,6 @@ func newChain(t *testing.T, stateTX bool) (blockchain.Blockchain, factory.Factor
26212611
sf,
26222612
protocol.NewGenericValidator(sf, accountutil.AccountState),
26232613
)),
2624-
blockchain.BlockTimeCalculatorBuilderOption(testutil.DummyBlockTimeBuilder()),
26252614
)
26262615
require.NotNil(bc)
26272616
ep := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGas, fakeGetBlockTime)

blocksync/blocksync_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ func TestBlockSyncerProcessBlockTipHeight(t *testing.T) {
210210
dao,
211211
factory.NewMinter(sf, ap),
212212
blockchain.BlockValidatorOption(block.NewValidator(sf, ap)),
213-
blockchain.BlockTimeCalculatorBuilderOption(testutil.DummyBlockTimeBuilder()),
214213
)
215214
require.NoError(chain.Start(ctx))
216215
require.NotNil(chain)
@@ -277,7 +276,6 @@ func TestBlockSyncerProcessBlockOutOfOrder(t *testing.T) {
277276
dao,
278277
factory.NewMinter(sf, ap1),
279278
blockchain.BlockValidatorOption(block.NewValidator(sf, ap1)),
280-
blockchain.BlockTimeCalculatorBuilderOption(testutil.DummyBlockTimeBuilder()),
281279
)
282280
require.NotNil(chain1)
283281
require.NoError(chain1.Start(ctx))
@@ -305,7 +303,6 @@ func TestBlockSyncerProcessBlockOutOfOrder(t *testing.T) {
305303
dao2,
306304
factory.NewMinter(sf2, ap2),
307305
blockchain.BlockValidatorOption(block.NewValidator(sf2, ap2)),
308-
blockchain.BlockTimeCalculatorBuilderOption(testutil.DummyBlockTimeBuilder()),
309306
)
310307
require.NotNil(chain2)
311308
require.NoError(chain2.Start(ctx))
@@ -381,7 +378,6 @@ func TestBlockSyncerProcessBlock(t *testing.T) {
381378
dao,
382379
factory.NewMinter(sf, ap1),
383380
blockchain.BlockValidatorOption(block.NewValidator(sf, ap1)),
384-
blockchain.BlockTimeCalculatorBuilderOption(testutil.DummyBlockTimeBuilder()),
385381
)
386382
require.NoError(chain1.Start(ctx))
387383
require.NotNil(chain1)
@@ -408,7 +404,6 @@ func TestBlockSyncerProcessBlock(t *testing.T) {
408404
dao2,
409405
factory.NewMinter(sf2, ap2),
410406
blockchain.BlockValidatorOption(block.NewValidator(sf2, ap2)),
411-
blockchain.BlockTimeCalculatorBuilderOption(testutil.DummyBlockTimeBuilder()),
412407
)
413408
require.NoError(chain2.Start(ctx))
414409
require.NotNil(chain2)
@@ -477,7 +472,6 @@ func TestBlockSyncerSync(t *testing.T) {
477472
dao,
478473
factory.NewMinter(sf, ap),
479474
blockchain.BlockValidatorOption(block.NewValidator(sf, ap)),
480-
blockchain.BlockTimeCalculatorBuilderOption(testutil.DummyBlockTimeBuilder()),
481475
)
482476
require.NoError(chain.Start(ctx))
483477
require.NotNil(chain)

0 commit comments

Comments
 (0)