From 2cf18fd299ea0fc270b213343314cab652cac271 Mon Sep 17 00:00:00 2001 From: Jimmy Hu Date: Mon, 18 Feb 2019 18:44:35 +0800 Subject: core: fix stuffs (#448) * Add log * core: delay first dkg * Fix test * core: only runDKG in prepare at round 0 --- core/blockchain.go | 4 ++++ core/consensus.go | 37 +++++++++++++++++++++---------------- core/consensus_test.go | 1 + 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index bfa8552..d1aa644 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -550,9 +550,13 @@ func (bc *blockChain) prepareBlock(position types.Position, } b.ParentHash = tip.Hash if !empty { + bc.logger.Debug("Calling Application.PreparePayload", + "position", b.Position) if b.Payload, err = bc.app.PreparePayload(b.Position); err != nil { return } + bc.logger.Debug("Calling Application.PrepareWitness", + "height", tip.Witness.Height) if b.Witness, err = bc.app.PrepareWitness( tip.Witness.Height); err != nil { return diff --git a/core/consensus.go b/core/consensus.go index ebbcbd1..f4c0a37 100644 --- a/core/consensus.go +++ b/core/consensus.go @@ -590,23 +590,28 @@ func (con *Consensus) prepare(initBlock *types.Block) (err error) { if err = con.bcModule.appendConfig(initRound+1, initPlusOneCfg); err != nil { return } - // Register events. - dkgSet, err := con.nodeSetCache.GetDKGSet(initRound) - if err != nil { - return - } - if _, exist := dkgSet[con.ID]; exist { - con.logger.Info("Selected as DKG set", "round", initRound) - go func() { - // Sleep until dMoment come. - time.Sleep(con.dMoment.Sub(time.Now().UTC())) - con.cfgModule.registerDKG(initRound, getDKGThreshold(initConfig)) - con.event.RegisterTime(con.dMoment.Add(initConfig.RoundInterval/4), - func(time.Time) { - con.runDKG(initRound, initConfig) - }) - }() + if initRound == 0 { + dkgSet, err := con.nodeSetCache.GetDKGSet(initRound) + if err != nil { + return err + } + if _, exist := dkgSet[con.ID]; exist { + con.logger.Info("Selected as DKG set", "round", initRound) + go func() { + // Sleep until dMoment come. + time.Sleep(con.dMoment.Sub(time.Now().UTC())) + // Network is not stable upon starting. Wait some time to ensure first + // DKG would success. Three is a magic number. + time.Sleep(initConfig.MinBlockInterval * 3) + con.cfgModule.registerDKG(initRound, getDKGThreshold(initConfig)) + con.event.RegisterTime(con.dMoment.Add(initConfig.RoundInterval/4), + func(time.Time) { + con.runDKG(initRound, initConfig) + }) + }() + } } + // Register events. con.initialRound(con.dMoment, initRound, initConfig) return } diff --git a/core/consensus_test.go b/core/consensus_test.go index 67070d2..fe28320 100644 --- a/core/consensus_test.go +++ b/core/consensus_test.go @@ -237,6 +237,7 @@ func (s *ConsensusTestSuite) TestDKGCRS() { nID := types.NewNodeID(key.PublicKey()) cons[nID] = con } + time.Sleep(gov.Configuration(0).MinBlockInterval * 4) for _, con := range cons { go con.runDKG(0, gov.Configuration(0)) } -- cgit v1.2.3