github.com/dim4egster/coreth@v0.10.2/plugin/evm/config.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package evm
     5  
     6  import (
     7  	"encoding/json"
     8  	"fmt"
     9  	"time"
    10  
    11  	"github.com/dim4egster/coreth/eth"
    12  	"github.com/spf13/cast"
    13  )
    14  
    15  const (
    16  	defaultAcceptorQueueLimit                     = 64 // Provides 2 minutes of buffer (2s block target) for a commit delay
    17  	defaultPruningEnabled                         = true
    18  	defaultCommitInterval                         = 4096
    19  	defaultSyncableCommitInterval                 = defaultCommitInterval * 4
    20  	defaultSnapshotAsync                          = true
    21  	defaultRpcGasCap                              = 50_000_000 // Default to 50M Gas Limit
    22  	defaultRpcTxFeeCap                            = 100        // 100 AVAX
    23  	defaultMetricsExpensiveEnabled                = true
    24  	defaultApiMaxDuration                         = 0 // Default to no maximum API call duration
    25  	defaultWsCpuRefillRate                        = 0 // Default to no maximum WS CPU usage
    26  	defaultWsCpuMaxStored                         = 0 // Default to no maximum WS CPU usage
    27  	defaultMaxBlocksPerRequest                    = 0 // Default to no maximum on the number of blocks per getLogs request
    28  	defaultContinuousProfilerFrequency            = 15 * time.Minute
    29  	defaultContinuousProfilerMaxFiles             = 5
    30  	defaultTxRegossipFrequency                    = 1 * time.Minute
    31  	defaultTxRegossipMaxSize                      = 15
    32  	defaultOfflinePruningBloomFilterSize   uint64 = 512 // Default size (MB) for the offline pruner to use
    33  	defaultLogLevel                               = "info"
    34  	defaultLogJSONFormat                          = false
    35  	defaultPopulateMissingTriesParallelism        = 1024
    36  	defaultMaxOutboundActiveRequests              = 16
    37  	defaultStateSyncServerTrieCache               = 64 // MB
    38  
    39  	// defaultStateSyncMinBlocks is the minimum number of blocks the blockchain
    40  	// should be ahead of local last accepted to perform state sync.
    41  	// This constant is chosen so normal bootstrapping is preferred when it would
    42  	// be faster than state sync.
    43  	// time assumptions:
    44  	// - normal bootstrap processing time: ~14 blocks / second
    45  	// - state sync time: ~6 hrs.
    46  	defaultStateSyncMinBlocks = 300_000
    47  )
    48  
    49  var defaultEnabledAPIs = []string{
    50  	"eth",
    51  	"eth-filter",
    52  	"net",
    53  	"web3",
    54  	"internal-eth",
    55  	"internal-blockchain",
    56  	"internal-transaction",
    57  }
    58  
    59  type Duration struct {
    60  	time.Duration
    61  }
    62  
    63  // Config ...
    64  type Config struct {
    65  	// Coreth APIs
    66  	SnowmanAPIEnabled     bool   `json:"snowman-api-enabled"`
    67  	CorethAdminAPIEnabled bool   `json:"coreth-admin-api-enabled"`
    68  	CorethAdminAPIDir     string `json:"coreth-admin-api-dir"`
    69  
    70  	// EnabledEthAPIs is a list of Ethereum services that should be enabled
    71  	// If none is specified, then we use the default list [defaultEnabledAPIs]
    72  	EnabledEthAPIs []string `json:"eth-apis"`
    73  
    74  	// Continuous Profiler
    75  	ContinuousProfilerDir       string   `json:"continuous-profiler-dir"`       // If set to non-empty string creates a continuous profiler
    76  	ContinuousProfilerFrequency Duration `json:"continuous-profiler-frequency"` // Frequency to run continuous profiler if enabled
    77  	ContinuousProfilerMaxFiles  int      `json:"continuous-profiler-max-files"` // Maximum number of files to maintain
    78  
    79  	// Coreth API Gas/Price Caps
    80  	RPCGasCap   uint64  `json:"rpc-gas-cap"`
    81  	RPCTxFeeCap float64 `json:"rpc-tx-fee-cap"`
    82  
    83  	// Eth Settings
    84  	Preimages      bool `json:"preimages-enabled"`
    85  	SnapshotAsync  bool `json:"snapshot-async"`
    86  	SnapshotVerify bool `json:"snapshot-verification-enabled"`
    87  
    88  	// Pruning Settings
    89  	Pruning                         bool    `json:"pruning-enabled"`                    // If enabled, trie roots are only persisted every 4096 blocks
    90  	AcceptorQueueLimit              int     `json:"accepted-queue-limit"`               // Maximum blocks to queue before blocking during acceptance
    91  	CommitInterval                  uint64  `json:"commit-interval"`                    // Specifies the commit interval at which to persist EVM and atomic tries.
    92  	AllowMissingTries               bool    `json:"allow-missing-tries"`                // If enabled, warnings preventing an incomplete trie index are suppressed
    93  	PopulateMissingTries            *uint64 `json:"populate-missing-tries,omitempty"`   // Sets the starting point for re-populating missing tries. Disables re-generation if nil.
    94  	PopulateMissingTriesParallelism int     `json:"populate-missing-tries-parallelism"` // Number of concurrent readers to use when re-populating missing tries on startup.
    95  
    96  	// Metric Settings
    97  	MetricsExpensiveEnabled bool `json:"metrics-expensive-enabled"` // Debug-level metrics that might impact runtime performance
    98  
    99  	// API Settings
   100  	LocalTxsEnabled         bool     `json:"local-txs-enabled"`
   101  	APIMaxDuration          Duration `json:"api-max-duration"`
   102  	WSCPURefillRate         Duration `json:"ws-cpu-refill-rate"`
   103  	WSCPUMaxStored          Duration `json:"ws-cpu-max-stored"`
   104  	MaxBlocksPerRequest     int64    `json:"api-max-blocks-per-request"`
   105  	AllowUnfinalizedQueries bool     `json:"allow-unfinalized-queries"`
   106  	AllowUnprotectedTxs     bool     `json:"allow-unprotected-txs"`
   107  
   108  	// Keystore Settings
   109  	KeystoreDirectory             string `json:"keystore-directory"` // both absolute and relative supported
   110  	KeystoreExternalSigner        string `json:"keystore-external-signer"`
   111  	KeystoreInsecureUnlockAllowed bool   `json:"keystore-insecure-unlock-allowed"`
   112  
   113  	// Gossip Settings
   114  	RemoteTxGossipOnlyEnabled bool     `json:"remote-tx-gossip-only-enabled"`
   115  	TxRegossipFrequency       Duration `json:"tx-regossip-frequency"`
   116  	TxRegossipMaxSize         int      `json:"tx-regossip-max-size"`
   117  
   118  	// Log
   119  	LogLevel      string `json:"log-level"`
   120  	LogJSONFormat bool   `json:"log-json-format"`
   121  
   122  	// Offline Pruning Settings
   123  	OfflinePruning                bool   `json:"offline-pruning-enabled"`
   124  	OfflinePruningBloomFilterSize uint64 `json:"offline-pruning-bloom-filter-size"`
   125  	OfflinePruningDataDirectory   string `json:"offline-pruning-data-directory"`
   126  
   127  	// VM2VM network
   128  	MaxOutboundActiveRequests int64 `json:"max-outbound-active-requests"`
   129  
   130  	// Sync settings
   131  	StateSyncEnabled         bool   `json:"state-sync-enabled"`
   132  	StateSyncSkipResume      bool   `json:"state-sync-skip-resume"` // Forces state sync to use the highest available summary block
   133  	StateSyncServerTrieCache int    `json:"state-sync-server-trie-cache"`
   134  	StateSyncIDs             string `json:"state-sync-ids"`
   135  	StateSyncCommitInterval  uint64 `json:"state-sync-commit-interval"`
   136  	StateSyncMinBlocks       uint64 `json:"state-sync-min-blocks"`
   137  }
   138  
   139  // EthAPIs returns an array of strings representing the Eth APIs that should be enabled
   140  func (c Config) EthAPIs() []string {
   141  	return c.EnabledEthAPIs
   142  }
   143  
   144  func (c Config) EthBackendSettings() eth.Settings {
   145  	return eth.Settings{MaxBlocksPerRequest: c.MaxBlocksPerRequest}
   146  }
   147  
   148  func (c *Config) SetDefaults() {
   149  	c.EnabledEthAPIs = defaultEnabledAPIs
   150  	c.RPCGasCap = defaultRpcGasCap
   151  	c.RPCTxFeeCap = defaultRpcTxFeeCap
   152  	c.MetricsExpensiveEnabled = defaultMetricsExpensiveEnabled
   153  	c.APIMaxDuration.Duration = defaultApiMaxDuration
   154  	c.WSCPURefillRate.Duration = defaultWsCpuRefillRate
   155  	c.WSCPUMaxStored.Duration = defaultWsCpuMaxStored
   156  	c.MaxBlocksPerRequest = defaultMaxBlocksPerRequest
   157  	c.ContinuousProfilerFrequency.Duration = defaultContinuousProfilerFrequency
   158  	c.ContinuousProfilerMaxFiles = defaultContinuousProfilerMaxFiles
   159  	c.Pruning = defaultPruningEnabled
   160  	c.AcceptorQueueLimit = defaultAcceptorQueueLimit
   161  	c.SnapshotAsync = defaultSnapshotAsync
   162  	c.TxRegossipFrequency.Duration = defaultTxRegossipFrequency
   163  	c.TxRegossipMaxSize = defaultTxRegossipMaxSize
   164  	c.OfflinePruningBloomFilterSize = defaultOfflinePruningBloomFilterSize
   165  	c.LogLevel = defaultLogLevel
   166  	c.PopulateMissingTriesParallelism = defaultPopulateMissingTriesParallelism
   167  	c.LogJSONFormat = defaultLogJSONFormat
   168  	c.MaxOutboundActiveRequests = defaultMaxOutboundActiveRequests
   169  	c.StateSyncServerTrieCache = defaultStateSyncServerTrieCache
   170  	c.CommitInterval = defaultCommitInterval
   171  	c.StateSyncCommitInterval = defaultSyncableCommitInterval
   172  	c.StateSyncMinBlocks = defaultStateSyncMinBlocks
   173  }
   174  
   175  func (d *Duration) UnmarshalJSON(data []byte) (err error) {
   176  	var v interface{}
   177  	if err := json.Unmarshal(data, &v); err != nil {
   178  		return err
   179  	}
   180  	d.Duration, err = cast.ToDurationE(v)
   181  	return err
   182  }
   183  
   184  // String implements the stringer interface.
   185  func (d Duration) String() string {
   186  	return d.Duration.String()
   187  }
   188  
   189  // String implements the stringer interface.
   190  func (d Duration) MarshalJSON() ([]byte, error) {
   191  	return json.Marshal(d.Duration.String())
   192  }
   193  
   194  // Validate returns an error if this is an invalid config.
   195  func (c *Config) Validate() error {
   196  	if c.PopulateMissingTries != nil && (c.OfflinePruning || c.Pruning) {
   197  		return fmt.Errorf("cannot enable populate missing tries while offline pruning (enabled: %t)/pruning (enabled: %t) are enabled", c.OfflinePruning, c.Pruning)
   198  	}
   199  	if c.PopulateMissingTries != nil && c.PopulateMissingTriesParallelism < 1 {
   200  		return fmt.Errorf("cannot enable populate missing tries without at least one reader (parallelism: %d)", c.PopulateMissingTriesParallelism)
   201  	}
   202  
   203  	if !c.Pruning && c.OfflinePruning {
   204  		return fmt.Errorf("cannot run offline pruning while pruning is disabled")
   205  	}
   206  	// If pruning is enabled, the commit interval must be non-zero so the node commits state tries every CommitInterval blocks.
   207  	if c.Pruning && c.CommitInterval == 0 {
   208  		return fmt.Errorf("cannot use commit interval of 0 with pruning enabled")
   209  	}
   210  
   211  	return nil
   212  }