diff options
author | Mission Liao <mission.liao@dexon.org> | 2018-10-29 17:49:33 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-29 17:49:33 +0800 |
commit | 2275880f65fbfa253c1ec1d4c25d0b94cbdb185d (patch) | |
tree | e9909dfd32c06332c62dcee5e3d9003b27670ffa | |
parent | 51904f3e9b821f3313d3ab1f268efb28f739eb5f (diff) | |
download | dexon-consensus-2275880f65fbfa253c1ec1d4c25d0b94cbdb185d.tar dexon-consensus-2275880f65fbfa253c1ec1d4c25d0b94cbdb185d.tar.gz dexon-consensus-2275880f65fbfa253c1ec1d4c25d0b94cbdb185d.tar.bz2 dexon-consensus-2275880f65fbfa253c1ec1d4c25d0b94cbdb185d.tar.lz dexon-consensus-2275880f65fbfa253c1ec1d4c25d0b94cbdb185d.tar.xz dexon-consensus-2275880f65fbfa253c1ec1d4c25d0b94cbdb185d.tar.zst dexon-consensus-2275880f65fbfa253c1ec1d4c25d0b94cbdb185d.zip |
core: fix consensus timestamp (#272)
* Fix panic
Assume DAG would be increased to 7 chains at round 4.
When encounter the first block from round 4, this module would
attempt to resize its working set to the numChains in round 2 or 3.
* Add a check to make sure consensus timestamps are increasing
* Fix consensus timestamp rewind
-rw-r--r-- | core/consensus-timestamp.go | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/core/consensus-timestamp.go b/core/consensus-timestamp.go index 49270d4..03a6f10 100644 --- a/core/consensus-timestamp.go +++ b/core/consensus-timestamp.go @@ -34,6 +34,8 @@ type consensusTimestamp struct { // dMoment represents the genesis time. dMoment time.Time + // lastTimestamp represents previous assigned consensus timestamp. + lastTimestamp time.Time } var ( @@ -42,6 +44,9 @@ var ( ErrTimestampNotIncrease = errors.New("timestamp is not increasing") // ErrNoRoundConfig for no round config found. ErrNoRoundConfig = errors.New("no round config found") + // ErrConsensusTimestampRewind would be reported if the generated timestamp + // is rewinded. + ErrConsensusTimestampRewind = errors.New("consensus timestamp rewind") ) // newConsensusTimestamp creates timestamper object. @@ -95,13 +100,13 @@ func (ct *consensusTimestamp) processBlocks(blocks []*types.Block) (err error) { if len(ct.numChainsOfRounds) < 2 { return ErrNoRoundConfig } + ct.numChainsBase++ + ct.numChainsOfRounds = ct.numChainsOfRounds[1:] if ct.numChainsOfRounds[0] > ct.numChainsOfRounds[1] { ct.resizeChainTimetamps(ct.numChainsOfRounds[0]) } else { ct.resizeChainTimetamps(ct.numChainsOfRounds[1]) } - ct.numChainsBase++ - ct.numChainsOfRounds = ct.numChainsOfRounds[1:] } else { // Error if round < base or round > base + 2. return ErrInvalidRoundID @@ -114,6 +119,11 @@ func (ct *consensusTimestamp) processBlocks(blocks []*types.Block) (err error) { return ErrTimestampNotIncrease } ct.chainTimestamps[block.Position.ChainID] = block.Timestamp + if block.Finalization.Timestamp.Before(ct.lastTimestamp) { + block.Finalization.Timestamp = ct.lastTimestamp + } else { + ct.lastTimestamp = block.Finalization.Timestamp + } } return } |