github.com/electroneum/electroneum-sc@v0.0.0-20230105223411-3bc1d078281e/eth/ethconfig/config.go (about)

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