code.vegaprotocol.io/vega@v0.79.0/core/protocol/protocol.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package protocol
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  
    22  	"code.vegaprotocol.io/vega/core/api"
    23  	"code.vegaprotocol.io/vega/core/blockchain"
    24  	"code.vegaprotocol.io/vega/core/broker"
    25  	ethclient "code.vegaprotocol.io/vega/core/client/eth"
    26  	"code.vegaprotocol.io/vega/core/config"
    27  	"code.vegaprotocol.io/vega/core/evtforward"
    28  	"code.vegaprotocol.io/vega/core/netparams"
    29  	"code.vegaprotocol.io/vega/core/nodewallets"
    30  	"code.vegaprotocol.io/vega/core/processor"
    31  	"code.vegaprotocol.io/vega/core/protocolupgrade"
    32  	"code.vegaprotocol.io/vega/core/spam"
    33  	"code.vegaprotocol.io/vega/core/stats"
    34  	"code.vegaprotocol.io/vega/core/vegatime"
    35  	"code.vegaprotocol.io/vega/libs/subscribers"
    36  	"code.vegaprotocol.io/vega/logging"
    37  	"code.vegaprotocol.io/vega/paths"
    38  
    39  	"github.com/blang/semver"
    40  )
    41  
    42  var Version = semver.MustParse("0.1.0")
    43  
    44  type Protocol struct {
    45  	*processor.App
    46  
    47  	log *logging.Logger
    48  
    49  	confWatcher     *config.Watcher
    50  	confListenerIDs []int
    51  
    52  	services *allServices
    53  }
    54  
    55  const namedLogger = "protocol"
    56  
    57  func New(
    58  	ctx context.Context,
    59  	confWatcher *config.Watcher,
    60  	log *logging.Logger,
    61  	cancel func(),
    62  	stopBlockchain func() error,
    63  	nodewallets *nodewallets.NodeWallets,
    64  	primaryEthClient *ethclient.PrimaryClient,
    65  	secondaryEthClient *ethclient.SecondaryClient,
    66  	primaryEthConfirmation *ethclient.EthereumConfirmations,
    67  	secondaryEthConfirmation *ethclient.EthereumConfirmations,
    68  	blockchainClient *blockchain.Client,
    69  	vegaPaths paths.Paths,
    70  	stats *stats.Stats,
    71  	l2Clients *ethclient.L2Clients,
    72  ) (proto *Protocol, err error) {
    73  	log = log.Named(namedLogger)
    74  
    75  	defer func() {
    76  		if err != nil {
    77  			log.Error("unable to start protocol", logging.Error(err))
    78  			return
    79  		}
    80  
    81  		ids := proto.confWatcher.OnConfigUpdateWithID(
    82  			func(cfg config.Config) { proto.ReloadConf(cfg.Processor) },
    83  		)
    84  		proto.confListenerIDs = ids
    85  	}()
    86  
    87  	svcs, err := newServices(
    88  		ctx,
    89  		log,
    90  		confWatcher,
    91  		nodewallets,
    92  		primaryEthClient,
    93  		secondaryEthClient,
    94  		primaryEthConfirmation,
    95  		secondaryEthConfirmation,
    96  		blockchainClient,
    97  		vegaPaths,
    98  		stats,
    99  		l2Clients,
   100  	)
   101  	if err != nil {
   102  		return nil, err
   103  	}
   104  
   105  	proto = &Protocol{
   106  		App: processor.NewApp(
   107  			log,
   108  			svcs.vegaPaths,
   109  			confWatcher.Get().Processor,
   110  			cancel,
   111  			stopBlockchain,
   112  			svcs.assets,
   113  			svcs.banking,
   114  			svcs.broker,
   115  			svcs.witness,
   116  			svcs.primaryEventForwarder,
   117  			svcs.forwarderHeartbeat,
   118  			svcs.executionEngine,
   119  			svcs.genesisHandler,
   120  			svcs.governance,
   121  			svcs.notary,
   122  			svcs.stats.Blockchain,
   123  			svcs.timeService,
   124  			svcs.epochService,
   125  			svcs.topology,
   126  			svcs.netParams,
   127  			&processor.Oracle{
   128  				Engine:                    svcs.oracle,
   129  				Adaptors:                  svcs.oracleAdaptors,
   130  				EthereumOraclesVerifier:   svcs.ethereumOraclesVerifier,
   131  				EthereumL2OraclesVerifier: svcs.l2Verifiers,
   132  			},
   133  			svcs.delegation,
   134  			svcs.limits,
   135  			svcs.stakeVerifier,
   136  			svcs.checkpoint,
   137  			svcs.spam,
   138  			svcs.pow,
   139  			svcs.stakingAccounts,
   140  			svcs.snapshotEngine,
   141  			svcs.statevar,
   142  			svcs.teamsEngine,
   143  			svcs.referralProgram,
   144  			svcs.volumeDiscount,
   145  			svcs.volumeRebate,
   146  			svcs.blockchainClient,
   147  			svcs.primaryMultisig,
   148  			svcs.secondaryMultisig,
   149  			stats.GetVersion(),
   150  			svcs.protocolUpgradeEngine,
   151  			svcs.codec,
   152  			svcs.gastimator,
   153  			svcs.ethCallEngine,
   154  			svcs.collateral,
   155  			svcs.partiesEngine,
   156  			svcs.txCache,
   157  		),
   158  		log:         log,
   159  		confWatcher: confWatcher,
   160  		services:    svcs,
   161  	}
   162  
   163  	proto.services.netParams.Watch(
   164  		netparams.WatchParam{
   165  			Param:   netparams.SpamProtectionMaxBatchSize,
   166  			Watcher: proto.App.OnSpamProtectionMaxBatchSizeUpdate,
   167  		},
   168  		netparams.WatchParam{
   169  			Param:   netparams.BlockchainsPrimaryEthereumConfig,
   170  			Watcher: proto.App.OnBlockchainPrimaryEthereumConfigUpdate,
   171  		},
   172  		netparams.WatchParam{
   173  			Param:   netparams.BlockchainsEVMBridgeConfigs,
   174  			Watcher: proto.App.OnBlockchainEVMChainConfigUpdate,
   175  		},
   176  	)
   177  
   178  	return proto, nil
   179  }
   180  
   181  // Start will start the protocol, this means it's ready to process
   182  // blocks from the blockchain.
   183  func (n *Protocol) Start(ctx context.Context) error {
   184  	if err := n.services.snapshotEngine.Start(ctx); err != nil {
   185  		return fmt.Errorf("could not start the snapshot engine: %w", err)
   186  	}
   187  	return nil
   188  }
   189  
   190  // Stop will stop all services of the protocol.
   191  func (n *Protocol) Stop() error {
   192  	// unregister conf listeners
   193  	n.log.Info("Stopping protocol services")
   194  	n.confWatcher.Unregister(n.confListenerIDs)
   195  	n.services.Stop()
   196  	return nil
   197  }
   198  
   199  func (n *Protocol) Protocol() semver.Version {
   200  	return Version
   201  }
   202  
   203  func (n *Protocol) GetEventForwarder() *evtforward.Forwarder {
   204  	return n.services.primaryEventForwarder
   205  }
   206  
   207  func (n *Protocol) GetTimeService() *vegatime.Svc {
   208  	return n.services.timeService
   209  }
   210  
   211  func (n *Protocol) GetEventService() *subscribers.Service {
   212  	return n.services.eventService
   213  }
   214  
   215  func (n *Protocol) GetBroker() *broker.Broker {
   216  	return n.services.broker
   217  }
   218  
   219  func (n *Protocol) GetPoW() api.ProofOfWorkParams {
   220  	return n.services.pow
   221  }
   222  
   223  func (n *Protocol) GetProtocolUpgradeService() *protocolupgrade.Engine {
   224  	return n.services.protocolUpgradeEngine
   225  }
   226  
   227  func (n *Protocol) GetSpamEngine() *spam.Engine {
   228  	return n.services.spam
   229  }
   230  
   231  func (n *Protocol) GetPowEngine() processor.PoWEngine {
   232  	return n.services.pow
   233  }