Merge pull request #18 from ngtuna/dynamic-validator

use channel listenning to checkpoint
This commit is contained in:
Tuna 2018-05-22 18:05:54 +07:00 committed by GitHub
commit 48ea492578
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 7 deletions

View file

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

View file

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

View file

@ -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() {

View file

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

View file

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