diff options
author | Mission Liao <mission.liao@dexon.org> | 2018-11-05 13:29:15 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-05 13:29:15 +0800 |
commit | c4541185c1d2502dffe09de1af52594f6fae16a6 (patch) | |
tree | 7ab3afa6cb30f2bd7ffdf5158da2e50384dab136 | |
parent | 62a00d3cf89330073df381caa7d913deeb01822d (diff) | |
download | tangerine-consensus-c4541185c1d2502dffe09de1af52594f6fae16a6.tar tangerine-consensus-c4541185c1d2502dffe09de1af52594f6fae16a6.tar.gz tangerine-consensus-c4541185c1d2502dffe09de1af52594f6fae16a6.tar.bz2 tangerine-consensus-c4541185c1d2502dffe09de1af52594f6fae16a6.tar.lz tangerine-consensus-c4541185c1d2502dffe09de1af52594f6fae16a6.tar.xz tangerine-consensus-c4541185c1d2502dffe09de1af52594f6fae16a6.tar.zst tangerine-consensus-c4541185c1d2502dffe09de1af52594f6fae16a6.zip |
core: notify consensus height for genesis rounds (#296)
* Add notifyGenesisRounds
* Log round for types.Vote and types.Block
-rw-r--r-- | core/consensus.go | 8 | ||||
-rw-r--r-- | core/constant.go | 25 | ||||
-rw-r--r-- | core/types/block.go | 3 | ||||
-rw-r--r-- | core/types/position.go | 2 | ||||
-rw-r--r-- | core/types/vote.go | 5 | ||||
-rw-r--r-- | core/utils.go | 11 |
6 files changed, 47 insertions, 7 deletions
diff --git a/core/consensus.go b/core/consensus.go index 04dfe57..2eed101 100644 --- a/core/consensus.go +++ b/core/consensus.go @@ -394,6 +394,9 @@ func NewConsensus( // Run starts running DEXON Consensus. func (con *Consensus) Run(initBlock *types.Block) { + con.logger.Debug("Calling Governance.NotifyRoundHeight for genesis rounds", + "block", initBlock) + notifyGenesisRounds(initBlock, con.gov) // Setup context. con.ctx, con.ctxCancel = context.WithCancel(context.Background()) con.ccModule.init(initBlock) @@ -930,9 +933,12 @@ func (con *Consensus) deliverBlock(b *types.Block) { // TODO(mission): clone types.FinalizationResult con.logger.Debug("Calling Application.BlockDelivered", "block", b) con.app.BlockDelivered(b.Hash, b.Position, b.Finalization) - if b.Position.Round+2 == con.roundToNotify { + if b.Position.Round+roundShift == con.roundToNotify { // Only the first block delivered of that round would // trigger this noitification. + con.logger.Debug("Calling Governance.NotifyRoundHeight", + "round", con.roundToNotify, + "height", b.Finalization.Height) con.gov.NotifyRoundHeight( con.roundToNotify, b.Finalization.Height) con.roundToNotify++ diff --git a/core/constant.go b/core/constant.go new file mode 100644 index 0000000..9a61c0a --- /dev/null +++ b/core/constant.go @@ -0,0 +1,25 @@ +// Copyright 2018 The dexon-consensus Authors +// This file is part of the dexon-consensus library. +// +// The dexon-consensus library is free software: you can redistribute it +// and/or modify it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation, either version 3 of the License, +// or (at your option) any later version. +// +// The dexon-consensus library is distributed in the hope that it will be +// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser +// General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the dexon-consensus library. If not, see +// <http://www.gnu.org/licenses/>. + +package core + +// round shift refers to the difference between block's round and config round +// derived from its state. +// +// For example, when round shift is 2, a block in round 0 should derive config +// for round 2. +const roundShift uint64 = 2 diff --git a/core/types/block.go b/core/types/block.go index bde07d5..27416d8 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -209,8 +209,7 @@ func (b *Block) DecodeRLP(s *rlp.Stream) error { } func (b *Block) String() string { - return fmt.Sprintf("Block(%v:%d:%d)", b.Hash.String()[:6], - b.Position.ChainID, b.Position.Height) + return fmt.Sprintf("Block(%v:%s)", b.Hash.String()[:6], &b.Position) } // Clone returns a deep copy of a block. diff --git a/core/types/position.go b/core/types/position.go index 404f303..1fc7e6b 100644 --- a/core/types/position.go +++ b/core/types/position.go @@ -29,7 +29,7 @@ type Position struct { } func (pos *Position) String() string { - return fmt.Sprintf("pos[%d:%d:%d]", pos.Round, pos.ChainID, pos.Height) + return fmt.Sprintf("[%d:%d:%d]", pos.Round, pos.ChainID, pos.Height) } // Equal checks if two positions are equal, it panics when their chainIDs diff --git a/core/types/vote.go b/core/types/vote.go index 32fb898..5b99f22 100644 --- a/core/types/vote.go +++ b/core/types/vote.go @@ -47,9 +47,8 @@ type Vote struct { } func (v *Vote) String() string { - return fmt.Sprintf("Vote[%s:%d:%d](%d:%d):%s", - v.ProposerID.String()[:6], v.Position.ChainID, v.Position.Height, - v.Period, v.Type, v.BlockHash.String()[:6]) + return fmt.Sprintf("Vote[%s:%s](%d:%d):%s", v.ProposerID.String()[:6], + &v.Position, v.Period, v.Type, v.BlockHash.String()[:6]) } // Clone returns a deep copy of a vote. diff --git a/core/utils.go b/core/utils.go index 6b9ce63..9159be8 100644 --- a/core/utils.go +++ b/core/utils.go @@ -159,3 +159,14 @@ func DiffUint64(a, b uint64) uint64 { } return b - a } + +// notifyGenesisRounds notifies governance to generate configs based on genesis +// state. +func notifyGenesisRounds(initBlock *types.Block, gov Governance) { + if initBlock.Position.Round != 0 || !initBlock.IsGenesis() { + return + } + for round := uint64(0); round < roundShift; round++ { + gov.NotifyRoundHeight(round, 0) + } +} |