Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rolldpos] refactor rolldpos.ChainManager #3516

Merged
merged 19 commits into from
Jul 27, 2022

Conversation

huof6829
Copy link
Contributor

Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context. List any dependencies that are required for this change.

Fixes #3462

Type of change

Please delete options that are not relevant.

  • [] Bug fix (non-breaking change which fixes an issue)
  • [] New feature (non-breaking change which adds functionality)
  • [] Code refactor or improvement
  • [] Breaking change (fix or feature that would cause a new or changed behavior of existing functionality)
  • [] This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • [] make test
  • [] fullsync
  • [] Other test (please specify)

Test Configuration:

  • Firmware version:
  • Hardware:
  • Toolchain:
  • SDK:

Checklist:

  • [] My code follows the style guidelines of this project
  • [] I have performed a self-review of my code
  • [] I have commented my code, particularly in hard-to-understand areas
  • [] I have made corresponding changes to the documentation
  • [] My changes generate no new warnings
  • [] I have added tests that prove my fix is effective or that my feature works
  • [] New and existing unit tests pass locally with my changes
  • [] Any dependent changes have been merged and published in downstream modules

@huof6829 huof6829 requested a review from a team as a code owner July 11, 2022 11:12
@Liuhaai
Copy link
Member

Liuhaai commented Jul 11, 2022

Should the old interface be removed?

@codecov
Copy link

codecov bot commented Jul 12, 2022

Codecov Report

Merging #3516 (de562f2) into master (a20e489) will decrease coverage by 1.30%.
The diff coverage is 56.99%.

❗ Current head de562f2 differs from pull request most recent head 8ba04c9. Consider uploading reports for the commit 8ba04c9 to get more accurate results

@@            Coverage Diff             @@
##           master    #3516      +/-   ##
==========================================
- Coverage   75.43%   74.12%   -1.31%     
==========================================
  Files         247      254       +7     
  Lines       22845    23404     +559     
==========================================
+ Hits        17233    17349     +116     
- Misses       4685     5129     +444     
+ Partials      927      926       -1     
Impacted Files Coverage Δ
action/protocol/staking/read_state.go 15.38% <0.00%> (ø)
api/grpcserver.go 79.49% <ø> (+0.55%) ⬆️
blockchain/blockchain.go 0.89% <0.00%> (ø)
blockchain/filedao/filedao_legacy.go 85.80% <ø> (ø)
blockchain/filedao/filedao_v2.go 98.51% <ø> (ø)
blockchain/pubsubmanager.go 0.00% <0.00%> (ø)
config/config.go 94.06% <ø> (+9.31%) ⬆️
db/db_bolt.go 72.51% <ø> (-0.19%) ⬇️
db/kvstore_impl.go 61.64% <ø> (ø)
db/kvstorewithcache.go 0.00% <0.00%> (ø)
... and 69 more

Help us with your feedback. Take ten seconds to tell us how you rate us.

// BlockProposeTime return propose time by height
BlockProposeTime(uint64) (time.Time, error)
// BlockCommitTime return commit time by height
BlockCommitTime(uint64) (time.Time, error)
Copy link
Member

@dustinxie dustinxie Jul 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to change the main Blockchain interface?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, no need,

// ChainAddress returns chain address on parent chain, the root chain return empty.
ChainAddress() string
}
)
Copy link
Member

@dustinxie dustinxie Jul 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add an internal struct here to implement ChainManager interface

struct xxx {
   bc blockchain.Blockchain
}
func (c *xxx) BlockProposeTime()
func (c *xxx) BlockCommitTime()

Copy link
Contributor Author

@huof6829 huof6829 Jul 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, internal struct is better

// BlockCommitTime return commit time by height
BlockCommitTime(uint64) (time.Time, error)
// Genesis returns the genesis
Genesis() genesis.Genesis
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

according to issue, can delete Genesis() if it's not used

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes,

// BlockProposeTime return propose time by height
BlockProposeTime(uint64) (time.Time, error)
// BlockCommitTime return commit time by height
BlockCommitTime(uint64) (time.Time, error)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still keep these 2

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and add GenesisTime()


type blockTime struct {
bc blockchain.Blockchain
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make this struct implement ChainManager interface

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and rename to chainManager

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -23,6 +24,7 @@ type roundCalculator struct {
rp *rolldpos.Protocol
delegatesByEpochFunc DelegatesByEpochFunc
beringHeight uint64
blockTime *blockTime
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to add this

@@ -138,6 +138,9 @@ func newRollDPoSCtx(
rp: rp,
timeBasedRotation: timeBasedRotation,
beringHeight: beringHeight,
blockTime: &blockTime{
bc: chain.(blockchain.Blockchain),
},
Copy link
Member

@dustinxie dustinxie Jul 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to add this, modify L137 chain to

chain: &blockTime{
			bc: chain.(blockchain.Blockchain),
		},

@@ -267,8 +326,8 @@ func (b *Builder) SetPriKey(priKey crypto.PrivateKey) *Builder {
}

// SetChainManager sets the blockchain APIs
func (b *Builder) SetChainManager(chain ChainManager) *Builder {
b.chain = chain
func (b *Builder) SetChainManager(bc blockchain.Blockchain) *Builder {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason we want to use interface is to prevent referring to blockchain.Blockchain directly (the current implementation is not complete, with other problems). Thus, this parameter type shouldn't be modified, and the chainManager struct shouldn't be defined in this package.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, still use SetChainManager(chain ChainManager)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

}

// GenesisTime return Genesis time by default
func (cm *chainManager) GenesisTime() time.Time {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if both BlockCommitTime(0) and BlockProposeTime(0) return bc.Genesis().Timestamp, we can delete this function from the interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

func (b *Builder) SetChainManager(chain ChainManager) *Builder {
b.chain = chain
func (b *Builder) SetChainManager(bc blockchain.Blockchain) *Builder {
b.chain = &chainManager{bc: bc}
Copy link
Member

@dustinxie dustinxie Jul 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

define a newChainManager(bc blockchain.Blockchain) ChainManager, so

rolldpos.NewRollDPoSBuilder(). ... .SetChainManager(newChainManager(bc))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right!

@@ -44,7 +44,7 @@ func TestRollDPoSCtx(t *testing.T) {
})

t.Run("case 2:panic because of rp is nil", func(t *testing.T) {
_, err := newRollDPoSCtx(consensusfsm.NewConsensusConfig(cfg), dbConfig, true, time.Second, true, b, block.NewDeserializer(0), nil, nil, dummyCandidatesByHeightFunc, "", nil, nil, 0)
_, err := newRollDPoSCtx(consensusfsm.NewConsensusConfig(cfg), dbConfig, true, time.Second, true, &chainManager{bc: b}, block.NewDeserializer(0), nil, nil, dummyCandidatesByHeightFunc, "", nil, nil, 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newChainManager(b)

bc blockchain.Blockchain
}

func newBlockTime(bc blockchain.Blockchain) *blockTime {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The arg here has to be ChainManager

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@@ -511,14 +511,14 @@ func (ctx *rollDPoSCtx) Commit(msg interface{}) (bool, error) {

_consensusDurationMtc.WithLabelValues().Set(float64(time.Since(ctx.round.roundStartTime)))
if pendingBlock.Height() > 1 {
prevBlkHeader, err := ctx.chain.BlockHeaderByHeight(pendingBlock.Height() - 1)
prevBlkProposeTime, err := ctx.roundCalc.chain.BlockProposeTime(pendingBlock.Height() - 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should be ctx.chain, which is set by SetChainManager()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

if lastBlock, err = c.chain.BlockHeaderByHeight(height - 1); err != nil {
var lastBlkProposeTime time.Time
lastBlkProposeTime, err = c.chain.BlockProposeTime(height - 1)
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write L140, 141 into 1 line, be consistent with L134

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

if lastBlock, err = c.chain.BlockFooterByHeight(height - 1); err != nil {
var lastBlkCommitTime time.Time
lastBlkCommitTime, err = c.chain.BlockCommitTime(height - 1)
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above, combine into 1 line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -511,14 +511,14 @@ func (ctx *rollDPoSCtx) Commit(msg interface{}) (bool, error) {

_consensusDurationMtc.WithLabelValues().Set(float64(time.Since(ctx.round.roundStartTime)))
if pendingBlock.Height() > 1 {
prevBlkHeader, err := ctx.chain.BlockHeaderByHeight(pendingBlock.Height() - 1)
prevBlkProposeTime, err := ctx.chain.BlockProposeTime(pendingBlock.Height() - 1)
if err != nil {
log.L().Error("Error when getting the previous block header.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logging info needs to be adjusted as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

}
header, err := cm.bc.BlockHeaderByHeight(height)
if err != nil {
return time.Now(), errors.Wrapf(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

time.Now() is not default value for Time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@Liuhaai Liuhaai merged commit 0cac228 into iotexproject:master Jul 27, 2022
@huof6829 huof6829 deleted the fix_3462 branch August 4, 2022 14:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

refactor rolldpos.ChainManager
5 participants