diff options
author | Mission Liao <mission.liao@dexon.org> | 2018-12-18 16:51:29 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-18 16:51:29 +0800 |
commit | eaf271f8e4d16920d8575cf77c65ece2960444d0 (patch) | |
tree | f16137dc83613f306278141886a390de94f906a5 /core | |
parent | 9f240d93507cdf03935ba7e4e3a7b226f150736d (diff) | |
download | dexon-consensus-eaf271f8e4d16920d8575cf77c65ece2960444d0.tar dexon-consensus-eaf271f8e4d16920d8575cf77c65ece2960444d0.tar.gz dexon-consensus-eaf271f8e4d16920d8575cf77c65ece2960444d0.tar.bz2 dexon-consensus-eaf271f8e4d16920d8575cf77c65ece2960444d0.tar.lz dexon-consensus-eaf271f8e4d16920d8575cf77c65ece2960444d0.tar.xz dexon-consensus-eaf271f8e4d16920d8575cf77c65ece2960444d0.tar.zst dexon-consensus-eaf271f8e4d16920d8575cf77c65ece2960444d0.zip |
misc: panic not ready (#374)
* Panic when config/crs not ready
For those calls to Governace.Configuration
and Governance.CRS without checking
returns, replace those calls with these newly
added helpers:
- utils.GetConfigurationWithPanic
- utils.GetCRSWithPanic
They would check returns, and panic directly
if not ready yet.
* Fix a bug that config is not ready
when syncing
Diffstat (limited to 'core')
-rw-r--r-- | core/agreement-mgr.go | 5 | ||||
-rw-r--r-- | core/compaction-chain.go | 3 | ||||
-rw-r--r-- | core/configuration-chain.go | 3 | ||||
-rw-r--r-- | core/consensus.go | 49 | ||||
-rw-r--r-- | core/dkg-tsig-protocol.go | 3 | ||||
-rw-r--r-- | core/syncer/consensus.go | 17 | ||||
-rw-r--r-- | core/ticker.go | 12 | ||||
-rw-r--r-- | core/utils/utils.go | 61 |
8 files changed, 103 insertions, 50 deletions
diff --git a/core/agreement-mgr.go b/core/agreement-mgr.go index 4cb47b1..fb65364 100644 --- a/core/agreement-mgr.go +++ b/core/agreement-mgr.go @@ -248,9 +248,10 @@ func (mgr *agreementMgr) processAgreementResult( "hash", result.BlockHash) mgr.network.PullBlocks(common.Hashes{result.BlockHash}) mgr.logger.Debug("Calling Governance.CRS", "round", result.Position.Round) - crs := mgr.gov.CRS(result.Position.Round) + crs := utils.GetCRSWithPanic(mgr.gov, result.Position.Round, mgr.logger) nIDs := nodes.GetSubSet( - int(mgr.gov.Configuration(result.Position.Round).NotarySetSize), + int(utils.GetConfigWithPanic( + mgr.gov, result.Position.Round, mgr.logger).NotarySetSize), types.NewNotarySetTarget(crs, result.Position.ChainID)) for key := range result.Votes { if err := agreement.processVote(&result.Votes[key]); err != nil { diff --git a/core/compaction-chain.go b/core/compaction-chain.go index dcd99f4..6192abd 100644 --- a/core/compaction-chain.go +++ b/core/compaction-chain.go @@ -25,6 +25,7 @@ import ( "github.com/dexon-foundation/dexon-consensus/common" "github.com/dexon-foundation/dexon-consensus/core/crypto" "github.com/dexon-foundation/dexon-consensus/core/types" + "github.com/dexon-foundation/dexon-consensus/core/utils" ) // Errors for compaction chain module. @@ -80,7 +81,7 @@ func (cc *compactionChain) init(initBlock *types.Block) { // It's the bootstrap case, compactionChain would only deliver blocks until // tips of all chains are received. if initBlock.Finalization.Height == 0 { - cc.chainUnsynced = cc.gov.Configuration(uint64(0)).NumChains + cc.chainUnsynced = utils.GetConfigWithPanic(cc.gov, 0, nil).NumChains } } diff --git a/core/configuration-chain.go b/core/configuration-chain.go index 2b3a859..3a43042 100644 --- a/core/configuration-chain.go +++ b/core/configuration-chain.go @@ -261,7 +261,8 @@ func (cc *configurationChain) recoverDKGInfo(round uint64) error { return ErrDKGNotReady } - threshold := getDKGThreshold(cc.gov.Configuration(round)) + threshold := getDKGThreshold( + utils.GetConfigWithPanic(cc.gov, round, cc.logger)) // Restore group public key. gpk, err := NewDKGGroupPublicKey(round, cc.gov.DKGMasterPublicKeys(round), diff --git a/core/consensus.go b/core/consensus.go index 35f1db8..741330e 100644 --- a/core/consensus.go +++ b/core/consensus.go @@ -400,12 +400,7 @@ func NewConsensus( } // Get configuration for genesis round. var round uint64 - logger.Debug("Calling Governance.Configuration", "round", round) - config := gov.Configuration(round) - if config == nil { - logger.Error("Unable to get configuration", "round", round) - return nil - } + config := utils.GetConfigWithPanic(gov, round, logger) // Init lattice. lattice := NewLattice( dMoment, round, config, authModule, app, debugApp, db, logger) @@ -550,29 +545,19 @@ func (con *Consensus) prepare(initBlock *types.Block) error { // full node. We don't have to notify it. con.roundToNotify = initBlock.Position.Round + 1 initRound := initBlock.Position.Round - con.logger.Debug("Calling Governance.Configuration", "round", initRound) - initConfig := con.gov.Configuration(initRound) + initConfig := utils.GetConfigWithPanic(con.gov, initRound, con.logger) // Setup context. con.ccModule.init(initBlock) - // Setup agreementMgr module. - con.logger.Debug("Calling Governance.Configuration", "round", initRound) - initCfg := con.gov.Configuration(initRound) - if initCfg == nil { - return ErrConfigurationNotReady - } con.logger.Debug("Calling Governance.CRS", "round", initRound) initCRS := con.gov.CRS(initRound) if (initCRS == common.Hash{}) { return ErrCRSNotReady } - if err := con.baMgr.appendConfig(initRound, initCfg, initCRS); err != nil { + if err := con.baMgr.appendConfig(initRound, initConfig, initCRS); err != nil { return err } // Setup lattice module. - initPlusOneCfg := con.gov.Configuration(initRound + 1) - if initPlusOneCfg == nil { - return ErrConfigurationNotReady - } + initPlusOneCfg := utils.GetConfigWithPanic(con.gov, initRound+1, con.logger) if err := con.lattice.AppendConfig(initRound+1, initPlusOneCfg); err != nil { return err } @@ -655,7 +640,8 @@ func (con *Consensus) runCRS(round uint64) { } // Start running next round CRS. con.logger.Debug("Calling Governance.CRS", "round", round) - psig, err := con.cfgModule.preparePartialSignature(round, con.gov.CRS(round)) + psig, err := con.cfgModule.preparePartialSignature( + round, utils.GetCRSWithPanic(con.gov, round, con.logger)) if err != nil { con.logger.Error("Failed to prepare partial signature", "error", err) } else if err = con.authModule.SignDKGPartialSignature(psig); err != nil { @@ -669,7 +655,8 @@ func (con *Consensus) runCRS(round uint64) { "hash", psig.Hash) con.network.BroadcastDKGPartialSignature(psig) con.logger.Debug("Calling Governance.CRS", "round", round) - crs, err := con.cfgModule.runCRSTSig(round, con.gov.CRS(round)) + crs, err := con.cfgModule.runCRSTSig( + round, utils.GetCRSWithPanic(con.gov, round, con.logger)) if err != nil { con.logger.Error("Failed to run CRS Tsig", "error", err) } else { @@ -714,12 +701,11 @@ func (con *Consensus) initialRound( time.Sleep(500 * time.Millisecond) } // Notify BA for new round. - con.logger.Debug("Calling Governance.Configuration", - "round", nextRound) - nextConfig := con.gov.Configuration(nextRound) + nextConfig := utils.GetConfigWithPanic( + con.gov, nextRound, con.logger) con.logger.Debug("Calling Governance.CRS", "round", nextRound) - nextCRS := con.gov.CRS(nextRound) + nextCRS := utils.GetCRSWithPanic(con.gov, nextRound, con.logger) if err := con.baMgr.appendConfig( nextRound, nextConfig, nextCRS); err != nil { panic(err) @@ -758,9 +744,8 @@ func (con *Consensus) initialRound( defer con.dkgReady.L.Unlock() con.dkgRunning = 0 }() - con.logger.Debug("Calling Governance.Configuration", - "round", nextRound) - nextConfig := con.gov.Configuration(nextRound) + nextConfig := utils.GetConfigWithPanic( + con.gov, nextRound, con.logger) con.runDKG(nextRound, nextConfig) }) }(round + 1) @@ -771,9 +756,7 @@ func (con *Consensus) initialRound( // Change round. // Get configuration for next round. nextRound := round + 1 - con.logger.Debug("Calling Governance.Configuration", - "round", nextRound) - nextConfig := con.gov.Configuration(nextRound) + nextConfig := utils.GetConfigWithPanic(con.gov, nextRound, con.logger) con.initialRound( startTime.Add(config.RoundInterval), nextRound, nextConfig) }) @@ -1026,9 +1009,7 @@ func (con *Consensus) deliverBlock(b *types.Block) { // - roundShift // - notifyGenesisRound futureRound := con.roundToNotify + 1 - con.logger.Debug("Calling Governance.Configuration", - "round", con.roundToNotify) - futureConfig := con.gov.Configuration(futureRound) + futureConfig := utils.GetConfigWithPanic(con.gov, futureRound, con.logger) con.logger.Debug("Append Config", "round", futureRound) if err := con.lattice.AppendConfig( futureRound, futureConfig); err != nil { diff --git a/core/dkg-tsig-protocol.go b/core/dkg-tsig-protocol.go index 8e03cbb..b120f81 100644 --- a/core/dkg-tsig-protocol.go +++ b/core/dkg-tsig-protocol.go @@ -26,6 +26,7 @@ import ( "github.com/dexon-foundation/dexon-consensus/core/crypto/dkg" "github.com/dexon-foundation/dexon-consensus/core/types" typesDKG "github.com/dexon-foundation/dexon-consensus/core/types/dkg" + "github.com/dexon-foundation/dexon-consensus/core/utils" ) // Errors for dkg module. @@ -491,7 +492,7 @@ func (tc *TSigVerifierCache) Update(round uint64) (bool, error) { gpk, err := NewDKGGroupPublicKey(round, tc.intf.DKGMasterPublicKeys(round), tc.intf.DKGComplaints(round), - int(tc.intf.Configuration(round).DKGSetSize/3)+1) + int(utils.GetConfigWithPanic(tc.intf, round, nil).DKGSetSize/3)+1) if err != nil { return false, err } diff --git a/core/syncer/consensus.go b/core/syncer/consensus.go index eababa0..da9d352 100644 --- a/core/syncer/consensus.go +++ b/core/syncer/consensus.go @@ -104,7 +104,9 @@ func NewConsensus( prv: prv, logger: logger, validatedChains: make(map[uint32]struct{}), - configs: []*types.Config{gov.Configuration(0)}, + configs: []*types.Config{ + utils.GetConfigWithPanic(gov, 0, logger), + }, roundBeginTimes: []time.Time{dMoment}, receiveChan: make(chan *types.Block, 1000), pullChan: make(chan common.Hash, 1000), @@ -560,17 +562,18 @@ func (con *Consensus) setupConfigs(blocks []*types.Block) { } } // Get configs from governance. - untilRound := maxRound + core.ConfigRoundShift + // + // In fullnode, the notification of new round is yet another TX, which + // needs to be executed after corresponding block delivered. Thus, the + // configuration for 'maxRound + core.ConfigRoundShift' won't be ready when + // seeing this block. + untilRound := maxRound + core.ConfigRoundShift - 1 curMaxNumChains := uint32(0) func() { con.lock.Lock() defer con.lock.Unlock() for r := uint64(len(con.configs)); r <= untilRound; r++ { - cfg := con.gov.Configuration(r) - if cfg == nil { - panic(fmt.Errorf( - "unable to get config for round: %v (syncer)", r)) - } + cfg := utils.GetConfigWithPanic(con.gov, r, con.logger) con.configs = append(con.configs, cfg) con.roundBeginTimes = append( con.roundBeginTimes, diff --git a/core/ticker.go b/core/ticker.go index 3728a79..f8d0c67 100644 --- a/core/ticker.go +++ b/core/ticker.go @@ -17,7 +17,11 @@ package core -import "time" +import ( + "time" + + "github.com/dexon-foundation/dexon-consensus/core/utils" +) // TickerType is the type of ticker. type TickerType int @@ -65,11 +69,11 @@ func newTicker(gov Governance, round uint64, tickerType TickerType) (t Ticker) { var duration time.Duration switch tickerType { case TickerBA: - duration = gov.Configuration(round).LambdaBA + duration = utils.GetConfigWithPanic(gov, round, nil).LambdaBA case TickerDKG: - duration = gov.Configuration(round).LambdaDKG + duration = utils.GetConfigWithPanic(gov, round, nil).LambdaDKG case TickerCRS: - duration = gov.Configuration(round).RoundInterval / 2 + duration = utils.GetConfigWithPanic(gov, round, nil).RoundInterval / 2 } t = newDefaultTicker(duration) } diff --git a/core/utils/utils.go b/core/utils/utils.go new file mode 100644 index 0000000..3e3803d --- /dev/null +++ b/core/utils/utils.go @@ -0,0 +1,61 @@ +// 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 utils + +import ( + "fmt" + + "github.com/dexon-foundation/dexon-consensus/common" + "github.com/dexon-foundation/dexon-consensus/core/types" +) + +type configAccessor interface { + Configuration(round uint64) *types.Config +} + +// GetConfigWithPanic is a helper to access configs, and panic when config for +// that round is not ready yet. +func GetConfigWithPanic(accessor configAccessor, round uint64, + logger common.Logger) *types.Config { + if logger != nil { + logger.Debug("Calling Governance.Configuration", "round", round) + } + c := accessor.Configuration(round) + if c == nil { + panic(fmt.Errorf("configuration is not ready %v", round)) + } + return c +} + +type crsAccessor interface { + CRS(round uint64) common.Hash +} + +// GetCRSWithPanic is a helper to access CRS, and panic when CRS for that +// round is not ready yet. +func GetCRSWithPanic(accessor crsAccessor, round uint64, + logger common.Logger) common.Hash { + if logger != nil { + logger.Debug("Calling Governance.CRS", "round", round) + } + crs := accessor.CRS(round) + if (crs == common.Hash{}) { + panic(fmt.Errorf("CRS is not ready %v", round)) + } + return crs + +} |