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