diff options
author | Haoping Ku <haoping.ku@dexon.org> | 2018-10-16 18:23:55 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-16 18:23:55 +0800 |
commit | 5a49482543f9d5d5641fd7b3b41a1598f5f83fd8 (patch) | |
tree | c0296e0399a1f6a07c30806736e0b9166d111b3e | |
parent | be7c5cc02e6b960abb92a63142d98cd3661ab4b4 (diff) | |
download | dexon-consensus-5a49482543f9d5d5641fd7b3b41a1598f5f83fd8.tar dexon-consensus-5a49482543f9d5d5641fd7b3b41a1598f5f83fd8.tar.gz dexon-consensus-5a49482543f9d5d5641fd7b3b41a1598f5f83fd8.tar.bz2 dexon-consensus-5a49482543f9d5d5641fd7b3b41a1598f5f83fd8.tar.lz dexon-consensus-5a49482543f9d5d5641fd7b3b41a1598f5f83fd8.tar.xz dexon-consensus-5a49482543f9d5d5641fd7b3b41a1598f5f83fd8.tar.zst dexon-consensus-5a49482543f9d5d5641fd7b3b41a1598f5f83fd8.zip |
core: consensus-timestamp: modify for round change (#214)
* core: consensus-timestamp: modify for round change
* core: consensus-timestamp: fix typos
-rw-r--r-- | core/consensus-timestamp.go | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/core/consensus-timestamp.go b/core/consensus-timestamp.go index 1822f74..e04cd07 100644 --- a/core/consensus-timestamp.go +++ b/core/consensus-timestamp.go @@ -30,6 +30,7 @@ type consensusTimestamp struct { // This part keeps configs for each round. numChainsForRounds []uint32 + numChainsRoundBase uint64 } var ( @@ -38,10 +39,11 @@ var ( ErrTimestampNotIncrease = errors.New("timestamp is not increasing") ) -// newConsensusTimestamp create timestamper object. +// newConsensusTimestamp creates timestamper object. func newConsensusTimestamp(numChains uint32) *consensusTimestamp { return &consensusTimestamp{ numChainsForRounds: []uint32{numChains}, + numChainsRoundBase: uint64(0), } } @@ -50,7 +52,7 @@ func newConsensusTimestamp(numChains uint32) *consensusTimestamp { func (ct *consensusTimestamp) appendConfig( round uint64, config *types.Config) error { - if round != uint64(len(ct.numChainsForRounds)) { + if round != uint64(len(ct.numChainsForRounds))+ct.numChainsRoundBase { return ErrRoundNotIncreasing } ct.numChainsForRounds = append(ct.numChainsForRounds, config.NumChains) @@ -61,8 +63,9 @@ func (ct *consensusTimestamp) appendConfig( func (ct *consensusTimestamp) processBlocks(blocks []*types.Block) (err error) { for _, block := range blocks { if !block.IsGenesis() { - if block.Finalization.Timestamp, err = - getMedianTime(ct.chainTimestamps); err != nil { + round := block.Position.Round - ct.numChainsRoundBase + ts := ct.chainTimestamps[:ct.numChainsForRounds[round]] + if block.Finalization.Timestamp, err = getMedianTime(ts); err != nil { return } } else { @@ -78,6 +81,11 @@ func (ct *consensusTimestamp) processBlocks(blocks []*types.Block) (err error) { } ct.chainTimestamps[block.Position.ChainID] = block.Timestamp + + if block.Position.Round > ct.numChainsRoundBase { + ct.numChainsRoundBase++ + ct.numChainsForRounds = ct.numChainsForRounds[1:] + } } return } |