diff options
author | Mission Liao <mission.liao@dexon.org> | 2018-11-08 10:35:52 +0800 |
---|---|---|
committer | Jimmy Hu <jimmy.hu@dexon.org> | 2018-11-08 10:35:52 +0800 |
commit | 33d9311270414d8911122a4c7354773786df7f85 (patch) | |
tree | edbbe64e54f8c22b55297356fa4724c300e66799 /core | |
parent | 766e6aac32b8f97934833c06814c37dbdd9b7ae2 (diff) | |
download | dexon-consensus-33d9311270414d8911122a4c7354773786df7f85.tar dexon-consensus-33d9311270414d8911122a4c7354773786df7f85.tar.gz dexon-consensus-33d9311270414d8911122a4c7354773786df7f85.tar.bz2 dexon-consensus-33d9311270414d8911122a4c7354773786df7f85.tar.lz dexon-consensus-33d9311270414d8911122a4c7354773786df7f85.tar.xz dexon-consensus-33d9311270414d8911122a4c7354773786df7f85.tar.zst dexon-consensus-33d9311270414d8911122a4c7354773786df7f85.zip |
Revert "core: unbind global round in Consensus (#304)" (#306)
This reverts commit 3714ebf2f1054d9984d37b89cf17e885a5856532.
Diffstat (limited to 'core')
-rw-r--r-- | core/consensus.go | 116 | ||||
-rw-r--r-- | core/consensus_test.go | 4 |
2 files changed, 59 insertions, 61 deletions
diff --git a/core/consensus.go b/core/consensus.go index 09bc0a8..6ff67bb 100644 --- a/core/consensus.go +++ b/core/consensus.go @@ -105,9 +105,8 @@ func (recv *consensusBAReceiver) ConfirmBlock( hash common.Hash, votes map[types.NodeID]*types.Vote) { var block *types.Block if (hash == common.Hash{}) { - aID := recv.agreementModule.agreementID() recv.consensus.logger.Info("Empty block is confirmed", - "position", &aID) + "position", recv.agreementModule.agreementID()) var err error block, err = recv.consensus.proposeEmptyBlock(recv.chainID) if err != nil { @@ -268,8 +267,9 @@ func (recv *consensusDKGReceiver) ProposeDKGFinalize(final *typesDKG.Finalize) { // Consensus implements DEXON Consensus algorithm. type Consensus struct { // Node Info. - ID types.NodeID - authModule *Authenticator + ID types.NodeID + authModule *Authenticator + currentConfig *types.Config // BA. baModules []*agreement @@ -351,6 +351,7 @@ func NewConsensus( // Construct Consensus instance. con := &Consensus{ ID: ID, + currentConfig: config, ccModule: newCompactionChain(gov), lattice: lattice, app: app, @@ -405,21 +406,19 @@ func (con *Consensus) Run(initBlock *types.Block) { con.logger.Debug("Calling Governance.NotifyRoundHeight for genesis rounds", "block", initBlock) notifyGenesisRounds(initBlock, con.gov) - initRound := initBlock.Position.Round - con.logger.Debug("Calling Governance.Configuration", "round", initRound) - initConfig := con.gov.Configuration(initRound) // Setup context. con.ctx, con.ctxCancel = context.WithCancel(context.Background()) con.ccModule.init(initBlock) // TODO(jimmy-dexon): change AppendConfig to add config for specific round. - for i := uint64(0); i <= initRound; i++ { + for i := uint64(0); i < initBlock.Position.Round; i++ { con.logger.Debug("Calling Governance.Configuration", "round", i+1) cfg := con.gov.Configuration(i + 1) if err := con.lattice.AppendConfig(i+1, cfg); err != nil { panic(err) } } - dkgSet, err := con.nodeSetCache.GetDKGSet(initRound) + round0 := uint64(0) + dkgSet, err := con.nodeSetCache.GetDKGSet(round0) if err != nil { panic(err) } @@ -428,16 +427,19 @@ func (con *Consensus) Run(initBlock *types.Block) { // Sleep until dMoment come. time.Sleep(con.dMoment.Sub(time.Now().UTC())) if _, exist := dkgSet[con.ID]; exist { - con.logger.Info("Selected as DKG set", "round", initRound) - con.cfgModule.registerDKG(initRound, int(initConfig.DKGSetSize)/3+1) - con.event.RegisterTime(con.dMoment.Add(initConfig.RoundInterval/4), + con.logger.Info("Selected as DKG set", "round", round0) + con.cfgModule.registerDKG(round0, int(con.currentConfig.DKGSetSize)/3+1) + con.event.RegisterTime(con.dMoment.Add(con.currentConfig.RoundInterval/4), func(time.Time) { - con.runDKGTSIG(initRound, initConfig) + con.runDKGTSIG(round0) }) } - con.initialRound(con.dMoment, initRound, initConfig) - ticks := make([]chan struct{}, 0, initConfig.NumChains) - for i := uint32(0); i < initConfig.NumChains; i++ { + round1 := uint64(1) + con.logger.Debug("Calling Governance.Configuration", "round", round1) + con.lattice.AppendConfig(round1, con.gov.Configuration(round1)) + con.initialRound(con.dMoment) + ticks := make([]chan struct{}, 0, con.currentConfig.NumChains) + for i := uint32(0); i < con.currentConfig.NumChains; i++ { tick := make(chan struct{}) ticks = append(ticks, tick) go con.runBA(i, tick) @@ -476,9 +478,8 @@ BALoop: select { case newNotary := <-recv.restartNotary: if newNotary { - configForNewRound := con.gov.Configuration(recv.round) recv.changeNotaryTime = - recv.changeNotaryTime.Add(configForNewRound.RoundInterval) + recv.changeNotaryTime.Add(con.currentConfig.RoundInterval) nodes, err := con.nodeSetCache.GetNodeSet(recv.round) if err != nil { panic(err) @@ -488,7 +489,7 @@ BALoop: con.logger.Debug("Calling Governance.Configuration", "round", recv.round) nIDs = nodes.GetSubSet( - int(configForNewRound.NotarySetSize), + int(con.gov.Configuration(recv.round).NotarySetSize), types.NewNotarySetTarget(crs, chainID)) } nextPos := con.lattice.NextPosition(chainID) @@ -499,7 +500,7 @@ BALoop: if agreement.pullVotes() { pos := agreement.agreementID() con.logger.Debug("Calling Network.PullVotes for syncing votes", - "position", &pos) + "position", pos) con.network.PullVotes(pos) } err := agreement.nextState() @@ -526,7 +527,7 @@ BALoop: } // runDKGTSIG starts running DKG+TSIG protocol. -func (con *Consensus) runDKGTSIG(round uint64, config *types.Config) { +func (con *Consensus) runDKGTSIG(round uint64) { con.dkgReady.L.Lock() defer con.dkgReady.L.Unlock() if con.dkgRunning != 0 { @@ -542,7 +543,7 @@ func (con *Consensus) runDKGTSIG(round uint64, config *types.Config) { con.dkgRunning = 2 DKGTime := time.Now().Sub(startTime) if DKGTime.Nanoseconds() >= - config.RoundInterval.Nanoseconds()/2 { + con.currentConfig.RoundInterval.Nanoseconds()/2 { con.logger.Warn("Your computer cannot finish DKG on time!", "nodeID", con.ID.String()) } @@ -582,10 +583,11 @@ func (con *Consensus) runDKGTSIG(round uint64, config *types.Config) { }() } -func (con *Consensus) runCRS(round uint64) { +func (con *Consensus) runCRS() { // Start running next round CRS. - con.logger.Debug("Calling Governance.CRS", "round", round) - psig, err := con.cfgModule.preparePartialSignature(round, con.gov.CRS(round)) + con.logger.Debug("Calling Governance.CRS", "round", con.round) + psig, err := con.cfgModule.preparePartialSignature( + con.round, con.gov.CRS(con.round)) if err != nil { con.logger.Error("Failed to prepare partial signature", "error", err) } else if err = con.authModule.SignDKGPartialSignature(psig); err != nil { @@ -598,89 +600,85 @@ func (con *Consensus) runCRS(round uint64) { "round", psig.Round, "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)) + con.logger.Debug("Calling Governance.CRS", "round", con.round) + crs, err := con.cfgModule.runCRSTSig(con.round, con.gov.CRS(con.round)) if err != nil { con.logger.Error("Failed to run CRS Tsig", "error", err) } else { con.logger.Debug("Calling Governance.ProposeCRS", - "round", round+1, + "round", con.round+1, "crs", hex.EncodeToString(crs)) - con.gov.ProposeCRS(round+1, crs) + con.gov.ProposeCRS(con.round+1, crs) } } } -func (con *Consensus) initialRound( - startTime time.Time, round uint64, config *types.Config) { +func (con *Consensus) initialRound(startTime time.Time) { select { case <-con.ctx.Done(): return default: } - curDkgSet, err := con.nodeSetCache.GetDKGSet(round) + con.logger.Debug("Calling Governance.Configuration", "round", con.round) + con.currentConfig = con.gov.Configuration(con.round) + curDkgSet, err := con.nodeSetCache.GetDKGSet(con.round) if err != nil { - con.logger.Error("Error getting DKG set", "round", round, "error", err) + con.logger.Error("Error getting DKG set", "round", con.round, "error", err) curDkgSet = make(map[types.NodeID]struct{}) } if _, exist := curDkgSet[con.ID]; exist { - con.event.RegisterTime(startTime.Add(config.RoundInterval/2), + con.event.RegisterTime(startTime.Add(con.currentConfig.RoundInterval/2), func(time.Time) { go func() { - con.runCRS(round) + con.runCRS() }() }) } - con.event.RegisterTime(startTime.Add(config.RoundInterval/2+config.LambdaDKG), + con.event.RegisterTime(startTime.Add(con.currentConfig.RoundInterval/2), func(time.Time) { - go func(nextRound uint64) { + go func() { + ticker := newTicker(con.gov, con.round, TickerDKG) + <-ticker.Tick() // Normally, gov.CRS would return non-nil. Use this for in case of // unexpected network fluctuation and ensure the robustness. - for (con.gov.CRS(nextRound) == common.Hash{}) { + for (con.gov.CRS(con.round+1) == common.Hash{}) { con.logger.Info("CRS is not ready yet. Try again later...", "nodeID", con.ID) time.Sleep(500 * time.Millisecond) } - nextDkgSet, err := con.nodeSetCache.GetDKGSet(nextRound) + nextDkgSet, err := con.nodeSetCache.GetDKGSet(con.round + 1) if err != nil { con.logger.Error("Error getting DKG set", - "round", nextRound, - "error", err) + "round", con.round+1, "error", err) return } if _, exist := nextDkgSet[con.ID]; !exist { return } - con.logger.Info("Selected as DKG set", "round", nextRound) + con.logger.Info("Selected as DKG set", "round", con.round+1) con.cfgModule.registerDKG( - nextRound, int(config.DKGSetSize/3)+1) + con.round+1, int(con.currentConfig.DKGSetSize/3)+1) con.event.RegisterTime( - startTime.Add(config.RoundInterval*2/3), + startTime.Add(con.currentConfig.RoundInterval*2/3), func(time.Time) { func() { con.dkgReady.L.Lock() defer con.dkgReady.L.Unlock() con.dkgRunning = 0 }() - con.logger.Debug("Calling Governance.Configuration", - "round", nextRound) - nextConfig := con.gov.Configuration(nextRound) - con.runDKGTSIG(nextRound, nextConfig) + con.runDKGTSIG(con.round + 1) }) - }(round + 1) + }() }) - con.event.RegisterTime(startTime.Add(config.RoundInterval), + con.event.RegisterTime(startTime.Add(con.currentConfig.RoundInterval), func(time.Time) { // Change round. - nextRound := round + 1 + con.round++ con.logger.Debug("Calling Governance.Configuration", - "round", nextRound) - nextConfig := con.gov.Configuration(nextRound) - con.lattice.AppendConfig(nextRound, nextConfig) - con.initialRound( - startTime.Add(config.RoundInterval), nextRound, nextConfig) - con.round = nextRound + "round", con.round+1) + con.lattice.AppendConfig(con.round+1, con.gov.Configuration(con.round+1)) + con.initialRound(startTime.Add(con.currentConfig.RoundInterval)) }) } @@ -845,7 +843,7 @@ func (con *Consensus) ProcessAgreementResult( agreement := con.baModules[rand.Position.ChainID] aID := agreement.agreementID() if rand.Position.Newer(&aID) { - con.logger.Info("Syncing BA", "position", &rand.Position) + con.logger.Info("Syncing BA", "position", rand.Position) nodes, err := con.nodeSetCache.GetNodeSet(rand.Position.Round) if err != nil { return err @@ -946,7 +944,7 @@ func (con *Consensus) ProcessBlockRandomnessResult( } con.logger.Debug("Calling Network.BroadcastRandomnessResult", "hash", rand.BlockHash, - "position", &rand.Position, + "position", rand.Position, "randomness", hex.EncodeToString(rand.Randomness)) con.network.BroadcastRandomnessResult(rand) if err := con.ccModule.processBlockRandomnessResult(rand); err != nil { diff --git a/core/consensus_test.go b/core/consensus_test.go index f518630..f5ab69f 100644 --- a/core/consensus_test.go +++ b/core/consensus_test.go @@ -522,7 +522,7 @@ func (s *ConsensusTestSuite) TestDKGCRS() { con.cfgModule.registerDKG(uint64(0), n/3+1) } for _, con := range cons { - con.runDKGTSIG(0, gov.Configuration(0)) + con.runDKGTSIG(uint64(0)) } for _, con := range cons { func() { @@ -536,7 +536,7 @@ func (s *ConsensusTestSuite) TestDKGCRS() { crsFinish := make(chan struct{}) for _, con := range cons { go func(con *Consensus) { - con.runCRS(0) + con.runCRS() crsFinish <- struct{}{} }(con) } |