github.com/tirogen/go-ethereum@v1.10.12-0.20221226051715-250cfede41b6/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/tirogen/go-ethereum/accounts"
    29  	"github.com/tirogen/go-ethereum/common"
    30  	"github.com/tirogen/go-ethereum/common/hexutil"
    31  	"github.com/tirogen/go-ethereum/consensus"
    32  	"github.com/tirogen/go-ethereum/consensus/beacon"
    33  	"github.com/tirogen/go-ethereum/consensus/clique"
    34  	"github.com/tirogen/go-ethereum/core"
    35  	"github.com/tirogen/go-ethereum/core/bloombits"
    36  	"github.com/tirogen/go-ethereum/core/rawdb"
    37  	"github.com/tirogen/go-ethereum/core/state/pruner"
    38  	"github.com/tirogen/go-ethereum/core/txpool"
    39  	"github.com/tirogen/go-ethereum/core/types"
    40  	"github.com/tirogen/go-ethereum/core/vm"
    41  	"github.com/tirogen/go-ethereum/eth/downloader"
    42  	"github.com/tirogen/go-ethereum/eth/ethconfig"
    43  	"github.com/tirogen/go-ethereum/eth/gasprice"
    44  	"github.com/tirogen/go-ethereum/eth/protocols/eth"
    45  	"github.com/tirogen/go-ethereum/eth/protocols/snap"
    46  	"github.com/tirogen/go-ethereum/ethdb"
    47  	"github.com/tirogen/go-ethereum/event"
    48  	"github.com/tirogen/go-ethereum/internal/ethapi"
    49  	"github.com/tirogen/go-ethereum/internal/shutdowncheck"
    50  	"github.com/tirogen/go-ethereum/log"
    51  	"github.com/tirogen/go-ethereum/miner"
    52  	"github.com/tirogen/go-ethereum/node"
    53  	"github.com/tirogen/go-ethereum/p2p"
    54  	"github.com/tirogen/go-ethereum/p2p/dnsdisc"
    55  	"github.com/tirogen/go-ethereum/p2p/enode"
    56  	"github.com/tirogen/go-ethereum/params"
    57  	"github.com/tirogen/go-ethereum/rlp"
    58  	"github.com/tirogen/go-ethereum/rpc"
    59  )
    60  
    61  // Config contains the configuration options of the ETH protocol.
    62  // Deprecated: use ethconfig.Config instead.
    63  type Config = ethconfig.Config
    64  
    65  // Ethereum implements the Ethereum full node service.
    66  type Ethereum struct {
    67  	config *ethconfig.Config
    68  
    69  	// Handlers
    70  	txPool             *txpool.TxPool
    71  	blockchain         *core.BlockChain
    72  	handler            *handler
    73  	ethDialCandidates  enode.Iterator
    74  	snapDialCandidates enode.Iterator
    75  	merger             *consensus.Merger
    76  
    77  	// DB interfaces
    78  	chainDb ethdb.Database // Block chain database
    79  
    80  	eventMux       *event.TypeMux
    81  	engine         consensus.Engine
    82  	accountManager *accounts.Manager
    83  
    84  	bloomRequests     chan chan *bloombits.Retrieval // Channel receiving bloom data retrieval requests
    85  	bloomIndexer      *core.ChainIndexer             // Bloom indexer operating during block imports
    86  	closeBloomHandler chan struct{}
    87  
    88  	APIBackend *EthAPIBackend
    89  
    90  	miner     *miner.Miner
    91  	gasPrice  *big.Int
    92  	etherbase common.Address
    93  
    94  	networkID     uint64
    95  	netRPCService *ethapi.NetAPI
    96  
    97  	p2pServer *p2p.Server
    98  
    99  	lock sync.RWMutex // Protects the variadic fields (e.g. gas price and etherbase)
   100  
   101  	shutdownTracker *shutdowncheck.ShutdownTracker // Tracks if and when the node has shutdown ungracefully
   102  }
   103  
   104  // New creates a new Ethereum object (including the
   105  // initialisation of the common Ethereum object)
   106  func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
   107  	// Ensure configuration values are compatible and sane
   108  	if config.SyncMode == downloader.LightSync {
   109  		return nil, errors.New("can't run eth.Ethereum in light sync mode, use les.LightEthereum")
   110  	}
   111  	if !config.SyncMode.IsValid() {
   112  		return nil, fmt.Errorf("invalid sync mode %d", config.SyncMode)
   113  	}
   114  	if config.Miner.GasPrice == nil || config.Miner.GasPrice.Cmp(common.Big0) <= 0 {
   115  		log.Warn("Sanitizing invalid miner gas price", "provided", config.Miner.GasPrice, "updated", ethconfig.Defaults.Miner.GasPrice)
   116  		config.Miner.GasPrice = new(big.Int).Set(ethconfig.Defaults.Miner.GasPrice)
   117  	}
   118  	if config.NoPruning && config.TrieDirtyCache > 0 {
   119  		if config.SnapshotCache > 0 {
   120  			config.TrieCleanCache += config.TrieDirtyCache * 3 / 5
   121  			config.SnapshotCache += config.TrieDirtyCache * 2 / 5
   122  		} else {
   123  			config.TrieCleanCache += config.TrieDirtyCache
   124  		}
   125  		config.TrieDirtyCache = 0
   126  	}
   127  	log.Info("Allocated trie memory caches", "clean", common.StorageSize(config.TrieCleanCache)*1024*1024, "dirty", common.StorageSize(config.TrieDirtyCache)*1024*1024)
   128  
   129  	// Assemble the Ethereum object
   130  	chainDb, err := stack.OpenDatabaseWithFreezer("chaindata", config.DatabaseCache, config.DatabaseHandles, config.DatabaseFreezer, "eth/db/chaindata/", false)
   131  	if err != nil {
   132  		return nil, err
   133  	}
   134  	if err := pruner.RecoverPruning(stack.ResolvePath(""), chainDb, stack.ResolvePath(config.TrieCleanCacheJournal)); err != nil {
   135  		log.Error("Failed to recover state", "error", err)
   136  	}
   137  	// Transfer mining-related config to the ethash config.
   138  	ethashConfig := config.Ethash
   139  	ethashConfig.NotifyFull = config.Miner.NotifyFull
   140  	cliqueConfig, err := core.LoadCliqueConfig(chainDb, config.Genesis)
   141  	if err != nil {
   142  		return nil, err
   143  	}
   144  	engine := ethconfig.CreateConsensusEngine(stack, &ethashConfig, cliqueConfig, config.Miner.Notify, config.Miner.Noverify, chainDb)
   145  
   146  	eth := &Ethereum{
   147  		config:            config,
   148  		merger:            consensus.NewMerger(chainDb),
   149  		chainDb:           chainDb,
   150  		eventMux:          stack.EventMux(),
   151  		accountManager:    stack.AccountManager(),
   152  		engine:            engine,
   153  		closeBloomHandler: make(chan struct{}),
   154  		networkID:         config.NetworkId,
   155  		gasPrice:          config.Miner.GasPrice,
   156  		etherbase:         config.Miner.Etherbase,
   157  		bloomRequests:     make(chan chan *bloombits.Retrieval),
   158  		bloomIndexer:      core.NewBloomIndexer(chainDb, params.BloomBitsBlocks, params.BloomConfirms),
   159  		p2pServer:         stack.Server(),
   160  		shutdownTracker:   shutdowncheck.NewShutdownTracker(chainDb),
   161  	}
   162  
   163  	bcVersion := rawdb.ReadDatabaseVersion(chainDb)
   164  	var dbVer = "<nil>"
   165  	if bcVersion != nil {
   166  		dbVer = fmt.Sprintf("%d", *bcVersion)
   167  	}
   168  	log.Info("Initialising Ethereum protocol", "network", config.NetworkId, "dbversion", dbVer)
   169  
   170  	if !config.SkipBcVersionCheck {
   171  		if bcVersion != nil && *bcVersion > core.BlockChainVersion {
   172  			return nil, fmt.Errorf("database version is v%d, Geth %s only supports v%d", *bcVersion, params.VersionWithMeta, core.BlockChainVersion)
   173  		} else if bcVersion == nil || *bcVersion < core.BlockChainVersion {
   174  			if bcVersion != nil { // only print warning on upgrade, not on init
   175  				log.Warn("Upgrade blockchain database version", "from", dbVer, "to", core.BlockChainVersion)
   176  			}
   177  			rawdb.WriteDatabaseVersion(chainDb, core.BlockChainVersion)
   178  		}
   179  	}
   180  	var (
   181  		vmConfig = vm.Config{
   182  			EnablePreimageRecording: config.EnablePreimageRecording,
   183  		}
   184  		cacheConfig = &core.CacheConfig{
   185  			TrieCleanLimit:      config.TrieCleanCache,
   186  			TrieCleanJournal:    stack.ResolvePath(config.TrieCleanCacheJournal),
   187  			TrieCleanRejournal:  config.TrieCleanCacheRejournal,
   188  			TrieCleanNoPrefetch: config.NoPrefetch,
   189  			TrieDirtyLimit:      config.TrieDirtyCache,
   190  			TrieDirtyDisabled:   config.NoPruning,
   191  			TrieTimeLimit:       config.TrieTimeout,
   192  			SnapshotLimit:       config.SnapshotCache,
   193  			Preimages:           config.Preimages,
   194  		}
   195  	)
   196  	// Override the chain config with provided settings.
   197  	var overrides core.ChainOverrides
   198  	if config.OverrideTerminalTotalDifficulty != nil {
   199  		overrides.OverrideTerminalTotalDifficulty = config.OverrideTerminalTotalDifficulty
   200  	}
   201  	if config.OverrideTerminalTotalDifficultyPassed != nil {
   202  		overrides.OverrideTerminalTotalDifficultyPassed = config.OverrideTerminalTotalDifficultyPassed
   203  	}
   204  	eth.blockchain, err = core.NewBlockChain(chainDb, cacheConfig, config.Genesis, &overrides, eth.engine, vmConfig, eth.shouldPreserve, &config.TxLookupLimit)
   205  	if err != nil {
   206  		return nil, err
   207  	}
   208  	eth.bloomIndexer.Start(eth.blockchain)
   209  
   210  	if config.TxPool.Journal != "" {
   211  		config.TxPool.Journal = stack.ResolvePath(config.TxPool.Journal)
   212  	}
   213  	eth.txPool = txpool.NewTxPool(config.TxPool, eth.blockchain.Config(), eth.blockchain)
   214  
   215  	// Permit the downloader to use the trie cache allowance during fast sync
   216  	cacheLimit := cacheConfig.TrieCleanLimit + cacheConfig.TrieDirtyLimit + cacheConfig.SnapshotLimit
   217  	checkpoint := config.Checkpoint
   218  	if checkpoint == nil {
   219  		checkpoint = params.TrustedCheckpoints[eth.blockchain.Genesis().Hash()]
   220  	}
   221  	if eth.handler, err = newHandler(&handlerConfig{
   222  		Database:       chainDb,
   223  		Chain:          eth.blockchain,
   224  		TxPool:         eth.txPool,
   225  		Merger:         eth.merger,
   226  		Network:        config.NetworkId,
   227  		Sync:           config.SyncMode,
   228  		BloomCache:     uint64(cacheLimit),
   229  		EventMux:       eth.eventMux,
   230  		Checkpoint:     checkpoint,
   231  		RequiredBlocks: config.RequiredBlocks,
   232  	}); err != nil {
   233  		return nil, err
   234  	}
   235  
   236  	eth.miner = miner.New(eth, &config.Miner, eth.blockchain.Config(), eth.EventMux(), eth.engine, eth.isLocalBlock)
   237  	eth.miner.SetExtra(makeExtraData(config.Miner.ExtraData))
   238  
   239  	eth.APIBackend = &EthAPIBackend{stack.Config().ExtRPCEnabled(), stack.Config().AllowUnprotectedTxs, eth, nil}
   240  	if eth.APIBackend.allowUnprotectedTxs {
   241  		log.Info("Unprotected transactions allowed")
   242  	}
   243  	gpoParams := config.GPO
   244  	if gpoParams.Default == nil {
   245  		gpoParams.Default = config.Miner.GasPrice
   246  	}
   247  	eth.APIBackend.gpo = gasprice.NewOracle(eth.APIBackend, gpoParams)
   248  
   249  	// Setup DNS discovery iterators.
   250  	dnsclient := dnsdisc.NewClient(dnsdisc.Config{})
   251  	eth.ethDialCandidates, err = dnsclient.NewIterator(eth.config.EthDiscoveryURLs...)
   252  	if err != nil {
   253  		return nil, err
   254  	}
   255  	eth.snapDialCandidates, err = dnsclient.NewIterator(eth.config.SnapDiscoveryURLs...)
   256  	if err != nil {
   257  		return nil, err
   258  	}
   259  
   260  	// Start the RPC service
   261  	eth.netRPCService = ethapi.NewNetAPI(eth.p2pServer, config.NetworkId)
   262  
   263  	// Register the backend on the node
   264  	stack.RegisterAPIs(eth.APIs())
   265  	stack.RegisterProtocols(eth.Protocols())
   266  	stack.RegisterLifecycle(eth)
   267  
   268  	// Successful startup; push a marker and check previous unclean shutdowns.
   269  	eth.shutdownTracker.MarkStartup()
   270  
   271  	return eth, nil
   272  }
   273  
   274  func makeExtraData(extra []byte) []byte {
   275  	if len(extra) == 0 {
   276  		// create default extradata
   277  		extra, _ = rlp.EncodeToBytes([]interface{}{
   278  			uint(params.VersionMajor<<16 | params.VersionMinor<<8 | params.VersionPatch),
   279  			"geth",
   280  			runtime.Version(),
   281  			runtime.GOOS,
   282  		})
   283  	}
   284  	if uint64(len(extra)) > params.MaximumExtraDataSize {
   285  		log.Warn("Miner extra data exceed limit", "extra", hexutil.Bytes(extra), "limit", params.MaximumExtraDataSize)
   286  		extra = nil
   287  	}
   288  	return extra
   289  }
   290  
   291  // APIs return the collection of RPC services the ethereum package offers.
   292  // NOTE, some of these services probably need to be moved to somewhere else.
   293  func (s *Ethereum) APIs() []rpc.API {
   294  	apis := ethapi.GetAPIs(s.APIBackend)
   295  
   296  	// Append any APIs exposed explicitly by the consensus engine
   297  	apis = append(apis, s.engine.APIs(s.BlockChain())...)
   298  
   299  	// Append all the local APIs and return
   300  	return append(apis, []rpc.API{
   301  		{
   302  			Namespace: "eth",
   303  			Service:   NewEthereumAPI(s),
   304  		}, {
   305  			Namespace: "miner",
   306  			Service:   NewMinerAPI(s),
   307  		}, {
   308  			Namespace: "eth",
   309  			Service:   downloader.NewDownloaderAPI(s.handler.downloader, s.eventMux),
   310  		}, {
   311  			Namespace: "admin",
   312  			Service:   NewAdminAPI(s),
   313  		}, {
   314  			Namespace: "debug",
   315  			Service:   NewDebugAPI(s),
   316  		}, {
   317  			Namespace: "net",
   318  			Service:   s.netRPCService,
   319  		},
   320  	}...)
   321  }
   322  
   323  func (s *Ethereum) ResetWithGenesisBlock(gb *types.Block) {
   324  	s.blockchain.ResetWithGenesisBlock(gb)
   325  }
   326  
   327  func (s *Ethereum) Etherbase() (eb common.Address, err error) {
   328  	s.lock.RLock()
   329  	etherbase := s.etherbase
   330  	s.lock.RUnlock()
   331  
   332  	if etherbase != (common.Address{}) {
   333  		return etherbase, nil
   334  	}
   335  	if wallets := s.AccountManager().Wallets(); len(wallets) > 0 {
   336  		if accounts := wallets[0].Accounts(); len(accounts) > 0 {
   337  			etherbase := accounts[0].Address
   338  
   339  			s.lock.Lock()
   340  			s.etherbase = etherbase
   341  			s.lock.Unlock()
   342  
   343  			log.Info("Etherbase automatically configured", "address", etherbase)
   344  			return etherbase, nil
   345  		}
   346  	}
   347  	return common.Address{}, fmt.Errorf("etherbase must be explicitly specified")
   348  }
   349  
   350  // isLocalBlock checks whether the specified block is mined
   351  // by local miner accounts.
   352  //
   353  // We regard two types of accounts as local miner account: etherbase
   354  // and accounts specified via `txpool.locals` flag.
   355  func (s *Ethereum) isLocalBlock(header *types.Header) bool {
   356  	author, err := s.engine.Author(header)
   357  	if err != nil {
   358  		log.Warn("Failed to retrieve block author", "number", header.Number.Uint64(), "hash", header.Hash(), "err", err)
   359  		return false
   360  	}
   361  	// Check whether the given address is etherbase.
   362  	s.lock.RLock()
   363  	etherbase := s.etherbase
   364  	s.lock.RUnlock()
   365  	if author == etherbase {
   366  		return true
   367  	}
   368  	// Check whether the given address is specified by `txpool.local`
   369  	// CLI flag.
   370  	for _, account := range s.config.TxPool.Locals {
   371  		if account == author {
   372  			return true
   373  		}
   374  	}
   375  	return false
   376  }
   377  
   378  // shouldPreserve checks whether we should preserve the given block
   379  // during the chain reorg depending on whether the author of block
   380  // is a local account.
   381  func (s *Ethereum) shouldPreserve(header *types.Header) bool {
   382  	// The reason we need to disable the self-reorg preserving for clique
   383  	// is it can be probable to introduce a deadlock.
   384  	//
   385  	// e.g. If there are 7 available signers
   386  	//
   387  	// r1   A
   388  	// r2     B
   389  	// r3       C
   390  	// r4         D
   391  	// r5   A      [X] F G
   392  	// r6    [X]
   393  	//
   394  	// In the round5, the inturn signer E is offline, so the worst case
   395  	// is A, F and G sign the block of round5 and reject the block of opponents
   396  	// and in the round6, the last available signer B is offline, the whole
   397  	// network is stuck.
   398  	if _, ok := s.engine.(*clique.Clique); ok {
   399  		return false
   400  	}
   401  	return s.isLocalBlock(header)
   402  }
   403  
   404  // SetEtherbase sets the mining reward address.
   405  func (s *Ethereum) SetEtherbase(etherbase common.Address) {
   406  	s.lock.Lock()
   407  	s.etherbase = etherbase
   408  	s.lock.Unlock()
   409  
   410  	s.miner.SetEtherbase(etherbase)
   411  }
   412  
   413  // StartMining starts the miner with the given number of CPU threads. If mining
   414  // is already running, this method adjust the number of threads allowed to use
   415  // and updates the minimum price required by the transaction pool.
   416  func (s *Ethereum) StartMining(threads int) error {
   417  	// Update the thread count within the consensus engine
   418  	type threaded interface {
   419  		SetThreads(threads int)
   420  	}
   421  	if th, ok := s.engine.(threaded); ok {
   422  		log.Info("Updated mining threads", "threads", threads)
   423  		if threads == 0 {
   424  			threads = -1 // Disable the miner from within
   425  		}
   426  		th.SetThreads(threads)
   427  	}
   428  	// If the miner was not running, initialize it
   429  	if !s.IsMining() {
   430  		// Propagate the initial price point to the transaction pool
   431  		s.lock.RLock()
   432  		price := s.gasPrice
   433  		s.lock.RUnlock()
   434  		s.txPool.SetGasPrice(price)
   435  
   436  		// Configure the local mining address
   437  		eb, err := s.Etherbase()
   438  		if err != nil {
   439  			log.Error("Cannot start mining without etherbase", "err", err)
   440  			return fmt.Errorf("etherbase missing: %v", err)
   441  		}
   442  		var cli *clique.Clique
   443  		if c, ok := s.engine.(*clique.Clique); ok {
   444  			cli = c
   445  		} else if cl, ok := s.engine.(*beacon.Beacon); ok {
   446  			if c, ok := cl.InnerEngine().(*clique.Clique); ok {
   447  				cli = c
   448  			}
   449  		}
   450  		if cli != nil {
   451  			wallet, err := s.accountManager.Find(accounts.Account{Address: eb})
   452  			if wallet == nil || err != nil {
   453  				log.Error("Etherbase account unavailable locally", "err", err)
   454  				return fmt.Errorf("signer missing: %v", err)
   455  			}
   456  			cli.Authorize(eb, wallet.SignData)
   457  		}
   458  		// If mining is started, we can disable the transaction rejection mechanism
   459  		// introduced to speed sync times.
   460  		atomic.StoreUint32(&s.handler.acceptTxs, 1)
   461  
   462  		go s.miner.Start(eb)
   463  	}
   464  	return nil
   465  }
   466  
   467  // StopMining terminates the miner, both at the consensus engine level as well as
   468  // at the block creation level.
   469  func (s *Ethereum) StopMining() {
   470  	// Update the thread count within the consensus engine
   471  	type threaded interface {
   472  		SetThreads(threads int)
   473  	}
   474  	if th, ok := s.engine.(threaded); ok {
   475  		th.SetThreads(-1)
   476  	}
   477  	// Stop the block creating itself
   478  	s.miner.Stop()
   479  }
   480  
   481  func (s *Ethereum) IsMining() bool      { return s.miner.Mining() }
   482  func (s *Ethereum) Miner() *miner.Miner { return s.miner }
   483  
   484  func (s *Ethereum) AccountManager() *accounts.Manager  { return s.accountManager }
   485  func (s *Ethereum) BlockChain() *core.BlockChain       { return s.blockchain }
   486  func (s *Ethereum) TxPool() *txpool.TxPool             { return s.txPool }
   487  func (s *Ethereum) EventMux() *event.TypeMux           { return s.eventMux }
   488  func (s *Ethereum) Engine() consensus.Engine           { return s.engine }
   489  func (s *Ethereum) ChainDb() ethdb.Database            { return s.chainDb }
   490  func (s *Ethereum) IsListening() bool                  { return true } // Always listening
   491  func (s *Ethereum) Downloader() *downloader.Downloader { return s.handler.downloader }
   492  func (s *Ethereum) Synced() bool                       { return atomic.LoadUint32(&s.handler.acceptTxs) == 1 }
   493  func (s *Ethereum) SetSynced()                         { atomic.StoreUint32(&s.handler.acceptTxs, 1) }
   494  func (s *Ethereum) ArchiveMode() bool                  { return s.config.NoPruning }
   495  func (s *Ethereum) BloomIndexer() *core.ChainIndexer   { return s.bloomIndexer }
   496  func (s *Ethereum) Merger() *consensus.Merger          { return s.merger }
   497  func (s *Ethereum) SyncMode() downloader.SyncMode {
   498  	mode, _ := s.handler.chainSync.modeAndLocalHead()
   499  	return mode
   500  }
   501  
   502  // Protocols returns all the currently configured
   503  // network protocols to start.
   504  func (s *Ethereum) Protocols() []p2p.Protocol {
   505  	protos := eth.MakeProtocols((*ethHandler)(s.handler), s.networkID, s.ethDialCandidates)
   506  	if s.config.SnapshotCache > 0 {
   507  		protos = append(protos, snap.MakeProtocols((*snapHandler)(s.handler), s.snapDialCandidates)...)
   508  	}
   509  	return protos
   510  }
   511  
   512  // Start implements node.Lifecycle, starting all internal goroutines needed by the
   513  // Ethereum protocol implementation.
   514  func (s *Ethereum) Start() error {
   515  	eth.StartENRUpdater(s.blockchain, s.p2pServer.LocalNode())
   516  
   517  	// Start the bloom bits servicing goroutines
   518  	s.startBloomHandlers(params.BloomBitsBlocks)
   519  
   520  	// Regularly update shutdown marker
   521  	s.shutdownTracker.Start()
   522  
   523  	// Figure out a max peers count based on the server limits
   524  	maxPeers := s.p2pServer.MaxPeers
   525  	if s.config.LightServ > 0 {
   526  		if s.config.LightPeers >= s.p2pServer.MaxPeers {
   527  			return fmt.Errorf("invalid peer config: light peer count (%d) >= total peer count (%d)", s.config.LightPeers, s.p2pServer.MaxPeers)
   528  		}
   529  		maxPeers -= s.config.LightPeers
   530  	}
   531  	// Start the networking layer and the light server if requested
   532  	s.handler.Start(maxPeers)
   533  	return nil
   534  }
   535  
   536  // Stop implements node.Lifecycle, terminating all internal goroutines used by the
   537  // Ethereum protocol.
   538  func (s *Ethereum) Stop() error {
   539  	// Stop all the peer-related stuff first.
   540  	s.ethDialCandidates.Close()
   541  	s.snapDialCandidates.Close()
   542  	s.handler.Stop()
   543  
   544  	// Then stop everything else.
   545  	s.bloomIndexer.Close()
   546  	close(s.closeBloomHandler)
   547  	s.txPool.Stop()
   548  	s.miner.Close()
   549  	s.blockchain.Stop()
   550  	s.engine.Close()
   551  
   552  	// Clean shutdown marker as the last thing before closing db
   553  	s.shutdownTracker.Stop()
   554  
   555  	s.chainDb.Close()
   556  	s.eventMux.Stop()
   557  
   558  	return nil
   559  }