github.com/oskarth/go-ethereum@v1.6.8-0.20191013093314-dac24a9d3494/eth/backend.go (about)

     1  // Copyright 2014 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 eth implements the Ethereum protocol.
    18  package eth
    19  
    20  import (
    21  	"errors"
    22  	"fmt"
    23  	"math/big"
    24  	"runtime"
    25  	"sync"
    26  	"sync/atomic"
    27  
    28  	"github.com/ethereum/go-ethereum/accounts"
    29  	"github.com/ethereum/go-ethereum/common"
    30  	"github.com/ethereum/go-ethereum/common/hexutil"
    31  	"github.com/ethereum/go-ethereum/consensus"
    32  	"github.com/ethereum/go-ethereum/consensus/clique"
    33  	"github.com/ethereum/go-ethereum/consensus/ethash"
    34  	"github.com/ethereum/go-ethereum/core"
    35  	"github.com/ethereum/go-ethereum/core/bloombits"
    36  	"github.com/ethereum/go-ethereum/core/rawdb"
    37  	"github.com/ethereum/go-ethereum/core/types"
    38  	"github.com/ethereum/go-ethereum/core/vm"
    39  	"github.com/ethereum/go-ethereum/eth/downloader"
    40  	"github.com/ethereum/go-ethereum/eth/filters"
    41  	"github.com/ethereum/go-ethereum/eth/gasprice"
    42  	"github.com/ethereum/go-ethereum/ethdb"
    43  	"github.com/ethereum/go-ethereum/event"
    44  	"github.com/ethereum/go-ethereum/internal/ethapi"
    45  	"github.com/ethereum/go-ethereum/log"
    46  	"github.com/ethereum/go-ethereum/miner"
    47  	"github.com/ethereum/go-ethereum/node"
    48  	"github.com/ethereum/go-ethereum/p2p"
    49  	"github.com/ethereum/go-ethereum/params"
    50  	"github.com/ethereum/go-ethereum/rlp"
    51  	"github.com/ethereum/go-ethereum/rpc"
    52  )
    53  
    54  type LesServer interface {
    55  	Start(srvr *p2p.Server)
    56  	Stop()
    57  	Protocols() []p2p.Protocol
    58  	SetBloomBitsIndexer(bbIndexer *core.ChainIndexer)
    59  }
    60  
    61  // Ethereum implements the Ethereum full node service.
    62  type Ethereum struct {
    63  	config      *Config
    64  	chainConfig *params.ChainConfig
    65  
    66  	// Channel for shutting down the service
    67  	shutdownChan chan bool // Channel for shutting down the Ethereum
    68  
    69  	// Handlers
    70  	txPool          *core.TxPool
    71  	blockchain      *core.BlockChain
    72  	protocolManager *ProtocolManager
    73  	lesServer       LesServer
    74  
    75  	// DB interfaces
    76  	chainDb ethdb.Database // Block chain database
    77  
    78  	eventMux       *event.TypeMux
    79  	engine         consensus.Engine
    80  	accountManager *accounts.Manager
    81  
    82  	bloomRequests chan chan *bloombits.Retrieval // Channel receiving bloom data retrieval requests
    83  	bloomIndexer  *core.ChainIndexer             // Bloom indexer operating during block imports
    84  
    85  	APIBackend *EthAPIBackend
    86  
    87  	miner     *miner.Miner
    88  	gasPrice  *big.Int
    89  	etherbase common.Address
    90  
    91  	networkID     uint64
    92  	netRPCService *ethapi.PublicNetAPI
    93  
    94  	lock sync.RWMutex // Protects the variadic fields (e.g. gas price and etherbase)
    95  }
    96  
    97  func (s *Ethereum) AddLesServer(ls LesServer) {
    98  	s.lesServer = ls
    99  	ls.SetBloomBitsIndexer(s.bloomIndexer)
   100  }
   101  
   102  // New creates a new Ethereum object (including the
   103  // initialisation of the common Ethereum object)
   104  func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
   105  	// Ensure configuration values are compatible and sane
   106  	if config.SyncMode == downloader.LightSync {
   107  		return nil, errors.New("can't run eth.Ethereum in light sync mode, use les.LightEthereum")
   108  	}
   109  	if !config.SyncMode.IsValid() {
   110  		return nil, fmt.Errorf("invalid sync mode %d", config.SyncMode)
   111  	}
   112  	if config.MinerGasPrice == nil || config.MinerGasPrice.Cmp(common.Big0) <= 0 {
   113  		log.Warn("Sanitizing invalid miner gas price", "provided", config.MinerGasPrice, "updated", DefaultConfig.MinerGasPrice)
   114  		config.MinerGasPrice = new(big.Int).Set(DefaultConfig.MinerGasPrice)
   115  	}
   116  	// Assemble the Ethereum object
   117  	chainDb, err := CreateDB(ctx, config, "chaindata")
   118  	if err != nil {
   119  		return nil, err
   120  	}
   121  	chainConfig, genesisHash, genesisErr := core.SetupGenesisBlock(chainDb, config.Genesis)
   122  	if _, ok := genesisErr.(*params.ConfigCompatError); genesisErr != nil && !ok {
   123  		return nil, genesisErr
   124  	}
   125  	log.Info("Initialised chain configuration", "config", chainConfig)
   126  
   127  	eth := &Ethereum{
   128  		config:         config,
   129  		chainDb:        chainDb,
   130  		chainConfig:    chainConfig,
   131  		eventMux:       ctx.EventMux,
   132  		accountManager: ctx.AccountManager,
   133  		engine:         CreateConsensusEngine(ctx, chainConfig, &config.Ethash, config.MinerNotify, config.MinerNoverify, chainDb),
   134  		shutdownChan:   make(chan bool),
   135  		networkID:      config.NetworkId,
   136  		gasPrice:       config.MinerGasPrice,
   137  		etherbase:      config.Etherbase,
   138  		bloomRequests:  make(chan chan *bloombits.Retrieval),
   139  		bloomIndexer:   NewBloomIndexer(chainDb, params.BloomBitsBlocks, params.BloomConfirms),
   140  	}
   141  
   142  	log.Info("Initialising Ethereum protocol", "versions", ProtocolVersions, "network", config.NetworkId)
   143  
   144  	if !config.SkipBcVersionCheck {
   145  		bcVersion := rawdb.ReadDatabaseVersion(chainDb)
   146  		if bcVersion != core.BlockChainVersion && bcVersion != 0 {
   147  			return nil, fmt.Errorf("Blockchain DB version mismatch (%d / %d).\n", bcVersion, core.BlockChainVersion)
   148  		}
   149  		rawdb.WriteDatabaseVersion(chainDb, core.BlockChainVersion)
   150  	}
   151  	var (
   152  		vmConfig = vm.Config{
   153  			EnablePreimageRecording: config.EnablePreimageRecording,
   154  			EWASMInterpreter:        config.EWASMInterpreter,
   155  			EVMInterpreter:          config.EVMInterpreter,
   156  		}
   157  		cacheConfig = &core.CacheConfig{Disabled: config.NoPruning, TrieNodeLimit: config.TrieCache, TrieTimeLimit: config.TrieTimeout}
   158  	)
   159  	eth.blockchain, err = core.NewBlockChain(chainDb, cacheConfig, eth.chainConfig, eth.engine, vmConfig, eth.shouldPreserve)
   160  	if err != nil {
   161  		return nil, err
   162  	}
   163  	// Rewind the chain in case of an incompatible config upgrade.
   164  	if compat, ok := genesisErr.(*params.ConfigCompatError); ok {
   165  		log.Warn("Rewinding chain to upgrade configuration", "err", compat)
   166  		eth.blockchain.SetHead(compat.RewindTo)
   167  		rawdb.WriteChainConfig(chainDb, genesisHash, chainConfig)
   168  	}
   169  	eth.bloomIndexer.Start(eth.blockchain)
   170  
   171  	if config.TxPool.Journal != "" {
   172  		config.TxPool.Journal = ctx.ResolvePath(config.TxPool.Journal)
   173  	}
   174  	eth.txPool = core.NewTxPool(config.TxPool, eth.chainConfig, eth.blockchain)
   175  
   176  	if eth.protocolManager, err = NewProtocolManager(eth.chainConfig, config.SyncMode, config.NetworkId, eth.eventMux, eth.txPool, eth.engine, eth.blockchain, chainDb); err != nil {
   177  		return nil, err
   178  	}
   179  
   180  	eth.miner = miner.New(eth, eth.chainConfig, eth.EventMux(), eth.engine, config.MinerRecommit, config.MinerGasFloor, config.MinerGasCeil, eth.isLocalBlock)
   181  	eth.miner.SetExtra(makeExtraData(config.MinerExtraData))
   182  
   183  	eth.APIBackend = &EthAPIBackend{eth, nil}
   184  	gpoParams := config.GPO
   185  	if gpoParams.Default == nil {
   186  		gpoParams.Default = config.MinerGasPrice
   187  	}
   188  	eth.APIBackend.gpo = gasprice.NewOracle(eth.APIBackend, gpoParams)
   189  
   190  	return eth, nil
   191  }
   192  
   193  func makeExtraData(extra []byte) []byte {
   194  	if len(extra) == 0 {
   195  		// create default extradata
   196  		extra, _ = rlp.EncodeToBytes([]interface{}{
   197  			uint(params.VersionMajor<<16 | params.VersionMinor<<8 | params.VersionPatch),
   198  			"geth",
   199  			runtime.Version(),
   200  			runtime.GOOS,
   201  		})
   202  	}
   203  	if uint64(len(extra)) > params.MaximumExtraDataSize {
   204  		log.Warn("Miner extra data exceed limit", "extra", hexutil.Bytes(extra), "limit", params.MaximumExtraDataSize)
   205  		extra = nil
   206  	}
   207  	return extra
   208  }
   209  
   210  // CreateDB creates the chain database.
   211  func CreateDB(ctx *node.ServiceContext, config *Config, name string) (ethdb.Database, error) {
   212  	db, err := ctx.OpenDatabase(name, config.DatabaseCache, config.DatabaseHandles)
   213  	if err != nil {
   214  		return nil, err
   215  	}
   216  	if db, ok := db.(*ethdb.LDBDatabase); ok {
   217  		db.Meter("eth/db/chaindata/")
   218  	}
   219  	return db, nil
   220  }
   221  
   222  // CreateConsensusEngine creates the required type of consensus engine instance for an Ethereum service
   223  func CreateConsensusEngine(ctx *node.ServiceContext, chainConfig *params.ChainConfig, config *ethash.Config, notify []string, noverify bool, db ethdb.Database) consensus.Engine {
   224  	// If proof-of-authority is requested, set it up
   225  	if chainConfig.Clique != nil {
   226  		return clique.New(chainConfig.Clique, db)
   227  	}
   228  	// Otherwise assume proof-of-work
   229  	switch config.PowMode {
   230  	case ethash.ModeFake:
   231  		log.Warn("Ethash used in fake mode")
   232  		return ethash.NewFaker()
   233  	case ethash.ModeTest:
   234  		log.Warn("Ethash used in test mode")
   235  		return ethash.NewTester(nil, noverify)
   236  	case ethash.ModeShared:
   237  		log.Warn("Ethash used in shared mode")
   238  		return ethash.NewShared()
   239  	default:
   240  		engine := ethash.New(ethash.Config{
   241  			CacheDir:       ctx.ResolvePath(config.CacheDir),
   242  			CachesInMem:    config.CachesInMem,
   243  			CachesOnDisk:   config.CachesOnDisk,
   244  			DatasetDir:     config.DatasetDir,
   245  			DatasetsInMem:  config.DatasetsInMem,
   246  			DatasetsOnDisk: config.DatasetsOnDisk,
   247  		}, notify, noverify)
   248  		engine.SetThreads(-1) // Disable CPU mining
   249  		return engine
   250  	}
   251  }
   252  
   253  // APIs return the collection of RPC services the ethereum package offers.
   254  // NOTE, some of these services probably need to be moved to somewhere else.
   255  func (s *Ethereum) APIs() []rpc.API {
   256  	apis := ethapi.GetAPIs(s.APIBackend)
   257  
   258  	// Append any APIs exposed explicitly by the consensus engine
   259  	apis = append(apis, s.engine.APIs(s.BlockChain())...)
   260  
   261  	// Append all the local APIs and return
   262  	return append(apis, []rpc.API{
   263  		{
   264  			Namespace: "eth",
   265  			Version:   "1.0",
   266  			Service:   NewPublicEthereumAPI(s),
   267  			Public:    true,
   268  		}, {
   269  			Namespace: "eth",
   270  			Version:   "1.0",
   271  			Service:   NewPublicMinerAPI(s),
   272  			Public:    true,
   273  		}, {
   274  			Namespace: "eth",
   275  			Version:   "1.0",
   276  			Service:   downloader.NewPublicDownloaderAPI(s.protocolManager.downloader, s.eventMux),
   277  			Public:    true,
   278  		}, {
   279  			Namespace: "miner",
   280  			Version:   "1.0",
   281  			Service:   NewPrivateMinerAPI(s),
   282  			Public:    false,
   283  		}, {
   284  			Namespace: "eth",
   285  			Version:   "1.0",
   286  			Service:   filters.NewPublicFilterAPI(s.APIBackend, false),
   287  			Public:    true,
   288  		}, {
   289  			Namespace: "admin",
   290  			Version:   "1.0",
   291  			Service:   NewPrivateAdminAPI(s),
   292  		}, {
   293  			Namespace: "debug",
   294  			Version:   "1.0",
   295  			Service:   NewPublicDebugAPI(s),
   296  			Public:    true,
   297  		}, {
   298  			Namespace: "debug",
   299  			Version:   "1.0",
   300  			Service:   NewPrivateDebugAPI(s.chainConfig, s),
   301  		}, {
   302  			Namespace: "net",
   303  			Version:   "1.0",
   304  			Service:   s.netRPCService,
   305  			Public:    true,
   306  		},
   307  	}...)
   308  }
   309  
   310  func (s *Ethereum) ResetWithGenesisBlock(gb *types.Block) {
   311  	s.blockchain.ResetWithGenesisBlock(gb)
   312  }
   313  
   314  func (s *Ethereum) Etherbase() (eb common.Address, err error) {
   315  	s.lock.RLock()
   316  	etherbase := s.etherbase
   317  	s.lock.RUnlock()
   318  
   319  	if etherbase != (common.Address{}) {
   320  		return etherbase, nil
   321  	}
   322  	if wallets := s.AccountManager().Wallets(); len(wallets) > 0 {
   323  		if accounts := wallets[0].Accounts(); len(accounts) > 0 {
   324  			etherbase := accounts[0].Address
   325  
   326  			s.lock.Lock()
   327  			s.etherbase = etherbase
   328  			s.lock.Unlock()
   329  
   330  			log.Info("Etherbase automatically configured", "address", etherbase)
   331  			return etherbase, nil
   332  		}
   333  	}
   334  	return common.Address{}, fmt.Errorf("etherbase must be explicitly specified")
   335  }
   336  
   337  // isLocalBlock checks whether the specified block is mined
   338  // by local miner accounts.
   339  //
   340  // We regard two types of accounts as local miner account: etherbase
   341  // and accounts specified via `txpool.locals` flag.
   342  func (s *Ethereum) isLocalBlock(block *types.Block) bool {
   343  	author, err := s.engine.Author(block.Header())
   344  	if err != nil {
   345  		log.Warn("Failed to retrieve block author", "number", block.NumberU64(), "hash", block.Hash(), "err", err)
   346  		return false
   347  	}
   348  	// Check whether the given address is etherbase.
   349  	s.lock.RLock()
   350  	etherbase := s.etherbase
   351  	s.lock.RUnlock()
   352  	if author == etherbase {
   353  		return true
   354  	}
   355  	// Check whether the given address is specified by `txpool.local`
   356  	// CLI flag.
   357  	for _, account := range s.config.TxPool.Locals {
   358  		if account == author {
   359  			return true
   360  		}
   361  	}
   362  	return false
   363  }
   364  
   365  // shouldPreserve checks whether we should preserve the given block
   366  // during the chain reorg depending on whether the author of block
   367  // is a local account.
   368  func (s *Ethereum) shouldPreserve(block *types.Block) bool {
   369  	// The reason we need to disable the self-reorg preserving for clique
   370  	// is it can be probable to introduce a deadlock.
   371  	//
   372  	// e.g. If there are 7 available signers
   373  	//
   374  	// r1   A
   375  	// r2     B
   376  	// r3       C
   377  	// r4         D
   378  	// r5   A      [X] F G
   379  	// r6    [X]
   380  	//
   381  	// In the round5, the inturn signer E is offline, so the worst case
   382  	// is A, F and G sign the block of round5 and reject the block of opponents
   383  	// and in the round6, the last available signer B is offline, the whole
   384  	// network is stuck.
   385  	if _, ok := s.engine.(*clique.Clique); ok {
   386  		return false
   387  	}
   388  	return s.isLocalBlock(block)
   389  }
   390  
   391  // SetEtherbase sets the mining reward address.
   392  func (s *Ethereum) SetEtherbase(etherbase common.Address) {
   393  	s.lock.Lock()
   394  	s.etherbase = etherbase
   395  	s.lock.Unlock()
   396  
   397  	s.miner.SetEtherbase(etherbase)
   398  }
   399  
   400  // StartMining starts the miner with the given number of CPU threads. If mining
   401  // is already running, this method adjust the number of threads allowed to use
   402  // and updates the minimum price required by the transaction pool.
   403  func (s *Ethereum) StartMining(threads int) error {
   404  	// Update the thread count within the consensus engine
   405  	type threaded interface {
   406  		SetThreads(threads int)
   407  	}
   408  	if th, ok := s.engine.(threaded); ok {
   409  		log.Info("Updated mining threads", "threads", threads)
   410  		if threads == 0 {
   411  			threads = -1 // Disable the miner from within
   412  		}
   413  		th.SetThreads(threads)
   414  	}
   415  	// If the miner was not running, initialize it
   416  	if !s.IsMining() {
   417  		// Propagate the initial price point to the transaction pool
   418  		s.lock.RLock()
   419  		price := s.gasPrice
   420  		s.lock.RUnlock()
   421  		s.txPool.SetGasPrice(price)
   422  
   423  		// Configure the local mining address
   424  		eb, err := s.Etherbase()
   425  		if err != nil {
   426  			log.Error("Cannot start mining without etherbase", "err", err)
   427  			return fmt.Errorf("etherbase missing: %v", err)
   428  		}
   429  		if clique, ok := s.engine.(*clique.Clique); ok {
   430  			wallet, err := s.accountManager.Find(accounts.Account{Address: eb})
   431  			if wallet == nil || err != nil {
   432  				log.Error("Etherbase account unavailable locally", "err", err)
   433  				return fmt.Errorf("signer missing: %v", err)
   434  			}
   435  			clique.Authorize(eb, wallet.SignHash)
   436  		}
   437  		// If mining is started, we can disable the transaction rejection mechanism
   438  		// introduced to speed sync times.
   439  		atomic.StoreUint32(&s.protocolManager.acceptTxs, 1)
   440  
   441  		go s.miner.Start(eb)
   442  	}
   443  	return nil
   444  }
   445  
   446  // StopMining terminates the miner, both at the consensus engine level as well as
   447  // at the block creation level.
   448  func (s *Ethereum) StopMining() {
   449  	// Update the thread count within the consensus engine
   450  	type threaded interface {
   451  		SetThreads(threads int)
   452  	}
   453  	if th, ok := s.engine.(threaded); ok {
   454  		th.SetThreads(-1)
   455  	}
   456  	// Stop the block creating itself
   457  	s.miner.Stop()
   458  }
   459  
   460  func (s *Ethereum) IsMining() bool      { return s.miner.Mining() }
   461  func (s *Ethereum) Miner() *miner.Miner { return s.miner }
   462  
   463  func (s *Ethereum) AccountManager() *accounts.Manager  { return s.accountManager }
   464  func (s *Ethereum) BlockChain() *core.BlockChain       { return s.blockchain }
   465  func (s *Ethereum) TxPool() *core.TxPool               { return s.txPool }
   466  func (s *Ethereum) EventMux() *event.TypeMux           { return s.eventMux }
   467  func (s *Ethereum) Engine() consensus.Engine           { return s.engine }
   468  func (s *Ethereum) ChainDb() ethdb.Database            { return s.chainDb }
   469  func (s *Ethereum) IsListening() bool                  { return true } // Always listening
   470  func (s *Ethereum) EthVersion() int                    { return int(s.protocolManager.SubProtocols[0].Version) }
   471  func (s *Ethereum) NetVersion() uint64                 { return s.networkID }
   472  func (s *Ethereum) Downloader() *downloader.Downloader { return s.protocolManager.downloader }
   473  
   474  // Protocols implements node.Service, returning all the currently configured
   475  // network protocols to start.
   476  func (s *Ethereum) Protocols() []p2p.Protocol {
   477  	if s.lesServer == nil {
   478  		return s.protocolManager.SubProtocols
   479  	}
   480  	return append(s.protocolManager.SubProtocols, s.lesServer.Protocols()...)
   481  }
   482  
   483  // Start implements node.Service, starting all internal goroutines needed by the
   484  // Ethereum protocol implementation.
   485  func (s *Ethereum) Start(srvr *p2p.Server) error {
   486  	// Start the bloom bits servicing goroutines
   487  	s.startBloomHandlers(params.BloomBitsBlocks)
   488  
   489  	// Start the RPC service
   490  	s.netRPCService = ethapi.NewPublicNetAPI(srvr, s.NetVersion())
   491  
   492  	// Figure out a max peers count based on the server limits
   493  	maxPeers := srvr.MaxPeers
   494  	if s.config.LightServ > 0 {
   495  		if s.config.LightPeers >= srvr.MaxPeers {
   496  			return fmt.Errorf("invalid peer config: light peer count (%d) >= total peer count (%d)", s.config.LightPeers, srvr.MaxPeers)
   497  		}
   498  		maxPeers -= s.config.LightPeers
   499  	}
   500  	// Start the networking layer and the light server if requested
   501  	s.protocolManager.Start(maxPeers)
   502  	if s.lesServer != nil {
   503  		s.lesServer.Start(srvr)
   504  	}
   505  	return nil
   506  }
   507  
   508  // Stop implements node.Service, terminating all internal goroutines used by the
   509  // Ethereum protocol.
   510  func (s *Ethereum) Stop() error {
   511  	s.bloomIndexer.Close()
   512  	s.blockchain.Stop()
   513  	s.engine.Close()
   514  	s.protocolManager.Stop()
   515  	if s.lesServer != nil {
   516  		s.lesServer.Stop()
   517  	}
   518  	s.txPool.Stop()
   519  	s.miner.Stop()
   520  	s.eventMux.Stop()
   521  
   522  	s.chainDb.Close()
   523  	close(s.shutdownChan)
   524  	return nil
   525  }