diff --git a/cmd/tomo/main.go b/cmd/tomo/main.go index 52e4f1bb69..60442de7e6 100644 --- a/cmd/tomo/main.go +++ b/cmd/tomo/main.go @@ -29,6 +29,7 @@ import ( "github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/consensus/clique" "github.com/ethereum/go-ethereum/console" "github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/ethclient" @@ -315,10 +316,12 @@ func startNode(ctx *cli.Context, stack *node.Node) { started = true log.Info("Enabled mining node!!!") } + defer close(clique.Checkpoint) for { - if ethereum.Checkpoint() { - //Checkpoint!!! It's time to reconcile node's state... + select { + case _ = <-clique.Checkpoint: + log.Info("Checkpoint!!! It's time to reconcile node's state...") ok, err := ethereum.ValidateMiner() if err != nil { utils.Fatalf("Can't verify validator permission: %v", err) diff --git a/consensus/clique/clique.go b/consensus/clique/clique.go index 13e8bb878e..60c0ec4b1d 100644 --- a/consensus/clique/clique.go +++ b/consensus/clique/clique.go @@ -65,6 +65,7 @@ var ( diffInTurn = big.NewInt(2) // Block difficulty for in-turn signatures diffNoTurn = big.NewInt(1) // Block difficulty for out-of-turn signatures + Checkpoint chan int ) // Various error messages to mark blocks invalid. These should be private to @@ -216,6 +217,7 @@ func New(config *params.CliqueConfig, db ethdb.Database) *Clique { if conf.Epoch == 0 { conf.Epoch = epochLength } + Checkpoint = make(chan int) // Allocate the snapshot caches and create the engine recents, _ := lru.NewARC(inmemorySnapshots) signatures, _ := lru.NewARC(inmemorySignatures) diff --git a/core/blockchain.go b/core/blockchain.go index b33eb85a44..799c22ac42 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -43,6 +43,7 @@ import ( "github.com/ethereum/go-ethereum/trie" "github.com/hashicorp/golang-lru" "gopkg.in/karalabe/cookiejar.v2/collections/prque" + "github.com/ethereum/go-ethereum/consensus/clique" ) var ( @@ -1185,6 +1186,11 @@ func (bc *BlockChain) insertChain(chain types.Blocks) (int, []interface{}, []*ty stats.processed++ stats.usedGas += usedGas stats.report(chain, i, bc.stateCache.TrieDB().Size()) + if i == len(chain) - 1 { + if (chain[i].NumberU64() % bc.chainConfig.Clique.Epoch) == 0 { + clique.Checkpoint <- 1 + } + } } // Append a single chain head event if we've progressed the chain if lastCanon != nil && bc.CurrentBlock().Hash() == lastCanon.Hash() { diff --git a/eth/backend.go b/eth/backend.go index bd6a53c3be..9f31f935c6 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -357,11 +357,6 @@ func (s *Ethereum) ValidateMiner() (bool, error) { return true, nil } -func (s *Ethereum) Checkpoint() bool { - number := s.blockchain.CurrentHeader().Number.Uint64() - return number%s.chainConfig.Clique.Epoch == 1 || number == 0 -} - func (s *Ethereum) StartMining(local bool) error { eb, err := s.Etherbase() if err != nil { diff --git a/miner/worker.go b/miner/worker.go index 15395ae0b9..742b2398d8 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -36,6 +36,7 @@ import ( "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/params" "gopkg.in/fatih/set.v0" + "github.com/ethereum/go-ethereum/consensus/clique" ) const ( @@ -488,6 +489,10 @@ func (self *worker) commitNewWork() { log.Info("Commit new mining work", "number", work.Block.Number(), "txs", work.tcount, "uncles", len(uncles), "elapsed", common.PrettyDuration(time.Since(tstart))) self.unconfirmed.Shift(work.Block.NumberU64() - 1) } + if (work.Block.NumberU64() % work.config.Clique.Epoch) == 0 { + log.Info("hey checkpoint") + clique.Checkpoint <- 1 + } self.push(work) }