Skip to content

Commit

Permalink
Merge pull request #1 from youchainhq/develop
Browse files Browse the repository at this point in the history
timestampfix
  • Loading branch information
kevinho authored May 20, 2020
2 parents 99bfc04 + 63ee735 commit e6482c9
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 13 deletions.
4 changes: 4 additions & 0 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ type Engine interface {

// Stop the engine
Stop() error

Pause() error

Resume() error
}

type Ucon interface {
Expand Down
3 changes: 3 additions & 0 deletions consensus/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ var (
// that is known, but the state of which is not available.
ErrPrunedAncestor = errors.New("pruned ancestor")

//new block should not older than parent
ErrOlderBlockTime = errors.New("timestamp older than parent")

// ErrFutureBlock is returned when a block's timestamp is in the future according
// to the current node.
ErrFutureBlock = errors.New("block in the future")
Expand Down
8 changes: 8 additions & 0 deletions consensus/solo/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ func (s *Solo) StartMining(chain consensus.ChainReader, inserter consensus.MineI
return nil
}

func (s *Solo) Pause() error {
return nil
}

func (s *Solo) Resume() error {
return nil
}

func (s *Solo) Stop() error {
//do nothing
return nil
Expand Down
4 changes: 4 additions & 0 deletions consensus/ucon/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ func (s *Server) verifyCascadingFields(chain consensus.ChainReader, header *type
return consensus.ErrUnknownAncestor
}

if header.Time <= parentHeader.Time {
return consensus.ErrOlderBlockTime
}

local := chain.GetHeaderByNumber(header.Number.Uint64())
if local != nil && header.Hash() != local.Hash() {
logging.Error("Exist canonical", "localHash", local.Hash().String(), "headerHash", header.Hash().String())
Expand Down
12 changes: 12 additions & 0 deletions consensus/ucon/timer_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,18 @@ func (tm *TimerManager) Stop() {
close(tm.quitChan)
}

func (tm *TimerManager) Pause() {
tm.lock.Lock()
defer tm.lock.Unlock()

if tm.timeoutTimer != nil {
tm.timeoutTimer.Stop()
}
if tm.stepTimer != nil {
tm.stepTimer.Stop()
}
}

func (tm *TimerManager) NewAddressMsg(ev ReceivedMsgEvent, validatorKind params.ValidatorKind, chamberNum, houseNum uint64) bool {
tm.lock.Lock()
defer tm.lock.Unlock()
Expand Down
46 changes: 44 additions & 2 deletions consensus/ucon/ucon.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ func (s *Server) StartMining(chain consensus.ChainReader, inserter consensus.Min
// Stop the engine
func (s *Server) Stop() error {
if !atomic.CompareAndSwapInt32(&s.alreadyStarted, 1, 0) {
logging.Error("Ucon already Stop")
return nil
}
logging.Info("Ucon Stop")
Expand All @@ -232,6 +233,44 @@ func (s *Server) Stop() error {
return nil
}

func (s *Server) Pause() error {
if !atomic.CompareAndSwapInt32(&s.alreadyStarted, 1, 0) {
logging.Error("Ucon already Stop")
return nil
}

s.timer.Pause()

logging.Info("consensus pause succeed")
return nil
}

func (s *Server) Resume() error {
logging.Info("try to resume")
if s.rawSk == nil {
return consensus.ErrValKeyNotSet
}
if s.blsSk == nil {
return consensus.ErrBlsKeyNotSet
}
s.startMiningLock.Lock()
defer s.startMiningLock.Unlock()
if s.isMining() {
logging.Info("Ucon Already Started")
return nil
}

err := s.StartNewRound(true)
if err != nil {
logging.Info("start new round error")
return err
}

atomic.StoreInt32(&s.alreadyStarted, 1)
logging.Info("consensus resume succeed")
return nil
}

func (s *Server) StartNewRound(newRound bool) error {
err := s.clearData(newRound)
if err != nil {
Expand Down Expand Up @@ -518,8 +557,11 @@ func (s *Server) updateBlockHeader(ev UpdateExistedHeaderEvent) {
//HandleMsg handles related consensus messages or
// fallback to default procotol manager's handler
func (s *Server) HandleMsg(data []byte, receivedAt time.Time) error {
err := s.msgHandler.HandleMsg(data, receivedAt)
return err
if s.isMining() {
err := s.msgHandler.HandleMsg(data, receivedAt)
return err
}
return nil
}

func (s *Server) processTimeout(round *big.Int, roundIndex uint32) {
Expand Down
7 changes: 4 additions & 3 deletions consensus/ucon/vote_bls.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,10 @@ type VoteBLSMgr struct {

func NewVoteBLSMgr(rawSk *ecdsa.PrivateKey, blsSk bls.SecretKey) *VoteBLSMgr {
vb := &VoteBLSMgr{
rawSk: rawSk,
blsSk: blsSk,
blsMgr: bls.NewBlsManager(),
rawSk: rawSk,
blsSk: blsSk,
blsMgr: bls.NewBlsManager(),
currRound: new(big.Int),
}
vb.Verifier = NewBlsVerifier(vb.blsMgr)
if nil != rawSk {
Expand Down
3 changes: 0 additions & 3 deletions consensus/ucon/vote_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,7 @@ func NewVoteDB(db youdb.Database, rawSk *ecdsa.PrivateKey) *VoteDB {
}

func (v *VoteDB) Stop() {
v.lock.Lock()
defer v.lock.Unlock()

v.db.Close()
}

func (v *VoteDB) UpdateContext(round *big.Int, roundIndex uint32) {
Expand Down
15 changes: 13 additions & 2 deletions miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/youchainhq/go-youchain/core/state"
"github.com/youchainhq/go-youchain/core/types"
"github.com/youchainhq/go-youchain/event"
"github.com/youchainhq/go-youchain/logging"
"github.com/youchainhq/go-youchain/node"
"github.com/youchainhq/go-youchain/params"
"github.com/youchainhq/go-youchain/you/downloader"
Expand Down Expand Up @@ -72,13 +73,19 @@ func (m *Miner) update() {
select {
case ev := <-events.Chan():
if ev == nil {
logging.Info("events.Chan quit")
return
}
switch ev.Data.(type) {
case downloader.StartEvent:
log.Info("miner recv StartEvent")
atomic.StoreInt32(&m.canStart, 0)
if m.worker.isRunning() {
log.Error("miner stop consensus engine")
if err := m.engine.Pause(); err != nil {
log.Error("miner pause consensus engine", "error", err)
}

m.Stop()
atomic.StoreInt32(&m.shouldStart, 1)
log.Info("mining aborted due to sync")
Expand All @@ -90,10 +97,14 @@ func (m *Miner) update() {
atomic.StoreInt32(&m.canStart, 1)
atomic.StoreInt32(&m.shouldStart, 0)
if shouldStart {

log.Info("miner restart consensus engine")
if err := m.engine.Resume(); err != nil {
log.Error("miner resume consensus engine", "error", err)
}

m.Start()
}
// stop immediately and ignore all further pending events
return
}
case <-m.quit:
return
Expand Down
8 changes: 7 additions & 1 deletion miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func (w *worker) commitNewWork(interrupt *int32) {

//quick fast
if !w.isRunning() {
log.Info("worker is not running")
log.Warn("worker is not running")
return
}

Expand All @@ -279,6 +279,12 @@ func (w *worker) commitNewWork(interrupt *int32) {
coinbase.SetBytes(w.engine.GetValMainAddress().Bytes())

parent := w.chain.CurrentBlock()

if uint64(time.Now().Unix()) <= parent.Time() {
log.Warn("local time is far behind")
return
}

num := parent.Number()
header := &types.Header{
ParentHash: parent.Hash(),
Expand Down
14 changes: 14 additions & 0 deletions you/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ type YouChain struct {
//DB
chainDb youdb.Database

voteDb youdb.Database

accountManager *accounts.Manager

BloomRequests chan chan *bloombits.Retrieval // Channel receiving bloom data retrieval requests
Expand Down Expand Up @@ -120,6 +122,7 @@ func New(config *Config, nodeConfig *node.Config) (*YouChain, error) {
nodeConfig: nodeConfig,
quit: make(chan bool),
chainDb: chainDb,
voteDb: voteDb,
engine: engine,
eventMux: eventMux,
BloomRequests: make(chan chan *bloombits.Retrieval),
Expand Down Expand Up @@ -342,6 +345,16 @@ func (you *YouChain) StartMining() error {
return nil
}

func (you *YouChain) StopMining() error {
err := you.engine.Stop()
if err != nil {
logging.Error("stop mining", "err", err)
}

you.miner.Stop()
return nil
}

func (you *YouChain) Stop() error {
you.blockChain.Stop()

Expand All @@ -352,6 +365,7 @@ func (you *YouChain) Stop() error {
you.eventMux.Stop()

you.chainDb.Close()
you.voteDb.Close()
you.stakingMan.Stop()

close(you.quit)
Expand Down
4 changes: 2 additions & 2 deletions you/miner_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func (api *PrivateMinerApi) Start() error {
return api.c.StartMining()
}

func (api *PrivateMinerApi) Stop() {
api.c.miner.Stop()
func (api *PrivateMinerApi) Stop() error {
return api.c.StopMining()
}

// Mining returns an indication if this node is currently mining.
Expand Down

0 comments on commit e6482c9

Please sign in to comment.