github.com/ethereum-optimism/optimism@v1.7.2/op-node/node/config.go (about)

     1  package node
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"math"
     8  	"time"
     9  
    10  	"github.com/ethereum-optimism/optimism/op-node/flags"
    11  	"github.com/ethereum-optimism/optimism/op-node/p2p"
    12  	"github.com/ethereum-optimism/optimism/op-node/rollup"
    13  	"github.com/ethereum-optimism/optimism/op-node/rollup/driver"
    14  	"github.com/ethereum-optimism/optimism/op-node/rollup/sync"
    15  	plasma "github.com/ethereum-optimism/optimism/op-plasma"
    16  	"github.com/ethereum-optimism/optimism/op-service/oppprof"
    17  	"github.com/ethereum/go-ethereum/log"
    18  )
    19  
    20  type Config struct {
    21  	L1 L1EndpointSetup
    22  	L2 L2EndpointSetup
    23  
    24  	Beacon L1BeaconEndpointSetup
    25  
    26  	Driver driver.Config
    27  
    28  	Rollup rollup.Config
    29  
    30  	// P2PSigner will be used for signing off on published content
    31  	// if the node is sequencing and if the p2p stack is enabled
    32  	P2PSigner p2p.SignerSetup
    33  
    34  	RPC RPCConfig
    35  
    36  	P2P p2p.SetupP2P
    37  
    38  	Metrics MetricsConfig
    39  
    40  	Pprof oppprof.CLIConfig
    41  
    42  	// Used to poll the L1 for new finalized or safe blocks
    43  	L1EpochPollInterval time.Duration
    44  
    45  	ConfigPersistence ConfigPersistence
    46  
    47  	// Path to store safe head database. Disabled when set to empty string
    48  	SafeDBPath string
    49  
    50  	// RuntimeConfigReloadInterval defines the interval between runtime config reloads.
    51  	// Disabled if <= 0.
    52  	// Runtime config changes should be picked up from log-events,
    53  	// but if log-events are not coming in (e.g. not syncing blocks) then the reload ensures the config stays accurate.
    54  	RuntimeConfigReloadInterval time.Duration
    55  
    56  	// Optional
    57  	Tracer    Tracer
    58  	Heartbeat HeartbeatConfig
    59  
    60  	Sync sync.Config
    61  
    62  	// To halt when detecting the node does not support a signaled protocol version
    63  	// change of the given severity (major/minor/patch). Disabled if empty.
    64  	RollupHalt string
    65  
    66  	// Cancel to request a premature shutdown of the node itself, e.g. when halting. This may be nil.
    67  	Cancel context.CancelCauseFunc
    68  
    69  	// [OPTIONAL] The reth DB path to read receipts from
    70  	RethDBPath string
    71  
    72  	// Conductor is used to determine this node is the leader sequencer.
    73  	ConductorEnabled    bool
    74  	ConductorRpc        string
    75  	ConductorRpcTimeout time.Duration
    76  
    77  	// Plasma DA config
    78  	Plasma plasma.CLIConfig
    79  }
    80  
    81  type RPCConfig struct {
    82  	ListenAddr  string
    83  	ListenPort  int
    84  	EnableAdmin bool
    85  }
    86  
    87  func (cfg *RPCConfig) HttpEndpoint() string {
    88  	return fmt.Sprintf("http://%s:%d", cfg.ListenAddr, cfg.ListenPort)
    89  }
    90  
    91  type MetricsConfig struct {
    92  	Enabled    bool
    93  	ListenAddr string
    94  	ListenPort int
    95  }
    96  
    97  func (m MetricsConfig) Check() error {
    98  	if !m.Enabled {
    99  		return nil
   100  	}
   101  
   102  	if m.ListenPort < 0 || m.ListenPort > math.MaxUint16 {
   103  		return errors.New("invalid metrics port")
   104  	}
   105  
   106  	return nil
   107  }
   108  
   109  type HeartbeatConfig struct {
   110  	Enabled bool
   111  	Moniker string
   112  	URL     string
   113  }
   114  
   115  func (cfg *Config) LoadPersisted(log log.Logger) error {
   116  	if !cfg.Driver.SequencerEnabled {
   117  		return nil
   118  	}
   119  	if state, err := cfg.ConfigPersistence.SequencerState(); err != nil {
   120  		return err
   121  	} else if state != StateUnset {
   122  		stopped := state == StateStopped
   123  		if stopped != cfg.Driver.SequencerStopped {
   124  			log.Warn(fmt.Sprintf("Overriding %v with persisted state", flags.SequencerStoppedFlag.Name), "stopped", stopped)
   125  		}
   126  		cfg.Driver.SequencerStopped = stopped
   127  	} else {
   128  		log.Info("No persisted sequencer state loaded")
   129  	}
   130  	return nil
   131  }
   132  
   133  // Check verifies that the given configuration makes sense
   134  func (cfg *Config) Check() error {
   135  	if err := cfg.L1.Check(); err != nil {
   136  		return fmt.Errorf("l2 endpoint config error: %w", err)
   137  	}
   138  	if err := cfg.L2.Check(); err != nil {
   139  		return fmt.Errorf("l2 endpoint config error: %w", err)
   140  	}
   141  	if cfg.Rollup.EcotoneTime != nil {
   142  		if cfg.Beacon == nil {
   143  			return fmt.Errorf("the Ecotone upgrade is scheduled but no L1 Beacon API endpoint is configured")
   144  		}
   145  		if err := cfg.Beacon.Check(); err != nil {
   146  			return fmt.Errorf("misconfigured L1 Beacon API endpoint: %w", err)
   147  		}
   148  	}
   149  	if err := cfg.Rollup.Check(); err != nil {
   150  		return fmt.Errorf("rollup config error: %w", err)
   151  	}
   152  	if err := cfg.Metrics.Check(); err != nil {
   153  		return fmt.Errorf("metrics config error: %w", err)
   154  	}
   155  	if err := cfg.Pprof.Check(); err != nil {
   156  		return fmt.Errorf("pprof config error: %w", err)
   157  	}
   158  	if cfg.P2P != nil {
   159  		if err := cfg.P2P.Check(); err != nil {
   160  			return fmt.Errorf("p2p config error: %w", err)
   161  		}
   162  	}
   163  	if !(cfg.RollupHalt == "" || cfg.RollupHalt == "major" || cfg.RollupHalt == "minor" || cfg.RollupHalt == "patch") {
   164  		return fmt.Errorf("invalid rollup halting option: %q", cfg.RollupHalt)
   165  	}
   166  	if cfg.ConductorEnabled {
   167  		if state, _ := cfg.ConfigPersistence.SequencerState(); state != StateUnset {
   168  			return fmt.Errorf("config persistence must be disabled when conductor is enabled")
   169  		}
   170  		if !cfg.Driver.SequencerEnabled {
   171  			return fmt.Errorf("sequencer must be enabled when conductor is enabled")
   172  		}
   173  	}
   174  	if err := cfg.Plasma.Check(); err != nil {
   175  		return fmt.Errorf("plasma config error: %w", err)
   176  	}
   177  	return nil
   178  }