Merge pull request #12 from ngtuna/miner-validator

check miner == validator
This commit is contained in:
Tuna 2018-05-15 11:24:23 +07:00 committed by GitHub
commit ca4470cc58
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 0 deletions

View file

@ -290,6 +290,14 @@ func startNode(ctx *cli.Context, stack *node.Node) {
if err := stack.Service(&ethereum); err != nil { if err := stack.Service(&ethereum); err != nil {
utils.Fatalf("Ethereum service not running: %v", err) utils.Fatalf("Ethereum service not running: %v", err)
} }
// Mining only enabled for validator nodes
if ok, err := ethereum.ValidateMiner(); err != nil {
utils.Fatalf("Can't verify validator permission: %v", err)
} else if !ok {
utils.Fatalf("Only validator can mine blocks")
}
// Use a reduced number of threads if requested // Use a reduced number of threads if requested
if threads := ctx.GlobalInt(utils.MinerThreadsFlag.Name); threads > 0 { if threads := ctx.GlobalInt(utils.MinerThreadsFlag.Name); threads > 0 {
type threaded interface { type threaded interface {

View file

@ -367,6 +367,18 @@ func (c *Clique) verifyCascadingFields(chain consensus.ChainReader, header *type
return c.verifySeal(chain, header, parents) return c.verifySeal(chain, header, parents)
} }
func (c *Clique) GetSnapshot(chain consensus.ChainReader, header *types.Header) (*Snapshot, error) {
number := header.Number.Uint64()
if number == 0 {
return nil, nil
}
snap, err := c.snapshot(chain, number-1, header.ParentHash, nil)
if err != nil {
return nil, err
}
return snap, nil
}
// snapshot retrieves the authorization snapshot at a given point in time. // snapshot retrieves the authorization snapshot at a given point in time.
func (c *Clique) snapshot(chain consensus.ChainReader, number uint64, hash common.Hash, parents []*types.Header) (*Snapshot, error) { func (c *Clique) snapshot(chain consensus.ChainReader, number uint64, hash common.Hash, parents []*types.Header) (*Snapshot, error) {
// Search for a snapshot in memory or on disk for checkpoints // Search for a snapshot in memory or on disk for checkpoints

View file

@ -334,6 +334,26 @@ func (self *Ethereum) SetEtherbase(etherbase common.Address) {
self.miner.SetEtherbase(etherbase) self.miner.SetEtherbase(etherbase)
} }
func (s *Ethereum) ValidateMiner() (bool, error) {
eb, err := s.Etherbase()
if err != nil {
return false, err
}
if c, ok := s.engine.(*clique.Clique); !ok {
return false, fmt.Errorf("Only verify miners in Clique protocol")
} else {
//check if miner's wallet is in set of validators
snap, err := c.GetSnapshot(s.blockchain, s.blockchain.CurrentHeader())
if err != nil {
return false, fmt.Errorf("Can't verify miner: %v", err)
}
if _, authorized := snap.Signers[eb]; !authorized {
return false, fmt.Errorf("This miner doesn't belong to set of validators")
}
}
return true, nil
}
func (s *Ethereum) StartMining(local bool) error { func (s *Ethereum) StartMining(local bool) error {
eb, err := s.Etherbase() eb, err := s.Etherbase()
if err != nil { if err != nil {