github.com/daeglee/go-ethereum@v0.0.0-20190504220456-cad3e8d18e9b/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  	APIs() []rpc.API
    58  	Protocols() []p2p.Protocol
    59  	SetBloomBitsIndexer(bbIndexer *core.ChainIndexer)
    60  }
    61  
    62  // Ethereum implements the Ethereum full node service.
    63  type Ethereum struct {
    64  	config *Config
    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  	if config.NoPruning && config.TrieDirtyCache > 0 {
   117  		config.TrieCleanCache += config.TrieDirtyCache
   118  		config.TrieDirtyCache = 0
   119  	}
   120  	log.Info("Allocated trie memory caches", "clean", common.StorageSize(config.TrieCleanCache)*1024*1024, "dirty", common.StorageSize(config.TrieDirtyCache)*1024*1024)
   121  
   122  	// Assemble the Ethereum object
   123  	chainDb, err := ctx.OpenDatabase("chaindata", config.DatabaseCache, config.DatabaseHandles, "eth/db/chaindata/")
   124  	if err != nil {
   125  		return nil, err
   126  	}
   127  	chainConfig, genesisHash, genesisErr := core.SetupGenesisBlockWithOverride(chainDb, config.Genesis, config.ConstantinopleOverride)
   128  	if _, ok := genesisErr.(*params.ConfigCompatError); genesisErr != nil && !ok {
   129  		return nil, genesisErr
   130  	}
   131  	log.Info("Initialised chain configuration", "config", chainConfig)
   132  
   133  	eth := &Ethereum{
   134  		config:         config,
   135  		chainDb:        chainDb,
   136  		eventMux:       ctx.EventMux,
   137  		accountManager: ctx.AccountManager,
   138  		engine:         CreateConsensusEngine(ctx, chainConfig, &config.Ethash, config.MinerNotify, config.MinerNoverify, chainDb),
   139  		shutdownChan:   make(chan bool),
   140  		networkID:      config.NetworkId,
   141  		gasPrice:       config.MinerGasPrice,
   142  		etherbase:      config.Etherbase,
   143  		bloomRequests:  make(chan chan *bloombits.Retrieval),
   144  		bloomIndexer:   NewBloomIndexer(chainDb, params.BloomBitsBlocks, params.BloomConfirms),
   145  	}
   146  
   147  
   148  	bcVersion := rawdb.ReadDatabaseVersion(chainDb)
   149  	var dbVer = "<nil>"
   150  	if bcVersion != nil {
   151  		dbVer = fmt.Sprintf("%d", *bcVersion)
   152  	}
   153  	log.Info("Initialising Ethereum protocol", "versions", ProtocolVersions, "network", config.NetworkId, "dbversion", dbVer)
   154  
   155  	if !config.SkipBcVersionCheck {
   156  		if bcVersion != nil && *bcVersion > core.BlockChainVersion {
   157  			return nil, fmt.Errorf("database version is v%d, Geth %s only supports v%d", *bcVersion, params.VersionWithMeta, core.BlockChainVersion)
   158  		} else if bcVersion == nil || *bcVersion < core.BlockChainVersion {
   159  			log.Warn("Upgrade blockchain database version", "from", dbVer, "to", core.BlockChainVersion)
   160  			rawdb.WriteDatabaseVersion(chainDb, core.BlockChainVersion)
   161  		}
   162  	}
   163  	var (
   164  		vmConfig = vm.Config{
   165  			EnablePreimageRecording: config.EnablePreimageRecording,
   166  			EWASMInterpreter:        config.EWASMInterpreter,
   167  			EVMInterpreter:          config.EVMInterpreter,
   168  		}
   169  		cacheConfig = &core.CacheConfig{Disabled: config.NoPruning, TrieCleanLimit: config.TrieCleanCache, TrieDirtyLimit: config.TrieDirtyCache, TrieTimeLimit: config.TrieTimeout}
   170  	)
   171  	eth.blockchain, err = core.NewBlockChain(chainDb, cacheConfig, chainConfig, eth.engine, vmConfig, eth.shouldPreserve)
   172  	if err != nil {
   173  		return nil, err
   174  	}
   175  	// Rewind the chain in case of an incompatible config upgrade.
   176  	if compat, ok := genesisErr.(*params.ConfigCompatError); ok {
   177  		log.Warn("Rewinding chain to upgrade configuration", "err", compat)
   178  		eth.blockchain.SetHead(compat.RewindTo)
   179  		rawdb.WriteChainConfig(chainDb, genesisHash, chainConfig)
   180  	}
   181  	eth.bloomIndexer.Start(eth.blockchain)
   182  
   183  	if config.TxPool.Journal != "" {
   184  		config.TxPool.Journal = ctx.ResolvePath(config.TxPool.Journal)
   185  	}
   186  	eth.txPool = core.NewTxPool(config.TxPool, chainConfig, eth.blockchain)
   187  
   188  	if eth.protocolManager, err = NewProtocolManager(chainConfig, config.SyncMode, config.NetworkId, eth.eventMux, eth.txPool, eth.engine, eth.blockchain, chainDb, config.Whitelist); err != nil {
   189  		return nil, err
   190  	}
   191  
   192  	eth.miner = miner.New(eth, chainConfig, eth.EventMux(), eth.engine, config.MinerRecommit, config.MinerGasFloor, config.MinerGasCeil, eth.isLocalBlock)
   193  	eth.miner.SetExtra(makeExtraData(config.MinerExtraData))
   194  
   195  	eth.APIBackend = &EthAPIBackend{eth, nil}
   196  	gpoParams := config.GPO
   197  	if gpoParams.Default == nil {
   198  		gpoParams.Default = config.MinerGasPrice
   199  	}
   200  	eth.APIBackend.gpo = gasprice.NewOracle(eth.APIBackend, gpoParams)
   201  
   202  	return eth, nil
   203  }
   204  
   205  func makeExtraData(extra []byte) []byte {
   206  	if len(extra) == 0 {
   207  		// create default extradata
   208  		extra, _ = rlp.EncodeToBytes([]interface{}{
   209  			uint(params.VersionMajor<<16 | params.VersionMinor<<8 | params.VersionPatch),
   210  			"geth",
   211  			runtime.Version(),
   212  			runtime.GOOS,
   213  		})
   214  	}
   215  	if uint64(len(extra)) > params.MaximumExtraDataSize {
   216  		log.Warn("Miner extra data exceed limit", "extra", hexutil.Bytes(extra), "limit", params.MaximumExtraDataSize)
   217  		extra = nil
   218  	}
   219  	return extra
   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 les server
   259  	if s.lesServer != nil {
   260  		apis = append(apis, s.lesServer.APIs()...)
   261  	}
   262  	// Append any APIs exposed explicitly by the consensus engine
   263  	apis = append(apis, s.engine.APIs(s.BlockChain())...)
   264  
   265  	// Append all the local APIs and return
   266  	return append(apis, []rpc.API{
   267  		{
   268  			Namespace: "eth",
   269  			Version:   "1.0",
   270  			Service:   NewPublicEthereumAPI(s),
   271  			Public:    true,
   272  		}, {
   273  			Namespace: "eth",
   274  			Version:   "1.0",
   275  			Service:   NewPublicMinerAPI(s),
   276  			Public:    true,
   277  		}, {
   278  			Namespace: "eth",
   279  			Version:   "1.0",
   280  			Service:   downloader.NewPublicDownloaderAPI(s.protocolManager.downloader, s.eventMux),
   281  			Public:    true,
   282  		}, {
   283  			Namespace: "miner",
   284  			Version:   "1.0",
   285  			Service:   NewPrivateMinerAPI(s),
   286  			Public:    false,
   287  		}, {
   288  			Namespace: "eth",
   289  			Version:   "1.0",
   290  			Service:   filters.NewPublicFilterAPI(s.APIBackend, false),
   291  			Public:    true,
   292  		}, {
   293  			Namespace: "admin",
   294  			Version:   "1.0",
   295  			Service:   NewPrivateAdminAPI(s),
   296  		}, {
   297  			Namespace: "debug",
   298  			Version:   "1.0",
   299  			Service:   NewPublicDebugAPI(s),
   300  			Public:    true,
   301  		}, {
   302  			Namespace: "debug",
   303  			Version:   "1.0",
   304  			Service:   NewPrivateDebugAPI(s),
   305  		}, {
   306  			Namespace: "net",
   307  			Version:   "1.0",
   308  			Service:   s.netRPCService,
   309  			Public:    true,
   310  		},
   311  	}...)
   312  }
   313  
   314  func (s *Ethereum) ResetWithGenesisBlock(gb *types.Block) {
   315  	s.blockchain.ResetWithGenesisBlock(gb)
   316  }
   317  
   318  func (s *Ethereum) Etherbase() (eb common.Address, err error) {
   319  	s.lock.RLock()
   320  	etherbase := s.etherbase
   321  	s.lock.RUnlock()
   322  
   323  	if etherbase != (common.Address{}) {
   324  		return etherbase, nil
   325  	}
   326  	if wallets := s.AccountManager().Wallets(); len(wallets) > 0 {
   327  		if accounts := wallets[0].Accounts(); len(accounts) > 0 {
   328  			etherbase := accounts[0].Address
   329  
   330  			s.lock.Lock()
   331  			s.etherbase = etherbase
   332  			s.lock.Unlock()
   333  
   334  			log.Info("Etherbase automatically configured", "address", etherbase)
   335  			return etherbase, nil
   336  		}
   337  	}
   338  	return common.Address{}, fmt.Errorf("etherbase must be explicitly specified")
   339  }
   340  
   341  // isLocalBlock checks whether the specified block is mined
   342  // by local miner accounts.
   343  //
   344  // We regard two types of accounts as local miner account: etherbase
   345  // and accounts specified via `txpool.locals` flag.
   346  func (s *Ethereum) isLocalBlock(block *types.Block) bool {
   347  	author, err := s.engine.Author(block.Header())
   348  	if err != nil {
   349  		log.Warn("Failed to retrieve block author", "number", block.NumberU64(), "hash", block.Hash(), "err", err)
   350  		return false
   351  	}
   352  	// Check whether the given address is etherbase.
   353  	s.lock.RLock()
   354  	etherbase := s.etherbase
   355  	s.lock.RUnlock()
   356  	if author == etherbase {
   357  		return true
   358  	}
   359  	// Check whether the given address is specified by `txpool.local`
   360  	// CLI flag.
   361  	for _, account := range s.config.TxPool.Locals {
   362  		if account == author {
   363  			return true
   364  		}
   365  	}
   366  	return false
   367  }
   368  
   369  // shouldPreserve checks whether we should preserve the given block
   370  // during the chain reorg depending on whether the author of block
   371  // is a local account.
   372  func (s *Ethereum) shouldPreserve(block *types.Block) bool {
   373  	// The reason we need to disable the self-reorg preserving for clique
   374  	// is it can be probable to introduce a deadlock.
   375  	//
   376  	// e.g. If there are 7 available signers
   377  	//
   378  	// r1   A
   379  	// r2     B
   380  	// r3       C
   381  	// r4         D
   382  	// r5   A      [X] F G
   383  	// r6    [X]
   384  	//
   385  	// In the round5, the inturn signer E is offline, so the worst case
   386  	// is A, F and G sign the block of round5 and reject the block of opponents
   387  	// and in the round6, the last available signer B is offline, the whole
   388  	// network is stuck.
   389  	if _, ok := s.engine.(*clique.Clique); ok {
   390  		return false
   391  	}
   392  	return s.isLocalBlock(block)
   393  }
   394  
   395  // SetEtherbase sets the mining reward address.
   396  func (s *Ethereum) SetEtherbase(etherbase common.Address) {
   397  	s.lock.Lock()
   398  	s.etherbase = etherbase
   399  	s.lock.Unlock()
   400  
   401  	s.miner.SetEtherbase(etherbase)
   402  }
   403  
   404  // StartMining starts the miner with the given number of CPU threads. If mining
   405  // is already running, this method adjust the number of threads allowed to use
   406  // and updates the minimum price required by the transaction pool.
   407  func (s *Ethereum) StartMining(threads int) error {
   408  	// Update the thread count within the consensus engine
   409  	type threaded interface {
   410  		SetThreads(threads int)
   411  	}
   412  	if th, ok := s.engine.(threaded); ok {
   413  		log.Info("Updated mining threads", "threads", threads)
   414  		if threads == 0 {
   415  			threads = -1 // Disable the miner from within
   416  		}
   417  		th.SetThreads(threads)
   418  	}
   419  	// If the miner was not running, initialize it
   420  	if !s.IsMining() {
   421  		// Propagate the initial price point to the transaction pool
   422  		s.lock.RLock()
   423  		price := s.gasPrice
   424  		s.lock.RUnlock()
   425  		s.txPool.SetGasPrice(price)
   426  
   427  		// Configure the local mining address
   428  		eb, err := s.Etherbase()
   429  		if err != nil {
   430  			log.Error("Cannot start mining without etherbase", "err", err)
   431  			return fmt.Errorf("etherbase missing: %v", err)
   432  		}
   433  		if clique, ok := s.engine.(*clique.Clique); ok {
   434  			wallet, err := s.accountManager.Find(accounts.Account{Address: eb})
   435  			if wallet == nil || err != nil {
   436  				log.Error("Etherbase account unavailable locally", "err", err)
   437  				return fmt.Errorf("signer missing: %v", err)
   438  			}
   439  			clique.Authorize(eb, wallet.SignData)
   440  		}
   441  		// If mining is started, we can disable the transaction rejection mechanism
   442  		// introduced to speed sync times.
   443  		atomic.StoreUint32(&s.protocolManager.acceptTxs, 1)
   444  
   445  		go s.miner.Start(eb)
   446  	}
   447  	return nil
   448  }
   449  
   450  // StopMining terminates the miner, both at the consensus engine level as well as
   451  // at the block creation level.
   452  func (s *Ethereum) StopMining() {
   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  		th.SetThreads(-1)
   459  	}
   460  	// Stop the block creating itself
   461  	s.miner.Stop()
   462  }
   463  
   464  func (s *Ethereum) IsMining() bool      { return s.miner.Mining() }
   465  func (s *Ethereum) Miner() *miner.Miner { return s.miner }
   466  
   467  func (s *Ethereum) AccountManager() *accounts.Manager  { return s.accountManager }
   468  func (s *Ethereum) BlockChain() *core.BlockChain       { return s.blockchain }
   469  func (s *Ethereum) TxPool() *core.TxPool               { return s.txPool }
   470  func (s *Ethereum) EventMux() *event.TypeMux           { return s.eventMux }
   471  func (s *Ethereum) Engine() consensus.Engine           { return s.engine }
   472  func (s *Ethereum) ChainDb() ethdb.Database            { return s.chainDb }
   473  func (s *Ethereum) IsListening() bool                  { return true } // Always listening
   474  func (s *Ethereum) EthVersion() int                    { return int(s.protocolManager.SubProtocols[0].Version) }
   475  func (s *Ethereum) NetVersion() uint64                 { return s.networkID }
   476  func (s *Ethereum) Downloader() *downloader.Downloader { return s.protocolManager.downloader }
   477  
   478  // Protocols implements node.Service, returning all the currently configured
   479  // network protocols to start.
   480  func (s *Ethereum) Protocols() []p2p.Protocol {
   481  	if s.lesServer == nil {
   482  		return s.protocolManager.SubProtocols
   483  	}
   484  	return append(s.protocolManager.SubProtocols, s.lesServer.Protocols()...)
   485  }
   486  
   487  // Start implements node.Service, starting all internal goroutines needed by the
   488  // Ethereum protocol implementation.
   489  func (s *Ethereum) Start(srvr *p2p.Server) error {
   490  	// Start the bloom bits servicing goroutines
   491  	s.startBloomHandlers(params.BloomBitsBlocks)
   492  
   493  	// Start the RPC service
   494  	s.netRPCService = ethapi.NewPublicNetAPI(srvr, s.NetVersion())
   495  
   496  	// Figure out a max peers count based on the server limits
   497  	maxPeers := srvr.MaxPeers
   498  	if s.config.LightServ > 0 {
   499  		if s.config.LightPeers >= srvr.MaxPeers {
   500  			return fmt.Errorf("invalid peer config: light peer count (%d) >= total peer count (%d)", s.config.LightPeers, srvr.MaxPeers)
   501  		}
   502  		maxPeers -= s.config.LightPeers
   503  	}
   504  	// Start the networking layer and the light server if requested
   505  	s.protocolManager.Start(maxPeers)
   506  	if s.lesServer != nil {
   507  		s.lesServer.Start(srvr)
   508  	}
   509  	return nil
   510  }
   511  
   512  // Stop implements node.Service, terminating all internal goroutines used by the
   513  // Ethereum protocol.
   514  func (s *Ethereum) Stop() error {
   515  	s.bloomIndexer.Close()
   516  	s.blockchain.Stop()
   517  	s.engine.Close()
   518  	s.protocolManager.Stop()
   519  	if s.lesServer != nil {
   520  		s.lesServer.Stop()
   521  	}
   522  	s.txPool.Stop()
   523  	s.miner.Stop()
   524  	s.eventMux.Stop()
   525  
   526  	s.chainDb.Close()
   527  	close(s.shutdownChan)
   528  	return nil
   529  }