Skip to content

Commit

Permalink
Merge pull request expanse-org#18 from ngtuna/dynamic-validator
Browse files Browse the repository at this point in the history
use channel listenning to checkpoint
  • Loading branch information
ngtuna committed May 22, 2018
2 parents 302d2d9 + ce3e641 commit 48ea492
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
7 changes: 5 additions & 2 deletions cmd/tomo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions consensus/clique/clique.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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() {
Expand Down
5 changes: 0 additions & 5 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 48ea492

Please sign in to comment.