Skip to content

Commit 97a2f95

Browse files
committed
deprecat blockTimeCalculator
1 parent 1d6dcdc commit 97a2f95

File tree

3 files changed

+52
-28
lines changed

3 files changed

+52
-28
lines changed

action/protocol/execution/evm/evm.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ func newParams(
187187
if vmCfg, ok := protocol.GetVMConfigCtx(ctx); ok {
188188
vmConfig = vmCfg
189189
}
190-
chainConfig, err := getChainConfig(g.Blockchain, blkCtx.BlockHeight, evmNetworkID, helperCtx.GetBlockTime)
190+
chainConfig, err := getChainConfig(g.Blockchain, blkCtx.BlockHeight, evmNetworkID, func(height uint64) (*time.Time, error) {
191+
return blockHeightToTime(ctx, height)
192+
})
191193
if err != nil {
192194
return nil, err
193195
}
@@ -399,7 +401,7 @@ func prepareStateDB(ctx context.Context, sm protocol.StateManager) (*StateDBAdap
399401
)
400402
}
401403

402-
func getChainConfig(g genesis.Blockchain, height uint64, id uint32, getBlockTime GetBlockTime) (*params.ChainConfig, error) {
404+
func getChainConfig(g genesis.Blockchain, height uint64, id uint32, getBlockTime func(uint64) (*time.Time, error)) (*params.ChainConfig, error) {
403405
var chainConfig params.ChainConfig
404406
chainConfig.ConstantinopleBlock = new(big.Int).SetUint64(0) // Constantinople switch block (nil = no fork, 0 = already activated)
405407
chainConfig.BeringBlock = new(big.Int).SetUint64(g.BeringBlockHeight)
@@ -423,19 +425,40 @@ func getChainConfig(g genesis.Blockchain, height uint64, id uint32, getBlockTime
423425
sumatraTime, err := getBlockTime(g.SumatraBlockHeight)
424426
if err != nil {
425427
return nil, err
428+
} else if sumatraTime != nil {
429+
sumatraTimestamp := (uint64)(sumatraTime.Unix())
430+
chainConfig.ShanghaiTime = &sumatraTimestamp
426431
}
427-
sumatraTimestamp := (uint64)(sumatraTime.Unix())
428-
chainConfig.ShanghaiTime = &sumatraTimestamp
429432
// enable Cancun at Vanuatu
430433
cancunTime, err := getBlockTime(g.VanuatuBlockHeight)
431434
if err != nil {
432435
return nil, err
436+
} else if cancunTime != nil {
437+
cancunTimestamp := (uint64)(cancunTime.Unix())
438+
chainConfig.CancunTime = &cancunTimestamp
433439
}
434-
cancunTimestamp := (uint64)(cancunTime.Unix())
435-
chainConfig.CancunTime = &cancunTimestamp
436440
return &chainConfig, nil
437441
}
438442

443+
// blockHeightToTime returns the block time by height
444+
// if height is greater than current block height, return nil
445+
// if height is equal to current block height, return current block time
446+
// otherwise, return the block time by height from the blockchain
447+
func blockHeightToTime(ctx context.Context, height uint64) (*time.Time, error) {
448+
blkCtx := protocol.MustGetBlockCtx(ctx)
449+
if height > blkCtx.BlockHeight {
450+
return nil, nil
451+
}
452+
if height == blkCtx.BlockHeight {
453+
return &blkCtx.BlockTimeStamp, nil
454+
}
455+
t, err := mustGetHelperCtx(ctx).GetBlockTime(height)
456+
if err != nil {
457+
return nil, err
458+
}
459+
return &t, nil
460+
}
461+
439462
// Error in executeInEVM is a consensus issue
440463
func executeInEVM(ctx context.Context, evmParams *Params, stateDB stateDB) ([]byte, uint64, uint64, string, iotextypes.ReceiptStatus, error) {
441464
var (

chainservice/builder.go

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ import (
4747
"github.com/iotexproject/iotex-core/v2/nodeinfo"
4848
"github.com/iotexproject/iotex-core/v2/p2p"
4949
"github.com/iotexproject/iotex-core/v2/pkg/log"
50-
"github.com/iotexproject/iotex-core/v2/pkg/util/blockutil"
5150
"github.com/iotexproject/iotex-core/v2/server/itx/nodestats"
5251
"github.com/iotexproject/iotex-core/v2/state/factory"
5352
"github.com/iotexproject/iotex-core/v2/systemcontractindex/stakingindex"
@@ -703,7 +702,14 @@ func (builder *Builder) registerAccountProtocol() error {
703702
}
704703

705704
func (builder *Builder) registerExecutionProtocol() error {
706-
return execution.NewProtocol(builder.cs.blockdao.GetBlockHash, rewarding.DepositGas, builder.cs.blockTimeCalculator.CalculateBlockTime).Register(builder.cs.registry)
705+
dao := builder.cs.BlockDAO()
706+
return execution.NewProtocol(builder.cs.blockdao.GetBlockHash, rewarding.DepositGas, func(u uint64) (time.Time, error) {
707+
header, err := dao.HeaderByHeight(u)
708+
if err != nil {
709+
return time.Time{}, err
710+
}
711+
return header.Timestamp(), nil
712+
}).Register(builder.cs.registry)
707713
}
708714

709715
func (builder *Builder) registerRollDPoSProtocol() error {
@@ -721,7 +727,13 @@ func (builder *Builder) registerRollDPoSProtocol() error {
721727
factory := builder.cs.factory
722728
dao := builder.cs.blockdao
723729
chain := builder.cs.chain
724-
getBlockTime := builder.cs.blockTimeCalculator.CalculateBlockTime
730+
getBlockTime := func(height uint64) (time.Time, error) {
731+
header, err := chain.BlockHeaderByHeight(height)
732+
if err != nil {
733+
return time.Time{}, err
734+
}
735+
return header.Timestamp(), nil
736+
}
725737
pollProtocol, err := poll.NewProtocol(
726738
builder.cfg.Consensus.Scheme,
727739
builder.cfg.Chain,
@@ -779,19 +791,6 @@ func (builder *Builder) registerRollDPoSProtocol() error {
779791
return pollProtocol.Register(builder.cs.registry)
780792
}
781793

782-
func (builder *Builder) buildBlockTimeCalculator() (err error) {
783-
consensusCfg := consensusfsm.NewConsensusConfig(builder.cfg.Consensus.RollDPoS.FSM, builder.cfg.DardanellesUpgrade, builder.cfg.Genesis, builder.cfg.Consensus.RollDPoS.Delay)
784-
dao := builder.cs.BlockDAO()
785-
builder.cs.blockTimeCalculator, err = blockutil.NewBlockTimeCalculator(consensusCfg.BlockInterval, builder.cs.Blockchain().TipHeight, func(height uint64) (time.Time, error) {
786-
blk, err := dao.GetBlockByHeight(height)
787-
if err != nil {
788-
return time.Time{}, err
789-
}
790-
return blk.Timestamp(), nil
791-
})
792-
return err
793-
}
794-
795794
func (builder *Builder) buildConsensusComponent() error {
796795
p2pAgent := builder.cs.p2pAgent
797796
copts := []consensus.Option{
@@ -852,9 +851,6 @@ func (builder *Builder) build(forSubChain, forTest bool) (*ChainService, error)
852851
if err := builder.buildBlockchain(forSubChain, forTest); err != nil {
853852
return nil, err
854853
}
855-
if err := builder.buildBlockTimeCalculator(); err != nil {
856-
return nil, err
857-
}
858854
// staking protocol need to be put in registry before poll protocol when enabling
859855
if err := builder.registerStakingProtocol(); err != nil {
860856
return nil, errors.Wrap(err, "failed to register staking protocol")

chainservice/chainservice.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package chainservice
77

88
import (
99
"context"
10+
"time"
1011

1112
"github.com/libp2p/go-libp2p/core/peer"
1213
"github.com/pkg/errors"
@@ -36,7 +37,6 @@ import (
3637
"github.com/iotexproject/iotex-core/v2/p2p"
3738
"github.com/iotexproject/iotex-core/v2/pkg/lifecycle"
3839
"github.com/iotexproject/iotex-core/v2/pkg/log"
39-
"github.com/iotexproject/iotex-core/v2/pkg/util/blockutil"
4040
"github.com/iotexproject/iotex-core/v2/server/itx/nodestats"
4141
"github.com/iotexproject/iotex-core/v2/state/factory"
4242
"github.com/iotexproject/iotex-core/v2/systemcontractindex/stakingindex"
@@ -77,7 +77,6 @@ type ChainService struct {
7777
registry *protocol.Registry
7878
nodeInfoManager *nodeinfo.InfoManager
7979
apiStats *nodestats.APILocalStats
80-
blockTimeCalculator *blockutil.BlockTimeCalculator
8180
actionsync *actsync.ActionSync
8281
}
8382

@@ -242,7 +241,13 @@ func (cs *ChainService) NewAPIServer(cfg api.Config, archive bool) (*api.ServerV
242241
cs.bfIndexer,
243242
cs.actpool,
244243
cs.registry,
245-
cs.blockTimeCalculator.CalculateBlockTime,
244+
func(u uint64) (time.Time, error) {
245+
header, err := cs.chain.BlockHeaderByHeight(u)
246+
if err != nil {
247+
return time.Time{}, err
248+
}
249+
return header.Timestamp(), nil
250+
},
246251
apiServerOptions...,
247252
)
248253
if err != nil {

0 commit comments

Comments
 (0)