-
Notifications
You must be signed in to change notification settings - Fork 324
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
Conversation
Should the old interface be removed? |
Codecov Report
@@ 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
Help us with your feedback. Take ten seconds to tell us how you rate us. |
blockchain/blockchain.go
Outdated
// BlockProposeTime return propose time by height | ||
BlockProposeTime(uint64) (time.Time, error) | ||
// BlockCommitTime return commit time by height | ||
BlockCommitTime(uint64) (time.Time, error) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 | ||
} | ||
) |
There was a problem hiding this comment.
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()
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still keep these 2
There was a problem hiding this comment.
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 | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and rename to chainManager
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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), | |||
}, |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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} |
There was a problem hiding this comment.
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))
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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()
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.", |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
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.
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
Test Configuration:
Checklist: