node waits to his turn until there is a new block comes in

This commit is contained in:
MestryOmkar 2018-10-22 10:37:12 +05:30
parent 4788ad3d80
commit 7adecc32d3
3 changed files with 45 additions and 6 deletions

View file

@ -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}
}
}

View file

@ -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 }

View file

@ -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
}
}
}
}