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