From 7adecc32d35747632f8a70ceceb75c7b25603267 Mon Sep 17 00:00:00 2001 From: MestryOmkar Date: Mon, 22 Oct 2018 10:37:12 +0530 Subject: [PATCH] node waits to his turn until there is a new block comes in --- core/blockchain.go | 1 - eth/backend.go | 4 ++-- miner/worker.go | 46 +++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index d79ebd2a3c..d79f634a7a 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1243,7 +1243,6 @@ func (st *insertStats) report(chain []*types.Block, index int, cache common.Stor context = append(context, []interface{}{"ignored", st.ignored}...) } log.Info("Imported new chain segment", context...) - *st = insertStats{startTime: now, lastIndex: index + 1} } } diff --git a/eth/backend.go b/eth/backend.go index d949fe08fd..1c691c4212 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -471,8 +471,8 @@ func (s *Ethereum) StartStaking(local bool) error { return nil } -func (s *Ethereum) StopStaking() { s.miner.Stop() } -func (s *Ethereum) IsStaking() bool { return s.miner.Mining() } +func (s *Ethereum) StopStaking() { s.miner.Stop() } +func (s *Ethereum) IsStaking() bool { return s.miner.Mining() } func (s *Ethereum) Miner() *miner.Miner { return s.miner } func (s *Ethereum) AccountManager() *accounts.Manager { return s.accountManager } diff --git a/miner/worker.go b/miner/worker.go index 6292468098..b6473913dc 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -51,6 +51,8 @@ const ( chainHeadChanSize = 10 // chainSideChanSize is the size of channel listening to ChainSideEvent. chainSideChanSize = 10 + // Timeout waiting for M1 + m1Timeout = 1 ) // Agent can register themself with the worker @@ -415,6 +417,24 @@ func (self *worker) makeCurrent(parent *types.Block, header *types.Header) error return nil } +func abs(x int64) int64 { + if x < 0 { + return -x + } + return x +} + +func hop(len, pre, cur int) int { + switch { + case pre < cur: + return cur - (pre + 1) + case pre > cur: + return (len - pre) + (cur - 1) + default: + return len - 1 + } +} + func (self *worker) commitNewWork() { self.mu.Lock() defer self.mu.Unlock() @@ -439,14 +459,34 @@ func (self *worker) commitNewWork() { log.Error("Failed when trying to commit new work", "err", err) return } - ok, err := clique.YourTurn(masternodes, snap, parent.Header(), self.coinbase) + preIndex, curIndex, ok, err := clique.YourTurn(masternodes, snap, parent.Header(), self.coinbase) if err != nil { log.Error("Failed when trying to commit new work", "err", err) return } if !ok { - log.Info("Not our turn to commit block. Wait for next time") - return + log.Info("Not my turn to commit block. Waiting...") + // in case some nodes are down + if preIndex == -1 { + // first block + return + } + h := hop(len(masternodes), preIndex, curIndex) + gap := int64(c.GetPeriod()) * int64(h) + log.Info("Distance from the parent block", "seconds", gap, "hops", h) + L: + select { + case newBlock := <-self.chainHeadCh: + if newBlock.Block.NumberU64() > parent.NumberU64() { + log.Info("New block has came already. Skip this turn", "new block", newBlock.Block.NumberU64(), "current block", parent.NumberU64()) + self.chainHeadCh <- newBlock + return + } + case <-time.After(time.Duration(gap+m1Timeout) * time.Second): + // wait enough. It's my turn + log.Info("Wait enough. It's my turn", "waited seconds", gap+m1Timeout) + break L + } } } }