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