github.com/LampardNguyen234/go-ethereum@v1.10.16-0.20220117140830-b6a3b0260724/eth/ethconfig/config.go (about)

     1  // Copyright 2017 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 ethconfig contains the configuration of the ETH and LES protocols.
    18  package ethconfig
    19  
    20  import (
    21  	"math/big"
    22  	"os"
    23  	"os/user"
    24  	"path/filepath"
    25  	"runtime"
    26  	"time"
    27  
    28  	"github.com/LampardNguyen234/go-ethereum/common"
    29  	"github.com/LampardNguyen234/go-ethereum/consensus"
    30  	"github.com/LampardNguyen234/go-ethereum/consensus/beacon"
    31  	"github.com/LampardNguyen234/go-ethereum/consensus/clique"
    32  	"github.com/LampardNguyen234/go-ethereum/consensus/ethash"
    33  	"github.com/LampardNguyen234/go-ethereum/core"
    34  	"github.com/LampardNguyen234/go-ethereum/eth/downloader"
    35  	"github.com/LampardNguyen234/go-ethereum/eth/gasprice"
    36  	"github.com/LampardNguyen234/go-ethereum/ethdb"
    37  	"github.com/LampardNguyen234/go-ethereum/log"
    38  	"github.com/LampardNguyen234/go-ethereum/miner"
    39  	"github.com/LampardNguyen234/go-ethereum/node"
    40  	"github.com/LampardNguyen234/go-ethereum/params"
    41  )
    42  
    43  // FullNodeGPO contains default gasprice oracle settings for full node.
    44  var FullNodeGPO = gasprice.Config{
    45  	Blocks:           20,
    46  	Percentile:       60,
    47  	MaxHeaderHistory: 1024,
    48  	MaxBlockHistory:  1024,
    49  	MaxPrice:         gasprice.DefaultMaxPrice,
    50  	IgnorePrice:      gasprice.DefaultIgnorePrice,
    51  }
    52  
    53  // LightClientGPO contains default gasprice oracle settings for light client.
    54  var LightClientGPO = gasprice.Config{
    55  	Blocks:           2,
    56  	Percentile:       60,
    57  	MaxHeaderHistory: 300,
    58  	MaxBlockHistory:  5,
    59  	MaxPrice:         gasprice.DefaultMaxPrice,
    60  	IgnorePrice:      gasprice.DefaultIgnorePrice,
    61  }
    62  
    63  // Defaults contains default settings for use on the Ethereum main net.
    64  var Defaults = Config{
    65  	SyncMode: downloader.SnapSync,
    66  	Ethash: ethash.Config{
    67  		CacheDir:         "ethash",
    68  		CachesInMem:      2,
    69  		CachesOnDisk:     3,
    70  		CachesLockMmap:   false,
    71  		DatasetsInMem:    1,
    72  		DatasetsOnDisk:   2,
    73  		DatasetsLockMmap: false,
    74  	},
    75  	NetworkId:               1,
    76  	TxLookupLimit:           2350000,
    77  	LightPeers:              100,
    78  	UltraLightFraction:      75,
    79  	DatabaseCache:           512,
    80  	TrieCleanCache:          154,
    81  	TrieCleanCacheJournal:   "triecache",
    82  	TrieCleanCacheRejournal: 60 * time.Minute,
    83  	TrieDirtyCache:          256,
    84  	TrieTimeout:             60 * time.Minute,
    85  	SnapshotCache:           102,
    86  	Miner: miner.Config{
    87  		GasCeil:  8000000,
    88  		GasPrice: big.NewInt(params.GWei),
    89  		Recommit: 3 * time.Second,
    90  	},
    91  	TxPool:        core.DefaultTxPoolConfig,
    92  	RPCGasCap:     50000000,
    93  	RPCEVMTimeout: 5 * time.Second,
    94  	GPO:           FullNodeGPO,
    95  	RPCTxFeeCap:   1, // 1 ether
    96  }
    97  
    98  func init() {
    99  	home := os.Getenv("HOME")
   100  	if home == "" {
   101  		if user, err := user.Current(); err == nil {
   102  			home = user.HomeDir
   103  		}
   104  	}
   105  	if runtime.GOOS == "darwin" {
   106  		Defaults.Ethash.DatasetDir = filepath.Join(home, "Library", "Ethash")
   107  	} else if runtime.GOOS == "windows" {
   108  		localappdata := os.Getenv("LOCALAPPDATA")
   109  		if localappdata != "" {
   110  			Defaults.Ethash.DatasetDir = filepath.Join(localappdata, "Ethash")
   111  		} else {
   112  			Defaults.Ethash.DatasetDir = filepath.Join(home, "AppData", "Local", "Ethash")
   113  		}
   114  	} else {
   115  		Defaults.Ethash.DatasetDir = filepath.Join(home, ".ethash")
   116  	}
   117  }
   118  
   119  //go:generate gencodec -type Config -formats toml -out gen_config.go
   120  
   121  // Config contains configuration options for of the ETH and LES protocols.
   122  type Config struct {
   123  	// The genesis block, which is inserted if the database is empty.
   124  	// If nil, the Ethereum main net block is used.
   125  	Genesis *core.Genesis `toml:",omitempty"`
   126  
   127  	// Protocol options
   128  	NetworkId uint64 // Network ID to use for selecting peers to connect to
   129  	SyncMode  downloader.SyncMode
   130  
   131  	// This can be set to list of enrtree:// URLs which will be queried for
   132  	// for nodes to connect to.
   133  	EthDiscoveryURLs  []string
   134  	SnapDiscoveryURLs []string
   135  
   136  	NoPruning  bool // Whether to disable pruning and flush everything to disk
   137  	NoPrefetch bool // Whether to disable prefetching and only load state on demand
   138  
   139  	TxLookupLimit uint64 `toml:",omitempty"` // The maximum number of blocks from head whose tx indices are reserved.
   140  
   141  	// Whitelist of required block number -> hash values to accept
   142  	Whitelist map[uint64]common.Hash `toml:"-"`
   143  
   144  	// Light client options
   145  	LightServ          int  `toml:",omitempty"` // Maximum percentage of time allowed for serving LES requests
   146  	LightIngress       int  `toml:",omitempty"` // Incoming bandwidth limit for light servers
   147  	LightEgress        int  `toml:",omitempty"` // Outgoing bandwidth limit for light servers
   148  	LightPeers         int  `toml:",omitempty"` // Maximum number of LES client peers
   149  	LightNoPrune       bool `toml:",omitempty"` // Whether to disable light chain pruning
   150  	LightNoSyncServe   bool `toml:",omitempty"` // Whether to serve light clients before syncing
   151  	SyncFromCheckpoint bool `toml:",omitempty"` // Whether to sync the header chain from the configured checkpoint
   152  
   153  	// Ultra Light client options
   154  	UltraLightServers      []string `toml:",omitempty"` // List of trusted ultra light servers
   155  	UltraLightFraction     int      `toml:",omitempty"` // Percentage of trusted servers to accept an announcement
   156  	UltraLightOnlyAnnounce bool     `toml:",omitempty"` // Whether to only announce headers, or also serve them
   157  
   158  	// Database options
   159  	SkipBcVersionCheck bool `toml:"-"`
   160  	DatabaseHandles    int  `toml:"-"`
   161  	DatabaseCache      int
   162  	DatabaseFreezer    string
   163  
   164  	TrieCleanCache          int
   165  	TrieCleanCacheJournal   string        `toml:",omitempty"` // Disk journal directory for trie cache to survive node restarts
   166  	TrieCleanCacheRejournal time.Duration `toml:",omitempty"` // Time interval to regenerate the journal for clean cache
   167  	TrieDirtyCache          int
   168  	TrieTimeout             time.Duration
   169  	SnapshotCache           int
   170  	Preimages               bool
   171  
   172  	// Mining options
   173  	Miner miner.Config
   174  
   175  	// Ethash options
   176  	Ethash ethash.Config
   177  
   178  	// Transaction pool options
   179  	TxPool core.TxPoolConfig
   180  
   181  	// Gas Price Oracle options
   182  	GPO gasprice.Config
   183  
   184  	// Enables tracking of SHA3 preimages in the VM
   185  	EnablePreimageRecording bool
   186  
   187  	// Miscellaneous options
   188  	DocRoot string `toml:"-"`
   189  
   190  	// RPCGasCap is the global gas cap for eth-call variants.
   191  	RPCGasCap uint64
   192  
   193  	// RPCEVMTimeout is the global timeout for eth-call.
   194  	RPCEVMTimeout time.Duration
   195  
   196  	// RPCTxFeeCap is the global transaction fee(price * gaslimit) cap for
   197  	// send-transction variants. The unit is ether.
   198  	RPCTxFeeCap float64
   199  
   200  	// Checkpoint is a hardcoded checkpoint which can be nil.
   201  	Checkpoint *params.TrustedCheckpoint `toml:",omitempty"`
   202  
   203  	// CheckpointOracle is the configuration for checkpoint oracle.
   204  	CheckpointOracle *params.CheckpointOracleConfig `toml:",omitempty"`
   205  
   206  	// Arrow Glacier block override (TODO: remove after the fork)
   207  	OverrideArrowGlacier *big.Int `toml:",omitempty"`
   208  
   209  	// OverrideTerminalTotalDifficulty (TODO: remove after the fork)
   210  	OverrideTerminalTotalDifficulty *big.Int `toml:",omitempty"`
   211  }
   212  
   213  // CreateConsensusEngine creates a consensus engine for the given chain configuration.
   214  func CreateConsensusEngine(stack *node.Node, chainConfig *params.ChainConfig, config *ethash.Config, notify []string, noverify bool, db ethdb.Database) consensus.Engine {
   215  	// If proof-of-authority is requested, set it up
   216  	var engine consensus.Engine
   217  	if chainConfig.Clique != nil {
   218  		engine = clique.New(chainConfig.Clique, db)
   219  	} else {
   220  		switch config.PowMode {
   221  		case ethash.ModeFake:
   222  			log.Warn("Ethash used in fake mode")
   223  		case ethash.ModeTest:
   224  			log.Warn("Ethash used in test mode")
   225  		case ethash.ModeShared:
   226  			log.Warn("Ethash used in shared mode")
   227  		}
   228  		engine = ethash.New(ethash.Config{
   229  			PowMode:          config.PowMode,
   230  			CacheDir:         stack.ResolvePath(config.CacheDir),
   231  			CachesInMem:      config.CachesInMem,
   232  			CachesOnDisk:     config.CachesOnDisk,
   233  			CachesLockMmap:   config.CachesLockMmap,
   234  			DatasetDir:       config.DatasetDir,
   235  			DatasetsInMem:    config.DatasetsInMem,
   236  			DatasetsOnDisk:   config.DatasetsOnDisk,
   237  			DatasetsLockMmap: config.DatasetsLockMmap,
   238  			NotifyFull:       config.NotifyFull,
   239  		}, notify, noverify)
   240  		engine.(*ethash.Ethash).SetThreads(-1) // Disable CPU mining
   241  	}
   242  	return beacon.New(engine)
   243  }