github.com/avence12/go-ethereum@v1.5.10-0.20170320123548-1dfd65f6d047/miner/unconfirmed.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package miner
    18  
    19  import (
    20  	"container/ring"
    21  	"fmt"
    22  	"sync"
    23  
    24  	"github.com/ethereum/go-ethereum/common"
    25  	"github.com/ethereum/go-ethereum/core/types"
    26  	"github.com/ethereum/go-ethereum/log"
    27  )
    28  
    29  // headerRetriever is used by the unconfirmed block set to verify whether a previously
    30  // mined block is part of the canonical chain or not.
    31  type headerRetriever interface {
    32  	// GetHeaderByNumber retrieves the canonical header associated with a block number.
    33  	GetHeaderByNumber(number uint64) *types.Header
    34  }
    35  
    36  // unconfirmedBlock is a small collection of metadata about a locally mined block
    37  // that is placed into a unconfirmed set for canonical chain inclusion tracking.
    38  type unconfirmedBlock struct {
    39  	index uint64
    40  	hash  common.Hash
    41  }
    42  
    43  // unconfirmedBlocks implements a data structure to maintain locally mined blocks
    44  // have have not yet reached enough maturity to guarantee chain inclusion. It is
    45  // used by the miner to provide logs to the user when a previously mined block
    46  // has a high enough guarantee to not be reorged out of te canonical chain.
    47  type unconfirmedBlocks struct {
    48  	chain  headerRetriever // Blockchain to verify canonical status through
    49  	depth  uint            // Depth after which to discard previous blocks
    50  	blocks *ring.Ring      // Block infos to allow canonical chain cross checks
    51  	lock   sync.RWMutex    // Protects the fields from concurrent access
    52  }
    53  
    54  // newUnconfirmedBlocks returns new data structure to track currently unconfirmed blocks.
    55  func newUnconfirmedBlocks(chain headerRetriever, depth uint) *unconfirmedBlocks {
    56  	return &unconfirmedBlocks{
    57  		chain: chain,
    58  		depth: depth,
    59  	}
    60  }
    61  
    62  // Insert adds a new block to the set of unconfirmed ones.
    63  func (set *unconfirmedBlocks) Insert(index uint64, hash common.Hash) {
    64  	// If a new block was mined locally, shift out any old enough blocks
    65  	set.Shift(index)
    66  
    67  	// Create the new item as its own ring
    68  	item := ring.New(1)
    69  	item.Value = &unconfirmedBlock{
    70  		index: index,
    71  		hash:  hash,
    72  	}
    73  	// Set as the initial ring or append to the end
    74  	set.lock.Lock()
    75  	defer set.lock.Unlock()
    76  
    77  	if set.blocks == nil {
    78  		set.blocks = item
    79  	} else {
    80  		set.blocks.Move(-1).Link(item)
    81  	}
    82  	// Display a log for the user to notify of a new mined block unconfirmed
    83  	log.Info(fmt.Sprintf("🔨  mined potential block #%d [%x…], waiting for %d blocks to confirm", index, hash.Bytes()[:4], set.depth))
    84  }
    85  
    86  // Shift drops all unconfirmed blocks from the set which exceed the unconfirmed sets depth
    87  // allowance, checking them against the canonical chain for inclusion or staleness
    88  // report.
    89  func (set *unconfirmedBlocks) Shift(height uint64) {
    90  	set.lock.Lock()
    91  	defer set.lock.Unlock()
    92  
    93  	for set.blocks != nil {
    94  		// Retrieve the next unconfirmed block and abort if too fresh
    95  		next := set.blocks.Value.(*unconfirmedBlock)
    96  		if next.index+uint64(set.depth) > height {
    97  			break
    98  		}
    99  		// Block seems to exceed depth allowance, check for canonical status
   100  		header := set.chain.GetHeaderByNumber(next.index)
   101  		switch {
   102  		case header == nil:
   103  			log.Warn(fmt.Sprintf("failed to retrieve header of mined block #%d [%x…]", next.index, next.hash.Bytes()[:4]))
   104  		case header.Hash() == next.hash:
   105  			log.Info(fmt.Sprintf("🔗  mined block #%d [%x…] reached canonical chain", next.index, next.hash.Bytes()[:4]))
   106  		default:
   107  			log.Info(fmt.Sprintf("⑂ mined block #%d [%x…] became a side fork", next.index, next.hash.Bytes()[:4]))
   108  		}
   109  		// Drop the block out of the ring
   110  		if set.blocks.Value == set.blocks.Next().Value {
   111  			set.blocks = nil
   112  		} else {
   113  			set.blocks = set.blocks.Move(-1)
   114  			set.blocks.Unlink(1)
   115  			set.blocks = set.blocks.Move(1)
   116  		}
   117  	}
   118  }